Skip to content

perf: Extend WindowTopN to support RANK - #22885

Merged
kumarUjjawal merged 5 commits into
apache:mainfrom
SubhamSinghal:window-topn-rank
Jul 14, 2026
Merged

perf: Extend WindowTopN to support RANK#22885
kumarUjjawal merged 5 commits into
apache:mainfrom
SubhamSinghal:window-topn-rank

Conversation

@SubhamSinghal

@SubhamSinghalSubhamSinghal commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

PR #21479 introduced WindowTopN for ROW_NUMBER only; RANK and DENSE_RANK were explicitly out of scope. This PR extends the rule to RANK, replacing the full sort under Filter(rk≤K) → Window(RANK) → Sort with a per-partition heap-of-K plus a boundary-tie buffer.

What changes are included in this PR?

  • datafusion/physical-plan/src/topk/mod.rs — new pub(crate) struct PartitionedTopKRank (sibling of
    PartitionedTopK from perf: share encoder/reservation across PartitionedTopKExec partition … #23096) with per-partition RankPartitionState { TopKHeap, Vec<TieEntry> }.
  • datafusion/physical-plan/src/sorts/partitioned_topk.rsWindowFnKind enum (RowNumber / Rank).
    do_partitioned_topk dispatches on fn_kind to PartitionedTopK::try_new or PartitionedTopKRank::try_new;
  • datafusion/physical-optimizer/src/window_topn.rsis_row_numbersupported_window_fn(expr) -> Option<WindowFnKind>; empty-order_by guard for RANK; WindowFnKind plumbed through PartitionedTopKExec::try_new.
  • datafusion/sqllogictest/test_files/window_topn.slt — RANK SLT cases: basic, strict (<), flipped (>= / >),
    boundary ties, ties spanning ob values, empty-ORDER BY (rule must NOT fire), mixed window functions, ASC/DESC × NULLS FIRST/LAST, QUALIFY.
  • datafusion/core/tests/physical_optimizer/window_topn.rs — 6 new RANK rule unit tests covering predicate
    matching, partition-by/order-by guards, dense_rank skip.
  • benchmarks/queries/h2o/window.sql — six new RANK queries (Q14–Q17, Q22, Q23) covering partition counts from ~100 to ~100K, low and heavy tie densities.

h2o window benchmark, 10M-row large table, RANK top-2, 3-iteration average. Toggle via DATAFUSION_OPTIMIZER_ENABLE_WINDOW_TOPN.

VariantPartitionsOFF (rule disabled)ON (rule enabled)Δ
RANK low ties (id3 % 100)~100305 ms107 ms2.84× faster
RANK low ties (id3 % 1000)~1K263 ms120 ms2.19× faster
RANK heavy ties (id3 % 1000, v2 % 10 OB)~1K282 ms125 ms2.25× faster
RANK low ties (id2)~10K363 ms140 ms2.59× faster
RANK heavy ties (id2, v2 % 10 OB)~10K291 ms143 ms2.04× faster
RANK low ties (id3 % 100K)~100K241 ms422 ms1.75× slower

Are these changes tested?

Yes:

  • cargo test -p datafusion-physical-plan --lib — 1455 passed
  • cargo test -p datafusion-physical-optimizer --lib — 27 passed
  • cargo test -p datafusion --test core_integration physical_optimizer::window_topn:: — 13 passed (7 ROW_NUMBER + 6 RANK)
  • cargo test --test sqllogictests -- window_topn — passed

Are there any user-facing changes?

The existing optimizer.enable_window_topn config flag (default false) now also covers RANK queries. No public API additions

@github-actionsgithub-actionsBot added optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate auto detected api change Auto detected API change labels Jun 10, 2026
@SubhamSinghal

Copy link
Copy Markdown
ContributorAuthor

@kumarUjjawal@2010YOUY01 will you be able to review this PR.

@comphead

Copy link
Copy Markdown
Contributor

might be related #23021

@github-actionsgithub-actionsBot removed optimizer Optimizer rules core Core DataFusion crate physical-plan Changes to the physical-plan crate labels Jun 30, 2026
@github-actionsgithub-actionsBot added optimizer Optimizer rules core Core DataFusion crate physical-plan Changes to the physical-plan crate and removed auto detected api change Auto detected API change labels Jun 30, 2026
@github-actions

github-actionsBot commented Jun 30, 2026

Copy link
Copy Markdown

Thank you for opening this pull request!

Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch).

Details
 Cloning apache/main
Building datafusion v54.0.0 (current)
Built [ 107.234s] (current)
Parsing datafusion v54.0.0 (current)
Parsed [ 0.038s] (current)
Building datafusion v54.0.0 (baseline)
Built [ 107.646s] (baseline)
Parsing datafusion v54.0.0 (baseline)
Parsed [ 0.038s] (baseline)
Checking datafusion v54.0.0 -> v54.0.0 (no change; assume patch)
Checked [ 0.956s] 223 checks: 223 pass, 30 skip
Summary no semver update required
Finished [ 218.224s] datafusion
Building datafusion-physical-optimizer v54.0.0 (current)
Built [ 39.349s] (current)
Parsing datafusion-physical-optimizer v54.0.0 (current)
Parsed [ 0.024s] (current)
Building datafusion-physical-optimizer v54.0.0 (baseline)
Built [ 38.304s] (baseline)
Parsing datafusion-physical-optimizer v54.0.0 (baseline)
Parsed [ 0.025s] (baseline)
Checking datafusion-physical-optimizer v54.0.0 -> v54.0.0 (no change; assume patch)
Checked [ 0.187s] 223 checks: 223 pass, 30 skip
Summary no semver update required
Finished [ 79.389s] datafusion-physical-optimizer
Building datafusion-physical-plan v54.0.0 (current)
Built [ 35.598s] (current)
Parsing datafusion-physical-plan v54.0.0 (current)
Parsed [ 0.148s] (current)
Building datafusion-physical-plan v54.0.0 (baseline)
Built [ 36.255s] (baseline)
Parsing datafusion-physical-plan v54.0.0 (baseline)
Parsed [ 0.149s] (baseline)
Checking datafusion-physical-plan v54.0.0 -> v54.0.0 (no change; assume patch)
Checked [ 0.937s] 223 checks: 222 pass, 1 fail, 0 warn, 30 skip
--- failure method_parameter_count_changed: pub method parameter count changed ---
Description:
A publicly-visible method now takes a different number of parameters, not counting the receiver (self) parameter.
ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.48.0/src/lints/method_parameter_count_changed.ron
Failed in:
datafusion_physical_plan::sorts::partitioned_topk::PartitionedTopKExec::try_new takes 4 parameters in /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/3f697da4e542e016b2a8f02a2a7f5d525c982695/datafusion/physical-plan/src/sorts/partitioned_topk.rs:196, but now takes 5 parameters in /home/runner/work/datafusion/datafusion/datafusion/physical-plan/src/sorts/partitioned_topk.rs:227
Summary semver requires new major version: 1 major and 0 minor checks failed
Finished [ 74.965s] datafusion-physical-plan
Building datafusion-sqllogictest v54.0.0 (current)
Built [ 190.398s] (current)
Parsing datafusion-sqllogictest v54.0.0 (current)
Parsed [ 0.023s] (current)
Building datafusion-sqllogictest v54.0.0 (baseline)
Built [ 189.344s] (baseline)
Parsing datafusion-sqllogictest v54.0.0 (baseline)
Parsed [ 0.025s] (baseline)
Checking datafusion-sqllogictest v54.0.0 -> v54.0.0 (no change; assume patch)
Checked [ 0.122s] 223 checks: 223 pass, 30 skip
Summary no semver update required
Finished [ 383.463s] datafusion-sqllogictest

@github-actionsgithub-actionsBot added the auto detected api change Auto detected API change label Jun 30, 2026
@SubhamSinghal

Copy link
Copy Markdown
ContributorAuthor

@kosiew can you help in reviewing this PR.

@kosiewkosiew 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.

@SubhamSinghal
Looks good overall.
I left a couple of suggestions to keep the docs accurate and to call out a possible memory-accounting follow-up.

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.

Small docs nit: the Limitations section still says this operator is only activated for ROW_NUMBER. Could you update this sentence to mention RANK and the non-empty ORDER BY requirement?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

fixed doc


// Commit this batch's ties as a single entry.
if !equal_indices.is_empty() {
state.ties.push(TieEntry {

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.

This looks correct for preserving tied rows. One possible follow-up: TieEntry keeps a clone of the full input RecordBatch and charges input_batch_bytes per partition tie entry, which could over-reserve memory when a single batch has boundary ties across many partitions.

It may be worth looking at compact tie batches or shared batch accounting later.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I will file a followup issue for this.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

created issue for this: #23326

@kosiew

Copy link
Copy Markdown
Contributor

@SubhamSinghal
Thanks for working on this.

Leaving this open for a few days for more reviews as this is a big PR.

@SubhamSinghal

Copy link
Copy Markdown
ContributorAuthor

@kosiew can we merge this PR?

@kumarUjjawal
kumarUjjawal added this pull request to the merge queueJul 14, 2026
@kumarUjjawal

Copy link
Copy Markdown
Contributor

Thank you @SubhamSinghal and @kosiew

Merged via the queue into apache:main with commit 79692c1Jul 14, 2026
40 checks passed
@SubhamSinghal
SubhamSinghal deleted the window-topn-rank branch July 14, 2026 07:42
kosiew pushed a commit to kosiew/datafusion that referenced this pull request Aug 12, 2026
## Which issue does this PR close?
- Related to apache#6899
(DENSE_RANK to follow in a separate PR will close it).
## Rationale for this change
PR apache#21479 introduced `WindowTopN` for `ROW_NUMBER` only; `RANK` and
`DENSE_RANK` were explicitly out of scope. This PR extends the rule to
`RANK`, replacing the full sort under `Filter(rk≤K) → Window(RANK) →
Sort` with a per-partition heap-of-K plus a boundary-tie buffer.
## What changes are included in this PR?
- **`datafusion/physical-plan/src/topk/mod.rs`** — new `pub(crate)
struct PartitionedTopKRank` (sibling of
`PartitionedTopK` from apache#23096) with per-partition `RankPartitionState {
TopKHeap, Vec<TieEntry> }`.
- **`datafusion/physical-plan/src/sorts/partitioned_topk.rs`** —
`WindowFnKind` enum (`RowNumber` / `Rank`).
`do_partitioned_topk` dispatches on `fn_kind` to
`PartitionedTopK::try_new` or `PartitionedTopKRank::try_new`;
- **`datafusion/physical-optimizer/src/window_topn.rs`** —
`is_row_number` → `supported_window_fn(expr) ->
Option<WindowFnKind>`; empty-`order_by` guard for RANK; `WindowFnKind`
plumbed through `PartitionedTopKExec::try_new`.
- **`datafusion/sqllogictest/test_files/window_topn.slt`** — RANK SLT
cases: basic, strict (`<`), flipped (`>=` / `>`),
boundary ties, ties spanning ob values, empty-`ORDER BY` (rule must NOT
fire), mixed window functions, ASC/DESC × NULLS FIRST/LAST, QUALIFY.
- **`datafusion/core/tests/physical_optimizer/window_topn.rs`** — 6 new
RANK rule unit tests covering predicate
matching, partition-by/order-by guards, dense_rank skip.
- **`benchmarks/queries/h2o/window.sql`** — six new RANK queries
(Q14–Q17, Q22, Q23) covering partition counts from ~100 to ~100K, low
and heavy tie densities.
h2o `window` benchmark, 10M-row `large` table, RANK top-2, 3-iteration
average. Toggle via `DATAFUSION_OPTIMIZER_ENABLE_WINDOW_TOPN`.
| Variant | Partitions | OFF (rule disabled) | ON (rule enabled) | Δ |
|---|---:|---:|---:|:---:|
| RANK low ties (`id3 % 100`) | ~100 | 305 ms | **107 ms** | **2.84×
faster** ✓ |
| RANK low ties (`id3 % 1000`) | ~1K | 263 ms | **120 ms** | **2.19×
faster** ✓ |
| RANK heavy ties (`id3 % 1000`, `v2 % 10` OB) | ~1K | 282 ms | **125
ms** | **2.25× faster** ✓ |
| RANK low ties (`id2`) | ~10K | 363 ms | **140 ms** | **2.59× faster**
✓ |
| RANK heavy ties (`id2`, `v2 % 10` OB) | ~10K | 291 ms | **143 ms** |
**2.04× faster** ✓ |
| RANK low ties (`id3 % 100K`) | ~100K | 241 ms | 422 ms | 1.75× slower
|
## Are these changes tested?
Yes:
- `cargo test -p datafusion-physical-plan --lib` — 1455 passed
- `cargo test -p datafusion-physical-optimizer --lib` — 27 passed
- `cargo test -p datafusion --test core_integration
physical_optimizer::window_topn::` — 13 passed (7 ROW_NUMBER + 6 RANK)
- `cargo test --test sqllogictests -- window_topn` — passed
## Are there any user-facing changes?
The existing `optimizer.enable_window_topn` config flag (default
`false`) now also covers `RANK` queries. No public API additions
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto detected api changeAuto detected API changecoreCore DataFusion crateoptimizerOptimizer rulesphysical-planChanges to the physical-plan cratesqllogictestSQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@SubhamSinghal@comphead@kosiew@kumarUjjawal