-
Notifications
You must be signed in to change notification settings - Fork 690
Implement SQL-99 compliant parenthesized SELECT support for UNION with ORDER BY #2417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
|
Continue |
|
@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>
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)
Done! The fix is complete. The grammar now accepts ORDER BY before UNION, resolving the parse error from the issue. (commit: 1830831) |
- 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>
|
Turns out this is not SQL-99 compliant. We will have to make it so we can wrap the selects in () and then we konw they belong. so we must make it and In the last example it does not really make sense as the order is set twice, but the notation should be supported. |
- 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>
... Implemented SQL-99 compliant solution. Now supports parenthesized SELECT statements: Done! The grammar now supports SQL-99 compliant parenthesized SELECT statements with ORDER BY before UNION. Tests cover all the cases you mentioned including |
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:
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:
Changes Made
ParenthesizedSelectgrammar rule: New ruleLPAR Select RPARallows any SELECT statement to be wrapped in parentheses, including those with ORDER BY and LIMIT clausesUnionClause: Modified to accept bothSelectWithoutOrderOrLimit(for standard queries) andParenthesizedSelect(for queries with ORDER BY/LIMIT before UNION), supporting all set operations: UNION, UNION ALL, EXCEPT, INTERSECT, and their CORRESPONDING variantsSelectrule: Added alternative production that allows starting withParenthesizedSelectfollowed byUnionClause OrderClause LimitClause, enabling proper chainingsrc/alasqlparser.js: Rebuilt the parser from the updated jison grammar filetest/test2414.js: Comprehensive tests with 16 test cases verifying SQL-99 compliant syntax works correctlySupported Syntax (SQL-99 Compliant)
(SELECT ... ORDER BY ...) UNION SELECT ...- Parenthesized first SELECT with ORDER BY(SELECT ... ORDER BY ...) UNION (SELECT ... ORDER BY ...)- Both SELECTs parenthesizedSELECT ... UNION ... ORDER BY ...- Standard syntax, ORDER BY applies to entire resultTesting
The implementation is minimal, focused, and fully compliant with the SQL-99 standard for set operations with ORDER BY clauses.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.