emecii opened a new pull request, #25143: URL: https://github.com/apache/datafusion/pull/25143
## Which issue does this PR close? Closes #25136. ## Rationale for this change `CASE WHEN s ~ '^[0-9]+$' THEN CAST(s AS INT) > 0 ELSE false END` currently becomes an `AND` predicate. On input containing `'abc'`, predicate evaluation can reach the cast even though the `WHEN` excluded that row. ## What changes are included in this PR? Restrict the searched Boolean CASE-to-AND/OR rewrite to columns or literals in `THEN`, `ELSE`, and later `WHEN` expressions. The first `WHEN` already runs on every row, but must be nonvolatile because expansion may repeat it. Apply the same restriction to the companion inversion rule. This is deliberately conservative: it does not attempt general expression fallibility analysis or change predicate ordering. More complex conditional expressions retain their CASE structure. Existing column/literal rewrites remain available, and two optimizer tests record the intentionally reduced simplification. Three EXPLAIN snapshots in `null_aware_mark_join.slt` and `subquery.slt` also change. CASE expressions generated for ANY comparisons now survive until EXISTS decorrelation, allowing the first mark column to be reused instead of duplicating its join. Their query-result expectations are unchanged. ## What is the testing strategy for this PR? The SQL regressions in `case.slt` cover guarded casts in filters and projections, true/false/NULL/implicit ELSE, a fallible later WHEN with literal outputs, guarded division, and a selected invalid branch that must still error. Five queries failed against current main before the optimizer change. The unit regression also covers a volatile first WHEN. Validation with pinned Rust 1.97.0: - `cargo fmt --all` and `git diff --check` passed. - `cargo clippy --all-targets --all-features -- -D warnings` passed. - The complete `./dev/rust_lint.sh` suite passed. - The contributor-guide extended workspace command passed: 11,341 Rust tests, 8 ignored, and all 513 SQL test files. No additional test exclusions were needed. The nearby `simplify_function_over_case` microbenchmark was run against current main with separately rebuilt binaries (Rust 1.98.0, dev profile, short Criterion samples). The quieter comparison reported no change or changes within the noise threshold. It exercises string-output CASE, not the retained Boolean CASE workloads, so this is not a general performance claim. ## Are there any user-facing changes? Guarded Boolean CASE expressions preserve branch-local evaluation. Some optimized plans retain CASE expressions where Boolean lowering was previously applied; there is no public API change. Generated with OpenAI Codex assistance, including implementation, tests, and this description. -- 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]
