reddb-io-server 1.1.1

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

use super::builders::{GraphQueryBuilder, PathQueryBuilder, TableQueryBuilder};
use crate::catalog::CollectionModel;
pub use crate::storage::engine::distance::DistanceMetric;
pub use crate::storage::engine::vector_metadata::MetadataFilter;
pub use crate::storage::queue::QueueMode;
use crate::storage::schema::{SqlTypeName, Value};

/// Root query expression
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum QueryExpr {
    /// Pure table query: SELECT ... FROM ...
    Table(TableQuery),
    /// Pure graph query: MATCH ... RETURN ...
    Graph(GraphQuery),
    /// Join between table and graph
    Join(JoinQuery),
    /// Path query: PATH FROM ... TO ...
    Path(PathQuery),
    /// Vector similarity search
    Vector(VectorQuery),
    /// Hybrid query combining structured and vector search
    Hybrid(HybridQuery),
    /// INSERT INTO table (cols) VALUES (vals)
    Insert(InsertQuery),
    /// UPDATE table SET col=val WHERE filter
    Update(UpdateQuery),
    /// DELETE FROM table WHERE filter
    Delete(DeleteQuery),
    /// CREATE TABLE name (columns)
    CreateTable(CreateTableQuery),
    /// CREATE COLLECTION name KIND kind
    CreateCollection(CreateCollectionQuery),
    /// CREATE VECTOR name DIM n [METRIC metric]
    CreateVector(CreateVectorQuery),
    /// DROP TABLE name
    DropTable(DropTableQuery),
    /// DROP GRAPH name
    DropGraph(DropGraphQuery),
    /// DROP VECTOR name
    DropVector(DropVectorQuery),
    /// DROP DOCUMENT name
    DropDocument(DropDocumentQuery),
    /// DROP KV name
    DropKv(DropKvQuery),
    /// DROP COLLECTION name
    DropCollection(DropCollectionQuery),
    /// TRUNCATE [model] name
    Truncate(TruncateQuery),
    /// ALTER TABLE name ADD/DROP/RENAME COLUMN
    AlterTable(AlterTableQuery),
    /// GRAPH subcommand (NEIGHBORHOOD, SHORTEST_PATH, etc.)
    GraphCommand(GraphCommand),
    /// SEARCH subcommand (SIMILAR, TEXT, HYBRID)
    SearchCommand(SearchCommand),
    /// ASK 'question' — RAG query with LLM synthesis
    Ask(AskQuery),
    /// CREATE INDEX name ON table (columns) USING type
    CreateIndex(CreateIndexQuery),
    /// DROP INDEX name ON table
    DropIndex(DropIndexQuery),
    /// Probabilistic data structure commands (HLL, SKETCH, FILTER)
    ProbabilisticCommand(ProbabilisticCommand),
    /// CREATE TIMESERIES name [RETENTION duration] [CHUNK_SIZE n]
    CreateTimeSeries(CreateTimeSeriesQuery),
    /// DROP TIMESERIES name
    DropTimeSeries(DropTimeSeriesQuery),
    /// CREATE QUEUE name [MAX_SIZE n] [PRIORITY] [WITH TTL duration]
    CreateQueue(CreateQueueQuery),
    /// ALTER QUEUE name SET MODE [FANOUT|WORK]
    AlterQueue(AlterQueueQuery),
    /// DROP QUEUE name
    DropQueue(DropQueueQuery),
    /// Read-only queue projection: SELECT ... FROM QUEUE name
    QueueSelect(QueueSelectQuery),
    /// QUEUE subcommand (PUSH, POP, PEEK, LEN, PURGE, GROUP, READ, ACK, NACK)
    QueueCommand(QueueCommand),
    /// KV subcommand (PUT, GET, DELETE)
    KvCommand(KvCommand),
    /// CONFIG keyed command (PUT, GET, ROTATE, DELETE, HISTORY)
    ConfigCommand(ConfigCommand),
    /// CREATE TREE name IN collection ROOT ... MAX_CHILDREN n
    CreateTree(CreateTreeQuery),
    /// DROP TREE name IN collection
    DropTree(DropTreeQuery),
    /// TREE subcommand (INSERT, MOVE, DELETE, VALIDATE, REBALANCE)
    TreeCommand(TreeCommand),
    /// SET CONFIG key = value
    SetConfig { key: String, value: Value },
    /// SHOW CONFIG [prefix]
    ShowConfig { prefix: Option<String> },
    /// SET SECRET key = value
    SetSecret { key: String, value: Value },
    /// DELETE SECRET key
    DeleteSecret { key: String },
    /// SHOW SECRET[S] [prefix]
    ShowSecrets { prefix: Option<String> },
    /// `SET TENANT 'id'` / `SET TENANT = 'id'` / `RESET TENANT`
    ///
    /// Session-scoped multi-tenancy handle. Populates a per-connection
    /// thread-local that `CURRENT_TENANT()` reads and that RLS
    /// policies combine with via `USING (tenant_id = CURRENT_TENANT())`.
    /// `None` clears the current tenant (RESET TENANT or SET TENANT
    /// NULL). Unlike `SetConfig` this is *not* persisted to red_config —
    /// it lives for the connection's lifetime only.
    SetTenant(Option<String>),
    /// `SHOW TENANT` — returns the thread-local tenant id (or NULL).
    ShowTenant,
    /// EXPLAIN ALTER FOR CREATE TABLE name (...) [FORMAT JSON]
    ///
    /// Pure read command that diffs the embedded `CREATE TABLE`
    /// statement against the live `CollectionContract` of the
    /// table with the same name and returns the `ALTER TABLE`
    /// operations that would close the gap. Never executes
    /// anything — output is text (default) or JSON depending on
    /// the optional `FORMAT JSON` suffix. Powers the Purple
    /// framework's migration generator and any other client that
    /// wants reddb to own the schema-diff rules.
    ExplainAlter(ExplainAlterQuery),
    /// CREATE MIGRATION name [DEPENDS ON dep1, dep2] [BATCH n ROWS] [NO ROLLBACK] body
    CreateMigration(CreateMigrationQuery),
    /// APPLY MIGRATION name | APPLY MIGRATION *  [FOR TENANT id]
    ApplyMigration(ApplyMigrationQuery),
    /// ROLLBACK MIGRATION name
    RollbackMigration(RollbackMigrationQuery),
    /// EXPLAIN MIGRATION name
    ExplainMigration(ExplainMigrationQuery),
    /// `EVENTS BACKFILL collection [WHERE pred] TO queue [LIMIT n]`.
    EventsBackfill(EventsBackfillQuery),
    /// `EVENTS BACKFILL STATUS collection` placeholder for the status slice.
    EventsBackfillStatus { collection: String },
    /// Transaction control: BEGIN, COMMIT, ROLLBACK, SAVEPOINT, RELEASE, ROLLBACK TO.
    ///
    /// Phase 1.1 (PG parity): parser + dispatch are wired so clients (psql, JDBC, etc.)
    /// can issue these statements without errors. Real isolation/atomicity semantics
    /// arrive with Phase 2.3 MVCC. Until then statements behave as autocommit (each
    /// DML is its own transaction); BEGIN/COMMIT/ROLLBACK return success but do NOT
    /// provide rollback-on-failure guarantees across multiple statements.
    TransactionControl(TxnControl),
    /// Maintenance commands: VACUUM [FULL] [table], ANALYZE [table].
    ///
    /// Phase 1.2 (PG parity): `VACUUM` triggers segment/page flush + planner stats
    /// refresh. `ANALYZE` refreshes planner statistics (histograms, null counts,
    /// distinct estimates). Both accept an optional table target; omitting the
    /// target iterates every collection.
    MaintenanceCommand(MaintenanceCommand),
    /// `CREATE SCHEMA [IF NOT EXISTS] name`
    ///
    /// Phase 1.3 (PG parity): schemas are logical namespaces stored in
    /// `red_config` under the key `schema.{name}`. Tables created inside a
    /// schema use `schema.table` qualified names (collection name = "schema.table").
    CreateSchema(CreateSchemaQuery),
    /// `DROP SCHEMA [IF EXISTS] name [CASCADE]`
    DropSchema(DropSchemaQuery),
    /// `CREATE SEQUENCE [IF NOT EXISTS] name [START [WITH] n] [INCREMENT [BY] n]`
    ///
    /// Phase 1.3 (PG parity): sequences are 64-bit monotonic counters persisted
    /// in `red_config` under the key `sequence.{name}`. Values are produced by
    /// the scalar functions `nextval('name')` and `currval('name')`.
    CreateSequence(CreateSequenceQuery),
    /// `DROP SEQUENCE [IF EXISTS] name`
    DropSequence(DropSequenceQuery),
    /// `COPY table FROM 'path' [WITH ...]` — CSV import (Phase 1.5 PG parity).
    ///
    /// Supported options: `DELIMITER c`, `HEADER [true|false]`. Rows stream
    /// into the named collection via the `CsvImporter`.
    CopyFrom(CopyFromQuery),
    /// `CREATE [OR REPLACE] [MATERIALIZED] VIEW [IF NOT EXISTS] name AS SELECT ...`
    ///
    /// Phase 2.1 (PG parity): views are stored as `view.{name}` entries in
    /// `red_config`. Materialized views additionally allocate a slot in the
    /// shared `MaterializedViewCache`; `REFRESH MATERIALIZED VIEW` re-runs
    /// the underlying query and repopulates the cache.
    CreateView(CreateViewQuery),
    /// `DROP [MATERIALIZED] VIEW [IF EXISTS] name`
    DropView(DropViewQuery),
    /// `REFRESH MATERIALIZED VIEW name`
    ///
    /// Re-executes the view's query and writes the result into the cache.
    RefreshMaterializedView(RefreshMaterializedViewQuery),
    /// `CREATE POLICY name ON table [FOR action] [TO role] USING (filter)`
    ///
    /// Phase 2.5 (PG parity): row-level security policy definition.
    /// Evaluated at read time — when the table has RLS enabled, all
    /// matching policies for the current role are combined with OR and
    /// AND-ed into the query's WHERE clause.
    CreatePolicy(CreatePolicyQuery),
    /// `DROP POLICY [IF EXISTS] name ON table`
    DropPolicy(DropPolicyQuery),
    /// `CREATE SERVER name FOREIGN DATA WRAPPER kind OPTIONS (...)`
    /// (Phase 3.2 PG parity). Registers a named foreign-data-wrapper
    /// instance in the runtime's `ForeignTableRegistry`.
    CreateServer(CreateServerQuery),
    /// `DROP SERVER [IF EXISTS] name [CASCADE]`
    DropServer(DropServerQuery),
    /// `CREATE FOREIGN TABLE name (cols) SERVER srv OPTIONS (...)`
    /// (Phase 3.2 PG parity). Makes `name` resolvable as a foreign table
    /// via the parent server's `ForeignDataWrapper`.
    CreateForeignTable(CreateForeignTableQuery),
    /// `DROP FOREIGN TABLE [IF EXISTS] name`
    DropForeignTable(DropForeignTableQuery),
    /// `GRANT { actions | ALL [PRIVILEGES] }
    ///   ON { TABLE | SCHEMA | DATABASE | FUNCTION } object_list
    ///   TO grant_principal_list
    ///   [WITH GRANT OPTION]`
    ///
    /// Granular RBAC primitive layered on top of the legacy 3-role model.
    /// See `crate::auth::privileges` for the resolution algorithm.
    Grant(GrantStmt),
    /// `REVOKE [GRANT OPTION FOR] { actions | ALL } ON … FROM …`
    Revoke(RevokeStmt),
    /// `ALTER USER name [VALID UNTIL 'ts'] [CONNECTION LIMIT n]
    ///   [ENABLE | DISABLE] [SET search_path = ...]`
    AlterUser(AlterUserStmt),
    // ----- IAM policy DDL (Agent #28 / IAM kernel integration) -----
    /// `CREATE POLICY '<id>' AS '<json>'` — installs an IAM policy
    /// document in the AuthStore. Distinct from the RLS-flavoured
    /// `CreatePolicy(CreatePolicyQuery)` above (which uses
    /// `CREATE POLICY name ON table ...`); the parser disambiguates
    /// at parse time by inspecting the token after the policy name.
    CreateIamPolicy { id: String, json: String },
    /// `DROP POLICY '<id>'` — removes an IAM policy and its
    /// attachments.
    DropIamPolicy { id: String },
    /// `ATTACH POLICY '<id>' TO USER <name>` /
    /// `ATTACH POLICY '<id>' TO GROUP <name>`.
    AttachPolicy {
        policy_id: String,
        principal: PolicyPrincipalRef,
    },
    /// `DETACH POLICY '<id>' FROM USER <name>` /
    /// `DETACH POLICY '<id>' FROM GROUP <name>`.
    DetachPolicy {
        policy_id: String,
        principal: PolicyPrincipalRef,
    },
    /// `SHOW POLICIES [FOR USER <name> | FOR GROUP <name>]`.
    ShowPolicies { filter: Option<PolicyPrincipalRef> },
    /// `SHOW EFFECTIVE PERMISSIONS FOR <name> [ON <kind>:<name>]`.
    ShowEffectivePermissions {
        user: PolicyUserRef,
        resource: Option<PolicyResourceRef>,
    },
    /// `SIMULATE <name> ACTION <verb> ON <kind>:<name>`.
    SimulatePolicy {
        user: PolicyUserRef,
        action: String,
        resource: PolicyResourceRef,
    },
}

/// Tenant-qualified user reference for IAM policy SQL DDL.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PolicyUserRef {
    pub tenant: Option<String>,
    pub username: String,
}

/// Resource reference (`<kind>:<name>`) used in `SIMULATE` /
/// `SHOW EFFECTIVE PERMISSIONS`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PolicyResourceRef {
    pub kind: String,
    pub name: String,
}

/// Principal target for ATTACH / DETACH / SHOW POLICIES filter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PolicyPrincipalRef {
    User(PolicyUserRef),
    Group(String),
}

// ---------------------------------------------------------------------------
// GRANT / REVOKE / ALTER USER AST
// ---------------------------------------------------------------------------

/// Object class targeted by a GRANT/REVOKE.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GrantObjectKind {
    Table,
    Schema,
    Database,
    Function,
}

/// One target object in a `GRANT ... ON ... <object_list>` clause.
///
/// `name` follows the parser's standard `[schema.]object` shape; the
/// optional `schema` is only populated for `Table` / `Function` and
/// stays `None` when the user wrote a bare identifier.
#[derive(Debug, Clone)]
pub struct GrantObject {
    pub schema: Option<String>,
    pub name: String,
}

/// Principal target of a GRANT (i.e. the recipient).
#[derive(Debug, Clone)]
pub enum GrantPrincipalRef {
    /// `TO username` — username may include an `@tenant` suffix.
    User {
        tenant: Option<String>,
        name: String,
    },
    /// `TO PUBLIC`.
    Public,
    /// `TO GROUP groupname` (parsed today, enforcement deferred).
    Group(String),
}

/// `GRANT` statement AST.
#[derive(Debug, Clone)]
pub struct GrantStmt {
    /// Privilege keywords as the user typed them, normalised to upper
    /// case. Matches the `Action` set in `crate::auth::privileges`. An
    /// empty list together with `all = true` represents `ALL [PRIVILEGES]`.
    pub actions: Vec<String>,
    /// Optional column list — populates the AST for column-level
    /// grants but enforcement is deferred (stretch goal).
    pub columns: Option<Vec<String>>,
    pub object_kind: GrantObjectKind,
    pub objects: Vec<GrantObject>,
    pub principals: Vec<GrantPrincipalRef>,
    pub with_grant_option: bool,
    /// `true` when the privilege list was `ALL [PRIVILEGES]`.
    pub all: bool,
}

/// `REVOKE` statement AST.
#[derive(Debug, Clone)]
pub struct RevokeStmt {
    pub actions: Vec<String>,
    pub columns: Option<Vec<String>>,
    pub object_kind: GrantObjectKind,
    pub objects: Vec<GrantObject>,
    pub principals: Vec<GrantPrincipalRef>,
    /// `REVOKE GRANT OPTION FOR ...` — strips just the grant option,
    /// keeping the underlying privilege.
    pub grant_option_for: bool,
    pub all: bool,
}

/// One attribute setting under `ALTER USER`.
#[derive(Debug, Clone)]
pub enum AlterUserAttribute {
    ValidUntil(String),
    ConnectionLimit(i64),
    Enable,
    Disable,
    SetSearchPath(String),
    AddGroup(String),
    DropGroup(String),
    /// Reset password (carry the new plaintext until the runtime
    /// hands it to AuthStore::change_password). Out of scope for the
    /// initial milestone — present so the parser can accept the
    /// keyword without a follow-up grammar change.
    Password(String),
}

/// `ALTER USER` statement AST.
#[derive(Debug, Clone)]
pub struct AlterUserStmt {
    pub tenant: Option<String>,
    pub username: String,
    pub attributes: Vec<AlterUserAttribute>,
}

#[derive(Debug, Clone)]
pub struct CreateServerQuery {
    pub name: String,
    /// Wrapper kind declared in `FOREIGN DATA WRAPPER <kind>`.
    pub wrapper: String,
    /// Generic `(key 'value', ...)` option bag.
    pub options: Vec<(String, String)>,
    pub if_not_exists: bool,
}

#[derive(Debug, Clone)]
pub struct DropServerQuery {
    pub name: String,
    pub if_exists: bool,
    pub cascade: bool,
}

#[derive(Debug, Clone)]
pub struct CreateForeignTableQuery {
    pub name: String,
    pub server: String,
    pub columns: Vec<ForeignColumnDef>,
    pub options: Vec<(String, String)>,
    pub if_not_exists: bool,
}

#[derive(Debug, Clone)]
pub struct ForeignColumnDef {
    pub name: String,
    pub data_type: String,
    pub not_null: bool,
}

#[derive(Debug, Clone)]
pub struct DropForeignTableQuery {
    pub name: String,
    pub if_exists: bool,
}

/// Row-level security policy definition.
#[derive(Debug, Clone)]
pub struct CreatePolicyQuery {
    pub name: String,
    pub table: String,
    /// Which action this policy gates. `None` = `ALL` (applies to all four).
    pub action: Option<PolicyAction>,
    /// Role the policy applies to. `None` = all roles.
    pub role: Option<String>,
    /// Boolean predicate the row must satisfy.
    pub using: Box<Filter>,
    /// Entity kind this policy targets (Phase 2.5.5 RLS universal).
    /// `CREATE POLICY p ON t ...` defaults to `Table`; writing
    /// `ON NODES OF g` / `ON VECTORS OF v` / `ON MESSAGES OF q` /
    /// `ON POINTS OF ts` / `ON EDGES OF g` targets the matching
    /// non-tabular kind. The evaluator filters polices by kind so
    /// a graph policy only gates graph reads, vector policy only
    /// gates vector reads, etc.
    pub target_kind: PolicyTargetKind,
}

/// Which flavour of entity a policy governs (Phase 2.5.5).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PolicyTargetKind {
    Table,
    Nodes,
    Edges,
    Vectors,
    Messages,
    Points,
    Documents,
}

impl PolicyTargetKind {
    /// Lowercase identifier for UX — used in messages and the
    /// `red_config.rls.policies.*` persistence key.
    pub fn as_ident(&self) -> &'static str {
        match self {
            Self::Table => "table",
            Self::Nodes => "nodes",
            Self::Edges => "edges",
            Self::Vectors => "vectors",
            Self::Messages => "messages",
            Self::Points => "points",
            Self::Documents => "documents",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PolicyAction {
    Select,
    Insert,
    Update,
    Delete,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DropPolicyQuery {
    pub name: String,
    pub table: String,
    pub if_exists: bool,
}

#[derive(Debug, Clone)]
pub struct CreateViewQuery {
    pub name: String,
    /// Parsed `SELECT ...` body. Stored as a boxed `QueryExpr` so the
    /// runtime can substitute the tree directly when a query references
    /// this view (no re-parsing per read).
    pub query: Box<QueryExpr>,
    pub materialized: bool,
    pub if_not_exists: bool,
    /// `CREATE OR REPLACE VIEW` — overwrites any existing definition.
    pub or_replace: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DropViewQuery {
    pub name: String,
    pub materialized: bool,
    pub if_exists: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefreshMaterializedViewQuery {
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CopyFromQuery {
    pub table: String,
    pub path: String,
    pub format: CopyFormat,
    pub delimiter: Option<char>,
    pub has_header: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CopyFormat {
    Csv,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateSchemaQuery {
    pub name: String,
    pub if_not_exists: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DropSchemaQuery {
    pub name: String,
    pub if_exists: bool,
    pub cascade: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateSequenceQuery {
    pub name: String,
    pub if_not_exists: bool,
    /// First value produced by `nextval`. Default 1.
    pub start: i64,
    /// Added to the current value on each `nextval`. Default 1.
    pub increment: i64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DropSequenceQuery {
    pub name: String,
    pub if_exists: bool,
}

/// Transaction-control statement variants. See [`QueryExpr::TransactionControl`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TxnControl {
    /// `BEGIN [WORK | TRANSACTION]`, `START TRANSACTION`
    Begin,
    /// `COMMIT [WORK | TRANSACTION]`, `END`
    Commit,
    /// `ROLLBACK [WORK | TRANSACTION]`
    Rollback,
    /// `SAVEPOINT name`
    Savepoint(String),
    /// `RELEASE [SAVEPOINT] name`
    ReleaseSavepoint(String),
    /// `ROLLBACK TO [SAVEPOINT] name`
    RollbackToSavepoint(String),
}

/// Maintenance command variants. See [`QueryExpr::MaintenanceCommand`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MaintenanceCommand {
    /// `VACUUM [FULL] [table]`
    ///
    /// Triggers segment compaction and planner stats refresh. `FULL` additionally
    /// forces a full pager sync. Target `None` applies to every collection.
    Vacuum { target: Option<String>, full: bool },
    /// `ANALYZE [table]`
    ///
    /// Refreshes planner statistics (histogram, distinct estimates, null counts).
    /// Target `None` re-analyzes every collection.
    Analyze { target: Option<String> },
}

/// AST node for `EXPLAIN ALTER FOR <CreateTableStmt> [FORMAT JSON]`.
///
/// `target` carries the CREATE TABLE structure exactly as the
/// parser produces it for a regular CREATE — full reuse of
/// `parse_create_table_body`. `format` determines whether the
/// executor emits a `ALTER TABLE …;`-flavored text payload
/// (the default — copy-paste friendly into the REPL) or a
/// structured JSON object (machine-friendly).
#[derive(Debug, Clone)]
pub struct ExplainAlterQuery {
    pub target: CreateTableQuery,
    pub format: ExplainFormat,
}

/// Output format requested for an `EXPLAIN ALTER` command.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ExplainFormat {
    /// Plain SQL text — `ALTER TABLE …;` lines plus header
    /// comments and rename hints. Default; copy-paste friendly.
    #[default]
    Sql,
    /// Structured JSON object with `operations`,
    /// `rename_candidates`, `summary`. Machine-friendly for
    /// driver code (Purple migration generator, dashboards,
    /// CLI tools).
    Json,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CreateMigrationQuery {
    pub name: String,
    pub body: String,
    pub depends_on: Vec<String>,
    pub batch_size: Option<u64>,
    pub no_rollback: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ApplyMigrationQuery {
    pub target: ApplyMigrationTarget,
    pub for_tenant: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ApplyMigrationTarget {
    Named(String),
    All,
}

#[derive(Debug, Clone, PartialEq)]
pub struct RollbackMigrationQuery {
    pub name: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ExplainMigrationQuery {
    pub name: String,
}

/// Probabilistic data structure commands
#[derive(Debug, Clone)]
pub enum ProbabilisticCommand {
    // HyperLogLog
    CreateHll {
        name: String,
        precision: u8,
        if_not_exists: bool,
    },
    HllAdd {
        name: String,
        elements: Vec<String>,
    },
    HllCount {
        names: Vec<String>,
    },
    HllMerge {
        dest: String,
        sources: Vec<String>,
    },
    HllInfo {
        name: String,
    },
    DropHll {
        name: String,
        if_exists: bool,
    },

    // Count-Min Sketch (Fase 7)
    CreateSketch {
        name: String,
        width: usize,
        depth: usize,
        if_not_exists: bool,
    },
    SketchAdd {
        name: String,
        element: String,
        count: u64,
    },
    SketchCount {
        name: String,
        element: String,
    },
    SketchMerge {
        dest: String,
        sources: Vec<String>,
    },
    SketchInfo {
        name: String,
    },
    DropSketch {
        name: String,
        if_exists: bool,
    },

    // Cuckoo Filter (Fase 8)
    CreateFilter {
        name: String,
        capacity: usize,
        if_not_exists: bool,
    },
    FilterAdd {
        name: String,
        element: String,
    },
    FilterCheck {
        name: String,
        element: String,
    },
    FilterDelete {
        name: String,
        element: String,
    },
    FilterCount {
        name: String,
    },
    FilterInfo {
        name: String,
    },
    DropFilter {
        name: String,
        if_exists: bool,
    },
}

/// Index type for CREATE INDEX ... USING <type>
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexMethod {
    BTree,
    Hash,
    Bitmap,
    RTree,
}

impl fmt::Display for IndexMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BTree => write!(f, "BTREE"),
            Self::Hash => write!(f, "HASH"),
            Self::Bitmap => write!(f, "BITMAP"),
            Self::RTree => write!(f, "RTREE"),
        }
    }
}

/// CREATE INDEX [UNIQUE] [IF NOT EXISTS] name ON table (col1, col2, ...) [USING method]
#[derive(Debug, Clone)]
pub struct CreateIndexQuery {
    pub name: String,
    pub table: String,
    pub columns: Vec<String>,
    pub method: IndexMethod,
    pub unique: bool,
    pub if_not_exists: bool,
}

/// DROP INDEX [IF EXISTS] name ON table
#[derive(Debug, Clone)]
pub struct DropIndexQuery {
    pub name: String,
    pub table: String,
    pub if_exists: bool,
}

/// ASK 'question' [USING provider] [MODEL 'model'] [DEPTH n] [LIMIT n] [MIN_SCORE x]
///                [COLLECTION col] [TEMPERATURE x] [SEED n] [STRICT ON|OFF] [STREAM]
///                [CACHE TTL '5m' | NOCACHE]
///
/// `temperature` and `seed` are per-query overrides resolved by the
/// `DeterminismDecider` (issue #400). The parser merely surfaces the
/// requested values; capability-based dropping happens at decide time.
#[derive(Debug, Clone)]
pub struct AskQuery {
    /// `EXPLAIN ASK '...'` returns the retrieval/provider/cost plan
    /// without making the LLM call.
    pub explain: bool,
    pub question: String,
    /// Optional `$N` / `?` parameter slot for the question text.
    pub question_param: Option<usize>,
    pub provider: Option<String>,
    pub model: Option<String>,
    pub depth: Option<usize>,
    pub limit: Option<usize>,
    pub min_score: Option<f32>,
    pub collection: Option<String>,
    /// Per-query temperature override (`ASK '...' TEMPERATURE 0.7`).
    /// `None` means fall back to `ask.default_temperature`.
    pub temperature: Option<f32>,
    /// Per-query seed override (`ASK '...' SEED 42`). `None` means the
    /// decider derives one from `hash(question + sources_fingerprint)`.
    pub seed: Option<u64>,
    /// Strict citation validation is on by default. `STRICT OFF` keeps
    /// citation diagnostics as warnings and skips retry/error handling.
    pub strict: bool,
    /// HTTP-only SSE response requested via `ASK '...' STREAM`.
    pub stream: bool,
    /// Per-query answer-cache override.
    pub cache: AskCacheClause,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AskCacheClause {
    Default,
    CacheTtl(String),
    NoCache,
}

impl Default for AskCacheClause {
    fn default() -> Self {
        Self::Default
    }
}

impl QueryExpr {
    /// Create a table query
    pub fn table(name: &str) -> TableQueryBuilder {
        TableQueryBuilder::new(name)
    }

    /// Create a graph query
    pub fn graph() -> GraphQueryBuilder {
        GraphQueryBuilder::new()
    }

    /// Create a path query
    pub fn path(from: NodeSelector, to: NodeSelector) -> PathQueryBuilder {
        PathQueryBuilder::new(from, to)
    }
}

// ============================================================================
// Table Query
// ============================================================================

/// Table query: SELECT columns FROM table WHERE filter ORDER BY ... LIMIT ...
#[derive(Debug, Clone)]
pub struct TableQuery {
    /// Table name. Legacy slot — still populated even when `source`
    /// is set to a subquery so existing call sites that read
    /// `query.table.as_str()` keep compiling. When `source` is
    /// `Some(TableSource::Subquery(…))`, this field holds a synthetic
    /// sentinel name (`"__subq_NNNN"`) that runtime code must never
    /// resolve against the real schema registry.
    pub table: String,
    /// Fase 2 Week 3: structured table source. `None` means the
    /// legacy `table` field is authoritative. `Some(Name)` is the
    /// same information as `table` but in typed form. `Some(Subquery)`
    /// wires a `(SELECT …) AS alias` in a FROM position — the Fase
    /// 1.7 unlock.
    pub source: Option<TableSource>,
    /// Optional table alias
    pub alias: Option<String>,
    /// Canonical SQL select list.
    pub select_items: Vec<SelectItem>,
    /// Columns to select (empty = all)
    pub columns: Vec<Projection>,
    /// Canonical SQL WHERE clause.
    pub where_expr: Option<super::Expr>,
    /// Filter condition
    pub filter: Option<Filter>,
    /// Canonical SQL GROUP BY items.
    pub group_by_exprs: Vec<super::Expr>,
    /// GROUP BY fields
    pub group_by: Vec<String>,
    /// Canonical SQL HAVING clause.
    pub having_expr: Option<super::Expr>,
    /// HAVING filter (applied after grouping)
    pub having: Option<Filter>,
    /// Order by clauses
    pub order_by: Vec<OrderByClause>,
    /// Limit
    pub limit: Option<u64>,
    /// User-supplied-parameter slot for `LIMIT $N`. Set by the parser
    /// when the LIMIT clause references `$N`/`?` instead of a literal;
    /// cleared by the binder (`user_params::bind`) after substituting
    /// the parameter into `limit`. Mirrors the `limit_param` slot on
    /// `SearchCommand` variants — see #361 slice 11.
    pub limit_param: Option<usize>,
    /// Offset
    pub offset: Option<u64>,
    /// User-supplied-parameter slot for `OFFSET $N`. Same lifecycle as
    /// `limit_param`. See #361 slice 11.
    pub offset_param: Option<usize>,
    /// WITH EXPAND options (graph traversal, cross-ref following)
    pub expand: Option<ExpandOptions>,
    /// Time-travel anchor. When present the executor resolves this
    /// to an MVCC xid and evaluates the query against that snapshot
    /// instead of the current one. Mirrors git's `AS OF` semantics.
    pub as_of: Option<AsOfClause>,
}

/// Source spec for `AS OF` — parsed form sits in `TableQuery`, then
/// `vcs_resolve_as_of` turns it into an MVCC xid at execute time.
#[derive(Debug, Clone)]
pub enum AsOfClause {
    /// Explicit commit hash literal: `AS OF COMMIT '<hex>'`.
    Commit(String),
    /// Branch or ref: `AS OF BRANCH 'main'` or `AS OF 'refs/heads/main'`.
    Branch(String),
    /// Tag: `AS OF TAG 'v1.0'`.
    Tag(String),
    /// Unix epoch milliseconds: `AS OF TIMESTAMP 1710000000000`.
    TimestampMs(i64),
    /// Raw MVCC snapshot xid: `AS OF SNAPSHOT 12345`.
    Snapshot(u64),
}

/// Structured FROM source for a `TableQuery`. Additive alongside the
/// legacy `TableQuery.table: String` slot — callers that understand
/// this type can branch on subqueries; callers that only read `table`
/// fall back to the synthetic sentinel name and, for subqueries,
/// produce an "unknown table" error until they migrate.
#[derive(Debug, Clone)]
pub enum TableSource {
    /// Plain table reference — equivalent to the legacy `String` form.
    Name(String),
    /// A subquery in FROM position: `FROM (SELECT …) AS alias`.
    Subquery(Box<QueryExpr>),
}

/// Options for WITH EXPAND clause on SELECT queries.
#[derive(Debug, Clone, Default)]
pub struct ExpandOptions {
    /// Expand via graph edges (WITH EXPAND GRAPH)
    pub graph: bool,
    /// Graph expansion depth (DEPTH n)
    pub graph_depth: usize,
    /// Expand via cross-references (WITH EXPAND CROSS_REFS)
    pub cross_refs: bool,
    /// Index hint from the optimizer (which index to prefer for this query)
    pub index_hint: Option<crate::storage::query::planner::optimizer::IndexHint>,
}

impl TableQuery {
    /// Create a new table query
    pub fn new(table: &str) -> Self {
        Self {
            table: table.to_string(),
            source: None,
            alias: None,
            select_items: Vec::new(),
            columns: Vec::new(),
            where_expr: None,
            filter: None,
            group_by_exprs: Vec::new(),
            group_by: Vec::new(),
            having_expr: None,
            having: None,
            order_by: Vec::new(),
            limit: None,
            limit_param: None,
            offset: None,
            offset_param: None,
            expand: None,
            as_of: None,
        }
    }

    /// Create a TableQuery that wraps a subquery in FROM position.
    /// The legacy `table` slot holds a synthetic sentinel so code that
    /// only reads `table.as_str()` errors loudly with a
    /// recognisable marker instead of silently treating it as a
    /// real collection.
    pub fn from_subquery(subquery: QueryExpr, alias: Option<String>) -> Self {
        let sentinel = match &alias {
            Some(a) => format!("__subq_{a}"),
            None => "__subq_anon".to_string(),
        };
        Self {
            table: sentinel,
            source: Some(TableSource::Subquery(Box::new(subquery))),
            alias,
            select_items: Vec::new(),
            columns: Vec::new(),
            where_expr: None,
            filter: None,
            group_by_exprs: Vec::new(),
            group_by: Vec::new(),
            having_expr: None,
            having: None,
            order_by: Vec::new(),
            limit: None,
            limit_param: None,
            offset: None,
            offset_param: None,
            expand: None,
            as_of: None,
        }
    }
}

/// Canonical SQL select item for table queries.
#[derive(Debug, Clone, PartialEq)]
pub enum SelectItem {
    Wildcard,
    Expr {
        expr: super::Expr,
        alias: Option<String>,
    },
}

// ============================================================================
// Graph Query
// ============================================================================

/// Graph query: MATCH pattern WHERE filter RETURN projection
#[derive(Debug, Clone)]
pub struct GraphQuery {
    /// Optional outer alias when used as a join source
    pub alias: Option<String>,
    /// Graph pattern to match
    pub pattern: GraphPattern,
    /// Filter condition
    pub filter: Option<Filter>,
    /// Return projections
    pub return_: Vec<Projection>,
    /// Optional row limit
    pub limit: Option<u64>,
}

impl GraphQuery {
    /// Create a new graph query
    pub fn new(pattern: GraphPattern) -> Self {
        Self {
            alias: None,
            pattern,
            filter: None,
            return_: Vec::new(),
            limit: None,
        }
    }

    /// Set outer alias
    pub fn alias(mut self, alias: &str) -> Self {
        self.alias = Some(alias.to_string());
        self
    }
}

/// Graph pattern: collection of node and edge patterns
#[derive(Debug, Clone, Default)]
pub struct GraphPattern {
    /// Node patterns
    pub nodes: Vec<NodePattern>,
    /// Edge patterns connecting nodes
    pub edges: Vec<EdgePattern>,
}

impl GraphPattern {
    /// Create an empty pattern
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a node pattern
    pub fn node(mut self, pattern: NodePattern) -> Self {
        self.nodes.push(pattern);
        self
    }

    /// Add an edge pattern
    pub fn edge(mut self, pattern: EdgePattern) -> Self {
        self.edges.push(pattern);
        self
    }
}

/// Node pattern: (alias:Type {properties})
#[derive(Debug, Clone)]
pub struct NodePattern {
    /// Variable alias for this node
    pub alias: String,
    /// Optional label filter. Stored as the user-supplied label string so
    /// the parser is registry-free; executors resolve it against the live
    /// [`crate::storage::engine::graph_store::LabelRegistry`].
    pub node_label: Option<String>,
    /// Property filters
    pub properties: Vec<PropertyFilter>,
}

impl NodePattern {
    /// Create a new node pattern
    pub fn new(alias: &str) -> Self {
        Self {
            alias: alias.to_string(),
            node_label: None,
            properties: Vec::new(),
        }
    }

    /// Set the label filter (string form — preferred).
    pub fn of_label(mut self, label: impl Into<String>) -> Self {
        self.node_label = Some(label.into());
        self
    }

    /// Add property filter
    pub fn with_property(mut self, name: &str, op: CompareOp, value: Value) -> Self {
        self.properties.push(PropertyFilter {
            name: name.to_string(),
            op,
            value,
        });
        self
    }
}

/// Edge pattern: -[alias:Type*min..max]->
#[derive(Debug, Clone)]
pub struct EdgePattern {
    /// Optional alias for this edge
    pub alias: Option<String>,
    /// Source node alias
    pub from: String,
    /// Target node alias
    pub to: String,
    /// Optional label filter (user-supplied string).
    pub edge_label: Option<String>,
    /// Edge direction
    pub direction: EdgeDirection,
    /// Minimum hops (for variable-length patterns)
    pub min_hops: u32,
    /// Maximum hops (for variable-length patterns)
    pub max_hops: u32,
}

impl EdgePattern {
    /// Create a new edge pattern
    pub fn new(from: &str, to: &str) -> Self {
        Self {
            alias: None,
            from: from.to_string(),
            to: to.to_string(),
            edge_label: None,
            direction: EdgeDirection::Outgoing,
            min_hops: 1,
            max_hops: 1,
        }
    }

    /// Set label filter (string form — preferred).
    pub fn of_label(mut self, label: impl Into<String>) -> Self {
        self.edge_label = Some(label.into());
        self
    }

    /// Set direction
    pub fn direction(mut self, dir: EdgeDirection) -> Self {
        self.direction = dir;
        self
    }

    /// Set hop range for variable-length patterns
    pub fn hops(mut self, min: u32, max: u32) -> Self {
        self.min_hops = min;
        self.max_hops = max;
        self
    }

    /// Set alias
    pub fn alias(mut self, alias: &str) -> Self {
        self.alias = Some(alias.to_string());
        self
    }
}

/// Edge direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EdgeDirection {
    /// Outgoing: (a)-[r]->(b)
    Outgoing,
    /// Incoming: (a)<-[r]-(b)
    Incoming,
    /// Both: (a)-[r]-(b)
    Both,
}

/// Property filter: name op value
#[derive(Debug, Clone)]
pub struct PropertyFilter {
    pub name: String,
    pub op: CompareOp,
    pub value: Value,
}

// ============================================================================
// Join Query
// ============================================================================

/// Join query: combines table and graph queries
#[derive(Debug, Clone)]
pub struct JoinQuery {
    /// Left side (typically table)
    pub left: Box<QueryExpr>,
    /// Right side (typically graph)
    pub right: Box<QueryExpr>,
    /// Join type
    pub join_type: JoinType,
    /// Join condition
    pub on: JoinCondition,
    /// Post-join filter condition
    pub filter: Option<Filter>,
    /// Post-join ordering
    pub order_by: Vec<OrderByClause>,
    /// Post-join limit
    pub limit: Option<u64>,
    /// Post-join offset
    pub offset: Option<u64>,
    /// Canonical SQL RETURN projection.
    pub return_items: Vec<SelectItem>,
    /// Post-join projection
    pub return_: Vec<Projection>,
}

impl JoinQuery {
    /// Create a new join query
    pub fn new(left: QueryExpr, right: QueryExpr, on: JoinCondition) -> Self {
        Self {
            left: Box::new(left),
            right: Box::new(right),
            join_type: JoinType::Inner,
            on,
            filter: None,
            order_by: Vec::new(),
            limit: None,
            offset: None,
            return_items: Vec::new(),
            return_: Vec::new(),
        }
    }

    /// Set join type
    pub fn join_type(mut self, jt: JoinType) -> Self {
        self.join_type = jt;
        self
    }
}

/// Join type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
    /// Inner join — only matching pairs emitted
    Inner,
    /// Left outer join — every left row, matched or padded with nulls on the right
    LeftOuter,
    /// Right outer join — every right row, matched or padded with nulls on the left
    RightOuter,
    /// Full outer join — LeftOuter ∪ RightOuter, each unmatched side padded
    FullOuter,
    /// Cross join — Cartesian product, no predicate
    Cross,
}

/// Join condition: how to match rows with nodes
#[derive(Debug, Clone)]
pub struct JoinCondition {
    /// Left field (table side)
    pub left_field: FieldRef,
    /// Right field (graph side)
    pub right_field: FieldRef,
}

impl JoinCondition {
    /// Create a new join condition
    pub fn new(left: FieldRef, right: FieldRef) -> Self {
        Self {
            left_field: left,
            right_field: right,
        }
    }
}

/// Reference to a field (table column, node property, or edge property)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FieldRef {
    /// Table column: table.column
    TableColumn { table: String, column: String },
    /// Node property: alias.property
    NodeProperty { alias: String, property: String },
    /// Edge property: alias.property
    EdgeProperty { alias: String, property: String },
    /// Node ID: alias.id
    NodeId { alias: String },
}

impl FieldRef {
    /// Create a table column reference
    pub fn column(table: &str, column: &str) -> Self {
        Self::TableColumn {
            table: table.to_string(),
            column: column.to_string(),
        }
    }

    /// Create a node property reference
    pub fn node_prop(alias: &str, property: &str) -> Self {
        Self::NodeProperty {
            alias: alias.to_string(),
            property: property.to_string(),
        }
    }

    /// Create a node ID reference
    pub fn node_id(alias: &str) -> Self {
        Self::NodeId {
            alias: alias.to_string(),
        }
    }

    /// Create an edge property reference
    pub fn edge_prop(alias: &str, property: &str) -> Self {
        Self::EdgeProperty {
            alias: alias.to_string(),
            property: property.to_string(),
        }
    }
}

// ============================================================================
// Path Query
// ============================================================================

/// Path query: find paths between nodes
#[derive(Debug, Clone)]
pub struct PathQuery {
    /// Optional outer alias when used as a join source
    pub alias: Option<String>,
    /// Source node selector
    pub from: NodeSelector,
    /// Target node selector
    pub to: NodeSelector,
    /// Edge labels to traverse (empty = any). Strings are resolved against
    /// the runtime registry by the executor.
    pub via: Vec<String>,
    /// Maximum path length
    pub max_length: u32,
    /// Filter on paths
    pub filter: Option<Filter>,
    /// Return projections
    pub return_: Vec<Projection>,
}

impl PathQuery {
    /// Create a new path query
    pub fn new(from: NodeSelector, to: NodeSelector) -> Self {
        Self {
            alias: None,
            from,
            to,
            via: Vec::new(),
            max_length: 10,
            filter: None,
            return_: Vec::new(),
        }
    }

    /// Set outer alias
    pub fn alias(mut self, alias: &str) -> Self {
        self.alias = Some(alias.to_string());
        self
    }

    /// Add an edge label constraint to traverse (string form).
    pub fn via_label(mut self, label: impl Into<String>) -> Self {
        self.via.push(label.into());
        self
    }
}

/// Node selector for path queries
#[derive(Debug, Clone)]
pub enum NodeSelector {
    /// By node ID
    ById(String),
    /// By node label and property
    ByType {
        node_label: String,
        filter: Option<PropertyFilter>,
    },
    /// By table row (linked node)
    ByRow { table: String, row_id: u64 },
}

impl NodeSelector {
    /// Select by node ID
    pub fn by_id(id: &str) -> Self {
        Self::ById(id.to_string())
    }

    /// Select by label string (preferred).
    pub fn by_label(label: impl Into<String>) -> Self {
        Self::ByType {
            node_label: label.into(),
            filter: None,
        }
    }

    /// Select by table row
    pub fn by_row(table: &str, row_id: u64) -> Self {
        Self::ByRow {
            table: table.to_string(),
            row_id,
        }
    }
}

// ============================================================================
// Vector Query
// ============================================================================

/// Vector similarity search query
///
/// ```text
/// VECTOR SEARCH embeddings
/// SIMILAR TO [0.1, 0.2, ..., 0.5]
/// WHERE metadata.source = 'nmap'
/// LIMIT 10
/// ```
#[derive(Debug, Clone)]
pub struct VectorQuery {
    /// Optional outer alias when used as a join source
    pub alias: Option<String>,
    /// Collection name to search
    pub collection: String,
    /// Query vector (or reference to get vector from)
    pub query_vector: VectorSource,
    /// Number of results to return
    pub k: usize,
    /// Metadata filter
    pub filter: Option<MetadataFilter>,
    /// Distance metric to use (defaults to collection's metric)
    pub metric: Option<DistanceMetric>,
    /// Include vectors in results
    pub include_vectors: bool,
    /// Include metadata in results
    pub include_metadata: bool,
    /// Minimum similarity threshold (optional)
    pub threshold: Option<f32>,
}

impl VectorQuery {
    /// Create a new vector query
    pub fn new(collection: &str, query: VectorSource) -> Self {
        Self {
            alias: None,
            collection: collection.to_string(),
            query_vector: query,
            k: 10,
            filter: None,
            metric: None,
            include_vectors: false,
            include_metadata: true,
            threshold: None,
        }
    }

    /// Set the number of results
    pub fn limit(mut self, k: usize) -> Self {
        self.k = k;
        self
    }

    /// Set metadata filter
    pub fn with_filter(mut self, filter: MetadataFilter) -> Self {
        self.filter = Some(filter);
        self
    }

    /// Include vectors in results
    pub fn with_vectors(mut self) -> Self {
        self.include_vectors = true;
        self
    }

    /// Set similarity threshold
    pub fn min_similarity(mut self, threshold: f32) -> Self {
        self.threshold = Some(threshold);
        self
    }

    /// Set outer alias
    pub fn alias(mut self, alias: &str) -> Self {
        self.alias = Some(alias.to_string());
        self
    }
}

/// Source of query vector
#[derive(Debug, Clone)]
pub enum VectorSource {
    /// Literal vector values
    Literal(Vec<f32>),
    /// Text to embed (requires embedding function)
    Text(String),
    /// Reference to another vector by ID
    Reference { collection: String, vector_id: u64 },
    /// From a subquery result
    Subquery(Box<QueryExpr>),
}

impl VectorSource {
    /// Create from literal vector
    pub fn literal(values: Vec<f32>) -> Self {
        Self::Literal(values)
    }

    /// Create from text (to be embedded)
    pub fn text(s: &str) -> Self {
        Self::Text(s.to_string())
    }

    /// Reference another vector
    pub fn reference(collection: &str, vector_id: u64) -> Self {
        Self::Reference {
            collection: collection.to_string(),
            vector_id,
        }
    }
}

// ============================================================================
// Hybrid Query
// ============================================================================

/// Hybrid query combining structured (table/graph) and vector search
///
/// ```text
/// FROM hosts h
/// JOIN VECTOR embeddings e ON h.id = e.metadata.host_id
/// SIMILAR TO 'ssh vulnerability'
/// WHERE h.os = 'Linux'
/// RETURN h.*, e.distance
/// ```
#[derive(Debug, Clone)]
pub struct HybridQuery {
    /// Optional outer alias when used as a join source
    pub alias: Option<String>,
    /// Structured query part (table/graph)
    pub structured: Box<QueryExpr>,
    /// Vector search part
    pub vector: VectorQuery,
    /// How to combine results
    pub fusion: FusionStrategy,
    /// Final result limit
    pub limit: Option<usize>,
}

impl HybridQuery {
    /// Create a new hybrid query
    pub fn new(structured: QueryExpr, vector: VectorQuery) -> Self {
        Self {
            alias: None,
            structured: Box::new(structured),
            vector,
            fusion: FusionStrategy::Rerank { weight: 0.5 },
            limit: None,
        }
    }

    /// Set fusion strategy
    pub fn with_fusion(mut self, fusion: FusionStrategy) -> Self {
        self.fusion = fusion;
        self
    }

    /// Set result limit
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set outer alias
    pub fn alias(mut self, alias: &str) -> Self {
        self.alias = Some(alias.to_string());
        self
    }
}

/// Strategy for combining structured and vector search results
#[derive(Debug, Clone)]
pub enum FusionStrategy {
    /// Vector similarity re-ranks structured results
    /// weight: 0.0 = pure structured, 1.0 = pure vector
    Rerank { weight: f32 },
    /// Filter with structured query, then search vectors among filtered
    FilterThenSearch,
    /// Search vectors first, then filter with structured query
    SearchThenFilter,
    /// Reciprocal Rank Fusion
    /// k: RRF constant (typically 60)
    RRF { k: u32 },
    /// Intersection: only return results that match both
    Intersection,
    /// Union: return results from either (with combined scores)
    Union {
        structured_weight: f32,
        vector_weight: f32,
    },
}

impl Default for FusionStrategy {
    fn default() -> Self {
        Self::Rerank { weight: 0.5 }
    }
}

// ============================================================================
// DML/DDL Query Types
// ============================================================================

/// Entity type qualifier for INSERT statements
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InsertEntityType {
    /// Default: plain row
    #[default]
    Row,
    /// INSERT INTO t NODE (...)
    Node,
    /// INSERT INTO t EDGE (...)
    Edge,
    /// INSERT INTO t VECTOR (...)
    Vector,
    /// INSERT INTO t DOCUMENT (...)
    Document,
    /// INSERT INTO t KV (...)
    Kv,
}

/// An item in a RETURNING clause: either `*` (all columns) or a named column.
#[derive(Debug, Clone, PartialEq)]
pub enum ReturningItem {
    /// RETURNING *
    All,
    /// RETURNING col
    Column(String),
}

/// INSERT INTO table (columns) VALUES (row1), (row2), ... [WITH TTL duration] [WITH METADATA (k=v)]
#[derive(Debug, Clone)]
pub struct InsertQuery {
    /// Target table name
    pub table: String,
    /// Entity type qualifier
    pub entity_type: InsertEntityType,
    /// Column names
    pub columns: Vec<String>,
    /// Canonical SQL rows of expressions.
    pub value_exprs: Vec<Vec<super::Expr>>,
    /// Rows of values (each inner Vec is one row)
    pub values: Vec<Vec<Value>>,
    /// Optional RETURNING clause items.
    pub returning: Option<Vec<ReturningItem>>,
    /// Optional TTL in milliseconds (from WITH TTL clause)
    pub ttl_ms: Option<u64>,
    /// Optional absolute expiration (from WITH EXPIRES AT clause)
    pub expires_at_ms: Option<u64>,
    /// Optional metadata key-value pairs (from WITH METADATA clause)
    pub with_metadata: Vec<(String, Value)>,
    /// Auto-embed fields on insert (from WITH AUTO EMBED clause)
    pub auto_embed: Option<AutoEmbedConfig>,
    /// Skip event subscription emission for this statement (SUPPRESS EVENTS).
    pub suppress_events: bool,
}

/// Configuration for automatic embedding generation on INSERT.
#[derive(Debug, Clone)]
pub struct AutoEmbedConfig {
    /// Fields to extract text from for embedding
    pub fields: Vec<String>,
    /// AI provider (e.g. "openai")
    pub provider: String,
    /// Optional model override
    pub model: Option<String>,
}

/// EVENTS BACKFILL collection [WHERE pred] TO queue [LIMIT n]
#[derive(Debug, Clone)]
pub struct EventsBackfillQuery {
    pub collection: String,
    pub where_filter: Option<String>,
    pub target_queue: String,
    pub limit: Option<u64>,
}

/// UPDATE table SET col=val, ... WHERE filter [WITH TTL duration] [WITH METADATA (...)]
#[derive(Debug, Clone)]
pub struct UpdateQuery {
    /// Target table name
    pub table: String,
    /// Canonical SQL assignments.
    pub assignment_exprs: Vec<(String, super::Expr)>,
    /// Best-effort literal-only cache of assignments. Non-foldable expressions
    /// are preserved exclusively in `assignment_exprs` and evaluated later
    /// against the row pre-image by the runtime.
    pub assignments: Vec<(String, Value)>,
    /// Canonical SQL WHERE clause.
    pub where_expr: Option<super::Expr>,
    /// Optional WHERE filter
    pub filter: Option<Filter>,
    /// Optional TTL in milliseconds (from WITH TTL clause)
    pub ttl_ms: Option<u64>,
    /// Optional absolute expiration (from WITH EXPIRES AT clause)
    pub expires_at_ms: Option<u64>,
    /// Optional metadata key-value pairs (from WITH METADATA clause)
    pub with_metadata: Vec<(String, Value)>,
    /// Optional RETURNING clause items.
    pub returning: Option<Vec<ReturningItem>>,
    /// Optional `LIMIT N` cap. Caps the number of rows the executor
    /// will mutate in a single statement. Required by `BATCH N ROWS`
    /// data migrations (#37) which run the same UPDATE body in a
    /// loop, advancing a checkpoint between batches.
    pub limit: Option<u64>,
    /// Skip event subscription emission for this statement (SUPPRESS EVENTS).
    pub suppress_events: bool,
}

/// DELETE FROM table WHERE filter
#[derive(Debug, Clone)]
pub struct DeleteQuery {
    /// Target table name
    pub table: String,
    /// Canonical SQL WHERE clause.
    pub where_expr: Option<super::Expr>,
    /// Optional WHERE filter
    pub filter: Option<Filter>,
    /// Optional RETURNING clause items.
    pub returning: Option<Vec<ReturningItem>>,
    /// Skip event subscription emission for this statement (SUPPRESS EVENTS).
    pub suppress_events: bool,
}

/// CREATE TABLE name (columns) or CREATE {KV|CONFIG|VAULT} name
#[derive(Debug, Clone)]
pub struct CreateTableQuery {
    /// Declared collection model. Defaults to Table for CREATE TABLE.
    pub collection_model: CollectionModel,
    /// Table name
    pub name: String,
    /// Column definitions
    pub columns: Vec<CreateColumnDef>,
    /// IF NOT EXISTS flag
    pub if_not_exists: bool,
    /// Optional default TTL applied to newly inserted items in this collection.
    pub default_ttl_ms: Option<u64>,
    /// Fields to prioritize in the context index (WITH CONTEXT INDEX ON (f1, f2))
    pub context_index_fields: Vec<String>,
    /// Enables the global context index for this table
    /// (`WITH context_index = true`). Default false — pure OLTP tables
    /// skip the tokenisation / 3-way RwLock write storm on every insert.
    /// Having `context_index_fields` non-empty also enables it implicitly.
    pub context_index_enabled: bool,
    /// When true, CREATE TABLE implicitly adds two user-visible columns
    /// `created_at` and `updated_at` (BIGINT unix-ms). The runtime
    /// populates them from `UnifiedEntity::created_at/updated_at` on
    /// every write; `created_at` is immutable after insert.
    /// Enabled via `WITH timestamps = true` in the DDL.
    pub timestamps: bool,
    /// Partitioning spec (Phase 2.2 PG parity).
    ///
    /// When present the table is the *parent* of a partition tree — every
    /// child partition is registered via `ALTER TABLE ... ATTACH PARTITION`.
    /// Phase 2.2 stops at registry-only: queries against a partitioned
    /// parent don't auto-rewrite as UNION yet (Phase 4 adds pruning).
    pub partition_by: Option<PartitionSpec>,
    /// Table-scoped multi-tenancy declaration (Phase 2.5.4).
    ///
    /// Syntax: `CREATE TABLE t (...) WITH (tenant_by = 'col_name')` or
    /// the shorthand `CREATE TABLE t (...) TENANT BY (col_name)`. The
    /// runtime treats the named column as the tenant discriminator and
    /// automatically:
    ///
    /// 1. Registers the table → column mapping so INSERTs that omit the
    ///    column get `CURRENT_TENANT()` auto-filled.
    /// 2. Installs an implicit RLS policy equivalent to
    ///    `USING (col = CURRENT_TENANT())` for SELECT/UPDATE/DELETE/INSERT.
    /// 3. Flips `rls_enabled_tables` on so the policy actually applies.
    ///
    /// None leaves the table non-tenant-scoped — callers manage tenancy
    /// manually via explicit CREATE POLICY if they want it.
    pub tenant_by: Option<String>,
    /// When true, UPDATE and DELETE on this table are rejected at
    /// parse time. Corresponds to `CREATE TABLE ... APPEND ONLY` or
    /// `WITH (append_only = true)`. Default false (mutable).
    pub append_only: bool,
    /// Declarative event subscriptions for this table. #291 stores
    /// metadata only; event emission is intentionally out of scope.
    pub subscriptions: Vec<crate::catalog::SubscriptionDescriptor>,
    /// `CREATE VAULT ... WITH OWN MASTER KEY`: provision per-vault
    /// key material instead of using the cluster vault key.
    pub vault_own_master_key: bool,
}

/// CREATE COLLECTION name KIND kind
#[derive(Debug, Clone)]
pub struct CreateCollectionQuery {
    pub name: String,
    pub kind: String,
    pub if_not_exists: bool,
}

/// CREATE VECTOR name DIM n [METRIC metric]
#[derive(Debug, Clone)]
pub struct CreateVectorQuery {
    pub name: String,
    pub dimension: usize,
    pub metric: DistanceMetric,
    pub if_not_exists: bool,
}

/// `PARTITION BY RANGE|LIST|HASH (column)` clause.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartitionSpec {
    pub kind: PartitionKind,
    /// Partition key column(s). Simple single-column for Phase 2.2.
    pub column: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PartitionKind {
    /// `PARTITION BY RANGE(col)` — children bind `FOR VALUES FROM (a) TO (b)`.
    Range,
    /// `PARTITION BY LIST(col)` — children bind `FOR VALUES IN (v1, v2, ...)`.
    List,
    /// `PARTITION BY HASH(col)` — children bind `FOR VALUES WITH (MODULUS m, REMAINDER r)`.
    Hash,
}

/// Column definition for CREATE TABLE
#[derive(Debug, Clone)]
pub struct CreateColumnDef {
    /// Column name
    pub name: String,
    /// Legacy declared type string preserved for the runtime/storage pipeline.
    pub data_type: String,
    /// Structured SQL type used by the semantic layer.
    pub sql_type: SqlTypeName,
    /// NOT NULL constraint
    pub not_null: bool,
    /// DEFAULT value expression
    pub default: Option<String>,
    /// Compression level (COMPRESS:N)
    pub compress: Option<u8>,
    /// UNIQUE constraint
    pub unique: bool,
    /// PRIMARY KEY constraint
    pub primary_key: bool,
    /// Enum variant names (for ENUM type)
    pub enum_variants: Vec<String>,
    /// Array element type (for ARRAY type)
    pub array_element: Option<String>,
    /// Decimal precision (for DECIMAL type)
    pub decimal_precision: Option<u8>,
}

/// DROP TABLE name
#[derive(Debug, Clone)]
pub struct DropTableQuery {
    /// Table name
    pub name: String,
    /// IF EXISTS flag
    pub if_exists: bool,
}

/// DROP GRAPH [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropGraphQuery {
    pub name: String,
    pub if_exists: bool,
}

/// DROP VECTOR [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropVectorQuery {
    pub name: String,
    pub if_exists: bool,
}

/// DROP DOCUMENT [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropDocumentQuery {
    pub name: String,
    pub if_exists: bool,
}

/// DROP {KV|CONFIG|VAULT} [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropKvQuery {
    pub name: String,
    pub if_exists: bool,
    pub model: CollectionModel,
}

/// DROP COLLECTION [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropCollectionQuery {
    pub name: String,
    pub if_exists: bool,
}

/// TRUNCATE {TABLE|GRAPH|VECTOR|DOCUMENT|TIMESERIES|KV|QUEUE|COLLECTION} [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct TruncateQuery {
    pub name: String,
    pub model: Option<CollectionModel>,
    pub if_exists: bool,
}

/// ALTER TABLE name operations
#[derive(Debug, Clone)]
pub struct AlterTableQuery {
    /// Table name
    pub name: String,
    /// Alter operations
    pub operations: Vec<AlterOperation>,
}

/// Single ALTER TABLE operation
#[derive(Debug, Clone)]
pub enum AlterOperation {
    /// ADD COLUMN definition
    AddColumn(CreateColumnDef),
    /// DROP COLUMN name
    DropColumn(String),
    /// RENAME COLUMN from TO to
    RenameColumn { from: String, to: String },
    /// `ATTACH PARTITION child FOR VALUES ...` (Phase 2.2 PG parity).
    ///
    /// Binds an existing child table to the parent partitioned table.
    /// The `bound` string captures the raw bound expression so the
    /// runtime can round-trip it back into `red_config` without a
    /// dedicated per-kind AST.
    AttachPartition {
        child: String,
        /// Human-readable bound string, e.g. `FROM (2024-01-01) TO (2025-01-01)`
        /// or `IN (1, 2, 3)` or `WITH (MODULUS 4, REMAINDER 0)`.
        bound: String,
    },
    /// `DETACH PARTITION child`
    DetachPartition { child: String },
    /// `ENABLE ROW LEVEL SECURITY` (Phase 2.5 PG parity).
    ///
    /// Flips the table into RLS-enforced mode. Reads against the table
    /// will be filtered by every matching `CREATE POLICY` (for the
    /// current role) combined with `AND`.
    EnableRowLevelSecurity,
    /// `DISABLE ROW LEVEL SECURITY` — disables enforcement; policies
    /// remain defined but are ignored until re-enabled.
    DisableRowLevelSecurity,
    /// `ENABLE TENANCY ON (col)` (Phase 2.5.4 PG parity-ish).
    ///
    /// Retrofit a tenant-scoped declaration onto an existing table —
    /// registers the column, installs the auto `__tenant_iso` RLS
    /// policy, and flips RLS on. Equivalent to re-running
    /// `CREATE TABLE ... TENANT BY (col)` minus the schema creation.
    EnableTenancy { column: String },
    /// `DISABLE TENANCY` — tears down the auto-policy and clears the
    /// tenancy registration. User-defined policies on the table are
    /// untouched; RLS stays enabled if any survive.
    DisableTenancy,
    /// `SET APPEND_ONLY = true|false` — flips the catalog flag.
    /// Setting `true` rejects all future UPDATE/DELETE at parse-time
    /// guard; setting `false` re-enables them. Existing rows are
    /// untouched either way — this is a purely declarative switch.
    SetAppendOnly(bool),
    /// `SET VERSIONED = true|false` — opt the table into (or out of)
    /// Git-for-Data. Enables merge / diff / AS OF semantics against
    /// this collection. Works retroactively: previously-created
    /// rows become part of the history accessible via AS OF as long
    /// as their xmin is still pinned by an existing commit.
    SetVersioned(bool),
    /// `ENABLE EVENTS ...` — install or re-enable table event subscription metadata.
    EnableEvents(crate::catalog::SubscriptionDescriptor),
    /// `DISABLE EVENTS` — mark all table event subscriptions disabled.
    DisableEvents,
    /// `ADD SUBSCRIPTION name TO queue [REDACT (...)] [WHERE ...]` — add a named subscription.
    AddSubscription {
        name: String,
        descriptor: crate::catalog::SubscriptionDescriptor,
    },
    /// `DROP SUBSCRIPTION name` — remove a named subscription by name.
    DropSubscription { name: String },
}

// ============================================================================
// Shared Types
// ============================================================================

/// Column/field projection
#[derive(Debug, Clone, PartialEq)]
pub enum Projection {
    /// Select all columns (*)
    All,
    /// Single column by name
    Column(String),
    /// Column with alias
    Alias(String, String),
    /// Function call (name, args)
    Function(String, Vec<Projection>),
    /// Expression with optional alias
    Expression(Box<Filter>, Option<String>),
    /// Field reference (for graph properties)
    Field(FieldRef, Option<String>),
}

impl Projection {
    /// Create a projection from a field reference
    pub fn from_field(field: FieldRef) -> Self {
        Projection::Field(field, None)
    }

    /// Create a column projection
    pub fn column(name: &str) -> Self {
        Projection::Column(name.to_string())
    }

    /// Create an aliased projection
    pub fn with_alias(column: &str, alias: &str) -> Self {
        Projection::Alias(column.to_string(), alias.to_string())
    }
}

/// Filter condition
#[derive(Debug, Clone, PartialEq)]
pub enum Filter {
    /// Comparison: field op value
    Compare {
        field: FieldRef,
        op: CompareOp,
        value: Value,
    },
    /// Field-to-field comparison: left.field op right.field. Used when
    /// WHERE / BETWEEN operands reference another column instead of a
    /// literal — the pre-Fase-2-parser-v2 shim for column-to-column
    /// predicates. Once the Expr-rewrite lands, this collapses into
    /// `Compare { left: Expr, op, right: Expr }`.
    CompareFields {
        left: FieldRef,
        op: CompareOp,
        right: FieldRef,
    },
    /// Expression-to-expression comparison: `lhs op rhs` where either
    /// side may be an arbitrary `Expr` tree (function call, CAST,
    /// arithmetic, nested CASE). This is the most general compare
    /// variant — `Compare` and `CompareFields` stay as fast-path
    /// specialisations because the planner / cost model / index
    /// selector all pattern-match on the simpler shapes. The parser
    /// only emits this variant when a simpler one cannot express the
    /// predicate.
    CompareExpr {
        lhs: super::Expr,
        op: CompareOp,
        rhs: super::Expr,
    },
    /// Logical AND
    And(Box<Filter>, Box<Filter>),
    /// Logical OR
    Or(Box<Filter>, Box<Filter>),
    /// Logical NOT
    Not(Box<Filter>),
    /// IS NULL
    IsNull(FieldRef),
    /// IS NOT NULL
    IsNotNull(FieldRef),
    /// IN (value1, value2, ...)
    In { field: FieldRef, values: Vec<Value> },
    /// BETWEEN low AND high
    Between {
        field: FieldRef,
        low: Value,
        high: Value,
    },
    /// LIKE pattern
    Like { field: FieldRef, pattern: String },
    /// STARTS WITH prefix
    StartsWith { field: FieldRef, prefix: String },
    /// ENDS WITH suffix
    EndsWith { field: FieldRef, suffix: String },
    /// CONTAINS substring
    Contains { field: FieldRef, substring: String },
}

impl Filter {
    /// Create a comparison filter
    pub fn compare(field: FieldRef, op: CompareOp, value: Value) -> Self {
        Self::Compare { field, op, value }
    }

    /// Combine with AND
    pub fn and(self, other: Filter) -> Self {
        Self::And(Box::new(self), Box::new(other))
    }

    /// Combine with OR
    pub fn or(self, other: Filter) -> Self {
        Self::Or(Box::new(self), Box::new(other))
    }

    /// Negate
    pub fn not(self) -> Self {
        Self::Not(Box::new(self))
    }

    /// Bottom-up AST rewrites: OR-of-equalities → IN, AND/OR flatten.
    /// Inspired by MongoDB's `MatchExpression::optimize()`.
    /// Call on the result of `effective_table_filter()` before evaluation.
    pub fn optimize(self) -> Self {
        crate::storage::query::filter_optimizer::optimize(self)
    }
}

/// Comparison operator
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareOp {
    /// Equal (=)
    Eq,
    /// Not equal (<> or !=)
    Ne,
    /// Less than (<)
    Lt,
    /// Less than or equal (<=)
    Le,
    /// Greater than (>)
    Gt,
    /// Greater than or equal (>=)
    Ge,
}

impl fmt::Display for CompareOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CompareOp::Eq => write!(f, "="),
            CompareOp::Ne => write!(f, "<>"),
            CompareOp::Lt => write!(f, "<"),
            CompareOp::Le => write!(f, "<="),
            CompareOp::Gt => write!(f, ">"),
            CompareOp::Ge => write!(f, ">="),
        }
    }
}

/// Order by clause.
///
/// Fase 2 migration: `field` is the legacy bare column reference and
/// remains populated for back-compat with existing callers (SPARQL /
/// Gremlin / Cypher translators, the planner cost model, etc.). The
/// new `expr` slot carries an arbitrary `Expr` tree — when present,
/// runtime comparators prefer it over `field`, so the parser can
/// emit `ORDER BY CAST(a AS INT)`, `ORDER BY a + b * 2`, etc. without
/// breaking the rest of the codebase.
///
/// When `expr` is `None`, the clause behaves exactly like before.
/// When `expr` is `Some(Expr::Column(f))`, runtime code may still use
/// the legacy path — it's equivalent. Constructors default `expr` to
/// `None` so all existing call sites stay source-compatible.
#[derive(Debug, Clone)]
pub struct OrderByClause {
    /// Field to order by. Left populated even when `expr` is set so
    /// legacy consumers (planner cardinality estimate, cost model,
    /// mode translators) that still pattern-match on `field` keep
    /// working during the Fase 2 migration.
    pub field: FieldRef,
    /// Fase 2 expression-aware sort key. When `Some`, runtime order
    /// comparators evaluate this expression per row and sort on the
    /// resulting values — unlocks `ORDER BY expr` (Fase 1.6).
    pub expr: Option<super::Expr>,
    /// Ascending or descending
    pub ascending: bool,
    /// Nulls first or last
    pub nulls_first: bool,
}

impl OrderByClause {
    /// Create ascending order
    pub fn asc(field: FieldRef) -> Self {
        Self {
            field,
            expr: None,
            ascending: true,
            nulls_first: false,
        }
    }

    /// Create descending order
    pub fn desc(field: FieldRef) -> Self {
        Self {
            field,
            expr: None,
            ascending: false,
            nulls_first: true,
        }
    }

    /// Attach an `Expr` sort key to an existing clause. Leaves `field`
    /// untouched so back-compat match sites keep their pattern.
    pub fn with_expr(mut self, expr: super::Expr) -> Self {
        self.expr = Some(expr);
        self
    }
}

// ============================================================================
// Graph Commands
// ============================================================================

/// Graph analytics command issued via SQL-like syntax
#[derive(Debug, Clone)]
pub struct GraphCommandOrderBy {
    pub metric: String,
    pub ascending: bool,
}

#[derive(Debug, Clone)]
pub enum GraphCommand {
    /// GRAPH NEIGHBORHOOD 'source' [DEPTH n] [DIRECTION dir] [EDGES IN ('label', ...)]
    Neighborhood {
        source: String,
        depth: u32,
        direction: String,
        edge_labels: Option<Vec<String>>,
    },
    /// GRAPH SHORTEST_PATH 'source' TO 'target' [ALGORITHM alg] [DIRECTION dir] [ORDER BY metric [ASC|DESC]] [LIMIT n]
    ShortestPath {
        source: String,
        target: String,
        algorithm: String,
        direction: String,
        limit: Option<u32>,
        order_by: Option<GraphCommandOrderBy>,
    },
    /// GRAPH TRAVERSE 'source' [STRATEGY bfs|dfs] [DEPTH n] [DIRECTION dir] [EDGES IN ('label', ...)]
    Traverse {
        source: String,
        strategy: String,
        depth: u32,
        direction: String,
        edge_labels: Option<Vec<String>>,
    },
    /// GRAPH CENTRALITY [ALGORITHM alg] [ORDER BY metric [ASC|DESC]] [LIMIT n]
    ///
    /// `limit = None` keeps the historical implicit top-100 cap. `Some(n)`
    /// caps the returned rows at `n`.
    Centrality {
        algorithm: String,
        limit: Option<u32>,
        order_by: Option<GraphCommandOrderBy>,
    },
    /// GRAPH COMMUNITY [ALGORITHM alg] [MAX_ITERATIONS n] [ORDER BY metric [ASC|DESC]] [LIMIT n]
    Community {
        algorithm: String,
        max_iterations: u32,
        limit: Option<u32>,
        order_by: Option<GraphCommandOrderBy>,
    },
    /// GRAPH COMPONENTS [MODE connected|weak|strong] [ORDER BY metric [ASC|DESC]] [LIMIT n]
    Components {
        mode: String,
        limit: Option<u32>,
        order_by: Option<GraphCommandOrderBy>,
    },
    /// GRAPH CYCLES [MAX_LENGTH n]
    Cycles { max_length: u32 },
    /// GRAPH CLUSTERING
    Clustering,
    /// GRAPH TOPOLOGICAL_SORT
    TopologicalSort,
    /// GRAPH PROPERTIES ['<id-or-label>']
    ///
    /// `source = None` returns graph-wide stats. `source = Some("...")` returns
    /// the full property bag of a specific node, resolved via the same label
    /// index as `GRAPH NEIGHBORHOOD` / `GRAPH TRAVERSE` (issue #416).
    Properties { source: Option<String> },
}

// ============================================================================
// Search Commands
// ============================================================================

/// Search command issued via SQL-like syntax
#[derive(Debug, Clone)]
pub enum SearchCommand {
    /// SEARCH SIMILAR [v1, v2, ...] | $N | TEXT 'query' [COLLECTION col] [LIMIT n] [MIN_SCORE f] [USING provider]
    Similar {
        vector: Vec<f32>,
        text: Option<String>,
        provider: Option<String>,
        collection: String,
        limit: usize,
        min_score: f32,
        /// `$N` placeholder for the vector slot. `Some(idx)` when the SQL
        /// used `SEARCH SIMILAR $N ...`; the binder substitutes the
        /// user-supplied `Value::Vector` and clears this back to `None`.
        /// Runtime executors assert this is `None` post-bind.
        vector_param: Option<usize>,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). The binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
        /// `$N` placeholder for the `MIN_SCORE` slot (issue #361). The
        /// binder substitutes the user-supplied float into `min_score`
        /// and clears this back to `None`.
        min_score_param: Option<usize>,
        /// `$N` placeholder for `SEARCH SIMILAR TEXT $N` (issue #361).
        /// Binder substitutes the user-supplied text into `text` and
        /// clears this back to `None`.
        text_param: Option<usize>,
    },
    /// SEARCH TEXT 'query' [COLLECTION col] [LIMIT n] [FUZZY]
    Text {
        query: String,
        collection: Option<String>,
        limit: usize,
        fuzzy: bool,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
        /// shape as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH HYBRID [vector] [TEXT 'query'] COLLECTION col [LIMIT n]
    Hybrid {
        vector: Option<Vec<f32>>,
        query: Option<String>,
        collection: String,
        limit: usize,
        /// `$N` placeholder for the `LIMIT` / `K` slot (issue #361).
        /// Same shape as `SearchCommand::Similar::limit_param`; the
        /// binder substitutes the user-supplied positive integer and
        /// clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH MULTIMODAL 'key_or_query' [COLLECTION col] [LIMIT n]
    Multimodal {
        query: String,
        collection: Option<String>,
        limit: usize,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
        /// shape as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH INDEX index VALUE 'value' [COLLECTION col] [LIMIT n] [EXACT]
    Index {
        index: String,
        value: String,
        collection: Option<String>,
        limit: usize,
        exact: bool,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
        /// shape as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH CONTEXT 'query' [FIELD field] [COLLECTION col] [LIMIT n] [DEPTH n]
    Context {
        query: String,
        field: Option<String>,
        collection: Option<String>,
        limit: usize,
        depth: usize,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
        /// shape as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH SPATIAL RADIUS lat lon radius_km COLLECTION col COLUMN col [LIMIT n]
    SpatialRadius {
        center_lat: f64,
        center_lon: f64,
        radius_km: f64,
        collection: String,
        column: String,
        limit: usize,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
        /// shape as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH SPATIAL BBOX min_lat min_lon max_lat max_lon COLLECTION col COLUMN col [LIMIT n]
    SpatialBbox {
        min_lat: f64,
        min_lon: f64,
        max_lat: f64,
        max_lon: f64,
        collection: String,
        column: String,
        limit: usize,
        /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
        /// shape as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `limit`
        /// and clears this back to `None`.
        limit_param: Option<usize>,
    },
    /// SEARCH SPATIAL NEAREST lat lon K n COLLECTION col COLUMN col
    SpatialNearest {
        lat: f64,
        lon: f64,
        k: usize,
        collection: String,
        column: String,
        /// `$N` placeholder for the `K` slot (issue #361). Same shape
        /// as `SearchCommand::Hybrid::limit_param`; the binder
        /// substitutes the user-supplied positive integer into `k`
        /// and clears this back to `None`.
        k_param: Option<usize>,
    },
}

// ============================================================================
// Time-Series DDL
// ============================================================================

/// CREATE TIMESERIES name [RETENTION duration] [CHUNK_SIZE n] [DOWNSAMPLE spec[, spec...]]
///
/// `CREATE HYPERTABLE` lands on the same AST with `hypertable` populated.
/// The TimescaleDB-style syntax (time column + chunk_interval) gives the
/// runtime enough to register a `HypertableSpec` alongside the
/// underlying collection contract, so chunk routing and TTL sweeps can
/// address the table without a separate DDL.
#[derive(Debug, Clone)]
pub struct CreateTimeSeriesQuery {
    pub name: String,
    pub retention_ms: Option<u64>,
    pub chunk_size: Option<usize>,
    pub downsample_policies: Vec<String>,
    pub if_not_exists: bool,
    /// When `Some`, the DDL was spelled `CREATE HYPERTABLE` and the
    /// runtime must register the spec with the hypertable registry.
    pub hypertable: Option<HypertableDdl>,
}

/// Hypertable-specific DDL fields — set only when the caller used
/// `CREATE HYPERTABLE`.
#[derive(Debug, Clone)]
pub struct HypertableDdl {
    /// Column that carries the nanosecond timestamp axis.
    pub time_column: String,
    /// Chunk width in nanoseconds.
    pub chunk_interval_ns: u64,
    /// Per-chunk default TTL in nanoseconds (`None` = no TTL).
    pub default_ttl_ns: Option<u64>,
}

/// DROP TIMESERIES [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropTimeSeriesQuery {
    pub name: String,
    pub if_exists: bool,
}

// ============================================================================
// Queue DDL & Commands
// ============================================================================

/// CREATE QUEUE name [MAX_SIZE n] [PRIORITY] [WITH TTL duration] [WITH DLQ name] [MAX_ATTEMPTS n]
#[derive(Debug, Clone)]
pub struct CreateQueueQuery {
    pub name: String,
    pub mode: QueueMode,
    pub priority: bool,
    pub max_size: Option<usize>,
    pub ttl_ms: Option<u64>,
    pub dlq: Option<String>,
    pub max_attempts: u32,
    pub if_not_exists: bool,
}

/// ALTER QUEUE name SET MODE [FANOUT|WORK]
#[derive(Debug, Clone)]
pub struct AlterQueueQuery {
    pub name: String,
    pub mode: QueueMode,
}

/// DROP QUEUE [IF EXISTS] name
#[derive(Debug, Clone)]
pub struct DropQueueQuery {
    pub name: String,
    pub if_exists: bool,
}

/// SELECT <columns> FROM QUEUE name [WHERE filter] [LIMIT n]
#[derive(Debug, Clone)]
pub struct QueueSelectQuery {
    pub queue: String,
    pub columns: Vec<String>,
    pub filter: Option<Filter>,
    pub limit: Option<u64>,
}

/// Which end of the queue
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueueSide {
    Left,
    Right,
}

/// Queue operation commands
#[derive(Debug, Clone)]
pub enum QueueCommand {
    Push {
        queue: String,
        value: Value,
        side: QueueSide,
        priority: Option<i32>,
    },
    Pop {
        queue: String,
        side: QueueSide,
        count: usize,
    },
    Peek {
        queue: String,
        count: usize,
    },
    Len {
        queue: String,
    },
    Purge {
        queue: String,
    },
    GroupCreate {
        queue: String,
        group: String,
    },
    GroupRead {
        queue: String,
        group: Option<String>,
        consumer: String,
        count: usize,
    },
    Pending {
        queue: String,
        group: String,
    },
    Claim {
        queue: String,
        group: String,
        consumer: String,
        min_idle_ms: u64,
    },
    Ack {
        queue: String,
        group: String,
        message_id: String,
    },
    Nack {
        queue: String,
        group: String,
        message_id: String,
    },
    Move {
        source: String,
        destination: String,
        filter: Option<Filter>,
        limit: usize,
    },
}

// ============================================================================
// Tree DDL & Commands
// ============================================================================

#[derive(Debug, Clone)]
pub struct TreeNodeSpec {
    pub label: String,
    pub node_type: Option<String>,
    pub properties: Vec<(String, Value)>,
    pub metadata: Vec<(String, Value)>,
    pub max_children: Option<usize>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TreePosition {
    First,
    Last,
    Index(usize),
}

#[derive(Debug, Clone)]
pub struct CreateTreeQuery {
    pub collection: String,
    pub name: String,
    pub root: TreeNodeSpec,
    pub default_max_children: usize,
    pub if_not_exists: bool,
}

#[derive(Debug, Clone)]
pub struct DropTreeQuery {
    pub collection: String,
    pub name: String,
    pub if_exists: bool,
}

#[derive(Debug, Clone)]
pub enum TreeCommand {
    Insert {
        collection: String,
        tree_name: String,
        parent_id: u64,
        node: TreeNodeSpec,
        position: TreePosition,
    },
    Move {
        collection: String,
        tree_name: String,
        node_id: u64,
        parent_id: u64,
        position: TreePosition,
    },
    Delete {
        collection: String,
        tree_name: String,
        node_id: u64,
    },
    Validate {
        collection: String,
        tree_name: String,
    },
    Rebalance {
        collection: String,
        tree_name: String,
        dry_run: bool,
    },
}

// ============================================================================
// KV DSL Commands
// ============================================================================

/// KV verb commands: `KV PUT key = value [EXPIRE n] [IF NOT EXISTS]`, `KV GET key`, `KV DELETE key`
#[derive(Debug, Clone)]
pub enum KvCommand {
    Put {
        model: CollectionModel,
        collection: String,
        key: String,
        value: Value,
        /// TTL in milliseconds (from EXPIRE clause)
        ttl_ms: Option<u64>,
        tags: Vec<String>,
        if_not_exists: bool,
    },
    InvalidateTags {
        collection: String,
        tags: Vec<String>,
    },
    Get {
        model: CollectionModel,
        collection: String,
        key: String,
    },
    Unseal {
        collection: String,
        key: String,
        version: Option<i64>,
    },
    Rotate {
        collection: String,
        key: String,
        value: Value,
        tags: Vec<String>,
    },
    History {
        collection: String,
        key: String,
    },
    List {
        model: CollectionModel,
        collection: String,
        prefix: Option<String>,
        limit: Option<usize>,
        offset: usize,
    },
    Purge {
        collection: String,
        key: String,
    },
    Watch {
        model: CollectionModel,
        collection: String,
        key: String,
        prefix: bool,
        from_lsn: Option<u64>,
    },
    Delete {
        model: CollectionModel,
        collection: String,
        key: String,
    },
    /// `KV INCR key [BY n] [EXPIRE dur]` — atomic increment; negative `by` = decrement.
    Incr {
        model: CollectionModel,
        collection: String,
        key: String,
        /// Step value; negative for DECR. Defaults to 1.
        by: i64,
        ttl_ms: Option<u64>,
    },
    /// `KV CAS key EXPECT <expected|NULL> SET <new> [EXPIRE dur]` — compare-and-set.
    ///
    /// `expected = None` means `EXPECT NULL` (key must be absent).
    Cas {
        model: CollectionModel,
        collection: String,
        key: String,
        /// The value the caller expects to be current; `None` = key must be absent.
        expected: Option<Value>,
        new_value: Value,
        ttl_ms: Option<u64>,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigValueType {
    Bool,
    Int,
    String,
    Url,
    Object,
    Array,
}

impl ConfigValueType {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Bool => "bool",
            Self::Int => "int",
            Self::String => "string",
            Self::Url => "url",
            Self::Object => "object",
            Self::Array => "array",
        }
    }

    pub fn parse(input: &str) -> Option<Self> {
        match input.to_ascii_lowercase().as_str() {
            "bool" | "boolean" => Some(Self::Bool),
            "int" | "integer" => Some(Self::Int),
            "string" | "str" | "text" => Some(Self::String),
            "url" => Some(Self::Url),
            "object" | "json_object" => Some(Self::Object),
            "array" | "list" => Some(Self::Array),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub enum ConfigCommand {
    Put {
        collection: String,
        key: String,
        value: Value,
        value_type: Option<ConfigValueType>,
        tags: Vec<String>,
    },
    Get {
        collection: String,
        key: String,
    },
    Resolve {
        collection: String,
        key: String,
    },
    Rotate {
        collection: String,
        key: String,
        value: Value,
        value_type: Option<ConfigValueType>,
        tags: Vec<String>,
    },
    Delete {
        collection: String,
        key: String,
    },
    History {
        collection: String,
        key: String,
    },
    List {
        collection: String,
        prefix: Option<String>,
        limit: Option<usize>,
        offset: usize,
    },
    Watch {
        collection: String,
        key: String,
        prefix: bool,
        from_lsn: Option<u64>,
    },
    InvalidVolatileOperation {
        operation: String,
        collection: String,
        key: Option<String>,
    },
}

// ============================================================================
// Builders (Fluent API)
// ============================================================================