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
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
use super::*;
use crate::auth::column_policy_gate::ColumnAccessRequest;
use crate::auth::UserId;
use crate::replication::cdc::ChangeRecord;
use crate::storage::query::ast::TableSource;
// Authorization surface moved to `super::authz` (issue #1622). Re-export the
// free IAM/policy-column helpers so this module's remaining dispatch code
// (and the `impl_core::decision_to_strings` path used by
// `server::handlers_iam_policy`) keeps calling them unqualified.
pub(crate) use super::authz::policy_columns::*;
// Graph inline-TVF/analytics moved to `super::graph_tvf` (issue #1625) and the
// RLS injection family to `super::rls_injection`. Re-export the free fns still
// referenced from this module (and via `crate::runtime::impl_core::…` paths by
// sibling files) so those call sites need no edits.
pub(crate) use super::graph_tvf::{abstract_degree_centrality, is_graph_tvf_name};
pub(crate) use super::rls_injection::{
apply_foreign_table_filters, inject_rls_filters, inject_rls_into_join, rls_is_enabled,
rls_policy_filter, rls_policy_filter_for_kind,
};
// VCS command parse/execute moved to `super::vcs_command` (issue #1626). The
// execute methods are inherent `pub(crate)` methods resolved via `self`; the
// free parse helpers still called from the central dispatch (and by
// `collections_referenced` here) are re-exported so those call sites need no
// edits.
pub(crate) use super::vcs_command::{
parse_runtime_vcs_command, strip_explain_prefix, walk_collections,
};
// Ranking / metrics / SLO / analytics-source DDL moved to
// `super::impl_ranking` (issue #1627). Execute methods are inherent
// `pub(crate)` methods resolved via `self`; no call-site edits needed.
// Config / secret / KV resolution moved to `super::impl_config_secret` and the
// query-audit + control-event families to `super::impl_audit_control`
// (issue #1628). Methods are inherent `pub(crate)` methods resolved via `self`;
// the one free fn still called via a `crate::runtime::impl_core::…` path
// (`control_event_outcome_for_error`, from `impl_backup`) is re-exported so that
// call site needs no edit. The dispatch here also still calls the three
// audit-planning free fns, so they are pulled back into scope unqualified.
pub(crate) use super::impl_audit_control::{
control_event_outcome_for_error, query_audit_plan, query_control_event_specs,
};
// Config/secret free helpers still called unqualified from the dispatch here.
pub(crate) use super::impl_config_secret::{
insert_config_json_path, secret_sql_value_to_string, seed_storage_deploy_config,
show_config_json_result, show_secrets_allows_key,
};
pub(super) use super::impl_core_outer_scope::{
first_column_values, query_references_outer_scope, relation_scopes_for_query,
};
pub(crate) use super::impl_core_result_cache_scopes::collect_table_refs;
pub(super) use super::impl_core_result_cache_scopes::query_expr_result_cache_scopes;
// Bootstrap / constructor / handle accessors moved to `super::impl_lifecycle`,
// telemetry / gates / limits / shutdown to `super::impl_telemetry_accessors`,
// and catalog / stats / maintenance to `super::impl_catalog_accessors`
// (issue #1629). Methods are inherent `pub(crate)`/`pub` methods resolved via
// `self`; the two moved free fns still called unqualified from the dispatch
// here (`view_records_to_entities`) and the runtime-index rehydration paths
// (`table_row_index_fields`) are re-exported so those call sites need no edit.
pub(crate) use super::impl_lifecycle::{table_row_index_fields, view_records_to_entities};
pub use super::execution_context::{
capture_current_snapshot, clear_current_auth_identity, clear_current_connection_id,
clear_current_snapshot, clear_current_tenant, current_auth_identity_for_audit,
current_connection_id, current_tenant, entity_visible_under_current_snapshot,
entity_visible_with_context, set_current_auth_identity, set_current_connection_id,
set_current_snapshot, set_current_tenant, snapshot_bundle, with_snapshot_bundle,
SnapshotBundle, SnapshotContext,
};
pub(crate) use super::execution_context::{
config_read_permitted, current_auth_identity, current_config_value, current_kv_value,
current_role_projected, current_scope_override, current_secret_value,
current_snapshot_requires_index_fallback, current_user_projected, has_scope_override_active,
kv_read_permitted, parse_set_local_tenant, update_current_config_value,
update_current_kv_value, update_current_secret_value, xids_visible_under_current_snapshot,
ConfigSnapshotGuard, CurrentSnapshotGuard, KvStoreGuard, ScopeOverrideGuard, SecretStoreGuard,
TxLocalTenantGuard,
};
/// Cheap prefix check for a leading `WITH` keyword. Used to gate the
/// CTE-aware parse in `execute_query` without paying for a full
/// lexer pass on every statement. Treats `WITHIN` as not-a-CTE so
/// `WITHIN TENANT '...' SELECT ...` doesn't mis-route.
pub(super) fn has_with_prefix(sql: &str) -> bool {
let trimmed = sql.trim_start();
let head_end = trimmed
.find(|c: char| c.is_whitespace() || c == '(')
.unwrap_or(trimmed.len());
trimmed[..head_end].eq_ignore_ascii_case("WITH")
}
/// Same as `peek_top_level_as_of` but also returns the table name
/// targeted by the AS OF clause (when the FROM clause names a
/// concrete table). `None` for the table slot means scalar SELECT
/// or a subquery source — callers treat those as "no enforcement".
pub(super) fn peek_top_level_as_of_with_table(
sql: &str,
) -> Option<(crate::application::vcs::AsOfSpec, Option<String>)> {
if !sql
.as_bytes()
.windows(5)
.any(|w| w.eq_ignore_ascii_case(b"as of"))
{
return None;
}
let parsed = crate::storage::query::parser::parse(sql).ok()?;
let crate::storage::query::ast::QueryExpr::Table(table) = parsed.query else {
return None;
};
let clause = table.as_of?;
let table_name = if table.table.is_empty() || table.table == "any" {
None
} else {
Some(table.table.clone())
};
let spec = match clause {
crate::storage::query::ast::AsOfClause::Commit(h) => {
crate::application::vcs::AsOfSpec::Commit(h)
}
crate::storage::query::ast::AsOfClause::Branch(b) => {
crate::application::vcs::AsOfSpec::Branch(b)
}
crate::storage::query::ast::AsOfClause::Tag(t) => crate::application::vcs::AsOfSpec::Tag(t),
crate::storage::query::ast::AsOfClause::TimestampMs(ts) => {
crate::application::vcs::AsOfSpec::TimestampMs(ts)
}
crate::storage::query::ast::AsOfClause::Snapshot(x) => {
crate::application::vcs::AsOfSpec::Snapshot(x)
}
};
Some((spec, table_name))
}
pub(super) fn query_has_volatile_builtin(sql: &str) -> bool {
// Lowercase the bytes up to the first null/newline into a small
// stack buffer for cheap contains() checks. Most SQL fits in the
// buffer; longer queries fall back to owned lowercase.
const VOLATILE_TOKENS: &[&str] = &[
"pg_advisory_lock",
"pg_try_advisory_lock",
"pg_advisory_unlock",
"random()",
// `$config.<path>` / `$secret.<path>` resolve mutable runtime config /
// vault state at execution time (#1370). A cached result would serve a
// stale value after a later `SET CONFIG` / `SET SECRET`, so treat any
// query referencing them as volatile (never result-cache it).
"$config",
"$secret",
"$kv",
// NOW() / CURRENT_TIMESTAMP / CURRENT_DATE intentionally
// omitted for now — they ARE volatile but today's tests rely
// on caching them. Revisit once a tighter volatility story
// lands.
];
let lowered = sql.to_ascii_lowercase();
VOLATILE_TOKENS.iter().any(|t| lowered.contains(t))
}
pub(super) fn query_is_ask_statement(sql: &str) -> bool {
let trimmed = sql.trim_start();
let head_end = trimmed
.find(|c: char| c.is_whitespace() || c == '(' || c == ';')
.unwrap_or(trimmed.len());
trimmed[..head_end].eq_ignore_ascii_case("ASK")
}
/// Pick the `(global_mode, collection_mode)` pair for an expression,
/// or `None` for variants that opt out of intent-locking entirely
/// (admin statements like `SHOW CONFIG`, transaction control, tenant
/// toggles).
///
/// Phase-1 contract:
/// - Reads — `(IX-compatible) (Global, IS) → (Collection, IS)`
/// - Writes — `(IX-compatible) (Global, IX) → (Collection, IX)`
/// - DDL — `(strong) (Global, IX) → (Collection, X)`
pub(super) fn intent_lock_modes_for(
expr: &QueryExpr,
) -> Option<(
crate::runtime::lock_manager::LockMode,
crate::runtime::lock_manager::LockMode,
)> {
use crate::runtime::lock_manager::LockMode::{Exclusive, IntentExclusive, IntentShared};
match expr {
// Reads — IS / IS.
QueryExpr::Table(_)
| QueryExpr::Join(_)
| QueryExpr::Vector(_)
| QueryExpr::Hybrid(_)
| QueryExpr::Graph(_)
| QueryExpr::Path(_)
| QueryExpr::Ask(_)
| QueryExpr::SearchCommand(_)
| QueryExpr::GraphCommand(_)
| QueryExpr::RankOf(_)
| QueryExpr::ApproxRankOf(_)
| QueryExpr::RankRange(_)
| QueryExpr::QueueSelect(_) => Some((IntentShared, IntentShared)),
// Writes — IX / IX. Non-tabular mutations (vector insert,
// graph node insert, queue push, timeseries point insert)
// don't carry their own dispatch arm here; they ride through
// the Insert variant or a command variant covered by the
// read-side arm above. P1.T4 expands only the TableQuery-ish
// writes; non-tabular kinds inherit when their DML variants
// land in later phases.
QueryExpr::Insert(_)
| QueryExpr::Update(_)
| QueryExpr::Delete(_)
| QueryExpr::QueueCommand(QueueCommand::Move { .. }) => {
Some((IntentExclusive, IntentExclusive))
}
QueryExpr::QueueCommand(_) => Some((IntentShared, IntentShared)),
// DDL — IX / X. A DDL against collection `c` blocks all
// other writers + readers on `c` but leaves other collections
// running (because Global stays IX, not X).
QueryExpr::CreateTable(_)
| QueryExpr::CreateCollection(_)
| QueryExpr::CreateVector(_)
| QueryExpr::DropTable(_)
| QueryExpr::DropGraph(_)
| QueryExpr::DropVector(_)
| QueryExpr::DropDocument(_)
| QueryExpr::DropKv(_)
| QueryExpr::DropCollection(_)
| QueryExpr::Truncate(_)
| QueryExpr::AlterTable(_)
| QueryExpr::CreateIndex(_)
| QueryExpr::DropIndex(_)
| QueryExpr::CreateTimeSeries(_)
| QueryExpr::CreateMetric(_)
| QueryExpr::AlterMetric(_)
| QueryExpr::CreateSlo(_)
| QueryExpr::DropTimeSeries(_)
| QueryExpr::CreateQueue(_)
| QueryExpr::AlterQueue(_)
| QueryExpr::DropQueue(_)
| QueryExpr::CreateTree(_)
| QueryExpr::DropTree(_)
| QueryExpr::CreatePolicy(_)
| QueryExpr::DropPolicy(_)
| QueryExpr::CreateView(_)
| QueryExpr::DropView(_)
| QueryExpr::RefreshMaterializedView(_)
| QueryExpr::CreateSchema(_)
| QueryExpr::DropSchema(_)
| QueryExpr::CreateSequence(_)
| QueryExpr::DropSequence(_)
| QueryExpr::CreateServer(_)
| QueryExpr::DropServer(_)
| QueryExpr::CreateForeignTable(_)
| QueryExpr::DropForeignTable(_) => Some((IntentExclusive, Exclusive)),
// Admin / control — skip intent locks. `SET TENANT`,
// `BEGIN / COMMIT / ROLLBACK`, `SET CONFIG`, `SHOW CONFIG`,
// `VACUUM`, etc. don't touch collection data the same way
// and the existing transaction layer already serialises the
// pieces that matter.
_ => None,
}
}
/// Best-effort collection inventory for an expression. Used to pick
/// `Collection(...)` resources for the intent-lock guard. Overshoots
/// are fine (take an extra IS, benign); undershoots leak writes past
/// DDL X locks, so err on the side of listing more names.
pub(super) fn collections_referenced(expr: &QueryExpr) -> Vec<String> {
let mut out = Vec::new();
walk_collections(expr, &mut out);
out.sort();
out.dedup();
out
}
impl RedDBRuntime {
/// Execute a query under a typed scope override without embedding
/// the tenant / user / role values into the SQL string. Use this
/// from transport middleware (HTTP / gRPC / worker loops) where the
/// scope is resolved from auth claims and the SQL is a parameterised
/// template — avoids the string-concat injection risk of building
/// `WITHIN TENANT '<id>' …` manually, and is drop-in compatible with
/// prepared statements that didn't know about tenancy.
///
/// Precedence matches the `WITHIN` clause: the passed `scope`
/// overrides `SET LOCAL TENANT`, which overrides `SET TENANT`.
/// The override is pushed on the thread-local scope stack for the
/// duration of the call and popped on return — pool-shared
/// connections cannot leak it across requests.
pub fn execute_query_with_scope(
&self,
query: &str,
scope: crate::runtime::within_clause::ScopeOverride,
) -> RedDBResult<RuntimeQueryResult> {
if scope.is_empty() {
return self.execute_query(query);
}
let _scope_guard = ScopeOverrideGuard::install(scope);
self.execute_query(query)
}
/// Issue #205 — single lifecycle exit for slow-query logging.
///
/// `execute_query_inner` does the real work; this wrapper times it
/// and, if elapsed exceeds the configured threshold, hands the
/// triple `(QueryKind, elapsed_ms, sql_redacted, scope)` to the
/// SlowQueryLogger. The threshold + sample_pct were captured at
/// SlowQueryLogger construction (runtime startup), so the per-call
/// cost on below-threshold paths is one relaxed atomic load.
pub fn execute_query(&self, query: &str) -> RedDBResult<RuntimeQueryResult> {
let started = std::time::Instant::now();
self.inner.node_load_telemetry.query_start();
let result = self.execute_query_inner(query);
self.finish_query_lifecycle(query, started, result)
}
/// Execute a SQL statement with already-decoded positional bind
/// parameters. Transports should call this instead of parsing +
/// binding on their side and then reaching for `execute_query_expr`:
/// this entry keeps parameterized statements inside the same
/// statement lifecycle as textual SQL (snapshot guard, config/secret
/// guards, coarse auth, intent locks, slow-query logging, integrity
/// tombstone filtering, and causal bookmarks).
pub fn execute_query_with_params(
&self,
query: &str,
params: &[Value],
) -> RedDBResult<RuntimeQueryResult> {
if params.is_empty() {
return self.execute_query(query);
}
let started = std::time::Instant::now();
self.inner.node_load_telemetry.query_start();
let result = self.execute_query_with_params_inner(query, params);
self.finish_query_lifecycle(query, started, result)
}
fn finish_query_lifecycle(
&self,
query: &str,
started: std::time::Instant,
mut result: RedDBResult<RuntimeQueryResult>,
) -> RedDBResult<RuntimeQueryResult> {
// Issue #765 / S6 — filter integrity-tombstoned rows out of SELECT
// results before they reach any consumer. Fast no-op (one relaxed
// atomic load) unless an input-stream digest mismatch has tombstoned
// a RID range on this store.
if let Ok(ref mut query_result) = result {
if query_result.statement_type == "select" {
self.filter_integrity_tombstoned(&mut query_result.result);
}
}
let elapsed_ms = started.elapsed().as_millis() as u64;
// Build EffectiveScope from the same thread-locals frame-build
// consults — keeps the slow-log row consistent with the audit /
// RLS view of "this statement". `ai_scope()` is the canonical
// builder.
let scope = self.ai_scope();
let kind = match result
.as_ref()
.map(|r| r.statement_type)
.unwrap_or("select")
{
"select" => crate::telemetry::slow_query_logger::QueryKind::Select,
"insert" => crate::telemetry::slow_query_logger::QueryKind::Insert,
"update" => crate::telemetry::slow_query_logger::QueryKind::Update,
"delete" => crate::telemetry::slow_query_logger::QueryKind::Delete,
_ => crate::telemetry::slow_query_logger::QueryKind::Internal,
};
// SQL redaction: pass the raw query through. The slow-query
// logger writes structured JSON so embedded literals stay
// escape-safe at the JSON boundary (proven by
// `adversarial_sql_is_escape_safe` in slow_query_logger.rs).
// PII redaction (e.g. literal masking) is a follow-up.
self.inner
.slow_query_logger
.record(kind, elapsed_ms, query.to_string(), &scope);
// Issue #1241 — record latency into the bounded per-`kind`
// histogram substrate (always, not only above the slow-query
// threshold). `started.elapsed()` is re-read here for sub-ms
// resolution; the cost is one `Instant::now` plus a handful of
// relaxed atomic adds (see `query_latency_telemetry` docs).
self.inner
.query_latency_telemetry
.observe(kind, started.elapsed().as_secs_f64());
// Issue #1245 — decrement the active-query gauge. One relaxed
// atomic sub; the matching increment happened at execute_query /
// execute_query_with_params entry.
self.inner.node_load_telemetry.query_finish();
if let Ok(ref mut query_result) = result {
if matches!(query_result.statement_type, "insert" | "update" | "delete") {
let bookmark = crate::replication::CausalBookmark::new(
self.current_replication_term(),
self.cdc_current_lsn(),
);
query_result.bookmark = Some(bookmark.encode());
}
}
result
}
fn execute_query_with_params_inner(
&self,
query: &str,
params: &[Value],
) -> RedDBResult<RuntimeQueryResult> {
let parsed = parse_multi(query).map_err(|err| RedDBError::Query(err.to_string()))?;
let bound = crate::storage::query::user_params::bind(&parsed, params).map_err(|err| {
RedDBError::Validation {
message: err.to_string(),
validation: crate::json!({
"code": "INVALID_PARAMS",
"surface": "query.params",
}),
}
})?;
self.execute_bound_query_expr_in_frame(query, bound)
}
fn execute_bound_query_expr_in_frame(
&self,
query: &str,
expr: QueryExpr,
) -> RedDBResult<RuntimeQueryResult> {
let rewritten_query = super::red_schema::rewrite_virtual_names(query);
let execution_query = rewritten_query.as_deref().unwrap_or(query);
let frame = super::statement_frame::StatementExecutionFrame::build(self, execution_query)?;
// Box the guards so the ~640-byte StatementFrameGuards struct lives on
// the heap rather than the call stack — important for recursive paths
// (view refresh, nested queries) where the stack can be as small as 2 MB.
let _frame_guards = Box::new(frame.install(self));
let _log_span = crate::telemetry::span::query_span(query).entered();
let expr = self.rewrite_view_refs(expr);
let mode = detect_mode(execution_query);
let control_event_specs = query_control_event_specs(&expr);
let _lock_guard = match frame.prepare_dispatch(self, &expr) {
Ok(guard) => guard,
Err(err) => {
let outcome = control_event_outcome_for_error(&err);
for spec in &control_event_specs {
self.emit_control_event(
spec.kind,
outcome,
spec.action,
spec.resource.clone(),
Some(err.to_string()),
spec.fields.clone(),
)?;
}
return Err(err);
}
};
let mut result = self.dispatch_expr(expr, query, mode)?;
if result.statement_type == "select" {
self.apply_secret_decryption(&mut result);
}
Ok(result)
}
pub fn causal_session(&self) -> crate::runtime::CausalSession {
crate::runtime::CausalSession {
runtime: self.clone(),
bookmark: None,
wait_timeout: std::time::Duration::from_secs(5),
}
}
pub fn wait_for_bookmark(
&self,
bookmark: &crate::replication::CausalBookmark,
timeout: std::time::Duration,
) -> RedDBResult<()> {
let deadline = std::time::Instant::now() + timeout;
loop {
let applied_lsn = self.local_contiguous_applied_lsn();
if applied_lsn >= bookmark.commit_lsn() {
return Ok(());
}
let now = std::time::Instant::now();
if now >= deadline {
return Err(RedDBError::InvalidOperation(format!(
"timed out waiting for causal bookmark lsn {}; applied={}",
bookmark.commit_lsn(),
applied_lsn
)));
}
let remaining = deadline.saturating_duration_since(now);
std::thread::sleep(remaining.min(std::time::Duration::from_millis(5)));
}
}
fn local_contiguous_applied_lsn(&self) -> u64 {
match self.inner.db.options().replication.role {
crate::replication::ReplicationRole::Replica { .. } => {
self.config_u64("red.replication.last_applied_lsn", 0)
}
_ => self.cdc_current_lsn(),
}
}
#[inline(never)]
pub(super) fn execute_query_inner(&self, query: &str) -> RedDBResult<RuntimeQueryResult> {
// ── ULTRA-TURBO: autocommit `SELECT * FROM t WHERE _entity_id = N` ──
//
// Moved above every boot-cost the normal path pays (WITHIN
// strip, SET LOCAL parse, tx_local_tenants read, snapshot
// guard, tracing span, tx_contexts read) because the bench's
// `select_point` scenario was observed at 28× vs PostgreSQL —
// the dominant cost wasn't the entity fetch but the ceremony
// before it. Only fires when there's no ambient transaction
// context or WITHIN override, so the snapshot install we skip
// truly is a no-op for this query.
if !has_scope_override_active()
&& !query.trim_start().starts_with("WITHIN")
&& !query.trim_start().starts_with("within")
&& !self.inner.query_audit.has_rules()
&& !self
.inner
.tx_contexts
.read()
.contains_key(¤t_connection_id())
{
if let Some(result) = self.try_fast_entity_lookup(query) {
return result;
}
}
// `WITHIN TENANT '<id>' [USER '<u>'] [AS ROLE '<r>'] <stmt>` —
// strip the prefix, push a stack-scoped override, recurse on
// the inner statement, pop on return. Stack lives in a
// thread-local but is balanced by the RAII guard, so a
// pool-shared connection cannot leak the override across
// requests and an early `?` return still pops cleanly.
match crate::runtime::within_clause::try_strip_within_prefix(query) {
Ok(Some((scope, inner))) => {
let _scope_guard = ScopeOverrideGuard::install(scope);
// Re-enter the inner path, NOT `execute_query`, so the
// slow-query lifecycle hook records exactly one row per
// top-level statement (the WITHIN-stripped form would
// double-record).
return self.execute_query_inner(inner);
}
Ok(None) => {}
Err(msg) => return Err(RedDBError::Query(msg)),
}
// `EXPLAIN ANALYZE <dml>` — paid truth tier. Executes the
// mutating statement inside a transaction that is always
// rolled back, then reports the actual affected row count.
if let Some(inner) = strip_explain_analyze_prefix(query) {
return self.explain_analyze_as_rows(query, inner);
}
// `EXPLAIN <stmt>` — introspection. Runs the planner on the
// inner statement (WITHOUT executing it) and returns the
// CanonicalLogicalNode tree as rows so the caller can see the
// operator shape and estimated cost. `EXPLAIN ALTER FOR ...`
// is a distinct schema-diff command and continues down the
// regular SQL path.
if let Some(inner) = strip_explain_prefix(query) {
return self.explain_as_rows(query, inner);
}
// `SET LOCAL TENANT '<id>'` — write the per-transaction tenant
// override and return. Outside a transaction the statement is
// an error (matches PG semantics: SET LOCAL only takes effect
// within an active transaction).
if let Some(value) = parse_set_local_tenant(query)? {
let conn_id = current_connection_id();
if !self.inner.tx_contexts.read().contains_key(&conn_id) {
return Err(RedDBError::Query(
"SET LOCAL TENANT requires an active transaction".to_string(),
));
}
self.inner
.tx_local_tenants
.write()
.insert(conn_id, value.clone());
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&match &value {
Some(id) => format!("local tenant set: {id}"),
None => "local tenant cleared".to_string(),
},
"set_local_tenant",
));
}
if super::red_schema::is_system_schema_write(query) {
return Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
));
}
if let Some(command) = parse_runtime_vcs_command(query) {
return self.execute_vcs_command(query, detect_mode(query), command?);
}
if let Some(create_source) = super::analytics_source_catalog::parse_create_statement(query)?
{
return self.execute_create_analytics_source(query, create_source);
}
// Issue #790 — `READ METRIC <path>` is intentionally rejected at
// v0. The descriptor itself is readable through
// `red.analytics.metrics`; the *output* read returns a
// structured error so callers can tell "execution engine not yet
// built" apart from "metric does not exist".
if let Some(path) = super::metric_descriptor_catalog::parse_read_metric_statement(query) {
return Err(super::metric_descriptor_catalog::read_output_unsupported(
&path,
));
}
// Issue #918 / ADR 0035 — leaderboard rank capability catalog
// declarations are still recognised before the general parser.
// Rank reads themselves are parser AST nodes, including Redis-flavor
// Z* sugar that desugars to the same canonical rank shapes.
if let Some(parsed) = super::ranking_descriptor_catalog::parse_create_ranking(query) {
return self.execute_create_ranking(query, parsed?);
}
if super::ranking_descriptor_catalog::parse_show_rankings(query) {
return self.execute_show_rankings(query);
}
let rewritten_query = super::red_schema::rewrite_virtual_names(query);
let execution_query = rewritten_query.as_deref().unwrap_or(query);
let frame = super::statement_frame::StatementExecutionFrame::build(self, execution_query)?;
let _frame_guards = Box::new(frame.install(self));
// Phase 6 logging: enter a span stamped with conn_id / tenant
// / query_len. Every downstream tracing::info!/warn!/error!
// inherits these fields — no need to thread them manually
// through storage/scan layers. Entered AFTER the WITHIN /
// SET LOCAL TENANT resolution above so the span reflects the
// effective scope for this statement.
let _log_span = crate::telemetry::span::query_span(query).entered();
// ── CTE prelude (#41) — `WITH x AS (...) SELECT ... FROM x` ──
if let Some(rewritten) = frame.prepare_cte(execution_query)? {
return self.execute_query_expr(rewritten);
}
// ── TURBO: bypass SQL parse for SELECT * FROM x WHERE _entity_id = N ──
if !self.inner.query_audit.has_rules() {
if let Some(result) = self.try_fast_entity_lookup(execution_query) {
return result;
}
}
// ── Result cache: return cached result if still fresh (30s TTL) ──
if !self.inner.query_audit.has_rules() {
if let Some(result) = frame.read_result_cache(self) {
return Ok(result);
}
}
let prepared = frame.prepare_statement(self, execution_query)?;
let mode = prepared.mode;
let expr = prepared.expr;
let statement = query_expr_name(&expr);
let result_cache_scopes = query_expr_result_cache_scopes(&expr);
let control_event_specs = query_control_event_specs(&expr);
let query_audit_plan = query_audit_plan(&expr);
let _lock_guard = match frame.prepare_dispatch(self, &expr) {
Ok(guard) => guard,
Err(err) => {
let outcome = control_event_outcome_for_error(&err);
for spec in &control_event_specs {
self.emit_control_event(
spec.kind,
outcome,
spec.action,
spec.resource.clone(),
Some(err.to_string()),
spec.fields.clone(),
)?;
}
return Err(err);
}
};
let frame_iface: &dyn super::statement_frame::ReadFrame = &frame;
let query_audit_started = std::time::Instant::now();
let query_result = match expr {
QueryExpr::Graph(_) | QueryExpr::Path(_) => {
// Apply MVCC visibility + RLS gate while materialising the
// graph: every node entity is screened against the source
// collection's policy chain (basic and `Nodes`-targeted)
// and dropped when the caller's tenant / role doesn't
// admit it. Edges are pruned automatically because the
// graph builder skips edges whose endpoints aren't in
// `allowed_nodes`.
let (graph, node_properties, edge_properties) =
self.materialize_graph_with_rls()?;
let result =
crate::storage::query::unified::UnifiedExecutor::execute_on_with_graph_properties(
&graph,
&expr,
node_properties,
edge_properties,
)
.map_err(|err| RedDBError::Query(err.to_string()))?;
Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "materialized-graph",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
QueryExpr::Table(table) => {
let table = self.resolve_table_expr_subqueries(
table,
&frame as &dyn super::statement_frame::ReadFrame,
)?;
// Table-valued functions (e.g. components(g)) dispatch to a
// read-only executor before any catalog/virtual-table routing
// (issue #795).
if let Some(TableSource::Function {
name,
args,
named_args,
}) = table.source.clone()
{
// The graph-collection form is cacheable (issue #802): the
// result-cache read at the top of this function keys on the
// query string, and `result_cache_scopes` carries the graph
// collection (see `collect_table_source_scopes`) so a write
// to it invalidates the entry. Deterministic algorithm
// output is worth caching at any row count, so the write
// bypasses the generic ≤5-row payload heuristic.
let tvf_result = RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-graph-tvf",
result: self.execute_table_function(&name, &args, &named_args)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
};
frame.write_result_cache(self, &tvf_result, result_cache_scopes.clone());
return Ok(tvf_result);
}
// Inline-graph TVF (issue #799): the graph is supplied by two
// subqueries instead of a collection reference. Unlike the
// graph-collection form, the result IS cacheable — its cache
// key is the query string (the result-cache read at the top of
// `execute_query_inner` keys on it) and `result_cache_scopes`
// already carries the `nodes`/`edges` source collections, so a
// write to any of them invalidates the entry.
if let Some(TableSource::InlineGraphFunction {
name,
nodes,
edges,
named_args,
}) = table.source.clone()
{
let inline_result = RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-graph-tvf-inline",
result: self.execute_inline_graph_function(
&name,
&nodes,
&edges,
&named_args,
)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
};
frame.write_result_cache(self, &inline_result, result_cache_scopes);
return Ok(inline_result);
}
if super::red_schema::is_virtual_table(&table.table) {
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-red-schema",
result: super::red_schema::red_query(
self,
&table.table,
&table,
&frame as &dyn super::statement_frame::ReadFrame,
)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
// `<graph>.<output>` analytics virtual view (issue #800).
// Recomputed on demand — intentionally not result-cached, so it
// always reflects the current graph data.
if let Some(view_result) = self.try_resolve_analytics_view(
&table,
&frame as &dyn super::statement_frame::ReadFrame,
)? {
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-graph-analytics-view",
result: view_result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
if let Some(result) = self.execute_probabilistic_select(&table)? {
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-probabilistic",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
// Foreign-table intercept (Phase 3.2.2 PG parity).
//
// When the referenced table matches a `CREATE FOREIGN TABLE`
// registration, short-circuit into the FDW scan. Phase 3.2
// wrappers don't yet support pushdown, so filters/projections
// apply post-scan via `apply_foreign_table_filters` — good
// enough for correctness; perf work lands in 3.2.3.
if self.inner.foreign_tables.is_foreign_table(&table.table) {
let records = self
.inner
.foreign_tables
.scan(&table.table)
.map_err(|e| RedDBError::Internal(e.to_string()))?;
let result = apply_foreign_table_filters(records, &table);
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-fdw",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
// Row-Level Security enforcement (Phase 2.5.2 PG parity).
//
// When RLS is enabled on this table, fetch every policy
// that applies to the current (role, SELECT) pair and
// fold them into the query's WHERE clause: policies
// OR-combine (any of them admitting the row is enough),
// then AND into the caller's existing filter.
//
// Anonymous callers (no thread-local identity) pass
// `role = None`; policies with a specific `TO role`
// clause skip, but `TO PUBLIC` policies still apply.
//
// When `inject_rls_filters` returns `None` the table has
// RLS enabled but no policy admits the caller's role —
// short-circuit with an empty result set instead of
// synthesising a contradiction filter.
let Some(table_with_rls) = self.authorize_relational_table_select(
table,
&frame as &dyn super::statement_frame::ReadFrame,
)?
else {
let empty = crate::storage::query::unified::UnifiedResult::empty();
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-table-rls",
result: empty,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
};
Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-table",
// #885: lend the frame-owned row-buffer arena to the
// streaming path so chunk buffers are reused across
// this statement's chunk-fetches instead of allocated
// fresh per chunk. This is the table-query dispatch
// that runs under a `StatementExecutionFrame`; the
// frameless prepared/subquery paths keep `None`.
result: execute_runtime_table_query_in(
&self.inner.db,
&table_with_rls,
Some(&self.inner.index_store),
Some(frame.row_arena()),
)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
QueryExpr::Join(join) => {
// Fold per-table RLS filters into each `QueryExpr::Table`
// leaf of the join tree before executing. Without this
// the join executor scans both tables raw and ignores
// policies — a `WITHIN TENANT 'x'` against a join of
// two tenant-scoped tables would leak cross-tenant rows.
// When any leaf has RLS enabled and zero matching policy,
// short-circuit to an empty join result instead of
// emitting a contradiction filter.
let join_with_rls = match self.authorize_relational_join_select(
join,
&frame as &dyn super::statement_frame::ReadFrame,
)? {
Some(j) => j,
None => {
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-join-rls",
result: crate::storage::query::unified::UnifiedResult::empty(),
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
};
Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-join",
result: execute_runtime_join_query(&self.inner.db, &join_with_rls)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
QueryExpr::Vector(vector) => Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-vector",
result: execute_runtime_vector_query(&self.inner.db, &vector)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
}),
QueryExpr::Hybrid(hybrid) => Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement,
engine: "runtime-hybrid",
result: execute_runtime_hybrid_query(&self.inner.db, &hybrid)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
}),
QueryExpr::RankOf(ref rank) => self.execute_rank_of(query, rank),
QueryExpr::ApproxRankOf(ref rank) => self.execute_approx_rank_of(query, rank),
QueryExpr::RankRange(ref range) => self.execute_rank_range(query, range),
// DML execution
QueryExpr::Insert(ref insert) if super::red_schema::is_virtual_table(&insert.table) => {
Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
))
}
QueryExpr::Update(ref update) if super::red_schema::is_virtual_table(&update.table) => {
Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
))
}
QueryExpr::Delete(ref delete) if super::red_schema::is_virtual_table(&delete.table) => {
Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
))
}
QueryExpr::Insert(ref insert) => self
.with_deferred_store_wal_for_dml(self.insert_may_emit_events(insert), || {
self.execute_insert(query, insert)
}),
QueryExpr::Update(ref update) => self
.with_deferred_store_wal_for_dml(self.update_may_emit_events(update), || {
self.execute_update(query, update)
}),
QueryExpr::Delete(ref delete) => self
.with_deferred_store_wal_for_dml(self.delete_may_emit_events(delete), || {
self.execute_delete(query, delete)
}),
// DDL execution
QueryExpr::CreateTable(ref create) => self.execute_create_table(query, create),
QueryExpr::CreateCollection(ref create) => {
self.execute_create_collection(query, create)
}
QueryExpr::CreateVector(ref create) => self.execute_create_vector(query, create),
QueryExpr::DropTable(ref drop_tbl) => self.execute_drop_table(query, drop_tbl),
QueryExpr::DropGraph(ref drop_graph) => self.execute_drop_graph(query, drop_graph),
QueryExpr::DropVector(ref drop_vector) => self.execute_drop_vector(query, drop_vector),
QueryExpr::DropDocument(ref drop_document) => {
self.execute_drop_document(query, drop_document)
}
QueryExpr::DropKv(ref drop_kv) => self.execute_drop_kv(query, drop_kv),
QueryExpr::DropCollection(ref drop_collection) => {
self.execute_drop_collection(query, drop_collection)
}
QueryExpr::Truncate(ref truncate) => self.execute_truncate(query, truncate),
QueryExpr::AlterTable(ref alter) => self.execute_alter_table(query, alter),
QueryExpr::CreateVcsRef(ref create) => self.execute_create_vcs_ref(query, create),
QueryExpr::DropVcsRef(ref drop_ref) => self.execute_drop_vcs_ref(query, drop_ref),
QueryExpr::ForkStore(ref fork) => self.execute_fork_store(query, fork),
QueryExpr::PromoteFork(ref promote_fork) => {
self.execute_promote_fork(query, promote_fork)
}
QueryExpr::DropFork(ref drop_fork) => self.execute_drop_fork(query, drop_fork),
QueryExpr::ExplainAlter(ref explain) => self.execute_explain_alter(query, explain),
// Graph analytics commands
QueryExpr::GraphCommand(ref cmd) => self.execute_graph_command(query, cmd),
// Search commands
QueryExpr::SearchCommand(ref cmd) => self.execute_search_command(query, cmd),
// ASK: RAG query with LLM synthesis
QueryExpr::Ask(ref ask) => self.execute_ask(query, ask),
QueryExpr::CreateIndex(ref create_idx) => self.execute_create_index(query, create_idx),
QueryExpr::DropIndex(ref drop_idx) => self.execute_drop_index(query, drop_idx),
QueryExpr::ProbabilisticCommand(ref cmd) => {
self.execute_probabilistic_command(query, cmd)
}
// Time-series DDL
QueryExpr::CreateTimeSeries(ref ts) => self.execute_create_timeseries(query, ts),
QueryExpr::CreateMetric(ref metric) => self.execute_create_metric(query, metric),
QueryExpr::AlterMetric(ref alter) => self.execute_alter_metric(query, alter),
QueryExpr::CreateSlo(ref slo) => self.execute_create_slo(query, slo),
QueryExpr::DropTimeSeries(ref ts) => self.execute_drop_timeseries(query, ts),
// Queue DDL and commands
QueryExpr::CreateQueue(ref q) => self.execute_create_queue(query, q),
QueryExpr::AlterQueue(ref q) => self.execute_alter_queue(query, q),
QueryExpr::DropQueue(ref q) => self.execute_drop_queue(query, q),
QueryExpr::QueueSelect(ref q) => self.execute_queue_select(query, q),
QueryExpr::QueueCommand(ref cmd) => self
.with_deferred_store_wal_if_transaction(|| self.execute_queue_command(query, cmd)),
QueryExpr::EventsBackfill(ref backfill) => {
self.execute_events_backfill(query, backfill)
}
QueryExpr::EventsBackfillStatus { ref collection } => Err(RedDBError::Query(format!(
"EVENTS BACKFILL STATUS for '{collection}' is not implemented in this slice"
))),
QueryExpr::KvCommand(ref cmd) => self.execute_kv_command(query, cmd),
QueryExpr::ConfigCommand(ref cmd) => self.execute_config_command(query, cmd),
QueryExpr::CreateTree(ref tree) => self.execute_create_tree(query, tree),
QueryExpr::DropTree(ref tree) => self.execute_drop_tree(query, tree),
QueryExpr::TreeCommand(ref cmd) => self.execute_tree_command(query, cmd),
// SET CONFIG key = value
QueryExpr::SetConfig { ref key, ref value } => {
if key.starts_with("red.secret.") {
return Err(RedDBError::Query(
"red.secret.* is reserved for vault secrets; use SET SECRET".to_string(),
));
}
if key.starts_with("red.secrets.") {
return Err(RedDBError::Query(
"red.secrets.* is reserved for vault secrets; use SET SECRET".to_string(),
));
}
// ADR-0068 §5 clean break: reject the removed AI config keys
// (old `default.*` provider/model and per-alias base_url shape)
// with a didactic error naming the replacement key.
crate::ai::validate_ai_config_key_on_write(key)?;
match self.check_managed_config_write_for_set_config(key) {
Err(err) => Err(err),
Ok(()) => {
let store = self.inner.db.store();
let json_val = match value {
Value::Text(s) => crate::serde_json::Value::String(s.to_string()),
Value::Integer(n) => crate::serde_json::Value::Number(*n as f64),
Value::Float(n) => crate::serde_json::Value::Number(*n),
Value::Boolean(b) => crate::serde_json::Value::Bool(*b),
_ => crate::serde_json::Value::String(value.to_string()),
};
store.set_config_tree(key, &json_val);
update_current_config_value(key, value.clone());
// Config changes can flip runtime behavior mid-session
// (auto_decrypt, auto_encrypt, etc.) — invalidate the
// result cache so subsequent reads re-execute against
// the new config.
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("config set: {key}"),
"set",
))
}
}
}
// SET SECRET key = value
QueryExpr::SetSecret { ref key, ref value } => {
if key.starts_with("red.config.") {
return Err(RedDBError::Query(
"red.config.* is reserved for config; use SET CONFIG".to_string(),
));
}
let auth_store = self.inner.auth_store.read().clone().ok_or_else(|| {
RedDBError::Query("SET SECRET requires an enabled, unsealed vault".to_string())
})?;
self.check_secret_write_privilege(&auth_store, key)?;
if matches!(value, Value::Null) {
auth_store
.vault_kv_try_delete(key)
.map_err(|err| RedDBError::Query(err.to_string()))?;
update_current_secret_value(key, None);
self.invalidate_result_cache();
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("secret deleted: {key}"),
"delete_secret",
));
}
let value = secret_sql_value_to_string(value)?;
auth_store
.vault_kv_try_set(key.clone(), value.clone())
.map_err(|err| RedDBError::Query(err.to_string()))?;
update_current_secret_value(key, Some(value));
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("secret set: {key}"),
"set_secret",
))
}
// DELETE SECRET key
QueryExpr::DeleteSecret { ref key } => {
let auth_store = self.inner.auth_store.read().clone().ok_or_else(|| {
RedDBError::Query(
"DELETE SECRET requires an enabled, unsealed vault".to_string(),
)
})?;
self.check_secret_write_privilege(&auth_store, key)?;
let deleted = auth_store
.vault_kv_try_delete(key)
.map_err(|err| RedDBError::Query(err.to_string()))?;
if deleted {
update_current_secret_value(key, None);
}
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("secret deleted: {key}"),
if deleted {
"delete_secret"
} else {
"delete_secret_not_found"
},
))
}
// SET KV key = value — plain (non-encrypted) user KV entry (#1602)
QueryExpr::SetKv { ref key, ref value } => {
let auth_store = self.inner.auth_store.read().clone().ok_or_else(|| {
RedDBError::Query("SET KV requires an auth store".to_string())
})?;
self.check_kv_write_privilege(&auth_store, key)?;
// `SET KV k = NULL` deletes, mirroring `SET SECRET`.
if matches!(value, Value::Null) {
auth_store.plain_kv_delete(key);
update_current_kv_value(key, None);
self.invalidate_result_cache();
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("kv deleted: {key}"),
"delete_kv",
));
}
let value = secret_sql_value_to_string(value)?;
auth_store.plain_kv_set(key.clone(), value.clone());
update_current_kv_value(key, Some(value));
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("kv set: {key}"),
"set_kv",
))
}
// DELETE KV key
QueryExpr::DeleteKv { ref key } => {
let auth_store = self.inner.auth_store.read().clone().ok_or_else(|| {
RedDBError::Query("DELETE KV requires an auth store".to_string())
})?;
self.check_kv_write_privilege(&auth_store, key)?;
let deleted = auth_store.plain_kv_delete(key);
if deleted {
update_current_kv_value(key, None);
}
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("kv deleted: {key}"),
if deleted {
"delete_kv"
} else {
"delete_kv_not_found"
},
))
}
QueryExpr::Scrub { background, budget } => {
self.execute_scrub_query(query, mode, background, budget)
}
// SHOW SECRET[S] [prefix]
QueryExpr::ShowSecrets { ref prefix } => {
let auth_store = self.inner.auth_store.read().clone().ok_or_else(|| {
RedDBError::Query("SHOW SECRET requires an enabled, unsealed vault".to_string())
})?;
if !auth_store.is_vault_backed() {
return Err(RedDBError::Query(
"SHOW SECRET requires an enabled, unsealed vault".to_string(),
));
}
let mut keys = auth_store.vault_kv_keys();
keys.sort();
let mut result = UnifiedResult::with_columns(vec![
"key".into(),
"value".into(),
"status".into(),
]);
for key in keys {
if !show_secrets_allows_key(&key) {
continue;
}
if let Some(ref pfx) = prefix {
if !key.starts_with(pfx) {
continue;
}
}
let mut record = UnifiedRecord::new();
record.set("key", Value::text(key));
record.set("value", Value::text("***"));
record.set("status", Value::text("active"));
result.push(record);
}
Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement: "show_secrets",
engine: "runtime-secret",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
// SHOW CONFIG [prefix] [AS JSON|FORMAT JSON]
QueryExpr::ShowConfig {
ref prefix,
as_json,
} => {
let store = self.inner.db.store();
let all_collections = store.list_collections();
if !all_collections.contains(&"red_config".to_string()) {
if as_json {
return Ok(show_config_json_result(
query,
mode,
prefix,
crate::serde_json::Value::Object(crate::serde_json::Map::new()),
));
}
let result = UnifiedResult::with_columns(vec!["key".into(), "value".into()]);
return Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement: "show_config",
engine: "runtime-config",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
let manager = store
.get_collection("red_config")
.ok_or_else(|| RedDBError::NotFound("red_config".to_string()))?;
let entities = manager.query_all(|_| true);
let mut latest = std::collections::BTreeMap::<String, (u64, Value, Value)>::new();
for entity in entities {
if let EntityData::Row(ref row) = entity.data {
if let Some(ref named) = row.named {
let key_val = named.get("key").cloned().unwrap_or(Value::Null);
let val = named.get("value").cloned().unwrap_or(Value::Null);
let key_str = match &key_val {
Value::Text(s) => s.as_ref(),
_ => continue,
};
if let Some(ref pfx) = prefix {
if !key_str.starts_with(pfx.as_str()) {
continue;
}
}
let entity_id = entity.id.raw();
match latest.get(key_str) {
Some((prev_id, _, _)) if *prev_id > entity_id => {}
_ => {
latest.insert(key_str.to_string(), (entity_id, key_val, val));
}
}
}
}
}
if as_json {
let mut tree = crate::serde_json::Value::Object(crate::serde_json::Map::new());
for (key, (_, _, val)) in latest {
let relative = match prefix {
Some(pfx) if key == *pfx => "",
Some(pfx) => key
.strip_prefix(pfx.as_str())
.and_then(|tail| tail.strip_prefix('.'))
.unwrap_or(key.as_str()),
None => key.as_str(),
};
insert_config_json_path(
&mut tree,
relative,
crate::presentation::entity_json::storage_value_to_json(&val),
);
}
return Ok(show_config_json_result(query, mode, prefix, tree));
}
let mut result = UnifiedResult::with_columns(vec!["key".into(), "value".into()]);
for (_, key_val, val) in latest.into_values() {
let mut record = UnifiedRecord::new();
record.set("key", key_val);
record.set("value", val);
result.push(record);
}
Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement: "show_config",
engine: "runtime-config",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
// Session-local multi-tenancy handle (Phase 2.5.3).
//
// SET TENANT 'id' / SET TENANT NULL / RESET TENANT — writes
// the thread-local; SHOW TENANT returns it. Paired with the
// CURRENT_TENANT() scalar for use in RLS policies.
QueryExpr::SetTenant(ref value) => {
match value {
Some(id) => set_current_tenant(id.clone()),
None => clear_current_tenant(),
}
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&match value {
Some(id) => format!("tenant set: {id}"),
None => "tenant cleared".to_string(),
},
"set_tenant",
))
}
QueryExpr::ShowTenant => {
let mut result = UnifiedResult::with_columns(vec!["tenant".into()]);
let mut record = UnifiedRecord::new();
record.set(
"tenant",
current_tenant().map(Value::text).unwrap_or(Value::Null),
);
result.push(record);
Ok(RuntimeQueryResult {
query: query.to_string(),
mode,
statement: "show_tenant",
engine: "runtime-tenant",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
// Transaction control (Phase 2.3 PG parity).
//
// BEGIN allocates a real `Xid` and stores a `TxnContext` keyed by
// the current connection's id. COMMIT/ROLLBACK release it through
// the `SnapshotManager` so future snapshots see the correct set of
// active/aborted transactions.
//
// Tuple stamping (xmin/xmax) and read-path visibility filtering
// land in Phase 2.3.2 — this dispatch only manages the snapshot
// registry. Statements running outside a TxnContext still behave
// as autocommit (xid=0 → visible to every snapshot).
QueryExpr::TransactionControl(ref ctl) => {
use crate::storage::query::ast::TxnControl;
use crate::storage::transaction::snapshot::{TxnContext, Xid};
use crate::storage::transaction::IsolationLevel;
// Phase 2.3 keys transactions by a thread-local connection id.
// The stdio/gRPC paths wire a real per-connection id later;
// for embedded use (one RedDBRuntime per process-ish caller)
// we fall back to a deterministic placeholder.
let conn_id = current_connection_id();
let (kind, msg) = match ctl {
TxnControl::Begin(requested_isolation) => {
let isolation = requested_isolation
.map(IsolationLevel::from)
.unwrap_or(IsolationLevel::SnapshotIsolation);
let mgr = Arc::clone(&self.inner.snapshot_manager);
let xid = mgr.begin();
if isolation == IsolationLevel::Serializable {
mgr.begin_serializable(xid);
}
let snapshot = mgr.snapshot(xid);
let ctx = TxnContext {
xid,
isolation,
snapshot,
savepoints: Vec::new(),
released_sub_xids: Vec::new(),
};
self.inner.tx_contexts.write().insert(conn_id, ctx);
("begin", format!("BEGIN — xid={xid} (snapshot isolation)"))
}
TxnControl::Commit => {
// SET LOCAL TENANT ends with the transaction.
self.inner.tx_local_tenants.write().remove(&conn_id);
let ctx = self.inner.tx_contexts.write().remove(&conn_id);
match ctx {
Some(ctx) => {
let mut own_xids = std::collections::HashSet::new();
own_xids.insert(ctx.xid);
for (_, sub) in &ctx.savepoints {
own_xids.insert(*sub);
}
for sub in &ctx.released_sub_xids {
own_xids.insert(*sub);
}
if let Err(err) = self.check_table_row_write_conflicts(
conn_id,
&ctx.snapshot,
&own_xids,
ctx.isolation,
) {
for (_, sub) in &ctx.savepoints {
self.inner.snapshot_manager.rollback(*sub);
}
for sub in &ctx.released_sub_xids {
self.inner.snapshot_manager.rollback(*sub);
}
self.inner.snapshot_manager.rollback(ctx.xid);
self.revive_pending_versioned_updates(conn_id);
self.revive_pending_tombstones(conn_id);
self.discard_pending_kv_watch_events(conn_id);
self.discard_pending_queue_wakes(conn_id);
self.discard_pending_store_wal_actions(conn_id);
self.release_pending_claim_locks(conn_id);
return Err(err);
}
if let Err(err) = self.check_queue_dedup_write_conflicts(
conn_id,
&ctx.snapshot,
&own_xids,
) {
for (_, sub) in &ctx.savepoints {
self.inner.snapshot_manager.rollback(*sub);
}
for sub in &ctx.released_sub_xids {
self.inner.snapshot_manager.rollback(*sub);
}
self.inner.snapshot_manager.rollback(ctx.xid);
self.revive_pending_versioned_updates(conn_id);
self.revive_pending_tombstones(conn_id);
self.discard_pending_queue_dedup(conn_id);
self.discard_pending_kv_watch_events(conn_id);
self.discard_pending_queue_wakes(conn_id);
self.discard_pending_store_wal_actions(conn_id);
self.release_pending_claim_locks(conn_id);
return Err(err);
}
self.restore_pending_write_stamps(conn_id);
if let Err(err) = self.flush_pending_store_wal_actions(conn_id) {
for (_, sub) in &ctx.savepoints {
self.inner.snapshot_manager.rollback(*sub);
}
for sub in &ctx.released_sub_xids {
self.inner.snapshot_manager.rollback(*sub);
}
self.inner.snapshot_manager.rollback(ctx.xid);
self.revive_pending_versioned_updates(conn_id);
self.revive_pending_tombstones(conn_id);
self.discard_pending_queue_dedup(conn_id);
self.discard_pending_kv_watch_events(conn_id);
self.discard_pending_queue_wakes(conn_id);
self.release_pending_claim_locks(conn_id);
return Err(err);
}
// Phase 2.3.2e: commit every open sub-xid
// so they also become visible. Their
// work is promoted to the parent txn's
// result exactly like a RELEASE would
// have done.
for (_, sub) in &ctx.savepoints {
self.inner.snapshot_manager.commit(*sub);
}
for sub in &ctx.released_sub_xids {
self.inner.snapshot_manager.commit(*sub);
}
self.inner.snapshot_manager.commit(ctx.xid);
self.finalize_pending_versioned_updates(conn_id);
self.finalize_pending_tombstones(conn_id);
self.finalize_pending_queue_dedup(conn_id);
self.finalize_pending_kv_watch_events(conn_id);
self.finalize_pending_queue_wakes(conn_id);
self.release_pending_claim_locks(conn_id);
("commit", format!("COMMIT — xid={} committed", ctx.xid))
}
None => (
"commit",
"COMMIT outside transaction — no-op (autocommit)".to_string(),
),
}
}
TxnControl::Rollback => {
self.inner.tx_local_tenants.write().remove(&conn_id);
let ctx = self.inner.tx_contexts.write().remove(&conn_id);
match ctx {
Some(ctx) => {
// Phase 2.3.2e: abort every open sub-xid
// too so their writes stay hidden.
for (_, sub) in &ctx.savepoints {
self.inner.snapshot_manager.rollback(*sub);
}
for sub in &ctx.released_sub_xids {
self.inner.snapshot_manager.rollback(*sub);
}
self.inner.snapshot_manager.rollback(ctx.xid);
// Phase 2.3.2b: tuples that the txn had
// xmax-stamped become live again — wipe xmax
// back to 0 so later snapshots see them.
self.revive_pending_versioned_updates(conn_id);
self.revive_pending_tombstones(conn_id);
self.discard_pending_queue_dedup(conn_id);
self.discard_pending_kv_watch_events(conn_id);
self.discard_pending_queue_wakes(conn_id);
self.discard_pending_store_wal_actions(conn_id);
self.release_pending_claim_locks(conn_id);
("rollback", format!("ROLLBACK — xid={} aborted", ctx.xid))
}
None => (
"rollback",
"ROLLBACK outside transaction — no-op (autocommit)".to_string(),
),
}
}
// Phase 2.3.2e: savepoints map onto sub-xids. Each
// SAVEPOINT allocates a fresh xid and pushes it
// onto the per-txn stack so subsequent writes can
// be selectively rolled back. RELEASE pops without
// aborting; ROLLBACK TO aborts the sub-xid (and
// any nested ones) + revives their tombstones.
TxnControl::Savepoint(name) => {
let mgr = Arc::clone(&self.inner.snapshot_manager);
let mut guard = self.inner.tx_contexts.write();
match guard.get_mut(&conn_id) {
Some(ctx) => {
let sub = mgr.begin();
ctx.savepoints.push((name.clone(), sub));
("savepoint", format!("SAVEPOINT {name} — sub_xid={sub}"))
}
None => (
"savepoint",
"SAVEPOINT outside transaction — no-op".to_string(),
),
}
}
TxnControl::ReleaseSavepoint(name) => {
let mut guard = self.inner.tx_contexts.write();
match guard.get_mut(&conn_id) {
Some(ctx) => {
let pos = ctx
.savepoints
.iter()
.position(|(n, _)| n == name)
.ok_or_else(|| {
RedDBError::Internal(format!(
"savepoint {name} does not exist"
))
})?;
// RELEASE pops the named savepoint and
// any nested ones. Their sub-xids move
// to `released_sub_xids` so they commit
// (or roll back) alongside the parent
// xid — PG semantics: released
// savepoints still contribute their
// work, but their names are gone.
let released = ctx.savepoints.len() - pos;
let popped: Vec<Xid> = ctx
.savepoints
.split_off(pos)
.into_iter()
.map(|(_, x)| x)
.collect();
ctx.released_sub_xids.extend(popped);
(
"release_savepoint",
format!("RELEASE SAVEPOINT {name} — {released} level(s)"),
)
}
None => (
"release_savepoint",
"RELEASE outside transaction — no-op".to_string(),
),
}
}
TxnControl::RollbackToSavepoint(name) => {
let mgr = Arc::clone(&self.inner.snapshot_manager);
// Splice out the savepoint + nested ones under
// a narrow lock, then run the snapshot-manager
// + tombstone side-effects without the tx map
// held so nothing re-enters.
let drop_result: Option<(Xid, Vec<Xid>)> = {
let mut guard = self.inner.tx_contexts.write();
if let Some(ctx) = guard.get_mut(&conn_id) {
let pos = ctx
.savepoints
.iter()
.position(|(n, _)| n == name)
.ok_or_else(|| {
RedDBError::Internal(format!(
"savepoint {name} does not exist"
))
})?;
let savepoint_xid = ctx.savepoints[pos].1;
let aborted: Vec<Xid> = ctx
.savepoints
.split_off(pos)
.into_iter()
.map(|(_, x)| x)
.collect();
Some((savepoint_xid, aborted))
} else {
None
}
};
match drop_result {
Some((savepoint_xid, aborted)) => {
for x in &aborted {
mgr.rollback(*x);
}
let reverted_updates =
self.revive_versioned_updates_since(conn_id, savepoint_xid);
let revived = self.revive_tombstones_since(conn_id, savepoint_xid);
(
"rollback_to_savepoint",
format!(
"ROLLBACK TO SAVEPOINT {name} — aborted {} sub_xid(s), reverted {reverted_updates} update(s), revived {revived} tombstone(s)",
aborted.len(),
),
)
}
None => (
"rollback_to_savepoint",
"ROLLBACK TO outside transaction — no-op".to_string(),
),
}
}
};
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&msg,
kind,
))
}
// Schema + Sequence DDL (Phase 1.3 PG parity).
//
// Schemas are lightweight logical namespaces: a CREATE SCHEMA call
// just registers the name in `red_config` under `schema.{name}`.
// Table lookups still happen by collection name; clients using
// `schema.table` qualified names collapse to collection `schema.table`.
//
// Sequences persist a 64-bit counter + metadata (start, increment)
// in `red_config` under `sequence.{name}.*`. Scalar callers
// `nextval('name')` / `currval('name')` arrive with the MVCC phase
// once we have a proper mutating-function dispatch path; for now the
// DDL just establishes the catalog entry so clients don't error.
QueryExpr::CreateSchema(ref q) => {
let store = self.inner.db.store();
let key = format!("schema.{}", q.name);
if store.get_config(&key).is_some() {
if q.if_not_exists {
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("schema {} already exists — skipped", q.name),
"create_schema",
));
}
return Err(RedDBError::Internal(format!(
"schema {} already exists",
q.name
)));
}
store.set_config_tree(&key, &crate::serde_json::Value::Bool(true));
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("schema {} created", q.name),
"create_schema",
))
}
QueryExpr::DropSchema(ref q) => {
let store = self.inner.db.store();
let key = format!("schema.{}", q.name);
let existed = store.get_config(&key).is_some();
if !existed && !q.if_exists {
return Err(RedDBError::Internal(format!(
"schema {} does not exist",
q.name
)));
}
// Remove marker from red_config via set to null.
store.set_config_tree(&key, &crate::serde_json::Value::Null);
let suffix = if q.cascade {
" (CASCADE accepted — tables untouched)"
} else {
""
};
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("schema {} dropped{}", q.name, suffix),
"drop_schema",
))
}
QueryExpr::CreateSequence(ref q) => {
let store = self.inner.db.store();
let base = format!("sequence.{}", q.name);
let start_key = format!("{base}.start");
let incr_key = format!("{base}.increment");
let curr_key = format!("{base}.current");
if store.get_config(&start_key).is_some() {
if q.if_not_exists {
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("sequence {} already exists — skipped", q.name),
"create_sequence",
));
}
return Err(RedDBError::Internal(format!(
"sequence {} already exists",
q.name
)));
}
// Persist start + increment, and set current so the first
// nextval returns `start`.
let initial_current = q.start - q.increment;
store.set_config_tree(
&start_key,
&crate::serde_json::Value::Number(q.start as f64),
);
store.set_config_tree(
&incr_key,
&crate::serde_json::Value::Number(q.increment as f64),
);
store.set_config_tree(
&curr_key,
&crate::serde_json::Value::Number(initial_current as f64),
);
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!(
"sequence {} created (start={}, increment={})",
q.name, q.start, q.increment
),
"create_sequence",
))
}
QueryExpr::DropSequence(ref q) => {
let store = self.inner.db.store();
let base = format!("sequence.{}", q.name);
let existed = store.get_config(&format!("{base}.start")).is_some();
if !existed && !q.if_exists {
return Err(RedDBError::Internal(format!(
"sequence {} does not exist",
q.name
)));
}
for k in ["start", "increment", "current"] {
store.set_config_tree(&format!("{base}.{k}"), &crate::serde_json::Value::Null);
}
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("sequence {} dropped", q.name),
"drop_sequence",
))
}
// Views — CREATE [MATERIALIZED] VIEW (Phase 2.1 PG parity).
//
// The view definition is stored in-memory on RuntimeInner (not
// persisted). SELECTs that reference the view name will substitute
// the stored `QueryExpr` via `resolve_view_reference` during
// planning (same entry point used by table-name resolution).
//
// Materialized views additionally allocate a slot in
// `MaterializedViewCache`; a REFRESH repopulates that slot.
QueryExpr::CreateView(ref q) => {
let mut views = self.inner.views.write();
if views.contains_key(&q.name) && !q.or_replace {
if q.if_not_exists {
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("view {} already exists — skipped", q.name),
"create_view",
));
}
return Err(RedDBError::Internal(format!(
"view {} already exists",
q.name
)));
}
views.insert(q.name.clone(), Arc::new(q.clone()));
drop(views);
// Materialized view: register cache slot (data is empty until REFRESH).
if q.materialized {
use crate::storage::cache::result::{MaterializedViewDef, RefreshPolicy};
let refresh = match q.refresh_every_ms {
Some(ms) => RefreshPolicy::Periodic(std::time::Duration::from_millis(ms)),
None => RefreshPolicy::Manual,
};
let dependencies = collect_table_refs(&q.query);
let def = MaterializedViewDef {
name: q.name.clone(),
query: format!("<parsed view {}>", q.name),
dependencies: dependencies.clone(),
refresh,
retention_duration_ms: q.retention_duration_ms,
};
self.inner.materialized_views.write().register(def);
// Issue #593 slice 9a — persist the descriptor to
// the system catalog so the definition survives a
// restart. Upsert semantics (delete-then-insert by
// name) keep the catalog free of duplicate rows
// across `CREATE OR REPLACE` churn.
let descriptor =
crate::runtime::continuous_materialized_view::MaterializedViewDescriptor {
name: q.name.clone(),
source_sql: query.to_string(),
source_collections: dependencies,
refresh_every_ms: q.refresh_every_ms,
retention_duration_ms: q.retention_duration_ms,
};
let store = self.inner.db.store();
crate::runtime::continuous_materialized_view::persist_descriptor(
store.as_ref(),
&descriptor,
)?;
// Issue #594 slice 9b — provision a Table-shaped
// backing collection named after the view. The
// rewriter skips materialized views (see
// `rewrite_view_refs_inner`) so `SELECT FROM v`
// resolves to this collection directly. Empty
// until REFRESH wires through it in 9c.
self.ensure_materialized_view_backing(&q.name)?;
}
// Plan cache may have cached a plan that didn't know about this
// view — invalidate so future references pick up the new binding.
// Result cache gets flushed too: OR REPLACE must not serve a
// prior execution of the obsolete body.
self.invalidate_plan_cache();
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!(
"{}view {} created",
if q.materialized { "materialized " } else { "" },
q.name
),
"create_view",
))
}
QueryExpr::DropView(ref q) => {
let mut views = self.inner.views.write();
let removed = views.remove(&q.name);
let existed = removed.is_some();
let removed_materialized =
removed.as_ref().map(|v| v.materialized).unwrap_or(false);
drop(views);
if q.materialized || existed {
// Try the materialised cache too — silent if absent.
self.inner.materialized_views.write().remove(&q.name);
// Issue #593 slice 9a — remove any persisted
// catalog row. Idempotent: a no-op when the view
// was never materialized (no row was ever written).
let store = self.inner.db.store();
crate::runtime::continuous_materialized_view::remove_by_name(
store.as_ref(),
&q.name,
)?;
}
// Issue #594 slice 9b — drop the backing collection
// that was provisioned at CREATE time. Only mat views
// ever had one; regular views never did.
if removed_materialized || q.materialized {
self.drop_materialized_view_backing(&q.name)?;
}
// Drop any plan / result cache entries that baked the
// view body into their QueryExpr.
self.invalidate_plan_cache();
self.invalidate_result_cache();
if !existed && !q.if_exists {
return Err(RedDBError::Internal(format!(
"view {} does not exist",
q.name
)));
}
self.invalidate_plan_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("view {} dropped", q.name),
"drop_view",
))
}
QueryExpr::RefreshMaterializedView(ref q) => {
// Look up the view definition, execute its underlying query,
// and stash the serialized result in the materialised cache.
let view = {
let views = self.inner.views.read();
views.get(&q.name).cloned()
};
let view = match view {
Some(v) => v,
None => {
return Err(RedDBError::Internal(format!(
"view {} does not exist",
q.name
)))
}
};
if !view.materialized {
return Err(RedDBError::Internal(format!(
"view {} is not materialized — REFRESH requires \
CREATE MATERIALIZED VIEW",
q.name
)));
}
// Execute the underlying query fresh.
let started = std::time::Instant::now();
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0);
match self.execute_query_expr((*view.query).clone()) {
Ok(inner_result) => {
// Issue #595 slice 9c — atomically replace the
// backing collection's contents under a single
// WAL group. Concurrent SELECT from the view
// sees either the prior or new contents, never
// partial. A crash before the WAL commit lands
// leaves the prior contents intact on recovery.
let entities =
view_records_to_entities(&q.name, &inner_result.result.records);
let row_count = entities.len() as u64;
let store = self.inner.db.store();
let serialized_records = match store.refresh_collection(&q.name, entities) {
Ok(records) => records,
Err(err) => {
let duration_ms = started.elapsed().as_millis() as u64;
let msg = err.to_string();
self.inner
.materialized_views
.write()
.record_refresh_failure(
&q.name,
msg.clone(),
duration_ms,
now_ms,
);
return Err(RedDBError::Internal(format!(
"REFRESH MATERIALIZED VIEW {}: {msg}",
q.name
)));
}
};
// Issue #596 slice 9d — emit a Refresh
// ChangeRecord into the logical-WAL spool so
// replicas deterministically replay the same
// backing-collection contents via
// `LogicalChangeApplier::apply_record`.
if let Some(ref primary) = self.inner.db.replication {
let lsn = self.inner.cdc.emit(
crate::replication::cdc::ChangeOperation::Refresh,
&q.name,
0,
"refresh",
);
self.invalidate_result_cache_for_table(&q.name);
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let record = ChangeRecord::for_refresh(
lsn,
timestamp,
q.name.clone(),
serialized_records,
)
.with_term(self.current_replication_term());
let encoded = record.encode();
primary.append_logical_record(record.lsn, encoded);
}
let duration_ms = started.elapsed().as_millis() as u64;
let serialized = format!("{:?}", inner_result.result);
self.inner
.materialized_views
.write()
.record_refresh_success(
&q.name,
serialized.into_bytes(),
row_count,
duration_ms,
now_ms,
);
// SELECT FROM v now reads through the rewriter
// skip into the backing collection — drop the
// result cache so prior empty-backing reads
// don't shadow the new contents.
self.invalidate_result_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("materialized view {} refreshed", q.name),
"refresh_materialized_view",
))
}
Err(err) => {
let duration_ms = started.elapsed().as_millis() as u64;
let msg = err.to_string();
self.inner
.materialized_views
.write()
.record_refresh_failure(&q.name, msg.clone(), duration_ms, now_ms);
Err(err)
}
}
}
// Row Level Security (Phase 2.5 PG parity).
//
// Policies live in an in-memory registry keyed by (table, name).
// Enforcement (AND-ing the policy's USING clause into every
// query's WHERE for the table) arrives in Phase 2.5.2 via the
// filter compiler; this dispatch only manages the catalog.
QueryExpr::CreatePolicy(ref q) => {
let key = (q.table.clone(), q.name.clone());
self.inner
.rls_policies
.write()
.insert(key, Arc::new(q.clone()));
self.invalidate_plan_cache();
// Issue #120 — surface policy names in the
// schema-vocabulary so AskPipeline (#121) can resolve
// a policy reference back to its table.
self.schema_vocabulary_apply(
crate::runtime::schema_vocabulary::DdlEvent::CreatePolicy {
collection: q.table.clone(),
policy: q.name.clone(),
},
);
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("policy {} on {} created", q.name, q.table),
"create_policy",
))
}
QueryExpr::DropPolicy(ref q) => {
let removed = self
.inner
.rls_policies
.write()
.remove(&(q.table.clone(), q.name.clone()))
.is_some();
if !removed && !q.if_exists {
return Err(RedDBError::Internal(format!(
"policy {} on {} does not exist",
q.name, q.table
)));
}
self.invalidate_plan_cache();
// Issue #120 — keep the schema-vocabulary policy
// entry in sync.
self.schema_vocabulary_apply(
crate::runtime::schema_vocabulary::DdlEvent::DropPolicy {
collection: q.table.clone(),
policy: q.name.clone(),
},
);
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("policy {} on {} dropped", q.name, q.table),
"drop_policy",
))
}
// Foreign Data Wrappers (Phase 3.2 PG parity).
//
// CREATE SERVER / CREATE FOREIGN TABLE register into the shared
// `ForeignTableRegistry`. The read path consults that registry
// before dispatching a SELECT — when the table name matches a
// registered foreign table, we forward the scan to the wrapper
// and skip the normal collection lookup.
//
// Phase 3.2 is in-memory only; persistence across restarts is a
// 3.2.2 follow-up that mirrors the view registry pattern.
QueryExpr::CreateServer(ref q) => {
use crate::storage::fdw::FdwOptions;
let registry = Arc::clone(&self.inner.foreign_tables);
if registry.server(&q.name).is_some() {
if q.if_not_exists {
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("server {} already exists — skipped", q.name),
"create_server",
));
}
return Err(RedDBError::Internal(format!(
"server {} already exists",
q.name
)));
}
let mut opts = FdwOptions::new();
for (k, v) in &q.options {
opts.values.insert(k.clone(), v.clone());
}
registry
.create_server(&q.name, &q.wrapper, opts)
.map_err(|e| RedDBError::Internal(e.to_string()))?;
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("server {} created (wrapper {})", q.name, q.wrapper),
"create_server",
))
}
QueryExpr::DropServer(ref q) => {
let existed = self.inner.foreign_tables.drop_server(&q.name);
if !existed && !q.if_exists {
return Err(RedDBError::Internal(format!(
"server {} does not exist",
q.name
)));
}
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!(
"server {} dropped{}",
q.name,
if q.cascade { " (cascade)" } else { "" }
),
"drop_server",
))
}
QueryExpr::CreateForeignTable(ref q) => {
use crate::storage::fdw::{FdwOptions, ForeignColumn, ForeignTable};
let registry = Arc::clone(&self.inner.foreign_tables);
if registry.foreign_table(&q.name).is_some() {
if q.if_not_exists {
return Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("foreign table {} already exists — skipped", q.name),
"create_foreign_table",
));
}
return Err(RedDBError::Internal(format!(
"foreign table {} already exists",
q.name
)));
}
let mut opts = FdwOptions::new();
for (k, v) in &q.options {
opts.values.insert(k.clone(), v.clone());
}
let columns: Vec<ForeignColumn> = q
.columns
.iter()
.map(|c| ForeignColumn {
name: c.name.clone(),
data_type: c.data_type.clone(),
not_null: c.not_null,
})
.collect();
registry
.create_foreign_table(ForeignTable {
name: q.name.clone(),
server_name: q.server.clone(),
columns,
options: opts,
})
.map_err(|e| RedDBError::Internal(e.to_string()))?;
self.invalidate_plan_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("foreign table {} created (server {})", q.name, q.server),
"create_foreign_table",
))
}
QueryExpr::DropForeignTable(ref q) => {
let existed = self.inner.foreign_tables.drop_foreign_table(&q.name);
if !existed && !q.if_exists {
return Err(RedDBError::Internal(format!(
"foreign table {} does not exist",
q.name
)));
}
self.invalidate_plan_cache();
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!("foreign table {} dropped", q.name),
"drop_foreign_table",
))
}
// COPY table FROM 'path' (Phase 1.5 PG parity).
//
// Stream CSV rows through the shared `CsvImporter`. The collection
// is auto-created on first insert (via `insert_auto`-style path);
// VACUUM/ANALYZE afterwards is up to the caller.
QueryExpr::CopyFrom(ref q) => {
use crate::storage::import::{CsvConfig, CsvImporter};
let store = self.inner.db.store();
let cfg = CsvConfig {
collection: q.table.clone(),
has_header: q.has_header,
delimiter: q.delimiter.map(|c| c as u8).unwrap_or(b','),
..CsvConfig::default()
};
let importer = CsvImporter::new(cfg);
let stats = importer
.import_file(&q.path, store.as_ref())
.map_err(|e| RedDBError::Internal(format!("COPY failed: {e}")))?;
// Tables are written → invalidate cached plans / result cache.
self.note_table_write(&q.table);
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&format!(
"COPY imported {} rows into {} ({} errors skipped, {}ms)",
stats.records_imported, q.table, stats.errors_skipped, stats.duration_ms
),
"copy_from",
))
}
// Maintenance commands (Phase 1.2 PG parity).
//
// - VACUUM [FULL] [table]: refreshes planner stats for the target
// collection(s) and — when FULL — triggers a full pager persist
// (flushes dirty pages + fsync). Also invalidates the result cache
// so subsequent reads re-execute against the freshly compacted
// storage. RedDB's segment/btree GC runs continuously via the
// background lifecycle; explicit space reclamation for sealed
// segments arrives with Phase 2.3 (MVCC + dead-tuple reclamation).
// - ANALYZE [table]: reruns `analyze_collection` +
// `persist_table_stats` via `refresh_table_planner_stats` so the
// planner has fresh histograms, distinct estimates, null counts.
//
// Both commands accept an optional target; omitting the target
// iterates every collection in the store.
QueryExpr::MaintenanceCommand(ref cmd) => {
use crate::storage::query::ast::MaintenanceCommand as Mc;
let store = self.inner.db.store();
let (kind, msg) = match cmd {
Mc::Analyze { target } => {
let targets: Vec<String> = match target {
Some(t) => vec![t.clone()],
None => store.list_collections(),
};
for t in &targets {
self.refresh_table_planner_stats(t);
}
(
"analyze",
format!("ANALYZE refreshed stats for {} table(s)", targets.len()),
)
}
Mc::Vacuum { target, full } => {
let targets: Vec<String> = match target {
Some(t) => vec![t.clone()],
None => store.list_collections(),
};
let cutoff_xid = self.mvcc_vacuum_cutoff_xid();
let mut vacuum_stats =
crate::storage::unified::store::MvccVacuumStats::default();
for t in &targets {
let stats = store.vacuum_mvcc_history(t, cutoff_xid).map_err(|e| {
RedDBError::Internal(format!(
"VACUUM MVCC history failed for {t}: {e}"
))
})?;
if stats.reclaimed_versions > 0 {
self.rebuild_runtime_indexes_for_table(t)?;
}
vacuum_stats.add(&stats);
}
self.inner.snapshot_manager.prune_aborted(cutoff_xid);
// Stats refresh covers every target (same as ANALYZE).
for t in &targets {
self.refresh_table_planner_stats(t);
}
// FULL forces a pager persist (dirty-page flush + fsync).
// Regular VACUUM relies on the background writer / segment
// lifecycle so the command is non-blocking.
let persisted = if *full {
match store.persist() {
Ok(()) => true,
Err(e) => {
return Err(RedDBError::Internal(format!(
"VACUUM FULL persist failed: {e:?}"
)));
}
}
} else {
false
};
// Result cache depended on pre-vacuum state.
self.invalidate_result_cache();
(
"vacuum",
format!(
"VACUUM{} processed {} table(s): scanned_versions={}, retained_versions={}, reclaimed_versions={}, retained_history_versions={}, reclaimed_history_versions={}, retained_tombstones={}, reclaimed_tombstones={}{}",
if *full { " FULL" } else { "" },
targets.len(),
vacuum_stats.scanned_versions,
vacuum_stats.retained_versions,
vacuum_stats.reclaimed_versions,
vacuum_stats.retained_history_versions,
vacuum_stats.reclaimed_history_versions,
vacuum_stats.retained_tombstones,
vacuum_stats.reclaimed_tombstones,
if persisted {
" (pages flushed to disk)"
} else {
""
}
),
)
}
};
Ok(RuntimeQueryResult::ok_message(
query.to_string(),
&msg,
kind,
))
}
// GRANT / REVOKE / ALTER USER (RBAC milestone).
//
// These hit the AuthStore directly. The statement frame /
// privilege gate has already decided whether the caller may
// even run the statement; here we just translate the AST into
// AuthStore calls.
QueryExpr::Grant(ref g) => self.execute_grant_statement(query, g),
QueryExpr::Revoke(ref r) => self.execute_revoke_statement(query, r),
QueryExpr::AlterUser(ref a) => self.execute_alter_user_statement(query, a),
QueryExpr::CreateUser(ref u) => self.execute_create_user_statement(query, u),
QueryExpr::CreateIamPolicy { ref id, ref json } => {
self.execute_create_iam_policy(query, id, json)
}
QueryExpr::DropIamPolicy { ref id } => self.execute_drop_iam_policy(query, id),
QueryExpr::AttachPolicy {
ref policy_id,
ref principal,
} => self.execute_attach_policy(query, policy_id, principal),
QueryExpr::DetachPolicy {
ref policy_id,
ref principal,
} => self.execute_detach_policy(query, policy_id, principal),
QueryExpr::ShowPolicies { ref filter } => {
self.execute_show_policies(query, filter.as_ref())
}
QueryExpr::ShowEffectivePermissions {
ref user,
ref resource,
} => self.execute_show_effective_permissions(query, user, resource.as_ref()),
QueryExpr::SimulatePolicy {
ref user,
ref action,
ref resource,
} => self.execute_simulate_policy(query, user, action, resource),
QueryExpr::LintPolicy { ref source } => self.execute_lint_policy(query, source),
QueryExpr::MigratePolicyMode {
ref target,
dry_run,
} => self.execute_migrate_policy_mode(query, target, dry_run),
QueryExpr::CreateMigration(ref q) => self.execute_create_migration(query, q),
QueryExpr::ApplyMigration(ref q) => self.execute_apply_migration(query, q),
QueryExpr::RollbackMigration(ref q) => self.execute_rollback_migration(query, q),
QueryExpr::ExplainMigration(ref q) => self.execute_explain_migration(query, q),
_ => Err(RedDBError::Query(
"unsupported command in runtime dispatcher".to_string(),
)),
};
if !control_event_specs.is_empty() {
let (outcome, reason) = match &query_result {
Ok(_) => (crate::runtime::control_events::Outcome::Allowed, None),
Err(err) => (control_event_outcome_for_error(err), Some(err.to_string())),
};
for spec in &control_event_specs {
self.emit_control_event(
spec.kind,
outcome,
spec.action,
spec.resource.clone(),
reason.clone(),
spec.fields.clone(),
)?;
}
}
if let (Some(plan), Ok(result)) = (&query_audit_plan, &query_result) {
self.emit_query_audit(
query,
plan,
query_audit_started.elapsed().as_millis() as u64,
result,
);
}
// Decrypt Value::Secret columns in-place before caching, so
// cached results match the post-decrypt shape and repeat
// queries skip the per-row AES-GCM pass.
let mut query_result = query_result;
if let Ok(ref mut result) = query_result {
if result.statement_type == "select" {
self.apply_secret_decryption(result);
}
}
// Cache SELECT results for 30s.
// Skip: pre-serialized JSON (large clone), and result sets > 5 rows.
// Large multi-row results (range scans, filtered scans) are rarely
// repeated with the same literal values so the cache hit rate is near
// zero while the clone cost (100 records × ~16 fields each) is high.
// Aggregations (1 row) and point lookups (1 row) still benefit.
if let Ok(ref result) = query_result {
frame.write_result_cache(self, result, result_cache_scopes);
}
query_result
}
/// Execute a pre-parsed `QueryExpr` directly, bypassing SQL parsing and the
/// plan cache. Used by the prepared-statement fast path so that `execute_prepared`
/// calls pay zero parse + cache overhead.
///
/// Applies secret decryption on SELECT results, identical to `execute_query`.
pub fn execute_query_expr(&self, expr: QueryExpr) -> RedDBResult<RuntimeQueryResult> {
let _config_snapshot_guard = ConfigSnapshotGuard::install(
Arc::clone(&self.inner.db),
self.inner.auth_store.read().clone(),
);
let _secret_store_guard = SecretStoreGuard::install(self.inner.auth_store.read().clone());
// View rewrite (Phase 2.1): substitute any `QueryExpr::Table(tq)`
// whose `tq.table` matches a registered view with the view's
// underlying query. Safe to call even when no views are registered.
let expr = self.rewrite_view_refs(expr);
self.validate_model_operations_before_auth(&expr)?;
// Granular RBAC privilege check. Runs before dispatch so a
// denied caller never reaches storage. Fail-closed: any error
// resolving the action / resource produces PermissionDenied.
if let Err(err) = self.check_query_privilege(&expr) {
return Err(RedDBError::Query(format!("permission denied: {err}")));
}
let statement = query_expr_name(&expr);
let mode = detect_mode(statement);
let query_str = statement;
let result = self.dispatch_expr(expr, query_str, mode)?;
let mut r = result;
if r.statement_type == "select" {
self.apply_secret_decryption(&mut r);
}
Ok(r)
}
pub(super) fn validate_model_operations_before_auth(
&self,
expr: &QueryExpr,
) -> RedDBResult<()> {
use crate::catalog::CollectionModel;
use crate::runtime::ddl::polymorphic_resolver;
use crate::storage::query::ast::KvCommand;
let system_schema_target = match expr {
QueryExpr::DropTable(q) => Some(q.name.as_str()),
QueryExpr::DropGraph(q) => Some(q.name.as_str()),
QueryExpr::DropVector(q) => Some(q.name.as_str()),
QueryExpr::DropDocument(q) => Some(q.name.as_str()),
QueryExpr::DropKv(q) => Some(q.name.as_str()),
QueryExpr::DropCollection(q) => Some(q.name.as_str()),
QueryExpr::Truncate(q) => Some(q.name.as_str()),
_ => None,
};
if system_schema_target.is_some_and(crate::runtime::impl_ddl::is_system_schema_name) {
return Err(RedDBError::Query("system schema is read-only".to_string()));
}
let expected = match expr {
QueryExpr::DropTable(q) => Some((q.name.as_str(), CollectionModel::Table)),
QueryExpr::DropGraph(q) => Some((q.name.as_str(), CollectionModel::Graph)),
QueryExpr::DropVector(q) => Some((q.name.as_str(), CollectionModel::Vector)),
QueryExpr::DropDocument(q) => Some((q.name.as_str(), CollectionModel::Document)),
QueryExpr::DropKv(q) => Some((q.name.as_str(), q.model)),
QueryExpr::DropCollection(q) => q.model.map(|model| (q.name.as_str(), model)),
QueryExpr::Truncate(q) => q.model.map(|model| (q.name.as_str(), model)),
QueryExpr::KvCommand(cmd) => {
let (collection, model) = match cmd {
KvCommand::Put {
collection, model, ..
}
| KvCommand::Get {
collection, model, ..
}
| KvCommand::Incr {
collection, model, ..
}
| KvCommand::Cas {
collection, model, ..
}
| KvCommand::List {
collection, model, ..
}
| KvCommand::Delete {
collection, model, ..
} => (collection.as_str(), *model),
KvCommand::Rotate { collection, .. }
| KvCommand::History { collection, .. }
| KvCommand::Purge { collection, .. } => {
(collection.as_str(), CollectionModel::Vault)
}
KvCommand::InvalidateTags { collection, .. } => {
(collection.as_str(), CollectionModel::Kv)
}
KvCommand::Watch {
collection, model, ..
} => (collection.as_str(), *model),
KvCommand::Unseal { collection, .. } => {
(collection.as_str(), CollectionModel::Vault)
}
};
Some((collection, model))
}
QueryExpr::ConfigCommand(cmd) => {
self.validate_config_command_before_auth(cmd)?;
None
}
_ => None,
};
let Some((name, expected_model)) = expected else {
return Ok(());
};
let snapshot = self.inner.db.catalog_model_snapshot();
let Some(actual_model) = snapshot
.collections
.iter()
.find(|collection| collection.name == name)
.map(|collection| collection.declared_model.unwrap_or(collection.model))
else {
return Ok(());
};
polymorphic_resolver::ensure_model_match(expected_model, actual_model)
}
/// Walk a `QueryExpr` and replace `QueryExpr::Table(tq)` nodes whose
/// `tq.table` matches a registered view name with the view's stored
/// body. Recurses through joins so `SELECT ... FROM t JOIN myview ...`
/// resolves correctly. Pure operation — no side effects.
pub(super) fn rewrite_view_refs(&self, expr: QueryExpr) -> QueryExpr {
// Fast path: no views registered → return original expression.
if self.inner.views.read().is_empty() {
return expr;
}
self.rewrite_view_refs_inner(expr)
}
fn rewrite_view_refs_inner(&self, expr: QueryExpr) -> QueryExpr {
use crate::storage::query::ast::{Filter, TableSource};
match expr {
QueryExpr::Table(mut tq) => {
// 1. If the TableSource is a subquery, recurse into it so
// `SELECT ... FROM (SELECT ... FROM myview) t` expands.
// The legacy `table` field (set to a synthetic
// "__subq_NNNN" sentinel) stays as-is so callers that
// read it keep compiling.
if let Some(TableSource::Subquery(body)) = tq.source.take() {
tq.source = Some(TableSource::Subquery(Box::new(
self.rewrite_view_refs_inner(*body),
)));
return QueryExpr::Table(tq);
}
// 2. Restore the source field (took it above for match).
// When the source was `None` or `TableSource::Name(_)`, the
// real lookup key is `tq.table` — check the view registry.
let maybe_view = {
let views = self.inner.views.read();
views.get(&tq.table).cloned()
};
let Some(view) = maybe_view else {
return QueryExpr::Table(tq);
};
// Issue #594 slice 9b — materialized views are read
// from their backing collection, not by substituting
// the body. Returning the TableQuery as-is lets the
// normal table-read path resolve `SELECT FROM v`
// against the collection provisioned at CREATE time.
if view.materialized {
return QueryExpr::Table(tq);
}
// Recurse into the view body — views may reference other
// views. The recursion yields the final QueryExpr we need
// to merge the outer's filter / limit / offset into.
let inner_expr = self.rewrite_view_refs_inner((*view.query).clone());
// Phase 5: when the body is a Table we merge the outer
// TableQuery's WHERE / LIMIT / OFFSET into it so stacked
// views filter recursively. Non-table bodies (Search,
// Ask, Vector, Graph, Hybrid) can't meaningfully combine
// with an outer Table query today — return the body
// verbatim; outer predicates are lost. Full projection
// merge lands in Phase 5.2.
match inner_expr {
QueryExpr::Table(mut inner_tq) => {
if let Some(outer_filter) = tq.filter.take() {
inner_tq.filter = Some(match inner_tq.filter.take() {
Some(existing) => {
Filter::And(Box::new(existing), Box::new(outer_filter))
}
None => outer_filter,
});
// Keep the `Expr` form in lock-step with the
// merged `Filter`. The executor prefers
// `where_expr` and nulls `filter` when it is
// present (see `execute_query_inner`), so a
// stacked view whose outer predicate was only
// merged into `filter` would silently drop that
// predicate at eval time (#635).
inner_tq.where_expr = inner_tq
.filter
.as_ref()
.map(crate::storage::query::sql_lowering::filter_to_expr);
}
if let Some(outer_limit) = tq.limit {
inner_tq.limit = Some(match inner_tq.limit {
Some(existing) => existing.min(outer_limit),
None => outer_limit,
});
}
if let Some(outer_offset) = tq.offset {
inner_tq.offset = Some(match inner_tq.offset {
Some(existing) => existing + outer_offset,
None => outer_offset,
});
}
QueryExpr::Table(inner_tq)
}
other => other,
}
}
QueryExpr::Join(mut jq) => {
jq.left = Box::new(self.rewrite_view_refs_inner(*jq.left));
jq.right = Box::new(self.rewrite_view_refs_inner(*jq.right));
QueryExpr::Join(jq)
}
// Other variants don't carry nested QueryExpr that can reference
// a view by table name. Return as-is.
other => other,
}
}
fn resolve_table_expr_subqueries(
&self,
mut table: TableQuery,
frame: &dyn super::statement_frame::ReadFrame,
) -> RedDBResult<TableQuery> {
// Only a `Subquery` source needs recursive resolution. `.take()`
// would otherwise drop a `Name` / `Function` source on the floor
// (the `if let` skips the body but the take already cleared it),
// which silently broke `SELECT * FROM components(g)` — the TVF
// dispatch downstream keys off `TableSource::Function` and never
// fired. Restore any non-subquery source unchanged (issue #795).
match table.source.take() {
Some(TableSource::Subquery(inner)) => {
let inner = self.resolve_select_expr_subqueries(*inner, frame)?;
table.source = Some(TableSource::Subquery(Box::new(inner)));
}
other => table.source = other,
}
let outer_scopes = relation_scopes_for_query(&QueryExpr::Table(table.clone()));
for item in &mut table.select_items {
if let crate::storage::query::ast::SelectItem::Expr { expr, .. } = item {
*expr = self.resolve_expr_subqueries(expr.clone(), &outer_scopes, frame)?;
}
}
if let Some(where_expr) = table.where_expr.take() {
table.where_expr =
Some(self.resolve_expr_subqueries(where_expr, &outer_scopes, frame)?);
table.filter = None;
}
if let Some(having_expr) = table.having_expr.take() {
table.having_expr =
Some(self.resolve_expr_subqueries(having_expr, &outer_scopes, frame)?);
table.having = None;
}
for expr in &mut table.group_by_exprs {
*expr = self.resolve_expr_subqueries(expr.clone(), &outer_scopes, frame)?;
}
for clause in &mut table.order_by {
if let Some(expr) = clause.expr.take() {
clause.expr = Some(self.resolve_expr_subqueries(expr, &outer_scopes, frame)?);
}
}
Ok(table)
}
fn resolve_select_expr_subqueries(
&self,
expr: QueryExpr,
frame: &dyn super::statement_frame::ReadFrame,
) -> RedDBResult<QueryExpr> {
match expr {
QueryExpr::Table(table) => self
.resolve_table_expr_subqueries(table, frame)
.map(QueryExpr::Table),
QueryExpr::Join(mut join) => {
join.left = Box::new(self.resolve_select_expr_subqueries(*join.left, frame)?);
join.right = Box::new(self.resolve_select_expr_subqueries(*join.right, frame)?);
Ok(QueryExpr::Join(join))
}
other => Ok(other),
}
}
fn resolve_expr_subqueries(
&self,
expr: crate::storage::query::ast::Expr,
outer_scopes: &[String],
frame: &dyn super::statement_frame::ReadFrame,
) -> RedDBResult<crate::storage::query::ast::Expr> {
use crate::storage::query::ast::Expr;
match expr {
Expr::Subquery { query, span } => {
let values = self.execute_expr_subquery_values(query, outer_scopes, frame)?;
if values.len() > 1 {
return Err(RedDBError::Query(
"scalar subquery returned more than one row".to_string(),
));
}
Ok(Expr::Literal {
value: values.into_iter().next().unwrap_or(Value::Null),
span,
})
}
Expr::BinaryOp { op, lhs, rhs, span } => Ok(Expr::BinaryOp {
op,
lhs: Box::new(self.resolve_expr_subqueries(*lhs, outer_scopes, frame)?),
rhs: Box::new(self.resolve_expr_subqueries(*rhs, outer_scopes, frame)?),
span,
}),
Expr::UnaryOp { op, operand, span } => Ok(Expr::UnaryOp {
op,
operand: Box::new(self.resolve_expr_subqueries(*operand, outer_scopes, frame)?),
span,
}),
Expr::Cast {
inner,
target,
span,
} => Ok(Expr::Cast {
inner: Box::new(self.resolve_expr_subqueries(*inner, outer_scopes, frame)?),
target,
span,
}),
Expr::FunctionCall { name, args, span } => {
let args = args
.into_iter()
.map(|arg| self.resolve_expr_subqueries(arg, outer_scopes, frame))
.collect::<RedDBResult<Vec<_>>>()?;
Ok(Expr::FunctionCall { name, args, span })
}
Expr::Case {
branches,
else_,
span,
} => {
let branches = branches
.into_iter()
.map(|(cond, value)| {
Ok((
self.resolve_expr_subqueries(cond, outer_scopes, frame)?,
self.resolve_expr_subqueries(value, outer_scopes, frame)?,
))
})
.collect::<RedDBResult<Vec<_>>>()?;
let else_ = else_
.map(|expr| self.resolve_expr_subqueries(*expr, outer_scopes, frame))
.transpose()?
.map(Box::new);
Ok(Expr::Case {
branches,
else_,
span,
})
}
Expr::IsNull {
operand,
negated,
span,
} => Ok(Expr::IsNull {
operand: Box::new(self.resolve_expr_subqueries(*operand, outer_scopes, frame)?),
negated,
span,
}),
Expr::InList {
target,
values,
negated,
span,
} => {
let target =
Box::new(self.resolve_expr_subqueries(*target, outer_scopes, frame)?);
let mut resolved = Vec::new();
for value in values {
if let Expr::Subquery { query, .. } = value {
resolved.extend(
self.execute_expr_subquery_values(query, outer_scopes, frame)?
.into_iter()
.map(Expr::lit),
);
} else {
resolved.push(self.resolve_expr_subqueries(value, outer_scopes, frame)?);
}
}
Ok(Expr::InList {
target,
values: resolved,
negated,
span,
})
}
Expr::Between {
target,
low,
high,
negated,
span,
} => Ok(Expr::Between {
target: Box::new(self.resolve_expr_subqueries(*target, outer_scopes, frame)?),
low: Box::new(self.resolve_expr_subqueries(*low, outer_scopes, frame)?),
high: Box::new(self.resolve_expr_subqueries(*high, outer_scopes, frame)?),
negated,
span,
}),
other => Ok(other),
}
}
fn execute_expr_subquery_values(
&self,
subquery: crate::storage::query::ast::ExprSubquery,
outer_scopes: &[String],
frame: &dyn super::statement_frame::ReadFrame,
) -> RedDBResult<Vec<Value>> {
let query = *subquery.query;
if query_references_outer_scope(&query, outer_scopes) {
return Err(RedDBError::Query(
"NOT_YET_SUPPORTED: correlated subqueries are not supported yet; track follow-up issue #470-correlated-subqueries".to_string(),
));
}
let query = self.rewrite_view_refs(query);
let query = self.resolve_select_expr_subqueries(query, frame)?;
let query = self.authorize_relational_select_expr(query, frame)?;
let result = match query {
QueryExpr::Table(table) => {
execute_runtime_table_query(&self.inner.db, &table, Some(&self.inner.index_store))?
}
QueryExpr::Join(join) => execute_runtime_join_query(&self.inner.db, &join)?,
other => {
return Err(RedDBError::Query(format!(
"expression subquery must be a SELECT query, got {}",
query_expr_name(&other)
)))
}
};
first_column_values(result)
}
fn dispatch_expr(
&self,
expr: QueryExpr,
query_str: &str,
mode: QueryMode,
) -> RedDBResult<RuntimeQueryResult> {
let statement = query_expr_name(&expr);
match expr {
QueryExpr::Graph(_) | QueryExpr::Path(_) => {
// Graph queries are not cacheable as prepared statements.
Err(RedDBError::Query(
"graph queries cannot be used as prepared statements".to_string(),
))
}
QueryExpr::Table(table) => {
let scope = self.ai_scope();
let table = self.resolve_table_expr_subqueries(
table,
&scope as &dyn super::statement_frame::ReadFrame,
)?;
// Table-valued functions (e.g. components(g)) dispatch to a
// read-only executor before any catalog/virtual-table routing
// (issue #795).
if let Some(TableSource::Function {
name,
args,
named_args,
}) = table.source.clone()
{
return Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-graph-tvf",
result: self.execute_table_function(&name, &args, &named_args)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
// Inline-graph TVF (issue #799) on the prepared-statement /
// direct-expr path. Result caching is wired on the
// `execute_query_inner` path; here we just compute and return.
if let Some(TableSource::InlineGraphFunction {
name,
nodes,
edges,
named_args,
}) = table.source.clone()
{
return Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-graph-tvf-inline",
result: self.execute_inline_graph_function(
&name,
&nodes,
&edges,
&named_args,
)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
if super::red_schema::is_virtual_table(&table.table) {
return Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-red-schema",
result: super::red_schema::red_query(
self,
&table.table,
&table,
&scope as &dyn super::statement_frame::ReadFrame,
)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
// `<graph>.<output>` analytics virtual view (issue #800).
if let Some(view_result) = self.try_resolve_analytics_view(
&table,
&scope as &dyn super::statement_frame::ReadFrame,
)? {
return Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-graph-analytics-view",
result: view_result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
}
let Some(table_with_rls) = self.authorize_relational_table_select(
table,
&scope as &dyn super::statement_frame::ReadFrame,
)?
else {
return Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-table-rls",
result: crate::storage::query::unified::UnifiedResult::empty(),
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
};
Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-table",
result: execute_runtime_table_query(
&self.inner.db,
&table_with_rls,
Some(&self.inner.index_store),
)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
QueryExpr::Join(join) => {
let scope = self.ai_scope();
let Some(join_with_rls) = self.authorize_relational_join_select(
join,
&scope as &dyn super::statement_frame::ReadFrame,
)?
else {
return Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-join-rls",
result: crate::storage::query::unified::UnifiedResult::empty(),
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
});
};
Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-join",
result: execute_runtime_join_query(&self.inner.db, &join_with_rls)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
QueryExpr::Vector(vector) => Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-vector",
result: execute_runtime_vector_query(&self.inner.db, &vector)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
}),
QueryExpr::Hybrid(hybrid) => Ok(RuntimeQueryResult {
query: query_str.to_string(),
mode,
statement,
engine: "runtime-hybrid",
result: execute_runtime_hybrid_query(&self.inner.db, &hybrid)?,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
}),
QueryExpr::Insert(ref insert) if super::red_schema::is_virtual_table(&insert.table) => {
Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
))
}
QueryExpr::Update(ref update) if super::red_schema::is_virtual_table(&update.table) => {
Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
))
}
QueryExpr::Delete(ref delete) if super::red_schema::is_virtual_table(&delete.table) => {
Err(RedDBError::Query(
super::red_schema::READ_ONLY_ERROR.to_string(),
))
}
QueryExpr::Insert(ref insert) => self
.with_deferred_store_wal_for_dml(self.insert_may_emit_events(insert), || {
self.execute_insert(query_str, insert)
}),
QueryExpr::Update(ref update) => self
.with_deferred_store_wal_for_dml(self.update_may_emit_events(update), || {
self.execute_update(query_str, update)
}),
QueryExpr::Delete(ref delete) => self
.with_deferred_store_wal_for_dml(self.delete_may_emit_events(delete), || {
self.execute_delete(query_str, delete)
}),
QueryExpr::SearchCommand(ref cmd) => self.execute_search_command(query_str, cmd),
QueryExpr::Ask(ref ask) => self.execute_ask(query_str, ask),
_ => Err(RedDBError::Query(format!(
"prepared-statement execution does not support {statement} statements"
))),
}
}
/// Dispatch a graph-collection table-valued function call in FROM
/// position (e.g. `SELECT * FROM components(g)`).
///
/// Validates the function name and arity here, materializes the whole
/// active graph read-only, then runs the algorithm via the shared
/// `dispatch_graph_algorithm` path. Never mutates the catalog or store.
fn execute_table_function(
&self,
name: &str,
args: &[String],
named_args: &[(String, f64)],
) -> RedDBResult<crate::storage::query::unified::UnifiedResult> {
if name.eq_ignore_ascii_case("red.diff") {
return self.execute_vcs_diff_tvf(args, named_args);
}
if !is_graph_tvf_name(name) {
return Err(RedDBError::Query(format!("unknown table function: {name}")));
}
// Every graph-collection TVF takes exactly one graph argument.
if args.len() != 1 {
return Err(RedDBError::Query(format!(
"table function '{name}' takes exactly 1 graph argument, got {}",
args.len()
)));
}
// Read-only materialization of the full active graph. Passing `None`
// for the projection uses the full graph store. Like #795/#796, the
// v0 form runs over the whole graph store regardless of the collection
// argument value. Materialization never mutates any store.
let (nodes, edges) = self.materialize_whole_graph_abstract()?;
self.dispatch_graph_algorithm(name, nodes, edges, named_args)
}
pub(crate) fn revive_versioned_updates_since(&self, conn_id: u64, stamper_xid: u64) -> usize {
let mut guard = self.inner.pending_versioned_updates.write();
let Some(pending) = guard.get_mut(&conn_id) else {
return 0;
};
let store = self.inner.db.store();
let mut reverted = 0usize;
pending.retain(|(collection, old_id, new_id, xid, previous_xmax)| {
if *xid < stamper_xid {
return true;
}
if let Some(manager) = store.get_collection(collection) {
if let Some(mut old) = manager.get(*old_id) {
if old.xmax == *xid {
old.set_xmax(*previous_xmax);
let _ = manager.update(old);
}
}
}
let _ = store.delete_batch(collection, &[*new_id]);
reverted += 1;
false
});
if pending.is_empty() {
guard.remove(&conn_id);
}
reverted
}
/// Wrap the planner's `RuntimeQueryExplain` as rows on a
/// `RuntimeQueryResult` so callers over the SQL interface see the
/// plan tree in the same shape a SELECT produces.
///
/// Columns: `op`, `source`, `estimated_rows`, `estimated_cost`, `depth`.
/// Nodes are walked depth-first; `depth` counts from 0 at the
/// root so a text renderer can indent without re-walking.
fn explain_as_rows(&self, raw_query: &str, inner_sql: &str) -> RedDBResult<RuntimeQueryResult> {
let explain = self.explain_query(inner_sql)?;
let columns = vec![
"op".to_string(),
"source".to_string(),
"estimated_rows".to_string(),
"estimated_cost".to_string(),
"depth".to_string(),
];
let mut records: Vec<crate::storage::query::unified::UnifiedRecord> = Vec::new();
// Prepend `CteScan` markers when the query carried a leading
// WITH clause. The CTE bodies are already inlined into the
// main plan tree, but operators reading EXPLAIN need to see
// which named CTEs were resolved — without this row the plan
// would look indistinguishable from a hand-inlined query.
for name in &explain.cte_materializations {
use std::sync::Arc;
let mut rec = crate::storage::query::unified::UnifiedRecord::default();
rec.set_arc(Arc::from("op"), Value::text("CteScan".to_string()));
rec.set_arc(Arc::from("source"), Value::text(name.clone()));
rec.set_arc(Arc::from("estimated_rows"), Value::Float(0.0));
rec.set_arc(Arc::from("estimated_cost"), Value::Float(0.0));
rec.set_arc(Arc::from("depth"), Value::Integer(0));
records.push(rec);
}
walk_plan_node(&explain.logical_plan.root, 0, &mut records);
let result = crate::storage::query::unified::UnifiedResult {
columns,
records,
stats: Default::default(),
pre_serialized_json: None,
};
Ok(RuntimeQueryResult {
query: raw_query.to_string(),
mode: explain.mode,
statement: "explain",
engine: "runtime-explain",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
fn explain_analyze_as_rows(
&self,
raw_query: &str,
inner_sql: &str,
) -> RedDBResult<RuntimeQueryResult> {
if !starts_with_dml_keyword(inner_sql) {
return Err(RedDBError::Query(
"EXPLAIN ANALYZE currently supports INSERT, UPDATE, and DELETE".to_string(),
));
}
let explain = self.explain_query(inner_sql)?;
let conn_id = current_connection_id();
if self.inner.tx_contexts.read().contains_key(&conn_id) {
return Err(RedDBError::Query(
"EXPLAIN ANALYZE requires no active transaction".to_string(),
));
}
self.execute_query_inner("BEGIN")?;
let started = std::time::Instant::now();
let execution = self.execute_query_inner(inner_sql);
let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
let rollback = self.execute_query_inner("ROLLBACK");
let affected_rows = match execution {
Ok(result) => result.affected_rows,
Err(err) => {
rollback?;
return Err(err);
}
};
rollback?;
let columns = vec![
"op".to_string(),
"source".to_string(),
"estimated_rows".to_string(),
"estimated_cost".to_string(),
"actual_rows".to_string(),
"actual_ms".to_string(),
"depth".to_string(),
];
let mut records = Vec::new();
walk_analyze_plan_node(
&explain.logical_plan.root,
0,
affected_rows,
elapsed_ms,
&mut records,
);
let result = crate::storage::query::unified::UnifiedResult {
columns,
records,
stats: Default::default(),
pre_serialized_json: None,
};
Ok(RuntimeQueryResult {
query: raw_query.to_string(),
mode: explain.mode,
statement: "explain_analyze",
engine: "runtime-explain-analyze",
result,
affected_rows: 0,
statement_type: "select",
bookmark: None,
notice: None,
})
}
}
fn strip_explain_analyze_prefix(sql: &str) -> Option<&str> {
let rest = strip_keyword_ci(sql.trim_start(), "EXPLAIN")?.trim_start();
Some(strip_keyword_ci(rest, "ANALYZE")?.trim_start()).filter(|inner| !inner.is_empty())
}
fn strip_keyword_ci<'a>(sql: &'a str, keyword: &str) -> Option<&'a str> {
if sql.len() < keyword.len() {
return None;
}
let (head, rest) = sql.split_at(keyword.len());
if !head.eq_ignore_ascii_case(keyword) {
return None;
}
if rest
.chars()
.next()
.is_some_and(|ch| !ch.is_ascii_whitespace())
{
return None;
}
Some(rest)
}
fn starts_with_dml_keyword(sql: &str) -> bool {
let trimmed = sql.trim_start();
let head_end = trimmed
.find(|ch: char| ch.is_ascii_whitespace())
.unwrap_or(trimmed.len());
let head = &trimmed[..head_end];
head.eq_ignore_ascii_case("INSERT")
|| head.eq_ignore_ascii_case("UPDATE")
|| head.eq_ignore_ascii_case("DELETE")
}
fn walk_plan_node(
node: &crate::storage::query::planner::CanonicalLogicalNode,
depth: usize,
out: &mut Vec<crate::storage::query::unified::UnifiedRecord>,
) {
use std::sync::Arc;
let mut rec = crate::storage::query::unified::UnifiedRecord::default();
rec.set_arc(Arc::from("op"), Value::text(node.operator.clone()));
rec.set_arc(
Arc::from("source"),
node.source.clone().map(Value::text).unwrap_or(Value::Null),
);
rec.set_arc(
Arc::from("estimated_rows"),
Value::Float(node.estimated_rows),
);
rec.set_arc(
Arc::from("estimated_cost"),
Value::Float(node.operator_cost),
);
rec.set_arc(Arc::from("depth"), Value::Integer(depth as i64));
out.push(rec);
for child in &node.children {
walk_plan_node(child, depth + 1, out);
}
}
fn walk_analyze_plan_node(
node: &crate::storage::query::planner::CanonicalLogicalNode,
depth: usize,
affected_rows: u64,
elapsed_ms: f64,
out: &mut Vec<crate::storage::query::unified::UnifiedRecord>,
) {
use std::sync::Arc;
let mut rec = crate::storage::query::unified::UnifiedRecord::default();
rec.set_arc(Arc::from("op"), Value::text(node.operator.clone()));
rec.set_arc(
Arc::from("source"),
node.source.clone().map(Value::text).unwrap_or(Value::Null),
);
rec.set_arc(
Arc::from("estimated_rows"),
Value::Float(node.estimated_rows),
);
rec.set_arc(
Arc::from("estimated_cost"),
Value::Float(node.operator_cost),
);
rec.set_arc(
Arc::from("actual_rows"),
Value::UnsignedInteger(if depth == 0 { affected_rows } else { 0 }),
);
rec.set_arc(
Arc::from("actual_ms"),
Value::Float(if depth == 0 { elapsed_ms } else { 0.0 }),
);
rec.set_arc(Arc::from("depth"), Value::Integer(depth as i64));
out.push(rec);
for child in &node.children {
walk_analyze_plan_node(child, depth + 1, 0, 0.0, out);
}
}
#[cfg(test)]
mod inline_graph_tvf_tests {
use super::*;
fn scopes_for(sql: &str) -> HashSet<String> {
let expr = crate::storage::query::parser::parse(sql)
.expect("parse")
.query;
query_expr_result_cache_scopes(&expr)
}
#[test]
fn inline_tvf_cache_scopes_include_source_collections() {
// The result-cache key for the inline form must derive from the
// `nodes`/`edges` source collections so a write to either invalidates
// the cached result (issue #799).
let scopes = scopes_for(
"SELECT * FROM components(nodes => (SELECT id FROM hosts), edges => (SELECT src, dst FROM links))",
);
assert!(scopes.contains("hosts"), "nodes source scoped: {scopes:?}");
assert!(scopes.contains("links"), "edges source scoped: {scopes:?}");
}
#[test]
fn graph_collection_tvf_cache_scope_is_graph_argument() {
// The graph-collection form still materializes the active graph, but
// result-cache invalidation is scoped to the named graph argument so
// INSERT INTO g NODE/EDGE invalidates cached TVF rows.
let scopes = scopes_for("SELECT * FROM components(g)");
assert!(scopes.contains("g"), "collection form scoped: {scopes:?}");
}
#[test]
fn abstract_degree_centrality_counts_undirected_endpoints() {
let nodes = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let edges = vec![
("a".to_string(), "b".to_string(), 1.0_f32),
("b".to_string(), "c".to_string(), 1.0_f32),
];
let degrees = abstract_degree_centrality(&nodes, &edges);
assert_eq!(
degrees,
vec![
("a".to_string(), 1),
("b".to_string(), 2),
("c".to_string(), 1),
]
);
}
}