Skip to content

Keep the ORDER BY and LIMIT of each part of a set operation where they were written to close #2712 - #3192

Merged
mathiasrw merged 3 commits into
mainfrom
fix/2712
Nov 30, 2026
Merged

mathiasrw merged 3 commits into
mainfrom
fix/2712

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closes #2712. Fixes #2711, #3134, #3135 and #3136.

The problem

A part of a set operation written in parentheses did not keep its own ORDER BY, LIMIT, OFFSET, TOP or set operation. The grammar copied the clauses of the statement onto the first SELECT, and queryfn3 guessed where to apply them.

With A = 3, 1, 2, B = 9, 8, 2 and C = 5, 4. SQLite 3.43 needs SELECT * FROM (...) around a part in parentheses.

Statement Before After SQLite
(SELECT a FROM A ORDER BY a LIMIT 1) UNION ALL SELECT a FROM B 1 1, 9, 8, 2 1, 9, 8, 2
SELECT a FROM A UNION ALL (SELECT a FROM C ORDER BY a LIMIT 1) ORDER BY a DESC 3, 2, 1, 4 4, 3, 2, 1 4, 3, 2, 1
(SELECT 1 AS a UNION ALL SELECT 2) UNION ALL SELECT 3 1, 3 1, 2, 3 1, 2, 3
(SELECT 1 AS a UNION ALL SELECT 2) EXCEPT SELECT 1 1, 2 2 2
SELECT a FROM A UNION (SELECT a FROM B ORDER BY a LIMIT 1) UNION SELECT a FROM A ORDER BY a parse error 1, 2, 3 1, 2, 3

What changed

  • src/alasqlparser.jison has Select, SelectBody and SelectCore in place of the three Select alternatives, SelectWithoutOrderOrLimit, UnionClause, UnionableSelect, both ParenthesizedSelect rules and Statement: ParenthesizedSelect. SelectBody is right recursive, so a chain keeps its right-to-left order. The chain is built as before (head.unionall = rest).
  • yy.headOfPart (src/engine/yy.js) gives the head of part UNION rest. When a part in parentheses has its own LIMIT, TOP or set operation, the head is a new SELECT that reads the part as its only source, like SELECT * FROM (part). It takes over the modifier and INTO of the part, and its toString prints the part as written.
  • queryfn3 loses unionBranchHasOrder, orderApplied, limitApplied and the first copy of the sort-key block. ORDER BY and LIMIT of a query now always run after its set operation.
  • compileInScope loses the four if (!query.orderfn && this.union.order) blocks. No test and no result of the parity check changed.
  • A part in parentheses in the middle of a chain now parses.
  • The part behind a new head gets the outer row of the head (subqueryDatafn passes query.oldscope for that head only), and isCorrelated looks into that part. So the part can read the outer row in EXISTS, IN, a scalar subquery and APPLY, and IN does not keep the answer of the first row. Other subqueries in FROM and JOIN are as on main. A correlated one still throws a TypeError, which is left to Compile every subquery to one form with a kind and correlation from the binding, and read FROM subqueries, views and CTEs through one table source #3164, and an IN over an uncorrelated one still runs it once.
  • compileOrder counts a position in the columns of the first SELECT, also behind a new head. So (SELECT a FROM A LIMIT 3) UNION ALL SELECT a FROM B ORDER BY 1 sorts.

Where this differs from the issue text

  • The issue asks for SelectPrimary: SelectClause ... | LPAR Select RPAR. With that rule, SELECT ((SELECT 1) UNION ALL SELECT 2) and a IN ((SELECT ...) UNION ALL SELECT ...) became parse errors. After ( ( Select ) the parser must choose between a subquery expression and a part of a set operation, and jison chose the expression. So LPAR Select RPAR and LPAR Select RPAR UnionOp SelectBody are alternatives of SelectBody. The set operator is then a shift, as it was before. This also tells the action that the part is in parentheses without a marker.
  • The 4.x rule. When the first part has an ORDER BY alone and no other part has an ORDER BY or LIMIT, the part stays the head, as before, so its ORDER BY sorts the whole result. The issue asks to copy the ORDER BY to a new head. The rows are the same, and a subquery in that ORDER BY keeps its list. The rule is one condition in headOfPart, with the comment "removed in a later breaking step".
  • TOP without parentheses, which the issue does not cover. SELECT TOP 1 a FROM A UNION ALL SELECT TOP 1 a FROM B gives 3, 9 on main, as queryfn3 kept the TOP to the first SELECT when the next part had an ORDER BY, LIMIT or TOP. Without such a part the TOP limits the whole result (SELECT TOP 2 a FROM A UNION ALL SELECT a FROM C gives 3, 1). Both stay. A first SELECT with TOP and no parentheses gets the new head when a later part has an ORDER BY, LIMIT or TOP, and is under the same 4.x condition otherwise. On main only the next part counted, and here every later part counts, as for the ORDER BY. TOP of a SELECT in a set operation also limits the SELECTs after it #3196 has the rest of this.

Grammar conflicts

yarn jison reports 243 conflicts on main (e82aab0) and 106 with this change.

Tests

Old against new

A matrix of 3,004 statements ran on the build of main and on this build, each twice through alasql.compile, and in SQLite 3.43. It has every pair of parts (plain, in parentheses, with ORDER BY, with LIMIT, TOP or OFFSET) for the four operators with and without CORRESPONDING, chains of three, and set operations in parentheses on either side, each with and without ORDER BY and LIMIT on the statement.

Statements Result
1,502 same rows in the same order as on main
501 first part with LIMIT, TOP or OFFSET (#2711), now SQLite's rows
173 ORDER BY or LIMIT of the statement after a last part in parentheses (#3134), now SQLite's rows
121 first part is a set operation in parentheses (#3135, #3136), now SQLite's rows
672 parse error on main (a part in parentheses in the middle of a chain), now SQLite's rows
29 one of the bugs above in a UNION with a LIMIT and no ORDER BY on the statement. SQLite picks other rows, as the order is not defined
6 #3135 with EXCEPT. The lost rows are back. EXCEPT keeps duplicates of the first part (#3167)

No statement outside these bugs changed, also not in row order.

A second matrix of 2,880 statements has TOP with and without parentheses in the first, middle and last part, against parts with ORDER BY and LIMIT, with a name or a position in the ORDER BY of the statement. SQLite ran it with each TOP as a LIMIT in SELECT * FROM (part).

Statements Result
2,600 SQLite's rows
21 TOP in the first SELECT without parentheses limits the whole result, as on main (4.x rule)
110 TOP in a middle SELECT without parentheses also limits the SELECTs after it (#3196). 38 give the rows of main. The other 72 have an ORDER BY or LIMIT on the statement, which now applies to all rows
149 UNION, EXCEPT or INTERSECT with a LIMIT and no ORDER BY on the statement, where the order is not defined

90 correlated statements (EXISTS, NOT EXISTS, IN, NOT IN, = ANY, a scalar subquery, CROSS APPLY and OUTER APPLY over a part in parentheses with LIMIT, TOP or a set operation) give the rows of main in 74 cases. 9 throw a TypeError as on main (a correlated subquery in FROM that is not a part, #3164). 7 lost rows on main through #2711.

Parity differences

Reference is the merge base e82aab0. Outside test/test2712.js the differences are results 19, errors 32, generated code 83 and only in the new recording 1, diverged 0. The reference also runs test/test2712.js, where 12 cases fail on it, which adds results 28, errors 1, generated code 21 and only in the new recording 86.

  • The 19 results are the bugs above (13 in the dialect cases, 4 in test2414, 1 in test1965) and the known clock shift in test815.js (an xlsx date).
  • The 32 errors are parse errors that are gone, a part in parentheses in the middle of a chain.
  • The 83 code differences. 32 are statements that did not parse on main. In 27 a part with an ORDER BY now sets its sort key in its own row function (r['$$$0']=r['a']), as it is no longer the head of the chain. In 13 the comparator that compileInScope compiled from the ORDER BY of the next part is gone ((no code)). In 11 the statement has a new head, so it has more functions and they line up differently (3 against 5).
  • Only in the new recording is DROP TABLE test8 in test1965.js. Case H failed on main before it reached that line, as its expectation is updated.
  • API has 3 paths, all in test/parity/api-allowed.txt. They are alasql.yy.setOperation, alasql.yy.headOfPart and alasql.yy.Select.fields (parenthesised).
  • Size of alasql.min.js goes from 548,109 to 546,737 bytes.
Results (19)
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION (SELECT v FROM q)
  before [{"v":4},{"v":3}]
  after  [{"v":2},{"v":3},{"v":3},{"v":4},{"v":undefined}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION (SELECT v FROM q) ORDER BY v
  before [{"v":undefined},{"v":1}]
  after  [{"v":undefined},{"v":2},{"v":3},{"v":3},{"v":4}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION (SELECT v FROM q WHERE v IN (SELECT v FROM r))
  before [{"v":4},{"v":3}]
  after  [{"v":3},{"v":3},{"v":4},{"v":2}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION (SELECT v FROM q WHERE v IN (SELECT v FROM r)) ORDER BY v
  before [{"v":undefined},{"v":1}]
  after  [{"v":2},{"v":3},{"v":3},{"v":4}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION ALL (SELECT v FROM q)
  before [{"v":4},{"v":3}]
  after  [{"v":3},{"v":2},{"v":2},{"v":3},{"v":3},{"v":4},{"v":undefined}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION ALL (SELECT v FROM q) ORDER BY v
  before [{"v":undefined},{"v":undefined}]
  after  [{"v":undefined},{"v":2},{"v":2},{"v":3},{"v":3},{"v":3},{"v":4}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION ALL (SELECT v FROM q WHERE v IN (SELECT v FROM r))
  before [{"v":4},{"v":3}]
  after  [{"v":3},{"v":2},{"v":3},{"v":3},{"v":4}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) UNION ALL (SELECT v FROM q WHERE v IN (SELECT v FROM r)) ORDER BY v
  before [{"v":undefined},{"v":1}]
  after  [{"v":2},{"v":3},{"v":3},{"v":3},{"v":4}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) INTERSECT (SELECT v FROM q) ORDER BY v
  before [{"v":undefined},{"v":2}]
  after  [{"v":2},{"v":3}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) EXCEPT (SELECT v FROM q)
  before [{"v":1}]
  after  []
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) EXCEPT (SELECT v FROM q) ORDER BY v
  before [{"v":1}]
  after  []
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) EXCEPT (SELECT v FROM q WHERE v IN (SELECT v FROM r))
  before [{"v":2},{"v":2}]
  after  [{"v":2}]
dialect | (SELECT v FROM p ORDER BY v DESC LIMIT 2) EXCEPT (SELECT v FROM q WHERE v IN (SELECT v FROM r)) ORDER BY v
  before [{"v":undefined},{"v":1}]
  after  [{"v":2}]
test1965.js | SELECT val FROM test8 UNION ALL (SELECT val + 100 AS val FROM test8 ORDER BY val DESC LIMIT 2) ORDER BY val ASC
  before [{"val":10},{"val":20},{"val":30},{"val":130},{"val":120}]
  after  [{"val":10},{"val":20},{"val":30},{"val":120},{"val":130}]
test2414.js | (SELECT a FROM ? LIMIT 2) UNION SELECT a FROM ? WHERE a > 2
  before [{"a":3},{"a":4}]
  after  [{"a":3},{"a":4},{"a":1},{"a":2}]
test2414.js | (SELECT a FROM ? ORDER BY a DESC LIMIT 2) UNION SELECT a FROM ? WHERE a < 2
  before [{"a":4},{"a":3}]
  after  [{"a":1},{"a":4},{"a":3}]
test2414.js | (SELECT a FROM ? ORDER BY a LIMIT 2 OFFSET 1) UNION SELECT a FROM ? WHERE a > 4
  before [{"a":2},{"a":3}]
  after  [{"a":5},{"a":2},{"a":3}]
test2414.js | ((SELECT a FROM ? WHERE a = 1) UNION (SELECT a FROM ? WHERE a = 2 ORDER BY a)) UNION SELECT a FROM ? WHERE a > 2 ORDER BY a DESC
  before [{"a":4},{"a":3},{"a":1}]
  after  [{"a":4},{"a":3},{"a":2},{"a":1}]
test815.js | SELECT * FROM xlsx("test/test815.xlsx", {cellDates: false})
  before [{"date":46316.50405384259},{"date":25569}]
  after  [{"date":46316.504053854165},{"date":25569}]
Parse errors that are gone (32)
(SELECT v FROM p) UNION (SELECT v FROM q) UNION (SELECT v FROM r)
(SELECT v FROM p) UNION (SELECT v FROM q) UNION (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION (SELECT v FROM q) UNION ALL (SELECT v FROM r)
(SELECT v FROM p) UNION (SELECT v FROM q) UNION ALL (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION (SELECT v FROM q) INTERSECT (SELECT v FROM r)
(SELECT v FROM p) UNION (SELECT v FROM q) INTERSECT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION (SELECT v FROM q) EXCEPT (SELECT v FROM r)
(SELECT v FROM p) UNION (SELECT v FROM q) EXCEPT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION ALL (SELECT v FROM q) UNION (SELECT v FROM r)
(SELECT v FROM p) UNION ALL (SELECT v FROM q) UNION (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION ALL (SELECT v FROM q) UNION ALL (SELECT v FROM r)
(SELECT v FROM p) UNION ALL (SELECT v FROM q) UNION ALL (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION ALL (SELECT v FROM q) INTERSECT (SELECT v FROM r)
(SELECT v FROM p) UNION ALL (SELECT v FROM q) INTERSECT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) UNION ALL (SELECT v FROM q) EXCEPT (SELECT v FROM r)
(SELECT v FROM p) UNION ALL (SELECT v FROM q) EXCEPT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) INTERSECT (SELECT v FROM q) UNION (SELECT v FROM r)
(SELECT v FROM p) INTERSECT (SELECT v FROM q) UNION (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) INTERSECT (SELECT v FROM q) UNION ALL (SELECT v FROM r)
(SELECT v FROM p) INTERSECT (SELECT v FROM q) UNION ALL (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) INTERSECT (SELECT v FROM q) INTERSECT (SELECT v FROM r)
(SELECT v FROM p) INTERSECT (SELECT v FROM q) INTERSECT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) INTERSECT (SELECT v FROM q) EXCEPT (SELECT v FROM r)
(SELECT v FROM p) INTERSECT (SELECT v FROM q) EXCEPT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) EXCEPT (SELECT v FROM q) UNION (SELECT v FROM r)
(SELECT v FROM p) EXCEPT (SELECT v FROM q) UNION (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) EXCEPT (SELECT v FROM q) UNION ALL (SELECT v FROM r)
(SELECT v FROM p) EXCEPT (SELECT v FROM q) UNION ALL (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) EXCEPT (SELECT v FROM q) INTERSECT (SELECT v FROM r)
(SELECT v FROM p) EXCEPT (SELECT v FROM q) INTERSECT (SELECT v FROM r) ORDER BY v
(SELECT v FROM p) EXCEPT (SELECT v FROM q) EXCEPT (SELECT v FROM r)
(SELECT v FROM p) EXCEPT (SELECT v FROM q) EXCEPT (SELECT v FROM r) ORDER BY v
Generated code by file (83)
dialect 56
test1965.js 7
test2414.js 5
test2605.js 7
test2610.js 8

Two more result changes

Not done

@mathiasrw mathiasrw added this to the ES6 code refactoring milestone Nov 30, 2026
@mathiasrw
mathiasrw merged commit 533a764 into main Nov 30, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment