You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/02 19:28:25 UTC

[GitHub] [arrow-datafusion] Dandandan opened a new pull request, #4090: Simplify small InListExpr

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

   # 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 #4089
   
   # 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 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 commented on pull request #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#issuecomment-1302655233

   > Looks great! One question I had was whether it makes sense to change THRESHOLD_INLINE_INLIST to be a configurable proper
   
   I would recommend putting this on `ConfigOptions` if you make it configurable


-- 
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] Dandandan commented on a diff in pull request #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
Dandandan commented on code in PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#discussion_r1013335651


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,32 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            Expr::InList {
+                expr: _expr,
+                list,
+                negated,
+            } if list.is_empty() => lit(negated),
+
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.len() <= THRESHOLD_INLINE_INLIST && expr.try_into_col().is_ok() => {
+                let first_val = list[0].clone();
+                if negated {
+                    list.into_iter()
+                        .skip(1)
+                        .fold((*expr.clone()).not_eq(first_val), |acc, y| {
+                            (*expr.clone()).not_eq(y.clone()).and(acc)
+                        })
+                } else {
+                    list.into_iter()
+                        .skip(1)
+                        .fold((*expr.clone()).eq(first_val), |acc, y| {
+                            (*expr.clone()).eq(y.clone()).or(acc)
+                        })
+                }

Review Comment:
   Unfortunately I don't think it's possible (this was my first try as well), as we also need to transform the first `acc` value.
   Here the first acc won't be of `expr.eq(acc)` but just `acc`.



-- 
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] isidentical commented on a diff in pull request #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
isidentical commented on code in PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#discussion_r1013349637


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,38 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            // expr IN () --> false
+            // expr NOT IN () --> true
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.is_empty() && *expr != Expr::Literal(ScalarValue::Null) => {
+                lit(negated)
+            }
+
+            // if expr is a single column reference:
+            // expr IN (A, B, ...) --> (expr = A) OR (expr = B) OR (expr = C)
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.len() == 1

Review Comment:
   Took me a while to understand why the length of 1 is special-cased. Maybe we could mention that the column reference check is strictly for ensuring that we are not doing an unnecessary evaluation of the left side over and over again (so a length of 1 is always fine or if it is something simple as a column access then it is also 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: 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 #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#issuecomment-1303629234

   👏 


-- 
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] Dandandan merged pull request #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
Dandandan merged PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090


-- 
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] ursabot commented on pull request #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#issuecomment-1303612014

   Benchmark runs are scheduled for baseline = 7e944ede86457fe0f43be44e0e5550229ecaf008 and contender = 60f3ef692298082d8c209be5cfc4042ceee45d8a. 60f3ef692298082d8c209be5cfc4042ceee45d8a is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/f2c8f02dad03483a88642b1103b9e9cd...8689c58582ef4ce1a64189f1ec18a529/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/2d8fe4b7e54a4bb2960e62f238ab512c...0b4f231cb05c48aaa531c77d5dcea29a/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/2d346acedb844609b8dd7b72d7d1984a...ee9ff26cc18643189b561cdb824cc14a/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/23b1afddf6c340b2a086ca0fca95f37d...5bc620a131db4bdcb82571f09deb1b11/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


-- 
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 #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#discussion_r1013288612


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,32 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            Expr::InList {
+                expr: _expr,
+                list,
+                negated,
+            } if list.is_empty() => lit(negated),

Review Comment:
   I don't know how important this is, but this only holds if `expr` is non null 🤔 
   
   ```
   postgres=# select null in (1);
    ?column? 
   ----------
    
   (1 row)
   ```
   
   However, note that you can't even make an empty inlist in postgres so I don't know how much this edge case matters
   
   ```sql
   postgres=# select null in ();
   ERROR:  syntax error at or near ")"
   LINE 1: select null in ();
   ```



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,32 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            Expr::InList {

Review Comment:
   ```suggestion
               // expr IN () --> false
               // expr NOT IN () --> true
               Expr::InList {
   ```



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,32 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            Expr::InList {
+                expr: _expr,
+                list,
+                negated,
+            } if list.is_empty() => lit(negated),
+
+            Expr::InList {

Review Comment:
   ```suggestion
               // if expr is a single column reference:
               // expr IN (A, B, ...) --> (expr = A) OR (expr = B) OR (expr = C)
               Expr::InList {
   ```
   
   



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,32 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            Expr::InList {
+                expr: _expr,
+                list,
+                negated,
+            } if list.is_empty() => lit(negated),
+
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.len() <= THRESHOLD_INLINE_INLIST && expr.try_into_col().is_ok() => {

Review Comment:
   I recommend also adding an explicit case for `list.len() == 1` (without `expr.try_into_col()`)



##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,32 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            Expr::InList {
+                expr: _expr,
+                list,
+                negated,
+            } if list.is_empty() => lit(negated),
+
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.len() <= THRESHOLD_INLINE_INLIST && expr.try_into_col().is_ok() => {
+                let first_val = list[0].clone();
+                if negated {
+                    list.into_iter()
+                        .skip(1)
+                        .fold((*expr.clone()).not_eq(first_val), |acc, y| {
+                            (*expr.clone()).not_eq(y.clone()).and(acc)
+                        })
+                } else {
+                    list.into_iter()
+                        .skip(1)
+                        .fold((*expr.clone()).eq(first_val), |acc, y| {
+                            (*expr.clone()).eq(y.clone()).or(acc)
+                        })
+                }

Review Comment:
   I think you can simplify this code using `reduce`:
   
   ```suggestion
                   if negated {
                       list.into_iter()
                           .reduce(|acc, y| {
                               (*expr.clone()).not_eq(y.clone()).and(acc)
                           }).unwrap()
                   } else {
                       list.into_iter()
                           .reduce(|acc, y| {
                               (*expr.clone()).eq(y.clone()).or(acc)
                           }).unwrap()
                   }
   ```



-- 
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 #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#discussion_r1013390818


##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -365,6 +367,38 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
                     None => lit_bool_null(),
                 }
             }
+            // expr IN () --> false
+            // expr NOT IN () --> true
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.is_empty() && *expr != Expr::Literal(ScalarValue::Null) => {
+                lit(negated)
+            }
+
+            // if expr is a single column reference:
+            // expr IN (A, B, ...) --> (expr = A) OR (expr = B) OR (expr = C)
+            Expr::InList {
+                expr,
+                list,
+                negated,
+            } if list.len() == 1

Review Comment:
   I agree documenting the rationale would be super helpful



-- 
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 #4090: Simplify small InListExpr

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4090:
URL: https://github.com/apache/arrow-datafusion/pull/4090#issuecomment-1302527608

   Until the fix for https://github.com/apache/arrow-datafusion/issues/4100 is merged, clippy will be failing on this PR 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