Fix PushdownSort dropping LIMIT when source returns Exact#153
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness issue in the physical optimizer where PushdownSort could eliminate a SortExec (when the source reports SortOrderPushdownResult::Exact) but accidentally drop SortExec.fetch (LIMIT), changing results for queries like ORDER BY ... LIMIT N.
Changes:
- Preserve
SortExec.fetchin theExactpushdown path by attemptingwith_fetch()on the pushed-down source, with a fallback to adding a limit operator. - Add an
ExactTestScantest helper that returnsExactfromtry_pushdown_sortand supportswith_fetch. - Add unit tests asserting fetch preservation (and no-op behavior when fetch is absent) for the
Exactpath.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
datafusion/physical-optimizer/src/pushdown_sort.rs |
Propagates fetch when eliminating SortExec in the Exact branch, using with_fetch or a limit wrapper. |
datafusion/core/tests/physical_optimizer/test_utils.rs |
Introduces ExactTestScan to simulate an Exact-ordering source and support fetch pushdown in tests. |
datafusion/core/tests/physical_optimizer/pushdown_sort.rs |
Adds snapshot tests validating Exact sort elimination preserves fetch and avoids introducing a limit when fetch is absent. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
14305cf to
595da58
Compare
6 tasks
ewgenius
approved these changes
Apr 16, 2026
krinart
added a commit
that referenced
this pull request
Apr 17, 2026
…)" This reverts commit f9443ab.
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?
When
PushdownSortremoves aSortExecbecause the source returnsExact(guaranteeing ordering), anyfetch(LIMIT) on theSortExecwas silently dropped, causing queries likeSELECT * FROM t ORDER BY a LIMIT 1to return all rows.LimitPushdownruns beforePushdownSortand mergesGlobalLimitExecintoSortExec.fetch. WhenPushdownSortlater removes theSortExecin theExactbranch, the fetch value is lostWhat changes are included in this PR?
In the
Exactbranch ofPushdownSort, propagatefetchby first tryingwith_fetch()on the source (allowing sources like SQL-backed scans to addLIMITto their query), falling back to wrapping withGlobalLimitExec.Alternative considered
An alternative approach would be to pass the limit directly as a parameter to
try_pushdown_sort(), allowing sources to incorporate bothORDER BYandLIMITin plan if supported. This was rejected becausetry_pushdown_sorthas ~30 implementations and is intentionally single-purpose (ordering only). Handling fetch separately viawith_fetch()/GlobalLimitExecfollows the existing composition pattern used byEnforceSortingand keeps thetry_pushdown_sortAPI focused.