Skip to content

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

Open
0 of 3 issues completed

Description

@mathiasrw

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

  1. 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, both ParenthesizedSelect rules and Statement: ParenthesizedSelect. UnionOp keeps its eight alternatives. A part in parentheses keeps its own ORDER BY, LIMIT, OFFSET and TOP.

  2. Build today's chain in the actions. SelectPrimary UnionOp SelectBody sets head[op] = right and corresponding on 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, like SELECT * FROM (part). Its toString prints the part as written. The statement's ORDER BY and LIMIT go on the head.

  3. In queryfn3, delete unionBranchHasOrder, orderApplied, limitApplied and the first copy of the sort-key block. In compileInScope (src/select/select.js), remove the four if (!query.orderfn && this.union.order) blocks if no test and no parity result changes. Otherwise keep them and say why in the pull request.

  4. 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".

  5. Close the SELECT's subquery list (Give each SELECT its own list of subqueries from parsing on #2666) in the action of the first SelectPrimary alternative. The Select action moves the subqueries of the statement's ORDER BY, the last entries of the current list, to the head's list with new positions.

  6. Run yarn jison and 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

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 needs SELECT * 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 jison has regenerated src/alasqlparser.js, yarn test-parser passes, 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).

Sub-issues

Sub-issues

0 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. mathiasrw commented on Oct 20, 2026

    @mathiasrw
    MemberAuthor

    Part of the set operation design in #2718.

  2. added this to the v4.x Focus milestone on Nov 26, 2026
  3. changed the title [-]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[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions