Skip to content

fix(explorer): read ORDER BY after a set operation as the combined result in column usage - #1638

Merged
debba merged 1 commit into
TabularisDB:mainfrom
shu-chen-dot-ca:fix-1542-union-order-by
Nov 23, 2026
Merged

debba merged 1 commit into
TabularisDB:mainfrom
shu-chen-dot-ca:fix-1542-union-order-by

Conversation

@shu-chen-dot-ca

@shu-chen-dot-ca shu-chen-dot-ca commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #1542, for the point @debba raised in its approving review: a trailing ORDER BY on 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, the ORDER BY / LIMIT / OFFSET / FETCH after 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.
    • In 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 for archive.customer_id. Before, the trailing ORDER BY added a second parsed match.
    • Nothing changes without a set operation: a plain SELECT's ORDER BY, an ORDER BY inside one parenthesised side, and a window ORDER BY (OVER (ORDER BY ...)) inside a side still resolve against their own tables.

Tests

columnUsage.test.ts: the new test reads ORDER BY and LIMIT after the last set-operation side as the combined result covers:

  • the case above;
  • ORDER BY ... LIMIT, and ORDER BY ... OFFSET ... FETCH;
  • parenthesised sides (UNION ALL (SELECT ...) ORDER BY);
  • a union with ORDER BY inside an IN (...) subquery;
  • these, which keep their match: an ORDER BY inside one parenthesised side, a plain SELECT's ORDER BY, a query whose subquery has a UNION while its own ORDER BY reads its table, and a window ORDER BY in one side.

The test fails on main.

Not changed: a qualified ORDER BY a.customer_id after a UNION still resolves through the alias a, 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=2 pass: 628 files, 9955 tests.

GitNexus: upstream impact of computeScopes and of the new startsResultClause is LOW (callers in the same file only).


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kilo-code-bot

kilo-code-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 1 Issue Found | Recommendation: Merge after considering the suggestion

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
packages/web-ui/src/utils/columnUsage.ts 585 LIMIT/OFFSET/FETCH used as a column or alias name in a split level is mistaken for the trailing result clause, so the rest of that side is missed.
Files Reviewed (2 files)
  • packages/web-ui/src/utils/columnUsage.ts - 1 issue
  • packages/web-ui/tests/utils/columnUsage.test.ts

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 main

@debba debba left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. startsResultClause matches bare OFFSET/FETCH/LIMIT without looking at what follows. On MySQL OFFSET isn't a reserved word, so it can be an unquoted column name: with SELECT id FROM orders UNION SELECT customer_id, offset FROM archive, the usage of archive.customer_id is found on main but not on this branch, because offset opens the result level and FROM archive ends up in it. Requiring OFFSET to be followed by a number, a parameter or ROW(S), and FETCH by FIRST/NEXT, would avoid that. It's rare and only a missed hint, so not blocking.
  2. Not a regression (main does the same), but INSERT … SELECT … UNION SELECT … ORDER BY … RETURNING col puts RETURNING in the table-less level too, in case you want to handle it while you're there.

@debba

debba commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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!

@debba
debba merged commit 906ddf0 into TabularisDB:main Nov 23, 2026
11 checks passed
@shu-chen-dot-ca
shu-chen-dot-ca deleted the fix-1542-union-order-by branch November 23, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants