[FLINK-40483][table-planner] Convert batch ROW_NUMBER Top-N to two-stage Rank - #29051
Draft
Timm0 wants to merge 1 commit into
Draft
[FLINK-40483][table-planner] Convert batch ROW_NUMBER Top-N to two-stage Rank#29051Timm0 wants to merge 1 commit into
Timm0 wants to merge 1 commit into
Conversation
…age Rank - Relax the `RANK`-only guards in `FlinkLogicalRankRuleForConstantRange` and `BatchPhysicalRankRule` to also admit `ROW_NUMBER`, routing batch `ROW_NUMBER() … WHERE rn <= N` (constant range) to the two-stage `Rank` instead of `OverAggregate` - Add a `RankType` parameter to `RankOperator`, emit on the row-number counter for `ROW_NUMBER`, and on the rank counter for `RANK` - Add `RankOperatorTest` and `RowNumberITCase` regenerate the flipped `ROW_NUMBER` goldens in `RankTest.xml` and `FlinkLogicalRankRuleForConstantRangeTest.xml` - Add the `ROW_NUMBER_TOP_N` compiled-plan restore program + JSON
Timm0
marked this pull request as ready for review
August 31, 2026 15:09
Collaborator
Timm0
marked this pull request as draft
August 31, 2026 15:17
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.
What is the purpose of the change
The batch planner compiles
ROW_NUMBER() OVER (PARTITION BY … ORDER BY …) … WHERE rn <= Nto a single-stageOverAggregatethat hash-shuffles and sorts the entire input before dropping all but the top rows. This change routes that pattern through the existing two-stageRank(a local top-N before the shuffle, and a global top-N after), so only the local survivors cross theExchange. It is a rule-based conversion at the logical phase, aligning batch with streaming.Brief change log
RANK-only guards inFlinkLogicalRankRuleForConstantRangeandBatchPhysicalRankRuleto also admitROW_NUMBER, routing batchROW_NUMBER() … WHERE rn <= N(constant range) to the two-stageRankinstead ofOverAggregateRankTypeparameter toRankOperator, emit on the row-number counter forROW_NUMBER, and on the rank counter forRANKRankOperatorTestandRowNumberITCaseregenerate the flippedROW_NUMBERgoldens inRankTest.xmlandFlinkLogicalRankRuleForConstantRangeTest.xmlROW_NUMBER_TOP_Ncompiled-plan restore program + JSONVerifying this change
RankOperatorTestandRowNumberITCaseregenerate the flippedROW_NUMBERgoldens inRankTest.xmlandFlinkLogicalRankRuleForConstantRangeTest.xmlROW_NUMBER_TOP_Ncompiled-plan restore program + JSONI also ran some benchmark tests on a dataset with near-unique keys (1 row per key) and lot's of duplicated keys (~1800 rows per key). Below are the results:
ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ... DESC) AS rn ... WHERE rn = 1Near-unique Keys:
Duplicated Keys:
ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ... DESC) AS rn ... WHERE rn <= 200Near-unique Keys:
Duplicated Keys:
Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Opus 4.8 (1M context)