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
//! Main execution methods for SelectExecutor
//!
//! This module implements the unified execution dispatcher that routes queries
//! to the appropriate execution pipeline based on the selected strategy.
//!
//! ## Execution Pipeline Architecture
//!
//! The dispatcher uses the `ExecutionPipeline` trait to provide a unified interface
//! for query execution across different strategies:
//!
//! - **NativeColumnar**: Zero-copy SIMD execution from columnar storage
//! - **StandardColumnar**: SIMD execution with row-to-batch conversion
//! - **RowOriented**: Traditional row-by-row execution
//! - **ExpressionOnly**: SELECT without FROM clause (special case)
//!
//! ```text
//! Strategy Selection → Create Pipeline → Execute Pipeline Stages → Results
//! ↓
//! apply_filter → apply_projection → apply_aggregation → apply_limit_offset
//! ```
use std::collections::HashMap;
use super::builder::SelectExecutor;
use crate::{
errors::ExecutorError,
optimizer::adaptive::{choose_execution_strategy, ExecutionStrategy, StrategyContext},
pipeline::{
ColumnarPipeline, ExecutionContext, ExecutionPipeline, NativeColumnarPipeline,
PipelineInput,
},
select::{
cte::{execute_ctes, execute_ctes_with_memory_check, CteResult},
helpers::apply_limit_offset,
join::FromResult,
SelectResult,
},
};
impl SelectExecutor<'_> {
/// Execute a SELECT statement
pub fn execute(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<Vec<vibesql_storage::Row>, ExecutorError> {
// Validate aggregate function argument counts FIRST (issue #4367)
// This catches errors like max(), min(*), sum(*) before any execution
// Must happen before any fast paths or strategy selection
super::validation::validate_aggregate_arguments(&stmt.select_list)?;
// Validate no nested aggregates (issue #4439)
// This catches errors like SUM(MIN(x)) before any execution
// Uses "misuse of aggregate function X()" format to match SQLite's resolve.c
super::validation::validate_no_nested_aggregates(&stmt.select_list)?;
// Validate join table limit (issue #4711)
// SQLite enforces a limit of 64 tables in a single join
// This catches queries like SELECT * FROM t, t, t, ... (65+ times)
super::validation::validate_join_table_limit(stmt)?;
// Validate IN (SELECT ...) subquery column counts (issue #5191)
// SQLite raises "sub-select returns N columns - expected 1" at
// prepare time; the runtime check in the expression evaluator only
// fires when a row is actually evaluated, so it misses the
// empty-table case (window9.test 3.4).
super::validation::validate_in_subquery_column_counts(stmt, self.database)?;
// Validate SQLite index hints (issue #5235)
// INDEXED BY must name an index that exists on that table; SQLite
// reports "no such index: X" at prepare time. The hint is otherwise
// ignored by the planner (PR #5234).
super::validation::validate_index_hints(stmt, self.database)?;
// Validate aggregates with outer column references in subqueries (issue #4853)
// This catches the specific case where a scalar subquery appears as an argument
// to an outer aggregate function, and the subquery's aggregate references an
// outer column. Example: SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
// Note: Standalone correlated subqueries are valid and allowed, e.g.,
// SELECT (SELECT count(x) FROM t35b) FROM t35a; is valid in SQLite.
super::validation::validate_aggregate_subquery_outer_refs(stmt, self.database)?;
// Validate bare scalar subqueries in the SELECT list / ORDER BY for
// misuse of aggregate / window / row-value (#5069, refined #5104).
//
// A "bare" scalar subquery (no FROM) re-borrows the outer aggregation
// context. SQLite has two distinct behaviors depending on where the
// subquery appears:
//
// * SELECT-list (#5104): `SELECT (SELECT avg(a)) FROM t2` is allowed —
// the outer query implicitly collapses to a single-row aggregate. We
// pass `SubqueryContext::SelectList` so the validator skips the
// "misuse of aggregate" rejection in this position.
// * WHERE / HAVING / ORDER BY (and arguments to outer functions): an
// outer-correlated aggregate inside a bare scalar subquery is still
// a misuse and must be rejected. We pass `WhereOrEqual` for ORDER BY
// to preserve SQLite's rejection there.
//
// Row-value misuse (`(SELECT (a, b))`) is detected in both contexts.
for item in &stmt.select_list {
if let vibesql_ast::SelectItem::Expression { expr, .. } = item {
super::validation::validate_subquery_context_misuse(
expr,
super::validation::SubqueryContext::SelectList,
)?;
}
}
if let Some(order_by) = stmt.order_by.as_deref() {
for item in order_by {
super::validation::validate_subquery_context_misuse(
&item.expr,
super::validation::SubqueryContext::WhereOrEqual,
)?;
}
}
// Validate GROUP BY clauses across this statement and all nested
// subqueries for window-function misuse, including positional
// (`GROUP BY 1`) and alias references that resolve to a window
// function in the subquery's SELECT list (#5093, window1.test 47.2).
// SQLite raises this at prepare time, so we must walk subqueries
// here rather than relying on per-SELECT execution paths (the
// outer WHERE may short-circuit before the inner SELECT executes).
super::validation::validate_group_by_window_misuse(stmt)?;
#[cfg(feature = "profile-q6")]
let execute_start = std::time::Instant::now();
// Reset arena and clear pointer-based caches for fresh query execution (only at top level)
// The subquery hash cache uses pointer-based keys which become invalid when ASTs are
// dropped and memory is reused, so we must clear it between queries.
// Note: The IN subquery result cache and correlation cache use content hashes as keys,
// not pointers, so they can safely persist across queries.
if self.subquery_depth == 0 {
self.reset_arena();
crate::evaluator::caching::clear_subquery_hash_cache();
}
// Check timeout before starting execution
self.check_timeout()?;
// Check subquery depth limit to prevent stack overflow
if self.subquery_depth >= crate::limits::MAX_EXPRESSION_DEPTH {
return Err(ExecutorError::ExpressionDepthExceeded {
depth: self.subquery_depth,
max_depth: crate::limits::MAX_EXPRESSION_DEPTH,
});
}
// Guard against exponentially self-referential view nests (SQLite ticket
// [d58ccbb3f1b], exercised by view3.test / #5394). VibeSQL materializes
// views lazily by executing their queries, so a doubling view nest
// (v2=v1∪v1, v4=v2∪v2, …) expands exponentially and would hang. Match
// SQLite by statically detecting >65535 references to any view before
// execution. Only run once per top-level query.
if self.subquery_depth == 0 {
crate::select::view_reference_guard::check_view_reference_limit(stmt, self.database)?;
}
// Fast path for simple point-lookup queries (TPC-C optimization)
// This bypasses expensive optimizer passes for queries like:
// SELECT col FROM table WHERE pk = value
// Skip fast path if reverse_unordered_selects is ON and no ORDER BY (needs reversal)
let skip_fast_path_for_reversal =
self.database.reverse_unordered_selects() && stmt.order_by.is_none();
if self.subquery_depth == 0
&& self.outer_row.is_none()
&& self.cte_context.is_none()
&& !skip_fast_path_for_reversal
&& super::fast_path::is_simple_point_query(stmt)
{
return self.execute_fast_path(stmt);
}
// Streaming aggregate fast path (#3815)
// For queries like: SELECT SUM(k) FROM sbtest1 WHERE id BETWEEN ? AND ?
// Accumulates aggregates inline during PK range scan without materializing rows
if self.subquery_depth == 0
&& self.outer_row.is_none()
&& self.cte_context.is_none()
&& !skip_fast_path_for_reversal
&& super::fast_path::is_streaming_aggregate_query(stmt)
{
if let Ok(result) = self.execute_streaming_aggregate(stmt) {
return Ok(result);
}
// Fall through to standard path if streaming aggregate fails
}
#[cfg(feature = "profile-q6")]
let _setup_time = execute_start.elapsed();
// Apply subquery rewriting optimizations (Phase 2 of IN subquery optimization)
// - Rewrites correlated IN → EXISTS with LIMIT 1 for early termination
// - Adds DISTINCT to uncorrelated IN subqueries to reduce duplicate processing
// This works in conjunction with Phase 1 (HashSet optimization, #2136)
#[cfg(feature = "profile-q6")]
let optimizer_start = std::time::Instant::now();
let optimized_stmt = crate::optimizer::rewrite_subquery_optimizations(stmt);
// Push WHERE conjuncts into window-function subqueries/views (#5292)
// Predicates over a PARTITION BY prefix of every window in a derived
// table or view are copied into the inner WHERE clause, filtering
// whole partitions before the window functions run (and enabling
// index scans inside the subquery). Mirrors SQLite's
// pushDownWhereTerms() gate for window queries.
//
// CTE names already in scope (from enclosing queries) are threaded
// in so the pass can decline view expansion for shadowed names: at
// execution time CTEs take precedence over catalog views, and this
// pass runs before CTE resolution. Empty set when there is no outer
// CTE context (the common case; HashSet::new() does not allocate).
let outer_cte_names: std::collections::HashSet<String> = self
.cte_context
.map(|ctx| ctx.keys().map(|name| name.to_ascii_lowercase()).collect())
.unwrap_or_default();
let optimized_stmt = crate::optimizer::push_where_into_window_subqueries(
optimized_stmt,
self.database,
&outer_cte_names,
);
// Apply scalar subquery decorrelation (#4760)
// Transforms correlated scalar subqueries with aggregates (e.g., AVG, SUM, MIN)
// into CTE + JOIN patterns for O(n) instead of O(n²) execution.
// Example: WHERE x > 1.2 * (SELECT AVG(y) FROM t WHERE t.c = outer.c)
// Becomes: WITH _cte AS (SELECT c, AVG(y) FROM t GROUP BY c)
// ... JOIN _cte ON outer.c = _cte.c WHERE x > 1.2 * _cte._avg
let optimized_stmt = crate::optimizer::apply_scalar_decorrelation(&optimized_stmt);
#[cfg(feature = "profile-q6")]
let _optimizer_time = optimizer_start.elapsed();
// Eliminate unused tables that create unnecessary cross joins (#3556)
// Must run BEFORE semi-join transformation to avoid complex interactions
// with derived tables from EXISTS/IN transformations
let optimized_stmt = crate::optimizer::eliminate_unused_tables(&optimized_stmt);
// Transform decorrelated IN/EXISTS subqueries to semi/anti-joins (#2424)
// This enables hash-based join execution instead of row-by-row subquery evaluation
// Converts WHERE clauses like "WHERE x IN (SELECT y FROM t)" to "SEMI JOIN t ON x = y"
let optimized_stmt = crate::optimizer::transform_subqueries_to_joins(&optimized_stmt);
// Execute CTEs if present and merge with outer query's CTE context
let mut cte_results = if let Some(with_clause) = &optimized_stmt.with_clause {
// This query has its own CTEs - execute them with memory tracking
execute_ctes_with_memory_check(
with_clause,
self.database,
|query, cte_ctx| self.execute_with_ctes(query, cte_ctx),
|size| self.track_memory_allocation(size),
)?
} else {
HashMap::new()
};
// If we have access to outer query's CTEs (for subqueries), merge them in
// Local CTEs take precedence over outer CTEs if there are name conflicts
if let Some(outer_cte_ctx) = self.cte_context {
for (name, result) in outer_cte_ctx {
cte_results.entry(name.clone()).or_insert_with(|| result.clone());
}
}
#[cfg(feature = "profile-q6")]
let _pre_execute_time = execute_start.elapsed();
// Execute the main query with CTE context
let mut result = self.execute_with_ctes(&optimized_stmt, &cte_results)?;
#[cfg(feature = "profile-q6")]
{
let _total_execute = execute_start.elapsed();
}
// Apply PRAGMA reverse_unordered_selects if enabled
// Only reverse if there's no ORDER BY clause in the original statement
if stmt.order_by.is_none() && self.database.reverse_unordered_selects() {
result.reverse();
}
Ok(result)
}
/// Execute a SELECT statement and return an iterator over results
///
/// This enables early termination when the full result set is not needed,
/// such as for IN subqueries where we stop after finding the first match.
///
/// # Phase 1 Implementation (Early Termination for IN subqueries)
///
/// Current implementation materializes results then returns an iterator.
/// This still enables early termination in the consumer (e.g., eval_in_subquery)
/// by stopping iteration when a match is found.
///
/// Future optimization: Leverage the existing RowIterator infrastructure
/// (crate::select::iterator) for truly lazy evaluation that stops execution
/// early, not just iteration.
pub fn execute_iter(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<impl Iterator<Item = vibesql_storage::Row>, ExecutorError> {
// For Phase 1, materialize then return iterator
// This still enables early termination in the consumer
let rows = self.execute(stmt)?;
Ok(rows.into_iter())
}
/// Execute a SELECT statement using the fast path directly
///
/// This method is used by prepared statements with cached SimpleFastPath plans.
/// It bypasses the `is_simple_point_query()` check because the eligibility was
/// already determined at prepare time.
///
/// # Performance
///
/// For repeated execution of prepared statements, this saves the cost of
/// re-checking fast path eligibility on every execution (~5-10µs per query).
pub fn execute_fast_path_with_columns(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<SelectResult, ExecutorError> {
// Reset arena for fresh query execution
if self.subquery_depth == 0 {
self.reset_arena();
}
// Check timeout before starting execution
self.check_timeout()?;
// Execute via fast path directly (skip is_simple_point_query check)
let rows = self.execute_fast_path(stmt)?;
// Derive column names from the SELECT list
// For fast path queries, we don't have a FromResult, so pass None
// The column derivation will use the SELECT list expressions directly
let columns = self.derive_fast_path_column_names(stmt)?;
Ok(SelectResult { columns, rows })
}
/// Derive column names for fast path execution
///
/// For fast path queries, we derive column names directly from the SELECT list
/// and table schema without going through the full FROM clause execution.
///
/// # Performance Note (#3780)
///
/// This method is called by `Session::execute_prepared()` to cache column names
/// in `SimpleFastPathPlan`. After the first execution, cached column names are
/// reused to avoid repeated table lookups and column name derivation.
pub fn derive_fast_path_column_names(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<Vec<String>, ExecutorError> {
use vibesql_ast::{FromClause, SelectItem};
// Get table name and schema for column resolution
let (table_name, table_alias) = match &stmt.from {
Some(FromClause::Table { name, alias, .. }) => (name.as_str(), alias.as_deref()),
_ => {
return Err(ExecutorError::Other(
"Fast path requires simple table FROM clause".to_string(),
))
}
};
let table = self
.database
.get_table(table_name)
.ok_or_else(|| ExecutorError::TableNotFound(table_name.to_string()))?;
let mut columns = Vec::with_capacity(stmt.select_list.len());
for item in &stmt.select_list {
match item {
SelectItem::Wildcard { .. } => {
// Add all columns from the table
for col in &table.schema.columns {
columns.push(col.name.clone());
}
}
SelectItem::QualifiedWildcard { qualifier, .. } => {
// Check if qualifier matches table name or alias
let effective_name = table_alias.unwrap_or(table_name);
if qualifier.eq_ignore_ascii_case(effective_name)
|| qualifier.eq_ignore_ascii_case(table_name)
{
for col in &table.schema.columns {
columns.push(col.name.clone());
}
}
}
SelectItem::Expression { expr, alias: col_alias, .. } => {
// Use alias if provided, otherwise derive from expression
let col_name = if let Some(a) = col_alias {
a.clone()
} else {
self.derive_column_name_from_expr(expr)
};
columns.push(col_name);
}
}
}
Ok(columns)
}
/// Derive a column name from an expression
fn derive_column_name_from_expr(&self, expr: &vibesql_ast::Expression) -> String {
match expr {
vibesql_ast::Expression::ColumnRef(col_id) => col_id.column_canonical().to_string(),
vibesql_ast::Expression::Literal(val) => format!("{}", val),
vibesql_ast::Expression::Wildcard => "*".to_string(),
_ => "?column?".to_string(),
}
}
/// Resolve the output column names of a SELECT statement WITHOUT returning rows.
///
/// This is the read-only column-name resolver used by the PostgreSQL extended
/// query protocol's `Describe` message (#5429), which must report the
/// `RowDescription` before any `Execute`. It reuses the exact wildcard
/// expansion and expression-naming logic as [`execute_with_columns`]
/// (`derive_column_names`), so the names match the simple-query path
/// label-for-label, including:
/// - explicit columns and aliases (`SELECT a, b AS x` -> `[a, x]`)
/// - `SELECT *` / `t.*` expanded against the catalog schema
/// - join wildcards (`SELECT t1.*, t2.c` -> t1's columns then `c`)
/// - derived names for expression columns
///
/// Unlike `execute_with_columns`, this does NOT materialize result rows: it
/// builds the FROM-clause schema (the same `FromResult` the executor uses)
/// purely to expand wildcards, then derives the names.
pub fn resolve_column_names(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<Vec<String>, ExecutorError> {
// Guard exponentially self-referential view nests before touching the
// FROM clause (which materializes views), mirroring execute_with_columns
// (#5394, view3.test).
crate::select::view_reference_guard::check_view_reference_limit(stmt, self.database)?;
// Resolve SELECT aliases in WHERE clause for schema construction, matching
// execute_with_columns so the FROM schema is built identically.
let resolved_where = if stmt.where_clause.is_some()
&& crate::select::order::select_list_has_aliases(&stmt.select_list)
{
stmt.where_clause.as_ref().map(|where_expr| {
if let Some(from_clause) = &stmt.from {
if let Some(early_schema) =
super::aggregation::build_early_schema(from_clause, self.database)
{
return crate::select::order::resolve_where_aliases_with_schema(
where_expr,
&stmt.select_list,
&early_schema,
);
}
}
crate::select::order::resolve_where_aliases(where_expr, &stmt.select_list)
})
} else {
stmt.where_clause.clone()
};
// Build the FROM result to access the combined schema. This is required
// to expand `*` / `table.*` against the real column layout (including
// joins and NATURAL/USING deduplication). We do not return the rows.
let from_result = if let Some(from_clause) = &stmt.from {
let mut cte_results = if let Some(with_clause) = &stmt.with_clause {
execute_ctes(with_clause, self.database, |query, cte_ctx| {
self.execute_with_ctes(query, cte_ctx)
})?
} else {
HashMap::new()
};
if let Some(outer_cte_ctx) = self.cte_context {
for (name, result) in outer_cte_ctx {
cte_results.entry(name.clone()).or_insert_with(|| result.clone());
}
}
let limit_val = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
Some(self.execute_from_with_where(
from_clause,
&cte_results,
resolved_where.as_ref(),
stmt.order_by.as_deref(),
limit_val,
Some(&stmt.select_list),
)?)
} else {
None
};
// Derive column names exactly as execute_with_columns does.
let columns = if stmt.select_list.is_empty() {
if let Some(values_rows) = &stmt.values {
let num_cols = values_rows.first().map(|r| r.len()).unwrap_or(0);
(1..=num_cols).map(|i| format!("column{}", i)).collect()
} else {
Vec::new()
}
} else {
self.derive_column_names(&stmt.select_list, from_result.as_ref())?
};
Ok(columns)
}
/// Execute a SELECT statement and return both columns and rows
pub fn execute_with_columns(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<SelectResult, ExecutorError> {
// Guard exponentially self-referential view nests before executing the
// FROM clause (which materializes views). This entry point runs the FROM
// *before* execute(), so the guard inside execute() would be too late
// (#5394, view3.test).
crate::select::view_reference_guard::check_view_reference_limit(stmt, self.database)?;
// Resolve SELECT aliases in WHERE clause BEFORE predicate pushdown (SQLite extension)
// This allows queries like: SELECT f1-22 AS x FROM t1 WHERE x > 0
// IMPORTANT: Use schema-aware resolution to avoid incorrectly substituting
// table column names with aggregate aliases (SQLite behavior)
// Example: SELECT COUNT(*) AS col1 FROM tab0 WHERE col1 > 0
// Here 'col1' in WHERE refers to the TABLE COLUMN, not the COUNT(*) alias
// PERFORMANCE: Skip alias resolution if no aliases exist (common case in OLTP)
let resolved_where = if stmt.where_clause.is_some()
&& crate::select::order::select_list_has_aliases(&stmt.select_list)
{
stmt.where_clause.as_ref().map(|where_expr| {
// Try to build early schema from FROM clause
if let Some(from_clause) = &stmt.from {
if let Some(early_schema) =
super::aggregation::build_early_schema(from_clause, self.database)
{
// Use schema-aware resolution
return crate::select::order::resolve_where_aliases_with_schema(
where_expr,
&stmt.select_list,
&early_schema,
);
}
}
// Fall back to non-schema-aware resolution for complex FROM clauses
crate::select::order::resolve_where_aliases(where_expr, &stmt.select_list)
})
} else {
stmt.where_clause.clone()
};
// First, get the FROM result to access the schema
let from_result = if let Some(from_clause) = &stmt.from {
let mut cte_results = if let Some(with_clause) = &stmt.with_clause {
execute_ctes(with_clause, self.database, |query, cte_ctx| {
self.execute_with_ctes(query, cte_ctx)
})?
} else {
HashMap::new()
};
// If we have access to outer query's CTEs (for subqueries/derived tables), merge them
// in Local CTEs take precedence over outer CTEs if there are name conflicts
// This is critical for queries like TPC-DS Q2 where CTEs are referenced from derived
// tables
if let Some(outer_cte_ctx) = self.cte_context {
for (name, result) in outer_cte_ctx {
cte_results.entry(name.clone()).or_insert_with(|| result.clone());
}
}
// Pass WHERE, ORDER BY, and LIMIT for optimizations
// This is critical for GROUP BY queries to avoid CROSS JOINs
// LIMIT enables early termination when ORDER BY is satisfied by index (#3253)
// Pass select_list for table elimination optimization (#3556)
let limit_val = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
Some(self.execute_from_with_where(
from_clause,
&cte_results,
resolved_where.as_ref(),
stmt.order_by.as_deref(),
limit_val,
Some(&stmt.select_list),
)?)
} else {
None
};
// Derive column names from the SELECT list (with table prefix for display)
// Issue #4696: For VALUES statements, select_list is empty - derive from VALUES rows
let columns = if stmt.select_list.is_empty() {
if let Some(values_rows) = &stmt.values {
// Generate column names: column1, column2, etc.
let num_cols = values_rows.first().map(|r| r.len()).unwrap_or(0);
(1..=num_cols).map(|i| format!("column{}", i)).collect()
} else {
// Empty select_list and no VALUES - return empty columns
Vec::new()
}
} else {
self.derive_column_names(&stmt.select_list, from_result.as_ref())?
};
// Execute the query to get rows
let rows = self.execute(stmt)?;
Ok(SelectResult { columns, rows })
}
/// Execute SELECT statement and return results with simple column names
///
/// This is similar to `execute_with_columns` but returns column names without
/// table prefixes. This is used for internal purposes like view creation
/// where the full table.column format would cause column lookup issues.
pub fn execute_with_simple_columns(
&self,
stmt: &vibesql_ast::SelectStmt,
) -> Result<SelectResult, ExecutorError> {
// Guard exponentially self-referential view nests before executing the
// FROM clause (which materializes views). See execute_with_columns and
// #5394 (view3.test).
crate::select::view_reference_guard::check_view_reference_limit(stmt, self.database)?;
// Resolve SELECT aliases in WHERE clause BEFORE predicate pushdown (SQLite extension)
// This allows queries like: SELECT f1-22 AS x FROM t1 WHERE x > 0
// IMPORTANT: Use schema-aware resolution to avoid incorrectly substituting
// table column names with aggregate aliases (SQLite behavior)
// PERFORMANCE: Skip alias resolution if no aliases exist (common case in OLTP)
let resolved_where = if stmt.where_clause.is_some()
&& crate::select::order::select_list_has_aliases(&stmt.select_list)
{
stmt.where_clause.as_ref().map(|where_expr| {
// Try to build early schema from FROM clause
if let Some(from_clause) = &stmt.from {
if let Some(early_schema) =
super::aggregation::build_early_schema(from_clause, self.database)
{
// Use schema-aware resolution
return crate::select::order::resolve_where_aliases_with_schema(
where_expr,
&stmt.select_list,
&early_schema,
);
}
}
// Fall back to non-schema-aware resolution for complex FROM clauses
crate::select::order::resolve_where_aliases(where_expr, &stmt.select_list)
})
} else {
stmt.where_clause.clone()
};
// Execute the FROM clause to get combined schema
let from_result = if let Some(from_clause) = &stmt.from {
let mut cte_results = if let Some(with_clause) = &stmt.with_clause {
execute_ctes(with_clause, self.database, |query, cte_ctx| {
self.execute_with_ctes(query, cte_ctx)
})?
} else {
HashMap::new()
};
// If we have access to outer query's CTEs (for subqueries/derived tables), merge them
if let Some(outer_cte_ctx) = self.cte_context {
for (name, result) in outer_cte_ctx {
cte_results.entry(name.clone()).or_insert_with(|| result.clone());
}
}
let limit_val = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
Some(self.execute_from_with_where(
from_clause,
&cte_results,
resolved_where.as_ref(),
stmt.order_by.as_deref(),
limit_val,
Some(&stmt.select_list),
)?)
} else {
None
};
// Derive column names from the SELECT list (without table prefix)
// Issue #4696: For VALUES statements, select_list is empty - derive from VALUES rows
let columns = if stmt.select_list.is_empty() {
if let Some(values_rows) = &stmt.values {
// Generate column names: column1, column2, etc.
let num_cols = values_rows.first().map(|r| r.len()).unwrap_or(0);
(1..=num_cols).map(|i| format!("column{}", i)).collect()
} else {
// Empty select_list and no VALUES - return empty columns
Vec::new()
}
} else {
self.derive_simple_column_names(&stmt.select_list, from_result.as_ref())?
};
// Execute the query to get rows
let rows = self.execute(stmt)?;
Ok(SelectResult { columns, rows })
}
/// Execute SELECT statement with CTE context
///
/// Uses unified strategy selection to determine the optimal execution path:
/// - NativeColumnar: Zero-copy SIMD execution from columnar storage
/// - StandardColumnar: SIMD execution with row-to-batch conversion
/// - RowOriented: Traditional row-by-row execution
/// - ExpressionOnly: SELECT without FROM clause (special case)
///
/// ## Pipeline-Based Execution (Phase 5)
///
/// This method uses the `ExecutionPipeline` trait to provide a unified interface
/// for query execution. Each strategy creates an appropriate pipeline that
/// implements filter, projection, aggregation, and limit/offset operations.
///
/// ```text
/// Strategy Selection → Create Pipeline → Execute via Trait Methods
/// ↓
/// NativeColumnar → NativeColumnarPipeline::apply_*()
/// StandardColumnar → ColumnarPipeline::apply_*()
/// RowOriented → RowOrientedPipeline::apply_*()
/// ExpressionOnly → Special case (no table scan)
/// ```
pub(super) fn execute_with_ctes(
&self,
stmt: &vibesql_ast::SelectStmt,
cte_results: &HashMap<String, CteResult>,
) -> Result<Vec<vibesql_storage::Row>, ExecutorError> {
// Note: Aggregate argument validation is done in execute() at the entry point.
// See issue #4367.
#[cfg(feature = "profile-q6")]
let _execute_ctes_start = std::time::Instant::now();
// Check if native columnar is enabled via feature flag or env var
let native_columnar_enabled =
cfg!(feature = "native-columnar") || std::env::var("VIBESQL_NATIVE_COLUMNAR").is_ok();
// Use unified strategy selection for the execution path
let strategy_ctx = StrategyContext::new(stmt, cte_results, native_columnar_enabled);
let strategy = choose_execution_strategy(&strategy_ctx);
log::debug!(
"Execution strategy selected: {} (reason: {})",
strategy.name(),
strategy.score().reason
);
#[cfg(feature = "profile-q6")]
eprintln!(
"[PROFILE-Q6] Execution strategy: {} ({})",
strategy.name(),
strategy.score().reason
);
// Dispatch based on selected strategy using ExecutionPipeline trait
// Pipeline execution returns Option<Vec<Row>> - None means fallback needed
let mut results = match strategy {
ExecutionStrategy::NativeColumnar { .. } => {
// First try the optimized zero-copy native columnar path
// This uses ColumnarBatch::from_storage_columnar() for zero-copy conversion
// and executes filter+aggregate in a single pass without row materialization
if let Some(result) = self.try_native_columnar_execution(stmt, cte_results)? {
#[cfg(feature = "profile-q6")]
eprintln!("[PROFILE-Q6] Native columnar: zero-copy path succeeded");
result
} else {
// Fall back to pipeline-based execution if zero-copy path is not applicable
// (e.g., complex predicates, multiple tables, unsupported aggregates)
log::debug!("Native columnar: zero-copy path not applicable, trying pipeline");
match self.execute_via_pipeline(
stmt,
cte_results,
NativeColumnarPipeline::new,
"NativeColumnar",
)? {
Some(result) => result,
None => {
// Fall back to row-oriented if pipeline also fails
log::debug!("Native columnar runtime fallback to row-oriented");
#[cfg(feature = "profile-q6")]
eprintln!("[PROFILE-Q6] Native columnar fallback to row-oriented");
self.execute_row_oriented(stmt, cte_results)?
}
}
}
}
ExecutionStrategy::StandardColumnar { .. } => {
// StandardColumnar uses the pipeline-based execution path
// Note: We don't use try_native_columnar_execution here because row tables
// go through the pipeline which correctly handles all data types including dates.
// The native columnar zero-copy path has known limitations with certain date
// comparisons.
match self.execute_via_pipeline(
stmt,
cte_results,
ColumnarPipeline::new,
"StandardColumnar",
)? {
Some(result) => result,
None => {
log::debug!("Standard columnar runtime fallback to row-oriented");
#[cfg(feature = "profile-q6")]
eprintln!("[PROFILE-Q6] Standard columnar fallback to row-oriented");
self.execute_row_oriented(stmt, cte_results)?
}
}
}
ExecutionStrategy::RowOriented { .. } => {
// Row-oriented uses the traditional path which has full feature support
// The RowOrientedPipeline is used for simpler queries, but complex
// queries (with JOINs, window functions, DISTINCT, etc.) need the
// full execute_row_oriented implementation
// Phase 4: Try columnar join execution for multi-table JOIN queries (#2943)
// This provides 3-5x speedup for TPC-H Q3 style queries
let has_joins = stmt
.from
.as_ref()
.is_some_and(|f| matches!(f, vibesql_ast::FromClause::Join { .. }));
if has_joins {
if let Some(result) = self.try_columnar_join_execution(stmt, cte_results)? {
log::info!("Columnar join execution succeeded");
// Apply LIMIT/OFFSET to columnar join results (#3776)
// Skip if set_operation exists - it will be applied later
if stmt.set_operation.is_none() {
let limit_val = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
let offset_val = stmt
.offset
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "OFFSET"))
.transpose()?;
apply_limit_offset(result, limit_val, offset_val)
} else {
result
}
} else {
log::debug!(
"Columnar join execution not applicable, falling back to row-oriented"
);
self.execute_row_oriented(stmt, cte_results)?
}
} else {
self.execute_row_oriented(stmt, cte_results)?
}
}
ExecutionStrategy::ExpressionOnly { .. } => {
// SELECT without FROM - special case that doesn't use pipelines
// May still have aggregates (e.g., SELECT COUNT(*), SELECT MAX(1))
// Note: Do NOT use early return here - we need to fall through to set operations
// handling
self.execute_expression_only(stmt, cte_results)?
}
};
// Handle set operations (UNION, INTERSECT, EXCEPT)
// Process operations left-to-right to ensure correct associativity
if let Some(set_op) = &stmt.set_operation {
// Extract collations from the leftmost SELECT list for set operation comparisons
// Use schema-aware lookup to get column collations from CREATE TABLE definitions
let collations = Self::extract_collations_from_select_list_with_schema(
&stmt.select_list,
Some(self.database),
stmt.from.as_ref(),
);
// Issue #4602: Compute left column count from AST for schema-level validation
// This is needed when the left result set is empty (table has no rows)
// Issue #4922: Must propagate errors (not use .ok()) to catch column count mismatches
// in set operations like UNION/INTERSECT/EXCEPT
let left_col_count = super::nonagg::compute_select_list_column_count(
stmt,
self.database,
Some(cte_results),
)?;
let left_col_count = Some(left_col_count);
results = self.execute_set_operations(
results,
set_op,
cte_results,
&collations,
left_col_count,
)?;
// Apply ORDER BY after set operations (if specified)
// The UNION's default sort is overridden by explicit ORDER BY
if let Some(order_by) = &stmt.order_by {
// Collect aliases from all UNION branches for ORDER BY resolution
// Pass database to enable wildcard expansion using table schemas
let all_aliases = super::set_operations::collect_union_aliases(self.database, stmt);
// Pass column collations from the first SELECT for collation-aware sorting
results = self.sort_set_operation_results(
results,
order_by,
&stmt.select_list,
&all_aliases,
&collations,
)?;
}
// Apply LIMIT/OFFSET to the final result (after all set operations and ORDER BY)
// For queries WITHOUT set operations, LIMIT/OFFSET is already applied
// in execute_without_aggregation() or execute_with_aggregation()
let limit_val = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
let offset_val = stmt
.offset
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "OFFSET"))
.transpose()?;
results = apply_limit_offset(results, limit_val, offset_val);
}
Ok(results)
}
/// Execute SELECT without FROM clause (ExpressionOnly strategy)
///
/// This is a special case that doesn't use the pipeline trait since there's
/// no table scan involved. Handles both simple expressions, aggregates,
/// and standalone VALUES statements.
fn execute_expression_only(
&self,
stmt: &vibesql_ast::SelectStmt,
cte_results: &HashMap<String, CteResult>,
) -> Result<Vec<vibesql_storage::Row>, ExecutorError> {
// Handle standalone VALUES statements (Issue #4546)
// VALUES(1,2), (3,4) returns rows directly without a SELECT list
if let Some(values_rows) = &stmt.values {
// Issue #5036: Check if any VALUES row contains window functions
// If so, evaluate those rows via SELECT-without-FROM path which supports
// window function evaluation, rather than the plain VALUES evaluator
let has_windows = values_rows
.iter()
.any(|row| row.iter().any(crate::select::window::expression_has_window_function));
let mut results = if has_windows {
let mut all_rows = Vec::with_capacity(values_rows.len());
for row_exprs in values_rows {
let row_has_window =
row_exprs.iter().any(crate::select::window::expression_has_window_function);
if row_has_window {
// Construct a SELECT <expr1>, <expr2>, ... statement
// and execute via the window-aware path
let select_list: Vec<vibesql_ast::SelectItem> = row_exprs
.iter()
.enumerate()
.map(|(i, expr)| vibesql_ast::SelectItem::Expression {
expr: expr.clone(),
alias: Some(format!("column{}", i + 1)),
source_text: None,
})
.collect();
let temp_stmt = vibesql_ast::SelectStmt {
with_clause: None,
distinct: false,
select_list,
into_table: None,
into_variables: None,
from: None,
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
set_operation: None,
values: None,
};
let row_results =
self.execute_select_without_from(&temp_stmt, cte_results)?;
all_rows.extend(row_results);
} else {
// No window functions - evaluate normally
let schema = crate::schema::CombinedSchema::empty();
let empty_row = vibesql_storage::Row::new(vec![]);
let mut evaluator =
crate::evaluator::CombinedExpressionEvaluator::with_database(
&schema,
self.database,
);
// Thread CTE context so subqueries in VALUES rows can
// reference names bound by an enclosing WITH (#5350)
if !cte_results.is_empty() {
evaluator = evaluator.with_cte_context(cte_results);
} else if let Some(cte_ctx) = self.cte_context {
evaluator = evaluator.with_cte_context(cte_ctx);
}
let mut values = Vec::with_capacity(row_exprs.len());
for expr in row_exprs {
let value = evaluator.eval(expr, &empty_row)?;
values.push(value);
}
all_rows.push(vibesql_storage::Row::new(values));
}
}
all_rows
} else {
// Thread CTE context so subqueries in VALUES rows can reference
// names bound by an enclosing WITH clause (#5353); fall back to
// the outer context when no local CTEs were materialized.
let cte_ctx =
if !cte_results.is_empty() { Some(cte_results) } else { self.cte_context };
let from_result = crate::select::scan::values::execute_values(
values_rows,
"_values_",
None,
Some(self.database),
cte_ctx,
)?;
from_result.into_rows()
};
// Issue #4696: If there's a set_operation, don't apply ORDER BY or LIMIT/OFFSET here
// Let the caller handle them after set operations are processed
if stmt.set_operation.is_some() {
return Ok(results);
}
// Apply ORDER BY if specified
if let Some(order_by) = &stmt.order_by {
// For VALUES, column names are column1, column2, etc.
// We need to map ORDER BY expressions to column indices
results = self.apply_values_order_by(results, order_by, values_rows)?;
}
// Apply LIMIT/OFFSET
let limit_val = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
let offset_val = stmt
.offset
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "OFFSET"))
.transpose()?;
return Ok(apply_limit_offset(results, limit_val, offset_val));
}
let has_aggregates = self.has_aggregates(&stmt.select_list) || stmt.having.is_some();
if has_aggregates {
// Aggregates without FROM need the aggregation path
self.execute_with_aggregation(stmt, cte_results)
} else {
// Simple expression evaluation (e.g., SELECT 1 + 1)
self.execute_select_without_from(stmt, cte_results)
}
}
/// Apply ORDER BY to VALUES statement results
fn apply_values_order_by(
&self,
mut rows: Vec<vibesql_storage::Row>,
order_by: &[vibesql_ast::OrderByItem],
values_rows: &[Vec<vibesql_ast::Expression>],
) -> Result<Vec<vibesql_storage::Row>, ExecutorError> {
// For VALUES, we sort by column position (1-indexed)
// or by column name (column1, column2, etc.)
let num_cols = values_rows.first().map(|r| r.len()).unwrap_or(0);
rows.sort_by(|a, b| {
for item in order_by {
// Get the column index from the ORDER BY expression
let col_idx = match &item.expr {
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(n)) => {
// 1-indexed column position
(*n as usize).saturating_sub(1)
}
vibesql_ast::Expression::ColumnRef(col_id) => {
// column1, column2, etc.
let col_name = col_id.column_canonical();
if let Some(stripped) = col_name.strip_prefix("column") {
stripped.parse::<usize>().unwrap_or(0).saturating_sub(1)
} else {
0
}
}
_ => 0, // Default to first column for complex expressions
};
if col_idx < num_cols {
let cmp = a.values[col_idx].partial_cmp(&b.values[col_idx]);
if let Some(ord) = cmp {
let ord = match item.direction {
vibesql_ast::OrderDirection::Asc => ord,
vibesql_ast::OrderDirection::Desc => ord.reverse(),
};
if ord != std::cmp::Ordering::Equal {
return ord;
}
}
}
}
std::cmp::Ordering::Equal
});
Ok(rows)
}
/// Execute a query using the specified execution pipeline
///
/// This method provides a unified interface for pipeline-based execution.
/// It creates the pipeline, prepares input, and executes the pipeline stages.
///
/// Returns `Ok(Some(results))` if the pipeline executed successfully,
/// `Ok(None)` if the pipeline cannot handle the query (fallback needed),
/// or `Err` if an error occurred.
///
/// # Type Parameters
///
/// * `P` - The pipeline type (must implement `ExecutionPipeline`)
/// * `F` - Factory function to create the pipeline
fn execute_via_pipeline<P, F>(
&self,
stmt: &vibesql_ast::SelectStmt,
cte_results: &HashMap<String, CteResult>,
create_pipeline: F,
strategy_name: &str,
) -> Result<Option<Vec<vibesql_storage::Row>>, ExecutorError>
where
P: ExecutionPipeline,
F: FnOnce() -> P,
{
#[cfg(feature = "profile-q6")]
let start = std::time::Instant::now();
// Check query complexity - pipelines don't support all features
let has_aggregates = self.has_aggregates(&stmt.select_list) || stmt.having.is_some();
let has_group_by = stmt.group_by.is_some();
let has_joins =
stmt.from.as_ref().is_some_and(|f| matches!(f, vibesql_ast::FromClause::Join { .. }));
let has_order_by = stmt.order_by.is_some();
let has_distinct = stmt.distinct;
let has_set_ops = stmt.set_operation.is_some();
let has_window_funcs = self.has_window_functions(&stmt.select_list);
let has_distinct_aggregates = self.has_distinct_aggregates(&stmt.select_list);
// Issue #5104: implicit-outer-aggregate-collapse requires the
// aggregation pipeline. The columnar pipelines do not support this
// semantic, so fall back to the row-oriented path which routes
// through `execute_with_aggregation`.
if !has_aggregates
&& !has_group_by
&& self.select_list_has_outer_aggregate_collapse(&stmt.select_list)
{
log::debug!(
"{} pipeline: implicit-outer-aggregate-collapse — falling back to row-oriented",
strategy_name
);
return Ok(None);
}
// Create the pipeline
let pipeline = create_pipeline();
// Check if the pipeline supports this query pattern
if !pipeline.supports_query_pattern(has_aggregates, has_group_by, has_joins) {
log::debug!(
"{} pipeline doesn't support query pattern (agg={}, group_by={}, joins={})",
strategy_name,
has_aggregates,
has_group_by,
has_joins
);
return Ok(None);
}
// For complex queries (ORDER BY, DISTINCT, window functions, set ops, DISTINCT aggregates),
// fall back to full execution paths which have complete support
if has_order_by
|| has_distinct
|| has_window_funcs
|| has_set_ops
|| has_distinct_aggregates
{
log::debug!(
"{} pipeline doesn't support complex features (order_by={}, distinct={}, window={}, set_ops={}, distinct_agg={})",
strategy_name,
has_order_by,
has_distinct,
has_window_funcs,
has_set_ops,
has_distinct_aggregates
);
return Ok(None);
}
// Must have a FROM clause for pipeline execution
let from_clause = match &stmt.from {
Some(from) => from,
None => return Ok(None),
};
// Execute FROM clause to get input data
// Note: WHERE, ORDER BY, and LIMIT are handled by the pipeline, not here
// Note: Table elimination requires WHERE clause, so pass None for select_list too
let from_result = self.execute_from_with_where(
from_clause,
cte_results,
None, // Pipeline will apply WHERE filter
None, // ORDER BY handled separately
None, // LIMIT applied after pipeline
None, // No table elimination when WHERE is deferred
)?;
// Build execution context
let mut exec_ctx = ExecutionContext::new(&from_result.schema, self.database);
// Add outer context for correlated subqueries (#2998)
if let (Some(outer_row), Some(outer_schema)) = (self.outer_row, self.outer_schema) {
exec_ctx = exec_ctx.with_outer_context(outer_row, outer_schema);
} else if let Some(proc_ctx) = self.procedural_context {
// Procedural variables in WHERE / SELECT (e.g. stored function bodies)
exec_ctx = exec_ctx.with_procedural_context(proc_ctx);
} else if let Some(trigger_ctx) = self.trigger_context {
// Issue #5082: thread trigger context so OLD/NEW pseudo-vars resolve in
// the synthetic SELECT used by UPDATE…FROM inside trigger bodies.
exec_ctx = exec_ctx.with_trigger_context(trigger_ctx);
}
// Add CTE context if available
if !cte_results.is_empty() {
exec_ctx = exec_ctx.with_cte_context(cte_results);
}
// Validate column references BEFORE processing
super::validation::validate_select_columns_with_context(
&stmt.select_list,
stmt.where_clause.as_ref(),
&from_result.schema,
self.procedural_context,
self.outer_schema,
)?;
// Prepare input from FROM result
let input = PipelineInput::from_rows_owned(from_result.data.into_rows());
// Execute pipeline stages with fallback on error
// If any pipeline stage fails with UnsupportedFeature, fall back to row-oriented
// Resolve SELECT aliases in WHERE clause (SQLite extension)
// This allows queries like: SELECT f1-22 AS x FROM t1 WHERE x > 0
// IMPORTANT: Use schema-aware resolution to avoid incorrectly substituting
// table column names with aggregate aliases (issue #4XXX)
// Example: SELECT COUNT(*) AS col1 FROM tab0 WHERE col1 > 0
// Here 'col1' in WHERE refers to the TABLE COLUMN, not the COUNT(*) alias
// PERFORMANCE: Skip alias resolution if no aliases exist (common case in OLTP)
let resolved_where = if crate::select::order::select_list_has_aliases(&stmt.select_list) {
stmt.where_clause.as_ref().map(|where_expr| {
crate::select::order::resolve_where_aliases_with_schema(
where_expr,
&stmt.select_list,
&from_result.schema,
)
})
} else {
stmt.where_clause.clone()
};
// Stage 1: Filter (WHERE clause)
let filtered = match pipeline.apply_filter(input, resolved_where.as_ref(), &exec_ctx) {
Ok(result) => result,
Err(ExecutorError::UnsupportedFeature(_))
| Err(ExecutorError::UnsupportedExpression(_)) => {
log::debug!("{} pipeline filter failed, falling back", strategy_name);
return Ok(None);
}
Err(e) => return Err(e),
};
// Stage 2: Projection or Aggregation
let result = if has_aggregates || has_group_by {
// Execute aggregation (includes projection)
// Get GROUP BY expressions if present (as slice)
let group_by_slice: Option<&[vibesql_ast::Expression]> =
stmt.group_by.as_ref().and_then(|g| g.as_simple()).map(|v| v.as_slice());
match pipeline.apply_aggregation(
filtered.into_input(),
&stmt.select_list,
group_by_slice,
stmt.having.as_ref(),
&exec_ctx,
) {
Ok(result) => result,
Err(ExecutorError::UnsupportedFeature(_))
| Err(ExecutorError::UnsupportedExpression(_)) => {
log::debug!("{} pipeline aggregation failed, falling back", strategy_name);
return Ok(None);
}
Err(e) => return Err(e),
}
} else {
// Execute projection only
match pipeline.apply_projection(filtered.into_input(), &stmt.select_list, &exec_ctx) {
Ok(result) => result,
Err(ExecutorError::UnsupportedFeature(_))
| Err(ExecutorError::UnsupportedExpression(_)) => {
log::debug!("{} pipeline projection failed, falling back", strategy_name);
return Ok(None);
}
Err(e) => return Err(e),
}
};
// Stage 3: Limit/Offset (evaluate expressions and convert to u64)
let limit_usize = stmt
.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?;
let offset_usize = stmt
.offset
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "OFFSET"))
.transpose()?;
let limit_u64 = limit_usize.map(|l| l as u64);
let offset_u64 = offset_usize.map(|o| o as u64);
let final_result = pipeline.apply_limit_offset(result, limit_u64, offset_u64)?;
#[cfg(feature = "profile-q6")]
{
eprintln!("[PROFILE-Q6] ✓ {} pipeline execution: {:?}", strategy_name, start.elapsed());
}
log::debug!("✓ {} pipeline execution succeeded", strategy_name);
Ok(Some(final_result))
}
/// Check if the select list contains window functions
fn has_window_functions(&self, select_list: &[vibesql_ast::SelectItem]) -> bool {
select_list.iter().any(|item| {
if let vibesql_ast::SelectItem::Expression { expr, .. } = item {
self.expr_has_window_function(expr)
} else {
false
}
})
}
/// Recursively check if an expression contains a window function
#[allow(clippy::only_used_in_recursion)]
fn expr_has_window_function(&self, expr: &vibesql_ast::Expression) -> bool {
match expr {
vibesql_ast::Expression::WindowFunction { .. } => true,
vibesql_ast::Expression::BinaryOp { left, right, .. } => {
self.expr_has_window_function(left) || self.expr_has_window_function(right)
}
vibesql_ast::Expression::UnaryOp { expr, .. } => self.expr_has_window_function(expr),
vibesql_ast::Expression::Function { args, .. } => {
args.iter().any(|arg| self.expr_has_window_function(arg))
}
vibesql_ast::Expression::Case { operand, when_clauses, else_result } => {
operand.as_ref().is_some_and(|e| self.expr_has_window_function(e))
|| when_clauses.iter().any(|case_when| {
case_when.conditions.iter().any(|c| self.expr_has_window_function(c))
|| self.expr_has_window_function(&case_when.result)
})
|| else_result.as_ref().is_some_and(|e| self.expr_has_window_function(e))
}
_ => false,
}
}
/// Check if the select list contains any DISTINCT aggregates (e.g., COUNT(DISTINCT x))
fn has_distinct_aggregates(&self, select_list: &[vibesql_ast::SelectItem]) -> bool {
select_list.iter().any(|item| {
if let vibesql_ast::SelectItem::Expression { expr, .. } = item {
self.expr_has_distinct_aggregate(expr)
} else {
false
}
})
}
/// Recursively check if an expression contains a DISTINCT aggregate
#[allow(clippy::only_used_in_recursion)]
fn expr_has_distinct_aggregate(&self, expr: &vibesql_ast::Expression) -> bool {
match expr {
vibesql_ast::Expression::AggregateFunction { distinct, .. } => *distinct,
vibesql_ast::Expression::BinaryOp { left, right, .. } => {
self.expr_has_distinct_aggregate(left) || self.expr_has_distinct_aggregate(right)
}
vibesql_ast::Expression::UnaryOp { expr, .. } => self.expr_has_distinct_aggregate(expr),
vibesql_ast::Expression::Function { args, .. } => {
args.iter().any(|arg| self.expr_has_distinct_aggregate(arg))
}
vibesql_ast::Expression::Case { operand, when_clauses, else_result } => {
operand.as_ref().is_some_and(|e| self.expr_has_distinct_aggregate(e))
|| when_clauses.iter().any(|case_when| {
case_when.conditions.iter().any(|c| self.expr_has_distinct_aggregate(c))
|| self.expr_has_distinct_aggregate(&case_when.result)
})
|| else_result.as_ref().is_some_and(|e| self.expr_has_distinct_aggregate(e))
}
_ => false,
}
}
/// Execute using traditional row-oriented path
///
/// This is the fallback path when columnar execution is not available or not beneficial.
fn execute_row_oriented(
&self,
stmt: &vibesql_ast::SelectStmt,
cte_results: &HashMap<String, CteResult>,
) -> Result<Vec<vibesql_storage::Row>, ExecutorError> {
let has_aggregates = self.has_aggregates(&stmt.select_list) || stmt.having.is_some();
let has_group_by = stmt.group_by.is_some();
// Issue #5104: SQLite's implicit-outer-aggregate-collapse semantics.
// When the SELECT list contains a bare scalar subquery whose body has
// an aggregate referencing an outer column, the outer query collapses
// into a single-row aggregate (with the inner aggregate computed over
// all outer rows). Route through the aggregation pipeline so the
// single-group grand-total path runs and `outer_rows` is wired through
// to the inner subquery.
let has_implicit_collapse = !has_aggregates
&& !has_group_by
&& self.select_list_has_outer_aggregate_collapse(&stmt.select_list);
if has_aggregates || has_group_by || has_implicit_collapse {
self.execute_with_aggregation(stmt, cte_results)
} else if let Some(from_clause) = &stmt.from {
// Re-enabled predicate pushdown for all queries (issue #1902)
//
// Previously, predicate pushdown was selectively disabled for multi-column IN clauses
// because index optimization happened in execute_without_aggregation() on row indices
// from the FROM result. When predicate pushdown filtered rows early, the indices no
// longer matched the original table, causing incorrect results.
//
// Now that all index optimization has been moved to the scan level
// (execute_index_scan), it happens BEFORE predicate pushdown, avoiding the
// row-index mismatch problem. This allows predicate pushdown to work
// correctly for all queries, improving performance.
//
// Fixes issues #1807, #1895, #1896, and #1902.
// Resolve SELECT aliases in WHERE clause BEFORE predicate pushdown (SQLite extension)
// This allows queries like: SELECT f1-22 AS x FROM t1 WHERE x > 0
// The alias 'x' is resolved to 'f1-22' so predicate pushdown can work correctly
// IMPORTANT: Use schema-aware resolution to avoid incorrectly substituting
// table column names with aggregate aliases (SQLite behavior)
// PERFORMANCE: Skip alias resolution if no aliases exist (common case in OLTP)
let resolved_where = if stmt.where_clause.is_some()
&& crate::select::order::select_list_has_aliases(&stmt.select_list)
{
stmt.where_clause.as_ref().map(|where_expr| {
// Try to build early schema from FROM clause
if let Some(early_schema) =
super::aggregation::build_early_schema(from_clause, self.database)
{
// Use schema-aware resolution
return crate::select::order::resolve_where_aliases_with_schema(
where_expr,
&stmt.select_list,
&early_schema,
);
}
// Fall back to non-schema-aware resolution for complex FROM clauses
crate::select::order::resolve_where_aliases(where_expr, &stmt.select_list)
})
} else {
stmt.where_clause.clone()
};
// Pass WHERE, ORDER BY, and LIMIT to execute_from for optimization
// LIMIT enables early termination when ORDER BY is satisfied by index (#3253)
// Pass select_list for table elimination optimization (#3556)
//
// Don't pass ORDER BY if there's a set operation - it will be handled at the set
// operation level
let order_by_hint =
if stmt.set_operation.is_some() { None } else { stmt.order_by.as_deref() };
// Don't pass LIMIT hint for set operations - limit must be applied after combining
// results This prevents early termination from incorrectly limiting the
// left side of UNION queries
let limit_val = if stmt.set_operation.is_some() {
None
} else {
stmt.limit
.as_ref()
.map(|expr| self.eval_limit_offset_expr(expr, "LIMIT"))
.transpose()?
};
let from_result = self.execute_from_with_where(
from_clause,
cte_results,
resolved_where.as_ref(),
order_by_hint,
limit_val,
Some(&stmt.select_list),
)?;
// Validate column references BEFORE processing rows (issue #2654)
// This ensures column errors are caught even when tables are empty
// Pass procedural context to allow procedure variables in WHERE clause
// Pass outer_schema for correlated subqueries (#2694)
// Note: We validate with the resolved_where since that's what gets executed
super::validation::validate_select_columns_with_context(
&stmt.select_list,
resolved_where.as_ref(),
&from_result.schema,
self.procedural_context,
self.outer_schema,
)?;
self.execute_without_aggregation(stmt, from_result, cte_results)
} else {
// SELECT without FROM - evaluate expressions as a single row
self.execute_select_without_from(stmt, cte_results)
}
}
/// Execute a FROM clause with WHERE, ORDER BY, and LIMIT for optimization
///
/// The LIMIT parameter enables early termination optimization (#3253):
/// - When ORDER BY is satisfied by an index and no post-filter is needed, the index scan can
/// stop after fetching LIMIT rows
///
/// Note: Table elimination (#3556) is now handled at the optimizer level
/// via crate::optimizer::eliminate_unused_tables(), which runs before
/// semi-join transformation to avoid complex interactions.
pub(super) fn execute_from_with_where(
&self,
from: &vibesql_ast::FromClause,
cte_results: &HashMap<String, CteResult>,
where_clause: Option<&vibesql_ast::Expression>,
order_by: Option<&[vibesql_ast::OrderByItem]>,
limit: Option<usize>,
_select_list: Option<&[vibesql_ast::SelectItem]>, /* No longer used - optimization moved
* to optimizer pass */
) -> Result<FromResult, ExecutorError> {
use crate::select::scan::execute_from_clause;
let from_result = execute_from_clause(
from,
cte_results,
self.database,
where_clause,
order_by,
limit,
self.outer_row,
self.outer_schema,
|query| {
// For derived table subqueries, create a child executor with:
// 1. CTE context (allows CTEs from outer WITH clause to be referenced)
// 2. Outer context (allows correlated columns from outer queries to be resolved)
// Critical for:
// - TPC-DS Q2: CTEs in FROM subqueries
// - select1-18.x: Nested correlated subqueries referencing outer columns
if !cte_results.is_empty() {
// FIX for select1-18.x: When both CTE and outer context exist,
// we need to use new_with_outer_and_cte_and_depth to pass both
if let (Some(outer_row), Some(outer_schema)) =
(self.outer_row, self.outer_schema)
{
let child = SelectExecutor::new_with_outer_and_cte_and_depth(
self.database,
outer_row,
outer_schema,
cte_results,
self.subquery_depth,
);
child.execute_with_columns(query)
} else {
let child = SelectExecutor::new_with_cte_and_depth(
self.database,
cte_results,
self.subquery_depth,
);
child.execute_with_columns(query)
}
} else if let (Some(outer_row), Some(outer_schema)) =
(self.outer_row, self.outer_schema)
{
// FIX for select1-18.x: Pass outer context for derived table subqueries
// This enables column resolution from outer scopes in deeply nested queries
let child = SelectExecutor::new_with_outer_context_and_depth(
self.database,
outer_row,
outer_schema,
self.subquery_depth,
);
child.execute_with_columns(query)
} else {
self.execute_with_columns(query)
}
},
)?;
// NOTE: We DON'T merge outer schema with from_result.schema here because:
// 1. from_result.rows only contain values from inner tables
// 2. Outer columns are resolved via the evaluator's outer_row/outer_schema
// 3. Merging would create schema/row mismatch (schema has outer cols, rows don't)
Ok(from_result)
}
}