[Refactor] UNION support on Query Parser and Formatter#105
Merged
HazelYuAhiru merged 2 commits intomainfrom Apr 9, 2026
Merged
[Refactor] UNION support on Query Parser and Formatter#105HazelYuAhiru merged 2 commits intomainfrom
HazelYuAhiru merged 2 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds AST + parser/formatter support for SQL UNION / UNION ALL by introducing a compound-query node and round-tripping it through mo_sql_parsing’s JSON format, along with enabling previously-skipped UNION-related test cases.
Changes:
- Added
NodeType.COMPOUND_QUERYand a newCompoundQueryNodeto representUNION/UNION ALLas a left-associative binary tree. - Updated
QueryParserto detect/parseunion/union_alldicts (including inFROM/JOINsources) and return aNode(query or compound query) at the top level. - Updated
QueryFormatterto convertCompoundQueryNodeback intomo_sql_parsing’s union JSON (including a special-case for unaliased UNION subqueries inFROM) and enabled UNION test cases + expected ASTs.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
core/ast/enums.py |
Adds a new NodeType for compound queries. |
core/ast/node.py |
Introduces CompoundQueryNode to model UNION/UNION ALL. |
core/query_parser.py |
Adds UNION detection + parsing and refactors FROM/JOIN source handling to support UNION subqueries. |
core/query_formatter.py |
Adds compound-query JSON formatting and a FROM-formatting special case for unaliased UNION subqueries. |
data/asts.py |
Adds expected ASTs for UNION-related queries (15/29/32) and widens get_ast return type. |
tests/ast_util.py |
Extends AST visualization to handle compound queries; widens type annotations to Node. |
tests/test_query_parser.py |
Enables parser tests for UNION queries. |
tests/test_query_formatter.py |
Enables formatter tests for UNION queries. |
tests/test_parser_formatter_e2e.py |
Enables parse→format→parse round-trip tests for UNION queries. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Add UNION and UNION ALL support to the query parser and formatter.
Code Changes
COMPOUND_QUERYtoNodeTypeenum incore/ast/enums.py.CompoundQueryNodetocore/ast/node.py. Represents a binary UNION / UNION ALL as a left-associative tree; chains of the same operator (e.g.A UNION B UNION C) are flattened back to flat lists by the formatter to match mo_sql_parsing's wire format.core/query_parser.py: added a few helper functions to detect and parse set-operator dicts from mo_sql_parsing. Refactored_build_from_sourceto handle UNION subqueries in FROM/JOIN sources uniformly.core/query_formatter.py: added_collect_union_branchesandcompound_to_mosql_jsonto convert the binary tree back to mo_sql_parsing's flat-list JSON. Added a special case informat_fromfor bare (unaliased) UNION subqueries, which mo_sql_parsing represents as a plain dict rather than a wrapped list entry.