Skip to content

Conversation

@petern48
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

Improves perf

What changes are included 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?

Yes

Are there any user-facing changes?

No

@petern48 petern48 changed the title perf: Simplify CASE when any TRUE perf: Simplify CASE for any WHEN TRUE Sep 16, 2025
@petern48 petern48 marked this pull request as ready for review September 16, 2025 18:39
Comment on lines 1408 to 1418
// 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()))
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Can also simplify this to:

let i = when_then_expr
    .iter()
    .position(|(when, _)| is_true(when.as_ref()))
    .unwrap();

Since the guard guarantees the unwrap is safe.

@github-actions github-actions bot added the optimizer Optimizer rules label Sep 17, 2025
Copy link
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; don't know when if let guards will be stabilized but not sure if there is a cleaner way to achieve it otherwise 🤔

Comment on lines 1408 to 1418
// 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()))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Can also simplify this to:

let i = when_then_expr
    .iter()
    .position(|(when, _)| is_true(when.as_ref()))
    .unwrap();

Since the guard guarantees the unwrap is safe.

@alamb alamb enabled auto-merge September 17, 2025 17:34
@alamb alamb added this pull request to the merge queue Sep 17, 2025
Merged via the queue into apache:main with commit 0c7da26 Sep 17, 2025
28 checks passed
@petern48 petern48 deleted the simplify_case branch September 17, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Further simplify CASE ... WHEN expressions

4 participants