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
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
// Copyright 2026 RadixDB Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Table trait for database tables
//!
use rustc_hash::FxHashMap;
use std::fmt;
use std::sync::Arc;
use parking_lot::RwLock;
use crate::expression::Expression;
use crate::traits::index::IndexKeyRange;
use crate::traits::{Index, QueryResult, Scanner};
use radixdb_core::{
CompactArc, DataType, Error, IndexType, Operator, Result, Row, RowVec, Schema, Value,
};
/// Operation requested from a storage-level aggregate pushdown.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AggregateOp {
Count,
CountStar,
Sum,
Min,
Max,
Avg,
}
/// Stable group identity shared by hot and cold aggregate implementations.
#[derive(Clone, Debug)]
pub enum GroupKey {
Single(CompactArc<Value>),
Multi(Vec<CompactArc<Value>>),
}
impl PartialEq for GroupKey {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Single(left), Self::Single(right)) => **left == **right,
(Self::Multi(left), Self::Multi(right)) => {
left.len() == right.len() && left.iter().zip(right.iter()).all(|(a, b)| **a == **b)
}
_ => false,
}
}
}
impl Eq for GroupKey {}
impl std::hash::Hash for GroupKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
Self::Single(value) => (**value).hash(state),
Self::Multi(values) => {
for value in values {
(**value).hash(state);
}
}
}
}
}
/// Result of one storage-level grouped aggregation.
#[derive(Debug, Clone)]
pub struct GroupedAggregateResult {
pub group_values: Vec<Value>,
pub aggregate_values: Vec<Value>,
}
/// Resolve the physical index key for one logical row.
///
/// Keeping this in the neutral table contract prevents partial-index
/// evaluation from diverging between hot MVCC and immutable segments.
#[doc(hidden)]
pub fn index_values_for_row(index: &dyn Index, row: &Row) -> Result<Option<Vec<Value>>> {
let column_ids = index.column_ids();
if column_ids.is_empty() {
return Ok(None);
}
if let Some(predicate) = index.partial_predicate() {
if !predicate.matches(row)? {
return Ok(None);
}
}
Ok(Some(
column_ids
.iter()
.map(|&column_id| {
row.get(column_id as usize)
.cloned()
.unwrap_or(Value::Null(DataType::Null))
})
.collect(),
))
}
/// Exact intermediate state for deferred SUM/AVG pushdown.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DeferredSum {
integer_sum: i128,
float_sum: f64,
integer_count: usize,
float_count: usize,
overflowed: bool,
}
impl DeferredSum {
pub const fn new() -> Self {
Self {
integer_sum: 0,
float_sum: 0.0,
integer_count: 0,
float_count: 0,
overflowed: false,
}
}
pub fn add_integer(&mut self, value: i128, count: usize) {
match (
self.integer_sum.checked_add(value),
self.integer_count.checked_add(count),
) {
(Some(sum), Some(total)) => {
self.integer_sum = sum;
self.integer_count = total;
}
_ => self.overflowed = true,
}
}
pub fn add_float(&mut self, value: f64, count: usize) {
self.float_sum += value;
self.float_count = match self.float_count.checked_add(count) {
Some(total) => total,
None => {
self.overflowed = true;
self.float_count
}
};
}
pub fn add_value(&mut self, value: &Value) {
match value {
Value::Integer(value) => self.add_integer(*value as i128, 1),
Value::Float(value) => self.add_float(*value, 1),
_ => {}
}
}
pub fn merge(&mut self, other: Self) {
self.overflowed |= other.overflowed;
self.add_integer(other.integer_sum, other.integer_count);
self.add_float(other.float_sum, other.float_count);
}
pub fn count(&self) -> usize {
self.integer_count.saturating_add(self.float_count)
}
pub fn as_f64(&self) -> f64 {
self.integer_sum as f64 + self.float_sum
}
pub fn into_value(self) -> Result<Value> {
if self.overflowed {
return Err(Error::invalid_argument("deferred SUM accumulator overflow"));
}
if self.float_count == 0 {
if let Ok(value) = i64::try_from(self.integer_sum) {
return Ok(Value::Integer(value));
}
let digits = self.integer_sum.to_string().trim_start_matches('-').len();
let precision = u8::try_from(digits)
.ok()
.filter(|precision| *precision <= 38)
.ok_or_else(|| Error::invalid_argument("deferred SUM exceeds DECIMAL(38)"))?;
return Value::try_decimal(self.integer_sum, precision, 0);
}
Ok(Value::Float(self.as_f64()))
}
}
impl Default for DeferredSum {
fn default() -> Self {
Self::new()
}
}
/// Describes the access method that will be used for a table scan
///
/// This is used by EXPLAIN to show users how their queries will be executed.
#[derive(Debug, Clone)]
pub enum ScanPlan {
/// Sequential scan - reads all rows and applies filter in memory
SeqScan {
table: String,
filter: Option<String>,
},
/// Parallel sequential scan - reads all rows and filters in parallel across workers
ParallelSeqScan {
table: String,
filter: Option<String>,
workers: usize,
},
/// Primary key lookup - O(1) direct access by primary key
PkLookup {
table: String,
pk_column: String,
pk_value: String,
},
/// Index scan - uses an index to find matching rows
IndexScan {
table: String,
index_name: String,
column: String,
condition: String,
/// Non-indexed predicates applied as in-memory filter after index lookup
filter: Option<String>,
},
/// Multi-index scan - uses multiple indexes with AND/OR operations
MultiIndexScan {
table: String,
indexes: Vec<(String, String, String)>, // (index_name, column, condition)
operation: String, // "AND" or "OR"
/// Non-indexed predicates applied as in-memory filter after index lookup
filter: Option<String>,
},
/// Union/intersection over persisted artifact-backed postings plus hot MVCC indexes.
SegmentedMultiIndexScan {
table: String,
indexes: Vec<(String, String, String)>,
operation: String,
filter: Option<String>,
cold_segments: usize,
cold_rows_hint: usize,
hot_rows_hint: usize,
},
/// Composite index scan - uses a multi-column index
CompositeIndexScan {
table: String,
index_name: String,
columns: Vec<String>,
conditions: Vec<String>,
/// Non-indexed predicates applied as in-memory filter after index lookup
filter: Option<String>,
},
/// Exact composite equality lookup over persisted artifact-backed postings plus the hot
/// MVCC index. This is separate from `CompositeIndexScan` so EXPLAIN does
/// not describe a cold source as a hot-only index.
SegmentedCompositeIndexScan {
table: String,
index_name: String,
columns: Vec<String>,
conditions: Vec<String>,
filter: Option<String>,
cold_segments: usize,
cold_rows_hint: usize,
hot_rows_hint: usize,
ordered: bool,
/// True when the physical declaration has multiple columns, even if
/// this lookup uses only one persisted leading prefix column.
composite: bool,
},
/// HNSW approximate nearest neighbor search
VectorSearch {
table: String,
index_name: String,
vector_column: String,
metric: String,
k: usize,
ef_search: usize,
filter: Option<String>,
},
/// Brute-force parallel vector distance scan
VectorBruteForce {
table: String,
vector_column: String,
metric: String,
k: usize,
filter: Option<String>,
},
/// Segment-backed table scan - merges immutable cold segments with hot rows.
///
/// This is deliberately separate from SeqScan/IndexScan: cold rows do not
/// participate in the hot MVCC secondary indexes. Cold data is read from
/// descriptor-backed artifact-backed row-group blocks.
SegmentedScan {
table: String,
filter: Option<String>,
cold_segments: usize,
cold_rows_hint: usize,
cold_row_groups_hint: usize,
cold_selected_segments: usize,
cold_selected_rows_hint: usize,
cold_selected_row_groups_hint: usize,
cold_metadata_pruned_segments: usize,
cold_metadata_pruned_rows_hint: usize,
hot_rows_hint: usize,
},
}
impl ScanPlan {
/// Stable, machine-readable access path identifier for EXPLAIN/debug output.
///
/// Human-readable `Display` output is intentionally kept stable for users and
/// existing tests. This identifier is the contract benchmark reports and
/// planner assertions should use.
pub fn access_path_id(&self) -> &'static str {
match self {
ScanPlan::SeqScan { .. } => "scan.seq",
ScanPlan::ParallelSeqScan { .. } => "scan.parallel_seq",
ScanPlan::PkLookup { .. } => "scan.pk",
ScanPlan::IndexScan { .. } => "scan.index",
ScanPlan::MultiIndexScan { .. } | ScanPlan::SegmentedMultiIndexScan { .. } => {
"scan.multi_index"
}
ScanPlan::CompositeIndexScan { .. } => "scan.composite_index",
ScanPlan::SegmentedCompositeIndexScan { composite, .. } if !composite => "scan.index",
ScanPlan::SegmentedCompositeIndexScan { .. } => "scan.composite_index",
ScanPlan::VectorSearch { .. } => "scan.vector_hnsw",
ScanPlan::VectorBruteForce { .. } => "scan.vector_bruteforce",
ScanPlan::SegmentedScan { hot_rows_hint, .. } => {
if *hot_rows_hint == 0 {
"scan.cold_artifact"
} else {
"scan.mixed_cold_artifact_hot"
}
}
}
}
/// Stable EXPLAIN lines emitted below the human-readable scan node.
pub fn stable_explain_lines(&self) -> Vec<String> {
let mut lines = vec![format!("Access Path: {}", self.access_path_id())];
// Stage 8 will introduce real RAM accelerator lifecycle. Until then,
// make the absence explicit so EXPLAIN/debug output never implies an
// invisible accelerator path.
lines.push("RAM Accelerator: none".to_string());
match self {
ScanPlan::SeqScan { filter, .. } => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.seq_scan".to_string());
if filter.is_some() {
lines.push("Access Filter: executor_or_storage_residual".to_string());
lines.push("Partial Index Eligibility: no_proven_partial_index".to_string());
}
}
ScanPlan::ParallelSeqScan { filter, .. } => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.parallel_seq_scan".to_string());
if filter.is_some() {
lines.push("Access Filter: executor_or_storage_residual".to_string());
lines.push("Partial Index Eligibility: no_proven_partial_index".to_string());
}
}
ScanPlan::PkLookup { pk_column, .. } => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.primary_key".to_string());
lines.push(format!("Access Key: primary_key({})", pk_column));
}
ScanPlan::IndexScan {
index_name,
column,
filter,
..
} => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.secondary_index".to_string());
lines.push(format!("Access Key: index({}.{})", index_name, column));
if filter.is_some() {
lines.push("Access Filter: post_index_residual".to_string());
}
}
ScanPlan::MultiIndexScan {
indexes,
operation,
filter,
..
} => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.multi_index".to_string());
lines.push(format!(
"Access Key: multi_index({}; {})",
operation,
indexes
.iter()
.map(|(idx, col, _)| format!("{}.{}", idx, col))
.collect::<Vec<_>>()
.join(", ")
));
if filter.is_some() {
lines.push("Access Filter: post_index_residual".to_string());
}
}
ScanPlan::SegmentedMultiIndexScan {
indexes,
operation,
filter,
cold_segments,
cold_rows_hint,
hot_rows_hint,
..
} => {
lines.push("Access Source: cold(artifact_index_postings)+hot".to_string());
lines.push("Cold Access Path: volume.multi_index_union".to_string());
lines.push("Hot Access Path: version_store.multi_index".to_string());
lines.push(format!(
"Access Key: multi_index({}; {})",
operation,
indexes
.iter()
.map(|(idx, col, _)| format!("{}.{}", idx, col))
.collect::<Vec<_>>()
.join(", ")
));
lines.push(format!("Cold Segments: {}", cold_segments));
lines.push(format!("Cold Rows Hint: {}", cold_rows_hint));
lines.push(format!("Hot Rows Hint: {}", hot_rows_hint));
if filter.is_some() {
lines.push("Access Filter: post_index_residual".to_string());
}
}
ScanPlan::CompositeIndexScan {
index_name,
columns,
filter,
..
} => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.composite_index".to_string());
lines.push(format!(
"Access Key: composite_index({}.({}))",
index_name,
columns.join(", ")
));
if filter.is_some() {
lines.push("Access Filter: post_index_residual".to_string());
}
}
ScanPlan::SegmentedCompositeIndexScan {
index_name,
columns,
cold_segments,
cold_rows_hint,
hot_rows_hint,
filter,
ordered,
composite,
..
} => {
let single_column = !composite;
lines.push(if *ordered {
"Access Source: cold(artifact_ordered_postings)+hot".to_string()
} else {
"Access Source: cold(artifact_exact_postings)+hot".to_string()
});
lines.push(if *ordered && single_column {
"Cold Access Path: volume.ordered_index".to_string()
} else if *ordered {
"Cold Access Path: volume.composite_ordered_index".to_string()
} else if single_column {
"Cold Access Path: volume.exact_index".to_string()
} else {
"Cold Access Path: volume.composite_exact_index".to_string()
});
lines.push(if single_column {
"Hot Access Path: version_store.secondary_index".to_string()
} else {
"Hot Access Path: version_store.composite_index".to_string()
});
lines.push(if single_column {
format!("Access Key: index({}.{})", index_name, columns[0])
} else {
format!(
"Access Key: composite_index({}.({}))",
index_name,
columns.join(", ")
)
});
lines.push(format!("Cold Segments: {}", cold_segments));
lines.push(format!("Cold Rows Hint: {}", cold_rows_hint));
lines.push(format!("Hot Rows Hint: {}", hot_rows_hint));
if filter.is_some() {
lines.push("Access Filter: post_index_residual".to_string());
}
}
ScanPlan::VectorSearch {
index_name,
vector_column,
metric,
..
} => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.vector_hnsw".to_string());
lines.push(format!(
"Access Key: hnsw({}.{}, metric={})",
index_name, vector_column, metric
));
}
ScanPlan::VectorBruteForce {
vector_column,
metric,
..
} => {
lines.push("Access Source: hot_mvcc".to_string());
lines.push("Hot Access Path: version_store.vector_bruteforce".to_string());
lines.push(format!(
"Access Key: vector_bruteforce({}, metric={})",
vector_column, metric
));
}
ScanPlan::SegmentedScan {
filter,
cold_segments,
cold_rows_hint,
cold_row_groups_hint,
cold_selected_segments,
cold_selected_rows_hint,
cold_selected_row_groups_hint,
cold_metadata_pruned_segments,
cold_metadata_pruned_rows_hint,
hot_rows_hint,
..
} => {
lines.push("Access Source: cold(artifact_blocks)+hot".to_string());
lines.push("Hot Access Path: version_store.snapshot_scan".to_string());
lines.push("Cold Metadata Path: zone_map+bloom+row_group_metadata".to_string());
lines.push(format!("Cold Segments: {}", cold_segments));
lines.push(format!("Cold Rows Hint: {}", cold_rows_hint));
lines.push(format!("Cold Row Groups Hint: {}", cold_row_groups_hint));
lines.push(format!(
"Cold Metadata Selected Segments: {}",
cold_selected_segments
));
lines.push(format!(
"Cold Metadata Selected Rows Hint: {}",
cold_selected_rows_hint
));
lines.push(format!(
"Cold Metadata Selected Row Groups Hint: {}",
cold_selected_row_groups_hint
));
lines.push(format!(
"Cold Metadata Pruned Segments: {}",
cold_metadata_pruned_segments
));
lines.push(format!(
"Cold Metadata Pruned Rows Hint: {}",
cold_metadata_pruned_rows_hint
));
lines.push(format!("Hot Rows Hint: {}", hot_rows_hint));
if filter.is_some() {
lines.push(
"Access Filter: cold_scan_predicate+hot_snapshot_residual".to_string(),
);
}
}
}
lines
}
}
impl fmt::Display for ScanPlan {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ScanPlan::SeqScan { table, filter } => {
write!(f, "Seq Scan on {}", table)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::ParallelSeqScan {
table,
filter,
workers,
} => {
write!(f, "Parallel Seq Scan on {} (workers={})", table, workers)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::PkLookup {
table,
pk_column,
pk_value,
} => {
write!(f, "PK Lookup on {}\n {} = {}", table, pk_column, pk_value)
}
ScanPlan::IndexScan {
table,
index_name,
column,
condition,
filter,
} => {
write!(
f,
"Index Scan using {} on {}\n Index Cond: {} {}",
index_name, table, column, condition
)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::MultiIndexScan {
table,
indexes,
operation,
filter,
} => {
write!(f, "Multi-Index Scan on {} ({})", table, operation)?;
for (idx_name, col, cond) in indexes {
write!(f, "\n -> {} on {}: {}", idx_name, col, cond)?;
}
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::SegmentedMultiIndexScan {
table,
indexes,
operation,
filter,
cold_segments,
cold_rows_hint,
hot_rows_hint,
} => {
write!(f, "Segmented Multi-Index Scan on {} ({})", table, operation)?;
for (idx_name, col, cond) in indexes {
write!(f, "\n -> {} on {}: {}", idx_name, col, cond)?;
}
write!(
f,
"\n Cold Segments: {}\n Cold Rows Hint: {}\n Hot Rows Hint: {}",
cold_segments, cold_rows_hint, hot_rows_hint
)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::CompositeIndexScan {
table,
index_name,
columns,
conditions,
filter,
} => {
write!(
f,
"Composite Index Scan using {} on {}\n Columns: ({})",
index_name,
table,
columns.join(", ")
)?;
for (col, cond) in columns.iter().zip(conditions.iter()) {
write!(f, "\n {} {}", col, cond)?;
}
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::SegmentedCompositeIndexScan {
table,
index_name,
columns,
conditions,
filter,
cold_segments,
cold_rows_hint,
hot_rows_hint,
ordered: _,
composite,
} => {
let node_name = if !composite {
"Segmented Index Scan"
} else {
"Segmented Composite Index Scan"
};
write!(
f,
"{} using {} on {}\n Columns: ({})\n Cold Segments: {}\n Cold Rows Hint: {}\n Hot Rows Hint: {}",
node_name,
index_name,
table,
columns.join(", "),
cold_segments,
cold_rows_hint,
hot_rows_hint
)?;
for (column, condition) in columns.iter().zip(conditions.iter()) {
write!(f, "\n {} {}", column, condition)?;
}
if let Some(residual) = filter {
write!(f, "\n Filter: {}", residual)?;
}
Ok(())
}
ScanPlan::VectorSearch {
table,
index_name,
vector_column,
metric,
k,
ef_search,
filter,
} => {
write!(
f,
"HNSW Index Scan using {} on {}\n Column: {}\n Metric: {}\n K: {}\n EF Search: {}",
index_name, table, vector_column, metric, k, ef_search
)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::VectorBruteForce {
table,
vector_column,
metric,
k,
filter,
} => {
write!(
f,
"Vector Scan on {}\n Column: {}\n Metric: {}\n K: {}",
table, vector_column, metric, k
)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
ScanPlan::SegmentedScan {
table,
filter,
cold_segments,
cold_rows_hint,
cold_selected_segments,
cold_selected_rows_hint,
hot_rows_hint,
..
} => {
write!(
f,
"Segmented Scan on {}\n Cold Segments: {}\n Cold Rows Hint: {}\n Cold Selected Segments: {}\n Cold Selected Rows Hint: {}\n Hot Rows Hint: {}",
table,
cold_segments,
cold_rows_hint,
cold_selected_segments,
cold_selected_rows_hint,
hot_rows_hint
)?;
if let Some(flt) = filter {
write!(f, "\n Filter: {}", flt)?;
}
Ok(())
}
}
}
}
/// Table represents a database table
///
/// This trait defines the interface for interacting with a table,
/// including schema management, data manipulation (CRUD), and scanning.
///
/// # Example
///
/// ```ignore
/// let table = transaction.get_table("users")?;
/// println!("Table: {}", table.name());
/// println!("Schema: {:?}", table.schema());
///
/// // Insert a row
/// let row = Row::from_values(vec![Value::Integer(1), Value::text("Alice")]);
/// table.insert(row)?;
///
/// // Scan all rows
/// let scanner = table.scan(&[0, 1], None)?;
/// while scanner.next() {
/// println!("{:?}", scanner.row());
/// }
/// ```
/// A normalized contiguous predicate over an INTEGER primary key.
///
/// The range is deliberately a storage contract rather than a parser detail:
/// a caller that has proven its WHERE clause consists only of conjunctive
/// comparisons on the current INTEGER PRIMARY KEY can ask storage for an
/// exact metadata-only count. Any other shape must use the normal scan path.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct IntegerPrimaryKeyRange {
lower: Option<(i64, bool)>,
upper: Option<(i64, bool)>,
}
impl IntegerPrimaryKeyRange {
/// Build a range from simple AND-combined comparisons.
///
/// When `require_all_columns` is true every supplied comparison must target
/// `pk_name`; this is the mode for an exact COUNT operator. Scanner
/// refinement may pass false and use only the PK portion as a safe prune.
pub fn from_conjunctive_comparisons(
comparisons: &[(&str, Operator, &Value)],
pk_name: &str,
require_all_columns: bool,
) -> Option<Self> {
fn unqualified(name: &str) -> &str {
name.rsplit('.').next().unwrap_or(name).trim_matches('"')
}
let mut range = Self::default();
let mut found = false;
for &(column, operator, value) in comparisons {
if !unqualified(column).eq_ignore_ascii_case(pk_name) {
if require_all_columns {
return None;
}
continue;
}
// Row-ID metadata has the exact INTEGER physical domain. A
// Float/Decimal bound cannot be truncated or saturated here:
// doing so may exclude a valid row before the canonical mixed
// numeric predicate gets its residual check (for example
// `id < 0.5` must retain row ID 0). Decline the optimization unless
// the bound already has the exact physical representation.
let Value::Integer(value) = value else {
return None;
};
if !range.apply_comparison(operator, *value) {
return None;
}
found = true;
}
found.then_some(range)
}
/// True when no INTEGER can satisfy both normalized bounds.
#[inline]
pub fn is_empty(&self) -> bool {
match (self.lower, self.upper) {
(Some((lower, lower_inclusive)), Some((upper, upper_inclusive))) => {
lower > upper || (lower == upper && (!lower_inclusive || !upper_inclusive))
}
_ => false,
}
}
/// Normalized lower bound as `(value, inclusive)`.
#[inline]
pub fn lower_bound(&self) -> Option<(i64, bool)> {
self.lower
}
/// Normalized upper bound as `(value, inclusive)`.
#[inline]
pub fn upper_bound(&self) -> Option<(i64, bool)> {
self.upper
}
/// Return whether one row id belongs to this range.
#[inline]
pub fn contains(&self, row_id: i64) -> bool {
if self.is_empty() {
return false;
}
if let Some((lower, inclusive)) = self.lower {
if row_id < lower || (row_id == lower && !inclusive) {
return false;
}
}
if let Some((upper, inclusive)) = self.upper {
if row_id > upper || (row_id == upper && !inclusive) {
return false;
}
}
true
}
/// Return the exact half-open slice of sorted row IDs matching this range.
#[inline]
pub fn slice_bounds(&self, row_ids: &[i64]) -> (usize, usize) {
self.bounds_with(row_ids.len(), |index| row_ids[index])
}
/// Return bounds over any sorted row-id owner without materializing a
/// temporary slice. Cold artifact-backed metadata uses this with compact row-id runs.
pub fn bounds_with(
&self,
len: usize,
mut row_id_at: impl FnMut(usize) -> i64,
) -> (usize, usize) {
if self.is_empty() {
return (0, 0);
}
let mut partition = |mut predicate: Box<dyn FnMut(i64) -> bool>| {
let mut left = 0usize;
let mut right = len;
while left < right {
let middle = left + (right - left) / 2;
if predicate(row_id_at(middle)) {
left = middle + 1;
} else {
right = middle;
}
}
left
};
let start = match self.lower {
Some((value, true)) => partition(Box::new(move |row_id| row_id < value)),
Some((value, false)) => partition(Box::new(move |row_id| row_id <= value)),
None => 0,
};
let end = match self.upper {
Some((value, true)) => partition(Box::new(move |row_id| row_id <= value)),
Some((value, false)) => partition(Box::new(move |row_id| row_id < value)),
None => len,
};
(start.min(end), end)
}
fn apply_comparison(&mut self, operator: Operator, value: i64) -> bool {
match operator {
Operator::Eq => {
self.tighten_lower(value, true);
self.tighten_upper(value, true);
}
Operator::Gt => self.tighten_lower(value, false),
Operator::Gte => self.tighten_lower(value, true),
Operator::Lt => self.tighten_upper(value, false),
Operator::Lte => self.tighten_upper(value, true),
_ => return false,
}
true
}
fn tighten_lower(&mut self, value: i64, inclusive: bool) {
if self.lower.is_none_or(|(current, current_inclusive)| {
value > current || (value == current && !inclusive && current_inclusive)
}) {
self.lower = Some((value, inclusive));
}
}
fn tighten_upper(&mut self, value: i64, inclusive: bool) {
if self.upper.is_none_or(|(current, current_inclusive)| {
value < current || (value == current && !inclusive && current_inclusive)
}) {
self.upper = Some((value, inclusive));
}
}
}
pub trait Table: Send + Sync {
/// Returns the name of the table
fn name(&self) -> &str;
/// Returns the schema of the table
fn schema(&self) -> &Schema;
/// Returns the transaction ID this table handle belongs to.
/// Used by FK enforcement to participate in the caller's transaction.
fn txn_id(&self) -> i64;
/// Stage removal of an index entry owned by an immutable storage tier.
/// Composite storage implementations use this hidden hook to join cold
/// index changes to the MVCC commit transition instead of mutating shared
/// indexes before commit.
#[doc(hidden)]
fn stage_external_index_removal(
&mut self,
_index: Arc<dyn Index>,
_values: Vec<Value>,
_row_id: i64,
) -> Result<()> {
Err(Error::NotSupported(
"table does not support transaction-private external index removals".to_string(),
))
}
/// Creates a new column in the table
///
/// # Arguments
/// * `name` - The name of the column
/// * `column_type` - The data type of the column
/// * `nullable` - Whether the column can contain NULL values
fn create_column(&mut self, name: &str, column_type: DataType, nullable: bool) -> Result<()>;
/// Creates a new column in the table with default expression
///
/// # Arguments
/// * `name` - The name of the column
/// * `column_type` - The data type of the column
/// * `nullable` - Whether the column can contain NULL values
/// * `default_expr` - Default value expression as string (to be evaluated during INSERT)
fn create_column_with_default(
&mut self,
name: &str,
column_type: DataType,
nullable: bool,
default_expr: Option<String>,
) -> Result<()> {
if default_expr.is_some() {
return Err(radixdb_core::Error::NotSupported(
"table implementation does not support column defaults".to_string(),
));
}
self.create_column(name, column_type, nullable)
}
/// Creates a new column with both expression and pre-computed default value
///
/// The pre-computed default value is used for schema evolution (backfilling existing rows)
/// while the expression string is used for new inserts.
///
/// # Arguments
/// * `name` - The name of the column
/// * `column_type` - The data type of the column
/// * `nullable` - Whether the column can contain NULL values
/// * `default_expr` - Default value expression as string (for INSERT)
/// * `default_value` - Pre-computed default value (for schema evolution)
fn create_column_with_default_value(
&mut self,
name: &str,
column_type: DataType,
nullable: bool,
default_expr: Option<String>,
default_value: Option<radixdb_core::Value>,
) -> Result<()> {
if default_value.is_some() {
return Err(radixdb_core::Error::NotSupported(
"table implementation does not support schema backfill values".to_string(),
));
}
self.create_column_with_default(name, column_type, nullable, default_expr)
}
/// Drops a column from the table
///
/// # Arguments
/// * `name` - The name of the column to drop
fn drop_column(&mut self, name: &str) -> Result<()>;
/// Materializes storage-owned values that must exist before statement-level
/// constraints are evaluated (currently AUTO_INCREMENT INTEGER/UUID keys).
///
/// The default is a no-op for table implementations without generated
/// values. Calling this more than once for the same row must be idempotent.
fn materialize_insert_values(&mut self, _row: &mut Row) -> Result<()> {
Ok(())
}
/// Inserts a single row into the table
///
/// # Arguments
/// * `row` - The row to insert
///
/// # Returns
/// The inserted row (with AUTO_INCREMENT values applied)
fn insert(&mut self, row: Row) -> Result<Row>;
/// Inserts a single row without returning it (avoids clone overhead)
///
/// Use this when the RETURNING clause is not needed.
/// This is ~7ms faster per 1000 rows by avoiding Row::clone().
///
/// # Arguments
/// * `row` - The row to insert
fn insert_discard(&mut self, row: Row) -> Result<()> {
self.insert(row)?;
Ok(())
}
/// Inserts multiple rows into the table in a single batch operation
///
/// This is more efficient than calling `insert` multiple times.
///
/// # Arguments
/// * `rows` - The rows to insert
fn insert_batch(&mut self, rows: Vec<Row>) -> Result<()>;
/// Updates rows matching the given expression
///
/// # Arguments
/// * `where_expr` - Expression to filter rows to update (None means all rows)
/// * `setter` - Function that transforms a row in place, returns true if changed
///
/// # Returns
/// The number of rows updated
fn update(
&mut self,
where_expr: Option<&dyn Expression>,
setter: &mut dyn FnMut(Row) -> Result<(Row, bool)>,
) -> Result<i32>;
/// Updates rows by their row IDs directly (O(k) lookup instead of O(n) scan).
///
/// This is an optimization for UPDATE with IN subquery on INTEGER PRIMARY KEY.
/// Instead of scanning all rows and filtering, we directly look up the specific row IDs.
///
/// # Arguments
/// * `row_ids` - The row IDs to update (should be sorted for cache locality)
/// * `setter` - Function that transforms a row in place, returns true if changed
///
/// # Returns
/// The number of rows updated
fn update_by_row_ids(
&mut self,
row_ids: &[i64],
setter: &mut dyn FnMut(Row) -> Result<(Row, bool)>,
) -> Result<i32>;
/// Deletes rows by their row IDs directly (O(k) lookup instead of O(n) scan).
///
/// This is an optimization for DELETE with IN subquery on INTEGER PRIMARY KEY.
///
/// # Arguments
/// * `row_ids` - The row IDs to delete (should be sorted for cache locality)
///
/// # Returns
/// The number of rows deleted
fn delete_by_row_ids(&mut self, row_ids: &[i64]) -> Result<i32>;
/// Discovers the internal row IDs matched by one DELETE predicate.
///
/// This is the read half of the DML access plan. An exact empty projection
/// preserves row identity while allowing storage implementations to read
/// only predicate columns. Scanner implementations may override
/// `collect_remaining_row_ids` to avoid constructing output rows entirely.
fn collect_delete_candidate_row_ids(
&self,
where_expr: Option<&dyn Expression>,
) -> Result<Vec<i64>> {
let mut scanner = self.scan_exact_projection(&[], where_expr)?;
let mut row_ids = Vec::new();
if !scanner.collect_remaining_row_ids(&mut row_ids)? {
while scanner.next() {
row_ids.push(scanner.current_row_id()?);
}
}
if let Some(error) = scanner.err() {
return Err(error.clone());
}
scanner.close()?;
row_ids.sort_unstable();
row_ids.dedup();
Ok(row_ids)
}
/// Applies the mutation half of a DELETE access plan to a bounded row-ID
/// set. Storage implementations that can revalidate a predicate after
/// acquiring write claims should override this method.
fn delete_candidate_row_ids(
&mut self,
row_ids: &[i64],
_recheck_expr: Option<&dyn Expression>,
) -> Result<i32> {
self.delete_by_row_ids(row_ids)
}
/// Applies a DELETE candidate batch and reports the exact physical IDs
/// that were staged. RETURNING uses this outcome to avoid issuing one
/// storage mutation per row while preserving concurrent recheck semantics.
fn delete_candidate_row_ids_collect(
&mut self,
row_ids: &[i64],
recheck_expr: Option<&dyn Expression>,
deleted_row_ids: &mut Vec<i64>,
) -> Result<i32> {
let deleted = self.delete_candidate_row_ids(row_ids, recheck_expr)?;
if deleted as usize == row_ids.len() {
deleted_row_ids.extend_from_slice(row_ids);
}
Ok(deleted)
}
/// Returns all active row IDs visible to the current transaction.
/// Used for NOT IN (anti-join) optimization.
fn get_active_row_ids(&self) -> Vec<i64>;
/// Populate a FxHashSet with all hot row_ids. Avoids the intermediate Vec
/// allocation of get_active_row_ids() when building skip sets.
fn collect_hot_row_ids_into(&self, dest: &mut rustc_hash::FxHashSet<i64>) {
for id in self.get_active_row_ids() {
dest.insert(id);
}
}
/// Populate the logical shadow set used while preparing a mixed hot/cold
/// scan. Only hot versions visible to this table transaction may shadow an
/// older cold version; transaction-local writes/deletes must also be
/// included. The caller owns the table's membership fence for the complete
/// hot+cold snapshot.
#[doc(hidden)]
fn collect_shadow_row_ids_into(&self, dest: &mut rustc_hash::FxHashSet<i64>) {
self.collect_hot_row_ids_into(dest);
}
/// Check if a specific row_id exists in the hot buffer.
/// O(log n) lookup instead of collecting all row_ids.
fn has_row_id(&self, _row_id: i64) -> bool {
false
}
/// Returns the per-table publication fence, when the storage implementation
/// has one.
///
/// A writer holds the exclusive side from publication of hot MVCC versions
/// through publication of matching cold tombstones and transaction
/// visibility. A membership probe holds the shared side for its complete
/// hot+cold observation. Implementations without an MVCC/cold split may
/// leave the default `None` value.
#[doc(hidden)]
fn membership_fence(&self) -> Option<Arc<RwLock<()>>> {
None
}
/// Scan while restricting candidates to stable row-ID ranges produced by
/// a non-stale zone-map generation. Implementations that cannot consume
/// the ranges safely retain the correctness-first ordinary scan fallback.
#[doc(hidden)]
fn scan_with_row_id_ranges(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
_row_id_ranges: &[(i64, i64)],
) -> Result<Box<dyn Scanner>> {
self.scan(column_indices, where_expr)
}
/// Exact-projection counterpart of [`Table::scan_with_row_id_ranges`].
#[doc(hidden)]
fn scan_exact_projection_with_row_id_ranges(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
_row_id_ranges: &[(i64, i64)],
) -> Result<Box<dyn Scanner>> {
self.scan_exact_projection(column_indices, where_expr)
}
/// Probe row IDs for visibility in the current transaction.
///
/// `matches[i]` corresponds exactly to `row_ids[i]`. Duplicate row IDs are
/// intentionally preserved, so the returned count includes every matching
/// input position. Implementations must overwrite every output slot.
///
/// The default implementation is a correctness fallback for table types
/// without a metadata-only lookup path. It collects the table's visible
/// rows and therefore may materialize payload data. Storage implementations
/// with MVCC/segment metadata should override this method.
fn probe_visible_row_ids(&self, row_ids: &[i64], matches: &mut [bool]) -> Result<usize> {
let fence = self.membership_fence();
let _publication_guard = fence.as_ref().map(|fence| fence.read());
self.probe_visible_row_ids_unfenced(row_ids, matches)
}
/// Implementation hook for [`Table::probe_visible_row_ids`].
///
/// The public method acquires [`Table::membership_fence`] before calling
/// this hook. Composite table implementations that need to hold one fence
/// across several storage sources may call it after acquiring that fence.
/// Callers outside the storage layer must use `probe_visible_row_ids`.
#[doc(hidden)]
fn probe_visible_row_ids_unfenced(
&self,
row_ids: &[i64],
matches: &mut [bool],
) -> Result<usize> {
if row_ids.len() != matches.len() {
return Err(Error::invalid_argument(format!(
"row ID probe output length mismatch: expected {}, got {}",
row_ids.len(),
matches.len()
)));
}
matches.fill(false);
if row_ids.is_empty() {
return Ok(0);
}
let requested: rustc_hash::FxHashSet<i64> = row_ids.iter().copied().collect();
let visible_rows = self.collect_all_rows(None)?;
let mut visible_ids =
rustc_hash::FxHashSet::with_capacity_and_hasher(requested.len(), Default::default());
for (row_id, _) in visible_rows {
if requested.contains(&row_id) {
visible_ids.insert(row_id);
}
}
let mut count = 0;
for (row_id, matched) in row_ids.iter().zip(matches.iter_mut()) {
*matched = visible_ids.contains(row_id);
count += usize::from(*matched);
}
Ok(count)
}
/// Count visible rows in an exact range of the current INTEGER PRIMARY
/// KEY without loading row payload blocks.
///
/// `None` means that this storage implementation cannot prove the answer
/// from metadata for the present MVCC/segment state and the executor must
/// use its regular filtered aggregate path. `Some(Err(_))` is a real
/// storage error and must not be silently converted into a scan.
fn count_visible_integer_primary_key_range(
&self,
_range: &IntegerPrimaryKeyRange,
) -> Option<Result<usize>> {
None
}
/// Claim a row for update to prevent concurrent cold-row modifications.
/// When two transactions update the same cold row, both mirror it into hot
/// via insert_discard. Without claiming, neither detects the other because
/// the row starts absent from hot. This method uses the VersionStore's
/// uncommitted_writes map to serialize access.
fn try_claim_row(&self, _row_id: i64) -> Result<()> {
Ok(())
}
/// Claims several rows in one deterministic mutation boundary.
///
/// Implementations with MVCC should override this method to sort/deduplicate
/// candidates and acquire the underlying write claims without repeating
/// transaction-store locks for every row. The default preserves the
/// contract for simpler table implementations.
fn try_claim_rows(&self, row_ids: &[i64]) -> Result<()> {
for &row_id in row_ids {
self.try_claim_row(row_id)?;
}
Ok(())
}
/// Claims DELETE candidates while preserving DELETE provenance in MVCC
/// implementations. Simple tables may use the generic claim contract.
fn try_claim_rows_for_delete(&self, row_ids: &[i64]) -> Result<()> {
self.try_claim_rows(row_ids)
}
/// Deletes rows matching the given expression
///
/// # Arguments
/// * `where_expr` - Expression to filter rows to delete (None means all rows)
///
/// # Returns
/// The number of rows deleted
fn delete(&mut self, where_expr: Option<&dyn Expression>) -> Result<i32>;
/// Truncates the table, removing all rows efficiently.
/// Unlike DELETE, this drops storage directly instead of creating delete versions.
/// Default implementation falls back to delete(None).
fn truncate(&mut self) -> Result<i32> {
self.delete(None)
}
/// Validate implementation-specific TRUNCATE preconditions, execute one
/// coordinated storage publication, then clear the table. Hybrid storage
/// overrides this so a failed hot preflight cannot expose a cold-only
/// partial outcome.
fn truncate_after(&mut self, before_clear: &mut dyn FnMut() -> Result<()>) -> Result<i32> {
before_clear()?;
self.truncate()
}
/// Scans the table and returns a scanner over matching rows
///
/// # Arguments
/// * `column_indices` - Indices of columns to include in the scan
/// * `where_expr` - Expression to filter rows (None means all rows)
///
/// # Returns
/// A scanner that iterates over the matching rows
fn scan(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
) -> Result<Box<dyn Scanner>>;
/// Visit the visible full-row snapshot without requiring one owning
/// `RowVec` for the complete table.
///
/// The default keeps third-party table implementations source-compatible
/// by consuming their scanner. Native MVCC/segmented tables override this
/// boundary so maintenance operations such as `ANALYZE` can retain only
/// bounded per-column state while walking a large table.
fn visit_visible_rows(&self, visitor: &mut dyn FnMut(i64, Row) -> Result<()>) -> Result<()> {
let projection: Vec<usize> = (0..self.schema().columns.len()).collect();
let mut scanner = self.scan(&projection, None)?;
while scanner.next() {
let (row_id, row) = scanner.take_row_with_id()?;
visitor(row_id, row)?;
}
if let Some(error) = scanner.err().cloned() {
let _ = scanner.close();
return Err(error);
}
scanner.close()
}
/// Scans the table with exact projection semantics.
///
/// Unlike `scan()`, an empty `column_indices` slice means "return zero-width
/// rows", not `SELECT *`. This boundary is for COUNT(*) and other
/// columnless paths that need row visibility/cardinality without reading
/// user payload columns.
fn scan_exact_projection(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
) -> Result<Box<dyn Scanner>> {
if column_indices.is_empty() {
return Err(radixdb_core::Error::NotSupported(
"table implementation does not support exact empty projection".to_string(),
));
}
self.scan(column_indices, where_expr)
}
/// Exact-projection scan used after the caller has already acquired this
/// table's exclusive membership fence.
///
/// Native hot+cold tables override this hook to avoid recursively taking
/// the non-reentrant shared side while transactional commit validation owns
/// the exclusive side. Callers outside the storage commit path must use
/// [`Table::scan_exact_projection`].
#[doc(hidden)]
fn scan_exact_projection_unfenced(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
) -> Result<Box<dyn Scanner>> {
self.scan_exact_projection(column_indices, where_expr)
}
/// Collects all rows matching the expression without intermediate cloning
///
/// This is more efficient than using scan() when you need all rows at once,
/// as it avoids the double-clone overhead of the scanner interface.
///
/// # Arguments
/// * `where_expr` - Expression to filter rows (None means all rows)
///
/// # Returns
/// Cached row vector - returns to cache on drop for reuse
fn collect_all_rows(&self, where_expr: Option<&dyn Expression>) -> Result<RowVec>;
/// Collects rows with an optional limit (LIMIT pushdown optimization)
///
/// This enables early termination when only a limited number of rows are needed,
/// avoiding the cost of scanning the entire table.
///
/// # Arguments
/// * `where_expr` - Optional filter expression
/// * `limit` - Maximum number of rows to return
/// * `offset` - Number of rows to skip before returning
///
/// # Returns
/// A RowVec containing rows up to the limit (after offset) with row IDs
fn collect_rows_with_limit(
&self,
where_expr: Option<&dyn Expression>,
limit: usize,
offset: usize,
) -> Result<RowVec> {
// Default implementation: collect all and apply limit/offset
let all_rows = self.collect_all_rows(where_expr)?;
Ok(all_rows.into_iter().skip(offset).take(limit).collect())
}
/// Collects rows with LIMIT/OFFSET without guaranteeing deterministic order.
///
/// This is an optimization for queries with LIMIT but without ORDER BY.
/// Since SQL doesn't guarantee order for LIMIT without ORDER BY, we can
/// skip sorting and return rows in arbitrary order. This provides significant
/// speedup by enabling true early termination.
///
/// # Arguments
/// * `where_expr` - Optional filter expression
/// * `limit` - Maximum number of rows to return
/// * `offset` - Number of rows to skip before returning
///
/// # Returns
/// A RowVec containing rows up to the limit (after offset), in arbitrary order
fn collect_rows_with_limit_unordered(
&self,
where_expr: Option<&dyn Expression>,
limit: usize,
offset: usize,
) -> Result<RowVec> {
// Default implementation: delegate to ordered version
self.collect_rows_with_limit(where_expr, limit, offset)
}
/// Collects projected rows with LIMIT/OFFSET without guaranteeing deterministic order.
///
/// This is the projected variant of `collect_rows_with_limit_unordered`.
/// The default implementation preserves each table's existing LIMIT behavior
/// and trims rows at the table boundary; storage implementations with column
/// blocks should override this to avoid reading unused columns.
fn collect_rows_with_limit_unordered_projected(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
limit: usize,
offset: usize,
) -> Result<RowVec> {
let rows = self.collect_rows_with_limit_unordered(where_expr, limit, offset)?;
let num_schema_cols = self.schema().columns.len();
let needs_projection = !column_indices.is_empty()
&& !(column_indices.len() == num_schema_cols
&& column_indices.iter().enumerate().all(|(i, &idx)| i == idx));
if !needs_projection {
return Ok(rows);
}
Ok(rows
.into_iter()
.map(|(row_id, row)| {
let values = column_indices
.iter()
.map(|&idx| row.get(idx).cloned().unwrap_or_else(Value::null_unknown))
.collect();
(row_id, Row::from_values(values))
})
.collect())
}
/// Collects projected rows with exact projection semantics and LIMIT/OFFSET.
///
/// Unlike `collect_rows_with_limit_unordered_projected()`, an empty
/// projection means zero-width rows. This is intended for expression
/// projection/dependency plans where the dependency set may legitimately be
/// empty.
fn collect_rows_with_limit_unordered_exact_projected(
&self,
column_indices: &[usize],
where_expr: Option<&dyn Expression>,
limit: usize,
offset: usize,
) -> Result<RowVec> {
let mut scanner = self.scan_exact_projection(column_indices, where_expr)?;
let mut rows = RowVec::with_capacity(limit.min(1024));
let mut skipped = 0usize;
while scanner.next() {
if skipped < offset {
skipped += 1;
continue;
}
if rows.len() >= limit {
break;
}
rows.push(scanner.take_row_with_id()?);
}
if let Some(err) = scanner.err() {
return Err(err.clone());
}
scanner.close()?;
Ok(rows)
}
/// Collects all rows WITHOUT guaranteeing deterministic order.
///
/// This is an optimization for GROUP BY queries where row order doesn't matter.
/// By skipping the O(n log n) sort, this provides significant speedup for large tables.
///
/// # Returns
/// Cached row vector in arbitrary (storage iteration) order
fn collect_all_rows_unsorted(&self) -> Result<RowVec> {
// Default implementation: delegate to ordered version
self.collect_all_rows(None)
}
/// Collects rows for specific row IDs.
///
/// Used by HNSW index search to fetch rows after approximate nearest neighbor lookup.
/// The returned rows preserve the order of the input row_ids.
fn collect_rows_by_ids(&self, _row_ids: &[i64]) -> Result<RowVec> {
Err(Error::NotSupported(
"collect_rows_by_ids not implemented".to_string(),
))
}
/// Collects exact projected rows for specific row IDs.
///
/// This is an unfiltered lookup boundary intended for operators that have
/// already proven visibility/key eligibility and only need a subset of row
/// columns. An empty projection means exactly zero columns, not `SELECT *`.
fn collect_rows_by_ids_projected(
&self,
row_ids: &[i64],
column_indices: &[usize],
) -> Result<RowVec> {
let rows = self.collect_rows_by_ids(row_ids)?;
rows.into_iter()
.map(|(row_id, row)| {
row.take_columns(column_indices)
.map(|projected| (row_id, projected))
})
.collect()
}
/// Collect rows with ORDER BY + LIMIT using deferred materialization
///
/// This is an optimization for `SELECT * FROM t ORDER BY col LIMIT n`:
/// - Loads only the sort column values (not full rows)
/// - Sorts indices by those values
/// - Materializes only the top N rows
///
/// # Arguments
/// * `sort_col_idx` - Column index to sort by
/// * `ascending` - Sort direction (true = ASC, false = DESC)
/// * `limit` - Maximum rows to return
/// * `offset` - Rows to skip before collecting
///
/// # Returns
/// A vector of rows sorted by the specified column
fn collect_rows_sorted_with_limit(
&self,
sort_col_idx: usize,
ascending: bool,
limit: usize,
offset: usize,
) -> Result<Vec<Row>> {
// Default implementation: collect all, sort, take limit
// Concrete implementations can override with deferred materialization
let mut rows = self.collect_all_rows(None)?;
rows.sort_by(|(_, a), (_, b)| {
let va = a.get(sort_col_idx);
let vb = b.get(sort_col_idx);
let cmp = match (va, vb) {
(None, None) => std::cmp::Ordering::Equal,
(None, Some(_)) => std::cmp::Ordering::Less,
(Some(_), None) => std::cmp::Ordering::Greater,
(Some(va), Some(vb)) => va.compare(vb).unwrap_or(std::cmp::Ordering::Equal),
};
if ascending {
cmp
} else {
cmp.reverse()
}
});
Ok(rows
.into_iter()
.skip(offset)
.take(limit)
.map(|(_, row)| row)
.collect())
}
/// Closes the table and releases any resources
fn close(&mut self) -> Result<()>;
/// Commits any pending changes in this table's transaction
///
/// This applies the table's local transaction changes to the global store,
/// making them visible to other transactions.
fn commit(&mut self) -> Result<()>;
/// Rolls back any pending changes in this table's transaction
///
/// This discards any local changes that have not been committed.
fn rollback(&mut self);
/// Rolls back changes to a specific timestamp (for savepoint support)
///
/// Discards all local changes with timestamps greater than the specified timestamp.
/// This is used by ROLLBACK TO SAVEPOINT to partially undo transaction changes.
///
/// # Arguments
/// * `timestamp` - The timestamp to roll back to (in nanoseconds since epoch)
fn rollback_to_timestamp(&self, timestamp: i64);
/// Returns true if this table has uncommitted local changes
///
/// This is used by the transaction to determine if the two-phase commit
/// protocol needs to be executed.
fn has_local_changes(&self) -> bool;
/// Returns the pending versions to be committed for WAL logging
///
/// Returns a list of (row_id, row_data, is_deleted, txn_id, create_time) tuples representing
/// all uncommitted changes in this table. This is called before commit()
/// to capture changes for WAL persistence.
///
/// # Returns
/// Vec of (row_id, row_data, is_deleted, txn_id, create_time) tuples
fn get_pending_versions(&self) -> Vec<(i64, Row, bool, i64, i64)> {
Vec::new() // Default implementation returns empty - override in concrete tables
}
// ---- Index Operations ----
/// Creates an index on the table
///
/// # Arguments
/// * `name` - The name of the index
/// * `columns` - The column names to include in the index
/// * `is_unique` - Whether this is a unique index
fn create_index(&self, name: &str, columns: &[&str], is_unique: bool) -> Result<()>;
/// Creates an index on the table with a specific index type
///
/// # Arguments
/// * `name` - The name of the index
/// * `columns` - The column names to include in the index
/// * `is_unique` - Whether this is a unique index
/// * `index_type` - Optional index type (Hash, BTree, Bitmap). If None, auto-selects based on column types.
///
/// # Type-Based Index Selection (when index_type is None):
/// - TEXT/JSON columns → Hash index (avoids O(strlen) comparisons)
/// - BOOLEAN columns → Bitmap index (only 2 values, fast AND/OR)
/// - INTEGER/FLOAT/TIMESTAMP columns → BTree index (supports range queries)
fn create_index_with_type(
&self,
name: &str,
columns: &[&str],
is_unique: bool,
index_type: Option<IndexType>,
) -> Result<()> {
// Default implementation calls create_index (ignores index_type)
let _ = index_type;
self.create_index(name, columns, is_unique)
}
/// Create a core-owned index whose logical external value is transformed
/// by a composition-layer encoder before reaching the access method.
#[doc(hidden)]
fn create_index_with_key_encoder(
&self,
name: &str,
columns: &[&str],
is_unique: bool,
index_type: IndexType,
encoder: crate::index::PreparedIndexKeyEncoder,
) -> Result<()> {
let _ = (name, columns, is_unique, index_type, encoder);
Err(Error::NotSupported(
"encoded index construction is not supported by this table".to_owned(),
))
}
/// Builds a complete index object without publishing it to table readers.
/// Segmented storage uses this internal hook to add cold rows before the
/// index becomes discoverable. Ordinary implementations may decline it.
#[doc(hidden)]
fn build_index_with_type_detached(
&self,
name: &str,
columns: &[&str],
is_unique: bool,
index_type: Option<IndexType>,
) -> Result<Arc<dyn Index>> {
let _ = (name, columns, is_unique, index_type);
Err(Error::NotSupported(
"detached index construction is not supported by this table".to_string(),
))
}
/// Builds and publishes only the hot portion of an index while immutable
/// cold coverage is prepared by the physical artifact publisher.
///
/// The method is intentionally separate from ordinary CREATE INDEX: a
/// caller must either publish complete cold coverage before readers are
/// released or call `complete_deferred_cold_index` as its failure path.
#[doc(hidden)]
fn create_index_with_deferred_cold_backfill(
&self,
name: &str,
columns: &[&str],
is_unique: bool,
index_type: Option<IndexType>,
) -> Result<()> {
let _ = (name, columns, is_unique, index_type);
Err(Error::NotSupported(
"deferred cold index construction is not supported by this table".to_string(),
))
}
/// Completes a previously deferred cold index in memory. This is the
/// correctness fallback when immutable accelerator publication cannot be
/// completed after the logical DDL commit became durable.
#[doc(hidden)]
fn complete_deferred_cold_index(&self, name: &str, columns: &[&str]) -> Result<()> {
let _ = (name, columns);
Err(Error::NotSupported(
"deferred cold index completion is not supported by this table".to_string(),
))
}
/// Publishes one fully built detached index as a single catalog action.
#[doc(hidden)]
fn publish_detached_index(&self, index: Arc<dyn Index>) -> Result<()> {
let _ = index;
Err(Error::NotSupported(
"detached index publication is not supported by this table".to_string(),
))
}
/// Creates a partial index on the table.
fn create_partial_index_with_type(
&self,
name: &str,
columns: &[&str],
is_unique: bool,
index_type: Option<IndexType>,
predicate: crate::index::PartialIndexPredicate,
) -> Result<()> {
let _ = (name, columns, is_unique, index_type, predicate);
Err(Error::invalid_argument(
"partial indexes are not supported by this table",
))
}
/// Creates an HNSW index with custom parameters
///
/// # Arguments
/// * `name` - The name of the index
/// * `column` - The vector column to index
/// * `is_unique` - Whether this is a unique index
/// * `m` - Max connections per node per layer
/// * `ef_construction` - Build-time beam width
/// * `ef_search` - Search-time beam width
/// * `metric` - Distance metric (L2, Cosine, InnerProduct)
#[allow(clippy::too_many_arguments)]
fn create_hnsw_index(
&self,
name: &str,
column: &str,
is_unique: bool,
m: usize,
ef_construction: usize,
ef_search: usize,
metric: crate::index::HnswDistanceMetric,
) -> Result<()> {
let _ = (
name,
column,
is_unique,
m,
ef_construction,
ef_search,
metric,
);
Err(radixdb_core::Error::internal(
"HNSW index not supported by this storage engine".to_string(),
))
}
/// Drops an index from the table
///
/// # Arguments
/// * `name` - The name of the index to drop
fn drop_index(&self, name: &str) -> Result<()>;
/// Renames an index without rebuilding index data.
///
/// # Arguments
/// * `old_name` - Existing index name
/// * `new_name` - New index name
fn rename_index(&self, old_name: &str, new_name: &str) -> Result<()>;
/// Creates a btree index on a column
///
/// # Arguments
/// * `column_name` - The column to index
/// * `is_unique` - Whether this is a unique index
/// * `custom_name` - Optional custom name for the index
fn create_btree_index(
&self,
column_name: &str,
is_unique: bool,
custom_name: Option<&str>,
) -> Result<()>;
/// Drops a btree index from the table
///
/// # Arguments
/// * `column_name` - The column whose index to drop
fn drop_btree_index(&self, column_name: &str) -> Result<()>;
/// Creates a multi-column index on the table
///
/// # Arguments
/// * `name` - The name of the index
/// * `columns` - The column names to include in the index
/// * `is_unique` - Whether this is a unique index
fn create_multi_column_index(
&self,
name: &str,
columns: &[&str],
is_unique: bool,
) -> Result<()> {
let _ = (name, columns, is_unique);
Err(Error::NotSupported(
"Multi-column indexes not supported by this table type".to_string(),
))
}
/// Checks if an index exists on a specific column
///
/// # Arguments
/// * `column_name` - The column to check for an index
///
/// # Returns
/// true if an index exists on the column, false otherwise
fn has_index_on_column(&self, column_name: &str) -> bool {
let _ = column_name;
false // Default implementation - override in concrete tables
}
/// Gets the index on a specific column (if any exists)
///
/// # Arguments
/// * `column_name` - The column to get the index for
///
/// # Returns
/// Some(index) if an index exists on the column, None otherwise
fn get_index_on_column(&self, column_name: &str) -> Option<std::sync::Arc<dyn Index>> {
let _ = column_name;
None // Default implementation - override in concrete tables
}
/// Resolve exact row-id candidates for several values of one indexed
/// column. Implementations with immutable storage must include every
/// physical source or return `None`; callers may then use an honest scan
/// fallback instead of treating a hot-only index as complete.
fn collect_row_ids_by_index_values(
&self,
column_name: &str,
values: &[Value],
) -> Option<Result<Vec<i64>>> {
// Transaction-local inserts and index-key updates are deliberately not
// published to shared indexes before commit. A shared index is
// therefore not a complete candidate source while local changes exist.
if self.has_local_changes() {
return None;
}
let index = self.get_index_on_column(column_name)?;
let (_, column) = self.schema().find_column(column_name)?;
if crate::expression::in_list::has_cross_numeric_physical_variant(column.data_type, values)
{
return None;
}
let mut row_ids = Vec::new();
for value in values {
let value = value.coerce_to_type(column.data_type);
if value.is_null() {
continue;
}
if let Err(error) =
index.get_row_ids_equal_into(std::slice::from_ref(&value), &mut row_ids)
{
return Some(Err(error));
}
}
row_ids.sort_unstable();
row_ids.dedup();
Some(Ok(row_ids))
}
/// Resolve and materialize exact indexed candidates in one physical
/// operation. Immutable tables may override this to fuse their
/// authoritative key recheck with the requested projection; the default
/// preserves the existing two-step contract.
fn collect_rows_by_index_values_projected(
&self,
column_name: &str,
values: &[Value],
projection: Option<&[usize]>,
) -> Option<Result<RowVec>> {
let row_ids = match self.collect_row_ids_by_index_values(column_name, values)? {
Ok(row_ids) => row_ids,
Err(error) => return Some(Err(error)),
};
Some(match projection {
Some(columns) => self.collect_rows_by_ids_projected(&row_ids, columns),
None => self.collect_rows_by_ids(&row_ids),
})
}
/// Resolve the union of several inclusive ranges through one named
/// core-owned index. `None` means the index cannot prove complete coverage
/// for this transaction/snapshot and the executor must use a full scan.
fn collect_row_ids_by_index_ranges(
&self,
index_name: &str,
ranges: &[IndexKeyRange],
) -> Option<Result<Vec<i64>>> {
if self.has_local_changes() {
return None;
}
let index = self.get_index(index_name)?;
let mut row_ids = Vec::new();
for range in ranges {
let entries = match index.find_physical_range(
std::slice::from_ref(&range.start),
std::slice::from_ref(&range.end),
true,
true,
) {
Ok(entries) => entries,
Err(error) => return Some(Err(error)),
};
row_ids.extend(entries.into_iter().map(|entry| entry.row_id));
}
row_ids.sort_unstable();
row_ids.dedup();
Some(Ok(row_ids))
}
/// Returns true when this table has immutable cold segment rows in addition
/// to the hot MVCC store.
///
/// Normal secondary indexes are hot-MVCC indexes unless a concrete table
/// explicitly documents and maintains cold-populated entries. Optimizers
/// must not assume that a secondary index covers cold rows merely because
/// `get_index_on_column()` returns an index handle.
fn has_cold_segments(&self) -> bool {
false
}
/// Gets all unique indexes on the table (for constraint checking).
///
/// Returns a list of (index_name, column_names) for each unique index.
/// Used by SegmentedTable to check volume data during inserts.
fn get_unique_indexes(&self) -> Vec<(String, Vec<String>)> {
Vec::new() // Default: no unique indexes
}
/// Gets all index handles known to this table.
///
/// Most callers should prefer narrower helpers. Volume-backed DML uses this
/// to maintain entries that were populated from cold rows for special index
/// families such as partial indexes.
fn get_indexes(&self) -> Vec<Arc<dyn Index>> {
Vec::new()
}
/// Finds a conflicting row ID for a unique-key lookup.
///
/// Volume-backed tables can override this to probe cold storage directly
/// after the hot index path misses, avoiding a full table scan on upserts.
fn find_unique_conflict_row_id(
&self,
_index_name: &str,
_column_name: &str,
_row_values: &[Value],
) -> Result<Option<i64>> {
Ok(None)
}
/// Iterate unique non-PK indexes by reference, avoiding per-call String allocations.
///
/// The callback receives `(&str, &[String])` — the index name and its column names —
/// without cloning. Implementations that hold an `RwLock<FxHashMap<String, Arc<dyn Index>>>`
/// can iterate the lock guard directly.
///
/// The default implementation falls back to `get_unique_indexes()`.
fn for_each_unique_non_pk_index(
&self,
f: &mut dyn FnMut(&str, &[String]) -> Result<()>,
) -> Result<()> {
for (name, cols) in self.get_unique_indexes() {
f(&name, &cols)?;
}
Ok(())
}
/// Gets unique non-PK index handles, preserving index metadata such as
/// CREATE INDEX ... WHERE predicates.
///
/// The older `(name, columns)` helper is still useful for allocation-free
/// full-index checks, but volume-backed constraint checks need the actual
/// index object to avoid treating partial unique indexes as full unique
/// indexes.
fn get_unique_non_pk_indexes(&self) -> Vec<Arc<dyn Index>> {
Vec::new()
}
/// Check if the table has any unique indexes that are NOT the PK column.
/// Used as a fast bail-out to avoid allocating get_unique_indexes().
fn has_unique_non_pk_indexes(&self) -> bool {
false // Default: no
}
/// Gets an index by name
///
/// # Arguments
/// * `name` - The name of the index
///
/// # Returns
/// Some(index) if found, None otherwise
fn get_index(&self, name: &str) -> Option<std::sync::Arc<dyn Index>> {
let _ = name;
None // Default implementation - override in concrete tables
}
/// Find the best multi-column index that matches a set of predicate columns.
/// Returns the index if it covers a prefix of the given columns.
/// For example, an index on (a, b, c) can be used for queries on (a), (a, b), or (a, b, c).
///
/// # Arguments
/// * `predicate_columns` - The columns used in WHERE clause predicates
///
/// # Returns
/// Some((index, matched_columns)) if found, None otherwise
fn get_multi_column_index(
&self,
predicate_columns: &[&str],
) -> Option<(std::sync::Arc<dyn Index>, usize)> {
let _ = predicate_columns;
None // Default implementation - override in concrete tables
}
/// Gets the minimum value from an indexed column (O(1) or O(log n) instead of O(n) scan)
///
/// # Arguments
/// * `column_name` - The column to get the minimum value from
///
/// # Returns
/// Some(Value) if the column has an index with min/max support, None otherwise
fn get_index_min_value(&self, column_name: &str) -> Option<Value> {
let _ = column_name;
None // Default implementation - override in concrete tables
}
/// Gets the maximum value from an indexed column (O(1) or O(log n) instead of O(n) scan)
///
/// # Arguments
/// * `column_name` - The column to get the maximum value from
///
/// # Returns
/// Some(Value) if the column has an index with min/max support, None otherwise
fn get_index_max_value(&self, column_name: &str) -> Option<Value> {
let _ = column_name;
None // Default implementation - override in concrete tables
}
/// Gets the count of rows in the table (COUNT(*) pushdown optimization)
///
/// This enables O(1) row counting instead of O(n) scan for `SELECT COUNT(*) FROM table`
/// without WHERE clause.
///
/// # Returns
/// The number of visible rows in the table
fn row_count(&self) -> usize {
0 // Default implementation - override in concrete tables
}
/// Fast O(1) row count hint for optimizer decisions
///
/// Returns an upper bound estimate without expensive visibility checks.
/// Use for cache eligibility and similar decisions where exact count isn't needed.
fn row_count_hint(&self) -> usize {
self.row_count() // Default falls back to row_count
}
/// Fast O(1) exact row count for COUNT(*) queries
///
/// Returns Some(count) if the fast path can be used (no local changes, sees all committed data),
/// Returns None if the caller should fall back to the full row_count() method.
///
/// This is different from row_count_hint() because it returns the EXACT count,
/// not an estimate. It's designed for COUNT(*) without WHERE clause.
fn fast_row_count(&self) -> Option<usize> {
None // Default: no fast path available
}
/// Collects rows sorted by an indexed column with limit (ORDER BY + LIMIT pushdown)
///
/// For queries like `SELECT * FROM table ORDER BY col LIMIT 10`, this uses the
/// index to get rows in sorted order, stopping after the limit is reached.
/// This is O(limit) instead of O(n log n) for full sort.
///
/// # Arguments
/// * `column_name` - The indexed column to order by
/// * `ascending` - True for ASC, false for DESC
/// * `limit` - Maximum number of rows to return
/// * `offset` - Number of rows to skip
///
/// # Returns
/// Some(RowVec) if the column has an index, None otherwise
fn collect_rows_ordered_by_index(
&self,
column_name: &str,
ascending: bool,
limit: usize,
offset: usize,
) -> Option<RowVec> {
let _ = (column_name, ascending, limit, offset);
None // Default implementation - override in concrete tables
}
/// Bounded ordered lookup for a composite index shaped as equality prefix
/// plus one range/order column. `None` means the storage layer cannot prove
/// this physical path for the current hot/cold state; `Some(Err(_))` is a
/// real execution error and must not be converted into a scan silently.
fn collect_rows_composite_ordered_range(
&self,
where_expr: &dyn Expression,
order_column: &str,
ascending: bool,
limit: usize,
offset: usize,
) -> Option<Result<RowVec>> {
let _ = (where_expr, order_column, ascending, limit, offset);
None
}
/// Keyset pagination optimization for PRIMARY KEY columns
///
/// For queries like `WHERE id > X ORDER BY id LIMIT Y`, this uses the PK's
/// natural ordering to start iteration from X and return only Y rows.
/// This provides O(limit) complexity instead of O(n) for full table scans.
///
/// # Arguments
/// * `start_after` - For `id > X` (exclusive bound)
/// * `start_from` - For `id >= X` (inclusive bound)
/// * `ascending` - True for ASC, false for DESC
/// * `limit` - Maximum number of rows to return
///
/// # Returns
/// Some(RowVec) if the table has an INTEGER PRIMARY KEY, None otherwise
fn collect_rows_pk_keyset(
&self,
start_after: Option<i64>,
start_from: Option<i64>,
ascending: bool,
limit: usize,
) -> Option<RowVec> {
let _ = (start_after, start_from, ascending, limit);
None // Default implementation - override in concrete tables
}
/// Collects rows grouped by an indexed partition column (PARTITION BY optimization)
///
/// For window functions with `PARTITION BY col` where col is indexed, this uses the
/// index to iterate through unique values and collect rows already grouped by partition.
/// This avoids O(n) hash-based grouping in window function execution.
///
/// # Arguments
/// * `column_name` - The indexed column to partition by
///
/// # Returns
/// Some(Vec<(Value, RowVec)>) where each tuple is (partition_value, rows_in_partition)
/// Returns None if the column has no index
fn collect_rows_grouped_by_partition(&self, column_name: &str) -> Option<Vec<(Value, RowVec)>> {
let _ = column_name;
None // Default implementation - override in concrete tables
}
/// Get distinct partition values from an indexed column.
/// Used for LIMIT pushdown in window functions - allows fetching partitions one at a time.
///
/// # Arguments
/// * `column_name` - The indexed column to get partition values from
///
/// # Returns
/// `Some(Vec<Value>)` with distinct values, or `None` if column has no index
fn get_partition_values(&self, column_name: &str) -> Option<Vec<Value>> {
let _ = column_name;
None // Default implementation - override in concrete tables
}
/// Compute distinct non-null values for a column by exploiting cold volume
/// metadata. For dictionary-encoded TEXT columns with no tombstones, this
/// extracts dictionary entries directly (O(unique values) per volume, no
/// row scan). Falls back to None when the fast path is not applicable.
///
/// # Arguments
/// * `col_idx` - Schema column index
///
/// # Returns
/// `Some(Vec<Value>)` with distinct non-null values, or `None` if fast path unavailable
fn compute_distinct_values(&self, _col_idx: usize) -> Option<Vec<Value>> {
None
}
/// Get the count of distinct non-null values from an indexed column.
/// Used for COUNT(DISTINCT col) optimization without cloning all values.
///
/// # Arguments
/// * `column_name` - The indexed column to count distinct values from
///
/// # Returns
/// Some(count) excluding NULL values, or None if column has no index
fn get_partition_count(&self, column_name: &str) -> Option<usize> {
let _ = column_name;
None // Default implementation - override in concrete tables
}
/// Get rows for a specific partition value.
/// Used for LIMIT pushdown in window functions - fetches only one partition at a time.
///
/// # Arguments
/// * `column_name` - The indexed column
/// * `partition_value` - The specific partition value to fetch rows for
///
/// # Returns
/// Some(RowVec) with rows matching the partition value, or None if column has no index
fn get_rows_for_partition_value(
&self,
column_name: &str,
partition_value: &Value,
) -> Option<RowVec> {
let _ = (column_name, partition_value);
None // Default implementation - override in concrete tables
}
/// Fetch rows by their row IDs with an optional filter.
///
/// # Arguments
/// * `row_ids` - The row IDs to fetch
/// * `filter` - Filter expression to apply to fetched rows
///
/// # Returns
/// A checked row vector in input-ID order. Storage/materialization and
/// expression failures remain distinguishable from a legitimate non-match.
fn fetch_rows_by_ids(&self, row_ids: &[i64], filter: &dyn Expression) -> Result<RowVec> {
let candidates = self.collect_rows_by_ids(row_ids)?;
let mut results = RowVec::with_capacity(candidates.len());
for (row_id, row) in candidates {
if filter.evaluate(&row)? {
results.push((row_id, row));
}
}
Ok(results)
}
/// Fetch rows into a reusable RowVec buffer
fn fetch_rows_by_ids_into(
&self,
row_ids: &[i64],
filter: &dyn Expression,
buffer: &mut RowVec,
) -> Result<()> {
buffer.extend(self.fetch_rows_by_ids(row_ids, filter)?);
Ok(())
}
// ---- Additional Column Operations ----
/// Renames a column in the table
///
/// # Arguments
/// * `old_name` - Current column name
/// * `new_name` - New column name
fn rename_column(&mut self, old_name: &str, new_name: &str) -> Result<()>;
/// Modifies a column's definition
///
/// # Arguments
/// * `name` - The column name
/// * `column_type` - The new data type
/// * `nullable` - Whether the column can contain NULL values
fn modify_column(&mut self, name: &str, column_type: DataType, nullable: bool) -> Result<()>;
/// Modifies a column's definition and replaces its default expression.
///
/// The default implementation preserves existing behavior for table
/// implementations that do not store default metadata.
fn modify_column_with_default(
&mut self,
name: &str,
column_type: DataType,
nullable: bool,
default_expr: Option<String>,
default_value: Option<Value>,
) -> Result<()> {
if default_expr.is_some() || default_value.is_some() {
return Err(radixdb_core::Error::NotSupported(
"table implementation does not support replacing column defaults".to_string(),
));
}
self.modify_column(name, column_type, nullable)
}
// ---- Query Operations ----
/// Executes a SELECT query on the table
///
/// # Arguments
/// * `columns` - Column names to include in the result
/// * `expr` - Optional filter expression
///
/// # Returns
/// A QueryResult with the matching rows
fn select(
&self,
columns: &[&str],
expr: Option<&dyn Expression>,
) -> Result<Box<dyn QueryResult>>;
/// Executes a SELECT query with column aliases
///
/// # Arguments
/// * `columns` - Column names to include in the result
/// * `expr` - Optional filter expression
/// * `aliases` - Map from alias names to original column names
///
/// # Returns
/// A QueryResult with the matching rows and aliased column names
fn select_with_aliases(
&self,
columns: &[&str],
expr: Option<&dyn Expression>,
aliases: &FxHashMap<String, String>,
) -> Result<Box<dyn QueryResult>>;
/// Executes a temporal SELECT query as of a specific point in time
///
/// # Arguments
/// * `columns` - Column names to include in the result
/// * `expr` - Optional filter expression
/// * `temporal_type` - Either "TRANSACTION" or "TIMESTAMP"
/// * `temporal_value` - Transaction ID or timestamp in nanoseconds
///
/// # Returns
/// A QueryResult with rows as they were at the specified point
fn select_as_of(
&self,
columns: &[&str],
expr: Option<&dyn Expression>,
temporal_type: &str,
temporal_value: i64,
) -> Result<Box<dyn QueryResult>>;
/// Explains what access method would be used for a scan
///
/// This method analyzes the WHERE expression and returns a ScanPlan
/// describing how the query would be executed (without actually executing it).
/// Used by EXPLAIN to show users the query execution strategy.
///
/// # Arguments
/// * `where_expr` - Optional filter expression to analyze
///
/// # Returns
/// A ScanPlan describing the access method that would be used
fn explain_scan(&self, where_expr: Option<&dyn Expression>) -> ScanPlan {
// Default implementation returns SeqScan
ScanPlan::SeqScan {
table: self.name().to_string(),
filter: where_expr.map(|e| format!("{:?}", e)),
}
}
// ---- Zone Map Operations (Statistics for Segment Pruning) ----
/// Sets the zone maps for this table
///
/// Zone maps contain min/max statistics per segment, enabling the query
/// executor to skip entire segments when predicates fall outside the range.
/// This is typically called by ANALYZE.
///
/// # Arguments
/// * `zone_maps` - The zone map statistics for the table
fn set_zone_maps(&self, _zone_maps: crate::volume::zonemap::TableZoneMap) {
// Default implementation does nothing - override in concrete tables
}
/// Data/schema generation captured before an ANALYZE build. Concrete MVCC
/// tables override this with a monotonic publication owner.
#[doc(hidden)]
fn zone_map_generation(&self) -> u64 {
0
}
/// Gets the zone maps for this table
///
/// Returns None if zone maps have not been built (ANALYZE not run)
/// Uses Arc to avoid expensive cloning on high QPS workloads
fn get_zone_maps(&self) -> Option<std::sync::Arc<crate::volume::zonemap::TableZoneMap>> {
None // Default implementation - override in concrete tables
}
/// Gets the segments that need to be scanned for a given predicate
///
/// Uses zone maps to determine which segments can be pruned (skipped)
/// based on the predicate's column, operator, and value.
///
/// # Arguments
/// * `column` - The column name in the predicate
/// * `operator` - The comparison operator
/// * `value` - The value being compared against
///
/// # Returns
/// Some(Vec<segment_ids>) if zone maps exist, None otherwise
fn get_segments_to_scan(
&self,
_column: &str,
_operator: radixdb_core::Operator,
_value: &Value,
) -> Option<Vec<u32>> {
None // Default implementation - override in concrete tables
}
// ---- Deferred Aggregation Methods ----
// These methods enable aggregation pushdown to avoid full row materialization.
// For `SELECT SUM(col) FROM table`, we can compute SUM directly from arena data
// without cloning any rows.
/// Compute SUM of a column without materializing rows (deferred aggregation)
///
/// Returns (sum, count_non_null) for proper NULL handling.
/// Returns None if the optimization is not available.
///
/// # Arguments
/// * `col_idx` - Column index to sum
fn sum_column(&self, _col_idx: usize) -> Option<DeferredSum> {
None // Default implementation - override in concrete tables
}
/// Compute AVG of a column without materializing rows (deferred aggregation)
///
/// Returns (sum, count_non_null) for computing average as sum/count.
/// Returns None if the optimization is not available.
///
/// # Arguments
/// * `col_idx` - Column index to average
fn avg_column(&self, _col_idx: usize) -> Option<DeferredSum> {
// Default: use sum_column if available
self.sum_column(_col_idx)
}
/// Compute MIN of a column without materializing rows (deferred aggregation)
///
/// Returns the minimum value, or None if no non-NULL values exist.
/// Returns None for the outer Option if the optimization is not available.
///
/// # Arguments
/// * `col_idx` - Column index to find minimum
fn min_column(&self, _col_idx: usize) -> Option<Option<Value>> {
None // Default implementation - override in concrete tables
}
/// Compute MAX of a column without materializing rows (deferred aggregation)
///
/// Returns the maximum value, or None if no non-NULL values exist.
/// Returns None for the outer Option if the optimization is not available.
///
/// # Arguments
/// * `col_idx` - Column index to find maximum
fn max_column(&self, _col_idx: usize) -> Option<Option<Value>> {
None // Default implementation - override in concrete tables
}
/// Compute aggregates with a WHERE filter at the storage level.
///
/// This pushes filtered aggregation (e.g. `SELECT SUM(col) FROM t WHERE x > 5`)
/// directly into the storage layer, scanning only rows that match the predicate
/// and computing aggregates without materializing full Row objects in the executor.
///
/// # Arguments
/// * `aggregates` - List of (operation, column_index) pairs
/// * `where_expr` - Storage expression representing the WHERE clause filter
///
/// # Returns
/// Some(values) with one Value per aggregate if optimization is available, None otherwise
fn compute_filtered_aggregates(
&self,
_aggregates: &[(AggregateOp, usize)],
_where_expr: &dyn Expression,
) -> Option<Vec<radixdb_core::Value>> {
None // Default: not supported
}
/// Compute grouped aggregates at the storage level.
///
/// This performs GROUP BY aggregation directly on arena storage without
/// materializing Row objects, significantly reducing memory allocations.
///
/// # Arguments
/// * `group_by_indices` - Column indices to group by
/// * `aggregates` - List of (operation, column_index) pairs
///
/// # Returns
/// Some(results) if optimization is available, None otherwise
fn compute_grouped_aggregates(
&self,
_group_by_indices: &[usize],
_aggregates: &[(AggregateOp, usize)],
) -> Option<Vec<GroupedAggregateResult>> {
None // Default: not supported
}
/// Compute grouped aggregates with a WHERE filter at the storage level.
///
/// This is the GROUP BY counterpart of `compute_filtered_aggregates`: the
/// storage layer applies the predicate, groups matching rows, and computes
/// aggregate values without forcing the executor to materialize all input
/// rows first.
///
/// # Arguments
/// * `group_by_indices` - Column indices to group by
/// * `aggregates` - List of (operation, column_index) pairs
/// * `where_expr` - Storage expression representing the WHERE clause filter
///
/// # Returns
/// Some(results) if optimization is available, None otherwise
fn compute_filtered_grouped_aggregates(
&self,
_group_by_indices: &[usize],
_aggregates: &[(AggregateOp, usize)],
_where_expr: &dyn Expression,
) -> Option<Vec<GroupedAggregateResult>> {
None // Default: not supported
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deferred_sum_preserves_values_beyond_float_integer_precision() {
let mut sum = DeferredSum::new();
sum.add_integer(9_007_199_254_740_992, 1);
sum.add_integer(1, 1);
assert_eq!(
sum.into_value().unwrap(),
Value::Integer(9_007_199_254_740_993)
);
let mut wide = DeferredSum::new();
wide.add_integer(i64::MAX as i128, 1);
wide.add_integer(1, 1);
assert_eq!(
wide.into_value().unwrap().as_decimal_parts(),
Some((i64::MAX as i128 + 1, 19, 0))
);
}
// Verify trait is object-safe
fn _assert_object_safe(_: &dyn Table) {}
#[test]
fn integer_primary_key_range_normalizes_bounds_and_exactness() {
let lower = Value::Integer(3);
let upper = Value::Integer(7);
let comparisons = [
("events.id", Operator::Gte, &lower),
("id", Operator::Lt, &upper),
];
let range = IntegerPrimaryKeyRange::from_conjunctive_comparisons(&comparisons, "id", true)
.expect("conjunctive PK range");
assert!(range.contains(3));
assert!(range.contains(6));
assert!(!range.contains(7));
assert_eq!(range.slice_bounds(&[1, 3, 4, 6, 7, 9]), (1, 4));
let other = Value::Integer(1);
let mixed = [
("id", Operator::Eq, &lower),
("status", Operator::Eq, &other),
];
assert!(IntegerPrimaryKeyRange::from_conjunctive_comparisons(&mixed, "id", true).is_none());
assert!(
IntegerPrimaryKeyRange::from_conjunctive_comparisons(&mixed, "id", false).is_some()
);
let fractional = Value::Float(3.5);
let fractional_comparison = [("id", Operator::Eq, &fractional)];
assert!(IntegerPrimaryKeyRange::from_conjunctive_comparisons(
&fractional_comparison,
"id",
true,
)
.is_none());
let not_equal = [("id", Operator::Ne, &lower)];
assert!(
IntegerPrimaryKeyRange::from_conjunctive_comparisons(¬_equal, "id", true).is_none()
);
}
#[test]
fn integer_primary_key_range_marks_contradictions_empty() {
let lower = Value::Integer(9);
let upper = Value::Integer(9);
let comparisons = [("id", Operator::Gt, &lower), ("id", Operator::Lte, &upper)];
let range = IntegerPrimaryKeyRange::from_conjunctive_comparisons(&comparisons, "id", true)
.expect("supported comparisons");
assert!(range.is_empty());
assert_eq!(range.slice_bounds(&[1, 9, 10]), (0, 0));
}
#[test]
fn integer_primary_key_range_handles_i64_edges_without_overflow() {
let min = Value::Integer(i64::MIN);
let max = Value::Integer(i64::MAX);
let row_ids = [i64::MIN, -1, 0, i64::MAX];
let all_comparisons = [("id", Operator::Gte, &min), ("id", Operator::Lte, &max)];
let all =
IntegerPrimaryKeyRange::from_conjunctive_comparisons(&all_comparisons, "id", true)
.unwrap();
assert_eq!(all.slice_bounds(&row_ids), (0, row_ids.len()));
let above_max = [("id", Operator::Gt, &max)];
let above_max =
IntegerPrimaryKeyRange::from_conjunctive_comparisons(&above_max, "id", true).unwrap();
assert_eq!(
above_max.slice_bounds(&row_ids),
(row_ids.len(), row_ids.len())
);
let below_min = [("id", Operator::Lt, &min)];
let below_min =
IntegerPrimaryKeyRange::from_conjunctive_comparisons(&below_min, "id", true).unwrap();
assert_eq!(below_min.slice_bounds(&row_ids), (0, 0));
}
// ScanPlan Display tests
#[test]
fn test_seq_scan_display_without_filter() {
let plan = ScanPlan::SeqScan {
table: "users".to_string(),
filter: None,
};
let display = format!("{}", plan);
assert_eq!(display, "Seq Scan on users");
}
#[test]
fn test_seq_scan_display_with_filter() {
let plan = ScanPlan::SeqScan {
table: "orders".to_string(),
filter: Some("amount > 100".to_string()),
};
let display = format!("{}", plan);
assert!(display.contains("Seq Scan on orders"));
assert!(display.contains("Filter: amount > 100"));
}
#[test]
fn test_parallel_seq_scan_display_without_filter() {
let plan = ScanPlan::ParallelSeqScan {
table: "products".to_string(),
filter: None,
workers: 4,
};
let display = format!("{}", plan);
assert_eq!(display, "Parallel Seq Scan on products (workers=4)");
}
#[test]
fn test_parallel_seq_scan_display_with_filter() {
let plan = ScanPlan::ParallelSeqScan {
table: "items".to_string(),
filter: Some("price < 50".to_string()),
workers: 8,
};
let display = format!("{}", plan);
assert!(display.contains("Parallel Seq Scan on items (workers=8)"));
assert!(display.contains("Filter: price < 50"));
}
#[test]
fn test_pk_lookup_display() {
let plan = ScanPlan::PkLookup {
table: "users".to_string(),
pk_column: "id".to_string(),
pk_value: "42".to_string(),
};
let display = format!("{}", plan);
assert!(display.contains("PK Lookup on users"));
assert!(display.contains("id = 42"));
}
#[test]
fn test_index_scan_display() {
let plan = ScanPlan::IndexScan {
table: "orders".to_string(),
index_name: "idx_customer_id".to_string(),
column: "customer_id".to_string(),
condition: "= 123".to_string(),
filter: None,
};
let display = format!("{}", plan);
assert!(display.contains("Index Scan using idx_customer_id on orders"));
assert!(display.contains("Index Cond: customer_id = 123"));
}
#[test]
fn test_multi_index_scan_display_and() {
let plan = ScanPlan::MultiIndexScan {
table: "products".to_string(),
indexes: vec![
(
"idx_category".to_string(),
"category".to_string(),
"= 'electronics'".to_string(),
),
(
"idx_price".to_string(),
"price".to_string(),
"> 100".to_string(),
),
],
operation: "AND".to_string(),
filter: None,
};
let display = format!("{}", plan);
assert!(display.contains("Multi-Index Scan on products (AND)"));
assert!(display.contains("idx_category on category: = 'electronics'"));
assert!(display.contains("idx_price on price: > 100"));
}
#[test]
fn test_multi_index_scan_display_or() {
let plan = ScanPlan::MultiIndexScan {
table: "items".to_string(),
indexes: vec![
("idx_a".to_string(), "col_a".to_string(), "= 1".to_string()),
("idx_b".to_string(), "col_b".to_string(), "= 2".to_string()),
],
operation: "OR".to_string(),
filter: None,
};
let display = format!("{}", plan);
assert!(display.contains("Multi-Index Scan on items (OR)"));
}
#[test]
fn test_composite_index_scan_display() {
let plan = ScanPlan::CompositeIndexScan {
table: "orders".to_string(),
index_name: "idx_cust_date".to_string(),
columns: vec!["customer_id".to_string(), "order_date".to_string()],
conditions: vec!["= 100".to_string(), "> '2024-01-01'".to_string()],
filter: None,
};
let display = format!("{}", plan);
assert!(display.contains("Composite Index Scan using idx_cust_date on orders"));
assert!(display.contains("Columns: (customer_id, order_date)"));
assert!(display.contains("customer_id = 100"));
assert!(display.contains("order_date > '2024-01-01'"));
}
#[test]
fn test_scan_plan_debug() {
let plan = ScanPlan::SeqScan {
table: "test".to_string(),
filter: Some("x > 1".to_string()),
};
let debug = format!("{:?}", plan);
assert!(debug.contains("SeqScan"));
assert!(debug.contains("test"));
}
#[test]
fn test_scan_plan_clone() {
let plan = ScanPlan::PkLookup {
table: "users".to_string(),
pk_column: "id".to_string(),
pk_value: "1".to_string(),
};
let cloned = plan.clone();
match cloned {
ScanPlan::PkLookup {
table,
pk_column,
pk_value,
} => {
assert_eq!(table, "users");
assert_eq!(pk_column, "id");
assert_eq!(pk_value, "1");
}
_ => panic!("Expected PkLookup"),
}
}
#[test]
fn test_multi_index_scan_empty_indexes() {
let plan = ScanPlan::MultiIndexScan {
table: "empty".to_string(),
indexes: vec![],
operation: "AND".to_string(),
filter: None,
};
let display = format!("{}", plan);
assert!(display.contains("Multi-Index Scan on empty (AND)"));
}
#[test]
fn test_composite_index_scan_single_column() {
let plan = ScanPlan::CompositeIndexScan {
table: "single".to_string(),
index_name: "idx_single".to_string(),
columns: vec!["id".to_string()],
conditions: vec!["= 1".to_string()],
filter: None,
};
let display = format!("{}", plan);
assert!(display.contains("Composite Index Scan using idx_single on single"));
assert!(display.contains("Columns: (id)"));
assert!(display.contains("id = 1"));
}
#[test]
fn test_parallel_seq_scan_single_worker() {
let plan = ScanPlan::ParallelSeqScan {
table: "small".to_string(),
filter: None,
workers: 1,
};
let display = format!("{}", plan);
assert_eq!(display, "Parallel Seq Scan on small (workers=1)");
}
#[test]
fn test_segmented_scan_stable_access_path_cold_artifact() {
let plan = ScanPlan::SegmentedScan {
table: "events".to_string(),
filter: None,
cold_segments: 2,
cold_rows_hint: 100,
cold_row_groups_hint: 4,
cold_selected_segments: 2,
cold_selected_rows_hint: 100,
cold_selected_row_groups_hint: 4,
cold_metadata_pruned_segments: 0,
cold_metadata_pruned_rows_hint: 0,
hot_rows_hint: 0,
};
assert_eq!(plan.access_path_id(), "scan.cold_artifact");
let lines = plan.stable_explain_lines();
assert!(lines.contains(&"Access Path: scan.cold_artifact".to_string()));
assert!(lines.contains(&"Access Source: cold(artifact_blocks)+hot".to_string()));
assert!(
lines.contains(&"Cold Metadata Path: zone_map+bloom+row_group_metadata".to_string())
);
assert!(lines.contains(&"Cold Segments: 2".to_string()));
assert!(lines.contains(&"Cold Rows Hint: 100".to_string()));
assert!(lines.contains(&"Cold Row Groups Hint: 4".to_string()));
assert!(lines.contains(&"Cold Metadata Selected Segments: 2".to_string()));
assert!(lines.contains(&"Cold Metadata Selected Rows Hint: 100".to_string()));
assert!(lines.contains(&"Cold Metadata Pruned Segments: 0".to_string()));
assert!(lines.contains(&"Hot Rows Hint: 0".to_string()));
}
#[test]
fn test_segmented_scan_stable_access_path_mixed_artifact_hot() {
let plan = ScanPlan::SegmentedScan {
table: "events".to_string(),
filter: Some("id > 10".to_string()),
cold_segments: 1,
cold_rows_hint: 50,
cold_row_groups_hint: 1,
cold_selected_segments: 1,
cold_selected_rows_hint: 50,
cold_selected_row_groups_hint: 1,
cold_metadata_pruned_segments: 0,
cold_metadata_pruned_rows_hint: 0,
hot_rows_hint: 3,
};
assert_eq!(plan.access_path_id(), "scan.mixed_cold_artifact_hot");
let lines = plan.stable_explain_lines();
assert!(lines.contains(&"Access Path: scan.mixed_cold_artifact_hot".to_string()));
assert!(
lines.contains(&"Access Filter: cold_scan_predicate+hot_snapshot_residual".to_string())
);
let display = format!("{}", plan);
assert!(display.contains("Segmented Scan on events"));
assert!(display.contains("Filter: id > 10"));
}
}