Repository navigation
Description
What happens today
The grammar (src/alasqlparser.jison) has several SELECT variants. They are Select with three alternatives, SelectWithoutOrderOrLimit, UnionClause, UnionableSelect, two ParenthesizedSelect rules and Statement: ParenthesizedSelect. The result is a chain on the first SELECT (A.unionall = B, B.except = C), evaluated right to left.
For (SELECT ... ORDER BY ... LIMIT ...) UNION ..., the alternative LPAR Select RPAR UnionClause OrderClause LimitClause adds the statement's ORDER BY and LIMIT to the inner SELECT with yy.extend, so they overwrite the part's own. queryfn3 (src/select/queryfn.js) then guesses with unionBranchHasOrder whether to apply them before or after the set operation. The chain has one place per operation, so a parenthesised set operation on the left loses one.
With A = 3, 1, 2 and B = 9, 8, (SELECT a FROM A ORDER BY a LIMIT 1) UNION ALL SELECT a FROM B gives 1. SQLite gives 1, 9, 8.
This also fixes #3134, #3135 and #3136.
What to change
-
Replace the SELECT variants with three rules.
Select : SelectBody OrderClause LimitClause | SEARCH ... /* as today */ ; SelectBody /* right recursion keeps v4's right-to-left order */ : SelectPrimary | SelectPrimary UnionOp SelectBody ; SelectPrimary : SelectClause RemoveClause? IntoClause FromClause PivotClause? WhereClause GroupClause | LPAR Select RPAR ;Remove
SelectWithoutOrderOrLimit,UnionClause,UnionableSelect, bothParenthesizedSelectrules andStatement: ParenthesizedSelect.UnionOpkeeps its eight alternatives. A part in parentheses keeps its own ORDER BY, LIMIT, OFFSET and TOP. -
Build today's chain in the actions.
SelectPrimary UnionOp SelectBodysetshead[op] = rightandcorrespondingon the first SELECT of the left part. When the left part is in parentheses and has its own ORDER BY, LIMIT, OFFSET, TOP or set operation, the head is a new SELECT that reads it as its only source and passes its rows on, likeSELECT * FROM (part). ItstoStringprints the part as written. The statement's ORDER BY and LIMIT go on the head. -
In
queryfn3, deleteunionBranchHasOrder,orderApplied,limitAppliedand the first copy of the sort-key block. IncompileInScope(src/select/select.js), remove the fourif (!query.orderfn && this.union.order)blocks if no test and no parity result changes. Otherwise keep them and say why in the pull request. -
Add one 4.x rule in the action that builds the head. When the first part is in parentheses and has an ORDER BY without LIMIT, OFFSET or TOP, the statement has no ORDER BY, and no other part has an ORDER BY or LIMIT, that ORDER BY also becomes the head's. Comment it with "removed in a later breaking step".
-
Close the SELECT's subquery list (Give each SELECT its own list of subqueries from parsing on #2666) in the action of the first
SelectPrimaryalternative. TheSelectaction moves the subqueries of the statement's ORDER BY, the last entries of the current list, to the head's list with new positions. -
Run
yarn jisonand compare the number of grammar conflicts. It must not grow. If a part in parentheses in the middle of a chain (SELECT ... UNION ALL (SELECT ... LIMIT 1) UNION ALL SELECT ...) now parses, add it to the tests. If it costs conflicts, leave it out.
What stays the same
- The right-to-left order of a chain without parentheses (
SELECT 1 INTERSECT SELECT 1 UNION ALL SELECT 2gives 1). - Column names, CORRESPONDING, test2605, test2610, and the recursive CTE, which reads
select.unionallof the head. - A part in parentheses on the right (
SELECT a FROM A UNION ALL (SELECT a FROM B ORDER BY a LIMIT 1)gives 3, 1, 2, 8). - A parenthesised SELECT that is the whole statement (
(SELECT a FROM A ORDER BY a) ORDER BY a DESCgives 3, 2, 1). - Subqueries in a part and in the statement's ORDER BY, for example
(SELECT a, (SELECT 5) AS f FROM A) UNION ALL SELECT a, 6 AS f FROM B ORDER BY a * (SELECT -1). - test2414 A, B and L keep their results through the 4.x rule. D, E and N change to SQLite's rows.
- Results change only for (SELECT ... LIMIT 1) UNION ALL SELECT ... applies the LIMIT to the whole result #2711, ORDER BY after a parenthesised part with its own LIMIT sorts only the rows of the first part #3134, A parenthesised UNION ALL followed by another UNION ALL loses rows of the parenthesised part #3135 and EXCEPT after a parenthesised UNION ALL is ignored #3136.
Done when
- A new test file named after this issue covers (SELECT ... LIMIT 1) UNION ALL SELECT ... applies the LIMIT to the whole result #2711 and the three bugs with SQLite's answers, for UNION, UNION ALL, EXCEPT and INTERSECT, with and without CORRESPONDING. It runs each statement twice through
alasql.compile. SQLite needsSELECT * FROM (...)around a parenthesised part. - test2414 D, E and N expect SQLite's rows.
- The dialect test entries of the set operation matrix that now match SQLite are removed with
bun test/dialect/run.js --update. yarn jisonhas regeneratedsrc/alasqlparser.js,yarn test-parserpasses, and the pull request gives the number of grammar conflicts before and after.- The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Fixes
Fixes #2711, #3134, #3135, #3136.
Depends on
#2666, which changes the same grammar actions (UnionOp, UnionableSelect, yy.queriesStack).
Activity
mathiasrw commentedon Oct 20, 2026
Part of the set operation design in #2718.
[-]Review #2395 for code improvements: the first SELECT's ORDER BY and LIMIT are guessed at run time[/-][+]Keep the ORDER BY and LIMIT of each part of a set operation where they were written[/+]