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
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
impl Executor {
/// Execute a simple table scan
fn execute_simple_table_scan(
&self,
table_source: &SimpleTableSource,
stmt: &SelectStatement,
ctx: &ExecutionContext,
classification: &std::sync::Arc<QueryClassification>,
) -> SelectResult {
// OPTIMIZATION: Use pre-computed lowercase name to avoid allocation per query
let table_name = &table_source.name.value_lower;
let source = open_query_source(
&self.engine,
&self.active_transaction,
table_name,
table_source.as_of.is_some(),
)?;
let current = match source {
QuerySourceHandle::Current(current) => current,
QuerySourceHandle::Temporal(transaction) => {
return self.execute_temporal_query(
table_name,
table_source
.as_of
.as_ref()
.expect("temporal source lost AS OF"),
stmt,
ctx,
transaction.as_ref(),
classification,
);
}
};
let (table, _standalone_tx, in_explicit_transaction) = current.into_parts();
// Build column list from schema (using cached version to avoid repeated clones)
let all_columns: Vec<String> = table.schema().column_names_owned().to_vec();
// Get pre-cached lowercase column names to avoid per-query to_lowercase() calls
let all_columns_lower = table.schema().column_names_lower_arc();
// Get table alias for correlated subquery support
let table_alias: Option<String> = table_source
.alias
.as_ref()
.map(|a| a.value_lower.to_string())
.or_else(|| Some(table_name.to_string()));
// classification is passed from caller to avoid redundant cache lookups
// AGGREGATION PUSHDOWN: Try to compute simple aggregates directly on storage
// This avoids all row materialization for queries like:
// - SELECT COUNT(*) FROM table
// - SELECT SUM(col), MIN(col), MAX(col) FROM table
// Must check before any row collection happens
if classification.has_aggregation && !classification.has_window_functions {
// First try global aggregation pushdown (no GROUP BY, no WHERE)
if let Some(result) =
self.try_aggregation_pushdown(table.as_ref(), stmt, ctx, classification)?
{
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
// Try filtered aggregation pushdown (WHERE + aggregates, no GROUP BY)
// Pushes both the filter and aggregation into the storage layer
if classification.has_where {
if let Some(result) = self.try_filtered_aggregation_pushdown(
table.as_ref(),
stmt,
ctx,
classification,
&all_columns,
)? {
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
}
// Try storage-level GROUP BY aggregation.
// This computes aggregates directly in storage without executor-side row collection.
if classification.has_group_by {
if let Some(result) = self.try_storage_aggregation(
table.as_ref(),
stmt,
ctx,
&all_columns,
classification,
) {
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
}
// Try streaming aggregation for expressions like AVG(col) * 100
// This avoids collecting all rows by streaming through a scanner
if let Some(result) =
self.try_streaming_global_aggregation(table.as_ref(), stmt, ctx, classification)?
{
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
}
// DISTINCT PUSHDOWN: Try to get distinct values directly from index
// This avoids all row materialization for queries like:
// - SELECT DISTINCT col FROM table (where col is indexed)
// Returns distinct values in O(unique values) instead of O(rows)
if classification.has_distinct {
if let Some(result) =
self.try_distinct_pushdown(table.as_ref(), stmt, &all_columns, classification)?
{
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
}
// Check if ORDER BY references columns not in SELECT
let order_by_needs_extra_columns = self.order_by_needs_extra_columns(stmt, &all_columns);
let prepared_predicate = access_predicate::prepare_scan_predicate(
&stmt.columns,
stmt.where_clause.as_deref(),
&all_columns,
table.schema(),
table_alias.as_deref(),
ctx,
self.function_registry.as_ref(),
classification.where_has_subqueries,
);
let where_to_use = prepared_predicate.effective();
let plugin_candidate_rows = self.try_plugin_candidate_scan(
table_name,
table_alias.as_deref(),
table.as_ref(),
where_to_use,
&all_columns,
ctx,
)?;
// Check if this query might reference outer columns (correlated)
let has_outer_context = ctx.outer_row().is_some();
// SEMANTIC CACHE: Check if we can serve this query from cache
// Eligible queries: simple column projections with WHERE, no aggregation/window/grouping, no outer context
// Use cached classification for is_select_star check
let is_select_star = classification.is_select_star;
let has_aggregation_window_grouping = classification.has_aggregation
|| classification.has_window_functions
|| classification.has_group_by;
// Use cached classification for subquery detection (avoids AST traversal)
let has_subqueries_in_where = classification.where_has_subqueries;
// CRITICAL: Check if WHERE clause contains parameters ($1, $2, etc.)
// Parameterized queries CANNOT be cached because:
// 1. The cache stores results tied to specific parameter values
// 2. But the AST only has parameter indices ($1), not actual values
// 3. A cache "hit" would return wrong results for different parameter values
// This was causing 100x slowdown for SELECT by ID queries due to:
// - Cache misses on every lookup (unique predicates)
// - Streaming disabled for cache-eligible queries
// - Cache insertions (write locks) on every query execution
// Use cached classification for parameter detection (avoids AST traversal)
let has_parameters_in_where = classification.where_has_parameters;
// Cache eligibility: We cache SELECT * queries because:
// 1. The cache stores full table rows with their original column layout
// 2. Subsumption detection works on full rows for filtering
// 3. For non-SELECT * queries, we would need to project cached rows on hit
//
// CRITICAL: Disable caching during explicit transactions (BEGIN/COMMIT)
// to preserve MVCC isolation guarantees. A transaction must see its own
// consistent snapshot, not cached results from other transactions.
let cache_eligible = is_select_star
&& plugin_candidate_rows.is_none()
&& where_to_use.is_some()
&& !has_aggregation_window_grouping
&& !has_outer_context
&& !has_subqueries_in_where
&& !has_parameters_in_where // Parameters can't be cached (values not in AST)
&& !classification.has_nondeterministic_functions // NOW(), RANDOM(), etc. return different values per execution
&& !classification.has_order_by
&& !classification.has_distinct
&& !classification.has_limit
&& !in_explicit_transaction; // MVCC safety: no caching in transactions
let semantic_cache_generation = cache_eligible.then(|| self.semantic_cache.generation());
// Try cache lookup for eligible queries
if cache_eligible {
if let Some(where_expr) = where_to_use {
use super::semantic_cache::CacheLookupResult;
match self
.semantic_cache
.lookup(table_name, &all_columns, Some(where_expr))
{
CacheLookupResult::ExactHit(rows_arc) => {
// Exact cache hit - return cached rows with zero-copy sharing
let output_columns = CompactArc::new(self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
));
let result = ExecutorResult::with_arc_columns_shared_rows(
CompactArc::clone(&output_columns),
rows_arc,
);
return Ok((Box::new(result), output_columns, false, None));
}
CacheLookupResult::SubsumptionHit {
rows: rows_arc,
filter,
columns,
} => {
// Subsumption hit - filter cached rows
// Clone the Vec since we need to filter (creates new Vec anyway)
use super::semantic_cache::SemanticCache;
let filtered_vec = SemanticCache::filter_rows(
(*rows_arc).clone(),
&filter,
&columns,
&self.function_registry,
)?;
// Convert Vec<Row> to RowVec
let filtered_rows: RowVec = filtered_vec
.into_iter()
.enumerate()
.map(|(i, row)| (i as i64, row))
.collect();
let output_columns = CompactArc::new(self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
));
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
filtered_rows,
);
return Ok((Box::new(result), output_columns, false, None));
}
CacheLookupResult::Miss => {
// Cache miss - continue with normal execution
// Result will be inserted into cache below
}
}
}
}
let storage_expr = &prepared_predicate.storage;
let needs_memory_filter = prepared_predicate.needs_memory_filter();
let memory_where_to_use = prepared_predicate.memory_filter();
// ZONE MAP PRUNING: Short-circuit if zone maps indicate no rows can match
// This checks min/max statistics per segment to skip entire scan when
// the WHERE clause predicates are outside all segment ranges
// IMPORTANT: Don't short-circuit if we have aggregation, window functions, or GROUP BY
// because aggregation on empty results needs to produce output (e.g., COUNT=0)
let has_aggregation_or_grouping = classification.has_aggregation
|| classification.has_window_functions
|| classification.has_group_by;
if !has_aggregation_or_grouping {
if let Some(ref expr) = storage_expr {
if self
.get_query_planner()
.can_prune_entire_scan(&*table, expr.as_ref())
{
// Zone maps indicate no segments can match - return empty result
let output_columns = CompactArc::new(self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
));
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
RowVec::new(),
);
return Ok((Box::new(result), output_columns, true, None));
}
}
}
// FAST PATH: MIN/MAX index optimization
// For queries like `SELECT MIN(col) FROM table` or `SELECT MAX(col) FROM table`
// without WHERE or GROUP BY, use the index directly (O(1) instead of O(n))
if storage_expr.is_none() && !needs_memory_filter && !classification.has_group_by {
if let Some((result, columns)) =
self.try_min_max_index_optimization(stmt, &*table, &all_columns)?
{
return Ok((result, columns, false, None));
}
}
// FAST PATH: COUNT(*) pushdown optimization
// For queries like `SELECT COUNT(*) FROM table` without WHERE or GROUP BY,
// use the table's row_count() method instead of scanning all rows
if storage_expr.is_none() && !needs_memory_filter && !classification.has_group_by {
if let Some((result, columns)) = self.try_count_star_optimization(stmt, &*table)? {
return Ok((result, columns, false, None));
}
}
// FAST PATH: Streaming GROUP BY optimization using B-tree index
// For queries like `SELECT user_id, SUM(amount) FROM orders GROUP BY user_id`
// where user_id has a B-tree index, we iterate through the index in sorted order
// and aggregate each group without using a hash map (SQLite-style sorted GROUP BY)
if storage_expr.is_none()
&& !needs_memory_filter
&& classification.has_group_by
&& classification.has_aggregation
&& !classification.has_window_functions
&& !classification.has_order_by
// No ORDER BY to avoid re-sorting
{
if let Some((result, columns)) =
self.try_streaming_group_by(stmt, &*table, &all_columns, ctx)?
{
return Ok((result, columns, false, None));
}
}
// FAST PATH: ORDER BY + LIMIT optimization (TOP-N)
// For queries like `SELECT * FROM table ORDER BY indexed_col LIMIT 10`,
// use index to get rows in sorted order directly, avoiding full table sort
if classification.has_limit
&& stmt.order_by.len() == 1
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !classification.has_distinct
&& storage_expr.is_none()
&& !needs_memory_filter
{
if let Some((result, columns)) =
self.try_order_by_index_optimization(stmt, &*table, &all_columns, ctx)?
{
// Note: ORDER BY + LIMIT already handles LIMIT at storage level
return Ok((result, columns, true, None));
}
}
// FAST PATH: exact parallel vector Top-K.
// For queries like `SELECT id, VEC_DISTANCE_L2(embedding, '...') AS dist
// FROM documents ORDER BY dist LIMIT 10`
// Supports WHERE by pre-filtering rows before exact distance evaluation.
// Skip when transaction has local changes — HNSW index and collect_rows_by_ids
// only see committed data, so local INSERTs/UPDATEs would be missed.
if classification.has_limit
&& stmt.order_by.len() == 1
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !classification.has_distinct
&& !table.has_local_changes()
{
if let Some((result, columns)) =
self.try_vector_search_optimization(stmt, &*table, &all_columns, ctx)?
{
return Ok((result, columns, true, None));
}
}
// FAST PATH: Keyset pagination optimization
// For queries like `SELECT * FROM table WHERE id > X ORDER BY id LIMIT Y`,
// use the PK's ordering to start iteration from X directly.
// This provides O(limit) complexity instead of O(n) for full scans.
// Note: OFFSET is not supported - queries with OFFSET fall through to regular execution.
if classification.has_limit
&& stmt.offset.is_none()
&& stmt.order_by.len() == 1
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !classification.has_distinct
&& !needs_memory_filter
{
if let Some((result, columns)) = self.try_keyset_pagination_optimization(
stmt,
where_to_use,
&*table,
&all_columns,
table_alias.as_deref(),
ctx,
)? {
return Ok((result, columns, true, None));
}
}
// FAST PATH: equality-prefix + trailing range over a composite index.
// Unlike the PK-only keyset path, this operator also merges persisted
// artifact-backed postings with the hot MVCC index and preserves index order.
if classification.has_limit
&& stmt.order_by.len() == 1
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !classification.has_distinct
&& !needs_memory_filter
{
if let Some(ref expr) = storage_expr {
if let Some((result, columns)) = self.try_composite_ordered_range_optimization(
stmt,
expr.as_ref(),
&*table,
&all_columns,
table_alias.as_deref(),
ctx,
)? {
return Ok((result, columns, true, None));
}
}
}
// FAST PATH: IN subquery index optimization
// For queries like `SELECT * FROM table WHERE id IN (SELECT col FROM other_table WHERE ...)`
// where 'id' has an index or is PRIMARY KEY, probe directly instead of scanning all rows
// Skip if query has aggregation: projection cannot compile aggregate functions
if needs_memory_filter
&& !has_outer_context
&& !classification.has_group_by
&& !classification.has_aggregation
{
if let Some(where_expr) = where_to_use {
if let Some((result, columns)) = self.try_in_subquery_index_optimization(
stmt,
where_expr,
&*table,
&all_columns,
table_alias.as_deref(),
ctx,
classification,
)? {
return Ok((result, columns, false, None));
}
}
}
// FAST PATH: IN list literal index optimization
// For queries like `SELECT * FROM table WHERE id IN (1, 2, 3, 5, 8)`
// where 'id' has an index or is PRIMARY KEY, probe directly instead of scanning all rows
// Skip if query has aggregation: projection cannot compile aggregate functions
if needs_memory_filter
&& !has_outer_context
&& !classification.has_group_by
&& !classification.has_aggregation
{
if let Some(where_expr) = where_to_use {
if let Some((result, columns)) = self.try_in_list_index_optimization(
stmt,
where_expr,
&*table,
&all_columns,
table_alias.as_deref(),
ctx,
classification,
)? {
return Ok((result, columns, false, None));
}
}
}
// FAST PATH: LIMIT pushdown optimization
// For simple queries like `SELECT * FROM table LIMIT 10` or
// `SELECT * FROM table WHERE indexed_col = value LIMIT 10` without ORDER BY,
// we can stop scanning early at the storage layer
let can_pushdown_limit = classification.has_limit
&& !classification.has_order_by
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !classification.has_distinct
&& !needs_memory_filter; // Allow with storage_expr (WHERE on indexed columns)
if can_pushdown_limit {
let limit = if let Some(ref limit_expr) = stmt.limit {
match ExpressionEval::compile(limit_expr, &[])?
.with_context(ctx)
.eval_slice(&Row::new())?
{
Value::Integer(l) if l >= 0 => l as usize,
Value::Integer(l) => {
return Err(Error::Parse(format!(
"LIMIT must be non-negative, got {}",
l
)));
}
Value::Float(f) if f >= 0.0 => f as usize,
Value::Float(f) => {
return Err(Error::Parse(format!(
"LIMIT must be non-negative, got {}",
f
)));
}
_ => usize::MAX,
}
} else {
usize::MAX
};
let offset = if let Some(ref offset_expr) = stmt.offset {
match ExpressionEval::compile(offset_expr, &[])?
.with_context(ctx)
.eval_slice(&Row::new())?
{
Value::Integer(o) if o >= 0 => o as usize,
Value::Integer(o) => {
return Err(Error::Parse(format!(
"OFFSET must be non-negative, got {}",
o
)));
}
Value::Float(f) if f >= 0.0 => f as usize,
Value::Float(f) => {
return Err(Error::Parse(format!(
"OFFSET must be non-negative, got {}",
f
)));
}
_ => 0,
}
} else {
0
};
if let Some((column_indices, output_column_names)) =
self.get_simple_projection_indices(&stmt.columns, &all_columns)
{
// Simple column projection can be pushed to the table boundary.
// Implementations keep their existing LIMIT/OFFSET order and
// early-termination strategy; cold columnar tables can also avoid
// reading unused columns.
let rows = table.collect_rows_with_limit_unordered_projected(
&column_indices,
storage_expr.as_deref(),
limit,
offset,
)?;
let output_columns = CompactArc::new(output_column_names);
let result =
ExecutorResult::with_arc_columns(CompactArc::clone(&output_columns), rows);
// LIMIT/OFFSET already applied during scanner consumption.
return Ok((Box::new(result), output_columns, true, None));
}
if !classification.select_has_scalar_subqueries {
let output_columns = self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
);
if let Some(plan) = self.build_expression_projection_scan_plan(
&stmt.columns,
&output_columns,
&all_columns,
) {
// Complex expressions can still be evaluated from a narrow
// dependency row. Use exact projection semantics because the
// dependency set may legitimately be empty for constant
// expressions.
let rows = table.collect_rows_with_limit_unordered_exact_projected(
&plan.scan_indices,
storage_expr.as_deref(),
limit,
offset,
)?;
let scan_columns_lower: Vec<String> =
plan.scan_columns.iter().map(|c| c.to_lowercase()).collect();
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
rows,
&plan.scan_columns,
Some(&scan_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns = CompactArc::new(plan.output_columns);
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
projected_rows,
);
return Ok((Box::new(result), output_columns, true, None));
}
}
// Fallback: complex SELECT expressions with unsupported dependency
// extraction still need full rows so the evaluator can access every
// source column it may reference.
let rows =
table.collect_rows_with_limit_unordered(storage_expr.as_deref(), limit, offset)?;
// Project rows according to SELECT expressions
// Note: collect_rows_with_limit always returns full rows (all columns),
// so we must always project here regardless of scanner_handled_projection
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
rows,
&all_columns,
Some(&all_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns = CompactArc::new(self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
));
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
projected_rows,
);
// LIMIT/OFFSET already applied at storage level
return Ok((Box::new(result), output_columns, true, None));
}
// STREAMING PATH: For simple queries without aggregation/window/ORDER BY
// Use streaming result to avoid materializing all rows into Vec
//
// For cache-eligible queries, we disable streaming ONLY if the table is small
// enough to potentially benefit from caching. Large tables (>100K rows) would
// exceed the cache limit anyway, so we allow streaming for them.
//
// OPTIMIZATION: Use row_count_hint() which is O(1) instead of row_count() which
// is O(n) with visibility checks. The hint returns an upper bound (versions.len())
// which is safe for this decision - if hint > threshold, actual count is also large.
let should_disable_streaming_for_cache = cache_eligible && {
let table_row_count = table.row_count_hint();
table_row_count <= super::semantic_cache::DEFAULT_MAX_CACHED_ROWS
};
let can_use_streaming = !classification.has_order_by
&& plugin_candidate_rows.is_none()
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !order_by_needs_extra_columns
&& !has_outer_context // Can't stream with outer context (correlated subqueries)
&& !should_disable_streaming_for_cache; // Only disable streaming for small cacheable queries
// Check for CORRELATED subqueries in WHERE - these require per-row evaluation
// Non-correlated scalar subqueries (like SELECT AVG(...)) are OK - they're evaluated once
// and the result is used as a literal value for all rows
let has_correlated_where_subqueries = classification.where_has_correlated_subqueries;
// CRITICAL OPTIMIZATION: When needs_memory_filter is true AND we have LIMIT,
// skip the streaming path. The streaming path pre-materializes ALL rows from
// storage (via collect_visible_rows) before FilteredResult applies the filter.
// This defeats early termination for LIMIT queries.
// Fall through to the parallel/sequential path which has proper early termination.
let skip_streaming_for_memory_filter_with_limit =
needs_memory_filter && stmt.limit.is_some() && stmt.order_by.is_empty();
if can_use_streaming
&& !has_correlated_where_subqueries
&& !skip_streaming_for_memory_filter_with_limit
{
// Check if we have simple column projection (no complex expressions)
let simple_projection = self.get_simple_projection_indices(&stmt.columns, &all_columns);
if let Some((column_indices, output_columns)) = simple_projection {
if needs_memory_filter && !classification.where_has_subqueries {
if let Some(where_expr) = memory_where_to_use {
if let Some(plan) = self.build_filtered_simple_projection_scan_plan(
where_expr,
&column_indices,
&output_columns,
&all_columns,
) {
let scanner = access_scan::open_exact_projection_scan(
table.as_ref(),
&plan.scan_indices,
storage_expr.as_deref(),
ctx,
)?;
let mut result: Box<dyn QueryResult> =
Box::new(ScannerResult::new(scanner, plan.scan_columns.clone()));
let filter =
RowFilter::new(where_expr, &plan.scan_columns)?.with_context(ctx);
result = Box::new(FilteredResult::from_filter(result, filter));
let output_columns = CompactArc::new(plan.output_columns);
result = Box::new(StreamingProjectionResult::new(
result,
plan.output_indices_in_scan,
(*output_columns).clone(),
));
return Ok((result, output_columns, false, None));
}
}
}
// All columns are simple references - we can stream!
//
// If no residual memory filter is needed, push the projection into
// the storage scanner and avoid streaming full rows only to trim
// them later. Duplicate projections (SELECT a, a) are part of the
// scan contract: the scanner returns one projected value per
// requested index.
let scanner_handles_projection = !needs_memory_filter;
let scan_columns: Vec<usize> = if scanner_handles_projection {
column_indices.clone()
} else {
(0..all_columns.len()).collect()
};
let scanner_columns = if scanner_handles_projection {
output_columns.clone()
} else {
all_columns.to_vec()
};
let scanner = if scanner_handles_projection {
access_scan::open_exact_projection_scan(
table.as_ref(),
&scan_columns,
storage_expr.as_deref(),
ctx,
)?
} else {
access_scan::open_scan(
table.as_ref(),
&scan_columns,
storage_expr.as_deref(),
ctx,
)?
};
// Wrap scanner in ScannerResult
let mut result: Box<dyn QueryResult> =
Box::new(ScannerResult::new(scanner, scanner_columns));
// If we need memory filtering (complex WHERE that couldn't be pushed down)
if needs_memory_filter {
if let Some(where_expr) = memory_where_to_use {
// Check if there are non-correlated scalar subqueries to process
// These need to be evaluated once before streaming
let has_scalar_subqueries = classification.where_has_subqueries
&& !classification.where_has_correlated_subqueries;
let filter = if has_scalar_subqueries {
// Process scalar subqueries to resolve them to literal values
let processed = self.process_where_subqueries(where_expr, ctx)?;
RowFilter::new(&processed, &all_columns)?.with_context(ctx)
} else {
// No scalar subqueries - use expression directly
RowFilter::new(where_expr, &all_columns)?.with_context(ctx)
};
result = Box::new(FilteredResult::from_filter(result, filter));
}
}
// Apply projection if needed (not SELECT *)
// OPTIMIZATION: Check if projection is identity without allocating a Vec
let is_identity_projection = column_indices.len() == all_columns.len()
&& column_indices.iter().enumerate().all(|(i, &idx)| idx == i);
// Check if column names differ (aliases)
let names_differ = output_columns.len() != all_columns.len()
|| output_columns
.iter()
.zip(all_columns.iter())
.any(|(out, all)| out != all);
// Need StreamingProjectionResult if either:
// 1. Non-identity projection (different columns or reordered)
// 2. Column names differ (aliases like "SELECT id AS a")
if scanner_handles_projection {
let output_columns = CompactArc::new(output_columns);
// LIMIT/OFFSET NOT applied yet - streaming path
return Ok((result, output_columns, false, None));
} else if !column_indices.is_empty() && (!is_identity_projection || names_differ) {
let output_columns = CompactArc::new(output_columns);
result = Box::new(StreamingProjectionResult::new(
result,
column_indices,
(*output_columns).clone(),
));
// LIMIT/OFFSET NOT applied yet - streaming path
return Ok((result, output_columns, false, None));
} else {
// SELECT * - no projection needed
// LIMIT/OFFSET NOT applied yet - streaming path
return Ok((result, CompactArc::new(output_columns), false, None));
}
} else if !classification.select_has_scalar_subqueries {
// STREAMING WITH EXPRESSIONS: Use ExprMappedResult for complex projections
// This avoids batch allocation when SELECT contains expressions like CASE
// NOTE: Only use this path when SELECT doesn't have subqueries (which need special processing)
if needs_memory_filter && !classification.where_has_subqueries {
if let Some(where_expr) = memory_where_to_use {
let output_columns = self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
);
if let Some(plan) = self.build_filtered_expression_projection_scan_plan(
where_expr,
&stmt.columns,
&output_columns,
&all_columns,
) {
let scanner = access_scan::open_exact_projection_scan(
table.as_ref(),
&plan.scan_indices,
storage_expr.as_deref(),
ctx,
)?;
let mut result: Box<dyn QueryResult> =
Box::new(ScannerResult::new(scanner, plan.scan_columns.clone()));
let filter =
RowFilter::new(where_expr, &plan.scan_columns)?.with_context(ctx);
result = Box::new(FilteredResult::from_filter(result, filter));
let output_columns = CompactArc::new(plan.output_columns);
result = Box::new(ExprMappedResult::with_context(
result,
stmt.columns.clone(),
(*output_columns).clone(),
ctx,
)?);
return Ok((result, output_columns, false, None));
}
}
}
let column_idx_vec: Vec<usize> = (0..all_columns.len()).collect();
let scanner = access_scan::open_scan(
table.as_ref(),
&column_idx_vec,
storage_expr.as_deref(),
ctx,
)?;
let mut result: Box<dyn QueryResult> =
Box::new(ScannerResult::new(scanner, all_columns.clone()));
// If we need memory filtering
if needs_memory_filter {
if let Some(where_expr) = memory_where_to_use {
let has_scalar_subqueries = classification.where_has_subqueries
&& !classification.where_has_correlated_subqueries;
let filter = if has_scalar_subqueries {
let processed = self.process_where_subqueries(where_expr, ctx)?;
RowFilter::new(&processed, &all_columns)?.with_context(ctx)
} else {
RowFilter::new(where_expr, &all_columns)?.with_context(ctx)
};
result = Box::new(FilteredResult::from_filter(result, filter));
}
}
// Use ExprMappedResult for expression-based projection with buffer reuse
let output_columns = self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
);
let output_columns = CompactArc::new(output_columns);
result = Box::new(ExprMappedResult::with_context(
result,
stmt.columns.clone(),
(*output_columns).clone(),
ctx,
)?);
return Ok((result, output_columns, false, None));
}
}
let can_use_ordered_projection_scan_plan = (classification.has_order_by
|| classification.has_distinct_on)
&& (classification.has_limit || classification.has_offset)
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions
&& !classification.select_has_scalar_subqueries
&& !classification.order_by_has_correlated_subqueries;
let ordered_projection_scan_plan = if can_use_ordered_projection_scan_plan
&& !needs_memory_filter
{
let output_columns =
self.get_output_column_names(&stmt.columns, &all_columns, table_alias.as_deref());
self.build_ordered_distinct_projection_scan_plan(stmt, &output_columns, &all_columns)
} else {
None
};
// Collect rows - choose optimal path based on query type
// PARALLEL EXECUTION: Use parallel filtering for large datasets
let parallel_config = ParallelConfig::default();
let rows_result: CollectedTableRows = if let Some(candidate) = plugin_candidate_rows {
CollectedTableRows::full(candidate.rows)
} else if needs_memory_filter {
// Path 1: Need in-memory filtering (subqueries or complex expressions)
// For memory filter, we need all columns to evaluate the WHERE clause
let column_idx_vec: Vec<usize> = (0..all_columns.len()).collect();
// OPTIMIZATION: Delay scanner creation until we know we need all rows.
// For early termination path, we'll collect rows directly with a limit.
// This avoids materializing all rows upfront when we only need a few.
// Check if WHERE contains correlated subqueries
// Use cached classification to avoid expensive AST traversal
let has_correlated = classification.where_has_subqueries
&& classification.where_has_correlated_subqueries;
// Check if WHERE contains any subqueries (correlated or not)
// Use cached classification to avoid redundant traversal of the expression tree
let has_subqueries = classification.where_has_subqueries;
// SEMI-JOIN OPTIMIZATION: Try to optimize correlated EXISTS subqueries
// This transforms EXISTS (SELECT ... WHERE outer.col = inner.col AND ...)
// into: outer.col IN (SELECT DISTINCT inner_col FROM inner WHERE ...)
// This changes O(outer × inner) to O(inner + outer) - massive performance win!
let (processed_where, has_correlated) = if has_correlated {
if let Some(where_expr) = where_to_use {
// Get outer table names for semi-join detection
let outer_tables = Self::collect_outer_table_names(&stmt.table_expr);
// Extract limit value for optimization decision
// With small LIMIT, index-nested-loop with early termination is faster
let outer_limit = stmt.limit.as_ref().and_then(|limit_expr| {
if let Expression::IntegerLiteral(lit) = limit_expr.as_ref() {
Some(lit.value)
} else {
None
}
});
// ANTI-JOIN OPTIMIZATION: For pure NOT EXISTS without LIMIT, use HashJoinOperator::Anti
// This is faster than InHashSet because:
// 1. Bulk hash table build and probe (no per-row expression evaluation)
// 2. Better cache efficiency
// 3. Direct row iteration without VM overhead
//
// HOWEVER: For queries with LIMIT, we skip anti-join because it materializes
// ALL outer rows before applying LIMIT. The streaming InHashSet path can stop
// early once LIMIT is reached, making it faster for limited result sets.
if let Some(not_exists_info) =
Self::try_extract_not_exists_info(where_expr, &outer_tables)
{
// Check if NOT EXISTS is the ONLY predicate (pure anti-join case)
let is_pure_not_exists = matches!(where_expr, Expression::Prefix(_));
// Only use anti-join for queries without LIMIT (or very large LIMIT)
// For LIMIT queries, the streaming InHashSet path is faster due to early termination
let has_limit = outer_limit.is_some()
&& outer_limit.unwrap() < ANTI_JOIN_LIMIT_THRESHOLD;
let use_anti_join = is_pure_not_exists && !has_limit;
if use_anti_join {
// Materialize outer table rows
let mut outer_rows = table.collect_all_rows(storage_expr.as_deref())?;
// Execute anti-join
let anti_join_result = self.execute_anti_join(
¬_exists_info,
CompactArc::new(outer_rows.drain_rows().collect()),
&all_columns,
ctx,
)?;
// Project and return result
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
anti_join_result,
&all_columns,
Some(&all_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns = CompactArc::new(self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
));
// Apply LIMIT if present
let final_rows = if let Some(limit_expr) = &stmt.limit {
if let Expression::IntegerLiteral(lit) = limit_expr.as_ref() {
let limit = lit.value as usize;
let offset = stmt
.offset
.as_ref()
.and_then(|o| {
if let Expression::IntegerLiteral(lit) = o.as_ref() {
Some(lit.value as usize)
} else {
None
}
})
.unwrap_or(0);
projected_rows
.into_iter()
.skip(offset)
.take(limit)
.collect()
} else {
projected_rows
}
} else {
projected_rows
};
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
final_rows,
);
return Ok((Box::new(result), output_columns, true, None));
}
}
// Try semi-join optimizations for both EXISTS and IN subqueries
// These transform O(outer × inner) to O(inner + outer)
// Avoid cloning upfront - only clone if no optimization succeeds
// 1. Try EXISTS semi-join optimization
let exists_optimized = self
.try_optimize_exists_to_semi_join(
where_expr,
ctx,
&outer_tables,
outer_limit,
)
.ok()
.flatten();
// 2. Try IN semi-join optimization (on EXISTS result or original)
let expr_for_in = exists_optimized.as_ref().unwrap_or(where_expr);
let in_optimized = self
.try_optimize_in_to_semi_join(expr_for_in, ctx, &outer_tables)
.ok()
.flatten();
// Determine final expression without unnecessary clones
let (current_expr, any_optimized) = match (exists_optimized, in_optimized) {
(_, Some(in_opt)) => (in_opt, true),
(Some(exists_opt), None) => (exists_opt, true),
(None, None) => (where_expr.clone(), false), // Clone only when needed
};
// Check if there are still correlated subqueries after optimizations
let still_correlated = Self::has_correlated_subqueries(¤t_expr);
if any_optimized && !still_correlated {
// All correlated subqueries were optimized away
(Some(current_expr), false)
} else if any_optimized {
// Some optimizations applied but still have correlated parts
(Some(current_expr), true)
} else {
// No optimizations applied - keep original for per-row processing
(Some(current_expr), true)
}
} else {
(None, false)
}
} else if let Some(where_expr) = memory_where_to_use {
if has_subqueries {
// Pre-process uncorrelated subqueries once
(Some(self.process_where_subqueries(where_expr, ctx)?), false)
} else {
(Some(where_expr.clone()), false)
}
} else {
(None, false)
};
// A nested EXISTS/IN may be rewritten completely, but the
// remaining predicate can still reference the parent row directly
// (for example `inner.parent_id = outer.id`). Keep the combined
// current+parent binding map whenever this SELECT was entered with
// an outer context; otherwise qualified local columns can be
// resolved against an incomplete scope after the rewrite.
let has_correlated = has_correlated || has_outer_context;
// FAST PATH: InHashSet index optimization (from EXISTS → semi-join transformation)
// If EXISTS was transformed to InHashSet and the column is PK/indexed,
// probe directly instead of scanning all rows
// Skip if there are correlated subqueries in SELECT columns
// Skip if query has aggregation: projection cannot compile aggregate functions
// Use cached classification to avoid AST traversal
if storage_expr.is_none()
&& !has_correlated
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.select_has_correlated_subqueries
{
if let Some(ref where_expr) = processed_where {
if let Some((result, columns)) = self.try_in_hashset_index_optimization(
stmt,
where_expr,
&*table,
&all_columns,
table_alias.as_deref(),
ctx,
)? {
return Ok((result, columns, false, None));
}
}
}
// Check if we can use the PARALLEL PATH:
// For simple WHERE without subqueries, collect all rows first
// then filter in parallel. This is much faster for large tables.
// CRITICAL: Cannot use parallel path when there's outer context because
// the WHERE clause may reference outer columns (e.g., products.id in
// a correlated subquery like WHERE product_id = products.id)
let use_parallel_path =
!has_correlated && !has_outer_context && processed_where.is_some();
if use_parallel_path {
let where_expr = processed_where.as_ref().unwrap();
// EARLY TERMINATION: For LIMIT queries without ORDER BY, GROUP BY,
// aggregation, or window functions, use streaming filter with early
// termination. This is critical for NOT IN performance.
let can_early_terminate = !classification.has_order_by
&& !classification.has_group_by
&& !classification.has_aggregation
&& !classification.has_window_functions;
let early_termination_target = if can_early_terminate {
let offset = stmt
.offset
.as_ref()
.and_then(|offset_expr| {
ExpressionEval::compile(offset_expr, &[])
.ok()
.and_then(|e| e.with_context(ctx).eval_slice(&Row::new()).ok())
.and_then(|v| {
if let Value::Integer(o) = v {
Some(o.max(0) as usize)
} else {
None
}
})
})
.unwrap_or(0);
stmt.limit.as_ref().and_then(|limit_expr| {
ExpressionEval::compile(limit_expr, &[])
.ok()
.and_then(|e| e.with_context(ctx).eval_slice(&Row::new()).ok())
.and_then(|v| {
if let Value::Integer(l) = v {
Some(offset + l.max(0) as usize)
} else {
None
}
})
})
} else {
None
};
// Use early termination path if we have a target
if let Some(target) = early_termination_target {
// OPTIMIZATION: For memory-filter + LIMIT, use iterative fetching
// with early termination. We fetch in batches to avoid loading
// all rows when only a few are needed.
let mut result_rows = RowVec::with_capacity(target.min(1000));
if needs_memory_filter {
if let Some((output_indices, output_columns)) =
self.get_simple_projection_indices(&stmt.columns, &all_columns)
{
if let Some(plan) = self.build_filtered_simple_projection_scan_plan(
where_expr,
&output_indices,
&output_columns,
&all_columns,
) {
// PARTIAL PUSHDOWN + NARROW PROJECTION: storage applies the
// pushable predicate part, while memory filtering and final
// projection operate on a row containing only predicate/output
// columns. This keeps LIMIT early termination without dragging
// unused payload columns through the batch loop.
let mut batch_size = target.max(100);
let mut offset = 0usize;
let mut memory_eval =
ExpressionEval::compile(where_expr, &plan.scan_columns)?
.with_context(ctx);
loop {
let batch = table.collect_rows_with_limit_unordered_projected(
&plan.scan_indices,
storage_expr.as_deref(),
batch_size,
offset,
)?;
if batch.is_empty() {
break;
}
for (row_id, row) in batch {
if memory_eval.eval_bool_checked(&row)? {
result_rows.push((row_id, row));
if result_rows.len() >= target {
break;
}
}
}
if result_rows.len() >= target {
break;
}
offset += batch_size;
batch_size *= 2;
}
let scan_columns_lower: Vec<String> =
plan.scan_columns.iter().map(|c| c.to_lowercase()).collect();
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
result_rows,
&plan.scan_columns,
Some(&scan_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns = CompactArc::new(plan.output_columns);
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
projected_rows,
);
return Ok((Box::new(result), output_columns, true, None));
}
}
if !classification.select_has_scalar_subqueries {
let output_columns = self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
);
if let Some(plan) = self.build_filtered_expression_projection_scan_plan(
where_expr,
&stmt.columns,
&output_columns,
&all_columns,
) {
// Same narrow-row contract for complex SELECT expressions:
// keep only residual predicate columns plus expression
// dependencies, then evaluate the expression projection at
// the API boundary.
let mut batch_size = target.max(100);
let mut offset = 0usize;
let mut memory_eval =
ExpressionEval::compile(where_expr, &plan.scan_columns)?
.with_context(ctx);
loop {
let batch = table.collect_rows_with_limit_unordered_projected(
&plan.scan_indices,
storage_expr.as_deref(),
batch_size,
offset,
)?;
if batch.is_empty() {
break;
}
for (row_id, row) in batch {
if memory_eval.eval_bool_checked(&row)? {
result_rows.push((row_id, row));
if result_rows.len() >= target {
break;
}
}
}
if result_rows.len() >= target {
break;
}
offset += batch_size;
batch_size *= 2;
}
let scan_columns_lower: Vec<String> =
plan.scan_columns.iter().map(|c| c.to_lowercase()).collect();
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
result_rows,
&plan.scan_columns,
Some(&scan_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns = CompactArc::new(plan.output_columns);
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
projected_rows,
);
return Ok((Box::new(result), output_columns, true, None));
}
}
// PARTIAL PUSHDOWN: Storage handles some filtering, memory handles rest.
// Use batched fetching with increasing batch sizes to minimize work.
let mut batch_size = target.max(100); // Start with at least target rows
let mut offset = 0usize;
// Pre-compile filter ONCE outside the loop
let mut memory_eval =
ExpressionEval::compile(where_expr, &all_columns)?.with_context(ctx);
loop {
// Fetch batch from storage with storage filter + limit
// Now returns RowVec directly with row IDs preserved
let batch = table.collect_rows_with_limit(
storage_expr.as_deref(),
batch_size,
offset,
)?;
if batch.is_empty() {
break; // No more rows from storage
}
// Apply full WHERE filter (includes both pushed and non-pushed parts)
for (row_id, row) in batch {
if memory_eval.eval_bool_checked(&row)? {
result_rows.push((row_id, row));
if result_rows.len() >= target {
break;
}
}
}
if result_rows.len() >= target {
break; // Got enough rows
}
// Need more rows - increase batch size and offset
offset += batch_size;
batch_size *= 2; // Exponential backoff
}
} else {
// NO MEMORY FILTER NEEDED: Full filter pushed to storage.
// Use storage-level limit directly. Returns RowVec directly.
result_rows =
table.collect_rows_with_limit(storage_expr.as_deref(), target, 0)?;
}
// Project rows and return early - LIMIT/OFFSET already applied
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
result_rows,
&all_columns,
Some(&all_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns = CompactArc::new(self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
));
let result = ExecutorResult::with_arc_columns(
CompactArc::clone(&output_columns),
projected_rows,
);
return Ok((Box::new(result), output_columns, true, None)); // true = LIMIT applied
}
if can_use_ordered_projection_scan_plan {
let output_columns = self.get_output_column_names(
&stmt.columns,
&all_columns,
table_alias.as_deref(),
);
if let Some(plan) = self.build_filtered_ordered_distinct_projection_scan_plan(
where_expr,
stmt,
&output_columns,
&all_columns,
) {
// ORDER BY/DISTINCT ON may need columns that are not part
// of the public SELECT output. Keep those dependencies in
// the scanner row, but avoid dragging unrelated payload
// columns through the sort/distinct materialization step.
let scanner = access_scan::open_exact_projection_scan(
table.as_ref(),
&plan.scan_indices,
storage_expr.as_deref(),
ctx,
)?;
let all_rows = self.collect_scanner_rows(scanner, ctx)?;
let filtered = parallel::parallel_filter(
all_rows,
where_expr,
&plan.scan_columns,
&self.function_registry,
¶llel_config,
ctx,
)?;
CollectedTableRows::projected(filtered, plan.scan_columns)
} else {
// Normal path: collect all rows first (for ORDER BY, aggregation, etc.)
// Now we create the scanner since we need all rows
let scanner = access_scan::open_scan(
table.as_ref(),
&column_idx_vec,
storage_expr.as_deref(),
ctx,
)?;
let all_rows = self.collect_scanner_rows(scanner, ctx)?;
// Apply parallel filtering if we have enough rows
// CRITICAL: Propagate errors with ? instead of silently swallowing them
let filtered = parallel::parallel_filter(
all_rows,
where_expr,
&all_columns,
&self.function_registry,
¶llel_config,
ctx,
)?;
CollectedTableRows::full(filtered)
}
} else {
// Normal path: collect all rows first (for ORDER BY, aggregation, etc.)
// Now we create the scanner since we need all rows
let scanner = access_scan::open_scan(
table.as_ref(),
&column_idx_vec,
storage_expr.as_deref(),
ctx,
)?;
let all_rows = self.collect_scanner_rows(scanner, ctx)?;
// Apply parallel filtering if we have enough rows
// CRITICAL: Propagate errors with ? instead of silently swallowing them
let filtered = parallel::parallel_filter(
all_rows,
where_expr,
&all_columns,
&self.function_registry,
¶llel_config,
ctx,
)?;
CollectedTableRows::full(filtered)
}
} else {
// SEQUENTIAL PATH: For correlated subqueries or complex cases
// Create evaluator once and reuse for all rows
let mut eval = if processed_where.is_some() {
let mut e = CompiledEvaluator::new(&self.function_registry);
e = e.with_context(ctx);
e.init_columns(&all_columns);
Some(e)
} else {
None
};
// OPTIMIZATION: Pre-compute column name mappings outside the loop
// This avoids repeated to_lowercase() and format!() calls per row
let column_keys: Option<Vec<ColumnKeyMapping>> = if has_correlated {
Some(ColumnKeyMapping::build_mappings(
&all_columns,
table_alias.as_deref(),
))
} else {
None
};
// OPTIMIZATION: Pre-allocate outer_row_map with capacity and reuse
let base_capacity = all_columns.len() * 2 + ctx.outer_row().map_or(0, |m| m.len());
let mut outer_row_map: FxHashMap<CompactArc<str>, Value> = FxHashMap::default();
outer_row_map.reserve(base_capacity);
// OPTIMIZATION: Wrap all_columns in Arc once, reuse for all rows (only if needed)
let all_columns_arc: Option<CompactArc<Vec<String>>> = if has_correlated {
Some(CompactArc::new(all_columns.clone()))
} else {
None
};
// LIMIT EARLY TERMINATION: For correlated subqueries without ORDER BY,
// we can stop as soon as we have enough matching rows.
// This turns O(outer_size) EXISTS evaluations into O(LIMIT) evaluations.
let early_termination_target: Option<usize> =
if has_correlated && stmt.order_by.is_empty() {
let offset = stmt
.offset
.as_ref()
.and_then(|offset_expr| {
ExpressionEval::compile(offset_expr, &[])
.ok()
.and_then(|e| e.with_context(ctx).eval_slice(&Row::new()).ok())
.and_then(|v| {
if let Value::Integer(o) = v {
Some(o.max(0) as usize)
} else {
None
}
})
})
.unwrap_or(0);
stmt.limit.as_ref().and_then(|limit_expr| {
ExpressionEval::compile(limit_expr, &[])
.ok()
.and_then(|e| e.with_context(ctx).eval_slice(&Row::new()).ok())
.and_then(|v| {
if let Value::Integer(l) = v {
Some(offset + l.max(0) as usize)
} else {
None
}
})
})
} else {
None
};
// Create scanner for the correlated subquery path
// For correlated subqueries, we can't push down the WHERE clause to storage
// because it depends on outer row values that change per row
let mut scanner = access_scan::open_scan(
table.as_ref(),
&column_idx_vec,
storage_expr.as_deref(),
ctx,
)?;
// Pre-allocate to reduce reallocations - 64 avoids first 6 grow operations
let mut rows: RowVec = RowVec::with_capacity(64);
let mut row_count = 0u64;
while scanner.next() {
// Check for cancellation every 100 rows (more frequent for slow queries)
row_count += 1;
if row_count.is_multiple_of(100) {
ctx.check_cancelled()?;
}
let row = scanner.take_row();
// Apply in-memory WHERE filter if needed (for complex expressions or subqueries)
if let (Some(ref where_expr), Some(ref mut evaluator)) =
(&processed_where, &mut eval)
{
evaluator.set_row_array(&row);
// For correlated subqueries, process per-row with outer context
if has_correlated {
// OPTIMIZATION: Clear and reuse outer_row_map instead of creating new
outer_row_map.clear();
// OPTIMIZATION: Copy parent outer context if exists (for nested correlated subqueries)
// Parent context doesn't change per row, but we need to clone since
// std::mem::take moves the map out each iteration
if let Some(parent_outer_row) = ctx.outer_row() {
outer_row_map.extend(
parent_outer_row.iter().map(|(k, v)| (k.clone(), v.clone())),
);
}
// Use pre-computed column mappings
if let Some(ref keys) = column_keys {
for mapping in keys {
if let Some(value) = row.get(mapping.index) {
// OPTIMIZATION: Clone value once and reuse for all key insertions
// Previously we cloned 2-3 times per column
let cloned_value = value.clone();
// Insert with unqualified part first (if column had a dot)
if let Some(ref upart) = mapping.unqualified_part {
outer_row_map
.insert(upart.clone(), cloned_value.clone());
}
// Insert with qualified name if available
if let Some(ref qname) = mapping.qualified_name {
outer_row_map
.insert(qname.clone(), cloned_value.clone());
}
// Insert with lowercase column name (move, no clone)
outer_row_map
.insert(mapping.col_lower.clone(), cloned_value);
}
}
}
// Create context with outer row (cheap due to Arc)
// SAFETY: all_columns_arc is always Some when has_correlated is true
let mut correlated_ctx = ctx.with_outer_row(
std::mem::take(&mut outer_row_map),
all_columns_arc.clone().unwrap(), // Arc clone = cheap
);
// FAST PATH: If WHERE is just EXISTS or NOT EXISTS, evaluate directly
// without creating AST nodes. This saves ~2-3μs per row.
let (result, used_evaluator) = if let Expression::Exists(exists) =
where_expr
{
let r = self
.execute_exists_subquery(&exists.subquery, &correlated_ctx)?;
(r, false)
} else if let Expression::Prefix(prefix) = where_expr {
if prefix.operator.eq_ignore_ascii_case("NOT") {
if let Expression::Exists(exists) = prefix.right.as_ref() {
let r = !self.execute_exists_subquery(
&exists.subquery,
&correlated_ctx,
)?;
(r, false)
} else {
// Not a simple NOT EXISTS, use standard path
let processed = self.process_correlated_where(
where_expr,
&correlated_ctx,
)?;
ctx.check_cancelled()?;
evaluator.set_outer_row_owned(
correlated_ctx.take_outer_row().unwrap_or_default(),
);
evaluator.set_row_array(&row);
let r = evaluator.evaluate_bool(&processed)?;
(r, true)
}
} else {
// Not EXISTS/NOT EXISTS, use standard path
let processed =
self.process_correlated_where(where_expr, &correlated_ctx)?;
ctx.check_cancelled()?;
evaluator.set_outer_row_owned(
correlated_ctx.take_outer_row().unwrap_or_default(),
);
evaluator.set_row_array(&row);
let r = evaluator.evaluate_bool(&processed)?;
(r, true)
}
} else {
// Complex WHERE expression, use standard path
let processed =
self.process_correlated_where(where_expr, &correlated_ctx)?;
// Check for cancellation after processing each correlated subquery
// This is critical for slow correlated subqueries
ctx.check_cancelled()?;
// OPTIMIZATION: Take ownership instead of cloning - avoids HashMap clone
evaluator.set_outer_row_owned(
correlated_ctx.take_outer_row().unwrap_or_default(),
);
evaluator.set_row_array(&row);
(evaluator.evaluate_bool(&processed)?, true)
};
// Take back the map for reuse
if used_evaluator {
outer_row_map = evaluator.take_outer_row();
} else {
outer_row_map = correlated_ctx.take_outer_row().unwrap_or_default();
}
if !result {
continue;
}
} else {
// Standard evaluation for non-correlated subqueries
if !evaluator.evaluate_bool(where_expr)? {
continue;
}
}
}
rows.push((row_count as i64, row));
// LIMIT EARLY TERMINATION: Stop if we have enough rows
if let Some(target) = early_termination_target {
if rows.len() >= target {
break;
}
}
}
CollectedTableRows::full(rows)
}
} else if storage_expr.is_some() {
// Path 2: WHERE clause with pushdown - use scanner for index optimization
if let Some(plan) = &ordered_projection_scan_plan {
let scanner = access_scan::open_exact_projection_scan(
table.as_ref(),
&plan.scan_indices,
storage_expr.as_deref(),
ctx,
)?;
let rows = self.collect_scanner_rows(scanner, ctx)?;
CollectedTableRows::projected(rows, plan.scan_columns.clone())
} else {
// Fallback: downstream projection/order evaluation still uses the full source schema.
let column_idx_vec: Vec<usize> = (0..all_columns.len()).collect();
let scanner = access_scan::open_scan(
table.as_ref(),
&column_idx_vec,
storage_expr.as_deref(),
ctx,
)?;
let rows = self.collect_scanner_rows(scanner, ctx)?;
CollectedTableRows::full(rows)
}
} else {
// Path 3: Full scan without WHERE - use collect_all_rows
// Projection is handled later by the executor which is more efficient
//
// OPTIMIZATION: For window functions, check if we can use index-based fetching:
// 1. PARTITION BY on indexed column -> fetch rows grouped by partition
// 2. ORDER BY on indexed column -> fetch rows in sorted order
let has_window = classification.has_window_functions;
let has_agg = classification.has_aggregation;
if let Some(plan) = &ordered_projection_scan_plan {
let scanner = access_scan::open_exact_projection_scan(
table.as_ref(),
&plan.scan_indices,
None,
ctx,
)?;
let rows = self.collect_scanner_rows(scanner, ctx)?;
CollectedTableRows::projected(rows, plan.scan_columns.clone())
} else if has_window && !has_agg {
// First try PARTITION BY optimization (bigger speedup, avoids O(n) hashing)
if let Some(partition_col) = Self::extract_window_partition_info(stmt) {
let col_lower = partition_col.to_lowercase();
let schema = table.schema();
let pk_columns = schema.primary_key_columns();
let is_pk = pk_columns.len() == 1 && pk_columns[0].name_lower == col_lower;
let has_index = is_pk || table.get_index_on_column(&partition_col).is_some();
if has_index {
// OPTIMIZATION: If we have LIMIT without ORDER BY, use lazy partition fetching
// This avoids fetching all partitions when only a few are needed
// NOTE: Cannot use this optimization if there's a top-level ORDER BY
// because we need all rows to sort before applying LIMIT
let has_order_by = !stmt.order_by.is_empty();
if !has_order_by {
if let Some(limit_expr) = &stmt.limit {
if let Expression::IntegerLiteral(lit) = limit_expr.as_ref() {
if lit.value > 0 {
let limit_val = lit.value as usize;
// Use lazy partition fetching - returns early!
let result = self
.execute_select_with_window_functions_lazy_partition(
stmt,
ctx,
table.as_ref(),
&all_columns,
&partition_col,
limit_val,
);
if let Ok(query_result) = result {
let columns =
CompactArc::new(query_result.columns().to_vec());
return Ok((query_result, columns, false, None));
}
// Fall through to regular path if optimization fails
}
}
}
}
// Regular path: Fetch rows grouped by partition (no hash grouping needed)
if let Some(grouped_data) =
table.collect_rows_grouped_by_partition(&partition_col)
{
// Flatten rows and build partition map
let mut all_rows = RowVec::new();
let mut partition_map: rustc_hash::FxHashMap<
smallvec::SmallVec<[Value; 4]>,
Vec<usize>,
> = rustc_hash::FxHashMap::default();
for (partition_value, partition_rows) in grouped_data {
let start_idx = all_rows.len();
let partition_size = partition_rows.len();
// Extend RowVec with (row_id, Row) tuples
for item in partition_rows {
all_rows.push(item);
}
// Build partition key and indices
let key: smallvec::SmallVec<[Value; 4]> =
smallvec::smallvec![partition_value];
let indices: Vec<usize> =
(start_idx..start_idx + partition_size).collect();
partition_map.insert(key, indices);
}
CollectedTableRows::full_pregrouped(
all_rows,
WindowPreGroupedState {
partition_map,
partition_column: col_lower.clone(),
},
)
} else {
CollectedTableRows::full(table.collect_all_rows(None)?)
}
} else {
CollectedTableRows::full(table.collect_all_rows(None)?)
}
}
// Then try ORDER BY optimization (avoids sorting)
else if let Some((col_name, ascending)) = Self::extract_window_order_info(stmt) {
let col_lower = col_name.to_lowercase();
let schema = table.schema();
let pk_columns = schema.primary_key_columns();
let is_pk = pk_columns.len() == 1 && pk_columns[0].name_lower == col_lower;
// The ordered secondary-index API uses structural Value
// ordering (NULL first), which is not a complete SQL NULL
// placement contract. It is nevertheless exact when the
// current index proves that it contains no NULL key. A
// single-column primary key is non-NULL by definition.
let has_index = is_pk
|| table.get_index_on_column(&col_name).is_some_and(|index| {
index.get_all_values().iter().all(|value| !value.is_null())
});
if has_index {
// OPTIMIZATION: If we have LIMIT without top-level ORDER BY,
// push the limit down to fetch only needed rows.
// This is safe ONLY for window functions that don't depend on total row count.
// Safe: ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, FIRST_VALUE, LAST_VALUE
// Unsafe: NTILE, PERCENT_RANK, CUME_DIST (need total count)
let has_order_by = !stmt.order_by.is_empty();
let is_window_safe = Self::is_window_safe_for_limit_pushdown(stmt);
let fetch_limit = if !has_order_by && is_window_safe {
if let Some(limit_expr) = &stmt.limit {
if let Expression::IntegerLiteral(lit) = limit_expr.as_ref() {
if lit.value > 0 {
lit.value as usize
} else {
usize::MAX
}
} else {
usize::MAX
}
} else {
usize::MAX
}
} else {
usize::MAX
};
// Fetch rows in sorted order from the index (no re-fetch needed)
if let Some(sorted_rows) = table.collect_rows_ordered_by_index(
&col_name,
ascending,
fetch_limit,
0,
) {
CollectedTableRows::full_presorted(
sorted_rows,
WindowPreSortedState {
column: col_lower,
ascending,
},
)
} else {
CollectedTableRows::full(table.collect_all_rows(None)?)
}
} else {
CollectedTableRows::full(table.collect_all_rows(None)?)
}
} else {
CollectedTableRows::full(table.collect_all_rows(None)?)
}
} else {
CollectedTableRows::full(table.collect_all_rows(None)?)
}
};
// Destructure: rows and optional window optimization states
let CollectedTableRows {
rows,
window_presorted_state,
window_pregrouped_state,
source_columns: row_source_columns,
} = rows_result;
let row_source_columns = row_source_columns.unwrap_or_else(|| all_columns.clone());
let row_source_columns_lower: Vec<String> = row_source_columns
.iter()
.map(|column| column.to_lowercase())
.collect();
let row_source_is_full = row_source_columns.len() == all_columns.len()
&& row_source_columns
.iter()
.zip(all_columns.iter())
.all(|(source, full)| source.eq_ignore_ascii_case(full));
// Record cardinality feedback for future estimate improvements
// This helps the optimizer learn from actual query execution
if let Some(where_expr) = where_to_use {
let actual_rows = rows.len() as u64;
// Only record feedback if we have a meaningful predicate and enough rows
if actual_rows >= 10 || rows.is_empty() {
if let Some(estimated_rows) = self
.get_query_planner()
.estimate_scan_rows(table_name, Some(where_expr))
{
self.get_query_planner().record_feedback(
table_name,
where_expr,
None, // Column-specific feedback not yet implemented
estimated_rows,
actual_rows,
);
}
}
}
// Handle the combination of window functions and aggregation
// Order: Aggregation first (GROUP BY), then window functions
let has_window = classification.has_window_functions;
let has_agg = classification.has_aggregation;
if has_agg && has_window {
// Both aggregation and window functions:
// 1. First apply GROUP BY aggregation
// 2. Then apply window functions on the aggregated result
let agg_result = self.execute_aggregation_for_window(stmt, ctx, &rows, &all_columns)?;
let agg_columns = agg_result.0.clone();
let agg_rows = agg_result.1;
// Apply window functions on aggregated rows
let result =
self.execute_select_with_window_functions(stmt, ctx, &agg_rows, &agg_columns)?;
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
// Check if we need window functions only (no aggregation)
if has_window {
// Use optimized paths if rows were pre-fetched with index optimization
let result = if let Some(pregrouped) = window_pregrouped_state {
// PARTITION BY optimization: rows are already grouped by partition
self.execute_select_with_window_functions_pregrouped(
stmt,
ctx,
&rows,
&all_columns,
pregrouped,
)?
} else if window_presorted_state.is_some() {
// ORDER BY optimization: rows are already sorted
self.execute_select_with_window_functions_presorted(
stmt,
ctx,
&rows,
&all_columns,
window_presorted_state,
)?
} else {
// Default path: no optimization
self.execute_select_with_window_functions(stmt, ctx, &rows, &all_columns)?
};
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
// Check if we need aggregation only (no window functions)
if has_agg {
let result = self.execute_select_with_aggregation(stmt, ctx, rows, &all_columns)?;
let columns = CompactArc::new(result.columns().to_vec());
return Ok((result, columns, false, None));
}
// Project rows according to SELECT expressions
// Check if deferred projection is applicable (ORDER BY + LIMIT with simple columns)
// This reduces allocations from O(matched_rows) to O(limit)
let deferred_projection_info = self.get_deferred_projection_info(
stmt,
&row_source_columns_lower,
&row_source_columns,
classification,
);
let (projected_rows, output_columns, deferred_proj) = if order_by_needs_extra_columns {
// When ORDER BY references columns not in SELECT, we need to:
// 1. Include those columns in the output (appended at end)
// 2. Sort will happen in execute_select
// 3. Extra columns will be projected out after sorting
let (projected_rows, extra_columns) = self.project_rows_with_order_by(
&stmt.columns,
&stmt.order_by,
&stmt.distinct_on,
rows,
&row_source_columns,
ctx,
)?;
// Get base column names
let mut output_columns =
self.get_output_column_names(&stmt.columns, &all_columns, table_alias.as_deref());
// The projection helper is the single source of truth for both the
// hidden value order and the corresponding column names.
output_columns.extend(extra_columns);
(projected_rows, output_columns, None)
} else if let Some((col_indices, output_names)) = deferred_projection_info {
// DEFERRED PROJECTION: Skip projection now, do it after ORDER BY + LIMIT
// Return all source columns; projection will be applied after TopNResult
(
rows,
row_source_columns.to_vec(),
Some((col_indices, output_names)),
)
} else {
// Standard projection
let projected_rows = self.project_rows_with_alias(
&stmt.columns,
rows,
&row_source_columns,
Some(&row_source_columns_lower),
ctx,
table_alias.as_deref(),
)?;
let output_columns =
self.get_output_column_names(&stmt.columns, &all_columns, table_alias.as_deref());
(projected_rows, output_columns, None)
};
// SEMANTIC CACHE: Insert result for eligible queries
// For SELECT * queries, cache the raw rows before returning
//
// Note: The cache stores Vec<Row>, so we extract rows from RowVec for caching.
// The result keeps the original RowVec.
// Skip caching when deferred projection is used (rows are not projected yet)
if cache_eligible && deferred_proj.is_none() && row_source_is_full {
if let Some(where_expr) = where_to_use {
// Clone rows for cache (cache needs Vec<Row>)
let rows_for_cache: Vec<Row> = projected_rows.rows().cloned().collect();
self.semantic_cache.insert_if_generation(
semantic_cache_generation.expect("eligible cache query has generation"),
table_name,
all_columns.clone(),
rows_for_cache,
Some(where_expr.clone()),
);
}
}
let output_columns = CompactArc::new(output_columns);
let result =
ExecutorResult::with_arc_columns(CompactArc::clone(&output_columns), projected_rows);
Ok((Box::new(result), output_columns, false, deferred_proj))
}
}