fix: preserve input ordering required by limits#23744
Open
jayhan94 wants to merge 4 commits into
Open
Conversation
jayhan94
marked this pull request as ready for review
July 23, 2026 06:41
jayhan94
force-pushed
the
fix_limit_sort
branch
2 times, most recently
from
July 24, 2026 02:33
6ce79b4 to
eb5a24a
Compare
Author
|
@alamb PTAL🙏 |
alamb
reviewed
Jul 24, 2026
| /// Unlike [`Self::required_input_ordering`], this property does not require | ||
| /// a particular ordering that can be expressed using the child's schema. | ||
| /// It only requires that an existing input ordering is not discarded or | ||
| /// replaced with an incompatible ordering by physical optimizations. For |
Contributor
There was a problem hiding this comment.
This property seems like it is some internal invariant of EnforceDistribution rather than something that describes how the ExecutionPlan behaves
I don't think we should add this to the ExecutionPlan trait
Contributor
|
Isn't LIMIT already handled for subqueries? Perhaps we just need to extend the same logic to handle I don't understand the need to add a new method to ExecutionPlan |
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?
GROUP BYconsumes a subquery containingORDER BY ... OFFSET. #23534.Rationale for this change
EnsureRequirementsmay remove aSortExecwhen no ordering requirement is visible from its parent.This is normally valid, but not when the sorted input is consumed by a limit.
LIMITandOFFSETuse the input sequence to determine which rows are returned. In the reported case, an interveningProjectionExecremoves the sort key from its output schema, causing itsoutput_ordering()to becomeNone. The optimizer consequently treats the sort below the projection as unnecessary and removes it, producing incorrect query results.maintains_input_order()only describes whether an operator reorders its surviving rows, whilerequired_input_ordering()requires a concrete ordering expressible using the child schema. Neither property expresses that an operator semantically depends on an existing input ordering.What changes are included in this PR?
ExecutionPlan::requires_input_order_preservation(), with a default value offalsefor each child.GlobalLimitExecandLocalLimitExecdeclare that their input ordering must be preserved.SortExeccan be removed.OFFSET.Are these changes tested?
Yes.
The regression tests cover both the physical plan and the original SQL behavior. Existing tests also verify that compatible sort pushdown and TopK optimizations are preserved.
The following checks were run:
Are there any user-facing changes?
Yes. Queries containing an ordered subquery followed by
LIMITorOFFSETnow preserve the ordering needed to select the correct rows, even when an intervening projection hides the sort columns.This PR also adds a defaulted method to the public
ExecutionPlantrait. Existing implementations do not need to be changed, and the change is not breaking.