Expand description
WHERE clause alias expansion transformer
This transformer allows users to reference SELECT clause aliases in WHERE clauses by automatically expanding those aliases to their full expressions.
§Problem
Users often want to reference complex SELECT expressions by their aliases in WHERE:
SELECT a, a * 2 as double_a FROM t WHERE double_a > 10This fails because WHERE is evaluated before SELECT, so aliases don’t exist yet.
§Solution
The transformer rewrites to:
SELECT a, a * 2 as double_a FROM t WHERE a * 2 > 10§Algorithm
- Extract all aliases from SELECT clause and their corresponding expressions
- Scan WHERE clause for column references
- If a column reference matches an alias name, replace it with the full expression
- Handle nested expressions (BinaryOp, CASE, etc.) recursively
§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§
- Where
Alias Expander - Transformer that expands SELECT aliases in WHERE clauses