You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "petern48 (via GitHub)" <gi...@apache.org> on 2025/09/16 17:41:47 UTC
[PR] perf: Simplify CASE when any TRUE [datafusion]
petern48 opened a new pull request, #17602:
URL: https://github.com/apache/datafusion/pull/17602
## 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 #17589
## 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.
-->
Improves perf
## 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.
-->
Extends the case simplification by simplifying the expr when any of the when statements are true. Previously the simplification only applied to when the first element was true.
## 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)?
-->
Yes
## 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.
-->
No
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Simplify CASE for any WHEN TRUE [datafusion]
Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #17602:
URL: https://github.com/apache/datafusion/pull/17602#discussion_r2353923042
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1400,13 +1400,40 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
//
// CASE WHEN true THEN A ... END --> A
+ // CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X THEN A ELSE B END
Expr::Case(Case {
expr: None,
mut when_then_expr,
- else_expr: _,
- }) if !when_then_expr.is_empty() && is_true(when_then_expr[0].0.as_ref()) => {
- let (_, then_) = when_then_expr.swap_remove(0);
- Transformed::yes(*then_)
+ else_expr,
+ // if let guard is not stabilized so we can't use it yet: https://github.com/rust-lang/rust/issues/51114
+ // Once it's supported we can avoid searching through when_then_expr twice in the below .any() and .position() calls
+ // }) if let Some(i) = when_then_expr.iter().position(|(when, _)| is_true(when.as_ref())) => {
+ }) if when_then_expr
+ .iter()
+ .any(|(when, _)| is_true(when.as_ref())) =>
+ {
+ if let Some(i) = when_then_expr
+ .iter()
+ .position(|(when, _)| is_true(when.as_ref()))
+ {
+ let (_, then_) = when_then_expr.swap_remove(i);
+ // CASE WHEN true THEN A ... END --> A
+ if i == 0 {
+ return Ok(Transformed::yes(*then_));
+ }
+ // CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X THEN A ELSE B END
+ let truncated_when_then_expr = when_then_expr[..i].to_vec();
Review Comment:
Great catch, thanks!
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Simplify CASE for any WHEN TRUE [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #17602:
URL: https://github.com/apache/datafusion/pull/17602#discussion_r2354384576
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1400,13 +1400,40 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
//
// CASE WHEN true THEN A ... END --> A
+ // CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X THEN A ELSE B END
Expr::Case(Case {
expr: None,
mut when_then_expr,
- else_expr: _,
- }) if !when_then_expr.is_empty() && is_true(when_then_expr[0].0.as_ref()) => {
- let (_, then_) = when_then_expr.swap_remove(0);
- Transformed::yes(*then_)
+ else_expr,
+ // if let guard is not stabilized so we can't use it yet: https://github.com/rust-lang/rust/issues/51114
+ // Once it's supported we can avoid searching through when_then_expr twice in the below .any() and .position() calls
+ // }) if let Some(i) = when_then_expr.iter().position(|(when, _)| is_true(when.as_ref())) => {
+ }) if when_then_expr
+ .iter()
+ .any(|(when, _)| is_true(when.as_ref())) =>
+ {
+ if let Some(i) = when_then_expr
+ .iter()
+ .position(|(when, _)| is_true(when.as_ref()))
+ {
Review Comment:
Makes sense to me. Can also simplify this to:
```rust
let i = when_then_expr
.iter()
.position(|(when, _)| is_true(when.as_ref()))
.unwrap();
```
Since the guard guarantees the unwrap is safe.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Simplify CASE for any WHEN TRUE [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #17602:
URL: https://github.com/apache/datafusion/pull/17602
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Simplify CASE for any WHEN TRUE [datafusion]
Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #17602:
URL: https://github.com/apache/datafusion/pull/17602#discussion_r2353355705
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1400,13 +1400,40 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
//
// CASE WHEN true THEN A ... END --> A
+ // CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X THEN A ELSE B END
Expr::Case(Case {
expr: None,
mut when_then_expr,
- else_expr: _,
- }) if !when_then_expr.is_empty() && is_true(when_then_expr[0].0.as_ref()) => {
- let (_, then_) = when_then_expr.swap_remove(0);
- Transformed::yes(*then_)
+ else_expr,
+ // if let guard is not stabilized so we can't use it yet: https://github.com/rust-lang/rust/issues/51114
+ // Once it's supported we can avoid searching through when_then_expr twice in the below .any() and .position() calls
+ // }) if let Some(i) = when_then_expr.iter().position(|(when, _)| is_true(when.as_ref())) => {
+ }) if when_then_expr
+ .iter()
+ .any(|(when, _)| is_true(when.as_ref())) =>
+ {
+ if let Some(i) = when_then_expr
+ .iter()
+ .position(|(when, _)| is_true(when.as_ref()))
+ {
Review Comment:
Removing the condition guard results in making the case rule below this one inaccessible, so we can't just remove the `.any()` condition here. An alternative approach would be to move all of this logic into the same block as the rule below this, but the code wouldn't be so clean. I feel like iterating over the `when_then_expr` vector twice for now isn't too bad since these shouldn't really get too long, but I'm happy to hear other thoughts.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: Simplify CASE for any WHEN TRUE [datafusion]
Posted by "EeshanBembi (via GitHub)" <gi...@apache.org>.
EeshanBembi commented on code in PR #17602:
URL: https://github.com/apache/datafusion/pull/17602#discussion_r2353529356
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1400,13 +1400,40 @@ impl<S: SimplifyInfo> TreeNodeRewriter for Simplifier<'_, S> {
//
// CASE WHEN true THEN A ... END --> A
+ // CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X THEN A ELSE B END
Expr::Case(Case {
expr: None,
mut when_then_expr,
- else_expr: _,
- }) if !when_then_expr.is_empty() && is_true(when_then_expr[0].0.as_ref()) => {
- let (_, then_) = when_then_expr.swap_remove(0);
- Transformed::yes(*then_)
+ else_expr,
+ // if let guard is not stabilized so we can't use it yet: https://github.com/rust-lang/rust/issues/51114
+ // Once it's supported we can avoid searching through when_then_expr twice in the below .any() and .position() calls
+ // }) if let Some(i) = when_then_expr.iter().position(|(when, _)| is_true(when.as_ref())) => {
+ }) if when_then_expr
+ .iter()
+ .any(|(when, _)| is_true(when.as_ref())) =>
+ {
+ if let Some(i) = when_then_expr
+ .iter()
+ .position(|(when, _)| is_true(when.as_ref()))
+ {
+ let (_, then_) = when_then_expr.swap_remove(i);
+ // CASE WHEN true THEN A ... END --> A
+ if i == 0 {
+ return Ok(Transformed::yes(*then_));
+ }
+ // CASE WHEN X THEN A WHEN TRUE THEN B ... END --> CASE WHEN X THEN A ELSE B END
+ let truncated_when_then_expr = when_then_expr[..i].to_vec();
Review Comment:
Performance / allocation nit (optional)
You currently build `truncated_when_then_expr` via a slice clone:
```rust
let truncated_when_then_expr = when_then_expr[..i].to_vec();
```
Since when_then_expr is already mut, you can avoid an allocation and extra copies by truncating in place:
```
when_then_expr.truncate(i);
let truncated_when_then_expr = when_then_expr;
```
This eliminates the to_vec() allocation and is more efficient, especially for larger CASE expressions.
--
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@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org