Fix UNION column alias aggregation#630
Merged
Merged
Conversation
When the same alias appears in multiple UNION branches, aggregate
all source columns into a list instead of overwriting with the last
occurrence.
Before:
SELECT a.A as M FROM tab1 a UNION SELECT b.B as M FROM tab2 b
columns_aliases = {"M": "tab2.B"} # Lost tab1.A
After:
columns_aliases = {"M": ["tab1.A", "tab2.B"]}
Implementation:
- Modified _Collector.add_alias to check for existing alias entries
- When duplicate found, convert single value to list or append to list
- Deduplicates identical sources (same alias pointing to same column)
Fixes macbre#401
collerek
reviewed
May 13, 2026
Comment on lines
+203
to
+216
| self.alias_map[name] = target | ||
| if name in self.alias_map: | ||
| # Alias already exists — aggregate targets into a list | ||
| existing = self.alias_map[name] | ||
| if isinstance(existing, list): | ||
| # Already a list — append new target | ||
| if target not in existing: | ||
| existing.append(target) | ||
| else: | ||
| # Single value — convert to list | ||
| if existing != target: | ||
| self.alias_map[name] = [existing, target] | ||
| else: | ||
| # First occurrence — store as-is | ||
| self.alias_map[name] = target |
Collaborator
There was a problem hiding this comment.
Good catch, thanks :)
Although we need some more fixes as this change will nest lists if the alias is referencing multiple columns, since this change changes the global resolution, not only the unions:
def test_union_alias_with_expression_targets():
# Case A: scalar then list-target → produces a NESTED list
q1 = """
SELECT a AS x FROM t1
UNION ALL
SELECT b + c AS x FROM t2
"""
p = Parser(q1)
# PR currently returns {'x': ['a', ['b', 'c']]} — nested
assert p.columns_aliases == {"x": ["a", "b", "c"]}
# Case B: list then list-target → TypeError: unhashable type: 'UniqueList'
q2 = """
SELECT a + b AS x FROM t1
UNION ALL
SELECT c + d AS x FROM t2
"""
p = Parser(q2)
# PR currently raises TypeError: unhashable type: 'UniqueList.'
assert p.columns_aliases == {"x": ["a", "b", "c", "d"]}we need something like:
def add_alias(self, name: str, target: Any, clause: str) -> None:
self.alias_names.append(name)
if clause:
self.alias_dict.setdefault(clause, UniqueList()).append(name)
if target is None:
return
existing = self.alias_map.get(name, [])
merged = UniqueList(existing if isinstance(existing, list) else [existing])
merged.extend(target if isinstance(target, list) else [target])
self.alias_map[name] = merged if len(merged) > 1 else merged[0]
collerek
requested changes
May 13, 2026
Per @collerek review: the prior add_alias logic produced nested lists when a scalar target was followed by a list target, and raised TypeError when both targets were UniqueList (unhashable in 'not in'). Adopt the reviewer's suggested implementation: normalize existing into a UniqueList, extend with target (list or scalar), collapse to scalar when len==1. Add regression tests for both cases.
Contributor
Author
|
Thanks for the careful review @collerek — both regressions reproduce exactly as you described:
Pushed 4ca0cd6 adopting your suggested implementation verbatim. Both regression cases are now covered in |
collerek
approved these changes
May 14, 2026
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.
Summary
columns_aliasesnow aggregates all source columns into a list instead of silently overwritingFixes #401
Test plan
pytest test/ -v: 270 tests passtest_union_column_aliasesverifies{"M": ["tab1.A", "tab2.B"]}for UNION ALL with matching aliases