Module order_by_alias_transformer

Module order_by_alias_transformer 

Source
Expand description

ORDER BY clause alias transformer

This transformer rewrites ORDER BY clauses that reference aggregate functions to use the aliases from the SELECT clause instead.

§Problem

Users often write queries like:

SELECT region, SUM(sales_amount) AS total
FROM sales
GROUP BY region
ORDER BY SUM(sales_amount) DESC

This fails because the parser treats SUM(sales_amount) as a column name “SUM” which doesn’t exist.

§Solution

The transformer rewrites to:

SELECT region, SUM(sales_amount) AS total
FROM sales
GROUP BY region
ORDER BY total DESC

§Algorithm

  1. Find all aggregate functions in SELECT clause and their aliases
  2. Scan ORDER BY clause for column names that match aggregate patterns
  3. Replace with the corresponding alias from SELECT

§Note

This transformer works at the string level since ORDER BY currently only supports column names, not full expressions in the AST.

Structs§

OrderByAliasTransformer
Transformer that rewrites ORDER BY to use aggregate aliases