Repository navigation
perf: Partitioned topk emit row number - #26133
Open
SubhamSinghal wants to merge 3 commits into
Open
SubhamSinghal wants to merge 3 commits into
SubhamSinghal wants to merge 3 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #26133 +/- ##
==========================================
+ Coverage 82.73% 82.75% +0.02%
==========================================
Files 1147 1147
Lines 449389 450243 +854
Branches 449389 450243 +854
==========================================
+ Hits 371788 372594 +806
+ Misses 54943 54932 -11
- Partials 22658 22717 +59 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
SubhamSinghal
marked this pull request as ready for review
October 9, 2026 04:44
Contributor
Author
|
@kosiew @jayzhan-synnada @kumarUjjawal Can you help in reviewing this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
With
enable_window_topn,WindowTopNrewritesFilter(rn <= K) → BoundedWindowAggExec → SortExecinto:The window node stays only to produce
rn, but the operator already knows every retained row's rank when it emits it: rows leave in(partition keys, order keys)order, and the retained set is exactly the rows with rank ≤ K. The node's cost scales with the operator's output (K × partitions) and never amortizes, because every batch it sees is dense in partition boundaries.What changes are included in this PR?
PartitionedTopKExec::try_newtakesranking_field: Option<FieldRef>. When set, the operator appends aUInt64column with each row's rank, andWindowTopNdrops the window node:Each policy derives the rank from what it already holds.
ROW_NUMBER: position in the partition's drain.RANK: position of the first row with the same key; boundary ties take the boundary key's.DENSE_RANK: index of the distinct-key group. All three rely on the retained set being a complete order-prefix of its partition.Applied only when the window node has exactly one expression, since the operator appends one column. Otherwise the node is kept, as today. The output schema is the node's (input fields plus that field), so column indices above are unchanged and
schema_check()holds.compute_propertiesdeclares the ordering the removed node published,[partition keys..., rank ASC NULLS LAST], soORDER BY pk, rnstays sort-free.Are these changes tested?
topk/mod.rs: the rank column for each policy, including boundary ties, an eviction that moves a row to the tie list, aDENSE_RANKgroup gathered from several input batches, and output spanning severalbatch_sizechunks.sorts/partitioned_topk.rs: the widened schema, and the[pk, rn]ordering (but not[rn]alone).core/tests/physical_optimizer/window_topn.rs: the node is removed for one expression and kept for two;ORDER BY pk, rnplans noSortExecfor all three functions; end to end, the emitted column equals the flag-off result (ties, 4 partitions, batch size 4).window_topn.sltandrange_partitioning.slt: EXPLAIN output updated, query results unchanged.Benchmark
h2o
window.sqlk=2 sweeps onJ1_1e7_1e7_NA.parquet(10 M rows, 14 partitions),enable_window_topn = truein both arms.ROW_NUMBERRANKDENSE_RANKOpen question for reviewers
WindowTopNrecognises ranking functions by name. Now that the window node is removed, a user-registeredrank/row_number/dense_ranknever runs its own evaluator:UInt64, the query fails at execution (column types must match schema types);UInt64, its values are silently replaced by the built-in's.Which fix do you prefer?
== rank_udwf()etc. (WindowUDFequality compares the concrete type and its fields). This needsdatafusion-functions-windowas a normal dependency ofdatafusion-physical-optimizer, which adds no crates for anyone already usingdatafusion.WindowUDFImplmethod declaring top-K ranking semantics, likelimit_effect. It is a public API addition, so probably a follow-up.UInt64. This fixes the error but not the silent replacement.Are there any user-facing changes?
Only with
enable_window_topn(default false):EXPLAINno longer shows the window node for single-expression ranking queries, andPartitionedTopKExecshowsemit=[...]. Query results are unchanged.PartitionedTopKExec::try_newgains a parameter, so this is a breaking API change; please add theapi changelabel.PartitionedTopKExec::ranking_field()is the new accessor.