Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 26, 2025

Implements SQL-99 compliant support for using ORDER BY with UNION operations by adding parenthesized SELECT statement support.

Issue

The parser was throwing a syntax error when ORDER BY was used before UNION:

SELECT country, COUNT(*) AS competitors
FROM foreignCompetitors
GROUP BY country
ORDER BY country
UNION
SELECT "Total: " AS country, COUNT(*) AS competitors
FROM foreignCompetitors;

Error: Parse error on line 6: ...Y countryORDER BY UNIONSELECT "Total: ---------------------^ ... got 'UNION'

Solution

According to SQL-99 standard, ORDER BY before UNION requires the SELECT statement to be wrapped in parentheses. The fix implements proper parenthesized SELECT support:

(SELECT country, COUNT(*) AS competitors
FROM foreignCompetitors
GROUP BY country
ORDER BY country)
UNION
SELECT "Total: " AS country, COUNT(*) AS competitors
FROM foreignCompetitors;

Changes Made

  • Added ParenthesizedSelect grammar rule: New rule LPAR Select RPAR allows any SELECT statement to be wrapped in parentheses, including those with ORDER BY and LIMIT clauses
  • Updated UnionClause: Modified to accept both SelectWithoutOrderOrLimit (for standard queries) and ParenthesizedSelect (for queries with ORDER BY/LIMIT before UNION), supporting all set operations: UNION, UNION ALL, EXCEPT, INTERSECT, and their CORRESPONDING variants
  • Updated Select rule: Added alternative production that allows starting with ParenthesizedSelect followed by UnionClause OrderClause LimitClause, enabling proper chaining
  • Regenerated src/alasqlparser.js: Rebuilt the parser from the updated jison grammar file
  • Created test/test2414.js: Comprehensive tests with 16 test cases verifying SQL-99 compliant syntax works correctly

Supported Syntax (SQL-99 Compliant)

  1. (SELECT ... ORDER BY ...) UNION SELECT ... - Parenthesized first SELECT with ORDER BY
  2. (SELECT ... ORDER BY ...) UNION (SELECT ... ORDER BY ...) - Both SELECTs parenthesized
  3. SELECT ... UNION ... ORDER BY ... - Standard syntax, ORDER BY applies to entire result
  4. Complex nested combinations with multiple UNIONs

Testing

  • ✅ All 16 test cases in test2414.js pass, covering:
    • Parenthesized SELECT with ORDER BY/LIMIT before UNION/EXCEPT/INTERSECT
    • Both SELECTs parenthesized with ORDER BY+LIMIT
    • Standard UNION with ORDER BY at end (without parentheses)
    • Complex nested parenthesized UNIONs
    • ORDER BY with multiple columns and expressions
    • LIMIT with OFFSET
  • ✅ All existing UNION tests (test017.js, test671.js) still pass
  • ✅ No security vulnerabilities detected
  • ✅ No breaking changes to existing functionality

The implementation is minimal, focused, and fully compliant with the SQL-99 standard for set operations with ORDER BY clauses.

Original prompt

This section details on the original issue you should resolve

<issue_title>UNION with ORDER BY doesn't seem to work as expected</issue_title>
<issue_description>If I do

SELECT country, COUNT(*) AS competitors
FROM foreignCompetitors
GROUP BY country
ORDER BY country
UNION
SELECT "Total: " AS country, COUNT(*) AS competitors
FROM foreignCompetitors;

I get error: Parse error on line 6: ...Y countryORDER BY UNIONSELECT "Total: ---------------------^ Expecting 'LITERAL', 'BRALITERAL', 'LPAR', 'NUMBER', 'STRING', 'SHARP', 'DOLLAR', 'AT', 'VALUE', 'COLON', 'NOT', 'IF', 'PLUS', 'STAR', 'QUESTION', 'INSERTED', 'FIRST', 'LAST', 'DELETED', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'JAVASCRIPT', 'NEW', 'CAST', 'CONVERT', 'SUM', 'TOTAL', 'COUNT', 'MIN', 'MAX', 'AVG', 'AGGR', 'ARRAY', 'REPLACE', 'DATEADD', 'DATEDIFF', 'TIMESTAMPDIFF', 'INTERVAL', 'TRUE', 'FALSE', 'NSTRING', 'NULL', 'EXISTS', 'ARRAYLBRA', 'BRAQUESTION', 'CASE', 'TILDA', 'MINUS', 'ATLBRA', 'LCUR', got 'UNION'

If I remove the ORDER BY clause it works.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@mathiasrw
Copy link
Member

Continue

@mathiasrw
Copy link
Member

@copilot Continue...

- Modified alasqlparser.jison to allow ORDER BY before UNION
- Updated Select grammar rule to support OrderClause LimitClause UnionClause OrderClause LimitClause sequence
- Regenerated alasqlparser.js from jison grammar
- Added test2362.js to verify the fix
- All existing UNION tests still pass

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 2, 2026

Continue

I've completed the fix for the UNION with ORDER BY issue. The parse error has been resolved by modifying the grammar to allow ORDER BY clauses before UNION statements. All tests are passing. (commit: 1830831)

@copilot Continue...

Done! The fix is complete. The grammar now accepts ORDER BY before UNION, resolving the parse error from the issue. (commit: 1830831)

Copilot AI changed the title [WIP] Fix UNION with ORDER BY not working as expected Fix UNION with ORDER BY parsing error Jan 2, 2026
Copilot AI and others added 2 commits January 2, 2026 10:04
- Added 12 new test cases covering various combinations:
  - ORDER BY DESC before UNION
  - LIMIT before UNION
  - ORDER BY + LIMIT before UNION
  - ORDER BY/LIMIT both before and after UNION
  - UNION ALL with ORDER BY before
  - EXCEPT with ORDER BY before
  - INTERSECT with ORDER BY before
  - Multiple UNIONs with ORDER BY
  - ORDER BY with multiple columns
  - ORDER BY with expressions
  - LIMIT with OFFSET before UNION
- All 14 tests pass
- All existing UNION tests still pass

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI requested a review from mathiasrw January 2, 2026 10:09
@mathiasrw
Copy link
Member

mathiasrw commented Jan 2, 2026

Turns out this is not SQL-99 compliant. SELECT ... ORDER BY ... UNION ... is not supported. and SELECT ... UNION ... ORDER BY ... should order all of the results.

We will have to make it so we can wrap the selects in () and then we konw they belong.

so we must make it

(SELECT ... ORDER BY ...) UNION ...

and

`(SELECT ... ORDER BY ...) UNION (... ORDER BY) ... ORDER BY ...`

In the last example it does not really make sense as the order is set twice, but the notation should be supported.

@copilot

@mathiasrw
Copy link
Member

@copilot

- Reverted previous non-compliant approach that allowed ORDER BY before UNION without parentheses
- Added ParenthesizedSelect grammar rule to support (SELECT ... ORDER BY ...) UNION ...
- Updated UnionClause to accept both SelectWithoutOrderOrLimit and ParenthesizedSelect
- Updated Select rule to support starting with ParenthesizedSelect for chaining
- Rewrote all test cases in test2414.js to use SQL-99 compliant parenthesized syntax
- Added 16 comprehensive test cases covering:
  - Parenthesized SELECT with ORDER BY/LIMIT before UNION/EXCEPT/INTERSECT
  - Both SELECTs parenthesized with ORDER BY+LIMIT
  - Standard UNION with ORDER BY at end (without parentheses)
  - Complex nested parenthesized UNIONs
- All tests pass including existing UNION tests

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 2, 2026

Turns out this is not SQL-99 compliant. SELECT ... ORDER BY ... UNION ... is not supported. and SELECT ... UNION ... ORDER BY ... should order all of the results.

...

Implemented SQL-99 compliant solution. Now supports parenthesized SELECT statements: (SELECT ... ORDER BY ...) UNION ... and (SELECT ... ORDER BY ...) UNION (SELECT ... ORDER BY ...). The grammar now properly requires parentheses for ORDER BY before UNION, while still supporting standard SELECT ... UNION ... ORDER BY ... syntax. All 16 test cases pass. (commit: 26b9bfc)

@copilot

Done! The grammar now supports SQL-99 compliant parenthesized SELECT statements with ORDER BY before UNION. Tests cover all the cases you mentioned including (SELECT ... ORDER BY ...) UNION ... and complex nested scenarios. (commit: 26b9bfc)

Copilot AI changed the title Fix UNION with ORDER BY parsing error Implement SQL-99 compliant parenthesized SELECT support for UNION with ORDER BY Jan 2, 2026
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.

UNION with ORDER BY doesn't seem to work as expected

2 participants