Skip to content

perf: Route remaining literal CASE branches in one pass - #194

Draft
Jason8Ni wants to merge 1 commit into
den-55from
Jason8Ni/literal-case-branch-routing
Draft

Jason8Ni wants to merge 1 commit into
den-55from
Jason8Ni/literal-case-branch-routing

Conversation

@Jason8Ni

@Jason8Ni Jason8Ni commented Oct 30, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

No linked issue; this is a performance improvement for the den-55 fork. This PR is stacked on #195; merge that maintenance update first, then retarget this PR to den-55.

Rationale for this change

Simple CASE key WHEN literal THEN expression ... expressions repeatedly compare keys and filter shrinking record batches. With many branches, copying the remaining rows can cost more than evaluating the selected results. This affects ordinary categorical SQL expressions as well as generated filters.

What changes are included in this PR?

For more than eight supported literal WHEN branches, reuse LiteralLookupTable to route remaining rows to their original branch indices. Keep the first two sequential comparisons so common leading matches retain their early exits; evaluate each remaining result only on its selected rows. Reuse the existing projection and result-merging logic, preserving first-match, NULL, cast, lazy-evaluation, and row-order semantics. Short CASE expressions and unsupported WHEN expressions retain the existing evaluator.

Are these changes tested?

  • Nine targeted regressions cover duplicate/missing/NULL keys, scalar and dictionary inputs, floating special values, sliced/empty batches, fallible expressions, ELSE casts, short/nonliteral fallback, and zero-column batches with generated keys.
  • Portable SQL tests cover many literal branches with expression results.
  • The extended workspace suite passes on the refreshed base with this CASE change applied, including all 503 SQL logic test files.
  • All-target/all-feature Clippy and ./dev/rust_lint.sh pass on the refreshed base.
  • Added Criterion workloads vary branch count and uniform, first-branch, first-two-branch, skewed, and nonmatching distributions.

Historical measurements on the earlier 55.1-based revision: a local interleaved comparison against the retained sequential evaluator used 8,192 rows, arithmetic results, 11 alternating rounds, and 60 evaluations per arm per round (ci profile, opt-level 1). Uniform 16/32/64/128-branch cases improved approximately 1.48/1.94/2.74/4.11×; the skewed 16-branch case improved about 1.17×. First/first-two-branch and short-case controls were approximately unchanged. These are expression-level measurements on a shared development machine, not end-to-end query latency claims or new measurements against the refreshed 55.2-based branch.

Follow-up cutoff experiments

Optimized-build experiments on the refreshed revision found that the current eight-branch cutoff is too aggressive for some ordinary inputs. In two repeated sweeps, a nine-branch Int64 case with varied missing keys took about 3.6× as long as sequential evaluation; a skewed 12-branch case took about 1.7× as long. Uniform matching inputs still benefit. The earlier opt-level-1 measurements do not establish a generally safe crossover.

A local prototype preserves the zero-match/all-match fast paths before routing and accumulates routed results directly in the existing result builder, avoiding an extra merge. It removes the severe no-match regression. Skew-sensitive cases remain at 32–48 branches. A conservative follow-up would combine those changes with a minimum of 64 branches: 1,324 prototype scenario evaluations at 64/96/128 branches had no median slowdown above 5%; focused skewed 64-branch cases improved about 1.20–1.23× across three fresh processes. This sacrifices some smaller-expression wins and does not prove 64 universally optimal.

The investigation covered 10,448 timed scenario evaluations, with equality checked before every timing, varying branch count, leading comparisons, match distribution, batch size, key representation, and computed result type. All 45 CASE unit tests passed on the final prototype. These were shared Apple M4 Max expression microbenchmarks using release-nonlto (opt-level 3), with lookup construction excluded. The prototype and proposed cutoff are not yet applied to this PR, and require full code validation plus production-hardware and end-to-end measurements before adoption.

Are there any user-facing changes?

Eligible CASE expressions do less repeated filtering and copying. SQL semantics and public APIs are unchanged; no query-specific detection or configuration is added.

@codecov-commenter

codecov-commenter commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.26%. Comparing base (24a553d) to head (bb39e8b).

Files with missing lines Patch % Lines
datafusion/physical-expr/src/expressions/case.rs 83.33% 6 Missing and 12 partials ⚠️
Additional details and impacted files
@@                        Coverage Diff                         @@
##           Jason8Ni/refresh-den-55-upstream     #194    +/-   ##
==================================================================
  Coverage                             81.26%   81.26%            
==================================================================
  Files                                  1110     1110            
  Lines                                389819   389924   +105     
  Branches                             389819   389924   +105     
==================================================================
+ Hits                                 316791   316887    +96     
- Misses                                54446    54447     +1     
- Partials                              18582    18590     +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Jason8Ni
Jason8Ni changed the base branch from den-55 to Jason8Ni/refresh-den-55-upstream October 30, 2026 18:55
@Jason8Ni
Jason8Ni force-pushed the Jason8Ni/literal-case-branch-routing branch from c80faee to bb39e8b Compare October 30, 2026 18:55
Base automatically changed from Jason8Ni/refresh-den-55-upstream to den-55 November 12, 2026 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants