EeshanBembi opened a new pull request, #17450:
URL: https://github.com/apache/datafusion/pull/17450
## Summary
Adds an optimization rule to simplify CASE expressions where the first
condition is always true (literal `true`), reducing them to just the THEN
expression.
This fixes the issue where expressions like:
```sql
CASE WHEN true THEN 1 ELSE x END
```
were not being simplified to just `1`.
## Changes
- Added new simplification rule in `expr_simplifier.rs` that detects `CASE
WHEN true THEN expr` patterns
- Added comprehensive tests covering various scenarios including:
- Simple literal values
- Column references
- Multiple WHEN clauses (only first is simplified)
- Cases with and without ELSE clauses
## Test Plan
- [x] Added unit tests for the new simplification rule
- [x] Verified existing CASE tests still pass
- [x] Tested with the exact example from the issue
- [x] Confirmed logical and physical plans now show simplified expressions
## Before/After
**Before:**
```
logical_plan | Projection: CASE WHEN Boolean(true) THEN Int64(1) ELSE
CAST(foo.x AS Int64) END
physical_plan | ProjectionExec: expr=[CASE WHEN true THEN 1 ELSE CAST(x@0 AS
Int64) END as ...]
```
**After:**
```
logical_plan | Projection: Int64(1) AS CASE WHEN Boolean(true) THEN
Int64(1) ELSE foo.x END
physical_plan | ProjectionExec: expr=[1 as CASE WHEN Boolean(true) THEN
Int64(1) ELSE foo.x END]
```
Fixes #17448
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]