You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tanruixiang (via GitHub)" <gi...@apache.org> on 2023/05/30 13:32:42 UTC

[GitHub] [arrow-datafusion] tanruixiang opened a new pull request, #6487: Support simplifying expressions like `~ ^(bar|foo)$`

tanruixiang opened a new pull request, #6487:
URL: https://github.com/apache/arrow-datafusion/pull/6487

   # 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 #.
   
   # 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.  
   -->
   Support simplifying expressions like  `~  ^(bar|foo)$` ,  This will help the subsequent optimizer to perform related optimizations such as predicate push-down.
   # 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.
   -->
   Optimization for regular expressions of this form is supported in the existing simplify_regex_expr.
   # 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)?
   -->
   Add test case in `expr_simplifier.rs`
   # 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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   @jackwener  Thank you very much, we also found this messy order issue and were about to fix the discovery, but we didn't expect that it was already done by #6414 .


-- 
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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   @alamb Hi, we have resolved the testing issue.


-- 
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] jackwener commented on a diff in pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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


##########
datafusion/optimizer/src/simplify_expressions/regex.rs:
##########
@@ -166,6 +165,40 @@ fn is_anchored_literal(v: &[Hir]) -> bool {
         .all(|h| matches!(h.kind(), HirKind::Literal(_)))
 }
 
+/// returns true if the elements in a `Concat` pattern are:
+/// - `[Look::Start, Capture(Alternation(Literals...)), Look::End]`
+fn is_anchored_capture(v: &[Hir]) -> bool {
+    if 3 != v.len() {
+        return false;
+    }
+
+    let first_last = (
+        v.first().expect("length checked"),
+        v.last().expect("length checked"),
+    );
+    if !matches!(first_last,
+    (s, e) if s.kind() == &HirKind::Look(Look::Start)
+        && e.kind() == &HirKind::Look(Look::End)
+         )
+    {
+        return false;
+    }

Review Comment:
   ```suggestion
       if v.len() != 3
           || !matches!((v.first().unwrap().kind(), v.last().unwrap().kind()),
                    (&HirKind::Look(Look::Start), &HirKind::Look(Look::End)))
       {
           return false;
       }
   ```



-- 
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] jackwener commented on a diff in pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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


##########
datafusion/optimizer/src/simplify_expressions/regex.rs:
##########
@@ -166,6 +165,40 @@ fn is_anchored_literal(v: &[Hir]) -> bool {
         .all(|h| matches!(h.kind(), HirKind::Literal(_)))
 }
 
+/// returns true if the elements in a `Concat` pattern are:
+/// - `[Look::Start, Capture(Alternation(Literals...)), Look::End]`
+fn is_anchored_capture(v: &[Hir]) -> bool {
+    if 3 != v.len() {
+        return false;
+    }
+
+    let first_last = (
+        v.first().expect("length checked"),
+        v.last().expect("length checked"),
+    );
+    if !matches!(first_last,
+    (s, e) if s.kind() == &HirKind::Look(Look::Start)
+        && e.kind() == &HirKind::Look(Look::End)
+         )
+    {
+        return false;
+    }

Review Comment:
   ```suggestion
       if v.len() != 3
           || !matches!(
               (v.first().unwrap().kind(), v.last().unwrap().kind()),
               (&HirKind::Look(Look::Start), &HirKind::Look(Look::End))
           )
       {
           return false;
       }
   ```



-- 
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 #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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


-- 
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] jackwener commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   Because your branch is `main`, so I can't push my patch into it.
   ```
    ! [remote rejected]     tanruixiang/main -> tanruixiang/main (permission denied)
   error: failed to push some refs to 'https://github.com/tanruixiang/arrow-datafusion.git'
   ```
   
   Please rebase to latest code.
   And add this patch.
   
   ```
   Subject: [PATCH] fix unit test
   ---
   Index: datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
   IDEA additional info:
   Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
   <+>UTF-8
   ===================================================================
   diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
   --- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs	(revision 1afab5c652b0273e3517bc5d5089e465c1983166)
   +++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs	(revision c9d56044a9457d8bd0cb59cda4e6895bb56492be)
   @@ -2482,13 +2482,13 @@
            // regular expressions that match exact captured literals
            assert_change(
                regex_match(col("c1"), lit("^(foo|bar)$")),
   -            col("c1").eq(lit("bar")).or(col("c1").eq(lit("foo"))),
   +            col("c1").eq(lit("foo")).or(col("c1").eq(lit("bar"))),
            );
            assert_change(
                regex_not_match(col("c1"), lit("^(foo|bar)$")),
                col("c1")
   -                .not_eq(lit("bar"))
   -                .and(col("c1").not_eq(lit("foo"))),
   +                .not_eq(lit("foo"))
   +                .and(col("c1").not_eq(lit("bar"))),
            );
            assert_change(
                regex_match(col("c1"), lit("^(foo)$")),
   @@ -2497,8 +2497,9 @@
            assert_change(
                regex_match(col("c1"), lit("^(foo|bar|baz)$")),
                col("c1")
   -                .eq(lit("baz"))
   -                .or((col("c1").eq(lit("bar"))).or(col("c1").eq(lit("foo")))),
   +                .eq(lit("foo"))
   +                .or(col("c1").eq(lit("bar")))
   +                .or(col("c1").eq(lit("baz"))),
            );
            assert_change(
                regex_match(col("c1"), lit("^(foo|bar|baz|qux)$")),
   
   ```


-- 
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] jackwener commented on a diff in pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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


##########
datafusion/optimizer/src/simplify_expressions/regex.rs:
##########
@@ -166,6 +165,31 @@ fn is_anchored_literal(v: &[Hir]) -> bool {
         .all(|h| matches!(h.kind(), HirKind::Literal(_)))
 }
 
+/// returns true if the elements in a `Concat` pattern are:
+/// - `[Look::Start, Capture(Alternation(Literals...)), Look::End]`
+fn is_anchored_capture(v: &[Hir]) -> bool {
+    if v.len() != 3
+        || !matches!((v.first().unwrap().kind(), v.last().unwrap().kind()),
+                 (&HirKind::Look(Look::Start), &HirKind::Look(Look::End)))
+    {
+        return false;
+    }

Review Comment:
   ```suggestion
       if v.len() != 3
           || !matches!(
               (v.first().unwrap().kind(), v.last().unwrap().kind()),
               (&HirKind::Look(Look::Start), &HirKind::Look(Look::End))
           )
       {
           return false;
       }
   ```



-- 
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 #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -2448,6 +2448,16 @@ mod tests {
             regex_not_match(col("c1"), lit("^foo$")),
             col("c1").not_eq(lit("foo")),
         );
+        assert_change(

Review Comment:
   Could you also add some tests for cases like the following as a way to try and get some more test coverage on the regexp matching code?
   
   1. `"(foo|bar)"` (expect no rewrite)
   1. `"(foo|bar)*"` (expect no rewrite)
   1. `"^(foo|bar)*"` (expect no rewrite)
   2. `"^(foo)$"`
   3. "^(foo|bar|baz)$"
   
   



-- 
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] jackwener commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   I pull latest code, I find this PR test fail due to #6414 
   


-- 
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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   cc @jiacai2050 


-- 
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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   Thank you very much for the review. Strange error reported, I'll look into what happened tomorrow.
   ```
   thread 'simplify_expressions::expr_simplifier::tests::test_simplify_regex' panicked at 'assertion failed: `(left == right)`
     left: `c1 = Utf8("baz") OR c1 = Utf8("bar") OR c1 = Utf8("foo")`,
    right: `c1 = Utf8("baz") OR c1 = Utf8("bar") OR c1 = Utf8("foo")`', datafusion\optimizer\src\simplify_expressions\expr_simplifier.rs:2471:9
   ```


-- 
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 #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   Thank you @tanruixiang  and @jackwener !


-- 
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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   @alamb  Thank you for the review and the reminder. The CI has passed after merging the main branches.


-- 
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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   > sorry, delete a comment by mistake.
   
   Don't worry, here are the comments you deleted by mistake.
   > @jackwener  Thank you very much, we also found this messy order issue and were about to fix the discovery, but we didn't expect that it was already done by #6414 .


-- 
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 #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   I think this PR will pass CI if you merge up from main now (to get the fix for https://github.com/apache/arrow-datafusion/issues/6495)


-- 
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] tanruixiang commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   @jackwener  Thank you very much, we also found this messy order issue and were about to fix the discovery, but we didn't expect that it was already done by #6414 .


-- 
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] jackwener commented on pull request #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   sorry, delete a comment by mistake.


-- 
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 #6487: Support simplifying expressions like `~ ^(bar|foo)$`

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

   The test failures are unrelated to this PR: https://github.com/apache/arrow-datafusion/issues/6495
   
   Hopefully we'll get that fixed today and get this PR merged as well


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