Skip to content

Conversation

@EeshanBembi
Copy link
Owner

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:

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

  • Added unit tests for the new simplification rule
  • Verified existing CASE tests still pass
  • Tested with the exact example from the issue
  • 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 apache#17448

Eeshan and others added 5 commits September 6, 2025 02:10
Add optimization rule to simplify CASE expressions where the first
condition is always true (literal true), reducing them to just the
THEN expression. This eliminates unnecessary branching and casting
for cases like "CASE WHEN true THEN 1 ELSE x END" which now
simplifies to "1".

Fixes apache#17448
- Use is_true() utility function instead of manual pattern matching
- Avoid unnecessary clone by using swap_remove(0) to extract then expression
- Add negative test cases for CASE expressions that should not be simplified:
  - CASE WHEN a THEN 1 ELSE 2 END (column condition)
  - CASE WHEN false THEN 1 ELSE 2 END (false literal)
  - CASE WHEN col("x") > 5 THEN 1 ELSE 2 END (expression condition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify CASE WHEN true <xx> ...

3 participants