Module unwrap_cast

Module unwrap_cast 

Source
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:

  1. The cast is done once (to the literal) rather than to every value
  2. Can enable other optimizations such as predicate pushdown that donโ€™t support casting

The rule is applied to expressions of the following forms:

  1. cast(left_expr as data_type) comparison_op literal_expr
  2. literal_expr comparison_op cast(left_expr as data_type)
  3. cast(literal_expr) IN (expr1, expr2, ...)
  4. 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)

Functionsยง

cast_literal_to_type_with_op ๐Ÿ”’
Specifically, rewrites
is_cast_expr_and_support_unwrap_cast_in_comparison_for_binary ๐Ÿ”’
is_cast_expr_and_support_unwrap_cast_in_comparison_for_inlist ๐Ÿ”’
unwrap_cast_in_comparison_for_binary ๐Ÿ”’