Expand description
Unwrap casts in binary comparisons
The functions in this module attempt to remove casts from
comparisons to literals (ScalarValues) by applying the casts
to the literals if possible. It is inspired by the optimizer rule
UnwrapCastInBinaryComparison of Spark.
Removing casts often improves performance because:
- The cast is done once (to the literal) rather than to every value
- Can enable other optimizations such as predicate pushdown that donโt support casting
The rule is applied to expressions of the following forms:
cast(left_expr as data_type) comparison_op literal_exprliteral_expr comparison_op cast(left_expr as data_type)cast(literal_expr) IN (expr1, expr2, ...)literal_expr IN (cast(expr1) , cast(expr2), ...)
If the expression matches one of the forms above, the rule will
ensure the value of literal is in range(min, max) of the
exprโs data_type, and if the scalar is within range, the literal
will be casted to the data type of expr on the other side, and the
cast will be removed from the other side.
ยงExample
If the DataType of c1 is INT32. Given the filter
cast(c1 as INT64) > INT64(10)`This rule will remove the cast and rewrite the expression to:
c1 > INT32(10)