|
5 | 5 | As of the execution mode unification completion, we have: |
6 | 6 |
|
7 | 7 | ✅ **Solid Foundation:** |
8 | | -- Unified execution path (`StatementExecutor` used by both `-q` and `-f` modes) |
9 | | -- 6 active preprocessor transformers (all enabled by default) |
| 8 | +- Unified execution path (`StatementExecutor` used by all modes: `-q`, `-f`, `--execute-statement`) |
| 9 | +- 7 active preprocessor transformers (all enabled by default) |
10 | 10 | - WHERE/GROUP BY alias expansion |
11 | 11 | - HAVING auto-aliasing |
| 12 | +- ORDER BY aggregate rewriting |
12 | 13 | - Expression lifting for window functions |
13 | 14 | - CTE hoisting (ready for subqueries) |
14 | 15 | - IN operator optimization |
@@ -70,23 +71,26 @@ GROUP BY region |
70 | 71 | --- |
71 | 72 |
|
72 | 73 | #### 2. Aggregate Expressions in ORDER BY |
73 | | -**Status:** ❌ Not implemented |
| 74 | +**Status:** ✅ **COMPLETED!** |
74 | 75 | **Difficulty:** Easy |
75 | 76 | **Example:** |
76 | 77 | ```sql |
77 | 78 | SELECT region, SUM(sales_amount) AS total |
78 | 79 | FROM sales |
79 | 80 | GROUP BY region |
80 | | -ORDER BY SUM(sales_amount) DESC -- This should work! |
| 81 | +ORDER BY SUM(sales_amount) DESC -- Works perfectly! |
81 | 82 | ``` |
82 | 83 |
|
83 | | -**Implementation Approach:** |
84 | | -- **Transformer:** `OrderByAggregateExpander` (similar to HavingAliasTransformer) |
85 | | -- Find aggregates in ORDER BY |
86 | | -- Match with SELECT clause aggregates |
87 | | -- Replace with alias or compute inline |
| 84 | +**Implementation:** |
| 85 | +- ✅ **Transformer:** `OrderByAliasTransformer` (implemented) |
| 86 | +- ✅ Extended parser to support expressions in ORDER BY |
| 87 | +- ✅ Updated AST with `OrderByItem` struct |
| 88 | +- ✅ Handles COUNT(*), all aggregates, auto-generates aliases |
| 89 | +- ✅ Works in all execution modes (fixed dependency-aware path) |
| 90 | +- ✅ See `examples/order_by_expressions.sql` with 8 examples |
| 91 | +- ✅ Formal test coverage |
88 | 92 |
|
89 | | -**Estimated Effort:** 1-2 days |
| 93 | +**Completed:** 2025-11-01 |
90 | 94 |
|
91 | 95 | --- |
92 | 96 |
|
@@ -322,27 +326,27 @@ SELECT * FROM sales WHERE region ILIKE '%north%' |
322 | 326 |
|
323 | 327 | ## Implementation Priority Matrix |
324 | 328 |
|
325 | | -| Feature | User Value | Difficulty | SQL Standard | Priority | |
326 | | -|---------|-----------|-----------|--------------|----------| |
327 | | -| DISTINCT in aggregates | High | Medium | Yes | **1** | |
328 | | -| ORDER BY aggregates | High | Easy | Yes | **2** | |
329 | | -| QUALIFY clause | Medium | Easy | No (Snowflake) | **3** | |
330 | | -| PIVOT/UNPIVOT | Medium | Medium | Yes (SQL:2016) | **4** | |
331 | | -| Correlated subqueries | High | Hard | Yes | **5** | |
332 | | -| ARRAY_AGG/STRING_AGG | Medium | Medium | Yes | **6** | |
333 | | -| ILIKE | Low | Trivial | No (Postgres) | **7** | |
334 | | -| SELECT * EXCLUDE | Low | Easy | No (DuckDB) | **8** | |
335 | | -| LATERAL joins | Low | Hard | Yes | **9** | |
336 | | -| Recursive CTEs | Low | Very Hard | Yes | **10** | |
| 329 | +| Feature | User Value | Difficulty | SQL Standard | Priority | Status | |
| 330 | +|---------|-----------|-----------|--------------|----------|--------| |
| 331 | +| ~~ORDER BY aggregates~~ | High | Easy | Yes | ~~**1**~~ | ✅ Done | |
| 332 | +| DISTINCT in aggregates | High | Medium | Yes | **1** | ⏳ Next | |
| 333 | +| QUALIFY clause | Medium | Easy | No (Snowflake) | **2** | 📋 Ready | |
| 334 | +| ILIKE | Low | Trivial | No (Postgres) | **3** | 📋 Ready | |
| 335 | +| PIVOT/UNPIVOT | Medium | Medium | Yes (SQL:2016) | **4** | 📋 Ready | |
| 336 | +| ARRAY_AGG/STRING_AGG | Medium | Medium | Yes | **5** | 📋 Ready | |
| 337 | +| SELECT * EXCLUDE | Low | Easy | No (DuckDB) | **6** | 📋 Ready | |
| 338 | +| Correlated subqueries | High | Hard | Yes | **7** | ⚠️ Complex | |
| 339 | +| LATERAL joins | Low | Hard | Yes | **8** | ⚠️ Complex | |
| 340 | +| Recursive CTEs | Low | Very Hard | Yes | **9** | ⚠️ Complex | |
337 | 341 |
|
338 | 342 | ## Next Steps |
339 | 343 |
|
340 | 344 | ### Immediate (1-2 weeks) |
341 | 345 | 1. ✅ Complete execution mode unification (Phases 0-3) - **DONE!** |
342 | 346 | 2. ✅ Document all transformers - **DONE!** |
343 | 347 | 3. ✅ Create `examples/expander_rewriters.sql` - **DONE!** |
344 | | -4. **NEXT:** Implement DISTINCT in aggregates (`DistinctAggregateLifter`) |
345 | | -5. **NEXT:** Implement ORDER BY aggregate expansion |
| 348 | +4. ✅ Implement ORDER BY aggregate expansion - **DONE!** (2025-11-01) |
| 349 | +5. **NEXT:** Choose from quick wins below |
346 | 350 |
|
347 | 351 | ### Short-term (1-2 months) |
348 | 352 | 1. Add QUALIFY clause support |
|
0 commit comments