Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 26, 2025

Fixes the parse error that occurred when using ORDER BY before a UNION clause in SQL queries.

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'

Changes Made

  • Modified src/alasqlparser.jison: Updated the Select grammar rule to support the sequence OrderClause LimitClause UnionClause OrderClause LimitClause. This allows ORDER BY and LIMIT clauses to appear both before and after UNION operations.
  • Regenerated src/alasqlparser.js: Rebuilt the parser from the updated jison grammar file.
  • Added test/test2362.js: Created comprehensive tests with 14 test cases to verify that ORDER BY and LIMIT can be used before UNION without parse errors and that existing UNION functionality remains intact.

The grammar now supports:

  1. SELECT ... ORDER BY ... UNION ... (ORDER BY before UNION)
  2. SELECT ... UNION ... ORDER BY ... (ORDER BY after UNION)
  3. SELECT ... ORDER BY ... UNION ... ORDER BY ... (both positions)

Testing

  • ✅ All 14 test cases in test2362.js pass, covering comprehensive edge cases:
    • Original issue scenario (ORDER BY before UNION)
    • 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 in various positions
    • ORDER BY with multiple columns before UNION
    • ORDER BY with expressions before UNION
    • LIMIT with OFFSET before UNION
  • ✅ All existing UNION tests (test017.js, test671.js) still pass
  • ✅ No security vulnerabilities detected
  • ✅ Verified with a sample of other tests to ensure no regressions

The fix is minimal and focused, affecting only the grammar to accept the previously invalid syntax without altering the execution semantics.

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 <[email protected]>
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 <[email protected]>
Copilot AI requested a review from mathiasrw January 2, 2026 10:09
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