Skip to content

Conversation

SidChaudhary7
Copy link

@SidChaudhary7 SidChaudhary7 commented Sep 17, 2025

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-actions github-actions bot added the optimizer Optimizer rules label Sep 17, 2025
@alamb
Copy link
Contributor

alamb commented Sep 17, 2025

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?

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.

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

alamb commented Sep 18, 2025

Thank you for this PR @SidChaudhary7

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

@alamb
Copy link
Contributor

alamb commented Sep 25, 2025

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

@alamb
Copy link
Contributor

alamb commented Sep 29, 2025

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

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