Expand description
GROUP BY clause alias expansion transformer
This transformer allows users to reference SELECT clause aliases in GROUP BY clauses by automatically expanding those aliases to their full expressions.
§Problem
Users often want to group by a complex expression using its alias:
SELECT id % 3 as grp, COUNT(*) FROM t GROUP BY grpThis fails because GROUP BY is evaluated before SELECT, so aliases don’t exist yet.
§Solution
The transformer rewrites to:
SELECT id % 3 as grp, COUNT(*) FROM t GROUP BY id % 3§Algorithm
- Extract all aliases from SELECT clause and their corresponding expressions
- Scan GROUP BY clause for column references
- If a column reference matches an alias name, replace it with the full expression
- Only expand simple column references (not qualified table.column)
§Limitations
- Only works for simple column aliases (not table.alias references)
- Aliases take precedence over actual column names if they conflict
- Complex expressions are duplicated (no common subexpression elimination)
Structs§
- Group
ByAlias Expander - Transformer that expands SELECT aliases in GROUP BY clauses