Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 26, 2025

  • Analyze the issue and understand SQL-99 compliance requirements
  • Revert previous non-compliant grammar changes
  • Implement ParenthesizedSelect grammar rule
  • Update UnionClause to accept ParenthesizedSelect
  • Update Select rule to support ParenthesizedSelect
  • Regenerate parser from jison file
  • Rewrite test cases to use SQL-99 compliant syntax:
    • (SELECT ... ORDER BY ...) UNION ...
    • (SELECT ... ORDER BY ...) UNION (SELECT ... ORDER BY ...)
    • Standard SELECT ... UNION ... ORDER BY ...
  • Build and test the fix
  • All 16 test cases passing
  • All existing UNION tests still pass
  • Updated all tests to use assert.deepEqual for complete outcome verification
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...

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
@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

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
Copilot AI and others added 5 commits January 4, 2026 21:35
- 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>
- 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 and others added 5 commits January 4, 2026 21:39
- 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>
- Updated all 16 test cases to use assert.deepEqual with exact expected results
- Replaced basic length checks with full result verification
- Ensures tests catch any regression in query behavior or result ordering
- All tests pass with precise expected output validation

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
@mathiasrw mathiasrw force-pushed the copilot/fix-union-order-by-issue branch from 28351be to 0b11e08 Compare January 4, 2026 10:51
@mathiasrw mathiasrw marked this pull request as ready for review January 4, 2026 10:54
@mathiasrw mathiasrw merged commit c4b46f3 into develop Jan 4, 2026
10 checks passed
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