Module where_alias_expander

Module where_alias_expander 

Source
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 > 10

This 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

  1. Extract all aliases from SELECT clause and their corresponding expressions
  2. Scan WHERE clause for column references
  3. If a column reference matches an alias name, replace it with the full expression
  4. 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§

WhereAliasExpander
Transformer that expands SELECT aliases in WHERE clauses