|
| 1 | +-- #! ../data/sales_data.csv |
| 2 | + |
| 3 | +-- ================================================================ |
| 4 | +-- ORDER BY Expression Support Examples |
| 5 | +-- ================================================================ |
| 6 | +-- Demonstrates the new ORDER BY expression support with automatic |
| 7 | +-- aggregate rewriting via the OrderByAliasTransformer. |
| 8 | +-- |
| 9 | +-- The transformer automatically rewrites ORDER BY aggregate expressions |
| 10 | +-- to use aliases from the SELECT clause, enabling standard SQL syntax. |
| 11 | +-- ================================================================ |
| 12 | + |
| 13 | +-- Example 1: Basic ORDER BY with aggregate (most common pattern) |
| 14 | +-- The SUM() in ORDER BY is automatically rewritten to use the 'total' alias |
| 15 | +SELECT |
| 16 | + region, |
| 17 | + SUM(sales_amount) AS total |
| 18 | +FROM sales |
| 19 | +GROUP BY region |
| 20 | +ORDER BY SUM(sales_amount) DESC; |
| 21 | +GO |
| 22 | + |
| 23 | +-- Example 2: COUNT(*) without explicit alias |
| 24 | +-- Transformer auto-generates an alias (expr_N) and uses it |
| 25 | +SELECT |
| 26 | + region, |
| 27 | + COUNT(*) AS count |
| 28 | +FROM sales |
| 29 | +GROUP BY region |
| 30 | +ORDER BY COUNT(*) DESC; |
| 31 | +GO |
| 32 | + |
| 33 | +-- Example 3: Multiple aggregates in ORDER BY |
| 34 | +-- Both aggregates are rewritten to use their respective aliases |
| 35 | +SELECT |
| 36 | + region, |
| 37 | + SUM(sales_amount) AS total_sales, |
| 38 | + AVG(sales_amount) AS avg_sales |
| 39 | +FROM sales |
| 40 | +GROUP BY region |
| 41 | +ORDER BY SUM(sales_amount) DESC, AVG(sales_amount) ASC; |
| 42 | +GO |
| 43 | + |
| 44 | +-- Example 4: Mix of aggregate and simple column in ORDER BY |
| 45 | +SELECT |
| 46 | + region, |
| 47 | + product, |
| 48 | + SUM(sales_amount) AS total |
| 49 | +FROM sales |
| 50 | +GROUP BY region, product |
| 51 | +ORDER BY region ASC, SUM(sales_amount) DESC; |
| 52 | +GO |
| 53 | + |
| 54 | +-- Example 5: Using MIN and MAX aggregates |
| 55 | +SELECT |
| 56 | + region, |
| 57 | + MIN(sales_amount) AS min_sale, |
| 58 | + MAX(sales_amount) AS max_sale |
| 59 | +FROM sales |
| 60 | +GROUP BY region |
| 61 | +ORDER BY MAX(sales_amount) DESC, MIN(sales_amount) ASC; |
| 62 | +GO |
| 63 | + |
| 64 | +-- Example 6: COUNT without explicit alias in SELECT |
| 65 | +-- Parser auto-generates expr_N, transformer uses that |
| 66 | +SELECT |
| 67 | + region, |
| 68 | + COUNT(*) |
| 69 | +FROM sales |
| 70 | +GROUP BY region |
| 71 | +ORDER BY COUNT(*) DESC; |
| 72 | +GO |
| 73 | + |
| 74 | +-- Example 7: AVG with ORDER BY |
| 75 | +SELECT |
| 76 | + product, |
| 77 | + AVG(sales_amount) AS average_price |
| 78 | +FROM sales |
| 79 | +GROUP BY product |
| 80 | +ORDER BY AVG(sales_amount) DESC; |
| 81 | +GO |
| 82 | + |
| 83 | +-- ================================================================ |
| 84 | +-- How It Works |
| 85 | +-- ================================================================ |
| 86 | +-- |
| 87 | +-- Before transformation: |
| 88 | +-- ORDER BY SUM(sales_amount) DESC |
| 89 | +-- |
| 90 | +-- After transformation: |
| 91 | +-- ORDER BY total DESC |
| 92 | +-- |
| 93 | +-- The OrderByAliasTransformer: |
| 94 | +-- 1. Finds all aggregates in SELECT clause |
| 95 | +-- 2. Maps them to their aliases (or generates aliases) |
| 96 | +-- 3. Rewrites matching aggregates in ORDER BY to use the aliases |
| 97 | +-- 4. Executor only sees simple column references |
| 98 | +-- |
| 99 | +-- Use --show-transformations flag to see the rewriting in action! |
| 100 | +-- ================================================================ |
| 101 | + |
| 102 | +-- Example 8: Testing with --show-transformations |
| 103 | +-- Run this with: ./target/release/sql-cli -f examples/order_by_expressions.sql --show-transformations |
| 104 | +SELECT |
| 105 | + region, |
| 106 | + COUNT(*) AS num_sales, |
| 107 | + SUM(sales_amount) AS total, |
| 108 | + AVG(sales_amount) AS average |
| 109 | +FROM sales |
| 110 | +GROUP BY region |
| 111 | +ORDER BY SUM(sales_amount) DESC, COUNT(*) DESC; |
| 112 | +GO |
0 commit comments