Repository navigation
fix(explorer): read ORDER BY after a set operation as the combined result in column usage - #1638
Conversation
…sult in column usage
| if (open !== undefined) opens.set(next, open); | ||
| stack[stack.length - 1] = {level: next++, own: true}; | ||
| stack[stack.length - 1] = {level: next++, own: true, setOperation: true}; | ||
| } else if (top.setOperation && startsResultClause(tokens, i)) { |
There was a problem hiding this comment.
SUGGESTION: startsResultClause matches LIMIT/OFFSET/FETCH anywhere in a split level, including inside the last side's select list.
A column or alias with one of those names (e.g. ... UNION SELECT offset FROM public.archive) is treated as the trailing clause. The rest of that side (FROM archive) is then moved to a table-less level, so its columns are missed. This is a regression from the old behaviour, but it needs a rare identifier. Consider requiring that the token is not directly preceded by SELECT, ,, . or AS, or that the side's FROM has been seen. Add a test for this case.
Code Review SummaryStatus: 1 Issue Found | Recommendation: Merge after considering the suggestion Overview
Issue Details (click to expand)SUGGESTION
Files Reviewed (2 files)
Fix these issues in Kilo Cloud Reviewed by claude-sonnet-5.5 · Input: 252 · Output: 2.4K · Cached: 153.8K Review guidance: REVIEW.md from base branch |
debba
left a comment
There was a problem hiding this comment.
Thanks for another solid fix! Marking the frames split by UNION/INTERSECT/EXCEPT with setOperation and giving the trailing ORDER BY/LIMIT/OFFSET/FETCH its own level keeps the change small, and window OVER (ORDER BY …) or a parenthesised side can't trigger it because those frames never get the flag. I also liked that the test covers the cases that must keep matching (ORDER BY inside a parenthesised side, a plain SELECT … ORDER BY, a subquery UNION, a window ORDER BY). I checked that it fails on main and passes here (44/44).
Approving. I'll do my manual checks before merging. A couple of small things for a follow-up:
startsResultClausematches bareOFFSET/FETCH/LIMITwithout looking at what follows. On MySQLOFFSETisn't a reserved word, so it can be an unquoted column name: withSELECT id FROM orders UNION SELECT customer_id, offset FROM archive, the usage ofarchive.customer_idis found on main but not on this branch, becauseoffsetopens the result level andFROM archiveends up in it. RequiringOFFSETto be followed by a number, a parameter orROW(S), andFETCHbyFIRST/NEXT, would avoid that. It's rare and only a missed hint, so not blocking.- Not a regression (main does the same), but
INSERT … SELECT … UNION SELECT … ORDER BY … RETURNING colputsRETURNINGin the table-less level too, in case you want to handle it while you're there.
|
Tested this in the app with views and history queries using UNION, INTERSECT and EXCEPT followed by ORDER BY. Show Usage on the second table no longer lists the trailing ORDER BY as a reference, while a plain ORDER BY, a window ORDER BY and an ORDER BY inside a subquery are still matched. lint, typecheck and the test suites pass. Merging, thanks! |
Follow-up to #1542, for the point @debba raised in its approving review: a trailing
ORDER BYon a UNION's combined result was attributed to the last side.What changed
computeScopes(utils/columnUsage.ts): when a level has been split by a set operator, theORDER BY/LIMIT/OFFSET/FETCHafter its last side gets a level of its own. That level has no tables and no parent, because it sorts and limits the combined result by its output columns, so it reads no table column.SELECT customer_id FROM public.orders UNION SELECT customer_id FROM public.archive ORDER BY customer_id, only the archive side's select list counts forarchive.customer_id. Before, the trailingORDER BYadded a second parsed match.ORDER BY, anORDER BYinside one parenthesised side, and a windowORDER BY(OVER (ORDER BY ...)) inside a side still resolve against their own tables.Tests
columnUsage.test.ts: the new testreads ORDER BY and LIMIT after the last set-operation side as the combined resultcovers:ORDER BY ... LIMIT, andORDER BY ... OFFSET ... FETCH;UNION ALL (SELECT ...) ORDER BY);ORDER BYinside anIN (...)subquery;ORDER BYinside one parenthesised side, a plain SELECT'sORDER BY, a query whose subquery has a UNION while its ownORDER BYreads its table, and a windowORDER BYin one side.The test fails on main.
Not changed: a qualified
ORDER BY a.customer_idafter a UNION still resolves through the aliasa, since aliases are statement-wide. PostgreSQL rejects that form anyway ("ORDER BY on a UNION result must be on one of the result columns").Checks:
pnpm run typecheck && pnpm run lint && pnpm run lint:theme && pnpm run format:check && pnpm run web:boundaries && pnpm run web:packaging && pnpm run web:docs && pnpm run web:manual-parity && pnpm run test:contrast && pnpm run build && pnpm exec vitest --config packages/web-ui/vitest.config.ts run --maxWorkers=2pass: 628 files, 9955 tests.GitNexus: upstream
impactofcomputeScopesand of the newstartsResultClauseis LOW (callers in the same file only).Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.