You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Weijun-H (via GitHub)" <gi...@apache.org> on 2023/03/31 22:16:37 UTC

[GitHub] [arrow-datafusion] Weijun-H opened a new pull request, #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Weijun-H opened a new pull request, #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #5817
   
   # Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb merged pull request #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819#discussion_r1156145218


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -2320,16 +2337,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"), "%"));

Review Comment:
   I double checked that datafusion got the right answer, both before and after this PR:
   
   ```shell
   ❯ select regexp_match('foo', '');
   +-----------------------------------+
   | regexpmatch(Utf8("foo"),Utf8("")) |
   +-----------------------------------+
   | []                                |
   +-----------------------------------+
   1 row in set. Query took 0.000 seconds.
   ```
   
   So things look ok



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819#discussion_r1156136411


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1118,6 +1119,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 == "%"

Review Comment:
   👍 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on pull request #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819#issuecomment-1494566679

   Thanks again @Weijun-H 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819#discussion_r1155093800


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1118,6 +1119,22 @@ 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) && pattern.eq(&Box::new(lit("%"))) => lit(!negated),

Review Comment:
   By writing `Box::new(...)` I think this is goign to do two allocations each time (one for the Box and one for the String in the literal)
   
   Would it be possible to rewrite this to a match statement without allocation?
   
   ```rust
   matches!(pattern.as_ref(), Expression::Literal(ScalarValue::Utf8(..)))
   ```
   ?
   
   You might have to define some better way to check for the literal scalar



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -2320,16 +2337,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"), "%"));

Review Comment:
   🤔 the original rewrite rule looks incorrect to me (as in the orignal rule shouldn't be rewriting regexp match). We can fix this in some other PR
   
   ```sql
   postgres=# select regexp_match('foo', '');
    regexp_match
   --------------
    {""}
   (1 row)
   
   postgres=# select like('foo', '%');
    like
   ------
    t
   (1 row)
   ```



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -2851,4 +2838,70 @@ 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 = Expr::Like(Like::new(
+            false,
+            Box::new(col("c1")),
+            Box::new(lit("%")),
+            None,
+        ));

Review Comment:
   What do you think about using the expr_fn's style for creating `like` https://docs.rs/datafusion/latest/datafusion/prelude/enum.Expr.html#method.like
   
   ```suggestion
           let expr = like(col("c1"), lit("%"));
   ```
   
   I think that would make it clearer what cases these tests were covering



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] Weijun-H commented on a diff in pull request #5819: feat: Add expression rewrite rules for LIKE and ILIKE

Posted by "Weijun-H (via GitHub)" <gi...@apache.org>.
Weijun-H commented on code in PR #5819:
URL: https://github.com/apache/arrow-datafusion/pull/5819#discussion_r1155105069


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -2851,4 +2838,70 @@ 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 = Expr::Like(Like::new(
+            false,
+            Box::new(col("c1")),
+            Box::new(lit("%")),
+            None,
+        ));

Review Comment:
   Yes, it is a better way. I forgot to use it 😄



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org