Conversation
Signed-off-by: 付典 <fudianchn@gmail.com>
fudianchn
marked this pull request as ready for review
October 2, 2026 07:25
This branch has not been deployed
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.
AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.
What
Trailing LIMIT and OFFSET clauses now belong to SetOperationList when its final SELECT is unparenthesized, including queries without ORDER BY.
Why
For
SELECT a FROM t1 UNION SELECT b FROM t2 LIMIT 1, the parser stores LIMIT on the last PlainSelect and leaves the set-level limit null. PostgreSQL applies that clause to the complete UNION result. SQL round trips hide the incorrect AST ownership, so consumers inspecting or changing the set-level limit cannot control the intended result.How
Root cause
The final PlainSelect consumes the trailing LIMIT and OFFSET before SetOperationList is constructed. Their transfer to the set node is nested inside the ORDER BY guard, so an absent ORDER BY leaves them attached to the branch.
Testing
bb55bb9d, 18 fail and 13 normal controls pass; the fix passes all 31. Cases include UNION/UNION ALL, INTERSECT, EXCEPT, the existing MINUS syntax, WITH, multiple branches, LIMIT expressions and parameters, LIMIT/OFFSET/FETCH combinations, and local/global parentheses. Removing the parenthesis guard is rejected by six tests; restoring the fixed grammar passes again.Behavior notes
Verification of the original issue
No open issue is linked. The missing no-ORDER path was reproduced on upstream master
bb55bb9de8377de9880b14e4fe3b3cc94bf63498; the locally verified fixed commit is9fa49735e7be923321df3544debf78d56d85e894. This builds on Tomer Shay's merged #1132, which fixed the ORDER-plus-LIMIT cases from #1018 and #1116. Thanks to Tomer Shay for establishing that transfer; this change completes its no-ORDER branch and keeps the historical examples as passing AST controls. Those closed issues are not reopened or claimed fixed again.The first two store their suffix on the set node and clear it from the last branch. The third keeps LIMIT on the inner PlainSelect and leaves the set-level limit null.