Repository navigation
fix: preserve sorts before limits when projections hide sort keys - #26763
Merged
jayzhan211 merged 1 commit intoOct 26, 2026
Merged
Conversation
jayzhan211
approved these changes
Oct 26, 2026
jayzhan211
left a comment
Contributor
There was a problem hiding this comment.
Thanks @kumarUjjawal 👍🏻
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?
Closes #23534.
This follows the closed, unmerged #23744.
Rationale for this change
A grouped query over
ORDER BY sort_key OFFSET 1can returnsorted_firstinstead ofphysical_second. A projection hides the sort key, so the physical optimizer drops the sort even though it determines which rows the offset retains.What changes are included in this PR?
Preserve linked sorts through operators that maintain input order, including when a projection removes their ordering metadata. Remove superseded sorts when enforcing a new ordering, and stop sort removal at explicit limits and operators with embedded fetch limits.
Add SQL and physical optimizer regressions for projections, intervening filters, local and global limits, replacement sorts, and optimizer idempotence. An execution test verifies that an embedded filter limit still selects the correct rows after one or two optimizer passes.
What is the testing strategy for this PR?
cargo fmt --all,cargo clippy --all-targets --all-features -- -D warnings, and the complete./dev/rust_lint.shsuite pass.physical_plan_struct_join_agg_sortandphysical_sorted_union_order_by_10_int64against main at350ca76505c9513d16e7dd9910bcd9bde2c911ed, using 30 samples each. Neither benchmark showed a statistically significant performance change.Are there any user-facing changes?
Queries retain the rows selected by ordered LIMIT/OFFSET when a projection hides the sort key. No public API changes.