Repository navigation
fix: preserve local TopK limits during exact sort pushdown - #26736
Draft
geoffreyclaude wants to merge 1 commit into
Draft
geoffreyclaude wants to merge 1 commit into
geoffreyclaude wants to merge 1 commit into
Conversation
geoffreyclaude
force-pushed
the
fix/exact-sort-pushdown-local-limit
branch
from
October 25, 2026 08:16
6ce49ef to
5c86f33
Compare
geoffreyclaude
force-pushed
the
fix/exact-sort-pushdown-local-limit
branch
from
October 25, 2026 08:36
5c86f33 to
4c1fa5a
Compare
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?
Closes #26739.
Rationale for this change
Sort pushdown can make an
ORDER BY ... LIMITquery fail plan validation when a source guarantees the requested ordering but cannot apply the row limit itself.A partition-preserving TopK keeps up to
krows per partition. Removing its sort must retain that local limit. The standalone exact-pushdown path always insertedGlobalLimitExecinstead, which requires a single input partition. With a four-partition source, the resulting plan fails with aSinglePartitiondistribution error.The direct merge/sort path already uses a local fallback. The standalone path needs to preserve the same limit scope.
What changes are included in this PR?
When the source cannot absorb the fetch, choose
LocalLimitExecif the removed sort preserves partitions, andGlobalLimitExecotherwise.What is the testing strategy for this PR?
The SQL regression uses a native running-sum window over a four-partition source, with an explicit
ORDER BY group_id, value LIMIT 3. The custom source only supplies data and accepts exact sort pushdown; it cannot apply a limit.The window is needed to reach the affected optimization:
PARTITION BYrequires redistributing this source's rows by group, and the outerLIMITlets the optimizer add a local TopK before that redistribution. Without the window, this query takes the already-correct merge/sort path.The test checks the three expected rows and uses
EXPLAINto verify thatLocalLimitExecremains below the repartition. Without the fix, the query fails plan validation. Removing the limit entirely would fail the plan assertion.Validation: the focused SQL file, existing
PushdownSortintegration tests, formatting, and Clippy with the standard CI feature set (avro,integration-tests,extended_tests).Are there any user-facing changes?
Affected queries execute successfully while preserving the per-partition and overall row limits. No public API changes.
Developed with AI assistance.