Repository navigation
fix: keep sort below OFFSET when the sort columns are projected away - #26584
Open
yashaggarwal85d wants to merge 1 commit into
Open
yashaggarwal85d wants to merge 1 commit into
yashaggarwal85d wants to merge 1 commit into
Conversation
ensure_sorting removed a sort that its parent did not require when the child reported no output ordering, assuming the sort was neutralized. Below a limit this is wrong: a projection that drops the sort columns hides the ordering, but the limit still keeps or skips rows based on it. This made ORDER BY ... OFFSET subqueries consumed by an aggregate return wrong results. Closes apache#26488.
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?
Rationale for this change
When a subquery uses
ORDER BY ... OFFSET nand an aggregate reads it, the physical plan drops theSortExecbelowGlobalLimitExec. As a result,OFFSETskips arbitrary rows instead of the firstnrows in sort order, and the query returns wrong results:The plan going into
EnsureRequirementsis:In
ensure_sorting, a child sort that the parent doesn't require is removed when the child has no output ordering, on the assumption that the sort is "neutralized". Here the child ofGlobalLimitExecis the projection, which dropssandid, so it reports no ordering, and the sort gets removed. But a limit decides which rows to keep from its input order, so that sort still determines the result.Without the aggregate (for example under a
FilterExec), the sort survives only becauseOutputRequirementswraps it in anOutputRequirementExec. The aggregate stops that search, which is why the bug needs an aggregate to show up.What changes are included in this PR?
In
ensure_sorting, don't remove a sort through the "neutralized sort" path when the current node is a limit (GlobalLimitExec/LocalLimitExec).A
SortExecwith a fetch (TopK) is intentionally not covered: it re-sorts its input, so a sort below it really is redundant (test_handles_multiple_orthogonal_sortscovers that).What is the testing strategy for this PR?
Added
limit.sltcases from the issue: grouped and ungrouped aggregates over anORDER BY ... OFFSETsubquery, plus anEXPLAINshowing that theSortExecis kept. The grouped query returned0 6/1 -2onmain; it now returns0 9/1 -2, matching the expected result in the issue.The full sqllogictest suite and the
physical_optimizerintegration tests pass.Are there any user-facing changes?
Only the bug fix: queries like the one above now return correct results.
AI-assisted: I used an AI coding assistant while preparing this PR. I have reviewed and understand all changes.