Repository navigation
Support equality with one in opt-in WindowTopN rule - #26160
osipovartem wants to merge 5 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #26160 +/- ##
==========================================
- Coverage 82.77% 82.77% -0.01%
==========================================
Files 1147 1147
Lines 450909 450930 +21
Branches 450909 450930 +21
==========================================
+ Hits 373252 373264 +12
- Misses 54934 54938 +4
- Partials 22723 22728 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Marked as draft after 14 days without activity: merge conflicts. Please address these items and mark Ready for review when ready. |
c60ed5a to
f9cb6cb
Compare
f9cb6cb to
76194d5
Compare
There was a problem hiding this comment.
Thanks for this focused improvement. Supporting equality with 1 is appropriate because ROW_NUMBER, RANK, and DENSE_RANK produce positive integer values, making = 1 equivalent to <= 1. The implementation is small, and the new tests establish both result correctness and the optimized RANK plan.
Extending predicate extraction is the right architectural boundary. It reuses the existing per-function TopK execution semantics while preserving the guards around window identity, sibling expressions, projections, and opt-in configuration. No new execution machinery or public API changes are needed.
I found no blocking correctness issues. The existing coverage supports the intended behavior, though additional plan assertions for the other ranking functions would help protect against future optimizer regressions. I'm leaving this as a non-blocking suggestion.
|
|
||
| query III rowsort | ||
| SELECT id, pk, val FROM ( | ||
| SELECT *, DENSE_RANK() OVER (PARTITION BY pk ORDER BY val) as rk FROM window_topn_rank_eq_t |
There was a problem hiding this comment.
Optional: Could we add compact EXPLAIN checks for the new ROW_NUMBER and DENSE_RANK equality cases, asserting the corresponding PartitionedTopKExec function and fetch=1? Their result tests would also pass without the optimization, while the current positive plan assertion covers only RANK.
There was a problem hiding this comment.
Added in df38616: physical-only EXPLAIN checks for ROW_NUMBER and DENSE_RANK, including the function and fetch=1. The setting is restored afterwards. The focused file and full extended suite pass locally.
There was a problem hiding this comment.
Thanks for adding the physical-plan regression checks for ROW_NUMBER and DENSE_RANK equality with one. The follow-up addresses my earlier suggestion, and the assertions now verify that both predicates activate PartitionedTopKExec with fetch=1.
The approach preserves the intended optimizer boundary: equality with one is recognized as an upper-bound predicate, while equality with other values remains excluded. The additional coverage strengthens this contract without changing predicate extraction, execution behavior, public APIs, or the opt-in default.
I found no remaining actionable issues in the follow-up. The focused SQL logic tests passed, and the changes remain intact through the subsequent merges.
What changes
rank_col = 1and1 = rank_colin the opt-in WindowTopN rule. Other equalities remain untouched.enable_window_topndisabled by default.Validation
cargo test --profile ci -p datafusion-sqllogictest --test sqllogictests -- window_topn.slt --test-threads 1(passed on current upstream/main, Rust 1.99.0)cargo test --profile ci -p datafusion-physical-optimizer --lib(43 passed after rebase, Rust 1.99.0)cargo +stable clippy --profile ci -p datafusion-physical-optimizer --all-targets -- -D warningscargo +stable fmt --all --checkPerformance scope
Five local synthetic 100k-row runs (median query elapsed): 100 groups, rule off/on 31/18 ms; 90k groups, rule off/on 120/140 ms. High-cardinality plans can regress, which is why this remains opt-in. These numbers are not a Snowplow benchmark. A pushed-down FilterExec projection can also prevent this rewrite. This PR does not change either behavior.
Review follow-up (2026-10-30)
Added compact physical-only EXPLAIN assertions for ROW_NUMBER and DENSE_RANK equality with one, covering the corresponding PartitionedTopKExec function and fetch=1. The EXPLAIN setting is restored afterwards; existing result and RANK plan tests remain unchanged. This follow-up changes only the SLT file.
Local verification on the updated head: window_topn.slt passed; workspace all-targets/all-features Clippy with -D warnings and rustfmt passed; the documented extended workspace test command passed (13,319 Rust tests, 8 existing ignored, and 543/543 SLT files). Independent read-only review approved with no blocking findings. No production execution code changes or new performance claims.