findepi commented on code in PR #13061:
URL: https://github.com/apache/datafusion/pull/13061#discussion_r1827434320


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1470,19 +1470,25 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for 
Simplifier<'a, S> {
             }) => Transformed::yes(simplify_regex_expr(left, op, right)?),
 
             // Rules for Like
-            Expr::Like(Like {
-                expr,
-                pattern,
-                negated,
-                escape_char: _,
-                case_insensitive: _,
-            }) if !is_null(&expr)
-                && matches!(
-                    pattern.as_ref(),
-                    Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) if 
pattern_str == "%"
-                ) =>
-            {
-                Transformed::yes(lit(!negated))
+            Expr::Like(ref like_expr) => {
+                if let Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) =
+                    like_expr.pattern.as_ref()
+                {
+                    if !is_null(&like_expr.expr) && pattern_str == "%" {
+                        Transformed::yes(lit(!like_expr.negated))
+                    } else if !pattern_str.contains(['%', '_'].as_ref()) {
+                        // If the pattern does not contain any wildcards, we 
can simplify the like expression to an equality expression

Review Comment:
   Add TODO to handle escaped search characters in a follow-up.
   Escaped wildcards are not uncommon in certain scenarios - eg correct use of 
JDBC DatabaseMetaData will produce LIKE `foo\_bar` when inspecting object 
`foo_bar`.



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1470,19 +1470,25 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for 
Simplifier<'a, S> {
             }) => Transformed::yes(simplify_regex_expr(left, op, right)?),
 
             // Rules for Like
-            Expr::Like(Like {
-                expr,
-                pattern,
-                negated,
-                escape_char: _,
-                case_insensitive: _,
-            }) if !is_null(&expr)
-                && matches!(
-                    pattern.as_ref(),
-                    Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) if 
pattern_str == "%"
-                ) =>
-            {
-                Transformed::yes(lit(!negated))
+            Expr::Like(ref like_expr) => {
+                if let Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) =
+                    like_expr.pattern.as_ref()
+                {
+                    if !is_null(&like_expr.expr) && pattern_str == "%" {
+                        Transformed::yes(lit(!like_expr.negated))

Review Comment:
   I don't think this is correct. Did you consider 
https://github.com/apache/datafusion/pull/13061#discussion_r1824014515 ?



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3632,6 +3638,16 @@ mod tests {
 
         let expr = not_ilike(null, "%");
         assert_eq!(simplify(expr), lit_bool_null());
+
+        // test cases that get converted to equality
+        let expr = like(col("c1"), "a");
+        assert_eq!(simplify(expr), col("c1").eq(lit("a")));
+        let expr = not_like(col("c1"), "a");
+        assert_eq!(simplify(expr), col("c1").not_eq(lit("a")));
+        let expr = like(col("c1"), "a_");
+        assert_eq!(simplify(expr), col("c1").like(lit("a_")));
+        let expr = not_like(col("c1"), "a_");
+        assert_eq!(simplify(expr), col("c1").not_like(lit("a_")));

Review Comment:
   add a test cases where pattern is constant NULL (it won't be simplified 
today, that's fine)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to