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) DESCThis 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
- Find all aggregate functions in SELECT clause and their aliases
- Scan ORDER BY clause for column names that match aggregate patterns
- 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§
- Order
ByAlias Transformer - Transformer that rewrites ORDER BY to use aggregate aliases