Skip to content

Support equality with one in opt-in WindowTopN rule - #26160

Open
osipovartem wants to merge 5 commits into
apache:mainfrom
Embucket:upstream-window-topn-eq-one
Open

osipovartem wants to merge 5 commits into
apache:mainfrom
Embucket:upstream-window-topn-eq-one

Conversation

@osipovartem

@osipovartem osipovartem commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What changes

  • Recognize rank_col = 1 and 1 = rank_col in the opt-in WindowTopN rule. Other equalities remain untouched.
  • Add physical-plan and result regressions for ROW_NUMBER, RANK (including ties), and DENSE_RANK. Preserve the existing sibling-window safety guard.
  • Leave enable_window_topn disabled 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 warnings
  • cargo +stable fmt --all --check
  • Independent read-only review approved correctness, regressions, performance fast path, API compatibility, and current-base test coverage after the EXPLAIN expectation was corrected.

Performance 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.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.77%. Comparing base (06aa131) to head (c60ed5a).
⚠️ Report is 3 commits behind head on main.

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.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
github-actions Bot marked this pull request as draft October 26, 2026 01:28
@github-actions

Copy link
Copy Markdown

Marked as draft after 14 days without activity: merge conflicts.

Please address these items and mark Ready for review when ready.

@osipovartem
osipovartem force-pushed the upstream-window-topn-eq-one branch from c60ed5a to f9cb6cb Compare October 28, 2026 18:12
@osipovartem
osipovartem force-pushed the upstream-window-topn-eq-one branch from f9cb6cb to 76194d5 Compare October 29, 2026 13:37
@osipovartem
osipovartem marked this pull request as ready for review October 29, 2026 13:37

@kosiew kosiew left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osipovartem,

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@osipovartem
osipovartem requested a review from kosiew November 2, 2026 22:43

@kosiew kosiew left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osipovartem,

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants