Expand description
HAVING clause auto-aliasing transformer
This transformer automatically adds aliases to aggregate functions in SELECT clauses and rewrites HAVING clauses to use those aliases instead of the aggregate function expressions.
§Problem
Users often write queries like:
SELECT region, COUNT(*) FROM sales GROUP BY region HAVING COUNT(*) > 5This fails because the executor can’t evaluate COUNT(*) in the HAVING
clause - it needs a column reference.
§Solution
The transformer rewrites to:
SELECT region, COUNT(*) as __agg_1 FROM sales GROUP BY region HAVING __agg_1 > 5§Algorithm
- Find all aggregate functions in SELECT clause
- For each aggregate without an explicit alias, generate one (__agg_N)
- Scan HAVING clause for matching aggregate expressions
- Replace aggregate expressions with column references to the aliases
Structs§
- Having
Alias Transformer - Transformer that adds aliases to aggregates and rewrites HAVING clauses