Apache DataFusion SQL Query Engine
feat: Add expression rewrite rules for LIKE and ILIKE (#5819)
Alex Huang9150d9539534
diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rsindex d58a6ee0c3..5ef672c90b 100644--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs@@ -30,7 +30,8 @@ use arrow::{use datafusion_common::tree_node::{RewriteRecursion, TreeNode, TreeNodeRewriter};use datafusion_common::{DFSchema, DFSchemaRef, DataFusionError, Result, ScalarValue};use datafusion_expr::{- and, lit, or, BinaryExpr, BuiltinScalarFunction, ColumnarValue, Expr, Volatility,+ and, lit, or, BinaryExpr, BuiltinScalarFunction, ColumnarValue, Expr, Like,+ Volatility,};use datafusion_physical_expr::{create_physical_expr, execution_props::ExecutionProps};@@ -1130,6 +1131,36 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {right,}) => simplify_regex_expr(left, op, right)?,+ // Rules for Like+ Expr::Like(Like {+ expr,+ pattern,+ negated,+ escape_char: _,+ }) if !is_null(&expr)+ && matches!(+ pattern.as_ref(),+ Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) if pattern_str == "%"+ ) =>+ {+ lit(!negated)+ }++ // Rules for ILike+ Expr::ILike(Like {+ expr,+ pattern,+ negated,+ escape_char: _,+ }) if !is_null(&expr)+ && matches!(+ pattern.as_ref(),+ Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) if pattern_str == "%"+ ) =>+ {+ lit(!negated)+ }+// no additional rewrites possibleexpr => expr,};@@ -2394,16 +2425,10 @@ mod tests {assert_no_change(regex_match(col("c1"), lit("f_o")));// empty cases- assert_change(regex_match(col("c1"), lit("")), like(col("c1"), "%"));- assert_change(- regex_not_match(col("c1"), lit("")),- not_like(col("c1"), "%"),- );- assert_change(regex_imatch(col("c1"), lit("")), ilike(col("c1"), "%"));- assert_change(- regex_not_imatch(col("c1"), lit("")),- not_ilike(col("c1"), "%"),- );+ assert_change(regex_match(col("c1"), lit("")), lit(true));+ assert_change(regex_not_match(col("c1"), lit("")), lit(false));+ assert_change(regex_imatch(col("c1"), lit("")), lit(true));+ assert_change(regex_not_imatch(col("c1"), lit("")), lit(false));// single characterassert_change(regex_match(col("c1"), lit("x")), like(col("c1"), "%x%"));@@ -2424,12 +2449,6 @@ mod tests {.or(like(col("c1"), "%x%")).or(like(col("c1"), "%baz%")),);- assert_change(- regex_match(col("c1"), lit("foo||baz")),- like(col("c1"), "%foo%")- .or(like(col("c1"), "%"))- .or(like(col("c1"), "%baz%")),- );assert_change(regex_not_match(col("c1"), lit("foo|bar|baz")),not_like(col("c1"), "%foo%")@@ -2925,4 +2944,34 @@ mod tests {or(col("c2").lt(lit(3)), col("c2").gt(lit(4))));}++ #[test]+ fn test_like_and_ilke() {+ // test non-null values+ let expr = like(col("c1"), "%");+ assert_eq!(simplify(expr), lit(true));++ let expr = not_like(col("c1"), "%");+ assert_eq!(simplify(expr), lit(false));++ let expr = ilike(col("c1"), "%");+ assert_eq!(simplify(expr), lit(true));++ let expr = not_ilike(col("c1"), "%");+ assert_eq!(simplify(expr), lit(false));++ // test null values+ let null = lit(ScalarValue::Utf8(None));+ let expr = like(null.clone(), "%");+ assert_eq!(simplify(expr), lit_bool_null());++ let expr = not_like(null.clone(), "%");+ assert_eq!(simplify(expr), lit_bool_null());++ let expr = ilike(null.clone(), "%");+ assert_eq!(simplify(expr), lit_bool_null());++ let expr = not_ilike(null, "%");+ assert_eq!(simplify(expr), lit_bool_null());+ }}