|
| 1 | +-- #! ../data/test_simple_math.csv |
| 2 | +-- Alias Expansion Transformers - Feature Reference |
| 3 | +-- ================================================ |
| 4 | +-- This file demonstrates what the preprocessing pipeline's alias expansion |
| 5 | +-- transformers support, and what could be supported with additional work. |
| 6 | +-- |
| 7 | +-- USAGE: |
| 8 | +-- sql-cli examples/expansion_transformers.sql |
| 9 | +-- |
| 10 | +-- Or test individual queries: |
| 11 | +-- sql-cli data/test_simple_math.csv -q "SELECT ..." -o table |
| 12 | + |
| 13 | +-- ============================================================================ |
| 14 | +-- SUPPORTED FEATURES (Working Now!) |
| 15 | +-- ============================================================================ |
| 16 | + |
| 17 | +-- ---------------------------------------------------------------------------- |
| 18 | +-- Quick Win #1: HAVING Auto-Aliasing |
| 19 | +-- ---------------------------------------------------------------------------- |
| 20 | +-- Automatically adds aliases to aggregates and rewrites HAVING to use them |
| 21 | +-- Before: SELECT region, COUNT(*) FROM sales GROUP BY region HAVING COUNT(*) > 5 |
| 22 | +-- After: SELECT region, COUNT(*) as __agg_1 FROM sales GROUP BY region HAVING __agg_1 > 5 |
| 23 | + |
| 24 | +-- Example 1: Basic HAVING with auto-aliasing |
| 25 | +SELECT a, COUNT(*) |
| 26 | +FROM test_simple_math |
| 27 | +GROUP BY a |
| 28 | +HAVING COUNT(*) >= 1; |
| 29 | + |
| 30 | +-- Example 2: HAVING with GROUP BY alias (two transformers working together!) |
| 31 | +SELECT id % 3 as grp, COUNT(*), SUM(b) |
| 32 | +FROM test_simple_math |
| 33 | +GROUP BY grp |
| 34 | +HAVING COUNT(*) > 2 AND SUM(b) > 100; |
| 35 | + |
| 36 | +-- ---------------------------------------------------------------------------- |
| 37 | +-- Quick Win #2: WHERE Alias Expansion |
| 38 | +-- ---------------------------------------------------------------------------- |
| 39 | +-- Expands SELECT aliases to their full expressions in WHERE clauses |
| 40 | +-- Works with: =, <, >, <=, >=, !=, IS NULL, IS NOT NULL, AND, OR, NOT |
| 41 | + |
| 42 | +SELECT a, b, a * 2 as double_a |
| 43 | +FROM test_simple_math |
| 44 | +WHERE double_a > 10; |
| 45 | +GO |
| 46 | + |
| 47 | +SELECT a, b, b / 10 as tens |
| 48 | +FROM test_simple_math |
| 49 | +WHERE tens > 5; |
| 50 | +GO |
| 51 | + |
| 52 | +SELECT a, b, a * 2 as double_a, a * 3 as triple_a |
| 53 | +FROM test_simple_math |
| 54 | +WHERE double_a > 10 AND triple_a < 25; |
| 55 | +GO |
| 56 | + |
| 57 | +-- Arithmetic operators work |
| 58 | +SELECT a, a + 5 as plus5 |
| 59 | +FROM test_simple_math |
| 60 | +WHERE plus5 > 15; |
| 61 | +GO |
| 62 | + |
| 63 | +SELECT a, a - 3 as minus3 |
| 64 | +FROM test_simple_math |
| 65 | +WHERE minus3 < 5; |
| 66 | +GO |
| 67 | + |
| 68 | +SELECT a, a * 2 as times2 |
| 69 | +FROM test_simple_math |
| 70 | +WHERE times2 = 20; |
| 71 | +GO |
| 72 | + |
| 73 | +SELECT a, b / 10 as divided |
| 74 | +FROM test_simple_math |
| 75 | +WHERE divided >= 10; |
| 76 | +GO |
| 77 | + |
| 78 | +SELECT a, a % 3 as modulo |
| 79 | +FROM test_simple_math |
| 80 | +WHERE modulo = 0; |
| 81 | +GO |
| 82 | + |
| 83 | +-- IS NULL / IS NOT NULL work with aliases |
| 84 | +SELECT a, b, a * 2 as doubled |
| 85 | +FROM test_simple_math |
| 86 | +WHERE doubled IS NOT NULL; |
| 87 | +GO |
| 88 | + |
| 89 | +-- Multiple uses of same alias |
| 90 | +SELECT a, a * 2 as double_a |
| 91 | +FROM test_simple_math |
| 92 | +WHERE double_a > 10 AND double_a < 30; |
| 93 | +GO |
| 94 | + |
| 95 | +-- ---------------------------------------------------------------------------- |
| 96 | +-- Quick Win #3: GROUP BY Alias Expansion |
| 97 | +-- ---------------------------------------------------------------------------- |
| 98 | +-- Expands SELECT aliases to their full expressions in GROUP BY clauses |
| 99 | + |
| 100 | +SELECT id % 3 as grp, COUNT(*) |
| 101 | +FROM test_simple_math |
| 102 | +GROUP BY grp; |
| 103 | +GO |
| 104 | + |
| 105 | +SELECT b / 10 as tens, COUNT(*), AVG(a) as avg_a |
| 106 | +FROM test_simple_math |
| 107 | +GROUP BY tens; |
| 108 | +GO |
| 109 | + |
| 110 | +-- Multiple aliases in GROUP BY |
| 111 | +SELECT a % 2 as even_odd, a % 3 as mod3, COUNT(*) |
| 112 | +FROM test_simple_math |
| 113 | +GROUP BY even_odd, mod3; |
| 114 | +GO |
| 115 | + |
| 116 | +-- ---------------------------------------------------------------------------- |
| 117 | +-- All Three Transformers Working Together |
| 118 | +-- ---------------------------------------------------------------------------- |
| 119 | + |
| 120 | +SELECT id % 3 as grp, COUNT(*) as cnt, SUM(b) as total |
| 121 | +FROM test_simple_math |
| 122 | +WHERE b > 50 -- WHERE: can use simple comparisons |
| 123 | +GROUP BY grp -- GROUP BY: alias expansion |
| 124 | +HAVING cnt > 2 AND total > 100 -- HAVING: aggregate auto-aliasing |
| 125 | +ORDER BY total DESC; -- ORDER BY: works naturally (evaluated after SELECT) |
| 126 | +GO |
| 127 | + |
| 128 | +-- ---------------------------------------------------------------------------- |
| 129 | +-- DISTINCT with Expressions (Already Works) |
| 130 | +-- ---------------------------------------------------------------------------- |
| 131 | + |
| 132 | +SELECT DISTINCT a % 3 FROM test_simple_math; |
| 133 | +GO |
| 134 | + |
| 135 | +SELECT DISTINCT a % 2, a % 3 FROM test_simple_math; |
| 136 | +GO |
| 137 | + |
| 138 | +-- ============================================================================ |
| 139 | +-- END OF WORKING FEATURES |
| 140 | +-- ============================================================================ |
| 141 | +-- Everything above works perfectly when run with -q flag or in interactive mode. |
| 142 | +-- |
| 143 | +-- Test examples: |
| 144 | +-- sql-cli data/test_simple_math.csv -q "SELECT a, a*2 as double_a FROM test_simple_math WHERE double_a > 10" |
| 145 | +-- sql-cli data/test_simple_math.csv -q "SELECT id % 3 as grp, COUNT(*) FROM test_simple_math GROUP BY grp" |
| 146 | +-- sql-cli -q "SELECT value % 3 as grp, COUNT(*) as cnt FROM range(20) WHERE value > 5 GROUP BY grp HAVING cnt > 3" |
| 147 | + |
| 148 | +-- ============================================================================ |
| 149 | +-- FUTURE ENHANCEMENTS (Not Yet Supported) |
| 150 | +-- ============================================================================ |
| 151 | +-- The features below would require additional work on the WHERE evaluator |
| 152 | +-- or other parts of the query engine. They are documented here as potential |
| 153 | +-- future improvements. |
| 154 | + |
| 155 | +-- ---------------------------------------------------------------------------- |
| 156 | +-- CASE Expressions in WHERE with Aliases |
| 157 | +-- ---------------------------------------------------------------------------- |
| 158 | +-- Status: NOT SUPPORTED |
| 159 | +-- Issue: WHERE evaluator doesn't handle CASE expressions |
| 160 | +-- Workaround: Use the full CASE expression in WHERE, or use a CTE |
| 161 | +-- Example that currently fails: |
| 162 | + |
| 163 | +-- SELECT value, |
| 164 | +-- CASE WHEN value > 5 THEN 'high' ELSE 'low' END as category |
| 165 | +-- FROM range(10) |
| 166 | +-- WHERE category = 'high'; |
| 167 | + |
| 168 | +-- Workaround using CTE: |
| 169 | +-- WITH categorized AS ( |
| 170 | +-- SELECT value, |
| 171 | +-- CASE WHEN value > 5 THEN 'high' ELSE 'low' END as category |
| 172 | +-- FROM range(10) |
| 173 | +-- ) |
| 174 | +-- SELECT * FROM categorized WHERE category = 'high'; |
| 175 | + |
| 176 | +-- ---------------------------------------------------------------------------- |
| 177 | +-- BETWEEN with Expression Aliases |
| 178 | +-- ---------------------------------------------------------------------------- |
| 179 | +-- Status: NOT SUPPORTED |
| 180 | +-- Issue: WHERE evaluator expects simple column references in BETWEEN |
| 181 | +-- Example that currently fails: |
| 182 | + |
| 183 | +-- SELECT value, value * 2 as doubled |
| 184 | +-- FROM range(10) |
| 185 | +-- WHERE doubled BETWEEN 10 AND 20; |
| 186 | + |
| 187 | +-- Workaround: Use comparison operators |
| 188 | +-- SELECT value, value * 2 as doubled |
| 189 | +-- FROM range(10) |
| 190 | +-- WHERE doubled >= 10 AND doubled <= 20; |
| 191 | + |
| 192 | +-- ---------------------------------------------------------------------------- |
| 193 | +-- IN with Expression Aliases |
| 194 | +-- ---------------------------------------------------------------------------- |
| 195 | +-- Status: NOT SUPPORTED |
| 196 | +-- Issue: WHERE evaluator expects simple column references in IN lists |
| 197 | +-- Example that currently fails: |
| 198 | + |
| 199 | +-- SELECT value, value * 2 as doubled |
| 200 | +-- FROM range(10) |
| 201 | +-- WHERE doubled IN (4, 6, 8); |
| 202 | + |
| 203 | +-- Workaround: Use multiple OR conditions |
| 204 | +-- SELECT value, value * 2 as doubled |
| 205 | +-- FROM range(10) |
| 206 | +-- WHERE doubled = 4 OR doubled = 6 OR doubled = 8; |
| 207 | + |
| 208 | +-- ---------------------------------------------------------------------------- |
| 209 | +-- Subquery Column References |
| 210 | +-- ---------------------------------------------------------------------------- |
| 211 | +-- Status: COMPLEX - Not a simple enhancement |
| 212 | +-- Would require significant changes to subquery evaluation |
| 213 | + |
| 214 | +-- ---------------------------------------------------------------------------- |
| 215 | +-- JOIN ON with Aliases |
| 216 | +-- ---------------------------------------------------------------------------- |
| 217 | +-- Status: UNCLEAR SEMANTICS |
| 218 | +-- Issue: Which table's alias? Confusing for users |
| 219 | +-- Probably not worth supporting |
| 220 | + |
| 221 | +-- ============================================================================ |
| 222 | +-- IMPLEMENTATION NOTES |
| 223 | +-- ============================================================================ |
| 224 | + |
| 225 | +-- The preprocessing pipeline runs transformers in this order: |
| 226 | +-- 1. ExpressionLifter - Lifts window functions and column alias dependencies |
| 227 | +-- 2. WhereAliasExpander - Expands SELECT aliases in WHERE (Quick Win #2) |
| 228 | +-- 3. GroupByAliasExpander - Expands SELECT aliases in GROUP BY (Quick Win #3) |
| 229 | +-- 4. HavingAliasTransformer - Adds aliases to aggregates, rewrites HAVING (Quick Win #1) |
| 230 | +-- 5. CTEHoister - Hoists nested CTEs to top level |
| 231 | +-- 6. InOperatorLifter - Optimizes large IN expressions |
| 232 | + |
| 233 | +-- Performance Impact: |
| 234 | +-- - Transformation overhead: ~0.06ms total for all transformers |
| 235 | +-- - Zero runtime overhead - aliases expanded at query planning time |
| 236 | +-- - Same execution performance as writing full expressions |
| 237 | + |
| 238 | +-- SQL Evaluation Order (Standard): |
| 239 | +-- FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT |
| 240 | +-- |
| 241 | +-- This is why: |
| 242 | +-- - WHERE needs alias expansion (evaluated before SELECT) |
| 243 | +-- - GROUP BY needs alias expansion (evaluated before SELECT) |
| 244 | +-- - HAVING needs alias expansion (evaluated before SELECT, but after GROUP BY) |
| 245 | +-- - ORDER BY works naturally (evaluated after SELECT, aliases already exist) |
| 246 | + |
| 247 | +-- ============================================================================ |
| 248 | +-- TESTING |
| 249 | +-- ============================================================================ |
| 250 | + |
| 251 | +-- Run the supported features: |
| 252 | +-- $ sql-cli examples/expansion_transformers.sql |
| 253 | + |
| 254 | +-- Run with preprocessing details: |
| 255 | +-- $ sql-cli data/test_simple_math.csv -q "SELECT a, a*2 as d FROM test_simple_math WHERE d > 10" --show-preprocessing |
| 256 | + |
| 257 | +-- Test individual features: |
| 258 | +-- $ ./tests/integration/test_where_alias_expansion.sh |
| 259 | +-- $ ./tests/integration/test_group_by_alias_expansion.sh |
| 260 | +-- $ ./tests/integration/test_having_auto_alias.sh |
| 261 | + |
| 262 | +-- Demo scripts: |
| 263 | +-- $ ./scripts/demo_where_alias_expansion.sh |
| 264 | +-- $ ./scripts/demo_group_by_alias_expansion.sh |
| 265 | +-- $ ./scripts/demo_having_auto_alias.sh |
0 commit comments