fix: preserve input ordering required by limits#23744
Draft
jayhan94 wants to merge 1 commit into
Draft
Conversation
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.