Repository navigation
[branch-56] fix: preserve local TopK limits during exact sort pushdown (backport #26736) - #27784
Merged
Merged
Conversation
) ## Which issue does this PR close? - Closes apache#26739. ## Rationale for this change Sort pushdown can make an `ORDER BY ... LIMIT` query fail plan validation when a source guarantees the requested ordering but cannot apply the row limit itself. A partition-preserving TopK keeps up to `k` rows **per partition**. Removing its sort must retain that local limit. The standalone exact-pushdown path always inserted `GlobalLimitExec` instead, which requires a single input partition. With a four-partition source, the resulting plan fails with a `SinglePartition` distribution 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 `LocalLimitExec` if the removed sort preserves partitions, and `GlobalLimitExec` otherwise. ## 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 BY` requires redistributing this source's rows by group, and the outer `LIMIT` lets 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 `EXPLAIN` to verify that `LocalLimitExec` remains below the repartition. Without the fix, the query fails plan validation. Removing the limit entirely would fail the plan assertion. [Two direct optimizer tests](https://github.com/apache/datafusion/blob/a7f447f4831d4d906d82968b287dc72dea72aca2/datafusion/sqllogictest/src/test_context/exact_sort_pushdown.rs#L134) reuse the same source: one checks the global-limit fallback and three returned rows; the other checks sort elimination without a limit and all 16 rows. ## 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. (cherry picked from commit 8ad8490)
xudong963
approved these changes
Nov 27, 2026
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more. @@ Coverage Diff @@
## branch-56 #27784 +/- ##
=============================================
- Coverage 83.30% 83.30% -0.01%
=============================================
Files 1170 1171 +1
Lines 476209 476344 +135
Branches 476209 476344 +135
=============================================
+ Hits 396694 396806 +112
- Misses 55487 55499 +12
- Partials 24028 24039 +11
... and 7 files with indirect coverage changes 🚀 New features to boost your workflow:
|
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?
branch-56, for Release DataFusion56.1.0(minor/patch) Release (Dec 2026) #24463.Rationale for this change
On 56.0.0, an
ORDER BY ... LIMITquery can fail plan validation with aSinglePartitiondistribution error when a source accepts an exact sort pushdown but cannot apply the limit itself: the sort is removed and replaced by aGlobalLimitExecover several partitions. #26736 fixed this onmain.What changes are included in this PR?
A cherry-pick of #26736 (
8ad8490) with-x. It applied cleanly.What is the testing strategy for this PR?
The regression tests from #26736 (
exact_sort_pushdowntest context and its sqllogictest) pass on this branch, along withsort_pushdown*.sltandlimit.slt.Are there any user-facing changes?
Affected queries run instead of failing plan validation. No public API changes.