Skip to content

Optimize CASE expressions by removing WHEN false branches - #17628

Closed
SidChaudhary7 wants to merge 2 commits into
apache:mainfrom
SidChaudhary7:release
Closed

Optimize CASE expressions by removing WHEN false branches#17628
SidChaudhary7 wants to merge 2 commits into
apache:mainfrom
SidChaudhary7:release

Conversation

@SidChaudhary7

@SidChaudhary7SidChaudhary7 commented Sep 17, 2025

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

This PR implements an optimization for CASE expressions that removes unreachable WHEN false branches and handles WHEN true cases that make subsequent branches unreachable.

For example:

CASE WHEN false THEN 1-- never executed
ELSE 2 END

Are there any user-facing changes?

No

This is my first contribution to DataFusion. Could a committer please trigger the CI tasks?
@alamb@andygrove@tustvold@ursabot

@github-actionsgithub-actionsBot added the optimizer Optimizer rules label Sep 17, 2025
@alamb

Copy link
Copy Markdown
Contributor

This is my first contribution to DataFusion. Could a committer please trigger the CI tasks? @alamb@andygrove@tustvold@ursabot

Done!
Thanks for the PR @SidChaudhary7

Can you also put a note in the PR description about what issue this fixes?

@JefffreyJefffrey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suggest looking at this recent PR for a good reference on how to implement this: #17602

expr: None,
when_then_expr,
else_expr,
}) if !when_then_expr.is_empty() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this if guard make the below case arm unreachable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I think so

In general I think we could combine this branch with the one above (that checks for a single true clause) 🤔

So the logic would be

  • if the WHEN exprs have any true or false literals
  • then rewrite the when_then expr, removing all false literals and any arms after the first true

@alamb

Copy link
Copy Markdown
Contributor

Thank you for this PR @SidChaudhary7

It seems there are a few failing tests -- can you look into them?

@alamb

Copy link
Copy Markdown
Contributor

Once we merge this pr from @jackkleeman

If @SidChaudhary7 doesn't have time by tomorrow, I'll try and push it along

@petern48

Copy link
Copy Markdown
Member

I don't mind taking it if you'd rather save your time for other things. Should be pretty straightforward for me since I did the previous WHEN TRUE PR. I'll leave that up to you @alamb

@alamb

Copy link
Copy Markdown
Contributor

I don't mind taking it if you'd rather save your time for other things. Should be pretty straightforward for me since I did the previous WHEN TRUE PR. I'll leave that up to you @alamb

That would be great @petern48 -- thank you for the offer! Please just ping me on the new PR 🙏

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizerOptimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify CASE WHEN false THEN ... Expression Simplification forExpr::Case expressions

5 participants

@SidChaudhary7@alamb@petern48@Jefffrey@Raisw-android