vibesql-executor 0.2.0

Query execution engine for vibesql SQL database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
//! Alias and expression resolution for ORDER BY and WHERE clauses.
//!
//! This module handles:
//! - Resolving ORDER BY column positions and aliases
//! - Resolving ORDER BY expressions for aggregate queries
//! - Resolving SELECT aliases in WHERE clauses (SQLite extension)
//! - Extracting aggregate expressions from ORDER BY for pre-computation

use std::borrow::Cow;

use super::position::{
    count_select_columns, extract_column_position, resolve_position_to_column_name,
    resolve_position_with_wildcards, validate_column_position, ColumnPositionResult,
    ResolvedPosition,
};
use crate::{errors::ExecutorError, schema::CombinedSchema};

// =============================================================================
// ORDER BY Alias Resolution for Aggregate Queries
// =============================================================================

/// Resolve ORDER BY expression for aggregate results to result schema column names
///
/// For aggregate queries, the result schema has columns named by their aliases.
/// This function maps ORDER BY expressions to ColumnRef expressions that can be
/// evaluated against the result schema.
///
/// Handles cases:
/// 1. Numeric position (ORDER BY 1) - returns ColumnRef to the alias/column at that position
/// 2. Alias name (ORDER BY alias) - returns ColumnRef to that alias
/// 3. Original column name (ORDER BY col where col is aliased to alias) - returns ColumnRef to
///    alias
/// 4. Complex expressions containing GROUPING() - recursively resolves sub-expressions
/// 5. Otherwise - returns the original expression (for expressions not matching aliases)
///
/// Returns an error if a numeric column position is out of range (0 or > column count)
///
/// # Arguments
/// * `order_expr` - The ORDER BY expression to resolve
/// * `select_list` - The SELECT list items
/// * `term_index` - 0-indexed position of this ORDER BY term (for error messages)
/// * `schema` - Optional schema for proper wildcard expansion when counting columns (#4413)
pub(crate) fn resolve_order_by_for_aggregates(
    order_expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
    term_index: usize, // 0-indexed position of this ORDER BY term
    schema: Option<&CombinedSchema>,
) -> Result<vibesql_ast::Expression, ExecutorError> {
    // Count actual columns after wildcard expansion (#4413)
    // Note: For aggregate queries, wildcards are typically not present, but we
    // handle them for consistency
    let column_count = count_select_columns(select_list, schema);

    // Validate numeric column positions at the TOP LEVEL ONLY
    // (nested integer literals like `WHERE x = 0` are not column positions)

    // Check for numeric column position (ORDER BY N, ORDER BY +N, ORDER BY -N)
    match extract_column_position(order_expr) {
        ColumnPositionResult::Position(pos) => {
            let idx = validate_column_position(pos, column_count, term_index)?;
            if let Some(col_name) = resolve_position_to_column_name(idx, select_list) {
                return Ok(vibesql_ast::Expression::ColumnRef(
                    vibesql_ast::ColumnIdentifier::simple(&col_name, false),
                ));
            }
        }
        ColumnPositionResult::Negative(pos) => {
            // Negative column positions are always invalid
            return Err(ExecutorError::OrderByOutOfRange {
                term_position: term_index + 1,
                column_number: -pos,
                select_list_len: column_count,
            });
        }
        ColumnPositionResult::NotAPosition => {
            // Not a column position, continue to other resolution logic
        }
    }

    // Delegate to helper that doesn't validate column numbers (for recursive calls)
    Ok(resolve_order_by_for_aggregates_inner(order_expr, select_list))
}

/// Internal helper for recursively resolving ORDER BY expressions.
/// Does NOT validate numeric column positions (validation is done at top level only).
fn resolve_order_by_for_aggregates_inner(
    order_expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
) -> vibesql_ast::Expression {
    // Check for numeric column position (ORDER BY 1, 2, 3, etc.)
    // NOTE: At this point we're in a nested expression, so integer literals are NOT column positions
    if let vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(pos)) = order_expr {
        if *pos > 0 && (*pos as usize) <= select_list.len() {
            let idx = (*pos as usize) - 1;
            if let vibesql_ast::SelectItem::Expression { expr, alias, .. } = &select_list[idx] {
                // Return a ColumnRef to the alias name (or derive from expression)
                let col_name = if let Some(alias_name) = alias {
                    alias_name.clone()
                } else if let vibesql_ast::Expression::ColumnRef(col_id) = expr {
                    col_id.column_canonical().to_string()
                } else if let vibesql_ast::Expression::AggregateFunction { name, .. } = expr {
                    // For aggregate functions, use the function name (lowercase)
                    // This matches the schema column name generated in apply_order_by_to_aggregates
                    name.to_lowercase()
                } else {
                    format!("col{}", idx + 1)
                };
                return vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                    &col_name, false,
                ));
            }
        }
        // If not a valid column position, just return the literal as-is
        return order_expr.clone();
    }

    // Check if ORDER BY expression is a simple column reference (no table qualifier)
    if let vibesql_ast::Expression::ColumnRef(col_id) = order_expr {
        if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() {
            let column = col_id.column_canonical();
            // First, check if column matches an alias name - return ColumnRef to that alias
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression { alias: Some(alias_name), .. } = item {
                    if alias_name.eq_ignore_ascii_case(column) {
                        // ORDER BY uses alias name, return ColumnRef to that alias
                        return vibesql_ast::Expression::ColumnRef(
                            vibesql_ast::ColumnIdentifier::simple(alias_name, false),
                        );
                    }
                }
            }

            // Second, check if column matches an original column name that has an alias
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: vibesql_ast::Expression::ColumnRef(select_col_id),
                    alias: Some(alias_name),
                    ..
                } = item
                {
                    if select_col_id.schema_canonical().is_none() {
                        let select_col = select_col_id.column_canonical();
                        if select_col.eq_ignore_ascii_case(column) {
                            // ORDER BY uses original column name, return ColumnRef to the alias
                            return vibesql_ast::Expression::ColumnRef(
                                vibesql_ast::ColumnIdentifier::simple(alias_name, false),
                            );
                        }
                    }
                }
            }

            // Third, check if column matches a SELECT list column without alias
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: vibesql_ast::Expression::ColumnRef(select_col_id),
                    alias: None,
                    ..
                } = item
                {
                    if select_col_id.schema_canonical().is_none() {
                        let select_col = select_col_id.column_canonical();
                        if select_col.eq_ignore_ascii_case(column) {
                            // Found matching non-aliased column, return as-is
                            return order_expr.clone();
                        }
                    }
                }
            }
        }
    }

    // IMPORTANT: Check if the ENTIRE expression matches any SELECT list expression FIRST
    // This handles cases like GROUPING(a) + GROUPING(b) matching an alias like "lochierarchy"
    // before we try to recursively decompose the expression
    if let Some(alias) = find_matching_select_expression(order_expr, select_list) {
        return vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
            &alias, false,
        ));
    }

    // Handle CASE expressions by recursively resolving sub-expressions
    // Use inner helper for recursion (doesn't validate column numbers)
    if let vibesql_ast::Expression::Case { operand, when_clauses, else_result } = order_expr {
        let resolved_operand = operand
            .as_ref()
            .map(|op| Box::new(resolve_order_by_for_aggregates_inner(op, select_list)));

        let mut resolved_when_clauses: Vec<vibesql_ast::CaseWhen> = Vec::new();
        for clause in when_clauses {
            let mut resolved_conditions = Vec::new();
            for cond in &clause.conditions {
                resolved_conditions.push(resolve_order_by_for_aggregates_inner(cond, select_list));
            }
            resolved_when_clauses.push(vibesql_ast::CaseWhen {
                conditions: resolved_conditions,
                result: resolve_order_by_for_aggregates_inner(&clause.result, select_list),
            });
        }

        let resolved_else = else_result
            .as_ref()
            .map(|e| Box::new(resolve_order_by_for_aggregates_inner(e, select_list)));

        return vibesql_ast::Expression::Case {
            operand: resolved_operand,
            when_clauses: resolved_when_clauses,
            else_result: resolved_else,
        };
    }

    // Handle BinaryOp expressions by recursively resolving sub-expressions
    // Try to match the entire binary expression first (already done above with
    // find_matching_select_expression) If no match, try matching each side separately
    if let vibesql_ast::Expression::BinaryOp { left, op, right } = order_expr {
        let resolved_left = resolve_order_by_for_aggregates_inner(left, select_list);
        let resolved_right = resolve_order_by_for_aggregates_inner(right, select_list);

        return vibesql_ast::Expression::BinaryOp {
            left: Box::new(resolved_left),
            op: *op,
            right: Box::new(resolved_right),
        };
    }

    // Handle Function calls (including GROUPING) by checking if they match a SELECT expression
    // This is already handled by find_matching_select_expression above, but keep as fallback
    if let vibesql_ast::Expression::Function { name, .. } = order_expr {
        if name.eq_ignore_ascii_case("GROUPING") || name.eq_ignore_ascii_case("GROUPING_ID") {
            // Try to find a matching GROUPING expression in the SELECT list
            if let Some(alias) = find_matching_select_expression(order_expr, select_list) {
                return vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                    &alias, false,
                ));
            }
        }
    }

    // Not an alias or column position, return the original expression
    order_expr.clone()
}

/// Find a matching expression in the SELECT list and return its alias or generated column name
fn find_matching_select_expression(
    expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
) -> Option<String> {
    for (idx, item) in select_list.iter().enumerate() {
        if let vibesql_ast::SelectItem::Expression { expr: select_expr, alias, .. } = item {
            if expressions_equal(expr, select_expr) {
                // Found matching expression
                // Return alias if present, otherwise derive column name from expression
                return Some(if let Some(alias_name) = alias {
                    alias_name.clone()
                } else if let vibesql_ast::Expression::ColumnRef(col_id) = select_expr {
                    col_id.column_canonical().to_string()
                } else if let vibesql_ast::Expression::AggregateFunction { name, .. } = select_expr
                {
                    // For aggregate functions, use the function name (lowercase)
                    // This matches the schema column name generated in apply_order_by_to_aggregates
                    name.to_lowercase()
                } else {
                    format!("col{}", idx + 1)
                });
            }
        }
    }
    None
}

/// Check if two expressions are structurally equal (for matching ORDER BY expressions to SELECT
/// list)
fn expressions_equal(a: &vibesql_ast::Expression, b: &vibesql_ast::Expression) -> bool {
    match (a, b) {
        (
            vibesql_ast::Expression::ColumnRef(col_id1),
            vibesql_ast::Expression::ColumnRef(col_id2),
        ) => {
            col_id1.schema_canonical().is_none()
                && col_id2.schema_canonical().is_none()
                && col_id1.table_canonical() == col_id2.table_canonical()
                && col_id1.column_canonical() == col_id2.column_canonical()
        }

        (vibesql_ast::Expression::Literal(v1), vibesql_ast::Expression::Literal(v2)) => v1 == v2,

        (
            vibesql_ast::Expression::BinaryOp { left: l1, op: o1, right: r1 },
            vibesql_ast::Expression::BinaryOp { left: l2, op: o2, right: r2 },
        ) => o1 == o2 && expressions_equal(l1, l2) && expressions_equal(r1, r2),

        (
            vibesql_ast::Expression::Function { name: n1, args: a1, .. },
            vibesql_ast::Expression::Function { name: n2, args: a2, .. },
        ) => {
            n1 == n2 // FunctionIdentifier comparison is case-insensitive via canonical
                && a1.len() == a2.len()
                && a1.iter().zip(a2.iter()).all(|(x, y)| expressions_equal(x, y))
        }

        // For other expression types, use debug representation comparison as fallback
        // This is not perfect but handles most common cases
        _ => format!("{:?}", a) == format!("{:?}", b),
    }
}

// =============================================================================
// ORDER BY Alias Resolution for Regular Queries
// =============================================================================

/// Resolve ORDER BY expression that might be a SELECT list alias or column position
///
/// Handles these cases:
/// 1. Numeric literal (e.g., ORDER BY 1, 2, 3) - returns the expression from that position in
///    SELECT list
/// 2. Simple column reference that matches a SELECT list alias - returns the SELECT list expression
/// 3. Simple column reference that matches an aliased column's original name - returns a ColumnRef
///    to the alias
/// 4. Complex expressions containing alias references (e.g., ORDER BY -x, ORDER BY abs(x)) -
///    recursively resolves alias references within the expression (#4436)
/// 5. Otherwise - returns the original ORDER BY expression
///
/// Returns an error if a numeric column position is out of range (0 or > column count)
///
/// # Arguments
/// * `order_expr` - The ORDER BY expression to resolve
/// * `select_list` - The SELECT list items
/// * `term_index` - 0-indexed position of this ORDER BY term (for error messages)
/// * `schema` - Optional schema for proper wildcard expansion when counting columns (#4413)
pub(crate) fn resolve_order_by_alias<'a>(
    order_expr: &'a vibesql_ast::Expression,
    select_list: &'a [vibesql_ast::SelectItem],
    term_index: usize, // 0-indexed position of this ORDER BY term
    schema: Option<&CombinedSchema>,
) -> Result<Cow<'a, vibesql_ast::Expression>, ExecutorError> {
    // Count actual columns after wildcard expansion (#4413)
    let column_count = count_select_columns(select_list, schema);

    // Check for numeric column position (ORDER BY N, ORDER BY +N, ORDER BY -N)
    match extract_column_position(order_expr) {
        ColumnPositionResult::Position(pos) => {
            let idx = validate_column_position(pos, column_count, term_index)?;

            // Resolve the position, handling wildcard expansion
            match resolve_position_with_wildcards(idx, select_list, schema) {
                ResolvedPosition::Expression(expr) => {
                    return Ok(Cow::Borrowed(expr));
                }
                ResolvedPosition::ColumnName { table, column } => {
                    // Build a table-qualified reference when the source table is
                    // known so the ambiguity check is not tripped when multiple
                    // tables expose the same column name (issue #5231).
                    // quoted=true preserves the canonical table/column names
                    // exactly as stored in the schema.
                    let col_id = match table {
                        Some(table_name) => vibesql_ast::ColumnIdentifier::qualified(
                            &table_name,
                            true,
                            &column,
                            true,
                        ),
                        None => vibesql_ast::ColumnIdentifier::simple(&column, false),
                    };
                    return Ok(Cow::Owned(vibesql_ast::Expression::ColumnRef(col_id)));
                }
                ResolvedPosition::NotFound => {
                    // Fallback: shouldn't reach here if validation passed
                }
            }
        }
        ColumnPositionResult::Negative(pos) => {
            // Negative column positions are always invalid
            return Err(ExecutorError::OrderByOutOfRange {
                term_position: term_index + 1,
                column_number: -pos,
                select_list_len: column_count,
            });
        }
        ColumnPositionResult::NotAPosition => {
            // Not a column position, continue to other resolution logic
        }
    }

    // Check if ORDER BY expression is a simple column reference (no table qualifier)
    if let vibesql_ast::Expression::ColumnRef(col_id) = order_expr {
        if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() {
            let column = col_id.column_canonical();
            // First, search for matching alias in SELECT list (ORDER BY using alias name)
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr, alias: Some(alias_name), ..
                } = item
                {
                    if alias_name.eq_ignore_ascii_case(column) {
                        // Found matching alias, use the SELECT list expression
                        return Ok(Cow::Borrowed(expr));
                    }
                }
            }

            // Second, check if column matches a SELECT list expression that has an alias
            // This handles: SELECT col AS alias ... ORDER BY col
            // HOWEVER: If the column exists in the FROM schema, we should NOT transform it
            // to the alias. The ORDER BY should reference the actual column from the schema.
            //
            // Example: SELECT cnt as tests_affected FROM (SELECT COUNT(*) as cnt ...) sub ORDER BY cnt
            // Here, 'cnt' exists in the subquery schema, so ORDER BY cnt should evaluate 'cnt' directly,
            // not be transformed to 'tests_affected' (which doesn't exist in the schema).
            //
            // Only transform to alias when the column does NOT exist in the FROM schema (rare case).
            let column_exists_in_schema = schema.is_some_and(|s| {
                // Check if column exists in any table in the schema
                s.table_schemas.values().any(|(_start_idx, table_schema)| {
                    table_schema.get_column_index(column).is_some()
                })
            });

            if !column_exists_in_schema {
                for item in select_list {
                    // Check if the SELECT expression is a column reference to the same column
                    if let vibesql_ast::SelectItem::Expression {
                        expr: vibesql_ast::Expression::ColumnRef(select_col_id),
                        alias: Some(alias_name),
                        ..
                    } = item
                    {
                        if select_col_id.schema_canonical().is_none() {
                            let select_col = select_col_id.column_canonical();
                            if select_col.eq_ignore_ascii_case(column) {
                                // The ORDER BY column matches the original column, but it's aliased
                                // Return a new ColumnRef using the alias name
                                return Ok(Cow::Owned(vibesql_ast::Expression::ColumnRef(
                                    vibesql_ast::ColumnIdentifier::simple(alias_name, false),
                                )));
                            }
                        }
                    }
                }
            }
        }
    }

    // For complex expressions (UnaryOp, BinaryOp, Function calls, etc.), recursively resolve
    // alias references within the expression (#4436)
    // This handles cases like: ORDER BY -x, ORDER BY abs(x), ORDER BY 10-x
    if let Some(resolved) = resolve_aliases_in_expression(order_expr, select_list) {
        return Ok(Cow::Owned(resolved));
    }

    // Not an alias or column position, use the original expression
    Ok(Cow::Borrowed(order_expr))
}

/// Recursively resolve alias references within an ORDER BY expression.
///
/// This handles complex expressions like:
/// - `ORDER BY -x` where x is an alias
/// - `ORDER BY abs(x)` where x is an alias
/// - `ORDER BY 10-x` where x is an alias
///
/// Returns Some(resolved_expression) if any aliases were resolved, None otherwise.
fn resolve_aliases_in_expression(
    expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
) -> Option<vibesql_ast::Expression> {
    match expr {
        // Handle UnaryOp (e.g., -x, +x, NOT x)
        vibesql_ast::Expression::UnaryOp { op, expr: inner } => {
            // Try to resolve the inner expression
            resolve_alias_or_clone(inner, select_list).map(|resolved_inner| {
                vibesql_ast::Expression::UnaryOp { op: *op, expr: Box::new(resolved_inner) }
            })
        }

        // Handle BinaryOp (e.g., 10-x, x+y)
        vibesql_ast::Expression::BinaryOp { left, op, right } => {
            let resolved_left = resolve_alias_or_clone(left, select_list);
            let resolved_right = resolve_alias_or_clone(right, select_list);

            // Only return Some if at least one side was resolved
            if resolved_left.is_some() || resolved_right.is_some() {
                Some(vibesql_ast::Expression::BinaryOp {
                    left: Box::new(resolved_left.unwrap_or_else(|| left.as_ref().clone())),
                    op: *op,
                    right: Box::new(resolved_right.unwrap_or_else(|| right.as_ref().clone())),
                })
            } else {
                None
            }
        }

        // Handle Function calls (e.g., abs(x), coalesce(x, 0))
        vibesql_ast::Expression::Function { name, args, character_unit } => {
            let mut any_resolved = false;
            let resolved_args: Vec<_> = args
                .iter()
                .map(|arg| {
                    if let Some(resolved) = resolve_alias_or_clone(arg, select_list) {
                        any_resolved = true;
                        resolved
                    } else {
                        arg.clone()
                    }
                })
                .collect();

            if any_resolved {
                Some(vibesql_ast::Expression::Function {
                    name: name.clone(),
                    args: resolved_args,
                    character_unit: character_unit.clone(),
                })
            } else {
                None
            }
        }

        // Handle CASE expressions
        vibesql_ast::Expression::Case { operand, when_clauses, else_result } => {
            let mut any_resolved = false;

            let resolved_operand = operand.as_ref().map(|op| {
                if let Some(resolved) = resolve_alias_or_clone(op, select_list) {
                    any_resolved = true;
                    Box::new(resolved)
                } else {
                    op.clone()
                }
            });

            let resolved_when_clauses: Vec<_> = when_clauses
                .iter()
                .map(|clause| {
                    let resolved_conditions: Vec<_> = clause
                        .conditions
                        .iter()
                        .map(|cond| {
                            if let Some(resolved) = resolve_alias_or_clone(cond, select_list) {
                                any_resolved = true;
                                resolved
                            } else {
                                cond.clone()
                            }
                        })
                        .collect();

                    let resolved_result = if let Some(resolved) =
                        resolve_alias_or_clone(&clause.result, select_list)
                    {
                        any_resolved = true;
                        resolved
                    } else {
                        clause.result.clone()
                    };

                    vibesql_ast::CaseWhen {
                        conditions: resolved_conditions,
                        result: resolved_result,
                    }
                })
                .collect();

            let resolved_else = else_result.as_ref().map(|e| {
                if let Some(resolved) = resolve_alias_or_clone(e, select_list) {
                    any_resolved = true;
                    Box::new(resolved)
                } else {
                    e.clone()
                }
            });

            if any_resolved {
                Some(vibesql_ast::Expression::Case {
                    operand: resolved_operand,
                    when_clauses: resolved_when_clauses,
                    else_result: resolved_else,
                })
            } else {
                None
            }
        }

        // Handle IS / IS NOT (parsed as IsDistinctFrom), e.g. ORDER BY (z IS y)
        // where z is a SELECT alias (window9.test 7.2/7.3)
        vibesql_ast::Expression::IsDistinctFrom { left, right, negated } => {
            let resolved_left = resolve_alias_or_clone(left, select_list);
            let resolved_right = resolve_alias_or_clone(right, select_list);

            if resolved_left.is_some() || resolved_right.is_some() {
                Some(vibesql_ast::Expression::IsDistinctFrom {
                    left: Box::new(resolved_left.unwrap_or_else(|| left.as_ref().clone())),
                    right: Box::new(resolved_right.unwrap_or_else(|| right.as_ref().clone())),
                    negated: *negated,
                })
            } else {
                None
            }
        }

        // Handle IS NULL / IS NOT NULL (e.g. ORDER BY x IS NULL where x is an alias)
        vibesql_ast::Expression::IsNull { expr: inner, negated } => {
            resolve_alias_or_clone(inner, select_list).map(|resolved_inner| {
                vibesql_ast::Expression::IsNull {
                    expr: Box::new(resolved_inner),
                    negated: *negated,
                }
            })
        }

        // Handle IS TRUE / IS FALSE / IS UNKNOWN
        vibesql_ast::Expression::IsTruthValue { expr: inner, truth_value, negated } => {
            resolve_alias_or_clone(inner, select_list).map(|resolved_inner| {
                vibesql_ast::Expression::IsTruthValue {
                    expr: Box::new(resolved_inner),
                    truth_value: *truth_value,
                    negated: *negated,
                }
            })
        }

        // Handle CAST(x AS type)
        vibesql_ast::Expression::Cast { expr: inner, data_type } => {
            resolve_alias_or_clone(inner, select_list).map(|resolved_inner| {
                vibesql_ast::Expression::Cast {
                    expr: Box::new(resolved_inner),
                    data_type: data_type.clone(),
                }
            })
        }

        // Handle x COLLATE name
        vibesql_ast::Expression::Collate { expr: inner, collation } => {
            resolve_alias_or_clone(inner, select_list).map(|resolved_inner| {
                vibesql_ast::Expression::Collate {
                    expr: Box::new(resolved_inner),
                    collation: collation.clone(),
                }
            })
        }

        // Handle x BETWEEN low AND high
        vibesql_ast::Expression::Between { expr: inner, low, high, negated, symmetric } => {
            let resolved_inner = resolve_alias_or_clone(inner, select_list);
            let resolved_low = resolve_alias_or_clone(low, select_list);
            let resolved_high = resolve_alias_or_clone(high, select_list);

            if resolved_inner.is_some() || resolved_low.is_some() || resolved_high.is_some() {
                Some(vibesql_ast::Expression::Between {
                    expr: Box::new(resolved_inner.unwrap_or_else(|| inner.as_ref().clone())),
                    low: Box::new(resolved_low.unwrap_or_else(|| low.as_ref().clone())),
                    high: Box::new(resolved_high.unwrap_or_else(|| high.as_ref().clone())),
                    negated: *negated,
                    symmetric: *symmetric,
                })
            } else {
                None
            }
        }

        // Handle x IN (a, b, c)
        vibesql_ast::Expression::InList { expr: inner, values, negated } => {
            let resolved_inner = resolve_alias_or_clone(inner, select_list);
            let mut any_resolved = resolved_inner.is_some();
            let resolved_values: Vec<_> = values
                .iter()
                .map(|v| {
                    if let Some(resolved) = resolve_alias_or_clone(v, select_list) {
                        any_resolved = true;
                        resolved
                    } else {
                        v.clone()
                    }
                })
                .collect();

            if any_resolved {
                Some(vibesql_ast::Expression::InList {
                    expr: Box::new(resolved_inner.unwrap_or_else(|| inner.as_ref().clone())),
                    values: resolved_values,
                    negated: *negated,
                })
            } else {
                None
            }
        }

        // For other expression types, no resolution needed
        _ => None,
    }
}

/// Helper: resolve an alias in an expression, or return None if no alias was found.
/// Returns Some(resolved_expression) if the expression contains an alias that was resolved.
fn resolve_alias_or_clone(
    expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
) -> Option<vibesql_ast::Expression> {
    // Check if this is a simple column reference that matches an alias
    if let vibesql_ast::Expression::ColumnRef(col_id) = expr {
        if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() {
            let column = col_id.column_canonical();
            // Search for matching alias in SELECT list
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: select_expr,
                    alias: Some(alias_name),
                    ..
                } = item
                {
                    if alias_name.eq_ignore_ascii_case(column) {
                        // Found matching alias, return the SELECT list expression
                        return Some(select_expr.clone());
                    }
                }
            }
        }
    }

    // Try recursive resolution for complex expressions
    resolve_aliases_in_expression(expr, select_list)
}

// =============================================================================
// WHERE Clause Alias Resolution (SQLite Extension)
// =============================================================================

/// Resolve SELECT aliases in WHERE clause expression (SQLite extension).
///
/// SQLite allows referencing SELECT column aliases in WHERE clause, even though
/// standard SQL does not. This function resolves alias references in a WHERE
/// clause expression by replacing them with the actual SELECT expressions.
///
/// Example:
/// ```sql
/// SELECT f1-22 AS x FROM t1 WHERE x > 0
/// -- becomes: SELECT f1-22 AS x FROM t1 WHERE (f1-22) > 0
/// ```
///
/// Returns the resolved expression (cloned with aliases replaced).
pub(crate) fn resolve_where_aliases(
    where_expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
) -> vibesql_ast::Expression {
    // Fast path: if no SELECT items have aliases, nothing to resolve
    if !select_list_has_aliases(select_list) {
        return where_expr.clone();
    }

    // Use empty schema - no table columns to protect from alias resolution
    resolve_where_expression_with_schema(where_expr, select_list, &std::collections::HashSet::new())
}

/// Check if any SELECT item has an alias.
///
/// This is used as a fast path check to skip alias resolution when no aliases exist.
/// Call sites should use this BEFORE building schemas to avoid unnecessary work.
#[inline]
pub(crate) fn select_list_has_aliases(select_list: &[vibesql_ast::SelectItem]) -> bool {
    select_list
        .iter()
        .any(|item| matches!(item, vibesql_ast::SelectItem::Expression { alias: Some(_), .. }))
}

/// Resolve SELECT aliases in WHERE clause with schema column awareness.
///
/// Same as `resolve_where_aliases`, but also takes a schema to extract table column names.
/// Column names that exist in the table schema will NOT be resolved as aliases.
///
/// **IMPORTANT**: Table column names take precedence over SELECT aliases.
/// If a column name exists in the table schema, it will NOT be resolved as an alias,
/// even if a SELECT alias with the same name exists.
///
/// Example:
/// ```sql
/// SELECT COUNT(*) AS col1 FROM tab0 WHERE col1 > 0
/// -- col1 in WHERE refers to the TABLE COLUMN (tab0.col1), not the alias
/// ```
pub(crate) fn resolve_where_aliases_with_schema(
    where_expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
    schema: &CombinedSchema,
) -> vibesql_ast::Expression {
    // Fast path: if no SELECT items have aliases, nothing to resolve
    // This is a common case in TPC-C and other OLTP workloads
    if !select_list_has_aliases(select_list) {
        return where_expr.clone();
    }

    // Use schema's has_column method directly instead of building a HashSet
    // This is a performance optimization for TPC-C and other high-throughput workloads
    resolve_where_expression_with_schema_ref(where_expr, select_list, schema)
}

/// Optimized version that uses CombinedSchema.has_column() directly.
/// Avoids building a HashSet of column names on every query.
fn resolve_where_expression_with_schema_ref(
    expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
    schema: &CombinedSchema,
) -> vibesql_ast::Expression {
    use vibesql_ast::Expression;

    match expr {
        // Column reference: check if it's an alias
        Expression::ColumnRef(col_id)
            if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() =>
        {
            let column = col_id.column_canonical();
            // SQLite behavior: table column names ALWAYS take precedence over aliases
            // Use schema.has_column() directly instead of HashSet lookup
            if schema.has_column(column) {
                return expr.clone();
            }

            // Fallback: check if any SELECT item is a direct column reference with this name
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: Expression::ColumnRef(sel_col_id),
                    ..
                } = item
                {
                    if sel_col_id.schema_canonical().is_none()
                        && sel_col_id.table_canonical().is_none()
                    {
                        let col_name = sel_col_id.column_canonical();
                        if col_name.eq_ignore_ascii_case(column) {
                            return expr.clone();
                        }
                    }
                    if sel_col_id.schema_canonical().is_none()
                        && sel_col_id.table_canonical().is_some()
                    {
                        let col_name = sel_col_id.column_canonical();
                        if col_name.eq_ignore_ascii_case(column) {
                            return expr.clone();
                        }
                    }
                }
            }

            // Now search for matching alias in SELECT list
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: select_expr,
                    alias: Some(alias_name),
                    ..
                } = item
                {
                    if alias_name.eq_ignore_ascii_case(column) {
                        return select_expr.clone();
                    }
                }
            }
            expr.clone()
        }

        // Qualified column references: not aliases
        Expression::ColumnRef(col_id)
            if col_id.schema_canonical().is_none() && col_id.table_canonical().is_some() =>
        {
            expr.clone()
        }
        Expression::ColumnRef(col_id) if col_id.schema_canonical().is_some() => expr.clone(),
        Expression::ColumnRef(_) => expr.clone(),

        // BinaryOp: resolve both sides
        Expression::BinaryOp { left, op, right } => Expression::BinaryOp {
            left: Box::new(resolve_where_expression_with_schema_ref(left, select_list, schema)),
            op: *op,
            right: Box::new(resolve_where_expression_with_schema_ref(right, select_list, schema)),
        },

        // UnaryOp: resolve inner expression
        Expression::UnaryOp { op, expr: inner } => Expression::UnaryOp {
            op: *op,
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
        },

        // Function call: resolve all arguments
        Expression::Function { name, args, character_unit } => Expression::Function {
            name: name.clone(),
            args: args
                .iter()
                .map(|arg| resolve_where_expression_with_schema_ref(arg, select_list, schema))
                .collect(),
            character_unit: character_unit.clone(),
        },

        // CASE expression
        Expression::Case { operand, when_clauses, else_result } => Expression::Case {
            operand: operand.as_ref().map(|op| {
                Box::new(resolve_where_expression_with_schema_ref(op, select_list, schema))
            }),
            when_clauses: when_clauses
                .iter()
                .map(|clause| vibesql_ast::CaseWhen {
                    conditions: clause
                        .conditions
                        .iter()
                        .map(|cond| {
                            resolve_where_expression_with_schema_ref(cond, select_list, schema)
                        })
                        .collect(),
                    result: resolve_where_expression_with_schema_ref(
                        &clause.result,
                        select_list,
                        schema,
                    ),
                })
                .collect(),
            else_result: else_result.as_ref().map(|e| {
                Box::new(resolve_where_expression_with_schema_ref(e, select_list, schema))
            }),
        },

        // IS NULL / IS NOT NULL
        Expression::IsNull { expr: inner, negated } => Expression::IsNull {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            negated: *negated,
        },

        // IS DISTINCT FROM
        Expression::IsDistinctFrom { left, right, negated } => Expression::IsDistinctFrom {
            left: Box::new(resolve_where_expression_with_schema_ref(left, select_list, schema)),
            right: Box::new(resolve_where_expression_with_schema_ref(right, select_list, schema)),
            negated: *negated,
        },

        // IS TRUE / IS FALSE / IS UNKNOWN
        Expression::IsTruthValue { expr: inner, truth_value, negated } => {
            Expression::IsTruthValue {
                expr: Box::new(resolve_where_expression_with_schema_ref(
                    inner,
                    select_list,
                    schema,
                )),
                truth_value: *truth_value,
                negated: *negated,
            }
        }

        // IN list
        Expression::InList { expr: inner, values, negated } => Expression::InList {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            values: values
                .iter()
                .map(|v| resolve_where_expression_with_schema_ref(v, select_list, schema))
                .collect(),
            negated: *negated,
        },

        // IN subquery
        Expression::In { expr: inner, subquery, negated } => Expression::In {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            subquery: subquery.clone(),
            negated: *negated,
        },

        // BETWEEN
        Expression::Between { expr: inner, low, high, negated, symmetric } => Expression::Between {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            low: Box::new(resolve_where_expression_with_schema_ref(low, select_list, schema)),
            high: Box::new(resolve_where_expression_with_schema_ref(high, select_list, schema)),
            negated: *negated,
            symmetric: *symmetric,
        },

        // LIKE
        Expression::Like { expr: inner, pattern, negated, escape } => Expression::Like {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            pattern: Box::new(resolve_where_expression_with_schema_ref(
                pattern,
                select_list,
                schema,
            )),
            negated: *negated,
            escape: escape.as_ref().map(|e| {
                Box::new(resolve_where_expression_with_schema_ref(e, select_list, schema))
            }),
        },

        // GLOB
        Expression::Glob { expr: inner, pattern, negated, escape } => Expression::Glob {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            pattern: Box::new(resolve_where_expression_with_schema_ref(
                pattern,
                select_list,
                schema,
            )),
            negated: *negated,
            escape: escape.as_ref().map(|e| {
                Box::new(resolve_where_expression_with_schema_ref(e, select_list, schema))
            }),
        },

        // CAST
        Expression::Cast { expr: inner, data_type } => Expression::Cast {
            expr: Box::new(resolve_where_expression_with_schema_ref(inner, select_list, schema)),
            data_type: data_type.clone(),
        },

        // Conjunction / Disjunction
        Expression::Conjunction(children) => Expression::Conjunction(
            children
                .iter()
                .map(|c| resolve_where_expression_with_schema_ref(c, select_list, schema))
                .collect(),
        ),
        Expression::Disjunction(children) => Expression::Disjunction(
            children
                .iter()
                .map(|c| resolve_where_expression_with_schema_ref(c, select_list, schema))
                .collect(),
        ),

        // Aggregate functions
        Expression::AggregateFunction { name, args, distinct, order_by, filter } => {
            Expression::AggregateFunction {
                name: name.clone(),
                args: args
                    .iter()
                    .map(|arg| resolve_where_expression_with_schema_ref(arg, select_list, schema))
                    .collect(),
                distinct: *distinct,
                order_by: order_by.as_ref().map(|items| {
                    items
                        .iter()
                        .map(|item| vibesql_ast::OrderByItem {
                            expr: resolve_where_expression_with_schema_ref(
                                &item.expr,
                                select_list,
                                schema,
                            ),
                            direction: item.direction.clone(),
                            nulls_order: item.nulls_order,
                        })
                        .collect()
                }),
                filter: filter.as_ref().map(|f| {
                    Box::new(resolve_where_expression_with_schema_ref(f, select_list, schema))
                }),
            }
        }

        // RowValueConstructor
        Expression::RowValueConstructor(children) => Expression::RowValueConstructor(
            children
                .iter()
                .map(|c| resolve_where_expression_with_schema_ref(c, select_list, schema))
                .collect(),
        ),

        // Collate
        Expression::Collate { expr, collation } => Expression::Collate {
            expr: Box::new(resolve_where_expression_with_schema_ref(expr, select_list, schema)),
            collation: collation.clone(),
        },

        // For all other expressions that don't need alias resolution, just clone
        _ => expr.clone(),
    }
}

/// Recursively resolve alias references in a WHERE clause expression (legacy version).
/// Uses a pre-built HashSet of column names. Kept for backward compatibility with
/// resolve_where_aliases() which doesn't have a schema reference.
fn resolve_where_expression_with_schema(
    expr: &vibesql_ast::Expression,
    select_list: &[vibesql_ast::SelectItem],
    table_columns: &std::collections::HashSet<String>,
) -> vibesql_ast::Expression {
    use vibesql_ast::Expression;

    match expr {
        // Column reference: check if it's an alias
        Expression::ColumnRef(col_id)
            if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() =>
        {
            let column = col_id.column_canonical();
            // SQLite behavior: table column names ALWAYS take precedence over aliases
            // If the column name exists in the table schema, it refers to the table column,
            // not to any SELECT alias with the same name.
            //
            // Example: SELECT COUNT(*) AS col1 FROM tab0 WHERE col1 > 0
            // Here 'col1' in WHERE refers to the TABLE COLUMN (tab0.col1), not the alias
            //
            // Check if this column exists in the table schema
            if table_columns.contains(&column.to_lowercase()) {
                // Column exists in table - don't resolve to alias
                return expr.clone();
            }

            // Fallback: check if any SELECT item is a direct column reference with this name
            // (this is for backward compatibility when no schema is provided)
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: Expression::ColumnRef(sel_col_id),
                    ..
                } = item
                {
                    if sel_col_id.schema_canonical().is_none()
                        && sel_col_id.table_canonical().is_none()
                    {
                        let col_name = sel_col_id.column_canonical();
                        if col_name.eq_ignore_ascii_case(column) {
                            // The name matches a column reference in SELECT - don't resolve to alias
                            return expr.clone();
                        }
                    }
                    // Also check qualified column references
                    if sel_col_id.schema_canonical().is_none()
                        && sel_col_id.table_canonical().is_some()
                    {
                        let col_name = sel_col_id.column_canonical();
                        if col_name.eq_ignore_ascii_case(column) {
                            // The name matches a column reference in SELECT - don't resolve to alias
                            return expr.clone();
                        }
                    }
                }
            }

            // Now search for matching alias in SELECT list (case-insensitive)
            for item in select_list {
                if let vibesql_ast::SelectItem::Expression {
                    expr: select_expr,
                    alias: Some(alias_name),
                    ..
                } = item
                {
                    if alias_name.eq_ignore_ascii_case(column) {
                        // Found matching alias, return the SELECT expression
                        return select_expr.clone();
                    }
                }
            }
            // Not an alias, return as-is
            expr.clone()
        }

        // Qualified column reference: not an alias, return as-is
        Expression::ColumnRef(col_id)
            if col_id.schema_canonical().is_none() && col_id.table_canonical().is_some() =>
        {
            expr.clone()
        }

        // Schema-qualified column reference: not an alias, return as-is
        Expression::ColumnRef(col_id) if col_id.schema_canonical().is_some() => expr.clone(),

        // Any other ColumnRef case (shouldn't happen, but handle for completeness)
        Expression::ColumnRef(_) => expr.clone(),

        // BinaryOp: resolve both sides
        Expression::BinaryOp { left, op, right } => Expression::BinaryOp {
            left: Box::new(resolve_where_expression_with_schema(left, select_list, table_columns)),
            op: *op,
            right: Box::new(resolve_where_expression_with_schema(
                right,
                select_list,
                table_columns,
            )),
        },

        // UnaryOp: resolve inner expression
        Expression::UnaryOp { op, expr: inner } => Expression::UnaryOp {
            op: *op,
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
        },

        // Function call: resolve all arguments
        Expression::Function { name, args, character_unit } => Expression::Function {
            name: name.clone(),
            args: args
                .iter()
                .map(|arg| resolve_where_expression_with_schema(arg, select_list, table_columns))
                .collect(),
            character_unit: character_unit.clone(),
        },

        // CASE expression: resolve all parts
        Expression::Case { operand, when_clauses, else_result } => Expression::Case {
            operand: operand.as_ref().map(|op| {
                Box::new(resolve_where_expression_with_schema(op, select_list, table_columns))
            }),
            when_clauses: when_clauses
                .iter()
                .map(|clause| vibesql_ast::CaseWhen {
                    conditions: clause
                        .conditions
                        .iter()
                        .map(|cond| {
                            resolve_where_expression_with_schema(cond, select_list, table_columns)
                        })
                        .collect(),
                    result: resolve_where_expression_with_schema(
                        &clause.result,
                        select_list,
                        table_columns,
                    ),
                })
                .collect(),
            else_result: else_result.as_ref().map(|e| {
                Box::new(resolve_where_expression_with_schema(e, select_list, table_columns))
            }),
        },

        // IS NULL / IS NOT NULL
        Expression::IsNull { expr: inner, negated } => Expression::IsNull {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            negated: *negated,
        },

        // IS DISTINCT FROM / IS NOT DISTINCT FROM
        Expression::IsDistinctFrom { left, right, negated } => Expression::IsDistinctFrom {
            left: Box::new(resolve_where_expression_with_schema(left, select_list, table_columns)),
            right: Box::new(resolve_where_expression_with_schema(
                right,
                select_list,
                table_columns,
            )),
            negated: *negated,
        },

        // IS TRUE / IS FALSE / IS UNKNOWN
        Expression::IsTruthValue { expr: inner, truth_value, negated } => {
            Expression::IsTruthValue {
                expr: Box::new(resolve_where_expression_with_schema(
                    inner,
                    select_list,
                    table_columns,
                )),
                truth_value: *truth_value,
                negated: *negated,
            }
        }

        // IN list
        Expression::InList { expr: inner, values, negated } => Expression::InList {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            values: values
                .iter()
                .map(|v| resolve_where_expression_with_schema(v, select_list, table_columns))
                .collect(),
            negated: *negated,
        },

        // IN subquery: resolve the left expression, leave subquery unchanged
        Expression::In { expr: inner, subquery, negated } => Expression::In {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            subquery: subquery.clone(),
            negated: *negated,
        },

        // BETWEEN
        Expression::Between { expr: inner, low, high, negated, symmetric } => Expression::Between {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            low: Box::new(resolve_where_expression_with_schema(low, select_list, table_columns)),
            high: Box::new(resolve_where_expression_with_schema(high, select_list, table_columns)),
            negated: *negated,
            symmetric: *symmetric,
        },

        // LIKE
        Expression::Like { expr: inner, pattern, negated, escape } => Expression::Like {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            pattern: Box::new(resolve_where_expression_with_schema(
                pattern,
                select_list,
                table_columns,
            )),
            negated: *negated,
            escape: escape.as_ref().map(|e| {
                Box::new(resolve_where_expression_with_schema(e, select_list, table_columns))
            }),
        },

        // GLOB
        Expression::Glob { expr: inner, pattern, negated, escape } => Expression::Glob {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            pattern: Box::new(resolve_where_expression_with_schema(
                pattern,
                select_list,
                table_columns,
            )),
            negated: *negated,
            escape: escape.as_ref().map(|e| {
                Box::new(resolve_where_expression_with_schema(e, select_list, table_columns))
            }),
        },

        // CAST
        Expression::Cast { expr: inner, data_type } => Expression::Cast {
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
            data_type: data_type.clone(),
        },

        // Conjunction / Disjunction
        Expression::Conjunction(children) => Expression::Conjunction(
            children
                .iter()
                .map(|c| resolve_where_expression_with_schema(c, select_list, table_columns))
                .collect(),
        ),
        Expression::Disjunction(children) => Expression::Disjunction(
            children
                .iter()
                .map(|c| resolve_where_expression_with_schema(c, select_list, table_columns))
                .collect(),
        ),

        // Aggregate functions (resolve arguments)
        Expression::AggregateFunction { name, args, distinct, order_by, filter } => {
            Expression::AggregateFunction {
                name: name.clone(),
                args: args
                    .iter()
                    .map(|arg| {
                        resolve_where_expression_with_schema(arg, select_list, table_columns)
                    })
                    .collect(),
                distinct: *distinct,
                order_by: order_by.as_ref().map(|items| {
                    items
                        .iter()
                        .map(|item| vibesql_ast::OrderByItem {
                            expr: resolve_where_expression_with_schema(
                                &item.expr,
                                select_list,
                                table_columns,
                            ),
                            direction: item.direction.clone(),
                            nulls_order: item.nulls_order,
                        })
                        .collect()
                }),
                filter: filter.as_ref().map(|f| {
                    Box::new(resolve_where_expression_with_schema(f, select_list, table_columns))
                }),
            }
        }

        // RowValueConstructor
        Expression::RowValueConstructor(children) => Expression::RowValueConstructor(
            children
                .iter()
                .map(|c| resolve_where_expression_with_schema(c, select_list, table_columns))
                .collect(),
        ),

        // Collate
        Expression::Collate { expr, collation } => Expression::Collate {
            expr: Box::new(resolve_where_expression_with_schema(expr, select_list, table_columns)),
            collation: collation.clone(),
        },

        // TRIM
        Expression::Trim { position, removal_char, string } => Expression::Trim {
            position: position.clone(),
            removal_char: removal_char.as_ref().map(|c| {
                Box::new(resolve_where_expression_with_schema(c, select_list, table_columns))
            }),
            string: Box::new(resolve_where_expression_with_schema(
                string,
                select_list,
                table_columns,
            )),
        },

        // POSITION
        Expression::Position { substring, string, character_unit } => Expression::Position {
            substring: Box::new(resolve_where_expression_with_schema(
                substring,
                select_list,
                table_columns,
            )),
            string: Box::new(resolve_where_expression_with_schema(
                string,
                select_list,
                table_columns,
            )),
            character_unit: character_unit.clone(),
        },

        // EXTRACT
        Expression::Extract { field, expr: inner } => Expression::Extract {
            field: field.clone(),
            expr: Box::new(resolve_where_expression_with_schema(inner, select_list, table_columns)),
        },

        // INTERVAL
        Expression::Interval { value, unit, leading_precision, fractional_precision } => {
            Expression::Interval {
                value: Box::new(resolve_where_expression_with_schema(
                    value,
                    select_list,
                    table_columns,
                )),
                unit: unit.clone(),
                leading_precision: *leading_precision,
                fractional_precision: *fractional_precision,
            }
        }

        // Quantified comparison
        Expression::QuantifiedComparison { expr: inner, op, quantifier, subquery } => {
            Expression::QuantifiedComparison {
                expr: Box::new(resolve_where_expression_with_schema(
                    inner,
                    select_list,
                    table_columns,
                )),
                op: *op,
                quantifier: quantifier.clone(),
                subquery: subquery.clone(),
            }
        }

        // RAISE: resolve aliases inside the error-message expression.
        Expression::Raise { action, error_message } => Expression::Raise {
            action: *action,
            error_message: error_message.as_ref().map(|msg| {
                Box::new(resolve_where_expression_with_schema(msg, select_list, table_columns))
            }),
        },

        // Expressions that don't need alias resolution (pass through)
        Expression::Literal(_)
        | Expression::Wildcard
        | Expression::CurrentDate
        | Expression::CurrentTime { .. }
        | Expression::CurrentTimestamp { .. }
        | Expression::Default
        | Expression::NextValue { .. }
        | Expression::SessionVariable { .. }
        | Expression::Placeholder(_)
        | Expression::NumberedPlaceholder(_)
        | Expression::NamedPlaceholder(_)
        | Expression::PseudoVariable { .. }
        | Expression::DuplicateKeyValue { .. }
        | Expression::ScalarSubquery(_)
        | Expression::Exists { .. }
        | Expression::WindowFunction { .. }
        | Expression::MatchAgainst { .. } => expr.clone(),
    }
}

// =============================================================================
// ORDER BY Aggregate Extraction
// =============================================================================

/// Extract aggregate expressions from ORDER BY items for pre-computation during GROUP BY.
///
/// When ORDER BY contains aggregate functions (e.g., `ORDER BY max(n)+0`), these aggregates
/// must be computed during the GROUP BY phase, not during ORDER BY evaluation.
/// This function collects all aggregate expressions from ORDER BY so they can be pre-computed.
///
/// Returns a vector of unique aggregate expressions found in ORDER BY.
pub(crate) fn extract_order_by_aggregates(
    order_by: &[vibesql_ast::OrderByItem],
) -> Vec<vibesql_ast::Expression> {
    let mut aggregates = Vec::new();
    for item in order_by {
        collect_aggregates_from_expr(&item.expr, &mut aggregates);
    }
    aggregates
}

/// Extract aggregate expressions from window function PARTITION BY/ORDER BY clauses
/// in the SELECT list. These need to be pre-computed during GROUP BY so the window
/// evaluator (which runs after aggregation) can reference them.
///
/// Example:
/// ```sql
/// SELECT max(b) OVER (ORDER BY max(c)) FROM t GROUP BY b;
/// ```
/// Here `max(c)` inside the OVER clause must be pre-computed per group, then the window
/// evaluator can sort by that pre-computed value.
///
/// Returns a vector of unique aggregate expressions used in any window's PARTITION BY,
/// ORDER BY, or frame offset.
pub(crate) fn extract_window_aggregates(
    select_list: &[vibesql_ast::SelectItem],
) -> Vec<vibesql_ast::Expression> {
    use vibesql_ast::SelectItem;

    let mut aggregates = Vec::new();

    for item in select_list {
        if let SelectItem::Expression { expr, .. } = item {
            // Window functions can be top-level SELECT items or embedded inside
            // larger expressions (e.g. `count(a) OVER (ORDER BY sum(a)) + total(a)
            // OVER()`, #5232) — walk the expression to find all of them.
            collect_window_over_aggregates_from_expr(expr, &mut aggregates);
        }
    }

    aggregates
}

/// Walk an expression tree (without descending into subqueries) and, for every
/// window function found, collect the aggregate expressions appearing in its
/// OVER clause (PARTITION BY, ORDER BY, frame offsets).
fn collect_window_over_aggregates_from_expr(
    expr: &vibesql_ast::Expression,
    aggregates: &mut Vec<vibesql_ast::Expression>,
) {
    use vibesql_ast::Expression;

    match expr {
        Expression::WindowFunction { function, over } => {
            // Window function arguments (e.g., `lead(sum(c)) OVER (...)`):
            // aggregates appearing as args need hidden per-group columns so the
            // post-aggregation window pass can resolve them (#5267).
            let args = match function {
                vibesql_ast::WindowFunctionSpec::Aggregate { args, .. }
                | vibesql_ast::WindowFunctionSpec::Ranking { args, .. }
                | vibesql_ast::WindowFunctionSpec::Value { args, .. } => args,
            };
            for arg in args {
                collect_aggregates_from_expr(arg, aggregates);
            }
            // PARTITION BY expressions
            if let Some(partition_by) = &over.partition_by {
                for p_expr in partition_by {
                    collect_aggregates_from_expr(p_expr, aggregates);
                }
            }
            // ORDER BY expressions
            if let Some(order_by) = &over.order_by {
                for ob_item in order_by {
                    collect_aggregates_from_expr(&ob_item.expr, aggregates);
                }
            }
            // Frame offset expressions (e.g., `ROWS BETWEEN sum(x) PRECEDING ...`)
            if let Some(frame) = &over.frame {
                if let vibesql_ast::FrameBound::Preceding(e)
                | vibesql_ast::FrameBound::Following(e) = &frame.start
                {
                    collect_aggregates_from_expr(e, aggregates);
                }
                if let Some(end) = &frame.end {
                    if let vibesql_ast::FrameBound::Preceding(e)
                    | vibesql_ast::FrameBound::Following(e) = end
                    {
                        collect_aggregates_from_expr(e, aggregates);
                    }
                }
            }
        }
        // Recurse into compound expressions to find embedded window functions.
        Expression::BinaryOp { left, right, .. } => {
            collect_window_over_aggregates_from_expr(left, aggregates);
            collect_window_over_aggregates_from_expr(right, aggregates);
        }
        Expression::UnaryOp { expr: inner, .. }
        | Expression::Cast { expr: inner, .. }
        | Expression::IsNull { expr: inner, .. }
        | Expression::IsTruthValue { expr: inner, .. }
        | Expression::Collate { expr: inner, .. } => {
            collect_window_over_aggregates_from_expr(inner, aggregates);
        }
        Expression::IsDistinctFrom { left, right, .. } => {
            collect_window_over_aggregates_from_expr(left, aggregates);
            collect_window_over_aggregates_from_expr(right, aggregates);
        }
        Expression::Function { args, .. } => {
            for arg in args {
                collect_window_over_aggregates_from_expr(arg, aggregates);
            }
        }
        Expression::Case { operand, when_clauses, else_result } => {
            if let Some(op) = operand {
                collect_window_over_aggregates_from_expr(op, aggregates);
            }
            for when_clause in when_clauses {
                for cond in &when_clause.conditions {
                    collect_window_over_aggregates_from_expr(cond, aggregates);
                }
                collect_window_over_aggregates_from_expr(&when_clause.result, aggregates);
            }
            if let Some(else_expr) = else_result {
                collect_window_over_aggregates_from_expr(else_expr, aggregates);
            }
        }
        Expression::Between { expr, low, high, .. } => {
            collect_window_over_aggregates_from_expr(expr, aggregates);
            collect_window_over_aggregates_from_expr(low, aggregates);
            collect_window_over_aggregates_from_expr(high, aggregates);
        }
        Expression::InList { expr, values, .. } => {
            collect_window_over_aggregates_from_expr(expr, aggregates);
            for item in values {
                collect_window_over_aggregates_from_expr(item, aggregates);
            }
        }
        Expression::Like { expr, pattern, .. } | Expression::Glob { expr, pattern, .. } => {
            collect_window_over_aggregates_from_expr(expr, aggregates);
            collect_window_over_aggregates_from_expr(pattern, aggregates);
        }
        Expression::Conjunction(exprs)
        | Expression::Disjunction(exprs)
        | Expression::RowValueConstructor(exprs) => {
            for e in exprs {
                collect_window_over_aggregates_from_expr(e, aggregates);
            }
        }
        // IN / quantified comparison: only the LHS shares this scope.
        Expression::In { expr, .. } | Expression::QuantifiedComparison { expr, .. } => {
            collect_window_over_aggregates_from_expr(expr, aggregates);
        }
        // Subqueries have their own scope; other leaves can't contain windows.
        _ => {}
    }
}

/// Recursively collect aggregate function expressions from an expression tree.
fn collect_aggregates_from_expr(
    expr: &vibesql_ast::Expression,
    aggregates: &mut Vec<vibesql_ast::Expression>,
) {
    use vibesql_ast::Expression;

    match expr {
        Expression::AggregateFunction { .. } => {
            // Check if this aggregate is already collected (avoid duplicates)
            let already_exists = aggregates
                .iter()
                .any(|existing| format!("{:?}", existing) == format!("{:?}", expr));
            if !already_exists {
                aggregates.push(expr.clone());
            }
        }

        // Recursively search in compound expressions
        Expression::BinaryOp { left, right, .. } => {
            collect_aggregates_from_expr(left, aggregates);
            collect_aggregates_from_expr(right, aggregates);
        }
        Expression::UnaryOp { expr: inner, .. } => {
            collect_aggregates_from_expr(inner, aggregates);
        }
        Expression::Function { args, .. } => {
            for arg in args {
                collect_aggregates_from_expr(arg, aggregates);
            }
        }
        Expression::Case { operand, when_clauses, else_result } => {
            if let Some(op) = operand {
                collect_aggregates_from_expr(op, aggregates);
            }
            for when_clause in when_clauses {
                for cond in &when_clause.conditions {
                    collect_aggregates_from_expr(cond, aggregates);
                }
                collect_aggregates_from_expr(&when_clause.result, aggregates);
            }
            if let Some(else_expr) = else_result {
                collect_aggregates_from_expr(else_expr, aggregates);
            }
        }
        Expression::Cast { expr: inner, .. } => {
            collect_aggregates_from_expr(inner, aggregates);
        }
        Expression::Between { expr, low, high, .. } => {
            collect_aggregates_from_expr(expr, aggregates);
            collect_aggregates_from_expr(low, aggregates);
            collect_aggregates_from_expr(high, aggregates);
        }
        Expression::InList { expr, values, .. } => {
            collect_aggregates_from_expr(expr, aggregates);
            for item in values {
                collect_aggregates_from_expr(item, aggregates);
            }
        }

        // Leaf expressions - nothing to extract
        Expression::Literal(_)
        | Expression::ColumnRef(_)
        | Expression::Wildcard
        | Expression::CurrentDate
        | Expression::CurrentTime { .. }
        | Expression::CurrentTimestamp { .. }
        | Expression::Default
        | Expression::NextValue { .. }
        | Expression::Placeholder(_)
        | Expression::NumberedPlaceholder(_)
        | Expression::NamedPlaceholder(_)
        | Expression::PseudoVariable { .. }
        | Expression::DuplicateKeyValue { .. }
        | Expression::SessionVariable { .. } => {}

        // Skip subqueries and window functions
        Expression::ScalarSubquery(_)
        | Expression::Exists { .. }
        | Expression::In { .. }
        | Expression::QuantifiedComparison { .. }
        | Expression::WindowFunction { .. } => {}

        // Other expressions we might need to handle
        Expression::Like { expr, pattern, .. } | Expression::Glob { expr, pattern, .. } => {
            collect_aggregates_from_expr(expr, aggregates);
            collect_aggregates_from_expr(pattern, aggregates);
        }
        Expression::IsNull { expr, .. } => {
            collect_aggregates_from_expr(expr, aggregates);
        }
        Expression::IsDistinctFrom { left, right, .. } => {
            collect_aggregates_from_expr(left, aggregates);
            collect_aggregates_from_expr(right, aggregates);
        }
        Expression::IsTruthValue { expr, .. } => {
            collect_aggregates_from_expr(expr, aggregates);
        }
        Expression::Conjunction(terms) | Expression::Disjunction(terms) => {
            for term in terms {
                collect_aggregates_from_expr(term, aggregates);
            }
        }
        Expression::RowValueConstructor(items) => {
            for item in items {
                collect_aggregates_from_expr(item, aggregates);
            }
        }
        Expression::Position { substring, string, .. } => {
            collect_aggregates_from_expr(substring, aggregates);
            collect_aggregates_from_expr(string, aggregates);
        }
        Expression::Trim { string, removal_char, .. } => {
            collect_aggregates_from_expr(string, aggregates);
            if let Some(c) = removal_char {
                collect_aggregates_from_expr(c, aggregates);
            }
        }
        Expression::Extract { expr, .. } => {
            collect_aggregates_from_expr(expr, aggregates);
        }
        Expression::Interval { value, .. } => {
            collect_aggregates_from_expr(value, aggregates);
        }
        Expression::Collate { expr: inner, .. } => {
            collect_aggregates_from_expr(inner, aggregates);
        }
        Expression::Raise { error_message, .. } => {
            if let Some(msg) = error_message {
                collect_aggregates_from_expr(msg, aggregates);
            }
        }
        Expression::MatchAgainst { .. } => {}
    }
}