-
Notifications
You must be signed in to change notification settings - Fork 690
Add SQL-99 compliant parenthesized SELECT support in UNION operations #2395
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
Co-authored-by: mathiasrw <[email protected]>
Modified grammar to allow ORDER BY and LIMIT before UNION operations. Added execution logic to apply ORDER BY/LIMIT to first SELECT before combining with UNION. Added comprehensive tests for UNION ALL and UNION with ORDER BY/LIMIT. Co-authored-by: mathiasrw <[email protected]>
…, execution logic needs refinement Grammar changes: - Created SelectForUnion rule for use in pattern 2 (ORDER BY before UNION) - Created UnionClauseWithOrder for pattern 2 - Added second Select alternative supporting pattern 2 - SelectWithoutOrderOrLimit reverted to original (no ORDER BY) for pattern 1 Execution logic: - Added check to detect pattern 2 (unionBranchHasOrder) - Apply ORDER BY/LIMIT before UNION only in pattern 2 - Pattern 1 regression: ORDER BY after UNION not working correctly yet Tests: - test_union_order_limit.js passes for pattern 2 - test7.js fails - ORDER BY after multiple UNIONs not sorting correctly - Need to investigate why ORDER BY DESC produces ASC results in pattern 1 Co-authored-by: mathiasrw <[email protected]>
mathiasrw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pattern 1 regression: SELECT ... UNION ALL SELECT ... ORDER BY ... (ORDER BY after all UNIONs) produces incorrect sort order. Test7 shows [10,20,30] instead of expected [30,20,10] for DESC ordering. Root cause under investigation - likely order key population timing issue.
Is this still an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like wayyy too much code for this problem. Is there a better way to solve this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right - this is too complex. The root issue is that the grammar doesn't support ORDER BY/LIMIT on individual SELECTs in a UNION without parentheses due to parsing ambiguity.
The standard SQL solution is to use parentheses: (SELECT ... ORDER BY ... LIMIT ...) UNION ALL (SELECT ... ORDER BY ... LIMIT ...), but AlaSQL doesn't support parenthesized SELECT statements yet.
A simpler approach would be:
- Add support for parenthesized SELECT statements (requires grammar changes but cleaner)
- Document that this syntax requires parentheses
- Remove the complex two-alternative approach and detection logic
The current ~40 lines in src/38query.js could be reduced to just handling the parenthesized case. Should I implement the parentheses-based solution instead?
Yes, this is still an issue. After investigation, the problem is that supporting ORDER BY/LIMIT on individual SELECTs in a UNION without parentheses creates fundamental grammar ambiguity. The parser can't distinguish between:
Standard SQL requires parentheses for the second pattern: I recommend either:
The current implementation has 140+ lines of grammar and execution changes, which is indeed complex for this problem. |
lets provide the ability to do these in parenthesis and adhire to SQL-99. But we also want to provide backwards compatability. if the last union got order by or limit, its for the whole query (like now) so if you want orderby or limit on the last union only you need to use a parenthesis. |
Co-authored-by: mathiasrw <[email protected]>
|
@copilot Continue |
Implementation complete (commit b5ef978). Added SQL-99 compliant parenthesized SELECT support:
The original issue can be solved by using parentheses on UNION branches or separating queries. |
UNION ALL queries with ORDER BY and LIMIT on each individual SELECT were failing with parse errors. This PR implements SQL-99 compliant syntax using parentheses to support ORDER BY and LIMIT on individual SELECT statements within UNION operations, while maintaining full backwards compatibility.
Previously this would fail with:
Parse error: Expecting 'EOF', got 'UNION'Changes
Parser Grammar (
src/alasqlparser.jison)ParenthesizedSelectstatement type for top-level parenthesized SELECT supportLPAR Select RPARalternatives toUnionClausefor all UNION operations (UNION, UNION ALL, EXCEPT, INTERSECT, and CORRESPONDING variants)LPAR Select RPAR UnionClause OrderClause LimitClausealternative toSelectruleQuery Execution (
src/38query.js)Tests (
test/test_union_order_limit.js)Backwards Compatibility
✅ All backwards compatibility tests pass (14 tests in test7.js)
SELECT ... UNION ALL SELECT ... ORDER BY ...works exactly as beforeSQL-99 Compliant Feature
✅ Use parentheses for ORDER BY/LIMIT on individual SELECTs in UNION branches
SELECT ... UNION ALL (SELECT ... ORDER BY ... LIMIT ...)Solution for Original Issue
The original query can now be written using parentheses on UNION branches:
Alternatively, use separate queries combined in JavaScript:
Test Results
Summary: Clean, minimal implementation that adds SQL-99 standard parenthesized SELECT support to UNION operations while maintaining 100% backwards compatibility.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.