package granary

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file exec.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
open Lwt.Syntax
module S = Granary_store.Store
module Cat = Granary_catalog.Catalog
module Row = Granary_encoding.Row
module Rowid = Granary_encoding.Rowid
module Index_key = Granary_encoding.Index_key
module Varint = Granary_encoding.Varint

(* #240: the set of user tables a write statement actually mutated, accumulated
   for an external read cache.  Like [query_stats] it rides Lwt
   sequence-associated storage so it need not be threaded through the DML and
   recursive cascade/trigger paths: [with_dirty] installs a fresh accumulator
   for the statement; every physical-mutation site calls [mark_dirty], a no-op
   when no accumulator is installed (plain [execute]/[run] callers pay nothing —
   a single predicted branch off the hot path, never per-row in the common case).
   Defined here, ahead of the DML functions, so every [mark_dirty] call site can
   reach it (the mutation sites span [execute_insert] onward).

   #417 Phase 0: an accumulator may ALSO capture the row-level delta — the rowid
   and old/new {!Row.t} of every mutated row — for an incremental view-maintenance
   consumer (DBSP-style).  This is opt-in: [make_change_acc] enables capture,
   [make_dirty_acc] does not.  The name-set path ([mark_dirty]) is byte-for-byte
   unchanged whether or not capture is on, so plain #240 callers pay nothing; the
   row-level [record_change] is a single predicted branch (returns immediately
   when [changes = None] or no accumulator is installed). *)
type row_change =
  | Inserted of
      { rowid : int64
      ; row : Row.t
      }
  | Deleted of
      { rowid : int64
      ; row : Row.t
      }
  | Updated of
      { rowid : int64
      ; old_row : Row.t
      ; new_row : Row.t
      }

type dirty_tables_acc =
  { names : (string, unit) Hashtbl.t
  ; changes : (string, row_change list ref) Hashtbl.t option
    (* [Some] => row-level capture is on; per-table lists held in reverse
       (newest-first) and reversed on drain by [dirty_changes]. *)
  }

let make_dirty_acc () : dirty_tables_acc = { names = Hashtbl.create 8; changes = None }

let make_change_acc () : dirty_tables_acc =
  { names = Hashtbl.create 8; changes = Some (Hashtbl.create 8) }
;;

let dirty_tables_key : dirty_tables_acc Lwt.key = Lwt.new_key ()

(* Reserved-prefix internal tables — the synthesized [sqlite_…] objects
   (sqlite_master / sqlite_sequence) — are never reported: an external cache only
   invalidates user tables.  [sqlite_] is the SOLE prefix [sema] forbids to user
   objects ([reject_reserved_name], sema.ml), so filtering exactly it is
   false-positive-free.  The engine's own [_sys_…] catalog trees are NOT matched:
   they are reached by fixed tree-id, never by name through a mark site, so they
   cannot appear here — whereas [_sys_]/[sys_] ARE legal user-table names (sema
   permits them), and a user table named e.g. [sys_audit] must still be reported. *)
let is_internal_table_name (name : string) = String.starts_with ~prefix:"sqlite_" name

(* Record that [name]'s rows changed in the current statement. *)
let mark_dirty (name : string) =
  match Lwt.get dirty_tables_key with
  | None -> ()
  | Some { names; _ } -> Hashtbl.replace names name ()
;;

(* #417: record one row-level [change] against [table].  A no-op (one branch)
   unless an accumulator with capture enabled is installed.  Also marks [table]
   in the name set so a captured change can never reference a table missing from
   {!dirty_elements} (callers at the per-row loop sites need not also call
   [mark_dirty]). *)
let record_change (table : string) (change : row_change) =
  match Lwt.get dirty_tables_key with
  | None | Some { changes = None; _ } -> ()
  | Some { names; changes = Some log } ->
    Hashtbl.replace names table ();
    (match Hashtbl.find_opt log table with
     | Some r -> r := change :: !r
     | None -> Hashtbl.add log table (ref [ change ]))
;;

(* #417: record an UPDATE whose row may have been re-keyed.  When the rowid is
   unchanged it is one [Updated]; when an UPDATE of the INTEGER-PK alias MOVED the
   row to a new rowid (#243/#249), the identity changed, so it is modelled as a
   [Deleted] of the old rowid followed by an [Inserted] at the new one — the same
   shape REPLACE-displacement emits, and what a Z-set/IVM consumer needs to track
   the move (it keys deltas by rowid). *)
let record_update (table : string) ~old_rowid ~new_rowid ~old_row ~new_row =
  if Int64.equal old_rowid new_rowid
  then record_change table (Updated { rowid = old_rowid; old_row; new_row })
  else (
    record_change table (Deleted { rowid = old_rowid; row = old_row });
    record_change table (Inserted { rowid = new_rowid; row = new_row }))
;;

(* Install [acc] as the active write-path mutation sink for [f]'s dynamic extent
   (propagated across binds, so nested cascade/trigger writes record into it). *)
let with_dirty (acc : dirty_tables_acc) (f : unit -> 'a Lwt.t) : 'a Lwt.t =
  Lwt.with_value dirty_tables_key (Some acc) f
;;

let current_dirty_acc () : dirty_tables_acc option = Lwt.get dirty_tables_key

(* Drain to the public shape: user tables only, deduplicated, sorted. *)
let dirty_elements ({ names; _ } : dirty_tables_acc) : string list =
  Hashtbl.fold
    (fun k () acc -> if is_internal_table_name k then acc else k :: acc)
    names
    []
  |> List.sort_uniq String.compare
;;

(* #417: drain the per-table row-level deltas: user tables only, sorted by name,
   each table's changes in the order they were applied.  Empty for a
   non-capturing accumulator. *)
let dirty_changes ({ changes; _ } : dirty_tables_acc) : (string * row_change list) list =
  match changes with
  | None -> []
  | Some log ->
    Hashtbl.fold
      (fun k r acc -> if is_internal_table_name k then acc else (k, List.rev !r) :: acc)
      log
      []
    |> List.sort (fun (a, _) (b, _) -> String.compare a b)
;;

(* ------------------------------------------------------------------ *)
(* Helpers                                                              *)
(* ------------------------------------------------------------------ *)

(* #247: kill-switch for the cursor-level aggregate fast path.  Defaults on;
   [GRANARY_AGG_FASTPATH=0] forces the general [stream_aggregate] path (a safety
   valve, and the foil the Gc-gate regression test compares against).  Read
   per-call — an aggregate runs it once per query, never in a tight loop. *)
let agg_fastpath_enabled () =
  match Sys.getenv_opt "GRANARY_AGG_FASTPATH" with
  | Some ("0" | "false" | "off") -> false
  | _ -> true
;;

let lit_to_value : Ast.literal -> Row.value = function
  | Ast.L_int n -> Row.V_int n
  | Ast.L_text s -> Row.V_text s
  | Ast.L_null -> Row.V_null
  | Ast.L_real f -> Row.V_real f
  | Ast.L_blob b -> Row.V_blob b
  | Ast.L_current_timestamp | Ast.L_current_date | Ast.L_current_time ->
    failwith "lit_to_value: CURRENT_* should not appear as a plan literal"
;;

let value_to_literal : Row.value -> Ast.literal = function
  | Row.V_int n -> Ast.L_int n
  | Row.V_text s -> Ast.L_text s
  | Row.V_real f -> Ast.L_real f
  | Row.V_blob b -> Ast.L_blob b
  | Row.V_null -> Ast.L_null
;;

let row_value_to_index_value : Row.value -> Index_key.value = function
  | Row.V_int n -> Index_key.IK_int n
  | Row.V_text s -> Index_key.IK_text s
  | Row.V_null -> Index_key.IK_null
  | Row.V_real f -> Index_key.IK_real f
  | Row.V_blob b -> Index_key.IK_blob b
;;

let compare_values (a : Row.value) (b : Row.value) : int =
  match a, b with
  | Row.V_null, Row.V_null -> 0
  | Row.V_null, _ ->
    -1 (* NULLs sort first — less than any non-null value, matches SQLite *)
  | _, Row.V_null -> 1
  | Row.V_int x, Row.V_int y -> Int64.compare x y
  | Row.V_real x, Row.V_real y -> Float.compare x y
  | Row.V_text x, Row.V_text y -> String.compare x y
  | Row.V_blob x, Row.V_blob y -> Bytes.compare x y
  | _, _ -> 0 (* cross-type: shouldn't happen *)
;;

let compare_with_nulls
      (dir : [ `Asc | `Desc ])
      (nulls : [ `Nulls_first | `Nulls_last ])
      (va : Row.value)
      (vb : Row.value)
  : int
  =
  match va, vb with
  | Row.V_null, Row.V_null -> 0
  | Row.V_null, _ ->
    (match nulls with
     | `Nulls_first -> -1
     | `Nulls_last -> 1)
  | _, Row.V_null ->
    (match nulls with
     | `Nulls_first -> 1
     | `Nulls_last -> -1)
  | _, _ ->
    let c = compare_values va vb in
    (match dir with
     | `Asc -> c
     | `Desc -> -c)
;;

let list_drop n lst =
  let rec go k = function
    | [] -> []
    | _ :: t as l -> if k <= 0 then l else go (k - 1) t
  in
  go n lst
;;

let list_take n lst =
  let rec go k = function
    | [] -> []
    | h :: t -> if k <= 0 then [] else h :: go (k - 1) t
  in
  go n lst
;;

(** Find a column ordinal by name within a [Row.column] list. *)
let find_col_idx_by_name (cols : Row.column list) (name : string) : int =
  let rec find i = function
    | [] -> failwith (Printf.sprintf "column not found: %s" name)
    | (c : Row.column) :: _ when String.equal c.Row.name name -> i
    | _ :: rest -> find (i + 1) rest
  in
  find 0 cols
;;

(* Module-level cache for compiled CHECK expressions.
   Key: (table_name, column_ordinal, check_sql) → compiled Plan.expr.
   Including check_sql avoids stale hits when different tables share the same
   name and column index across DB instances (e.g. test isolation). *)
let check_expr_cache : (string * int * string, Plan.expr) Hashtbl.t = Hashtbl.create 16

(* ── DDL reconstruction for Op_sqlite_master ─────────────────── *)

let sql_of_row_type = function
  | Row.Integer -> "INTEGER"
  | Row.Text -> "TEXT"
  | Row.Real -> "REAL"
  | Row.Blob -> "BLOB"
;;

(* A SQL single-quoted string literal with embedded quotes doubled. *)
let quote_text_literal s = "'" ^ String.concat "''" (String.split_on_char '\'' s) ^ "'"

let sql_of_default_value = function
  | Row.DV_int n -> Int64.to_string n
  | Row.DV_text s -> quote_text_literal s
  | Row.DV_real f -> Printf.sprintf "%g" f
  | Row.DV_blob b ->
    let hex =
      Bytes.to_seq b
      |> Seq.map (fun c -> Printf.sprintf "%02X" (Char.code c))
      |> List.of_seq
      |> String.concat ""
    in
    Printf.sprintf "X'%s'" hex
  | Row.DV_null -> "NULL"
  | Row.DV_current_timestamp -> "CURRENT_TIMESTAMP"
  | Row.DV_current_date -> "CURRENT_DATE"
  | Row.DV_current_time -> "CURRENT_TIME"
;;

let sql_of_fk_action = function
  | Cat.FA_no_action -> "NO ACTION"
  | Cat.FA_restrict -> "RESTRICT"
  | Cat.FA_cascade -> "CASCADE"
  | Cat.FA_set_null -> "SET NULL"
  | Cat.FA_set_default -> "SET DEFAULT"
;;

(* Phase 35 task 3b: quote DDL identifiers that contain non-alphanumeric
   characters, start with a digit, or are empty.  Embedded double-quotes are
   doubled per SQL identifier syntax. *)
let needs_quoting s =
  String.length s = 0
  || (let c = s.[0] in
      not ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c = '_'))
  || String.exists
       (fun c ->
          not
            ((c >= 'A' && c <= 'Z')
             || (c >= 'a' && c <= 'z')
             || (c >= '0' && c <= '9')
             || c = '_'))
       s
;;

let quote_ident s =
  if needs_quoting s
  then "\"" ^ String.concat "\"\"" (String.split_on_char '"' s) ^ "\""
  else s
;;

let format_pk_suffix ~autoinc_idx i (col : Row.column) buf =
  Buffer.add_string buf " PRIMARY KEY";
  (* #312: a DESC PK is a non-alias; re-emit DESC so reopen reproduces
     the non-alias shape (hidden rowid + __pk index). *)
  if col.Row.pk_desc then Buffer.add_string buf " DESC";
  if Some i = autoinc_idx then Buffer.add_string buf " AUTOINCREMENT"
;;

let format_column ~autoinc_idx i (col : Row.column) =
  let buf = Buffer.create 64 in
  Buffer.add_string buf (quote_ident col.Row.name);
  Buffer.add_char buf ' ';
  Buffer.add_string buf (sql_of_row_type col.Row.ty);
  if col.Row.not_null then Buffer.add_string buf " NOT NULL";
  if col.Row.primary_key then format_pk_suffix ~autoinc_idx i col buf;
  (match col.Row.default with
   | None -> ()
   | Some dv ->
     Buffer.add_string buf " DEFAULT ";
     Buffer.add_string buf (sql_of_default_value dv));
  (match col.Row.check_sql with
   | None -> ()
   | Some sql ->
     Buffer.add_string buf " CHECK(";
     Buffer.add_string buf sql;
     Buffer.add_char buf ')');
  (match col.Row.generated_as with
   | None -> ()
   | Some (expr_sql, is_stored) ->
     Buffer.add_string buf " GENERATED ALWAYS AS (";
     Buffer.add_string buf expr_sql;
     Buffer.add_string buf ") ";
     Buffer.add_string buf (if is_stored then "STORED" else "VIRTUAL"));
  Buffer.contents buf
;;

let ddl_of_table (meta : Cat.table_meta) =
  let without_rowid, autoincrement =
    match meta.Cat.storage with
    | Cat.Row { without_rowid; autoincrement; _ } -> without_rowid, autoincrement
    | Cat.Columnar _ -> false, false
  in
  let autoinc_idx =
    if autoincrement
    then Cat.compute_rowid_alias_col meta.Cat.columns ~without_rowid
    else None
  in
  let col_parts = List.mapi (format_column ~autoinc_idx) meta.Cat.columns in
  let fk_parts =
    List.map
      (fun (fk : Cat.fk_constraint) ->
         Printf.sprintf
           "FOREIGN KEY (%s) REFERENCES %s(%s) ON DELETE %s ON UPDATE %s"
           (String.concat ", " (List.map quote_ident fk.Cat.fk_local_cols))
           (quote_ident fk.Cat.fk_parent_table)
           (String.concat ", " (List.map quote_ident fk.Cat.fk_parent_cols))
           (sql_of_fk_action fk.Cat.fk_on_delete)
           (sql_of_fk_action fk.Cat.fk_on_update))
      meta.Cat.fk_constraints
  in
  Printf.sprintf
    "CREATE TABLE %s (%s)%s%s"
    (quote_ident meta.Cat.name)
    (String.concat ", " (col_parts @ fk_parts))
    (if without_rowid then " WITHOUT ROWID" else "")
    (if Cat.is_columnar meta then " USING COLUMNSTORE" else "")
;;

(** Extract the ON <table> target from a CREATE TRIGGER statement.
    Falls back to the trigger name if the ON clause is not found. *)
let trigger_table_of_sql trigger_name sql =
  (* Look for " ON " followed by identifier, case-insensitive *)
  let upper = String.uppercase_ascii sql in
  match String.index_opt upper 'O' with
  | None -> trigger_name
  | _ ->
    let n = String.length upper in
    (* Search for " ON " pattern *)
    let rec search i =
      if i + 4 >= n
      then trigger_name
      else if
        upper.[i] = ' '
        && upper.[i + 1] = 'O'
        && upper.[i + 2] = 'N'
        && upper.[i + 3] = ' '
      then (
        (* Found " ON " — extract the identifier that follows *)
        let start = i + 4 in
        let j = ref start in
        while
          !j < n
          &&
          let c = upper.[!j] in
          (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c = '_'
        do
          incr j
        done;
        if !j > start then String.sub sql start (!j - start) else trigger_name)
      else search (i + 1)
    in
    search 0
;;

let ddl_of_index (idx : Cat.index_info) =
  let unique_kw = if idx.Cat.idx_unique then "UNIQUE " else "" in
  let col_strs =
    List.map2
      (fun col_sql is_expr ->
         if is_expr then Printf.sprintf "(%s)" col_sql else quote_ident col_sql)
      idx.Cat.idx_columns
      idx.Cat.idx_expr_flags
  in
  let cols_str = String.concat ", " col_strs in
  let where_clause =
    match idx.Cat.idx_where_sql with
    | None -> ""
    | Some sql -> Printf.sprintf " WHERE %s" sql
  in
  Printf.sprintf
    "CREATE %sINDEX %s ON %s (%s)%s"
    unique_kw
    (quote_ident idx.Cat.idx_name)
    (quote_ident idx.Cat.idx_table)
    cols_str
    where_clause
;;

let ddl_of_fts (m : Cat.fts_table_meta) =
  Printf.sprintf
    "CREATE VIRTUAL TABLE %s USING fts5(%s)"
    (quote_ident m.Cat.fts_name)
    (String.concat ", " (List.map quote_ident m.Cat.fts_columns))
;;

(* ------------------------------------------------------------------ *)
(* Expression evaluation                                                *)
(* (Defined before [execute] so that [Op_update] can evaluate WHERE     *)
(*  predicates and right-hand-side expressions for SET assignments.)    *)
(* ------------------------------------------------------------------ *)

let value_truthy : Row.value -> bool = function
  | Row.V_null | Row.V_int 0L -> false
  | _ -> true
;;

(* Pattern matching helpers for LIKE and GLOB.
   Uses naive recursive backtracking: worst case is O(2^k) for k '%'/'*'
   metacharacters against an adversarial string.  Acceptable for typical
   SQL workloads; replace with NFA/DP if adversarial patterns are a concern. *)
let rec like_match pat pi str si =
  let plen = String.length pat
  and slen = String.length str in
  if pi = plen
  then si = slen
  else (
    match pat.[pi] with
    | '%' ->
      like_match pat (pi + 1) str si || (si < slen && like_match pat pi str (si + 1))
    | '_' -> si < slen && like_match pat (pi + 1) str (si + 1)
    | c ->
      si < slen
      && Char.lowercase_ascii c = Char.lowercase_ascii str.[si]
      && like_match pat (pi + 1) str (si + 1))
;;

let rec glob_match pat pi str si =
  let plen = String.length pat
  and slen = String.length str in
  if pi = plen
  then si = slen
  else (
    match pat.[pi] with
    | '*' ->
      glob_match pat (pi + 1) str si || (si < slen && glob_match pat pi str (si + 1))
    | '?' -> si < slen && glob_match pat (pi + 1) str (si + 1)
    | c -> si < slen && c = str.[si] && glob_match pat (pi + 1) str (si + 1))
;;

let str_trim_spaces s =
  let n = String.length s in
  let l = ref 0
  and r = ref (n - 1) in
  while
    !l <= !r
    &&
    let c = s.[!l] in
    c = ' ' || c = '\t' || c = '\n' || c = '\r'
  do
    incr l
  done;
  while
    !r >= !l
    &&
    let c = s.[!r] in
    c = ' ' || c = '\t' || c = '\n' || c = '\r'
  do
    decr r
  done;
  if !l > !r then "" else String.sub s !l (!r - !l + 1)
;;

let parse_int_prefix s =
  let s = String.trim s in
  match Int64.of_string_opt s with
  | Some n -> n
  | None ->
    (match float_of_string_opt s with
     | Some f -> Int64.of_float f
     | None ->
       (* Scan leading numeric prefix: optional sign, digits, optional decimal *)
       let n = String.length s in
       let i = ref 0 in
       if !i < n && (s.[!i] = '-' || s.[!i] = '+') then incr i;
       let digit_start = !i in
       while !i < n && s.[!i] >= '0' && s.[!i] <= '9' do
         incr i
       done;
       (* Include decimal part for float->int conversion *)
       let has_dot = !i < n && s.[!i] = '.' in
       if has_dot
       then (
         incr i;
         while !i < n && s.[!i] >= '0' && s.[!i] <= '9' do
           incr i
         done);
       if !i > digit_start
       then (
         match float_of_string_opt (String.sub s 0 !i) with
         | Some f -> Int64.of_float f
         | None ->
           (match Int64.of_string_opt (String.sub s 0 !i) with
            | Some v -> v
            | None -> 0L))
       else 0L)
;;

let parse_real_prefix s =
  let s = String.trim s in
  match float_of_string_opt s with
  | Some f -> f
  | None ->
    (* Try progressively shorter prefixes until one parses *)
    let n = String.length s in
    let result = ref 0.0 in
    let found = ref false in
    let i = ref n in
    while !i > 0 && not !found do
      match float_of_string_opt (String.sub s 0 !i) with
      | Some f ->
        result := f;
        found := true
      | None -> decr i
    done;
    !result
;;

let str_trim_chars s chars =
  let n = String.length s in
  let l = ref 0
  and r = ref (n - 1) in
  while !l <= !r && String.contains chars s.[!l] do
    incr l
  done;
  while !r >= !l && String.contains chars s.[!r] do
    decr r
  done;
  if !l > !r then "" else String.sub s !l (!r - !l + 1)
;;

let str_ltrim_spaces s =
  let n = String.length s in
  let l = ref 0 in
  while
    !l < n
    &&
    let c = s.[!l] in
    c = ' ' || c = '\t' || c = '\n' || c = '\r'
  do
    incr l
  done;
  String.sub s !l (n - !l)
;;

let str_ltrim_chars s chars =
  let n = String.length s in
  let l = ref 0 in
  while !l < n && String.contains chars s.[!l] do
    incr l
  done;
  String.sub s !l (n - !l)
;;

let str_rtrim_spaces s =
  let n = String.length s in
  let r = ref (n - 1) in
  while
    !r >= 0
    &&
    let c = s.[!r] in
    c = ' ' || c = '\t' || c = '\n' || c = '\r'
  do
    decr r
  done;
  if !r < 0 then "" else String.sub s 0 (!r + 1)
;;

let str_rtrim_chars s chars =
  let r = ref (String.length s - 1) in
  while !r >= 0 && String.contains chars s.[!r] do
    decr r
  done;
  if !r < 0 then "" else String.sub s 0 (!r + 1)
;;

let str_replace s old rep =
  if String.length old = 0
  then s
  else (
    let buf = Buffer.create (String.length s) in
    let n = String.length s
    and m = String.length old in
    let i = ref 0 in
    while !i <= n - m do
      if String.sub s !i m = old
      then (
        Buffer.add_string buf rep;
        i := !i + m)
      else (
        Buffer.add_char buf s.[!i];
        incr i)
    done;
    while !i < n do
      Buffer.add_char buf s.[!i];
      incr i
    done;
    Buffer.contents buf)
;;

let str_instr s sub =
  let n = String.length s
  and m = String.length sub in
  if m = 0
  then 1
  else (
    let found = ref 0 in
    let i = ref 0 in
    while !found = 0 && !i <= n - m do
      if String.sub s !i m = sub then found := !i + 1 (* 1-indexed *) else incr i
    done;
    !found)
;;

let row_key (row : Row.t) : string =
  let buf = Buffer.create 64 in
  Array.iter
    (function
      | Row.V_null -> Buffer.add_string buf "N|"
      | Row.V_int n ->
        Buffer.add_char buf 'I';
        Buffer.add_string buf (Int64.to_string n);
        Buffer.add_char buf '|'
      | Row.V_real f ->
        Buffer.add_char buf 'R';
        Buffer.add_string buf (Printf.sprintf "%h" f);
        Buffer.add_char buf '|'
      | Row.V_text s ->
        Buffer.add_char buf 'T';
        Buffer.add_string buf (string_of_int (String.length s));
        Buffer.add_char buf ':';
        Buffer.add_string buf s;
        Buffer.add_char buf '|'
      | Row.V_blob b ->
        Buffer.add_char buf 'B';
        Buffer.add_string buf (string_of_int (Bytes.length b));
        Buffer.add_char buf ':';
        Buffer.add_bytes buf b;
        Buffer.add_char buf '|')
    row;
  Buffer.contents buf
;;

let json_of_sql : Row.value -> Json.value = function
  | Row.V_null -> Json.J_null
  | Row.V_int n -> Json.J_int n
  | Row.V_real f -> Json.J_float f
  | Row.V_text s -> Json.J_string s
  | Row.V_blob b -> Json.J_string (Bytes.to_string b)
;;

let sql_of_json : Json.value -> Row.value = function
  | Json.J_null -> Row.V_null
  | Json.J_bool b -> Row.V_int (if b then 1L else 0L)
  | Json.J_int n -> Row.V_int n
  | Json.J_float f -> Row.V_real f
  | Json.J_string s -> Row.V_text s
  | Json.J_array _ as v -> Row.V_text (Json.to_string v)
  | Json.J_object _ as v -> Row.V_text (Json.to_string v)
;;

(* ── Scalar-function evaluation, split by category (#168) ──────────
   [eval_func] dispatches to the [eval_*_func] helpers below; each returns
   [Some v] for the functions it owns and [None] otherwise, so [eval_func]
   can chain them and fall through to the arity-error case.  Verbose
   per-function bodies are themselves factored into small named helpers. *)

let hex_encode_str s =
  let buf = Buffer.create (String.length s * 2) in
  String.iter (fun c -> Buffer.add_string buf (Printf.sprintf "%02X" (Char.code c))) s;
  Buffer.contents buf
;;

(* #264: render a value as a standalone SQL literal for a logical dump.
   The output must parse back to the identical value through our own executor:
   - text is single-quoted with embedded quotes doubled;
   - blobs use the [X'..'] hex syntax;
   - a finite float always carries a '.' or exponent so it re-reads as REAL
     (not INTEGER), and uses the shortest decimal that round-trips bit-for-bit;
   - non-finite floats map to [1e999]/[-1e999] (overflow to ±inf, as SQLite's
     own .dump emits) and NaN to NULL (SQLite cannot store a NaN). *)
let sql_literal_of_value : Row.value -> string = function
  | Row.V_null -> "NULL"
  | Row.V_int n -> Int64.to_string n
  | Row.V_text s -> quote_text_literal s
  | Row.V_blob b -> "X'" ^ hex_encode_str (Bytes.to_string b) ^ "'"
  | Row.V_real f ->
    if Float.is_nan f
    then "NULL"
    else if f = Float.infinity
    then "1e999"
    else if f = Float.neg_infinity
    then "-1e999"
    else (
      let rec shortest p =
        if p >= 17
        then Printf.sprintf "%.17g" f
        else (
          let s = Printf.sprintf "%.*g" p f in
          if float_of_string s = f then s else shortest (p + 1))
      in
      let s = shortest 1 in
      if String.contains s '.' || String.contains s 'e' || String.contains s 'E'
      then s
      else s ^ ".0")
;;

(* UTF-8 encode each in-range integer codepoint, mirroring SQLite's char(). *)
let char_encode args =
  let buf = Buffer.create 16 in
  List.iter
    (fun v ->
       match v with
       | Row.V_int n when n >= 1L && n <= 0x10FFFFL ->
         let cp = Int64.to_int n in
         if cp < 0x80
         then Buffer.add_char buf (Char.chr cp)
         else if cp < 0x800
         then (
           Buffer.add_char buf (Char.chr (0xC0 lor (cp lsr 6)));
           Buffer.add_char buf (Char.chr (0x80 lor (cp land 0x3F))))
         else if cp < 0x10000
         then (
           Buffer.add_char buf (Char.chr (0xE0 lor (cp lsr 12)));
           Buffer.add_char buf (Char.chr (0x80 lor ((cp lsr 6) land 0x3F)));
           Buffer.add_char buf (Char.chr (0x80 lor (cp land 0x3F))))
         else (
           Buffer.add_char buf (Char.chr (0xF0 lor (cp lsr 18)));
           Buffer.add_char buf (Char.chr (0x80 lor ((cp lsr 12) land 0x3F)));
           Buffer.add_char buf (Char.chr (0x80 lor ((cp lsr 6) land 0x3F)));
           Buffer.add_char buf (Char.chr (0x80 lor (cp land 0x3F))))
       | _ -> ())
    args;
  Buffer.contents buf
;;

(* Decode the codepoint of the first UTF-8 character of [s] (s non-empty). *)
let unicode_codepoint s =
  let b0 = Char.code s.[0] in
  if b0 < 0x80
  then b0
  else if b0 < 0xE0 && String.length s >= 2
  then ((b0 land 0x1F) lsl 6) lor (Char.code s.[1] land 0x3F)
  else if b0 < 0xF0 && String.length s >= 3
  then
    ((b0 land 0x0F) lsl 12)
    lor ((Char.code s.[1] land 0x3F) lsl 6)
    lor (Char.code s.[2] land 0x3F)
  else if b0 >= 0xF0 && String.length s >= 4
  then
    ((b0 land 0x07) lsl 18)
    lor ((Char.code s.[1] land 0x3F) lsl 12)
    lor ((Char.code s.[2] land 0x3F) lsl 6)
    lor (Char.code s.[3] land 0x3F)
  else b0
;;

(* Emit one printf conversion [spec] (the char after '%') to [buf], pulling
   the next argument via [get_arg]. *)
let printf_emit buf spec (get_arg : unit -> Row.value) =
  match spec with
  | '%' -> Buffer.add_char buf '%'
  | 'd' | 'i' ->
    (match get_arg () with
     | Row.V_int n2 -> Buffer.add_string buf (Int64.to_string n2)
     | Row.V_real f -> Buffer.add_string buf (string_of_int (int_of_float f))
     | Row.V_text s ->
       (try Buffer.add_string buf (string_of_int (int_of_string s)) with
        | Failure _ -> ())
     | _ -> ())
  | 'f' ->
    (match get_arg () with
     | Row.V_real f -> Buffer.add_string buf (Printf.sprintf "%f" f)
     | Row.V_int n2 -> Buffer.add_string buf (Printf.sprintf "%f" (Int64.to_float n2))
     | _ -> ())
  | 'e' ->
    (match get_arg () with
     | Row.V_real f -> Buffer.add_string buf (Printf.sprintf "%e" f)
     | Row.V_int n2 -> Buffer.add_string buf (Printf.sprintf "%e" (Int64.to_float n2))
     | _ -> ())
  | 'g' ->
    (match get_arg () with
     | Row.V_real f -> Buffer.add_string buf (Printf.sprintf "%g" f)
     | Row.V_int n2 -> Buffer.add_string buf (Printf.sprintf "%g" (Int64.to_float n2))
     | _ -> ())
  | 's' ->
    (match get_arg () with
     | Row.V_text s -> Buffer.add_string buf s
     | Row.V_int n2 -> Buffer.add_string buf (Int64.to_string n2)
     | Row.V_real f -> Buffer.add_string buf (Printf.sprintf "%g" f)
     | Row.V_null -> Buffer.add_string buf "NULL"
     | Row.V_blob _ -> Buffer.add_string buf "")
  | 'q' ->
    (match get_arg () with
     | Row.V_text s ->
       String.iter
         (fun c -> if c = '\'' then Buffer.add_string buf "''" else Buffer.add_char buf c)
         s
     | Row.V_int n2 -> Buffer.add_string buf (Int64.to_string n2)
     | Row.V_real f -> Buffer.add_string buf (Printf.sprintf "%g" f)
     | Row.V_null -> Buffer.add_string buf "NULL"
     | Row.V_blob _ -> ())
  | c ->
    Buffer.add_char buf '%';
    Buffer.add_char buf c
;;

(* SQLite printf()/format(): a small subset of C printf conversions. *)
let printf_format fmt rest =
  let args_arr = Array.of_list rest in
  let arg_idx = ref 0 in
  let get_arg () =
    let v =
      if !arg_idx < Array.length args_arr then args_arr.(!arg_idx) else Row.V_null
    in
    incr arg_idx;
    v
  in
  let buf = Buffer.create 64 in
  let n = String.length fmt in
  let i = ref 0 in
  while !i < n do
    if fmt.[!i] = '%'
    then (
      incr i;
      if !i < n
      then (
        printf_emit buf fmt.[!i] get_arg;
        incr i))
    else (
      Buffer.add_char buf fmt.[!i];
      incr i)
  done;
  Buffer.contents buf
;;

let eval_str_func (func : Ast.scalar_func) (args : Row.value list) : Row.value option =
  match func, args with
  | Ast.Fn_length, [ Row.V_text s ] -> Some (Row.V_int (Int64.of_int (String.length s)))
  | Ast.Fn_length, [ Row.V_blob b ] -> Some (Row.V_int (Int64.of_int (Bytes.length b)))
  | Ast.Fn_length, [ Row.V_null ] -> Some Row.V_null
  | Ast.Fn_length, [ _ ] -> Some Row.V_null (* non-text/blob: return null like SQLite *)
  | Ast.Fn_lower, [ Row.V_text s ] -> Some (Row.V_text (String.lowercase_ascii s))
  | Ast.Fn_lower, [ Row.V_null ] -> Some Row.V_null
  | Ast.Fn_lower, [ _ ] -> Some Row.V_null
  | Ast.Fn_upper, [ Row.V_text s ] -> Some (Row.V_text (String.uppercase_ascii s))
  | Ast.Fn_upper, [ Row.V_null ] -> Some Row.V_null
  | Ast.Fn_upper, [ _ ] -> Some Row.V_null
  | Ast.Fn_substr, Row.V_text s :: rest ->
    Some
      (match rest with
       | [ Row.V_int start ] ->
         let i = max 0 (Int64.to_int start - 1) in
         if i >= String.length s
         then Row.V_text ""
         else Row.V_text (String.sub s i (String.length s - i))
       | [ Row.V_int start; Row.V_int len ] ->
         let i = max 0 (Int64.to_int start - 1) in
         let l = Int64.to_int len in
         if i >= String.length s || l <= 0
         then Row.V_text ""
         else Row.V_text (String.sub s i (min l (String.length s - i)))
       | _ -> Row.V_null)
  | Ast.Fn_substr, Row.V_null :: _ -> Some Row.V_null
  | Ast.Fn_trim, [ Row.V_text s ] -> Some (Row.V_text (str_trim_spaces s))
  | Ast.Fn_trim, [ Row.V_text s; Row.V_text chars ] ->
    Some (Row.V_text (str_trim_chars s chars))
  | Ast.Fn_trim, [ _; Row.V_null ] -> Some Row.V_null
  | Ast.Fn_trim, Row.V_null :: _ -> Some Row.V_null
  | Ast.Fn_ltrim, [ Row.V_text s ] -> Some (Row.V_text (str_ltrim_spaces s))
  | Ast.Fn_ltrim, [ Row.V_text s; Row.V_text chars ] ->
    Some (Row.V_text (str_ltrim_chars s chars))
  | Ast.Fn_ltrim, [ _; Row.V_null ] -> Some Row.V_null
  | Ast.Fn_ltrim, Row.V_null :: _ -> Some Row.V_null
  | Ast.Fn_rtrim, [ Row.V_text s ] -> Some (Row.V_text (str_rtrim_spaces s))
  | Ast.Fn_rtrim, [ Row.V_text s; Row.V_text chars ] ->
    Some (Row.V_text (str_rtrim_chars s chars))
  | Ast.Fn_rtrim, [ _; Row.V_null ] -> Some Row.V_null
  | Ast.Fn_rtrim, Row.V_null :: _ -> Some Row.V_null
  | Ast.Fn_replace, [ Row.V_text s; Row.V_text old; Row.V_text rep ] ->
    Some (Row.V_text (str_replace s old rep))
  | Ast.Fn_replace, [ _; Row.V_null; _ ] -> Some Row.V_null
  | Ast.Fn_replace, [ _; _; Row.V_null ] -> Some Row.V_null
  | Ast.Fn_replace, Row.V_null :: _ -> Some Row.V_null
  | Ast.Fn_instr, [ Row.V_text s; Row.V_text sub ] ->
    Some (Row.V_int (Int64.of_int (str_instr s sub)))
  | Ast.Fn_instr, Row.V_null :: _ | Ast.Fn_instr, [ _; Row.V_null ] -> Some Row.V_null
  | Ast.Fn_hex, [ Row.V_blob b ] -> Some (Row.V_text (hex_encode_str (Bytes.to_string b)))
  | Ast.Fn_hex, [ Row.V_text s ] -> Some (Row.V_text (hex_encode_str s))
  | Ast.Fn_hex, [ Row.V_int n ] -> Some (Row.V_text (hex_encode_str (Int64.to_string n)))
  | Ast.Fn_hex, [ Row.V_null ] -> Some (Row.V_text "")
  | Ast.Fn_char, args -> Some (Row.V_text (char_encode args))
  | Ast.Fn_unicode, [ Row.V_text s ] when String.length s > 0 ->
    Some (Row.V_int (Int64.of_int (unicode_codepoint s)))
  | Ast.Fn_unicode, [ Row.V_text _ ] -> Some Row.V_null
  | Ast.Fn_unicode, [ Row.V_null ] -> Some Row.V_null
  | Ast.Fn_printf, Row.V_text fmt :: rest -> Some (Row.V_text (printf_format fmt rest))
  | Ast.Fn_printf, _ -> Some Row.V_null
  | _ -> None
;;

let eval_math_func (func : Ast.scalar_func) (args : Row.value list) : Row.value option =
  let to_float_opt = function
    | Row.V_real f -> Some f
    | Row.V_int n -> Some (Int64.to_float n)
    | _ -> None
  in
  match func, args with
  | Ast.Fn_abs, [ Row.V_int n ] -> Some (Row.V_int (Int64.abs n))
  | Ast.Fn_abs, [ Row.V_real f ] -> Some (Row.V_real (Float.abs f))
  | Ast.Fn_abs, [ Row.V_null ] -> Some Row.V_null
  | Ast.Fn_abs, [ _ ] -> Some Row.V_null
  | Ast.Fn_round, [ Row.V_real f ] -> Some (Row.V_real (Float.round f))
  | Ast.Fn_round, [ Row.V_int n ] -> Some (Row.V_real (Int64.to_float n))
  | Ast.Fn_round, [ Row.V_real f; Row.V_int d ] ->
    let factor = 10. ** Int64.to_float d in
    Some (Row.V_real (Float.round (f *. factor) /. factor))
  | Ast.Fn_round, [ Row.V_int n; Row.V_int _ ] -> Some (Row.V_real (Int64.to_float n))
  | Ast.Fn_round, [ _; Row.V_null ] -> Some Row.V_null
  | Ast.Fn_round, Row.V_null :: _ -> Some Row.V_null
  | Ast.Fn_ceil, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.ceil f)
       | None -> Row.V_null)
  | Ast.Fn_floor, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.floor f)
       | None -> Row.V_null)
  | Ast.Fn_sqrt, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.sqrt f)
       | None -> Row.V_null)
  | Ast.Fn_pow, [ b; e ] ->
    Some
      (match to_float_opt b, to_float_opt e with
       | Some bf, Some ef -> Row.V_real (bf ** ef)
       | _ -> Row.V_null)
  | Ast.Fn_exp, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.exp f)
       | None -> Row.V_null)
  | Ast.Fn_ln, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.log f)
       | None -> Row.V_null)
  | Ast.Fn_log, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.log f)
       | None -> Row.V_null)
  | Ast.Fn_log, [ b; x ] ->
    Some
      (match to_float_opt b, to_float_opt x with
       | Some bf, Some xf -> Row.V_real (Float.log xf /. Float.log bf)
       | _ -> Row.V_null)
  | Ast.Fn_log2, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.log f /. Float.log 2.0)
       | None -> Row.V_null)
  | Ast.Fn_log10, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.log10 f)
       | None -> Row.V_null)
  | Ast.Fn_sign, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_int (if f > 0.0 then 1L else if f < 0.0 then -1L else 0L)
       | None -> Row.V_null)
  | Ast.Fn_trunc, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (if f >= 0.0 then Float.floor f else Float.ceil f)
       | None -> Row.V_null)
  | Ast.Fn_trunc, [ v; d ] ->
    Some
      (match to_float_opt v, to_float_opt d with
       | Some f, Some df ->
         let factor = 10.0 ** Float.round df in
         let fx = f *. factor in
         Row.V_real ((if fx >= 0.0 then Float.floor fx else Float.ceil fx) /. factor)
       | _ -> Row.V_null)
  | Ast.Fn_pi, [] -> Some (Row.V_real Float.pi)
  | Ast.Fn_sin, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.sin f)
       | None -> Row.V_null)
  | Ast.Fn_cos, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.cos f)
       | None -> Row.V_null)
  | Ast.Fn_tan, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.tan f)
       | None -> Row.V_null)
  | Ast.Fn_asin, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.asin f)
       | None -> Row.V_null)
  | Ast.Fn_acos, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.acos f)
       | None -> Row.V_null)
  | Ast.Fn_atan, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (Float.atan f)
       | None -> Row.V_null)
  | Ast.Fn_atan2, [ y; x ] ->
    Some
      (match to_float_opt y, to_float_opt x with
       | Some yf, Some xf -> Row.V_real (Float.atan2 yf xf)
       | _ -> Row.V_null)
  | Ast.Fn_degrees, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (f *. 180.0 /. Float.pi)
       | None -> Row.V_null)
  | Ast.Fn_radians, [ v ] ->
    Some
      (match to_float_opt v with
       | Some f -> Row.V_real (f *. Float.pi /. 180.0)
       | None -> Row.V_null)
  | _ -> None
;;

(* date/time/datetime/julianday/unixepoch share arg-shape handling; only the
   final conversion differs. *)
let eval_datetime_unary clock args (conv : Datetime.dt -> Row.value) : Row.value =
  match args with
  | [] | [ Row.V_null ] -> Row.V_null
  | Row.V_null :: _ -> Row.V_null
  | Row.V_text ts :: rest ->
    if rest <> []
    then Row.V_null
    else (
      match Datetime.parse ?now:clock ts with
      | Error _ -> Row.V_null
      | Ok dt -> conv dt)
  | _ -> Row.V_null
;;

let eval_datetime_func clock (func : Ast.scalar_func) (args : Row.value list)
  : Row.value option
  =
  match func with
  | Ast.Fn_date ->
    Some (eval_datetime_unary clock args (fun dt -> Row.V_text (Datetime.to_date dt)))
  | Ast.Fn_time ->
    Some (eval_datetime_unary clock args (fun dt -> Row.V_text (Datetime.to_time dt)))
  | Ast.Fn_datetime ->
    Some (eval_datetime_unary clock args (fun dt -> Row.V_text (Datetime.to_datetime dt)))
  | Ast.Fn_julianday ->
    Some
      (eval_datetime_unary clock args (fun dt -> Row.V_real (Datetime.to_julianday dt)))
  | Ast.Fn_unixepoch ->
    Some (eval_datetime_unary clock args (fun dt -> Row.V_int (Datetime.to_unixepoch dt)))
  | Ast.Fn_strftime ->
    Some
      (match args with
       | Row.V_text fmt :: Row.V_text ts :: rest ->
         if rest <> []
         then Row.V_null
         else (
           match Datetime.parse ?now:clock ts with
           | Error _ -> Row.V_null
           | Ok dt -> Row.V_text (Datetime.strftime fmt dt))
       | _ -> Row.V_null)
  | _ -> None
;;

(* json_set/insert/replace differ only in the per-path Json.path_* operation. *)
let json_modify (path_op : Json.value -> string -> Json.value -> Json.value) json_v rest
  : Row.value
  =
  let json_s =
    match json_v with
    | Row.V_text s -> s
    | _ -> ""
  in
  match Json.parse json_s with
  | Error _ -> Row.V_null
  | Ok jv ->
    let rec apply jv = function
      | path_v :: val_v :: rest ->
        let path =
          match path_v with
          | Row.V_text s -> s
          | _ -> ""
        in
        apply (path_op jv path (json_of_sql val_v)) rest
      | _ -> jv
    in
    Row.V_text (Json.to_string (apply jv rest))
;;

let eval_json_func (func : Ast.scalar_func) (args : Row.value list) : Row.value option =
  match func, args with
  | Ast.Fn_json_extract, [ json_v; path_v ] ->
    let json_s =
      match json_v with
      | Row.V_text s -> s
      | _ -> ""
    in
    let path_s =
      match path_v with
      | Row.V_text s -> s
      | _ -> ""
    in
    Some
      (match Json.parse json_s with
       | Error _ -> Row.V_null
       | Ok jv ->
         (match Json.path_get jv path_s with
          | None -> Row.V_null
          | Some v -> sql_of_json v))
  | Ast.Fn_json_object, pairs ->
    if List.length pairs mod 2 <> 0
    then Some Row.V_null
    else (
      let rec make_pairs = function
        | [] -> []
        | k :: v :: rest ->
          let key =
            match k with
            | Row.V_text s -> s
            | _ -> ""
          in
          (key, json_of_sql v) :: make_pairs rest
        | [ _ ] -> assert false
      in
      Some (Row.V_text (Json.to_string (Json.J_object (make_pairs pairs)))))
  | Ast.Fn_json_array, elems ->
    Some (Row.V_text (Json.to_string (Json.J_array (List.map json_of_sql elems))))
  | Ast.Fn_json_type, [ json_v ] ->
    Some
      (match json_v with
       | Row.V_text s ->
         (match Json.parse s with
          | Error _ -> Row.V_null
          | Ok jv -> Row.V_text (Json.type_name jv))
       | _ -> Row.V_null)
  | Ast.Fn_json_type, [ json_v; path_v ] ->
    Some
      (match json_v, path_v with
       | Row.V_text s, Row.V_text path ->
         (match Json.parse s with
          | Error _ -> Row.V_null
          | Ok jv ->
            (match Json.path_get jv path with
             | None -> Row.V_null
             | Some sub -> Row.V_text (Json.type_name sub)))
       | _ -> Row.V_null)
  | Ast.Fn_json_valid, [ json_v ] ->
    Some
      (match json_v with
       | Row.V_null -> Row.V_null
       | Row.V_text s ->
         (match Json.parse s with
          | Ok _ -> Row.V_int 1L
          | Error _ -> Row.V_int 0L)
       | _ -> Row.V_int 0L)
  | Ast.Fn_json_set, json_v :: rest -> Some (json_modify Json.path_set json_v rest)
  | Ast.Fn_json_insert, json_v :: rest -> Some (json_modify Json.path_insert json_v rest)
  | Ast.Fn_json_replace, json_v :: rest ->
    Some (json_modify Json.path_replace json_v rest)
  | Ast.Fn_json_remove, json_v :: paths ->
    let json_s =
      match json_v with
      | Row.V_text s -> s
      | _ -> ""
    in
    Some
      (match Json.parse json_s with
       | Error _ -> Row.V_null
       | Ok jv ->
         let result =
           List.fold_left
             (fun acc path_v ->
                let path =
                  match path_v with
                  | Row.V_text s -> s
                  | _ -> ""
                in
                Json.path_remove acc path)
             jv
             paths
         in
         Row.V_text (Json.to_string result))
  | _ -> None
;;

let eval_misc_func (func : Ast.scalar_func) (args : Row.value list) : Row.value option =
  match func, args with
  | Ast.Fn_coalesce, vs ->
    Some
      (match List.find_opt (fun v -> v <> Row.V_null) vs with
       | Some v -> v
       | None -> Row.V_null)
  | Ast.Fn_ifnull, [ a; b ] ->
    Some
      (match a with
       | Row.V_null -> b
       | v -> v)
  | Ast.Fn_typeof, [ v ] ->
    Some
      (Row.V_text
         (match v with
          | Row.V_int _ -> "integer"
          | Row.V_real _ -> "real"
          | Row.V_text _ -> "text"
          | Row.V_blob _ -> "blob"
          | Row.V_null -> "null"))
  | Ast.Fn_zeroblob, [ Row.V_int n ] when n >= 0L ->
    Some (Row.V_blob (Bytes.make (Int64.to_int n) '\000'))
  | Ast.Fn_zeroblob, _ -> Some Row.V_null
  | Ast.Fn_random, [] ->
    let b0 = Int64.of_int (Random.bits ()) in
    let b1 = Int64.of_int (Random.bits ()) in
    let b2 = Int64.of_int (Random.bits ()) in
    let sign = if Random.bool () then Int64.min_int else 0L in
    let v =
      Int64.logor
        sign
        (Int64.logor (Int64.shift_left b2 60) (Int64.logor (Int64.shift_left b1 30) b0))
    in
    Some (Row.V_int v)
  | Ast.Fn_random, _ -> Some Row.V_null
  (* SQLite always generates at least 1 byte, even for n <= 0.
     Clamp to [1, Sys.max_string_length] to avoid allocation errors. *)
  | Ast.Fn_randomblob, [ Row.V_int n ] ->
    let sz =
      max
        1
        (if n < 0L || n > Int64.of_int Sys.max_string_length then 1 else Int64.to_int n)
    in
    Some (Row.V_blob (Bytes.init sz (fun _ -> Char.chr (Random.int 256))))
  | Ast.Fn_randomblob, _ -> Some Row.V_null
  | Ast.Fn_changes, [] -> Some (Row.V_int 0L)
  | Ast.Fn_changes, _ -> Some Row.V_null
  | Ast.Fn_last_insert_rowid, [] -> Some (Row.V_int 0L)
  | Ast.Fn_last_insert_rowid, _ -> Some Row.V_null
  | Ast.Fn_total_changes, [] -> Some (Row.V_int 0L)
  | Ast.Fn_total_changes, _ -> Some Row.V_null
  | Ast.Fn_sqlite_version, [] -> Some (Row.V_text "3.45.0-granary")
  | Ast.Fn_sqlite_version, _ -> Some Row.V_null
  | _ -> None
;;

(* CAST evaluation; [v] is the already-evaluated operand. NULL casts to NULL. *)
let eval_cast (v : Row.value) (ty : Ast.ty) : Row.value =
  match v with
  | Row.V_null -> Row.V_null
  | _ ->
    (match ty with
     | Ast.Ty_int ->
       (match v with
        | Row.V_int n -> Row.V_int n
        | Row.V_real f -> Row.V_int (Int64.of_float f)
        | Row.V_text s -> Row.V_int (parse_int_prefix s)
        | Row.V_blob _ -> Row.V_int 0L
        | Row.V_null -> assert false)
     | Ast.Ty_real ->
       (match v with
        | Row.V_int n -> Row.V_real (Int64.to_float n)
        | Row.V_real f -> Row.V_real f
        | Row.V_text s -> Row.V_real (parse_real_prefix s)
        | Row.V_blob _ -> Row.V_real 0.0
        | Row.V_null -> assert false)
     | Ast.Ty_text ->
       (match v with
        | Row.V_int n -> Row.V_text (Int64.to_string n)
        | Row.V_real f ->
          (* SQLite appends ".0" when the %.15g result has no decimal point
             or exponent, so that CAST(1.0 AS TEXT) → "1.0" not "1". *)
          let s = Printf.sprintf "%.15g" f in
          let needs_dot =
            not
              (String.contains s '.'
               || String.contains s 'e'
               || String.contains s 'E'
               || String.contains s 'n')
          in
          Row.V_text (if needs_dot then s ^ ".0" else s)
        | Row.V_text s -> Row.V_text s
        | Row.V_blob b -> Row.V_text (Bytes.to_string b)
        | Row.V_null -> assert false)
     | Ast.Ty_blob ->
       (match v with
        | Row.V_blob b -> Row.V_blob b
        | Row.V_text s -> Row.V_blob (Bytes.of_string s)
        | Row.V_int n -> Row.V_blob (Bytes.of_string (Int64.to_string n))
        | Row.V_real f -> Row.V_blob (Bytes.of_string (Printf.sprintf "%.15g" f))
        | Row.V_null -> assert false))
;;

(* Bitwise binops: result is NULL unless both operands are integers. *)
let int_bitop lv rv f =
  match lv, rv with
  | Row.V_int a, Row.V_int b -> Row.V_int (f a b)
  | _ -> Row.V_null
;;

let rec eval_expr
          (clock : (unit -> float) option)
          (params : Row.value array)
          (row : Row.t)
          (e : Plan.expr)
  : Row.value
  =
  match e with
  | Plan.P_lit l -> lit_to_value l
  | Plan.P_col i -> row.(i)
  | Plan.P_param i -> if i < Array.length params then params.(i) else Row.V_null
  | Plan.P_neg e ->
    (match eval_expr clock params row e with
     | Row.V_int n -> Row.V_int (Int64.neg n)
     | Row.V_real f -> Row.V_real (-.f)
     | Row.V_null -> Row.V_null
     | _ -> failwith "unary minus requires numeric operand")
  | Plan.P_bitnot e ->
    (match eval_expr clock params row e with
     | Row.V_int n -> Row.V_int (Int64.lognot n)
     | Row.V_null -> Row.V_null
     | _ -> Row.V_null)
  | Plan.P_between (x, lo, hi) ->
    let vx = eval_expr clock params row x in
    let vlo = eval_expr clock params row lo in
    let vhi = eval_expr clock params row hi in
    (match vx, vlo, vhi with
     | Row.V_null, _, _ | _, Row.V_null, _ | _, _, Row.V_null -> Row.V_null
     | _ ->
       let ge_lo = compare_values vx vlo >= 0 in
       let le_hi = compare_values vx vhi <= 0 in
       Row.V_int (if ge_lo && le_hi then 1L else 0L))
  | Plan.P_in (x, vals) -> eval_in clock params row x vals
  | Plan.P_is_null e ->
    (match eval_expr clock params row e with
     | Row.V_null -> Row.V_int 1L
     | _ -> Row.V_int 0L)
  | Plan.P_is_not_null e ->
    (match eval_expr clock params row e with
     | Row.V_null -> Row.V_int 0L
     | _ -> Row.V_int 1L)
  | Plan.P_not e ->
    (match eval_expr clock params row e with
     | Row.V_null -> Row.V_null
     | v -> if value_truthy v then Row.V_int 0L else Row.V_int 1L)
  | Plan.P_binop (op, lhs_e, rhs_e) ->
    let lv = eval_expr clock params row lhs_e in
    let rv = eval_expr clock params row rhs_e in
    let is_nocase = function
      | Plan.P_collate (_, Ast.Collate_nocase) -> true
      | _ -> false
    in
    let nocase_text v =
      match v with
      | Row.V_text s -> Row.V_text (String.lowercase_ascii s)
      | o -> o
    in
    let lv', rv' =
      if is_nocase lhs_e
      then lv, nocase_text rv
      else if is_nocase rhs_e
      then nocase_text lv, rv
      else lv, rv
    in
    eval_binop op lv' rv'
  | Plan.P_func (func, args) ->
    eval_func clock func (List.map (eval_expr clock params row) args)
  | Plan.P_case { scrutinee; branches; else_ } ->
    eval_case_expr clock params row scrutinee branches else_
  | Plan.P_cast (e, ty) -> eval_cast (eval_expr clock params row e) ty
  | Plan.P_subquery _ | Plan.P_exists _ | Plan.P_in_select _ ->
    (* These are replaced by pre_eval_subquery before row evaluation. *)
    Row.V_null
  | Plan.P_excluded_col _ ->
    failwith "Exec: P_excluded_col in eval_expr — must be substituted before evaluation"
  | Plan.P_window_slot _ ->
    failwith
      "Exec: P_window_slot in eval_expr — must be substituted by planner before \
       evaluation"
  | Plan.P_collate (e, Ast.Collate_nocase) ->
    let v = eval_expr clock params row e in
    (match v with
     | Row.V_text s -> Row.V_text (String.lowercase_ascii s)
     | o -> o)
  | Plan.P_collate (e, _) ->
    eval_expr clock params row e (* Collate_binary and Collate_rtrim are identity *)

and eval_in clock params row x vals =
  let vx = eval_expr clock params row x in
  if vx = Row.V_null
  then Row.V_null
  else (
    let result =
      List.fold_left
        (fun acc ve ->
           let v = eval_expr clock params row ve in
           match acc with
           | `Found -> `Found
           | _ when v = Row.V_null -> `Maybe
           | _ when compare_values vx v = 0 -> `Found
           | acc -> acc)
        `Not_found
        vals
    in
    match result with
    | `Found -> Row.V_int 1L
    | `Maybe -> Row.V_null
    | `Not_found -> Row.V_int 0L)

and eval_case_expr clock params row scrutinee branches else_ =
  let scr_val = Option.map (eval_expr clock params row) scrutinee in
  let rec find_match = function
    | [] ->
      (match else_ with
       | None -> Row.V_null
       | Some e -> eval_expr clock params row e)
    | (cond, result) :: rest ->
      let matched =
        match scr_val with
        | None -> value_truthy (eval_expr clock params row cond)
        | Some sv ->
          let cv = eval_expr clock params row cond in
          (match sv, cv with
           | Row.V_null, _ | _, Row.V_null -> false
           | _ -> compare_values sv cv = 0)
      in
      if matched then eval_expr clock params row result else find_match rest
  in
  find_match branches

and eval_func
      (clock : (unit -> float) option)
      (func : Ast.scalar_func)
      (args : Row.value list)
  : Row.value
  =
  match eval_str_func func args with
  | Some v -> v
  | None ->
    (match eval_math_func func args with
     | Some v -> v
     | None ->
       (match eval_datetime_func clock func args with
        | Some v -> v
        | None ->
          (match eval_json_func func args with
           | Some v -> v
           | None ->
             (match eval_misc_func func args with
              | Some v -> v
              | None ->
                failwith
                  "scalar_func: unexpected argument count (arity check should have \
                   caught this)"))))

and eval_binop (op : Plan.binop) (lv : Row.value) (rv : Row.value) : Row.value =
  match op with
  | Plan.And ->
    let lt = value_truthy lv
    and rt = value_truthy rv in
    let ln = lv = Row.V_null
    and rn = rv = Row.V_null in
    if lt && rt
    then Row.V_int 1L
    else if ((not ln) && not lt) || ((not rn) && not rt)
    then Row.V_int 0L
    else Row.V_null
  | Plan.Or ->
    let lt = value_truthy lv
    and rt = value_truthy rv in
    let ln = lv = Row.V_null
    and rn = rv = Row.V_null in
    if lt || rt
    then Row.V_int 1L
    else if (not ln) && not rn
    then Row.V_int 0L
    else Row.V_null
  (* NULL compared with anything yields NULL (3-valued logic). Cross-type → false. *)
  | Plan.Eq ->
    (match lv, rv with
     | Row.V_null, _ | _, Row.V_null -> Row.V_null
     | Row.V_int x, Row.V_int y -> if Int64.equal x y then Row.V_int 1L else Row.V_int 0L
     | Row.V_text x, Row.V_text y ->
       if String.equal x y then Row.V_int 1L else Row.V_int 0L
     | Row.V_real x, Row.V_real y ->
       if Float.equal x y then Row.V_int 1L else Row.V_int 0L
     | Row.V_blob x, Row.V_blob y ->
       if Bytes.equal x y then Row.V_int 1L else Row.V_int 0L
     | _ -> Row.V_int 0L)
  | Plan.Ne ->
    (match lv, rv with
     | Row.V_null, _ | _, Row.V_null -> Row.V_null
     | Row.V_int x, Row.V_int y -> if Int64.equal x y then Row.V_int 0L else Row.V_int 1L
     | Row.V_text x, Row.V_text y ->
       if String.equal x y then Row.V_int 0L else Row.V_int 1L
     | Row.V_real x, Row.V_real y ->
       if Float.equal x y then Row.V_int 0L else Row.V_int 1L
     | Row.V_blob x, Row.V_blob y ->
       if Bytes.equal x y then Row.V_int 0L else Row.V_int 1L
     | _ -> Row.V_int 0L)
  | Plan.Lt -> cmp_result lv rv (fun c -> c < 0)
  | Plan.Le -> cmp_result lv rv (fun c -> c <= 0)
  | Plan.Gt -> cmp_result lv rv (fun c -> c > 0)
  | Plan.Ge -> cmp_result lv rv (fun c -> c >= 0)
  | Plan.Add -> arith_op lv rv Int64.add ( +. )
  | Plan.Sub -> arith_op lv rv Int64.sub ( -. )
  | Plan.Mul -> arith_op lv rv Int64.mul ( *. )
  | Plan.Div ->
    arith_op
      lv
      rv
      (fun a b -> if Int64.equal b 0L then failwith "division by zero" else Int64.div a b)
      ( /. )
  | Plan.Concat ->
    (match lv, rv with
     | Row.V_null, _ | _, Row.V_null -> Row.V_null
     | Row.V_text a, Row.V_text b -> Row.V_text (a ^ b)
     | Row.V_text a, Row.V_int n -> Row.V_text (a ^ Int64.to_string n)
     | Row.V_int n, Row.V_text b -> Row.V_text (Int64.to_string n ^ b)
     | Row.V_int a, Row.V_int b -> Row.V_text (Int64.to_string a ^ Int64.to_string b)
     | _ -> Row.V_null)
  | Plan.Mod ->
    (match lv, rv with
     | Row.V_null, _ | _, Row.V_null -> Row.V_null
     | Row.V_int a, Row.V_int b ->
       if b = 0L then Row.V_null else Row.V_int (Int64.rem a b)
     | Row.V_real a, Row.V_real b ->
       if b = 0.0 then Row.V_null else Row.V_real (mod_float a b)
     | Row.V_int a, Row.V_real b ->
       if b = 0.0 then Row.V_null else Row.V_real (mod_float (Int64.to_float a) b)
     | Row.V_real a, Row.V_int b ->
       if b = 0L then Row.V_null else Row.V_real (mod_float a (Int64.to_float b))
     | _ -> Row.V_null)
  | Plan.Bit_and -> int_bitop lv rv Int64.logand
  | Plan.Bit_or -> int_bitop lv rv Int64.logor
  | Plan.Lshift ->
    int_bitop lv rv (fun a b ->
      let n = Int64.to_int b in
      if n < 0 || n >= 64 then 0L else Int64.shift_left a n)
  | Plan.Rshift ->
    int_bitop lv rv (fun a b ->
      let n = Int64.to_int b in
      if n < 0 || n >= 64 then 0L else Int64.shift_right a n)
  | Plan.Like ->
    (match lv, rv with
     | Row.V_null, _ | _, Row.V_null -> Row.V_null
     | Row.V_text str, Row.V_text pat ->
       Row.V_int
         (if like_match (String.lowercase_ascii pat) 0 (String.lowercase_ascii str) 0
          then 1L
          else 0L)
     | _ -> Row.V_null)
  | Plan.Glob ->
    (match lv, rv with
     | Row.V_null, _ | _, Row.V_null -> Row.V_null
     | Row.V_text str, Row.V_text pat ->
       Row.V_int (if glob_match pat 0 str 0 then 1L else 0L)
     | _ -> Row.V_null)

and cmp_result lv rv pred =
  match lv, rv with
  | Row.V_null, _ | _, Row.V_null -> Row.V_null
  | Row.V_int _, Row.V_int _
  | Row.V_text _, Row.V_text _
  | Row.V_real _, Row.V_real _
  | Row.V_blob _, Row.V_blob _ ->
    if pred (compare_values lv rv) then Row.V_int 1L else Row.V_int 0L
  (* Cross-type numeric comparisons: promote int to float *)
  | Row.V_real a, Row.V_int b ->
    let c = Float.compare a (Int64.to_float b) in
    if pred c then Row.V_int 1L else Row.V_int 0L
  | Row.V_int a, Row.V_real b ->
    let c = Float.compare (Int64.to_float a) b in
    if pred c then Row.V_int 1L else Row.V_int 0L
  | _ -> Row.V_int 0L (* cross-type comparisons are false *)

and arith_op lv rv int_f float_f =
  match lv, rv with
  | Row.V_null, _ | _, Row.V_null -> Row.V_null
  | Row.V_int a, Row.V_int b -> Row.V_int (int_f a b)
  | Row.V_real a, Row.V_real b -> Row.V_real (float_f a b)
  | Row.V_int a, Row.V_real b -> Row.V_real (float_f (Int64.to_float a) b)
  | Row.V_real a, Row.V_int b -> Row.V_real (float_f a (Int64.to_float b))
  | _ -> failwith "arithmetic on non-numeric operands"
;;

let project_row (ords : int list) (row : Row.t) : Row.t =
  Array.of_list (List.map (fun i -> row.(i)) ords)
;;

(* ------------------------------------------------------------------ *)
(* CHECK constraint evaluation                                          *)
(* ------------------------------------------------------------------ *)

let ast_binop_to_plan : Ast.binop -> Plan.binop = function
  | Ast.Eq -> Plan.Eq
  | Ast.Ne -> Plan.Ne
  | Ast.Lt -> Plan.Lt
  | Ast.Le -> Plan.Le
  | Ast.Gt -> Plan.Gt
  | Ast.Ge -> Plan.Ge
  | Ast.Add -> Plan.Add
  | Ast.Sub -> Plan.Sub
  | Ast.Mul -> Plan.Mul
  | Ast.Div -> Plan.Div
  | Ast.And -> Plan.And
  | Ast.Or -> Plan.Or
  | Ast.Concat -> Plan.Concat
  | Ast.Mod -> Plan.Mod
  | Ast.Bit_and -> Plan.Bit_and
  | Ast.Bit_or -> Plan.Bit_or
  | Ast.Lshift -> Plan.Lshift
  | Ast.Rshift -> Plan.Rshift
  | Ast.Like -> Plan.Like
  | Ast.Glob -> Plan.Glob
;;

let rec ast_expr_to_plan_check (columns : Row.column list) (e : Ast.expr) : Plan.expr =
  match e with
  | Ast.E_lit l -> Plan.P_lit l
  | Ast.E_col name -> Plan.P_col (find_col_idx_by_name columns name)
  | Ast.E_tbl_col (_, name) -> Plan.P_col (find_col_idx_by_name columns name)
  | Ast.E_binop (op, a, b) ->
    Plan.P_binop
      ( ast_binop_to_plan op
      , ast_expr_to_plan_check columns a
      , ast_expr_to_plan_check columns b )
  | Ast.E_not e -> Plan.P_not (ast_expr_to_plan_check columns e)
  | Ast.E_is_null e -> Plan.P_is_null (ast_expr_to_plan_check columns e)
  | Ast.E_is_not_null e -> Plan.P_is_not_null (ast_expr_to_plan_check columns e)
  | Ast.E_neg e -> Plan.P_neg (ast_expr_to_plan_check columns e)
  | Ast.E_bitnot e -> Plan.P_bitnot (ast_expr_to_plan_check columns e)
  | Ast.E_between (x, lo, hi) ->
    Plan.P_between
      ( ast_expr_to_plan_check columns x
      , ast_expr_to_plan_check columns lo
      , ast_expr_to_plan_check columns hi )
  | Ast.E_in (x, vals) ->
    Plan.P_in
      (ast_expr_to_plan_check columns x, List.map (ast_expr_to_plan_check columns) vals)
  | Ast.E_func (f, args) -> Plan.P_func (f, List.map (ast_expr_to_plan_check columns) args)
  | Ast.E_case { scrutinee; branches; else_ } ->
    let go = ast_expr_to_plan_check columns in
    Plan.P_case
      { scrutinee = Option.map go scrutinee
      ; branches = List.map (fun (c, r) -> go c, go r) branches
      ; else_ = Option.map go else_
      }
  | Ast.E_cast (e, ty) -> Plan.P_cast (ast_expr_to_plan_check columns e, ty)
  | Ast.E_collate (e, c) -> Plan.P_collate (ast_expr_to_plan_check columns e, c)
  | _ -> failwith "ast_expr_to_plan_check: unsupported expression in CHECK"
;;

let compile_check_expr
      (table_name : string)
      (col_idx : int)
      (columns : Row.column list)
      (check_sql : string)
  : Plan.expr
  =
  let key = table_name, col_idx, check_sql in
  match Hashtbl.find_opt check_expr_cache key with
  | Some e -> e
  | None ->
    let lexbuf = Lexing.from_string check_sql in
    let ast_expr =
      try Parser.expr_only Lexer.token lexbuf with
      | Parser.Error | Failure _ ->
        failwith
          (Printf.sprintf
             "CHECK constraint parse error for %s.col%d: %s"
             table_name
             col_idx
             check_sql)
    in
    let plan_expr = ast_expr_to_plan_check columns ast_expr in
    Hashtbl.add check_expr_cache key plan_expr;
    plan_expr
;;

(* Cache for compiled generated-column expressions.
   Key: (table_name, col_idx, expr_sql) — same three-part pattern as check_expr_cache.
   Schema changes invalidate entries via clear on DROP TABLE / DROP COLUMN. *)
let generated_expr_cache : (string * int * string, Plan.expr) Hashtbl.t = Hashtbl.create 8

let compile_generated_expr
      (table_name : string)
      (col_idx : int)
      (columns : Row.column list)
      (expr_sql : string)
  : Plan.expr
  =
  let key = table_name, col_idx, expr_sql in
  match Hashtbl.find_opt generated_expr_cache key with
  | Some e -> e
  | None ->
    let lexbuf = Lexing.from_string expr_sql in
    let ast_expr =
      try Parser.expr_only Lexer.token lexbuf with
      | Parser.Error | Failure _ ->
        failwith
          (Printf.sprintf
             "generated column expr parse error for %s.col%d: %s"
             table_name
             col_idx
             expr_sql)
    in
    let plan_expr = ast_expr_to_plan_check columns ast_expr in
    Hashtbl.add generated_expr_cache key plan_expr;
    plan_expr
;;

(** Compute STORED generated columns on the write path, in-place in [row].
    Iterates columns in schema order; earlier generated columns are available
    to later generated column expressions (in-order dependency). VIRTUAL
    generated columns are set to [V_null] in memory and on disk; they are
    recomputed on read via [compute_virtual_generated_cols]. *)
let compute_stored_generated_cols
      (clock : (unit -> float) option)
      (params : Row.value array)
      (meta : Cat.table_meta)
      (row : Row.t)
  : unit
  =
  (* #347: skip when no column is a STORED generated column — the common case.
     VIRTUAL columns stay at V_null (set by [build_insert_row]'s Array.make). *)
  if
    List.exists
      (fun (c : Row.column) ->
         match c.Row.generated_as with
         | Some (_, true) -> true
         | _ -> false)
      meta.Cat.columns
  then
    List.iteri
      (fun i (col : Row.column) ->
         match col.Row.generated_as with
         | None -> ()
         | Some (sql, true) ->
           let plan_e = compile_generated_expr meta.Cat.name i meta.Cat.columns sql in
           row.(i) <- eval_expr clock params row plan_e
         | Some (_, false) ->
           (* VIRTUAL: write NULL placeholder; recomputed on read. *)
           row.(i) <- Row.V_null)
      meta.Cat.columns
;;

(** Recompute VIRTUAL generated columns from the underlying row values.
    Invoked after [Row.decode] for table-row reads in [exec.ml]. *)
let compute_virtual_generated_cols
      (clock : (unit -> float) option)
      (params : Row.value array)
      (meta : Cat.table_meta)
      (row : Row.t)
  : unit
  =
  List.iteri
    (fun i (col : Row.column) ->
       match col.Row.generated_as with
       | Some (sql, false) ->
         let plan_e = compile_generated_expr meta.Cat.name i meta.Cat.columns sql in
         row.(i) <- eval_expr clock params row plan_e
       | _ -> ())
    meta.Cat.columns
;;

(** Like [compute_virtual_generated_cols] but driven by [(name, columns)]
    rather than a full [Cat.table_meta]. Used by call sites that only have
    a column list in scope (e.g., [execute_create_index]). *)
let compute_virtual_generated_cols_cols
      (clock : (unit -> float) option)
      (params : Row.value array)
      ~(table_name : string)
      (columns : Row.column list)
      (row : Row.t)
  : unit
  =
  List.iteri
    (fun i (col : Row.column) ->
       match col.Row.generated_as with
       | Some (sql, false) ->
         let plan_e = compile_generated_expr table_name i columns sql in
         row.(i) <- eval_expr clock params row plan_e
       | _ -> ())
    columns
;;

let has_virtual_cols (columns : Row.column list) : bool =
  List.exists
    (fun (c : Row.column) ->
       match c.Row.generated_as with
       | Some (_, false) -> true
       | _ -> false)
    columns
;;

(** [with_computed_virtuals]: return a copy of [row] with any VIRTUAL
    generated columns recomputed.  Used by the index-key extraction and
    CHECK-evaluation write paths so that VIRTUAL cells contribute the
    up-to-date value instead of [V_null].  Returns [row] unchanged when
    the table has no virtual columns (the common case). *)
let with_computed_virtuals
      (clock : (unit -> float) option)
      (params : Row.value array)
      (meta : Cat.table_meta)
      (row : Row.t)
  : Row.t
  =
  if not (has_virtual_cols meta.Cat.columns)
  then row
  else (
    let row' = Array.copy row in
    compute_virtual_generated_cols clock params meta row';
    row')
;;

(** Like [with_computed_virtuals] but takes a [(table_name, columns)] pair. *)
let with_computed_virtuals_cols
      (clock : (unit -> float) option)
      (params : Row.value array)
      ~(table_name : string)
      (columns : Row.column list)
      (row : Row.t)
  : Row.t
  =
  if not (has_virtual_cols columns)
  then row
  else (
    let row' = Array.copy row in
    compute_virtual_generated_cols_cols clock params ~table_name columns row';
    row')
;;

(** [decode_with_virtual]: like [Row.decode], but also recomputes any VIRTUAL
    generated columns in the schema. Skips the recompute when the table has
    no virtual cols (the common case). *)
let decode_with_virtual
      (clock : (unit -> float) option)
      (params : Row.value array)
      (meta : Cat.table_meta)
      (bytes : bytes)
  : Row.t
  =
  let row = Row.decode meta.Cat.columns bytes in
  if has_virtual_cols meta.Cat.columns
  then compute_virtual_generated_cols clock params meta row;
  row
;;

(** Variant that takes a [(table_name, columns)] pair instead of a full meta. *)
let decode_with_virtual_cols
      (clock : (unit -> float) option)
      (params : Row.value array)
      ~(table_name : string)
      (columns : Row.column list)
      (bytes : bytes)
  : Row.t
  =
  let row = Row.decode columns bytes in
  if has_virtual_cols columns
  then compute_virtual_generated_cols_cols clock params ~table_name columns row;
  row
;;

let index_where_cache : (string * string * string * string, Plan.expr) Hashtbl.t =
  Hashtbl.create 8
;;

let compile_index_where (idx : Cat.index_info) (columns : Row.column list) : Plan.expr =
  match idx.idx_where_sql with
  | None -> failwith "compile_index_where: called on non-partial index"
  | Some sql ->
    let schema_sig = String.concat "," (List.map (fun c -> c.Row.name) columns) in
    let key = idx.idx_name, idx.idx_table, sql, schema_sig in
    (match Hashtbl.find_opt index_where_cache key with
     | Some e -> e
     | None ->
       let lexbuf = Lexing.from_string sql in
       let ast_expr =
         try Parser.expr_only Lexer.token lexbuf with
         | Parser.Error | Failure _ ->
           failwith (Printf.sprintf "index WHERE parse error for %s: %s" idx.idx_name sql)
       in
       let plan_expr = ast_expr_to_plan_check columns ast_expr in
       Hashtbl.add index_where_cache key plan_expr;
       plan_expr)
;;

let row_matches_index_where
      (clock : (unit -> float) option)
      (params : Row.value array)
      (idx : Cat.index_info)
      (schema : Row.column list)
      (row : Row.t)
  : bool
  =
  match idx.idx_where_sql with
  | None -> true
  | Some _ ->
    let plan_e = compile_index_where idx schema in
    value_truthy (eval_expr clock params row plan_e)
;;

(* Cache for compiled index column expressions.
   Key: (idx_name, idx_table, expr_sql, schema_sig) — four parts to prevent collisions. *)
let index_expr_cache : (string * string * string * string, Plan.expr) Hashtbl.t =
  Hashtbl.create 8
;;

let compile_index_col_expr (idx : Cat.index_info) (i : int) (columns : Row.column list)
  : Plan.expr
  =
  let expr_sql = List.nth idx.idx_columns i in
  let schema_sig = String.concat "," (List.map (fun c -> c.Row.name) columns) in
  let key = idx.idx_name, idx.idx_table, expr_sql, schema_sig in
  match Hashtbl.find_opt index_expr_cache key with
  | Some e -> e
  | None ->
    let lexbuf = Lexing.from_string expr_sql in
    let ast_expr =
      try Parser.expr_only Lexer.token lexbuf with
      | Parser.Error | Failure _ ->
        failwith
          (Printf.sprintf "index expr parse error for %s[%d]: %s" idx.idx_name i expr_sql)
    in
    let plan_e = ast_expr_to_plan_check columns ast_expr in
    Hashtbl.add index_expr_cache key plan_e;
    plan_e
;;

(** Evaluate all index key values for [row] against [idx].
    For expression-indexed columns, evaluates the compiled expression.
    For plain columns, fetches from the row by column ordinal. *)
let get_index_key_values
      (clock : (unit -> float) option)
      (params : Row.value array)
      (idx : Cat.index_info)
      (schema : Row.column list)
      (row : Row.t)
  : Row.value list
  =
  List.mapi
    (fun i col_sql ->
       let is_expr =
         if i < List.length idx.idx_expr_flags
         then List.nth idx.idx_expr_flags i
         else false
       in
       if is_expr
       then (
         let plan_e = compile_index_col_expr idx i schema in
         eval_expr clock params row plan_e)
       else (
         let col_idx = find_col_idx_by_name schema col_sql in
         row.(col_idx)))
    idx.idx_columns
;;

let eval_check_constraints
      (clock : (unit -> float) option)
      (params : Row.value array)
      (table_meta : Cat.table_meta)
      (row : Row.t)
  : unit
  =
  (* #347: skip entirely when no column carries a CHECK — the common case. *)
  if
    List.exists
      (fun (c : Row.column) -> Option.is_some c.check_sql)
      table_meta.Cat.columns
  then (
    (* Phase 35 Task 2: populate VIRTUAL generated columns into a scratch row
       before evaluating CHECKs, so checks that reference a VIRTUAL column see
       the up-to-date value instead of [V_null]. *)
    let row_for_check = with_computed_virtuals clock params table_meta row in
    List.iteri
      (fun i (col : Row.column) ->
         match col.check_sql with
         | None -> ()
         | Some check_sql ->
           let check_plan =
             compile_check_expr table_meta.name i table_meta.columns check_sql
           in
           let result = eval_expr clock params row_for_check check_plan in
           (* SQLite: NULL result -> passes (not a violation) *)
           if result <> Row.V_null && not (value_truthy result)
           then
             failwith
               (Printf.sprintf "CHECK constraint failed: %s.%s" table_meta.name col.name))
      table_meta.columns)
;;

(* ------------------------------------------------------------------ *)
(* FTS inverted-index helpers                                           *)
(* ------------------------------------------------------------------ *)

(** Key format: term_bytes ++ "\x00" ++ rowid_be8
    Rowid stored with sign bit flipped so unsigned byte order = signed int64 order. *)
let fts_term_key term rowid =
  let rb = Bytes.create 8 in
  let v = Int64.logxor rowid Int64.min_int in
  for i = 0 to 7 do
    Bytes.set_uint8
      rb
      i
      (Int64.to_int (Int64.logand (Int64.shift_right_logical v ((7 - i) * 8)) 0xFFL))
  done;
  Bytes.concat Bytes.empty [ Bytes.of_string term; Bytes.of_string "\x00"; rb ]
;;

let fts_stats_key = Bytes.of_string "\x00\x00"

let fts_doclen_key rowid =
  let rb = Bytes.create 8 in
  let v = Int64.logxor rowid Int64.min_int in
  for i = 0 to 7 do
    Bytes.set_uint8
      rb
      i
      (Int64.to_int (Int64.logand (Int64.shift_right_logical v ((7 - i) * 8)) 0xFFL))
  done;
  Bytes.cat (Bytes.of_string "\x00\x01") rb
;;

(** Value: varint pairs (col, pos)* — all positions for one (term, rowid). *)
let encode_positions positions =
  let buf = Buffer.create (List.length positions * 2) in
  List.iter
    (fun (col, pos) ->
       Varint.encode_uint64 buf (Int64.of_int col);
       Varint.encode_uint64 buf (Int64.of_int pos))
    positions;
  Buffer.to_bytes buf
;;

(** FTS content row: n_cols_varint ++ (col_len_varint ++ col_bytes)* *)
let fts_encode_content (texts : string list) : bytes =
  let buf = Buffer.create 64 in
  Varint.encode_uint64 buf (Int64.of_int (List.length texts));
  List.iter
    (fun s ->
       let b = Bytes.of_string s in
       Varint.encode_uint64 buf (Int64.of_int (Bytes.length b));
       Buffer.add_bytes buf b)
    texts;
  Buffer.to_bytes buf
;;

let decode_positions value =
  let len = Bytes.length value in
  let pos = ref 0 in
  let result = ref [] in
  while !pos < len do
    let col, off1 = Varint.decode_uint64 value !pos in
    let p, off2 = Varint.decode_uint64 value off1 in
    result := (Int64.to_int col, Int64.to_int p) :: !result;
    pos := off2
  done;
  List.rev !result
;;

let fts_decode_content bytes =
  let n, off0 = Varint.decode_uint64 bytes 0 in
  let nc = Int64.to_int n in
  let texts = ref [] in
  let pos = ref off0 in
  for _ = 1 to nc do
    let len, off = Varint.decode_uint64 bytes !pos in
    let s = Bytes.sub_string bytes off (Int64.to_int len) in
    texts := s :: !texts;
    pos := off + Int64.to_int len
  done;
  List.rev !texts
;;

(** Read global FTS stats from index tree: (total_docs, total_tokens). *)
let read_fts_stats tx index_tree =
  let+ bytes_opt = S.get tx index_tree fts_stats_key in
  match bytes_opt with
  | None -> 0, 0
  | Some b ->
    let docs, off = Varint.decode_uint64 b 0 in
    let toks, _ = Varint.decode_uint64 b off in
    Int64.to_int docs, Int64.to_int toks
;;

let write_fts_stats tx index_tree docs tokens =
  let buf = Buffer.create 16 in
  Varint.encode_uint64 buf (Int64.of_int docs);
  Varint.encode_uint64 buf (Int64.of_int tokens);
  S.put tx index_tree fts_stats_key (Buffer.to_bytes buf)
;;

(** Write inverted index entries for a newly inserted document. *)
let fts_index_document tx ~(fts_meta : Cat.fts_table_meta) ~rowid ~col_texts =
  let tokens = Fts_tokenizer.tokenize col_texts in
  (* Group by term *)
  let by_term : (string, (int * int) list) Hashtbl.t = Hashtbl.create 8 in
  List.iter
    (fun (tok : Fts_tokenizer.token) ->
       let lst = Option.value ~default:[] (Hashtbl.find_opt by_term tok.term) in
       Hashtbl.replace by_term tok.term ((tok.col, tok.pos) :: lst))
    tokens;
  (* Write one entry per unique term *)
  let* () =
    Hashtbl.fold
      (fun term positions acc ->
         let* () = acc in
         let key = fts_term_key term rowid in
         let value = encode_positions (List.rev positions) in
         S.put tx fts_meta.Cat.fts_index_tree key value)
      by_term
      Lwt.return_unit
  in
  (* Write doc length *)
  let dlen = List.length tokens in
  let dlen_buf = Buffer.create 4 in
  Varint.encode_uint64 dlen_buf (Int64.of_int dlen);
  let* () =
    S.put tx fts_meta.Cat.fts_index_tree (fts_doclen_key rowid) (Buffer.to_bytes dlen_buf)
  in
  (* Update global stats *)
  let* docs, toks = read_fts_stats tx fts_meta.Cat.fts_index_tree in
  write_fts_stats tx fts_meta.Cat.fts_index_tree (docs + 1) (toks + dlen)
;;

(** Remove inverted index entries for a deleted document. *)
let fts_deindex_document tx ~(fts_meta : Cat.fts_table_meta) ~rowid ~col_texts =
  let tokens = Fts_tokenizer.tokenize col_texts in
  let terms =
    List.sort_uniq
      String.compare
      (List.map (fun (t : Fts_tokenizer.token) -> t.term) tokens)
  in
  let* () =
    Lwt_list.iter_s
      (fun term -> S.del tx fts_meta.Cat.fts_index_tree (fts_term_key term rowid))
      terms
  in
  let dlen = List.length tokens in
  let* () = S.del tx fts_meta.Cat.fts_index_tree (fts_doclen_key rowid) in
  let* docs, toks = read_fts_stats tx fts_meta.Cat.fts_index_tree in
  write_fts_stats tx fts_meta.Cat.fts_index_tree (max 0 (docs - 1)) (max 0 (toks - dlen))
;;

(* ------------------------------------------------------------------ *)
(* FTS query execution                                                  *)
(* ------------------------------------------------------------------ *)

(** Fetch the posting list for an exact term: [(rowid, positions)] *)
let fts_posting_list tx ~index_tree term =
  (* Scan keys from term\x00 onwards (sorted order).  Native O(log n) seek +
     lazy streaming of the matching prefix range, instead of draining the whole
     FTS index tree per query (#233; same fix class as #228/#229). *)
  let prefix = Bytes.cat (Bytes.of_string term) (Bytes.of_string "\x00") in
  let plen = Bytes.length prefix in
  let* cur = S.seek_ge tx index_tree prefix in
  let entries = ref [] in
  let rec gather () =
    match%lwt S.seek_next cur with
    | None -> Lwt.return_unit
    | Some (key, value) ->
      if Bytes.length key >= plen && Bytes.equal (Bytes.sub key 0 plen) prefix
      then (
        (* Extract rowid from last 8 bytes (sign-bit-flipped) *)
        let rowid_off = Bytes.length key - 8 in
        let v = ref 0L in
        for i = 0 to 7 do
          v
          := Int64.logor
               (Int64.shift_left !v 8)
               (Int64.of_int (Bytes.get_uint8 key (rowid_off + i)))
        done;
        let rowid = Int64.logxor !v Int64.min_int in
        let positions = decode_positions value in
        entries := (rowid, positions) :: !entries;
        gather ())
      else Lwt.return_unit (* keys are sorted: first non-match ends the range *)
  in
  let* () = gather () in
  S.seek_close cur;
  Lwt.return (List.rev !entries)
;;

(** Fetch posting lists for a prefix: merge all (rowid, positions) for terms matching prefix* *)
let fts_prefix_posting_list tx ~index_tree prefix_str =
  let prefix_bytes = Bytes.of_string prefix_str in
  let plen = Bytes.length prefix_bytes in
  (* Native O(log n) seek + lazy streaming of the matching prefix range,
     instead of draining the whole FTS index tree per query (#233). *)
  let* cur = S.seek_ge tx index_tree prefix_bytes in
  let by_rowid : (int64, (int * int) list) Hashtbl.t = Hashtbl.create 16 in
  let rec gather () =
    match%lwt S.seek_next cur with
    | None -> Lwt.return_unit
    | Some (key, value) ->
      (* Find the null byte separating term from rowid *)
      let null_pos = ref (-1) in
      let klen = Bytes.length key in
      let i = ref 0 in
      while !i < klen - 8 && !null_pos = -1 do
        if Bytes.get_uint8 key !i = 0 then null_pos := !i;
        incr i
      done;
      if !null_pos > 0
      then (
        let term_len = !null_pos in
        (* Check term has our prefix *)
        if term_len >= plen && Bytes.equal (Bytes.sub key 0 plen) prefix_bytes
        then (
          let rowid_off = !null_pos + 1 in
          if rowid_off + 8 <= klen
          then (
            let v = ref 0L in
            for j = 0 to 7 do
              v
              := Int64.logor
                   (Int64.shift_left !v 8)
                   (Int64.of_int (Bytes.get_uint8 key (rowid_off + j)))
            done;
            let rowid = Int64.logxor !v Int64.min_int in
            let positions = decode_positions value in
            let existing = Option.value ~default:[] (Hashtbl.find_opt by_rowid rowid) in
            Hashtbl.replace by_rowid rowid (existing @ positions);
            gather ())
          else Lwt.return_unit)
        else Lwt.return_unit (* term no longer has the prefix, stop — keys are sorted *))
      else Lwt.return_unit
  in
  let* () = gather () in
  S.seek_close cur;
  Lwt.return
    (Hashtbl.fold (fun rowid positions acc -> (rowid, positions) :: acc) by_rowid [])
;;

(* FTS phrase match: all [words] must appear consecutively in the same column.
   For each candidate doc, check there is a start position p and column c with
   word[i] at (col=c, pos=p+i) for all i. *)
(** Execute an FTS query, returning [(rowid, positions)] for matching documents. *)
let fts_phrase_match tx ~index_tree words =
  match words with
  | [] -> Lwt.return []
  | first :: rest ->
    let* first_pl = fts_posting_list tx ~index_tree first in
    let* rest_pls = Lwt_list.map_s (fts_posting_list tx ~index_tree) rest in
    (* Keep only docs present in every posting list. *)
    let intersect_ids acc pl =
      let ids = List.map fst pl in
      List.filter (fun (r, _) -> List.mem r ids) acc
    in
    let candidates = List.fold_left intersect_ids first_pl rest_pls in
    (* Build an array of per-term posting lists for position checking. *)
    let all_pls = Array.of_list (first_pl :: rest_pls) in
    let n = Array.length all_pls in
    (* Check whether doc with [rowid] contains the phrase. *)
    let phrase_matches rowid =
      let term_positions =
        Array.map
          (fun pl ->
             match List.assoc_opt rowid pl with
             | None -> []
             | Some pos -> pos)
          all_pls
      in
      List.exists
        (fun (c0, p0) ->
           let rec check i =
             if i >= n
             then true
             else List.mem (c0, p0 + i) term_positions.(i) && check (i + 1)
           in
           check 1)
        term_positions.(0)
    in
    let matched = List.filter (fun (r, _) -> phrase_matches r) candidates in
    Lwt.return matched
;;

let rec fts_execute_query tx ~index_tree query =
  match query with
  | Fts_query.FQ_term (Fts_query.FT_exact term) -> fts_posting_list tx ~index_tree term
  | Fts_query.FQ_term (Fts_query.FT_prefix prefix) ->
    fts_prefix_posting_list tx ~index_tree prefix
  | Fts_query.FQ_term (Fts_query.FT_phrase words) -> fts_phrase_match tx ~index_tree words
  | Fts_query.FQ_and qs ->
    let positive =
      List.filter
        (function
          | Fts_query.FQ_not _ -> false
          | _ -> true)
        qs
    in
    let negated =
      List.filter_map
        (function
          | Fts_query.FQ_not q -> Some q
          | _ -> None)
        qs
    in
    let* pos_results = Lwt_list.map_s (fts_execute_query tx ~index_tree) positive in
    let* neg_results = Lwt_list.map_s (fts_execute_query tx ~index_tree) negated in
    let neg_ids = List.concat_map (List.map fst) neg_results in
    let intersected =
      match pos_results with
      | [] -> []
      | first :: rest ->
        List.fold_left
          (fun acc pl ->
             let ids = List.map fst pl in
             List.filter (fun (r, _) -> List.mem r ids) acc)
          first
          rest
    in
    Lwt.return (List.filter (fun (r, _) -> not (List.mem r neg_ids)) intersected)
  | Fts_query.FQ_or qs ->
    let* results = Lwt_list.map_s (fts_execute_query tx ~index_tree) qs in
    let seen : (int64, unit) Hashtbl.t = Hashtbl.create 16 in
    let union =
      List.concat_map
        (fun pl ->
           List.filter
             (fun (r, _) ->
                if Hashtbl.mem seen r
                then false
                else (
                  Hashtbl.replace seen r ();
                  true))
             pl)
        results
    in
    Lwt.return union
  | Fts_query.FQ_not _ ->
    (* Standalone NOT is meaningless; returns empty set.
       NOT inside AND is handled in the FQ_and case above. *)
    Lwt.return []
;;

(** Helper: find the first index [i] such that [pred lst[i]] holds. *)
let list_find_index pred lst =
  let rec go i = function
    | [] -> None
    | x :: _ when pred x -> Some (i, x)
    | _ :: rest -> go (i + 1) rest
  in
  go 0 lst
;;

(* ------------------------------------------------------------------ *)
(* Transaction mode                                                     *)
(* ------------------------------------------------------------------ *)

type txn_mode =
  | Auto (** Each DML op starts and commits its own RW txn. *)
  | In_txn of S.rw S.txn (** Use this txn; skip auto begin/commit. *)
  | In_ro_txn of S.ro S.txn
  (** #274: read every scan through this one RO snapshot so a multi-statement
        read (e.g. [Db.dump] sweeping every table) observes a single
        point-in-time committed state.  Read-only: never reaches a write path;
        its lifecycle is owned by the caller, not ended by a scanner. *)

let acquire_txn store mode =
  match mode with
  | Auto ->
    let* tx = S.rw_begin store in
    Lwt.return (tx, true)
  | In_txn tx -> Lwt.return (tx, false)
  | In_ro_txn _ ->
    (* A write was attempted under a read-only ambient snapshot — a caller bug,
       not a runtime condition: [In_ro_txn] is only ever set on read paths. *)
    Lwt.fail (Failure "write attempted under a read-only transaction (In_ro_txn)")
;;

(* Commit [tx] if [owned], otherwise no-op.  When [cat] is provided and
   [owned], flush deferred rowid counters first (#347) so that counters
   dirtied by nested In_txn DML (e.g. trigger inserts) are persisted, then
   persist any dirty columnar stores before committing.  After commit
   succeeds, clear the dirty flag on the stores that were persisted so the
   next cycle only flushes new mutations. *)
let release_txn ?cat tx owned =
  if owned
  then (
    let* saved =
      match cat with
      | None -> Lwt.return []
      | Some c ->
        let* () = Cat.flush_dirty_counters_tx c tx in
        Cat.persist_dirty_columnar_stores c tx
    in
    let* () = S.commit tx in
    List.iter Granary_columnar.Col_store.mark_clean saved;
    Lwt.return_unit)
  else Lwt.return_unit
;;

(** Extract the in-memory columnar store from a table_meta.  Asserts [Row]
    cannot happen at call sites guarded by [Cat.is_columnar]. *)
let col_store_of_meta (m : Cat.table_meta) : Granary_columnar.Col_store.t =
  match m.Cat.storage with
  | Cat.Columnar (cs, _) -> cs
  | Cat.Row _ -> assert false
;;

(* #269: run a DDL body [f tx] under a transaction chosen by [mode], threading
   the writer txn into the catalog so DDL participates in any ambient explicit
   transaction instead of opening its own (which would self-deadlock against the
   single-writer lock the explicit txn already holds).

   Ownership decides who finalizes:
   - [Auto] (we opened the txn): commit on success / rollback on failure, and
     correspondingly clear or run the catalog's schema-cache undo log — the DDL
     mutated the in-memory cache before this commit, so a rollback must revert it.
   - [In_txn] (borrowed): leave commit/rollback AND schema-undo finalization to
     the db layer's COMMIT/ROLLBACK; an error here propagates with the ambient
     transaction left open.  A failed in-txn DDL statement may have already
     applied partial on-disk effects (e.g. [alter_drop_column] dropping a
     dependent index, then rewriting rows, before [Cat.drop_column] raises), and
     we have no statement-level savepoint to undo just this statement (#280/#283).
     So we poison the catalog (#286): the db layer forces a later COMMIT to roll
     the whole transaction back instead of persisting the half-applied DDL — the
     transaction is uncommittable, matching SQLite.  A ROLLBACK still unwinds
     cleanly via the whole-txn schema-undo log + store rollback. *)
let with_ddl_txn store (cat : Cat.t) mode f =
  let* tx, owned = acquire_txn store mode in
  Lwt.catch
    (fun () ->
       let* r = f tx in
       let* () =
         if owned
         then (
           let* () = S.commit tx in
           Cat.commit_schema_changes cat;
           Lwt.return_unit)
         else Lwt.return_unit
       in
       Lwt.return r)
    (fun exn ->
       let* () =
         if owned
         then (
           let* () = S.rollback tx in
           Cat.rollback_schema_changes cat;
           Lwt.return_unit)
         else (
           (* #286: borrowed txn — partial effects remain; mark uncommittable. *)
           Cat.mark_schema_txn_poisoned cat;
           Lwt.return_unit)
       in
       Lwt.fail exn)
;;

(* #262: a read handle for a base scanner.  Inside an explicit transaction
   ([In_txn tx]) reads must go THROUGH [tx] so they observe the transaction's
   own uncommitted writes (read-your-own-writes).  A scanner that instead opens
   a fresh RO snapshot ([S.ro_begin]) is, by snapshot-isolation design (#178),
   blind to the active writer's in-flight mutations — so a [SELECT] after
   [BEGIN; INSERT] would see the pre-[BEGIN] committed state.  In [Auto] mode
   (no explicit txn) the scanner owns a fresh RO snapshot and ends it when the
   read finishes; the borrowed txn is never ended here — Db owns its lifecycle. *)
type read_handle =
  | RH_borrowed of S.rw S.txn (* active explicit txn; lifecycle owned by Db *)
  | RH_borrowed_ro of S.ro S.txn (* #274: shared RO snapshot; lifecycle owned by caller *)
  | RH_owned of S.ro S.txn (* scanner-owned RO snapshot; ended on finish *)

let rh_begin store = function
  | In_txn tx -> Lwt.return (RH_borrowed tx)
  | In_ro_txn tx -> Lwt.return (RH_borrowed_ro tx)
  | Auto ->
    let* tx = S.ro_begin store in
    Lwt.return (RH_owned tx)
;;

let rh_finish = function
  | RH_borrowed _ | RH_borrowed_ro _ -> Lwt.return_unit
  | RH_owned tx -> S.ro_end tx
;;

let rh_get = function
  | RH_borrowed tx -> S.get tx
  | RH_borrowed_ro tx -> S.get tx
  | RH_owned tx -> S.get tx
;;

let rh_seek_ge = function
  | RH_borrowed tx -> S.seek_ge tx
  | RH_borrowed_ro tx -> S.seek_ge tx
  | RH_owned tx -> S.seek_ge tx
;;

let rh_cursor_open = function
  | RH_borrowed tx -> S.cursor_open tx
  | RH_borrowed_ro tx -> S.cursor_open tx
  | RH_owned tx -> S.cursor_open tx
;;

(* [with_read store mode f] runs [f] over a read handle, ending it afterwards
   only when the scanner owns it (Auto).  The txn-aware analogue of [S.with_ro];
   like it, the handle is released even if [f] raises (#164). *)
let with_read store mode f =
  let* rh = rh_begin store mode in
  Lwt.finalize (fun () -> f rh) (fun () -> rh_finish rh)
;;

(* ------------------------------------------------------------------ *)
(* execute: write operations only                                       *)
(* ------------------------------------------------------------------ *)

(** Replace every [P_excluded_col i] with [P_lit (value_to_literal excluded_row.(i))].
    Used to materialise UPSERT excluded-row refs before [eval_expr]. *)
let rec substitute_excluded (excluded_row : Row.t) (e : Plan.expr) : Plan.expr =
  match e with
  | Plan.P_excluded_col i -> Plan.P_lit (value_to_literal excluded_row.(i))
  | Plan.P_binop (op, a, b) ->
    Plan.P_binop
      (op, substitute_excluded excluded_row a, substitute_excluded excluded_row b)
  | Plan.P_not e -> Plan.P_not (substitute_excluded excluded_row e)
  | Plan.P_is_null e -> Plan.P_is_null (substitute_excluded excluded_row e)
  | Plan.P_is_not_null e -> Plan.P_is_not_null (substitute_excluded excluded_row e)
  | Plan.P_neg e -> Plan.P_neg (substitute_excluded excluded_row e)
  | Plan.P_bitnot e -> Plan.P_bitnot (substitute_excluded excluded_row e)
  | Plan.P_between (x, lo, hi) ->
    Plan.P_between
      ( substitute_excluded excluded_row x
      , substitute_excluded excluded_row lo
      , substitute_excluded excluded_row hi )
  | Plan.P_in (x, vals) ->
    Plan.P_in
      ( substitute_excluded excluded_row x
      , List.map (substitute_excluded excluded_row) vals )
  | Plan.P_func (f, args) ->
    Plan.P_func (f, List.map (substitute_excluded excluded_row) args)
  | Plan.P_case { scrutinee; branches; else_ } ->
    let go = substitute_excluded excluded_row in
    Plan.P_case
      { scrutinee = Option.map go scrutinee
      ; branches = List.map (fun (c, r) -> go c, go r) branches
      ; else_ = Option.map go else_
      }
  | Plan.P_cast (e, ty) -> Plan.P_cast (substitute_excluded excluded_row e, ty)
  | Plan.P_collate (e, c) -> Plan.P_collate (substitute_excluded excluded_row e, c)
  | other -> other
;;

(** True if any value in the list is NULL. *)
let any_null_val = List.exists (fun v -> v = Row.V_null)

(** Find column indices for a list of column names in [schema].
    Returns [None] for any name not found. *)
let find_col_idxs schema col_names =
  List.map
    (fun name ->
       let rec fi i = function
         | [] -> None
         | (c : Row.column) :: _ when String.equal c.name name -> Some i
         | _ :: rest -> fi (i + 1) rest
       in
       fi 0 schema)
    col_names
;;

(** Non-raising variant of find_col_idx_by_name: returns [None] if not found. *)
let find_col_idx_by_name_opt schema col_name =
  let rec fi i = function
    | [] -> None
    | (c : Row.column) :: _ when String.equal c.name col_name -> Some i
    | _ :: rest -> fi (i + 1) rest
  in
  fi 0 schema
;;

(** Encode a multi-column index-key prefix (no rowid).  Used by FK enforcement
    to seek to the first entry whose leading key columns match a target value
    list.  Returns the prefix bytes and their length. *)
let encode_index_key_prefix (ivs : Index_key.value list) : bytes * int =
  let parts = List.map Index_key.encode_value ivs in
  let total = List.fold_left (fun acc b -> acc + Bytes.length b) 0 parts in
  let buf = Bytes.create total in
  let off = ref 0 in
  List.iter
    (fun b ->
       let len = Bytes.length b in
       Bytes.blit b 0 buf !off len;
       off := !off + len)
    parts;
  buf, total
;;

(** Decode the rowid from the trailing 8 bytes of an index key. *)
let decode_index_key_rowid (ikey : bytes) : int64 =
  let n = Bytes.length ikey in
  let v = ref 0L in
  for i = 0 to 7 do
    v
    := Int64.logor
         (Int64.shift_left !v 8)
         (Int64.of_int (Bytes.get_uint8 ikey (n - 8 + i)))
  done;
  Int64.logxor !v Int64.min_int
;;

(* Full-table-scan fallbacks shared by the FK lookup/scan helpers below
   (used when no index covers the child columns). [full_scan_exists] stops at
   the first matching row; [full_scan_collect] gathers all (rowid,row) matches. *)
let full_scan_exists tx (meta : Cat.table_meta) (pred : Row.t -> bool) : bool Lwt.t =
  let tree_id, _, _, _ = Cat.row_storage meta in
  let* cur = S.cursor_open tx tree_id in
  let _sr = S.cursor_first cur in
  let found = ref false in
  let rec scan () =
    if !found
    then ()
    else (
      match S.cursor_next cur with
      | None -> ()
      | Some (_k, vbytes) ->
        let row = decode_with_virtual None [||] meta vbytes in
        if pred row then found := true else scan ())
  in
  scan ();
  S.cursor_close cur;
  Lwt.return !found
;;

let full_scan_collect tx (meta : Cat.table_meta) (pred : Row.t -> bool)
  : (int64 * Row.t) list Lwt.t
  =
  let tree_id, _, _, _ = Cat.row_storage meta in
  let* cur = S.cursor_open tx tree_id in
  let _sr = S.cursor_first cur in
  let buf = ref [] in
  let rec scan () =
    match S.cursor_next cur with
    | None -> ()
    | Some (kbytes, vbytes) ->
      let rowid = Rowid.decode kbytes in
      let row = decode_with_virtual None [||] meta vbytes in
      if pred row then buf := (rowid, row) :: !buf;
      scan ()
  in
  scan ();
  S.cursor_close cur;
  Lwt.return (List.rev !buf)
;;

(** Internal: scan [child_meta] within an already-open transaction (RO or RW)
    for any row whose [child_col_idxs] match [parent_vals].  Used by both the
    public store-opening variant below and the deferred FK recheck path
    (which must see writes performed in the active RW txn — opening a fresh
    [ro_begin] on the B+-tree backend would snapshot the pre-txn state and
    miss the about-to-commit rows). *)
let fk_child_has_ref_multi_in_tx
      (cat : Cat.t)
      tx
      (child_meta : Cat.table_meta)
      ~(child_col_idxs : int list)
      ~(parent_vals : Row.value list)
  =
  match
    Cat.find_index_covering_cols
      cat
      ~table_name:child_meta.Cat.name
      ~col_idxs:child_col_idxs
  with
  | Some idx when not (List.exists (fun v -> v = Row.V_null) parent_vals) ->
    let ivs = List.map row_value_to_index_value parent_vals in
    let prefix, plen = encode_index_key_prefix ivs in
    let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
    (* O(log n) native seek; stop at the first non-matching prefix (#228/#229). *)
    let* cur = S.seek_ge tx idx.Cat.idx_tree_id seek_key in
    let found = ref false in
    let exhausted = ref false in
    let rec walk () =
      if !found || !exhausted
      then Lwt.return_unit
      else (
        match%lwt S.seek_next cur with
        | None ->
          exhausted := true;
          Lwt.return_unit
        | Some (ikey, _ival) ->
          if Bytes.length ikey >= plen + 8 && Bytes.equal (Bytes.sub ikey 0 plen) prefix
          then (
            let rowid = decode_index_key_rowid ikey in
            let child_tree_id, _, _, _ = Cat.row_storage child_meta in
            let* row_opt = S.get tx child_tree_id (Rowid.encode rowid) in
            match row_opt with
            | None -> walk ()
            | Some vbytes ->
              let row = decode_with_virtual None [||] child_meta vbytes in
              let ok =
                List.for_all2
                  (fun ci pv -> compare_values row.(ci) pv = 0)
                  child_col_idxs
                  parent_vals
              in
              if ok
              then (
                found := true;
                Lwt.return_unit)
              else walk ())
          else (
            exhausted := true;
            Lwt.return_unit))
    in
    let* () = walk () in
    S.seek_close cur;
    Lwt.return !found
  | _ ->
    full_scan_exists tx child_meta (fun row ->
      List.for_all2
        (fun ci pv -> compare_values row.(ci) pv = 0)
        child_col_idxs
        parent_vals)
;;

(** Scan [child_meta] for any row where all [child_col_idxs] match [parent_vals]
    simultaneously.  When an index covers [child_col_idxs] as a leading prefix,
    use it; otherwise fall back to a full table scan.
    Opens and closes its own RO snapshot. *)
let fk_child_has_ref_multi
      (cat : Cat.t)
      store
      (child_meta : Cat.table_meta)
      ~(child_col_idxs : int list)
      ~(parent_vals : Row.value list)
  =
  S.with_ro store
  @@ fun ro_tx ->
  fk_child_has_ref_multi_in_tx cat ro_tx child_meta ~child_col_idxs ~parent_vals
;;

(** Internal: scan [parent_meta] within an already-open transaction (RO or
    RW) for a row matching [parent_vals] on [parent_idxs].  Used by the
    deferred FK recheck path to observe uncommitted writes in the active
    write txn. *)
let fk_parent_has_row_in_tx
      tx
      (parent_meta : Cat.table_meta)
      ~(parent_idxs : int list)
      ~(parent_vals : Row.value list)
  : bool Lwt.t
  =
  (* Columnar tables cannot be FK parents — no unique constraints, no indexes. *)
  if Cat.is_columnar parent_meta
  then Lwt.return false
  else (
    let parent_tree_id, _, _, _ = Cat.row_storage parent_meta in
    let* cur = S.cursor_open tx parent_tree_id in
    let _sr = S.cursor_first cur in
    let found = ref false in
    let rec scan () =
      if !found
      then ()
      else (
        match S.cursor_next cur with
        | None -> ()
        | Some (_k, vbytes) ->
          let row = decode_with_virtual None [||] parent_meta vbytes in
          let ok =
            List.for_all2
              (fun pi pv -> compare_values row.(pi) pv = 0)
              parent_idxs
              parent_vals
          in
          if ok then found := true else scan ())
    in
    scan ();
    S.cursor_close cur;
    Lwt.return !found)
;;

(** Scan [parent_meta] for a row matching [parent_vals] on [parent_idxs].
    Returns true iff such a row exists.  Used at INSERT/UPDATE time
    (immediate FK enforcement); opens and closes its own RO snapshot. *)
let fk_parent_has_row
      store
      (parent_meta : Cat.table_meta)
      ~(parent_idxs : int list)
      ~(parent_vals : Row.value list)
  : bool Lwt.t
  =
  S.with_ro store
  @@ fun ro_tx ->
  let* found = fk_parent_has_row_in_tx ro_tx parent_meta ~parent_idxs ~parent_vals in
  Lwt.return found
;;

(** Helper for FK enforcement: routes a violation either to the pending
    queue (deferred) or raises immediately (immediate).  [recheck] is the
    closure invoked at commit time; it must return true iff the violation
    is still present. *)
let fk_violation
      ~deferred
      (cat : Cat.t)
      ~kind
      ~table
      ~rowid
      ~msg
      ~(recheck : Cat.pending_fk_recheck)
  =
  if deferred
  then (
    Cat.queue_pending_fk_check
      cat
      { Cat.pfk_kind = kind
      ; Cat.pfk_table = table
      ; Cat.pfk_rowid = rowid
      ; Cat.pfk_message = msg
      ; Cat.pfk_recheck = recheck
      };
    Lwt.return_unit)
  else Lwt.fail_with msg
;;

(* Immediate/deferred FK existence check for one [fk] of an INSERT row. *)
let enforce_insert_fk
      store
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      (row : Row.t)
      (fk : Cat.fk_constraint)
  : unit Lwt.t
  =
  let is_deferred = fk.fk_deferrable || Cat.get_defer_fks_pragma cat in
  let local_idxs_opt = find_col_idxs table_meta.Cat.columns fk.fk_local_cols in
  if List.exists Option.is_none local_idxs_opt
  then
    Lwt.fail_with
      (Printf.sprintf
         "FOREIGN KEY: some local columns not found in table '%s'"
         table_meta.Cat.name)
  else (
    let local_idxs = List.filter_map Fun.id local_idxs_opt in
    let local_vals = List.map (fun i -> row.(i)) local_idxs in
    (* NULL in any FK column => skip enforcement *)
    if any_null_val local_vals
    then Lwt.return_unit
    else (
      match Cat.find_table_cached cat ~name:fk.fk_parent_table with
      | None ->
        Lwt.fail_with
          (Printf.sprintf "FOREIGN KEY: parent table '%s' not found" fk.fk_parent_table)
      | Some parent_meta ->
        let parent_idxs_opt = find_col_idxs parent_meta.Cat.columns fk.fk_parent_cols in
        let parent_idxs = List.filter_map Fun.id parent_idxs_opt in
        if List.length parent_idxs <> List.length fk.fk_parent_cols
        then
          Lwt.fail_with
            (Printf.sprintf
               "FOREIGN KEY: column not found in parent table '%s'"
               fk.fk_parent_table)
        else (
          let child_col_idxs = local_idxs in
          let table_name = table_meta.Cat.name in
          let parent_meta_name = parent_meta.Cat.name in
          let msg =
            Printf.sprintf
              "FOREIGN KEY constraint failed: no row in '%s' where %s matches"
              fk.fk_parent_table
              (String.concat ", " fk.fk_parent_cols)
          in
          let* found =
            fk_parent_has_row store parent_meta ~parent_idxs ~parent_vals:local_vals
          in
          if found
          then Lwt.return_unit
          else (
            (* Deferred recheck threads the active write txn so it observes
              uncommitted writes (a fresh ro_begin would miss them). *)
            let recheck =
              { Cat.recheck =
                  (fun (type m) (recheck_tx : m S.txn) ->
                    match
                      ( Cat.find_table_cached cat ~name:table_name
                      , Cat.find_table_cached cat ~name:parent_meta_name )
                    with
                    | None, _ | _, None -> Lwt.return false
                    | Some child_now, Some parent_now ->
                      let* has_child =
                        fk_child_has_ref_multi_in_tx
                          cat
                          recheck_tx
                          child_now
                          ~child_col_idxs
                          ~parent_vals:local_vals
                      in
                      if not has_child
                      then Lwt.return false
                      else
                        let* has_parent =
                          fk_parent_has_row_in_tx
                            recheck_tx
                            parent_now
                            ~parent_idxs
                            ~parent_vals:local_vals
                        in
                        Lwt.return (not has_parent))
              }
            in
            fk_violation
              ~deferred:is_deferred
              cat
              ~kind:`Insert
              ~table:table_name
              ~rowid:0L
              ~msg
              ~recheck))))
;;

(* Evaluate all FK constraints for an INSERT of [row] before any writes. *)
let enforce_insert_fks store (cat : Cat.t) (table_meta : Cat.table_meta) (row : Row.t)
  : unit Lwt.t
  =
  let fks = table_meta.Cat.fk_constraints in
  if fks = [] || not (Cat.get_fk_enforcement cat)
  then Lwt.return_unit
  else Lwt_list.iter_s (enforce_insert_fk store cat table_meta row) fks
;;

(* Resolve the rowid for an INSERT: the INTEGER PRIMARY KEY for WITHOUT ROWID
   tables (must be present, non-NULL, integer), else a freshly allocated one. *)
let insert_rowid
      ?(defer_counter = false)
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      (row : Row.t)
  : int64 Lwt.t
  =
  let _, _, without_rowid, autoincrement = Cat.row_storage table_meta in
  if without_rowid
  then (
    match
      List.find_index (fun (c : Row.column) -> c.primary_key) table_meta.Cat.columns
    with
    | None ->
      Lwt.fail_with
        (Printf.sprintf
           "WITHOUT ROWID table '%s' has no PRIMARY KEY column"
           table_meta.Cat.name)
    | Some pk_idx ->
      (match row.(pk_idx) with
       | Row.V_int n -> Lwt.return n
       | Row.V_null ->
         Lwt.fail_with
           (Printf.sprintf
              "WITHOUT ROWID table '%s': PRIMARY KEY column must not be NULL"
              table_meta.Cat.name)
       | _ ->
         Lwt.fail_with
           (Printf.sprintf
              "WITHOUT ROWID table '%s': PRIMARY KEY column must be INTEGER"
              table_meta.Cat.name)))
  else (
    match Cat.rowid_alias_col table_meta with
    | Some pk_idx ->
      (* #243 (T1): INTEGER PRIMARY KEY IS the rowid.  Use the supplied integer
         as the table key; on NULL/omitted, auto-allocate and write it back so
         [SELECT id] / RETURNING observe the assigned value.  An explicit value
         advances the autoincrement counter past it (SQLite parity: a later NULL
         insert gets max(existing)+1). *)
      (match row.(pk_idx) with
       | Row.V_int n ->
         let* () =
           if Int64.compare n Int64.max_int < 0
           then
             Cat.bump_next_rowid_in_txn
               ~defer_counter
               cat
               ~name:table_meta.name
               ~at_least:(Int64.add n 1L)
               tx
           else if autoincrement
           then
             (* #312: pin the AUTOINCREMENT counter at max_int so the next
                auto-allocation detects exhaustion and raises SQLITE_FULL. *)
             Cat.bump_next_rowid_in_txn
               ~defer_counter
               cat
               ~name:table_meta.name
               ~at_least:Int64.max_int
               tx
           else Lwt.return_unit
         in
         Lwt.return n
       | Row.V_null ->
         let* id = Cat.next_rowid_in_txn ~defer_counter cat ~name:table_meta.name tx in
         row.(pk_idx) <- Row.V_int id;
         Lwt.return id
       | _ ->
         Lwt.fail_with
           (Printf.sprintf
              "datatype mismatch: INTEGER PRIMARY KEY column '%s' requires an integer"
              (List.nth table_meta.columns pk_idx).Row.name))
    | None -> Cat.next_rowid_in_txn ~defer_counter cat ~name:table_meta.name tx)
;;

(* SQLite-faithful UNIQUE violation message: "UNIQUE constraint failed: t.a"
   (each column listed as "<table>.<col>", comma-separated for composite keys).
   Shared by every secondary-index uniqueness check — INSERT-time
   ([check_insert_unique]), UPDATE-time ([check_index_unique_on_update]) and the
   CREATE UNIQUE INDEX build (#288) — so all three report identically, and match
   the rowid-alias/PRIMARY KEY paths that already use this exact wording. *)
let unique_constraint_failed_msg ~(table : string) ~(columns : string list) : string =
  Printf.sprintf
    "UNIQUE constraint failed: %s"
    (String.concat ", " (List.map (fun c -> table ^ "." ^ c) columns))
;;

(* UNIQUE pre-check for INSERT: fold over [idxs] returning (skip, rowids to
   delete for REPLACE, optional rowid to update for UPSERT). Raises on a plain
   UNIQUE violation. *)
let check_insert_unique
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~(row_for_idx : Row.t)
      ~(on_conflict : Ast.conflict_action option)
      ~(upsert_update : (string list * (int * Plan.expr) list) option)
      (idxs : Cat.index_info list)
  : (bool * int64 list * int64 option) Lwt.t
  =
  Lwt_list.fold_left_s
    (fun (skip, dels, upsert_rid) (idx : Cat.index_info) ->
       if skip || not idx.idx_unique
       then Lwt.return (skip, dels, upsert_rid)
       else if
         not (row_matches_index_where clock params idx table_meta.columns row_for_idx)
       then Lwt.return (skip, dels, upsert_rid)
       else (
         let key_vals =
           get_index_key_values clock params idx table_meta.columns row_for_idx
         in
         (* #290: SQLite treats every NULL as distinct in a UNIQUE index — a row
            whose key has ANY NULL column is exempt from the uniqueness probe (it
            is still inserted into the index tree, it just never conflicts). *)
         if any_null_val key_vals
         then Lwt.return (false, dels, upsert_rid)
         else (
           let iks = List.map row_value_to_index_value key_vals in
           let prefix, plen = encode_index_key_prefix iks in
           let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
           (* O(log n) native probe: only the first entry >= seek_key is needed
            to detect a duplicate prefix — never drain the whole index (#229). *)
           let* cur = S.seek_ge tx idx.idx_tree_id seek_key in
           let* first = S.seek_next cur in
           let conflict_rowid_opt =
             match first with
             | None -> None
             | Some (ikey, _) ->
               if Bytes.length ikey >= plen && Bytes.equal (Bytes.sub ikey 0 plen) prefix
               then (
                 let rid_bytes = Bytes.sub ikey plen (Bytes.length ikey - plen) in
                 Some (Rowid.decode rid_bytes))
               else None
           in
           S.seek_close cur;
           match conflict_rowid_opt with
           | None -> Lwt.return (false, dels, upsert_rid)
           | Some old_rowid ->
             (match on_conflict, upsert_update with
              | Some Ast.CA_ignore, _ ->
                Lwt.return (true, dels, upsert_rid) (* skip=true, stop checking *)
              | Some Ast.CA_replace, _ -> Lwt.return (false, old_rowid :: dels, upsert_rid)
              | _, Some (conflict_cols, _)
                when List.sort String.compare idx.idx_columns
                     = List.sort String.compare conflict_cols ->
                Lwt.return (false, dels, Some old_rowid)
              | _ ->
                Lwt.fail_with
                  (unique_constraint_failed_msg
                     ~table:table_meta.Cat.name
                     ~columns:idx.idx_columns)))))
    (false, [], None)
    idxs
;;

(* Write [row]'s index entries (honoring each index's WHERE predicate). *)
let insert_row_indexes
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~(row_for_idx : Row.t)
      ~rowid
      (idxs : Cat.index_info list)
  : unit Lwt.t
  =
  Lwt_list.iter_s
    (fun (idx : Cat.index_info) ->
       if not (row_matches_index_where clock params idx table_meta.columns row_for_idx)
       then Lwt.return_unit
       else (
         let iks =
           List.map
             row_value_to_index_value
             (get_index_key_values clock params idx table_meta.columns row_for_idx)
         in
         let ikey = Index_key.encode iks ~rowid in
         S.put tx idx.idx_tree_id ikey Bytes.empty))
    idxs
;;

(* [row_for_idx] must already have VIRTUAL generated columns applied (e.g.
   via [decode_with_virtual] or [with_computed_virtuals]).  Callers that
   obtain the row from [decode_with_virtual] can pass it directly — virtual
   cols are computed in-place there, so no extra [Array.copy] is needed. *)
let delete_row_indexes
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~(row_for_idx : Row.t)
      ~rowid
      indexes
  : unit Lwt.t
  =
  let schema = table_meta.Cat.columns in
  Lwt_list.iter_s
    (fun (idx : Cat.index_info) ->
       if not (row_matches_index_where clock params idx schema row_for_idx)
       then Lwt.return_unit
       else (
         let iks =
           List.map
             row_value_to_index_value
             (get_index_key_values clock params idx schema row_for_idx)
         in
         let old_ikey = Index_key.encode iks ~rowid in
         S.del tx idx.idx_tree_id old_ikey))
    indexes
;;

(* REPLACE conflict resolution: delete each [to_delete] row and its index
   entries (firing BEFORE DELETE); returns the displaced rows in original order. *)
let delete_replace_conflicts
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~(idxs : Cat.index_info list)
      ~on_replace_delete_before
      to_delete
  : Row.t list Lwt.t
  =
  let displaced_rows : Row.t list ref = ref [] in
  let* () =
    Lwt_list.iter_s
      (fun old_rowid ->
         let old_key = Rowid.encode old_rowid in
         let* old_bytes_opt =
           S.get
             tx
             (let x, _, _, _ = Cat.row_storage table_meta in
              x)
             old_key
         in
         match old_bytes_opt with
         | None -> Lwt.return_unit
         | Some old_bytes ->
           let old_row = decode_with_virtual clock params table_meta old_bytes in
           displaced_rows := old_row :: !displaced_rows;
           (* #417: REPLACE/INSERT OR REPLACE displaces a conflicting row before
              re-inserting; record the removed row so the delta feed pairs this
              [Deleted] with the [Inserted] the insert path emits. *)
           record_change
             table_meta.Cat.name
             (Deleted { rowid = old_rowid; row = old_row });
           let* () =
             match on_replace_delete_before with
             | None -> Lwt.return_unit
             | Some f -> f ~tx ~old_row
           in
           let* () =
             S.del
               tx
               (let x, _, _, _ = Cat.row_storage table_meta in
                x)
               old_key
           in
           (* old_row from decode_with_virtual already has VIRTUAL cols applied *)
           delete_row_indexes
             tx
             table_meta
             ~clock
             ~params
             ~row_for_idx:old_row
             ~rowid:old_rowid
             idxs)
      (List.sort_uniq compare to_delete)
  in
  Lwt.return (List.rev !displaced_rows)
;;

(* #243/#249: write [new_row] for the row currently stored at [old_rowid],
   MOVING it to a new table-tree key when the INTEGER PRIMARY KEY alias column
   changed — with a uniqueness probe on the new key — and re-keying its
   secondary-index entries (the rowid is their key suffix) old->new.  For a
   non-alias table or an unchanged alias this is an in-place rewrite.  Returns
   the (possibly new) rowid.

   Every single-row UPDATE path (UPDATE, UPSERT DO UPDATE, ON UPDATE CASCADE)
   funnels through this so none can independently re-introduce the divergence
   between the stored key and the id column (#249). *)
let write_row_rekeyed
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~(old_row : Row.t)
      ~(new_row : Row.t)
      ~old_rowid
      ~indexes
  : int64 Lwt.t
  =
  let alias_col = Cat.rowid_alias_col table_meta in
  let* new_rowid =
    match alias_col with
    | None -> Lwt.return old_rowid
    | Some i ->
      (match new_row.(i) with
       | Row.V_int n -> Lwt.return n
       | _ ->
         Lwt.fail_with
           (Printf.sprintf
              "datatype mismatch: INTEGER PRIMARY KEY column '%s' requires an integer"
              (List.nth table_meta.Cat.columns i).Row.name))
  in
  let* () =
    if Int64.equal new_rowid old_rowid
    then Lwt.return_unit
    else (
      let upd_tree_id, _, _, _ = Cat.row_storage table_meta in
      let* existing = S.get tx upd_tree_id (Rowid.encode new_rowid) in
      match existing with
      | None -> Lwt.return_unit
      | Some _ ->
        let col_name =
          match alias_col with
          | Some i -> (List.nth table_meta.Cat.columns i).Row.name
          | None -> "rowid"
        in
        Lwt.fail_with
          (Printf.sprintf "UNIQUE constraint failed: %s.%s" table_meta.Cat.name col_name))
  in
  let schema = table_meta.Cat.columns in
  let old_row_for_idx = with_computed_virtuals clock params table_meta old_row in
  let new_row_for_idx = with_computed_virtuals clock params table_meta new_row in
  let* () =
    Lwt_list.iter_s
      (fun (idx : Cat.index_info) ->
         let old_matches =
           row_matches_index_where clock params idx schema old_row_for_idx
         in
         let new_matches =
           row_matches_index_where clock params idx schema new_row_for_idx
         in
         let old_iks =
           List.map
             row_value_to_index_value
             (get_index_key_values clock params idx schema old_row_for_idx)
         in
         let new_iks =
           List.map
             row_value_to_index_value
             (get_index_key_values clock params idx schema new_row_for_idx)
         in
         let old_ikey = Index_key.encode old_iks ~rowid:old_rowid in
         let new_ikey = Index_key.encode new_iks ~rowid:new_rowid in
         let* () =
           if old_matches then S.del tx idx.idx_tree_id old_ikey else Lwt.return_unit
         in
         if new_matches
         then S.put tx idx.idx_tree_id new_ikey Bytes.empty
         else Lwt.return_unit)
      indexes
  in
  let upd_tree_id2, _, _, _ = Cat.row_storage table_meta in
  let new_bytes = Row.encode schema new_row in
  let* () = S.del tx upd_tree_id2 (Rowid.encode old_rowid) in
  let* () = S.put tx upd_tree_id2 (Rowid.encode new_rowid) new_bytes in
  Lwt.return new_rowid
;;

(* UPSERT DO UPDATE: apply [assigns] to conflicting row [old_rowid], refresh
   indexes, fire BEFORE/AFTER UPDATE hooks, commit if we own the txn. *)
let execute_upsert_update
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~owned
      ~(row : Row.t)
      ~(assigns : (int * Plan.expr) list)
      ~old_rowid
      ~on_upsert_update_before
      ~on_upsert_update
  : bool Lwt.t
  =
  let old_key = Rowid.encode old_rowid in
  let* old_bytes_opt =
    S.get
      tx
      (let x, _, _, _ = Cat.row_storage table_meta in
       x)
      old_key
  in
  match old_bytes_opt with
  | None ->
    let* () = if owned then S.rollback tx else Lwt.return_unit in
    Lwt.return false
  | Some old_bytes ->
    let old_row = decode_with_virtual clock params table_meta old_bytes in
    let new_row = Array.copy old_row in
    List.iter
      (fun (col_ord, expr) ->
         let e' = substitute_excluded row expr in
         new_row.(col_ord) <- eval_expr clock params old_row e')
      assigns;
    compute_stored_generated_cols clock params table_meta new_row;
    eval_check_constraints clock params table_meta new_row;
    let* () =
      match on_upsert_update_before with
      | None -> Lwt.return_unit
      | Some f -> f ~tx ~old_row ~new_row
    in
    (* #249: SET id = N in a DO UPDATE must move the row (and check uniqueness),
       same as a plain UPDATE — funnel through the shared re-key helper. *)
    let* (_ : int64) =
      write_row_rekeyed
        tx
        table_meta
        ~clock
        ~params
        ~old_row
        ~new_row
        ~old_rowid
        ~indexes:(Cat.indexes_for_table cat ~table:table_meta.name)
    in
    let* () =
      match on_upsert_update with
      | None -> Lwt.return_unit
      | Some f -> f ~tx ~old_row ~new_row
    in
    (* #417: secondary-index UPSERT DO UPDATE writes via [write_row_rekeyed],
       bypassing the marked plain-insert/update loops; record the pre/post images
       here so the delta feed covers it. *)
    record_change table_meta.Cat.name (Updated { rowid = old_rowid; old_row; new_row });
    let* () = release_txn ~cat tx owned in
    Lwt.return true
;;

(* Plain INSERT path (no UPSERT match from secondary indexes): honor IGNORE
   (skip), delete REPLACE conflicts, write the new row + index entries using
   [S.put_x] (combined check+write) when [alias_explicit=true] to avoid a
   separate pre-read for the alias PK uniqueness check (#350). *)
let execute_insert_write
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~owned
      ~(row : Row.t)
      ~(row_for_idx : Row.t)
      ~rowid
      ~(idxs : Cat.index_info list)
      ~skip
      ~to_delete
      ~alias_explicit
      ~alias_col_name
      ~(on_conflict : Ast.conflict_action option)
      ~(upsert_update : (string list * (int * Plan.expr) list) option)
      ~on_replace_delete_before
      ~on_replace_delete
      ~on_upsert_update_before
      ~on_upsert_update
      ~after_hook
  : bool Lwt.t
  =
  if skip
  then
    (* IGNORE from secondary-index pre-check: rollback if owned.
       [on_conflict = CA_ignore] means [check_insert_unique] set [skip=true]
       and never populated [to_delete], so the else-branch (including
       [delete_replace_conflicts]) is unreachable — no hooks have fired and no
       B-tree deletes have been made, so [S.rollback] is safe. *)
    let* () = if owned then S.rollback tx else Lwt.return_unit in
    Lwt.return false
  else
    let* displaced_rows =
      delete_replace_conflicts
        tx
        table_meta
        ~clock
        ~params
        ~idxs
        ~on_replace_delete_before
        to_delete
    in
    let key = Rowid.encode rowid in
    let bytes = Row.encode table_meta.columns row in
    (* #350: use put_x for explicit alias PK rows to combine the uniqueness
       check with the write in a single B-tree descent (1 descent on the
       no-conflict path; 2 for CA_replace since put_x does not write on
       conflict and a follow-up S.put is needed).  For all other cases (no
       alias col, auto-allocated rowid) just S.put — no alias PK conflict
       is possible. *)
    let* conflict_opt =
      if alias_explicit
      then
        S.put_x
          tx
          (let x, _, _, _ = Cat.row_storage table_meta in
           x)
          key
          bytes
      else
        let* () =
          S.put
            tx
            (let x, _, _, _ = Cat.row_storage table_meta in
             x)
            key
            bytes
        in
        Lwt.return None
    in
    match conflict_opt with
    | None ->
      (* Common path: key was absent, write succeeded. *)
      let* () =
        insert_row_indexes tx table_meta ~clock ~params ~row_for_idx ~rowid idxs
      in
      let* () =
        match on_replace_delete with
        | None -> Lwt.return_unit
        | Some f -> Lwt_list.iter_s (fun old_row -> f ~tx ~old_row) displaced_rows
      in
      let* () =
        match after_hook with
        | None -> Lwt.return_unit
        | Some f -> f ~tx ~new_row:row
      in
      let* () = release_txn ~cat tx owned in
      Lwt.return true
    | Some _ ->
      (* Alias PK conflict detected by put_x (key present, NOT overwritten).
         put_x returns a sentinel [Some Bytes.empty] — callers that need the
         old row bytes (CA_replace) fetch them via S.get below. *)
      let col_name = Option.value alias_col_name ~default:"rowid" in
      (match on_conflict, upsert_update with
       | Some Ast.CA_ignore, _ ->
         let* () = if owned then S.rollback tx else Lwt.return_unit in
         Lwt.return false
       | Some Ast.CA_replace, _ ->
         let* old_bytes_opt =
           S.get
             tx
             (let x, _, _, _ = Cat.row_storage table_meta in
              x)
             key
         in
         let old_row =
           match old_bytes_opt with
           | Some b -> decode_with_virtual clock params table_meta b
           | None -> failwith "put_x conflict but row gone before CA_replace fetch"
         in
         (* Fire BEFORE DELETE for the alias-PK displaced row.  Note: when
            there are simultaneous secondary-index REPLACE conflicts, those
            hooks fired first (inside delete_replace_conflicts above), so the
            alias-PK BEFORE DELETE fires last. *)
         let* () =
           match on_replace_delete_before with
           | None -> Lwt.return_unit
           | Some f -> f ~tx ~old_row
         in
         (* Delete the alias-PK row's index entries.
            old_rowid = rowid by alias-PK invariant: the conflict is on this
            same key, so the displaced row's rowid equals the inserted rowid.
            old_row from decode_with_virtual already has VIRTUAL cols applied. *)
         let* () =
           delete_row_indexes
             tx
             table_meta
             ~clock
             ~params
             ~row_for_idx:old_row
             ~rowid
             idxs
         in
         let* () =
           S.put
             tx
             (let x, _, _, _ = Cat.row_storage table_meta in
              x)
             key
             bytes
         in
         let* () =
           insert_row_indexes tx table_meta ~clock ~params ~row_for_idx ~rowid idxs
         in
         (* Fire AFTER DELETE for all displaced rows.  Use [displaced_rows @
            [old_row]] so alias-PK row fires last — matching the BEFORE DELETE
            order (secondary conflicts first, alias-PK last). *)
         let all_displaced = displaced_rows @ [ old_row ] in
         let* () =
           match on_replace_delete with
           | None -> Lwt.return_unit
           | Some f -> Lwt_list.iter_s (fun r -> f ~tx ~old_row:r) all_displaced
         in
         let* () =
           match after_hook with
           | None -> Lwt.return_unit
           | Some f -> f ~tx ~new_row:row
         in
         let* () = release_txn ~cat tx owned in
         Lwt.return true
       | _, Some (conflict_cols, assigns) when conflict_cols = [ col_name ] ->
         (* Alias PK is always a single column, so single-element equality
            suffices — no sort needed.  Fire AFTER DELETE for any secondary
            REPLACE displaced rows before handing off to the upsert path. *)
         let* () =
           match on_replace_delete with
           | None -> Lwt.return_unit
           | Some f -> Lwt_list.iter_s (fun r -> f ~tx ~old_row:r) displaced_rows
         in
         execute_upsert_update
           tx
           cat
           table_meta
           ~clock
           ~params
           ~owned
           ~row
           ~assigns
           ~old_rowid:rowid
           ~on_upsert_update_before
           ~on_upsert_update
       | _ ->
         Lwt.fail_with
           (Printf.sprintf "UNIQUE constraint failed: %s.%s" table_meta.Cat.name col_name))
;;

(* Build the row to insert: use [prebuilt_row] if given, else evaluate each
   (ordinal, expr) into a fresh NULL-filled row of the table's width. *)
let build_insert_row
      ~clock
      ~params
      ~prebuilt_row
      ~ordinals
      ~values
      (table_meta : Cat.table_meta)
  : Row.t
  =
  match prebuilt_row with
  | Some r -> r
  | None ->
    let n = List.length table_meta.columns in
    let r = Array.make n Row.V_null in
    List.iter2
      (fun ord expr -> r.(ord) <- eval_expr clock params [||] expr)
      ordinals
      values;
    r
;;

(** Run [Op_insert] against the store: write the new row to the table
    tree and, if any indexes are defined on the table, also write the
    corresponding index entries (checking UNIQUE constraints first).
    Uses a SINGLE RW txn for both the row write and index writes. *)
let execute_insert
      ?(mode = Auto)
      ?(params = [||])
      ?(clock : (unit -> float) option = None)
      ?(on_conflict : Ast.conflict_action option = None)
      ?(upsert_update : (string list * (int * Plan.expr) list) option = None)
      ?(prebuilt_row : Row.t option = None)
      ?(before_hook : (tx:S.rw S.txn -> new_row:Row.t -> unit Lwt.t) option = None)
      ?(after_hook : (tx:S.rw S.txn -> new_row:Row.t -> unit Lwt.t) option = None)
      ?(on_replace_delete_before : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option =
        None)
      ?(on_replace_delete : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option = None)
      ?(on_upsert_update_before :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      ?(on_upsert_update :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      (store : S.t)
      (cat : Cat.t)
      ~(table_meta : Cat.table_meta)
      ~ordinals
      ~(values : Plan.expr list)
  : bool Lwt.t
  =
  let row = build_insert_row ~clock ~params ~prebuilt_row ~ordinals ~values table_meta in
  compute_stored_generated_cols clock params table_meta row;
  (* Evaluate CHECK and FK constraints before any writes. *)
  eval_check_constraints clock params table_meta row;
  let* () = enforce_insert_fks store cat table_meta row in
  (* When an explicit transaction is already held, we must NOT call
     Cat.next_rowid (which opens its own RW txn and deadlocks on the mutex):
     acquire/reuse the txn first, then allocate the rowid within it.  BEFORE
     INSERT fires inside the parent txn so its nested DML shares the tx and
     its writes roll back atomically with the parent on failure. *)
  let* tx, owned = acquire_txn store mode in
  Lwt.catch
    (fun () ->
       let* () =
         match before_hook with
         | None -> Lwt.return_unit
         | Some f -> f ~tx ~new_row:(Array.copy row)
       in
       (* #243 (T1): capture whether an EXPLICIT integer id was supplied for the
          rowid-alias column BEFORE [insert_rowid] writes back an auto value. *)
       let alias_idx = Cat.rowid_alias_col table_meta in
       let alias_explicit =
         match alias_idx with
         | Some i ->
           (match row.(i) with
            | Row.V_int _ -> true
            | _ -> false)
         | None -> false
       in
       (* #347: defer the per-row counter B-tree write when inside an explicit txn;
          [flush_dirty_counters_tx] writes it once at COMMIT instead. *)
       let* rowid = insert_rowid ~defer_counter:(not owned) tx cat table_meta row in
       let idxs = Cat.indexes_for_table cat ~table:table_meta.name in
       (* Phase 35 Task 2: compute VIRTUAL generated columns into a scratch row
         before extracting index keys so VIRTUAL cells contribute their value. *)
       let row_for_idx = with_computed_virtuals clock params table_meta row in
       let* skip, to_delete, upsert_rowid =
         check_insert_unique
           tx
           table_meta
           ~clock
           ~params
           ~row_for_idx
           ~on_conflict
           ~upsert_update
           idxs
       in
       (* #243 (T1): alias PK conflict detection is now folded into put_x
          inside execute_insert_write (#350) — no pre-read needed. *)
       let alias_col_name =
         match alias_idx with
         | Some i when alias_explicit -> Some (List.nth table_meta.columns i).Row.name
         | _ -> None
       in
       match upsert_update, upsert_rowid with
       | Some (_, assigns), Some old_rowid ->
         (* Secondary-index upsert conflict: update the conflicting row.
            [execute_upsert_update] writes via [write_row_rekeyed] (raw put/del),
            bypassing the marked normal-insert path, so mark here when it actually
            updated the row. *)
         let* updated =
           execute_upsert_update
             tx
             cat
             table_meta
             ~clock
             ~params
             ~owned
             ~row
             ~assigns
             ~old_rowid
             ~on_upsert_update_before
             ~on_upsert_update
         in
         if updated then mark_dirty table_meta.Cat.name;
         Lwt.return updated
       | _ ->
         let* inserted =
           execute_insert_write
             tx
             cat
             table_meta
             ~clock
             ~params
             ~owned
             ~row
             ~row_for_idx
             ~rowid
             ~idxs
             ~skip
             ~to_delete
             ~alias_explicit
             ~alias_col_name
             ~on_conflict
             ~upsert_update
             ~on_replace_delete_before
             ~on_replace_delete
             ~on_upsert_update_before
             ~on_upsert_update
             ~after_hook
         in
         (* #243 (T1): record the rowid actually written so [last_insert_rowid()]
            is correct even when an explicit id differs from [next_rowid - 1].
            Skipped inserts (ON CONFLICT IGNORE ⇒ [inserted=false]) leave it. *)
         if inserted then Cat.set_last_inserted_rowid cat rowid;
         if inserted
         then (
           mark_dirty table_meta.Cat.name;
           record_change table_meta.Cat.name (Inserted { rowid; row }));
         Lwt.return inserted)
    (fun exn ->
       (* On any exception: rollback if we own the txn, then re-raise. *)
       let* () = if owned then S.rollback tx else Lwt.return_unit in
       Lwt.fail exn)
;;

(** Run [Op_create_index]: register the index in the catalog, then scan
    the table tree and populate the index tree with one entry per row. *)
let execute_create_index
      ?(mode = Auto)
      (store : S.t)
      (cat : Cat.t)
      ~name
      ~table
      ~tree_id
      ~col_sqls
      ~col_expr_flags
      ~(where_expr : Plan.expr option)
      ~where_sql
      ~unique
      ~(columns : Row.column list)
  : unit Lwt.t
  =
  (* #269: register the index in the catalog AND populate the index tree through
     ONE writer txn.  Previously [Cat.create_index] opened (and committed) its
     own txn before this scan acquired another — which self-deadlocks when an
     explicit transaction already holds the writer lock.  [with_ddl_txn] threads
     a single txn through both so the whole CREATE INDEX participates in (and
     rolls back with) any ambient explicit transaction. *)
  with_ddl_txn store cat mode (fun tx ->
    let* res =
      Cat.create_index
        ~txn:tx
        cat
        ~name
        ~table
        ~columns:col_sqls
        ~unique
        ~expr_flags:col_expr_flags
        ~where_sql
        ~origin:`User
    in
    match res with
    | Error msg -> failwith msg
    | Ok info ->
      let* cur = S.cursor_open tx tree_id in
      let _sr = S.cursor_first cur in
      let rec walk () =
        match S.cursor_next cur with
        | None -> Lwt.return_unit
        | Some (kbytes, vbytes) ->
          let rowid = Rowid.decode kbytes in
          let row = decode_with_virtual_cols None [||] ~table_name:table columns vbytes in
          let skip =
            match where_expr with
            | None -> false
            | Some we -> not (value_truthy (eval_expr None [||] row we))
          in
          if skip
          then walk ()
          else (
            let key_vals = get_index_key_values None [||] info columns row in
            let iks = List.map row_value_to_index_value key_vals in
            let ikey = Index_key.encode iks ~rowid in
            (* #288: for a UNIQUE index, the build must detect pre-existing
               duplicate values.  The encoded key includes the rowid suffix, so
               two rows sharing the indexed value produce DISTINCT keys and never
               collide in the tree — uniqueness would otherwise only be enforced
               at INSERT time, letting pre-existing duplicates slip through.
               Probe the partially-built index for an entry already carrying this
               value prefix (rowid excluded) using the SAME mechanism as
               [check_insert_unique], so build-time and insert-time uniqueness
               agree (including multi-column, partial-WHERE and NULL handling).
               #290: a key with ANY NULL column is exempt from the conflict
               probe (NULLs are distinct in SQLite) — but is STILL inserted into
               the index tree below, exactly as [check_insert_unique] does.
               A raise here unwinds through [with_ddl_txn]: an owned txn rolls
               back (no partial entries), a borrowed one is poisoned (#286).
               The raise skips the outer [S.cursor_close cur] below, but
               [cursor_close] is a no-op (no OS handle) and the txn unwind
               reclaims all store state — so no leak. *)
            let* () =
              if (not unique) || any_null_val key_vals
              then Lwt.return_unit
              else (
                let prefix, plen = encode_index_key_prefix iks in
                let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
                let* probe = S.seek_ge tx info.idx_tree_id seek_key in
                let* first = S.seek_next probe in
                S.seek_close probe;
                match first with
                | Some (existing, _)
                  when Bytes.length existing >= plen
                       && Bytes.equal (Bytes.sub existing 0 plen) prefix ->
                  Lwt.fail_with
                    (unique_constraint_failed_msg ~table ~columns:info.idx_columns)
                | _ -> Lwt.return_unit)
            in
            let* () = S.put tx info.idx_tree_id ikey Bytes.empty in
            walk ())
      in
      let* () = walk () in
      S.cursor_close cur;
      Lwt.return_unit)
;;

(** Check whether inserting a new index entry for [new_row] with
    [rowid] into [idx] would violate a UNIQUE constraint.  Returns
    [true] if a different row already has the same indexed value. *)
let unique_violation_on_update
      (tx : S.rw S.txn)
      (idx : Cat.index_info)
      (_new_values : Row.value list)
        (* kept for call-site compat but unused for expr indexes *)
      ~(rowid : int64)
      ~(new_row : Row.t)
      ~(schema : Row.column list)
  : bool Lwt.t
  =
  (* For UNIQUE check we use the first value as the seek prefix.
     This is a conservative approach: we seek to the first key with the
     matching first-column value, then compare the entire encoded key.
     Phase 35 Task 2: populate VIRTUAL gen cols on the new row so the
     UNIQUE comparison keys reflect their computed value. *)
  let new_row_for_idx =
    with_computed_virtuals_cols None [||] ~table_name:idx.Cat.idx_table schema new_row
  in
  let key_vals = get_index_key_values None [||] idx schema new_row_for_idx in
  (* #290: a key with ANY NULL column is exempt — NULLs are distinct in a SQLite
     UNIQUE index, so it can never collide.  Short-circuit before probing. *)
  if any_null_val key_vals
  then Lwt.return false
  else (
    let ik_values = List.map row_value_to_index_value key_vals in
    (* Encode all values (no rowid) as the exact-match key; [encode_index_key_prefix]
     concatenates each value's encoding in order, same as the index key body. *)
    let full_key_no_rowid, full_klen = encode_index_key_prefix ik_values in
    let prefix =
      match ik_values with
      | [] -> Bytes.empty
      | ik :: _ -> Index_key.encode_value ik
    in
    let plen = Bytes.length prefix in
    let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
    (* O(log n) native seek; scan only the matching prefix range (#229). *)
    let* cur = S.seek_ge tx idx.idx_tree_id seek_key in
    (* Scan entries while the value prefix matches.  A different rowid
     with the same full value sequence is a UNIQUE violation. *)
    let rec scan () =
      match%lwt S.seek_next cur with
      | None -> Lwt.return false
      | Some (ikey, _) ->
        if Bytes.length ikey >= plen + 8 && Bytes.equal (Bytes.sub ikey 0 plen) prefix
        then
          (* Check that the full value prefix (all columns) also matches *)
          if
            Bytes.length ikey >= full_klen + 8
            && Bytes.equal (Bytes.sub ikey 0 full_klen) full_key_no_rowid
          then (
            let rowid_bytes = Bytes.sub ikey (Bytes.length ikey - 8) 8 in
            let other = Rowid.decode rowid_bytes in
            if Int64.equal other rowid then scan () else Lwt.return true)
          else scan ()
        else Lwt.return false
    in
    let* result = scan () in
    S.seek_close cur;
    Lwt.return result)
;;

(** Build the list of (child_table_meta, relevant_fk_constraints) pairs
    for tables that have FK constraints pointing to [parent_table_name]. *)
let build_child_refs cat ~parent_table_name =
  let* all_tables = Cat.list_tables cat in
  Lwt.return
    (List.filter_map
       (fun (child_meta : Cat.table_meta) ->
          let fks =
            List.filter
              (fun (fk : Cat.fk_constraint) ->
                 String.equal fk.fk_parent_table parent_table_name)
              child_meta.Cat.fk_constraints
          in
          if fks = [] then None else Some (child_meta, fks))
       all_tables)
;;

(** Scan [child_meta] using an existing RW transaction for rows where all
    [child_col_idxs] match [parent_vals] simultaneously.  When an index covers
    [child_col_idxs] as a leading prefix, the scan is driven by the index;
    otherwise it falls back to a full table scan.
    Returns (rowid, row) list. *)
let scan_child_rows_multi_tx
      (cat : Cat.t)
      tx
      (child_meta : Cat.table_meta)
      ~(child_col_idxs : int list)
      ~(parent_vals : Row.value list)
  =
  match
    Cat.find_index_covering_cols
      cat
      ~table_name:child_meta.Cat.name
      ~col_idxs:child_col_idxs
  with
  | Some idx when not (List.exists (fun v -> v = Row.V_null) parent_vals) ->
    let ivs = List.map row_value_to_index_value parent_vals in
    let prefix, plen = encode_index_key_prefix ivs in
    let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
    (* O(log n) native seek; scan only the matching prefix range (#228/#229). *)
    let* cur = S.seek_ge tx idx.Cat.idx_tree_id seek_key in
    let buf = ref [] in
    let exhausted = ref false in
    let rec walk () =
      if !exhausted
      then Lwt.return_unit
      else (
        match%lwt S.seek_next cur with
        | None ->
          exhausted := true;
          Lwt.return_unit
        | Some (ikey, _ival) ->
          if Bytes.length ikey >= plen + 8 && Bytes.equal (Bytes.sub ikey 0 plen) prefix
          then (
            let rowid = decode_index_key_rowid ikey in
            let child_tree_id_fk, _, _, _ = Cat.row_storage child_meta in
            let* row_opt = S.get tx child_tree_id_fk (Rowid.encode rowid) in
            match row_opt with
            | None -> walk ()
            | Some vbytes ->
              let row = decode_with_virtual None [||] child_meta vbytes in
              let all_match =
                List.for_all2
                  (fun ci pv -> compare_values row.(ci) pv = 0)
                  child_col_idxs
                  parent_vals
              in
              if all_match then buf := (rowid, row) :: !buf;
              walk ())
          else (
            exhausted := true;
            Lwt.return_unit))
    in
    let* () = walk () in
    S.seek_close cur;
    Lwt.return (List.rev !buf)
  | _ ->
    full_scan_collect tx child_meta (fun row ->
      List.for_all2
        (fun ci pv -> compare_values row.(ci) pv = 0)
        child_col_idxs
        parent_vals)
;;

(** Delete a single row and its index entries within an existing RW transaction. *)
let delete_row_in_tx tx (cat : Cat.t) (meta : Cat.table_meta) ~rowid ~(row : Row.t) =
  mark_dirty meta.Cat.name;
  (* #417: this primitive fires for FK-cascade child deletes (the top-level
     delete records via [apply_delete_row]'s loop), so the removed child row
     reaches the delta feed. *)
  record_change meta.Cat.name (Deleted { rowid; row });
  let rowid_key = Rowid.encode rowid in
  let child_idxs = Cat.indexes_for_table cat ~table:meta.Cat.name in
  (* Phase 35 Task 2: ensure VIRTUAL gen cols are populated before key extraction. *)
  let row_for_idx = with_computed_virtuals None [||] meta row in
  let* () =
    Lwt_list.iter_s
      (fun (idx : Cat.index_info) ->
         if not (row_matches_index_where None [||] idx meta.Cat.columns row_for_idx)
         then Lwt.return_unit
         else (
           let iks =
             List.map
               row_value_to_index_value
               (get_index_key_values None [||] idx meta.Cat.columns row_for_idx)
           in
           let old_ikey = Index_key.encode iks ~rowid in
           S.del tx idx.idx_tree_id old_ikey))
      child_idxs
  in
  let del_tree_id, _, _, _ = Cat.row_storage meta in
  let* () = S.del tx del_tree_id rowid_key in
  (* #409: reuse a plain rowid table's high-water after a committed delete. *)
  Cat.note_rowid_deleted cat ~name:meta.Cat.name ~rowid tx
;;

(** Update one column to [new_val] in a row within an existing RW transaction.
    Also updates index entries for any index that covers [col_idx]. *)
let update_col_in_tx
      tx
      (cat : Cat.t)
      (meta : Cat.table_meta)
      ~rowid
      ~(row : Row.t)
      ~col_idx
      ~new_val
  =
  mark_dirty meta.Cat.name;
  let new_row = Array.copy row in
  new_row.(col_idx) <- new_val;
  compute_stored_generated_cols None [||] meta new_row;
  (* #249: a cascade that lands on the child's own INTEGER PRIMARY KEY column
     must MOVE the child row (re-key + reindex), like any other alias-column
     UPDATE — funnel through the shared helper. *)
  let* new_rowid =
    write_row_rekeyed
      tx
      meta
      ~clock:None
      ~params:[||]
      ~old_row:row
      ~new_row
      ~old_rowid:rowid
      ~indexes:(Cat.indexes_for_table cat ~table:meta.Cat.name)
  in
  (* #417: record the FK-cascade child column update (ON UPDATE CASCADE / SET
     NULL / SET DEFAULT); a cascade that re-keyed the child's own INTEGER PK is
     recorded as Deleted+Inserted, like any rowid-changing UPDATE. *)
  record_update meta.Cat.name ~old_rowid:rowid ~new_rowid ~old_row:row ~new_row;
  Lwt.return_unit
;;

(* Build the commit-time recheck for a deferred FK violation: it still stands
   iff a child row references [parent_vals] AND no parent row has them.
   Re-resolves column indices against the current schema. *)
let make_fk_recheck
      (cat : Cat.t)
      ~child_name
      ~parent_name
      ~child_cols
      ~parent_cols
      ~parent_vals
  : Cat.pending_fk_recheck
  =
  { Cat.recheck =
      (fun (type m) (recheck_tx : m S.txn) ->
        match
          ( Cat.find_table_cached cat ~name:child_name
          , Cat.find_table_cached cat ~name:parent_name )
        with
        | None, _ | _, None -> Lwt.return false
        | Some child_now, Some parent_now ->
          let cci =
            List.filter_map (find_col_idx_by_name_opt child_now.Cat.columns) child_cols
          in
          let pci =
            List.filter_map (find_col_idx_by_name_opt parent_now.Cat.columns) parent_cols
          in
          if
            List.length cci <> List.length child_cols
            || List.length pci <> List.length parent_cols
          then Lwt.return false
          else
            let* has_child =
              fk_child_has_ref_multi_in_tx
                cat
                recheck_tx
                child_now
                ~child_col_idxs:cci
                ~parent_vals
            in
            if not has_child
            then Lwt.return false
            else
              let* has_parent =
                fk_parent_has_row_in_tx
                  recheck_tx
                  parent_now
                  ~parent_idxs:pci
                  ~parent_vals
              in
              Lwt.return (not has_parent))
  }
;;

(* The DEFAULT value for [col] as a Row.value, resolving CURRENT_* sentinels
   via the clock.  Shared by the ON DELETE / ON UPDATE SET DEFAULT cascades. *)
let fk_default_value clock params (col : Row.column) : Row.value =
  match col.Row.default with
  | None -> Row.V_null
  | Some (Row.DV_int n) -> Row.V_int n
  | Some (Row.DV_text s) -> Row.V_text s
  | Some (Row.DV_real f) -> Row.V_real f
  | Some (Row.DV_blob b) -> Row.V_blob b
  | Some Row.DV_null -> Row.V_null
  | Some Row.DV_current_timestamp ->
    eval_expr
      clock
      params
      [||]
      (Plan.P_func (Ast.Fn_datetime, [ Plan.P_lit (Ast.L_text "now") ]))
  | Some Row.DV_current_date ->
    eval_expr
      clock
      params
      [||]
      (Plan.P_func (Ast.Fn_date, [ Plan.P_lit (Ast.L_text "now") ]))
  | Some Row.DV_current_time ->
    eval_expr
      clock
      params
      [||]
      (Plan.P_func (Ast.Fn_time, [ Plan.P_lit (Ast.L_text "now") ]))
;;

(** Recursively delete a row and cascade FK actions to child tables.
    Only runs cascade logic when FK enforcement is enabled in [cat]. *)
let rec cascade_delete_row_in_tx
          tx
          (cat : Cat.t)
          ?(visited : (string * int64, unit) Hashtbl.t = Hashtbl.create 16)
          (clock : (unit -> float) option)
          (params : Row.value array)
          (meta : Cat.table_meta)
          ~rowid
          ~(row : Row.t)
  =
  let visited_key = meta.Cat.name, rowid in
  if Hashtbl.mem visited visited_key
  then Lwt.return_unit
  else (
    Hashtbl.add visited visited_key ();
    let* child_refs =
      if Cat.get_fk_enforcement cat
      then build_child_refs cat ~parent_table_name:meta.Cat.name
      else Lwt.return []
    in
    let* () =
      Lwt_list.iter_s
        (fun (child_meta, fks) ->
           Lwt_list.iter_s
             (fun (fk : Cat.fk_constraint) ->
                cascade_delete_fk
                  tx
                  cat
                  visited
                  clock
                  params
                  meta
                  ~rowid
                  ~row
                  child_meta
                  fk)
             fks)
        child_refs
    in
    delete_row_in_tx tx cat meta ~rowid ~row)

(* Apply the ON DELETE action of one [fk] (child_meta references meta) while
   deleting [row] of [meta] at [rowid]. *)
and cascade_delete_fk
      tx
      cat
      visited
      clock
      params
      (meta : Cat.table_meta)
      ~rowid
      ~(row : Row.t)
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
  =
  let parent_col_idxs_opt =
    List.map (find_col_idx_by_name_opt meta.Cat.columns) fk.Cat.fk_parent_cols
  in
  if List.exists Option.is_none parent_col_idxs_opt
  then Lwt.return_unit
  else (
    let parent_col_idxs = List.filter_map Fun.id parent_col_idxs_opt in
    let parent_vals = List.map (fun i -> row.(i)) parent_col_idxs in
    if any_null_val parent_vals
    then Lwt.return_unit
    else (
      let child_col_idxs_opt =
        List.map (find_col_idx_by_name_opt child_meta.Cat.columns) fk.Cat.fk_local_cols
      in
      if List.exists Option.is_none child_col_idxs_opt
      then Lwt.return_unit
      else (
        let child_col_idxs = List.filter_map Fun.id child_col_idxs_opt in
        match fk.Cat.fk_on_delete with
        | Cat.FA_restrict | Cat.FA_no_action ->
          cascade_delete_restrict
            cat
            tx
            meta
            child_meta
            fk
            ~parent_vals
            ~child_col_idxs
            ~rowid
        | Cat.FA_cascade ->
          let* child_rows =
            scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
          in
          Lwt_list.iter_s
            (fun (crid, crow) ->
               cascade_delete_row_in_tx
                 tx
                 cat
                 ~visited
                 clock
                 params
                 child_meta
                 ~rowid:crid
                 ~row:crow)
            child_rows
        | Cat.FA_set_null ->
          cascade_delete_set_null
            tx
            cat
            visited
            clock
            params
            child_meta
            ~child_col_idxs
            ~parent_vals
        | Cat.FA_set_default ->
          cascade_delete_set_default
            tx
            cat
            visited
            clock
            params
            child_meta
            ~child_col_idxs
            ~parent_vals)))

(* ON DELETE RESTRICT/NO ACTION: if any child row still references the parent,
   queue a deferred recheck or raise immediately. *)
and cascade_delete_restrict
      cat
      tx
      (meta : Cat.table_meta)
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
      ~(parent_vals : Row.value list)
      ~child_col_idxs
      ~rowid
  =
  let is_deferred = fk.Cat.fk_deferrable || Cat.get_defer_fks_pragma cat in
  let* child_rows =
    scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
  in
  if child_rows <> []
  then (
    let msg =
      Printf.sprintf
        "FOREIGN KEY constraint failed: '%s.%s' is still referenced by '%s.%s'"
        meta.Cat.name
        (String.concat "," fk.Cat.fk_parent_cols)
        child_meta.Cat.name
        (String.concat "," fk.Cat.fk_local_cols)
    in
    let parent_meta_name = meta.Cat.name in
    let child_meta_name = child_meta.Cat.name in
    let parent_cols_copy = fk.Cat.fk_parent_cols in
    let child_cols_copy = fk.Cat.fk_local_cols in
    let recheck =
      { Cat.recheck =
          (fun (type m) (recheck_tx : m S.txn) ->
            match
              ( Cat.find_table_cached cat ~name:child_meta_name
              , Cat.find_table_cached cat ~name:parent_meta_name )
            with
            | None, _ | _, None -> Lwt.return false
            | Some child_now, Some parent_now ->
              let cci =
                List.filter_map
                  (find_col_idx_by_name_opt child_now.Cat.columns)
                  child_cols_copy
              in
              let pci =
                List.filter_map
                  (find_col_idx_by_name_opt parent_now.Cat.columns)
                  parent_cols_copy
              in
              if
                List.length cci <> List.length child_cols_copy
                || List.length pci <> List.length parent_cols_copy
              then Lwt.return false
              else
                let* has_child =
                  fk_child_has_ref_multi_in_tx
                    cat
                    recheck_tx
                    child_now
                    ~child_col_idxs:cci
                    ~parent_vals
                in
                if not has_child
                then Lwt.return false
                else
                  let* has_parent =
                    fk_parent_has_row_in_tx
                      recheck_tx
                      parent_now
                      ~parent_idxs:pci
                      ~parent_vals
                  in
                  Lwt.return (not has_parent))
      }
    in
    fk_violation
      ~deferred:is_deferred
      cat
      ~kind:`Delete
      ~table:parent_meta_name
      ~rowid
      ~msg
      ~recheck)
  else Lwt.return_unit

(* ON DELETE SET NULL: set each child FK column to NULL (rejecting NOT NULL),
   routing through cascade_update_col_in_tx so further ON UPDATE chains run. *)
and cascade_delete_set_null
      tx
      cat
      visited
      clock
      params
      (child_meta : Cat.table_meta)
      ~child_col_idxs
      ~(parent_vals : Row.value list)
  =
  let* child_rows =
    scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
  in
  if child_rows = []
  then Lwt.return_unit
  else
    (* For single-col FKs (common case), apply to the one child col.
       For multi-col, apply SET NULL to each child col independently. *)
    Lwt_list.iter_s
      (fun child_col_idx ->
         let col = List.nth child_meta.Cat.columns child_col_idx in
         if col.Row.not_null
         then
           Lwt.fail_with
             (Printf.sprintf
                "FOREIGN KEY constraint failed: ON DELETE SET NULL on NOT NULL column \
                 '%s.%s'"
                child_meta.Cat.name
                col.Row.name)
         else
           Lwt_list.iter_s
             (fun (crid, crow) ->
                cascade_update_col_in_tx
                  tx
                  cat
                  ~visited
                  clock
                  params
                  child_meta
                  ~rowid:crid
                  ~row:crow
                  ~col_idx:child_col_idx
                  ~new_val:Row.V_null)
             child_rows)
      child_col_idxs

(* ON DELETE SET DEFAULT: like SET NULL but with each column's DEFAULT value. *)
and cascade_delete_set_default
      tx
      cat
      visited
      clock
      params
      (child_meta : Cat.table_meta)
      ~child_col_idxs
      ~(parent_vals : Row.value list)
  =
  let* child_rows =
    scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
  in
  if child_rows = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun child_col_idx ->
         let col = List.nth child_meta.Cat.columns child_col_idx in
         let default_val = fk_default_value clock params col in
         if col.Row.not_null && default_val = Row.V_null
         then
           Lwt.fail_with
             (Printf.sprintf
                "FOREIGN KEY constraint failed: ON DELETE SET DEFAULT on NOT NULL column \
                 '%s.%s' with no default"
                child_meta.Cat.name
                col.Row.name)
         else
           Lwt_list.iter_s
             (fun (crid, crow) ->
                cascade_update_col_in_tx
                  tx
                  cat
                  ~visited
                  clock
                  params
                  child_meta
                  ~rowid:crid
                  ~row:crow
                  ~col_idx:child_col_idx
                  ~new_val:default_val)
             child_rows)
      child_col_idxs

(** Recursively update a column and cascade FK UPDATE actions to child tables
    that reference this column. *)
and cascade_update_col_in_tx
      tx
      (cat : Cat.t)
      ?(visited : (string * int64, unit) Hashtbl.t = Hashtbl.create 16)
      (clock : (unit -> float) option)
      (params : Row.value array)
      (meta : Cat.table_meta)
      ~rowid
      ~(row : Row.t)
      ~col_idx
      ~new_val
  =
  let visited_key = meta.Cat.name, rowid in
  if Hashtbl.mem visited visited_key
  then Lwt.return_unit
  else (
    Hashtbl.add visited visited_key ();
    let* () = update_col_in_tx tx cat meta ~rowid ~row ~col_idx ~new_val in
    if not (Cat.get_fk_enforcement cat)
    then Lwt.return_unit
    else (
      let parent_col_name = (List.nth meta.Cat.columns col_idx).Row.name in
      let* all_child_refs = build_child_refs cat ~parent_table_name:meta.Cat.name in
      let col_child_refs =
        List.filter_map
          (fun (child_meta, fks) ->
             let matching_fks =
               List.filter
                 (fun (fk : Cat.fk_constraint) ->
                    List.mem parent_col_name fk.Cat.fk_parent_cols)
                 fks
             in
             if matching_fks = [] then None else Some (child_meta, matching_fks))
          all_child_refs
      in
      Lwt_list.iter_s
        (fun (child_meta, fks) ->
           Lwt_list.iter_s
             (fun (fk : Cat.fk_constraint) ->
                cascade_update_fk
                  tx
                  cat
                  visited
                  clock
                  params
                  meta
                  ~row
                  ~new_val
                  ~parent_col_name
                  child_meta
                  fk)
             fks)
        col_child_refs))

(* Apply the ON UPDATE action of one [fk] when [parent_col_name] of [meta]
   changes to [new_val]. *)
and cascade_update_fk
      tx
      cat
      visited
      clock
      params
      (meta : Cat.table_meta)
      ~(row : Row.t)
      ~new_val
      ~parent_col_name
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
  =
  (* Find the position of parent_col_name in fk_parent_cols to get the
     corresponding fk_local_cols entry for single-update cascade. *)
  let fk_pos =
    let rec find_pos i = function
      | [] -> 0
      | col :: _ when String.equal col parent_col_name -> i
      | _ :: rest -> find_pos (i + 1) rest
    in
    find_pos 0 fk.Cat.fk_parent_cols
  in
  let child_col_name = List.nth fk.Cat.fk_local_cols fk_pos in
  let child_col_idx = find_col_idx_by_name child_meta.Cat.columns child_col_name in
  (* For multi-col FKs, we need all parent_vals to scan child rows *)
  let all_parent_col_idxs =
    List.map (fun c -> find_col_idx_by_name meta.Cat.columns c) fk.Cat.fk_parent_cols
  in
  let all_parent_vals_old = List.map (fun i -> row.(i)) all_parent_col_idxs in
  match fk.Cat.fk_on_update with
  | Cat.FA_restrict | Cat.FA_no_action -> Lwt.return_unit
  | Cat.FA_cascade ->
    let all_child_col_idxs =
      List.map
        (fun c -> find_col_idx_by_name child_meta.Cat.columns c)
        fk.Cat.fk_local_cols
    in
    let* child_rows =
      scan_child_rows_multi_tx
        cat
        tx
        child_meta
        ~child_col_idxs:all_child_col_idxs
        ~parent_vals:all_parent_vals_old
    in
    Lwt_list.iter_s
      (fun (crid, crow) ->
         cascade_update_col_in_tx
           tx
           cat
           ~visited
           clock
           params
           child_meta
           ~rowid:crid
           ~row:crow
           ~col_idx:child_col_idx
           ~new_val)
      child_rows
  | Cat.FA_set_null ->
    cascade_update_set_null
      tx
      cat
      visited
      clock
      params
      child_meta
      fk
      ~child_col_idx
      ~child_col_name
      ~parent_vals_old:all_parent_vals_old
  | Cat.FA_set_default ->
    cascade_update_set_default
      tx
      cat
      visited
      clock
      params
      child_meta
      fk
      ~child_col_idx
      ~child_col_name
      ~parent_vals_old:all_parent_vals_old

(* ON UPDATE SET NULL for one fk's child column. *)
and cascade_update_set_null
      tx
      cat
      visited
      clock
      params
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
      ~child_col_idx
      ~child_col_name
      ~parent_vals_old
  =
  let col = List.nth child_meta.Cat.columns child_col_idx in
  if col.Row.not_null
  then
    Lwt.fail_with
      (Printf.sprintf
         "FOREIGN KEY constraint failed: ON UPDATE SET NULL on NOT NULL column '%s.%s'"
         child_meta.Cat.name
         child_col_name)
  else (
    let all_child_col_idxs =
      List.map
        (fun c -> find_col_idx_by_name child_meta.Cat.columns c)
        fk.Cat.fk_local_cols
    in
    let* child_rows =
      scan_child_rows_multi_tx
        cat
        tx
        child_meta
        ~child_col_idxs:all_child_col_idxs
        ~parent_vals:parent_vals_old
    in
    Lwt_list.iter_s
      (fun (crid, crow) ->
         cascade_update_col_in_tx
           tx
           cat
           ~visited
           clock
           params
           child_meta
           ~rowid:crid
           ~row:crow
           ~col_idx:child_col_idx
           ~new_val:Row.V_null)
      child_rows)

(* ON UPDATE SET DEFAULT for one fk's child column. *)
and cascade_update_set_default
      tx
      cat
      visited
      clock
      params
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
      ~child_col_idx
      ~child_col_name
      ~parent_vals_old
  =
  let all_child_col_idxs =
    List.map (fun c -> find_col_idx_by_name child_meta.Cat.columns c) fk.Cat.fk_local_cols
  in
  let* child_rows =
    scan_child_rows_multi_tx
      cat
      tx
      child_meta
      ~child_col_idxs:all_child_col_idxs
      ~parent_vals:parent_vals_old
  in
  if child_rows = []
  then Lwt.return_unit
  else (
    let col = List.nth child_meta.Cat.columns child_col_idx in
    let default_val = fk_default_value clock params col in
    if col.Row.not_null && default_val = Row.V_null
    then
      Lwt.fail_with
        (Printf.sprintf
           "FOREIGN KEY constraint failed: ON UPDATE SET DEFAULT on NOT NULL column \
            '%s.%s' with no default"
           child_meta.Cat.name
           child_col_name)
    else
      Lwt_list.iter_s
        (fun (crid, crow) ->
           cascade_update_col_in_tx
             tx
             cat
             ~visited
             clock
             params
             child_meta
             ~rowid:crid
             ~row:crow
             ~col_idx:child_col_idx
             ~new_val:default_val)
        child_rows)
;;

(* Apply SET NULL to each [child_col_idxs] of every row in [child_rows],
   rejecting NOT NULL columns; routes through cascade_update_col_in_tx so the
   write propagates further ON UPDATE chains.  [op_label] is "ON UPDATE" /
   "ON DELETE" for the error message. *)
let cascade_apply_set_null
      tx
      (cat : Cat.t)
      ~clock
      ~params
      ~visited
      ~op_label
      (child_meta : Cat.table_meta)
      ~child_col_idxs
      child_rows
  : unit Lwt.t
  =
  if child_rows = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun child_col_idx ->
         let col = List.nth child_meta.Cat.columns child_col_idx in
         if col.Row.not_null
         then
           Lwt.fail_with
             (Printf.sprintf
                "FOREIGN KEY constraint failed: %s SET NULL on NOT NULL column '%s.%s'"
                op_label
                child_meta.Cat.name
                col.Row.name)
         else
           Lwt_list.iter_s
             (fun (crid, crow) ->
                cascade_update_col_in_tx
                  tx
                  cat
                  ~visited
                  clock
                  params
                  child_meta
                  ~rowid:crid
                  ~row:crow
                  ~col_idx:child_col_idx
                  ~new_val:Row.V_null)
             child_rows)
      child_col_idxs
;;

(* Apply SET DEFAULT to each [child_col_idxs] of every row in [child_rows]. *)
let cascade_apply_set_default
      tx
      (cat : Cat.t)
      ~clock
      ~params
      ~visited
      ~op_label
      (child_meta : Cat.table_meta)
      ~child_col_idxs
      child_rows
  : unit Lwt.t
  =
  if child_rows = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun child_col_idx ->
         let col = List.nth child_meta.Cat.columns child_col_idx in
         let default_val = fk_default_value clock params col in
         if col.Row.not_null && default_val = Row.V_null
         then
           Lwt.fail_with
             (Printf.sprintf
                "FOREIGN KEY constraint failed: %s SET DEFAULT on NOT NULL column \
                 '%s.%s' with no default"
                op_label
                child_meta.Cat.name
                col.Row.name)
         else
           Lwt_list.iter_s
             (fun (crid, crow) ->
                cascade_update_col_in_tx
                  tx
                  cat
                  ~visited
                  clock
                  params
                  child_meta
                  ~rowid:crid
                  ~row:crow
                  ~col_idx:child_col_idx
                  ~new_val:default_val)
             child_rows)
      child_col_idxs
;;

(* Drain all rows of [table_meta] satisfying [where] into a (rowid,row) list
   under an RO snapshot, so subsequent writes don't invalidate the cursor. *)
(* Drain matching rows from the given txn (RO or RW). *)
let drain_matching_rows_in_tx
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~(where : Plan.expr option)
  : (int64 * Row.t) list Lwt.t
  =
  let* cur =
    S.cursor_open
      tx
      (let x, _, _, _ = Cat.row_storage table_meta in
       x)
  in
  let _sr = S.cursor_first cur in
  let buf = ref [] in
  let rec drain () =
    match S.cursor_next cur with
    | None -> ()
    | Some (kbytes, vbytes) ->
      let rowid = Rowid.decode kbytes in
      let row = decode_with_virtual clock params table_meta vbytes in
      let keep =
        match where with
        | None -> true
        | Some pred -> value_truthy (eval_expr clock params row pred)
      in
      if keep then buf := (rowid, row) :: !buf;
      drain ()
  in
  drain ();
  S.cursor_close cur;
  Lwt.return (List.rev !buf)
;;

(* Apply ORDER BY, then OFFSET, then LIMIT to a drained (rowid,row) list. *)
let apply_order_offset_limit ~clock ~params ~order ~offset ~limit matches =
  let sorted =
    if order = []
    then matches
    else
      List.sort
        (fun (_, ra) (_, rb) ->
           let rec cmp = function
             | [] -> 0
             | (e, dir, nulls) :: rest ->
               let va = eval_expr clock params ra e in
               let vb = eval_expr clock params rb e in
               let c = compare_with_nulls dir nulls va vb in
               if c <> 0 then c else cmp rest
           in
           cmp order)
        matches
  in
  let after_offset =
    match offset with
    | None | Some 0 -> sorted
    | Some n -> list_drop n sorted
  in
  match limit with
  | None -> after_offset
  | Some n -> list_take n after_offset
;;

(* Build the post-UPDATE row: copy [old_row] and apply each (i, expr) in
   [assignments], evaluating expr against the OLD row. *)
let apply_assignments ~clock ~params assignments (old_row : Row.t) : Row.t =
  let new_row = Array.copy old_row in
  List.iter
    (fun (i, expr) -> new_row.(i) <- eval_expr clock params old_row expr)
    assignments;
  new_row
;;

(* Pre-write RESTRICT/NO ACTION FK check for one UPDATE row's [fk]: if the
   parent key changes and is still referenced, raise (or queue deferred). *)
let precheck_update_fk
      store
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~rowid_outer
      ~(old_row : Row.t)
      ~(new_row : Row.t)
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
  : unit Lwt.t
  =
  match fk.fk_on_update with
  | Cat.FA_cascade | Cat.FA_set_null | Cat.FA_set_default -> Lwt.return_unit
  | Cat.FA_restrict | Cat.FA_no_action ->
    let is_deferred = fk.fk_deferrable || Cat.get_defer_fks_pragma cat in
    let parent_col_idxs =
      List.map (fun c -> find_col_idx_by_name table_meta.Cat.columns c) fk.fk_parent_cols
    in
    let old_vals = List.map (fun i -> old_row.(i)) parent_col_idxs in
    let new_vals = List.map (fun i -> new_row.(i)) parent_col_idxs in
    let unchanged =
      List.for_all2 (fun ov nv -> compare_values ov nv = 0) old_vals new_vals
    in
    if unchanged
    then Lwt.return_unit
    else if any_null_val old_vals
    then Lwt.return_unit
    else (
      let child_col_idxs =
        List.map (fun c -> find_col_idx_by_name child_meta.Cat.columns c) fk.fk_local_cols
      in
      let* has_ref =
        fk_child_has_ref_multi cat store child_meta ~child_col_idxs ~parent_vals:old_vals
      in
      if has_ref
      then (
        let msg =
          Printf.sprintf
            "FOREIGN KEY constraint failed: update to '%s.%s' is referenced by '%s.%s'"
            table_meta.Cat.name
            (String.concat "," fk.fk_parent_cols)
            child_meta.Cat.name
            (String.concat "," fk.fk_local_cols)
        in
        let recheck =
          make_fk_recheck
            cat
            ~child_name:child_meta.Cat.name
            ~parent_name:table_meta.Cat.name
            ~child_cols:fk.fk_local_cols
            ~parent_cols:fk.fk_parent_cols
            ~parent_vals:old_vals
        in
        fk_violation
          ~deferred:is_deferred
          cat
          ~kind:`Update
          ~table:table_meta.Cat.name
          ~rowid:rowid_outer
          ~msg
          ~recheck)
      else Lwt.return_unit)
;;

(* Pre-write FK RESTRICT check across all matched UPDATE rows. *)
let precheck_update_fk_restrict
      store
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~assignments
      ~child_refs
      matches
  : unit Lwt.t
  =
  if child_refs = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun (rowid_outer, old_row) ->
         let new_row = apply_assignments ~clock ~params assignments old_row in
         Lwt_list.iter_s
           (fun (child_meta, fks) ->
              Lwt_list.iter_s
                (precheck_update_fk
                   store
                   cat
                   table_meta
                   ~rowid_outer
                   ~old_row
                   ~new_row
                   child_meta)
                fks)
           child_refs)
      matches
;;

(* First UPDATE pass: validate UNIQUE for every target row against the full
   set of new values (an updated row may collide with another updated row). *)
(* Check one unique index for an UPDATE that turns [old_row] into [new_row]
   (with virtuals computed in [new_row_for_idx]); fails the Lwt thread on a
   duplicate. *)
let check_index_unique_on_update
      tx
      (idx : Cat.index_info)
      ~clock
      ~params
      ~schema
      ~old_row
      ~new_row
      ~new_row_for_idx
      ~rowid
  : unit Lwt.t
  =
  if not idx.idx_unique
  then Lwt.return_unit
  else if not (row_matches_index_where clock params idx schema new_row_for_idx)
  then Lwt.return_unit
  else (
    let old_vs = get_index_key_values clock params idx schema old_row in
    let new_vs = get_index_key_values clock params idx schema new_row_for_idx in
    (* #290: a new key with ANY NULL column is exempt — NULLs are distinct in a
       SQLite UNIQUE index, so the updated row can never conflict.  (The index
       entry itself is still maintained by the regular update path.) *)
    if any_null_val new_vs
    then Lwt.return_unit
    else (
      let values_equal a b =
        match a, b with
        | Row.V_null, Row.V_null -> true
        | Row.V_int x, Row.V_int y -> Int64.equal x y
        | Row.V_text x, Row.V_text y -> String.equal x y
        | Row.V_real x, Row.V_real y -> Float.equal x y
        | Row.V_blob x, Row.V_blob y -> Bytes.equal x y
        | _ -> false
      in
      let unchanged = List.for_all2 values_equal old_vs new_vs in
      if unchanged
      then Lwt.return_unit
      else
        let* dup = unique_violation_on_update tx idx new_vs ~rowid ~new_row ~schema in
        if dup
        then
          Lwt.fail_with
            (unique_constraint_failed_msg
               ~table:idx.Cat.idx_table
               ~columns:idx.idx_columns)
        else Lwt.return_unit))
;;

let validate_update_unique
      tx
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~indexes
      ~assignments
      matches
  : unit Lwt.t
  =
  let schema = table_meta.Cat.columns in
  Lwt_list.iter_s
    (fun (rowid, old_row) ->
       let new_row = apply_assignments ~clock ~params assignments old_row in
       compute_stored_generated_cols clock params table_meta new_row;
       eval_check_constraints clock params table_meta new_row;
       let new_row_for_idx = with_computed_virtuals clock params table_meta new_row in
       Lwt_list.iter_s
         (fun (idx : Cat.index_info) ->
            check_index_unique_on_update
              tx
              idx
              ~clock
              ~params
              ~schema
              ~old_row
              ~new_row
              ~new_row_for_idx
              ~rowid)
         indexes)
    matches
;;

(* Apply the ON UPDATE cascade of one [fk] for a parent row changing
   [old_row] -> [new_row], within the RW txn (RESTRICT handled in precheck). *)
let apply_update_cascade_fk
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~visited
      ~(old_row : Row.t)
      ~(new_row : Row.t)
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
  : unit Lwt.t
  =
  let parent_col_idxs =
    List.map (fun c -> find_col_idx_by_name table_meta.Cat.columns c) fk.fk_parent_cols
  in
  let old_vals = List.map (fun i -> old_row.(i)) parent_col_idxs in
  let new_vals = List.map (fun i -> new_row.(i)) parent_col_idxs in
  let unchanged =
    List.for_all2 (fun ov nv -> compare_values ov nv = 0) old_vals new_vals
  in
  if unchanged
  then Lwt.return_unit
  else if any_null_val old_vals
  then Lwt.return_unit
  else (
    let child_col_idxs =
      List.map (fun c -> find_col_idx_by_name child_meta.Cat.columns c) fk.fk_local_cols
    in
    match fk.fk_on_update with
    | Cat.FA_restrict | Cat.FA_no_action -> Lwt.return_unit
    | Cat.FA_cascade ->
      let* child_rows =
        scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals:old_vals
      in
      (* For cascade, use the first child col (single-col FK compat) *)
      let child_col_idx = List.hd child_col_idxs in
      let new_val_single = List.hd new_vals in
      Lwt_list.iter_s
        (fun (crid, crow) ->
           cascade_update_col_in_tx
             tx
             cat
             ~visited
             clock
             params
             child_meta
             ~rowid:crid
             ~row:crow
             ~col_idx:child_col_idx
             ~new_val:new_val_single)
        child_rows
    | Cat.FA_set_null ->
      let* child_rows =
        scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals:old_vals
      in
      cascade_apply_set_null
        tx
        cat
        ~clock
        ~params
        ~visited
        ~op_label:"ON UPDATE"
        child_meta
        ~child_col_idxs
        child_rows
    | Cat.FA_set_default ->
      let* child_rows =
        scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals:old_vals
      in
      cascade_apply_set_default
        tx
        cat
        ~clock
        ~params
        ~visited
        ~op_label:"ON UPDATE"
        child_meta
        ~child_col_idxs
        child_rows)
;;

(* Apply all ON UPDATE cascades for a parent row changing old_row -> new_row. *)
let apply_update_cascades
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~visited
      ~child_refs
      ~(old_row : Row.t)
      ~(new_row : Row.t)
  : unit Lwt.t
  =
  if child_refs = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun (child_meta, fks) ->
         Lwt_list.iter_s
           (apply_update_cascade_fk
              tx
              cat
              table_meta
              ~clock
              ~params
              ~visited
              ~old_row
              ~new_row
              child_meta)
           fks)
      child_refs
;;

(* Apply one matched UPDATE row: compute new row, run ON UPDATE cascades,
   reindex, and overwrite the row in the table tree. *)
let apply_update_row
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~child_refs
      ~indexes
      ~assignments
      (rowid, old_row)
  : (int64 * Row.t) Lwt.t
  =
  let new_row = apply_assignments ~clock ~params assignments old_row in
  compute_stored_generated_cols clock params table_meta new_row;
  (* Phase 35 task 3a: per-row visited set seeded with parent rowid, so
     cyclic ON UPDATE cascades terminate. *)
  let visited = Hashtbl.create 16 in
  Hashtbl.add visited (table_meta.Cat.name, rowid) ();
  let* () =
    apply_update_cascades
      tx
      cat
      table_meta
      ~clock
      ~params
      ~visited
      ~child_refs
      ~old_row
      ~new_row
  in
  (* #243/#249: re-keys the row when the UPDATE changed the INTEGER PRIMARY KEY
     alias column (uniqueness probe + del-old/put-new + reindex), else rewrites
     in place.  Shared with the UPSERT and ON UPDATE CASCADE paths. *)
  let* new_rowid =
    write_row_rekeyed
      tx
      table_meta
      ~clock
      ~params
      ~old_row
      ~new_row
      ~old_rowid:rowid
      ~indexes
  in
  (* Return the row as actually stored (generated columns included) so callers
     such as UPDATE ... RETURNING can project committed values, not a pre-lock
     snapshot (#226); [new_rowid] (#417) differs from [rowid] only when the UPDATE
     moved the INTEGER-PK alias. *)
  Lwt.return (new_rowid, new_row)
;;

(* Fire an UPDATE row-hook (BEFORE/AFTER) for each matched row, recomputing
   the post-UPDATE row from the pre-write snapshot.  For non-deterministic
   expressions (random(), now()) the value the trigger sees may differ from
   the committed row. *)
let run_update_hook ~clock ~params ~assignments ~tx hook matches : unit Lwt.t =
  match hook with
  | None -> Lwt.return_unit
  | Some f ->
    Lwt_list.iter_s
      (fun (_rowid, old_row) ->
         let new_row = apply_assignments ~clock ~params assignments old_row in
         f ~tx ~old_row ~new_row)
      matches
;;

(** Run [Op_update]: drain matching rows into a list (snapshot read),
    then for each (rowid, old_row) compute the new row, update index
    entries, and overwrite the row in the table tree.  Returns the
    number of rows whose contents were modified. *)
let execute_update
      ?(mode = Auto)
      ?(params = [||])
      ?(clock : (unit -> float) option = None)
      ?(before_hook :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      ?(after_hook :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      ?(collect : (Row.t -> unit) option = None)
      (store : S.t)
      (cat : Cat.t)
      ~(table_meta : Cat.table_meta)
      ~(assignments : (int * Plan.expr) list)
      ~(where : Plan.expr option)
      ~(order : (Plan.expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list)
      ~(limit : int option)
      ~(offset : int option)
      ~(indexes : Cat.index_info list)
  : int Lwt.t
  =
  (* Acquire the write lock BEFORE draining so the read-modify-write is atomic.
     Draining via a separate RO snapshot first (the old [Auto] path) let a
     concurrent commit land between the row read and the lock acquisition, so
     the new row was computed from a stale value and clobbered that commit —
     a lost update on backends whose commit yields, e.g. WAL/file (#223).
     [In_txn] already drained under the caller's lock; this makes [Auto] match. *)
  let* tx, owned = acquire_txn store mode in
  Lwt.catch
    (fun () ->
       let* matches = drain_matching_rows_in_tx tx table_meta ~clock ~params ~where in
       let matches =
         apply_order_offset_limit ~clock ~params ~order ~offset ~limit matches
       in
       let n = List.length matches in
       if n = 0
       then
         let* () = if owned then S.rollback tx else Lwt.return_unit in
         Lwt.return 0
       else
         let* child_refs =
           if Cat.get_fk_enforcement cat
           then build_child_refs cat ~parent_table_name:table_meta.Cat.name
           else Lwt.return []
         in
         (* FK pre-check: fail for RESTRICT/NO_ACTION when a referenced key
            changes.  CASCADE/SET_NULL/SET_DEFAULT are applied in the txn below. *)
         let* () =
           precheck_update_fk_restrict
             store
             cat
             table_meta
             ~clock
             ~params
             ~assignments
             ~child_refs
             matches
         in
         (* Phase 38: BEFORE/AFTER UPDATE fire inside the parent txn so nested
            DML shares it (atomic rollback on failure; no nested-trigger
            deadlock). *)
         let* () = run_update_hook ~clock ~params ~assignments ~tx before_hook matches in
         let* () =
           validate_update_unique
             tx
             table_meta
             ~clock
             ~params
             ~indexes
             ~assignments
             matches
         in
         let* () =
           Lwt_list.iter_s
             (fun ((rowid, old_row) as m) ->
                let* new_rowid, new_row =
                  apply_update_row
                    tx
                    cat
                    table_meta
                    ~clock
                    ~params
                    ~child_refs
                    ~indexes
                    ~assignments
                    m
                in
                (* #417: capture the per-row pre/post images for the delta feed
                   (no-op unless a change-capturing accumulator is installed); a
                   rowid-changing UPDATE is recorded as Deleted+Inserted. *)
                record_update
                  table_meta.Cat.name
                  ~old_rowid:rowid
                  ~new_rowid
                  ~old_row
                  ~new_row;
                (* RETURNING / row collection sees the committed row (#226). *)
                (match collect with
                 | Some f -> f new_row
                 | None -> ());
                Lwt.return_unit)
             matches
         in
         let* () = run_update_hook ~clock ~params ~assignments ~tx after_hook matches in
         let* () = release_txn ~cat tx owned in
         if n > 0 then mark_dirty table_meta.Cat.name;
         Lwt.return n)
    (fun exn ->
       let* () = if owned then S.rollback tx else Lwt.return_unit in
       Lwt.fail exn)
;;

(* Pre-write RESTRICT/NO ACTION FK check for one DELETE row's [fk]: if a
   child still references the row being deleted, raise (or queue deferred). *)
let precheck_delete_fk
      store
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~rowid_outer
      ~(row : Row.t)
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
  : unit Lwt.t
  =
  match fk.fk_on_delete with
  | Cat.FA_cascade | Cat.FA_set_null | Cat.FA_set_default -> Lwt.return_unit
  | Cat.FA_restrict | Cat.FA_no_action ->
    let is_deferred = fk.fk_deferrable || Cat.get_defer_fks_pragma cat in
    let parent_col_idxs =
      List.map (fun c -> find_col_idx_by_name table_meta.Cat.columns c) fk.fk_parent_cols
    in
    let parent_vals = List.map (fun i -> row.(i)) parent_col_idxs in
    if any_null_val parent_vals
    then Lwt.return_unit
    else (
      let child_col_idxs =
        List.map (fun c -> find_col_idx_by_name child_meta.Cat.columns c) fk.fk_local_cols
      in
      let* has_ref =
        fk_child_has_ref_multi cat store child_meta ~child_col_idxs ~parent_vals
      in
      if has_ref
      then (
        let msg =
          Printf.sprintf
            "FOREIGN KEY constraint failed: '%s.%s' is still referenced by '%s.%s'"
            table_meta.Cat.name
            (String.concat "," fk.fk_parent_cols)
            child_meta.Cat.name
            (String.concat "," fk.fk_local_cols)
        in
        let recheck =
          make_fk_recheck
            cat
            ~child_name:child_meta.Cat.name
            ~parent_name:table_meta.Cat.name
            ~child_cols:fk.fk_local_cols
            ~parent_cols:fk.fk_parent_cols
            ~parent_vals
        in
        fk_violation
          ~deferred:is_deferred
          cat
          ~kind:`Delete
          ~table:table_meta.Cat.name
          ~rowid:rowid_outer
          ~msg
          ~recheck)
      else Lwt.return_unit)
;;

(* Pre-write FK RESTRICT check across all matched DELETE rows. *)
let precheck_delete_fk_restrict
      store
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~child_refs
      matches
  : unit Lwt.t
  =
  if child_refs = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun (rowid_outer, row) ->
         Lwt_list.iter_s
           (fun (child_meta, fks) ->
              Lwt_list.iter_s
                (precheck_delete_fk store cat table_meta ~rowid_outer ~row child_meta)
                fks)
           child_refs)
      matches
;;

(* Apply the ON DELETE cascade of one [fk] for parent [row] being deleted,
   within the RW txn (RESTRICT handled in precheck). *)
let apply_delete_cascade_fk
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~visited
      ~(row : Row.t)
      (child_meta : Cat.table_meta)
      (fk : Cat.fk_constraint)
  : unit Lwt.t
  =
  let parent_col_idxs =
    List.map (fun c -> find_col_idx_by_name table_meta.Cat.columns c) fk.fk_parent_cols
  in
  let parent_vals = List.map (fun i -> row.(i)) parent_col_idxs in
  if any_null_val parent_vals
  then Lwt.return_unit
  else (
    let child_col_idxs =
      List.map (fun c -> find_col_idx_by_name child_meta.Cat.columns c) fk.fk_local_cols
    in
    match fk.fk_on_delete with
    | Cat.FA_restrict | Cat.FA_no_action -> Lwt.return_unit
    | Cat.FA_cascade ->
      let* child_rows =
        scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
      in
      Lwt_list.iter_s
        (fun (crid, crow) ->
           cascade_delete_row_in_tx
             tx
             cat
             ~visited
             clock
             params
             child_meta
             ~rowid:crid
             ~row:crow)
        child_rows
    | Cat.FA_set_null ->
      let* child_rows =
        scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
      in
      cascade_apply_set_null
        tx
        cat
        ~clock
        ~params
        ~visited
        ~op_label:"ON DELETE"
        child_meta
        ~child_col_idxs
        child_rows
    | Cat.FA_set_default ->
      let* child_rows =
        scan_child_rows_multi_tx cat tx child_meta ~child_col_idxs ~parent_vals
      in
      cascade_apply_set_default
        tx
        cat
        ~clock
        ~params
        ~visited
        ~op_label:"ON DELETE"
        child_meta
        ~child_col_idxs
        child_rows)
;;

(* Apply all ON DELETE cascades for parent [row] being deleted. *)
let apply_delete_cascades
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~visited
      ~child_refs
      ~(row : Row.t)
  : unit Lwt.t
  =
  if child_refs = []
  then Lwt.return_unit
  else
    Lwt_list.iter_s
      (fun (child_meta, fks) ->
         Lwt_list.iter_s
           (apply_delete_cascade_fk
              tx
              cat
              table_meta
              ~clock
              ~params
              ~visited
              ~row
              child_meta)
           fks)
      child_refs
;;

(* Delete one matched row: run ON DELETE cascades, remove index entries, then
   remove the row.  Visited set seeded with this row so cyclic cascades stop. *)
let apply_delete_row
      tx
      (cat : Cat.t)
      (table_meta : Cat.table_meta)
      ~clock
      ~params
      ~child_refs
      ~indexes
      (rowid, row)
  : unit Lwt.t
  =
  let visited = Hashtbl.create 16 in
  Hashtbl.add visited (table_meta.Cat.name, rowid) ();
  let* () =
    apply_delete_cascades tx cat table_meta ~clock ~params ~visited ~child_refs ~row
  in
  let rowid_key = Rowid.encode rowid in
  (* row from drain_matching_rows_in_tx → decode_with_virtual: VIRTUAL cols
     already applied in-place, so pass directly as row_for_idx. *)
  let* () =
    delete_row_indexes tx table_meta ~clock ~params ~row_for_idx:row ~rowid indexes
  in
  let* () =
    S.del
      tx
      (let x, _, _, _ = Cat.row_storage table_meta in
       x)
      rowid_key
  in
  (* #409: reuse a plain rowid table's high-water after a committed delete. *)
  Cat.note_rowid_deleted cat ~name:table_meta.Cat.name ~rowid tx
;;

(** Run [Op_delete]: drain matching rows into a list (snapshot read),
    then for each matching (rowid, row) remove index entries and the
    row itself from the table tree.  Returns the number of rows deleted. *)
let execute_delete
      ?(mode = Auto)
      ?(params = [||])
      ?(clock : (unit -> float) option = None)
      ?(before_hook : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option = None)
      ?(after_hook : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option = None)
      ?(collect : (Row.t -> unit) option = None)
      (store : S.t)
      (cat : Cat.t)
      ~(table_meta : Cat.table_meta)
      ~(where : Plan.expr option)
      ~(order : (Plan.expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list)
      ~(limit : int option)
      ~(offset : int option)
      ~(indexes : Cat.index_info list)
  : int Lwt.t
  =
  (* Acquire the write lock BEFORE draining so the match set can't go stale
     between the read and the delete (same TOCTOU as the UPDATE path, #223):
     otherwise a row matched on a separate RO snapshot could be concurrently
     modified to no longer match — yet still be deleted.  [In_txn] already
     drained under the caller's lock; this makes [Auto] match. *)
  let* tx, owned = acquire_txn store mode in
  Lwt.catch
    (fun () ->
       let* matches = drain_matching_rows_in_tx tx table_meta ~clock ~params ~where in
       let matches =
         apply_order_offset_limit ~clock ~params ~order ~offset ~limit matches
       in
       let n = List.length matches in
       if n = 0
       then
         let* () = if owned then S.rollback tx else Lwt.return_unit in
         Lwt.return 0
       else
         (* FK pre-check: fail for RESTRICT/NO_ACTION; CASCADE/SET_NULL/SET_DEFAULT
            are applied inside the RW transaction below. *)
         let* child_refs =
           if Cat.get_fk_enforcement cat
           then build_child_refs cat ~parent_table_name:table_meta.Cat.name
           else Lwt.return []
         in
         let* () = precheck_delete_fk_restrict store cat table_meta ~child_refs matches in
         (* Phase 38: BEFORE/AFTER DELETE fire inside the parent txn so nested
            DML shares it and trigger failures roll back the DELETE. *)
         let* () =
           match before_hook with
           | None -> Lwt.return_unit
           | Some f -> Lwt_list.iter_s (fun (_rowid, old_row) -> f ~tx ~old_row) matches
         in
         let* () =
           Lwt_list.iter_s
             (fun ((rowid, old_row) as m) ->
                let* () =
                  apply_delete_row tx cat table_meta ~clock ~params ~child_refs ~indexes m
                in
                (* #417: capture the removed row for the delta feed (no-op unless
                   a change-capturing accumulator is installed). *)
                record_change table_meta.Cat.name (Deleted { rowid; row = old_row });
                (* RETURNING / row collection sees the row as deleted under the
                   write lock, not a pre-lock snapshot (#226). *)
                (match collect with
                 | Some f -> f old_row
                 | None -> ());
                Lwt.return_unit)
             matches
         in
         let* () =
           match after_hook with
           | None -> Lwt.return_unit
           | Some f -> Lwt_list.iter_s (fun (_rowid, old_row) -> f ~tx ~old_row) matches
         in
         let* () = release_txn ~cat tx owned in
         if n > 0 then mark_dirty table_meta.Cat.name;
         Lwt.return n)
    (fun exn ->
       let* () = if owned then S.rollback tx else Lwt.return_unit in
       Lwt.fail exn)
;;

(** Run [Op_drop_table]: remove catalog entries for the table and all
    its indexes.  The B+-tree pages are NOT reclaimed in Phase 2.

    #279: runs through [with_ddl_txn] so it participates in any ambient explicit
    transaction (borrowed [In_txn]) or owns its own auto-committed txn ([Auto]),
    inheriting the same poison-on-failure / no-partial-effect-COMMIT behaviour as
    CREATE/ALTER (#286).  [drop_table] removes the table AND its dependent
    indexes from the in-memory cache before the caller commits, so a schema-cache
    undo is registered to restore both on a [ROLLBACK] (the store reverts the
    _sys_* row deletes; this re-syncs the cache).  The undo restores EXACTLY the
    entries [drop_table] removes; if an index was itself created earlier in the
    same transaction, this DROP undo restores it but the earlier CREATE INDEX's
    undo — running later in LIFO order — removes it again, netting the correct
    "absent after ROLLBACK" outcome. *)
let execute_drop_table
      ?(mode = Auto)
      (store : S.t)
      (cat : Cat.t)
      ~(table_meta : Cat.table_meta)
      ~(_indexes : Cat.index_info list)
  : unit Lwt.t
  =
  with_ddl_txn store cat mode (fun tx ->
    let name = table_meta.Cat.name in
    (* #283: [Cat.drop_table] self-registers the cache undo for the table and each
       dependent index (via [Schema_cache.remove_table]/[remove_index]), so no
       external snapshot+undo is needed here. *)
    Cat.drop_table cat tx ~name)
;;

(** Run [Op_drop_index]: remove catalog entry for the index.
    The B+-tree pages are NOT reclaimed in Phase 2.

    #279: as for [execute_drop_table] — runs through [with_ddl_txn] and registers
    a schema-cache undo so a [ROLLBACK] restores the dropped index entry. *)
let execute_drop_index
      ?(mode = Auto)
      (store : S.t)
      (cat : Cat.t)
      ~(idx_info : Cat.index_info)
  : unit Lwt.t
  =
  with_ddl_txn store cat mode (fun tx ->
    (* #283: [Cat.drop_index] self-registers the cache undo. *)
    Cat.drop_index cat tx ~name:idx_info.Cat.idx_name)
;;

(* ------------------------------------------------------------------ *)
(* EXPLAIN plan-tree pretty-printer                                     *)
(* ------------------------------------------------------------------ *)

let op_name = function
  | Plan.Op_seq_scan { table_meta } -> "SeqScan(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_col_seq_scan { table_meta } -> "ColSeqScan(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_filter _ -> "Filter"
  | Plan.Op_project _ -> "Project"
  | Plan.Op_expr_project _ -> "ExprProject"
  | Plan.Op_sort _ -> "Sort"
  | Plan.Op_limit { limit; offset; _ } ->
    Printf.sprintf "Limit(%d offset %d)" limit offset
  | Plan.Op_aggregate _ -> "Aggregate"
  | Plan.Op_hash_join { join_kind; _ } ->
    (match join_kind with
     | `Inner -> "HashJoin"
     | `Left -> "LeftHashJoin")
  | Plan.Op_nested_loop_join { join_kind; right_meta; _ } ->
    (match join_kind with
     | `Inner -> "NestedLoopJoin(" ^ right_meta.Cat.name ^ ")"
     | `Left -> "LeftNestedLoopJoin(" ^ right_meta.Cat.name ^ ")")
  | Plan.Op_index_lookup { table_meta; _ } -> "IndexLookup(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_rowid_lookup { table_meta; _ } -> "RowidLookup(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_union { all; _ } -> if all then "UnionAll" else "Union"
  | Plan.Op_intersect _ -> "Intersect"
  | Plan.Op_except _ -> "Except"
  | Plan.Op_distinct _ -> "Distinct"
  | Plan.Op_const_select _ -> "ConstSelect"
  | Plan.Op_window _ -> "Window"
  | Plan.Op_with_cte { cte_name; _ } -> "WithCte(" ^ cte_name ^ ")"
  | Plan.Op_cte_scan { cte_name; _ } -> "CteScan(" ^ cte_name ^ ")"
  | Plan.Op_insert { table_meta; _ } -> "Insert(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_insert_select { table_meta; _ } -> "InsertSelect(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_update { table_meta; _ } -> "Update(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_delete { table_meta; _ } -> "Delete(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_create_table { name; _ } -> "CreateTable(" ^ name ^ ")"
  | Plan.Op_col_create_table { name; _ } -> "ColCreateTable(" ^ name ^ ")"
  | Plan.Op_create_index { name; table; _ } ->
    "CreateIndex(" ^ name ^ " on " ^ table ^ ")"
  | Plan.Op_drop_table { table_meta; _ } -> "DropTable(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_drop_index { idx_info } -> "DropIndex(" ^ idx_info.Cat.idx_name ^ ")"
  | Plan.Op_alter_table { table_meta; _ } -> "AlterTable(" ^ table_meta.Cat.name ^ ")"
  | Plan.Op_begin -> "Begin"
  | Plan.Op_commit -> "Commit"
  | Plan.Op_rollback -> "Rollback"
  | Plan.Op_savepoint name -> "Savepoint(" ^ name ^ ")"
  | Plan.Op_release name -> "Release(" ^ name ^ ")"
  | Plan.Op_rollback_to name -> "RollbackTo(" ^ name ^ ")"
  | Plan.Op_create_view { name; _ } -> "CreateView(" ^ name ^ ")"
  | Plan.Op_create_reactive_view { name; _ } -> "CreateReactiveView(" ^ name ^ ")"
  | Plan.Op_drop_view { name } -> "DropView(" ^ name ^ ")"
  | Plan.Op_create_trigger { name; _ } -> "CreateTrigger(" ^ name ^ ")"
  | Plan.Op_drop_trigger { name } -> "DropTrigger(" ^ name ^ ")"
  | Plan.Op_pragma_rows _ -> "Pragma"
  | Plan.Op_pragma_get_user_version -> "Pragma(get_user_version)"
  | Plan.Op_pragma_set_user_version { version } ->
    Printf.sprintf "Pragma(set_user_version=%Ld)" version
  | Plan.Op_pragma_integrity_check -> "Pragma(integrity_check)"
  | Plan.Op_pragma_get_fk -> "Pragma(get_foreign_keys)"
  | Plan.Op_pragma_set_fk { on } -> Printf.sprintf "Pragma(set_foreign_keys=%b)" on
  | Plan.Op_pragma_get_recursive_triggers -> "Pragma(get_recursive_triggers)"
  | Plan.Op_pragma_set_recursive_triggers { on } ->
    Printf.sprintf "Pragma(set_recursive_triggers=%b)" on
  | Plan.Op_pragma_get_defer_fk -> "Pragma(get_defer_foreign_keys)"
  | Plan.Op_pragma_set_defer_fk { on } ->
    Printf.sprintf "Pragma(set_defer_foreign_keys=%b)" on
  | Plan.Op_pragma_wal_checkpoint -> "Pragma(wal_checkpoint)"
  | Plan.Op_pragma_get_wal_autocheckpoint -> "Pragma(get_wal_autocheckpoint)"
  | Plan.Op_pragma_set_wal_autocheckpoint { n } ->
    Printf.sprintf "Pragma(set_wal_autocheckpoint=%Ld)" n
  | Plan.Op_pragma_get_synchronous -> "Pragma(get_synchronous)"
  | Plan.Op_pragma_set_synchronous { mode } ->
    Printf.sprintf "Pragma(set_synchronous=%s)" mode
  | Plan.Op_pragma_get_wal_batch_commits -> "Pragma(get_wal_batch_commits)"
  | Plan.Op_pragma_set_wal_batch_commits { n } ->
    Printf.sprintf "Pragma(set_wal_batch_commits=%Ld)" n
  | Plan.Op_pragma_get_wal_batch_interval_ms -> "Pragma(get_wal_batch_interval_ms)"
  | Plan.Op_pragma_set_wal_batch_interval_ms { n } ->
    Printf.sprintf "Pragma(set_wal_batch_interval_ms=%Ld)" n
  | Plan.Op_vacuum -> "Vacuum"
  | Plan.Op_attach { schema; _ } -> Printf.sprintf "Attach(%s)" schema
  | Plan.Op_detach { schema } -> Printf.sprintf "Detach(%s)" schema
  | Plan.Op_database_list -> "Pragma(database_list)"
  | Plan.Op_active_database_get -> "Pragma(active_database)"
  | Plan.Op_active_database_set { schema } ->
    Printf.sprintf "Pragma(active_database=%s)" schema
  | Plan.Op_no_op -> "NoOp"
  | Plan.Op_changes -> "Changes"
  | Plan.Op_last_insert_rowid -> "LastInsertRowid"
  | Plan.Op_total_changes -> "TotalChanges"
  | Plan.Op_explain { analyze; _ } -> if analyze then "ExplainAnalyze" else "Explain"
  | Plan.Op_create_fts_table { name; _ } -> "CreateFtsTable(" ^ name ^ ")"
  | Plan.Op_fts_insert { fts_meta; _ } -> "FtsInsert(" ^ fts_meta.Cat.fts_name ^ ")"
  | Plan.Op_fts_delete { fts_meta; _ } -> "FtsDelete(" ^ fts_meta.Cat.fts_name ^ ")"
  | Plan.Op_fts_seq_scan { fts_meta; _ } -> "FtsSeqScan(" ^ fts_meta.Cat.fts_name ^ ")"
  | Plan.Op_fts_match_scan { fts_meta; _ } ->
    "FtsMatchScan(" ^ fts_meta.Cat.fts_name ^ ")"
  | Plan.Op_sqlite_master -> "SqliteMaster"
  | Plan.Op_sqlite_sequence -> "SqliteSequence"
  | Plan.Op_seq_set { table; _ } -> "SeqSet(" ^ table ^ ")"
  | Plan.Op_seq_reset { table } ->
    "SeqReset("
    ^ (match table with
       | Some t -> t
       | None -> "*")
    ^ ")"
;;

let op_children = function
  | Plan.Op_filter { child; _ } -> [ child ]
  | Plan.Op_project { child; _ } -> [ child ]
  | Plan.Op_expr_project { child; _ } -> [ child ]
  | Plan.Op_sort { child; _ } -> [ child ]
  | Plan.Op_limit { child; _ } -> [ child ]
  | Plan.Op_distinct { child } -> [ child ]
  | Plan.Op_aggregate { child; _ } -> [ child ]
  | Plan.Op_window { child; _ } -> [ child ]
  | Plan.Op_hash_join { left; right; _ } -> [ left; right ]
  | Plan.Op_nested_loop_join { left; _ } -> [ left ]
  | Plan.Op_union { left; right; _ } -> [ left; right ]
  | Plan.Op_intersect { left; right } -> [ left; right ]
  | Plan.Op_except { left; right } -> [ left; right ]
  | Plan.Op_with_cte { def; query; _ } -> [ def; query ]
  | Plan.Op_explain { inner; _ } -> [ inner ]
  | Plan.Op_insert_select { source; _ } -> [ source ]
  | _ -> []
;;

let explain_plan op =
  let counter = ref 0 in
  let rec walk parent op =
    let id = !counter in
    incr counter;
    let my_row =
      [| Row.V_int (Int64.of_int id)
       ; Row.V_int (Int64.of_int parent)
       ; Row.V_text (op_name op)
      |]
    in
    my_row :: List.concat_map (walk id) (op_children op)
  in
  walk (-1) op
;;

(* #239: per-query cost/stats signal for an external cost-based cache.
   [rows_examined] counts rows the executor pulled from a base table/index scan
   (the true work signal: a query that scans a million rows to return one reads
   examined=1_000_000, returned=1); [rows_returned] is the size of the result
   stream once drained; [used_index] is the plan-time fact that the base access
   is an index/rowid seek rather than a full scan.  Mirage-pure — plain counters
   the executor already has, no clock/Unix dependency. *)
type query_stats =
  { mutable rows_examined : int
  ; mutable rows_returned : int
  ; mutable used_index : bool
  }

let make_query_stats () = { rows_examined = 0; rows_returned = 0; used_index = false }

(* The active query's stats record, propagated to the base scanners via Lwt
   sequence-associated storage rather than threaded through the ~50 mutually
   recursive [to_stream] helpers.  A leaf scanner reads it ONCE at stream
   construction (which runs inside [query]'s [with_value] scope, carried across
   binds), captures the result in its row-producing closure, and increments per
   row pulled — correct regardless of when the lazy stream is later drained, and
   safe across interleaved fibres because each query has its own record. *)
let query_stats_key : query_stats Lwt.key = Lwt.new_key ()

(* #262: the active transaction mode for the query currently executing, carried
   in Lwt sequence-associated storage.  Subquery evaluation ([pre_eval_subquery]
   and the correlated re-eval at pull time) reads it to run inner reads under the
   same txn, so [SELECT … WHERE x IN (SELECT …)] inside an open transaction sees
   the transaction's own uncommitted writes — not just the top-level scan.  The
   base scanners take [mode] as an explicit argument and do not consult this. *)
let txn_mode_key : txn_mode Lwt.key = Lwt.new_key ()

let current_txn_mode () =
  match Lwt.get txn_mode_key with
  | Some mode -> mode
  | None -> Auto
;;

(* #262: re-establish BOTH per-query Lwt-storage contexts (the stats record and
   the txn mode) for work that runs at pull time — outside [query]'s
   construction-time [with_value] scope — currently the correlated-subquery
   re-eval in [stream_filter] / [stream_expr_project].  Bundling the pair here
   keeps them in lock-step: a future pull-time site cannot restore one and
   silently drop the other (the exact omission #262 corrected for the mode). *)
let with_pull_context ~stats ~mode f =
  Lwt.with_value query_stats_key stats
  @@ fun () -> Lwt.with_value txn_mode_key (Some mode) f
;;

(* Increment via the closure-captured option; never calls [Lwt.get] at pull time
   (the consumer drains outside the [with_value] scope).  [None] for the common
   no-stats query is a single predicted branch with no per-row cost. *)
let incr_examined (s_opt : query_stats option) =
  match s_opt with
  | Some s -> s.rows_examined <- s.rows_examined + 1
  | None -> ()
;;

(** Forward reference to [to_stream], which is defined in the mutually-recursive
    block starting at [pre_eval_subquery].  [execute_with_count] needs this to
    implement [Op_insert_select] (read source, then write rows). *)
let to_stream_ref
  : ((unit -> float) option
     -> Row.value array
     -> S.t
     -> ?mode:txn_mode
     -> ?cat:Cat.t option
     -> Plan.op
     -> Row.t Lwt_stream.t Lwt.t)
      ref
  =
  ref (fun _clock _params _store ?mode:_ ?cat:_ _op ->
    failwith "to_stream_ref not yet initialised")
;;

(* Op_create_table: register the table, its UNIQUE indexes, and FK constraints. *)
(** [execute_with_count] returns the rows-affected count.  For most
    write ops this is 1 (INSERT) or 0 (DDL); for UPDATE it is the
    number of rows whose contents were modified. *)
let execute_create_table_op
      (store : S.t)
      (cat : Cat.t)
      ~mode
      ~name
      ~columns
      ~uniq_idxs
      ~if_not_exists
      ~fk_constraints
      ~without_rowid
      ~autoincrement
  : int Lwt.t
  =
  if Cat.table_exists cat ~name
  then
    if if_not_exists
    then Lwt.return 0
    else (* Raise synchronously (before acquiring any txn), as callers expect. *)
      failwith (Printf.sprintf "table '%s' already exists" name)
  else
    (* #269: the table, its implicit UNIQUE indexes, and its FK rows all go
       through one writer txn (the ambient explicit one if any), so the whole
       CREATE TABLE is atomic and never self-deadlocks. *)
    with_ddl_txn store cat mode (fun tx ->
      let* _tid =
        Cat.create_table ~txn:tx cat ~name ~columns ~without_rowid ~autoincrement
      in
      let* () =
        Lwt_list.iter_s
          (fun (idx_name, col_names, origin) ->
             let* result =
               Cat.create_index
                 ~txn:tx
                 cat
                 ~name:idx_name
                 ~table:name
                 ~columns:col_names
                 ~unique:true
                 ~expr_flags:(List.map (fun _ -> false) col_names)
                 ~where_sql:None
                 ~origin
             in
             match result with
             | Error msg -> Lwt.fail_with msg
             | Ok _ -> Lwt.return_unit)
          uniq_idxs
      in
      let* () =
        if fk_constraints = []
        then Lwt.return_unit
        else (
          let fk_list =
            List.map
              (fun (lcs, pt, pcs, od, ou, def) ->
                 Cat.
                   { fk_local_cols = lcs
                   ; fk_parent_table = pt
                   ; fk_parent_cols = pcs
                   ; fk_on_delete = od
                   ; fk_on_update = ou
                   ; fk_deferrable = def
                   })
              fk_constraints
          in
          let* () = Cat.save_fk_constraints ~txn:tx cat ~table_name:name ~fks:fk_list in
          (* Raw, undo-free cache update by design — reverted on ROLLBACK by
             [create_table]'s schema-cache undo, which removes the whole table
             entry.  See [Cat.set_fk_constraints]. *)
          Cat.set_fk_constraints cat ~table_name:name ~fks:fk_list;
          Lwt.return_unit)
      in
      Lwt.return 0)
;;

(* Op_insert: insert each VALUES row, counting successful inserts. *)
let execute_insert_values
      store
      (cat : Cat.t)
      ~mode
      ~params
      ~clock
      ~before_hook
      ~after_hook
      ~on_replace_delete_before
      ~on_replace_delete
      ~on_upsert_update_before
      ~on_upsert_update
      ~table_meta
      ~ordinals
      ~values
      ~on_conflict
      ~upsert_update
  : int Lwt.t
  =
  let bh =
    Option.map
      (fun f ~tx ~new_row -> f ~tx ~new_row:(Some new_row) ~old_row:None)
      before_hook
  in
  let ah =
    Option.map
      (fun f ~tx ~new_row -> f ~tx ~new_row:(Some new_row) ~old_row:None)
      after_hook
  in
  Lwt_list.fold_left_s
    (fun count row_vals ->
       let* inserted =
         execute_insert
           ~mode
           ~params
           ~clock
           ~on_conflict
           ~upsert_update
           ~before_hook:bh
           ~after_hook:ah
           ~on_replace_delete_before
           ~on_replace_delete
           ~on_upsert_update_before
           ~on_upsert_update
           store
           cat
           ~table_meta
           ~ordinals
           ~values:row_vals
       in
       Lwt.return (count + if inserted then 1 else 0))
    0
    values
;;

(* Op_insert_select: insert one row per source-stream row. *)
let execute_insert_select_op
      store
      (cat : Cat.t)
      ~mode
      ~params
      ~clock
      ~before_hook
      ~after_hook
      ~on_replace_delete_before
      ~on_replace_delete
      ~on_upsert_update_before
      ~on_upsert_update
      ~(table_meta : Cat.table_meta)
      ~ordinals
      ~source
      ~on_conflict
  : int Lwt.t
  =
  let n_cols = List.length table_meta.Cat.columns in
  let bh =
    Option.map
      (fun f ~tx ~new_row -> f ~tx ~new_row:(Some new_row) ~old_row:None)
      before_hook
  in
  let ah =
    Option.map
      (fun f ~tx ~new_row -> f ~tx ~new_row:(Some new_row) ~old_row:None)
      after_hook
  in
  let* stream = !to_stream_ref clock params store ~mode ~cat:(Some cat) source in
  let* src_rows = Lwt_stream.to_list stream in
  Lwt_list.fold_left_s
    (fun count src_row ->
       let row_arr = Array.make n_cols Row.V_null in
       List.iteri
         (fun i ord -> if i < Array.length src_row then row_arr.(ord) <- src_row.(i))
         ordinals;
       let* inserted =
         execute_insert
           ~mode
           ~params
           ~clock
           ~on_conflict
           ~before_hook:bh
           ~after_hook:ah
           ~on_replace_delete_before
           ~on_replace_delete
           ~on_upsert_update_before
           ~on_upsert_update
           store
           cat
           ~table_meta
           ~ordinals
           ~values:[]
           ~prebuilt_row:(Some row_arr)
       in
       Lwt.return (count + if inserted then 1 else 0))
    0
    src_rows
;;

(* Op_update dispatch: adapt the new/old-row hooks and delegate to execute_update. *)
let execute_update_op
      store
      cat
      ~mode
      ~params
      ~clock
      ~before_hook
      ~after_hook
      ~table_meta
      ~assignments
      ~where
      ~order
      ~limit
      ~offset
      ~indexes
  : int Lwt.t
  =
  let bh =
    Option.map
      (fun f ~tx ~old_row ~new_row ->
         f ~tx ~new_row:(Some new_row) ~old_row:(Some old_row))
      before_hook
  in
  let ah =
    Option.map
      (fun f ~tx ~old_row ~new_row ->
         f ~tx ~new_row:(Some new_row) ~old_row:(Some old_row))
      after_hook
  in
  execute_update
    ~mode
    ~params
    ~clock
    ~before_hook:bh
    ~after_hook:ah
    store
    cat
    ~table_meta
    ~assignments
    ~where
    ~order
    ~limit
    ~offset
    ~indexes
;;

(* Op_delete dispatch: adapt the old-row hooks and delegate to execute_delete. *)
let execute_delete_op
      store
      cat
      ~mode
      ~params
      ~clock
      ~before_hook
      ~after_hook
      ~table_meta
      ~where
      ~order
      ~limit
      ~offset
      ~indexes
  : int Lwt.t
  =
  let bh =
    Option.map
      (fun f ~tx ~old_row -> f ~tx ~new_row:None ~old_row:(Some old_row))
      before_hook
  in
  let ah =
    Option.map
      (fun f ~tx ~old_row -> f ~tx ~new_row:None ~old_row:(Some old_row))
      after_hook
  in
  execute_delete
    ~mode
    ~params
    ~clock
    ~before_hook:bh
    ~after_hook:ah
    store
    cat
    ~table_meta
    ~where
    ~order
    ~limit
    ~offset
    ~indexes
;;

(* Op_drop_table: drop the table and invalidate its cached CHECK / generated
   expressions. *)
let execute_drop_table_op
      store
      (cat : Cat.t)
      ~mode
      ~(table_meta : Cat.table_meta)
      ~indexes
  : int Lwt.t
  =
  let* () = execute_drop_table ~mode store cat ~table_meta ~_indexes:indexes in
  Hashtbl.filter_map_inplace
    (fun (tbl, _, _) v -> if String.equal tbl table_meta.name then None else Some v)
    check_expr_cache;
  Hashtbl.filter_map_inplace
    (fun (tbl, _, _) v -> if String.equal tbl table_meta.name then None else Some v)
    generated_expr_cache;
  Lwt.return 0
;;

(* Op_fts_insert: allocate a rowid, store the content row, and index it. *)
let execute_fts_insert
      store
      (cat : Cat.t)
      ~mode
      ~clock
      ~params
      (fts_meta : Cat.fts_table_meta)
      ~col_names
      ~col_values
      ~rowid_value
  : int Lwt.t
  =
  let* tx, owned = acquire_txn store mode in
  Lwt.catch
    (fun () ->
       (* #330: an explicit [rowid] is used verbatim (and the high-water advanced
          past it so a later auto-insert never reuses it); otherwise allocate the
          next rowid as before. *)
       let* rowid =
         match rowid_value with
         | None -> Cat.next_fts_rowid_in_txn cat ~name:fts_meta.Cat.fts_name tx
         | Some e ->
           let rowid =
             match eval_expr clock params [||] e with
             | Row.V_int n -> n
             | Row.V_real f -> Int64.of_float f
             | _ -> raise (Failure "FTS rowid must be an integer")
           in
           let* () =
             Cat.ensure_fts_rowid_above_in_txn cat ~name:fts_meta.Cat.fts_name tx rowid
           in
           Lwt.return rowid
       in
       let key = Rowid.encode rowid in
       (* #330: if a row already exists at this rowid (explicit-rowid collision),
          de-index it first so its index entries are not left stale. *)
       let* () =
         match rowid_value with
         | None -> Lwt.return_unit
         | Some _ ->
           let* existing = S.get tx fts_meta.Cat.fts_content_tree key in
           (match existing with
            | None -> Lwt.return_unit
            | Some old_bytes ->
              let old_texts = fts_decode_content old_bytes in
              let old_col_texts = List.mapi (fun i t -> i, t) old_texts in
              fts_deindex_document tx ~fts_meta ~rowid ~col_texts:old_col_texts)
       in
       let vals = List.map (fun e -> eval_expr clock params [||] e) col_values in
       let n_cols = List.length fts_meta.Cat.fts_columns in
       let texts = Array.make n_cols "" in
       List.iter2
         (fun col_name v ->
            match list_find_index (String.equal col_name) fts_meta.Cat.fts_columns with
            | None -> ()
            | Some (i, _) ->
              texts.(i)
              <- (match v with
                  | Row.V_text s -> s
                  | _ -> ""))
         col_names
         vals;
       let text_list = Array.to_list texts in
       let* () =
         S.put tx fts_meta.Cat.fts_content_tree key (fts_encode_content text_list)
       in
       let col_texts = List.mapi (fun i t -> i, t) text_list in
       let* () = fts_index_document tx ~fts_meta ~rowid ~col_texts in
       let* () = release_txn ~cat tx owned in
       mark_dirty fts_meta.Cat.fts_name;
       Lwt.return 1)
    (fun exn ->
       let* () = if owned then S.rollback tx else Lwt.return_unit in
       Lwt.fail exn)
;;

(* Op_fts_delete: drain matching content rows, then delete + de-index them. *)
let execute_fts_delete
      store
      (cat : Cat.t)
      ~mode
      ~clock
      ~params
      (fts_meta : Cat.fts_table_meta)
      ~where
  : int Lwt.t
  =
  ignore cat;
  let* matches =
    S.with_ro store
    @@ fun tx_ro ->
    let* cur = S.cursor_open tx_ro fts_meta.Cat.fts_content_tree in
    let _sr = S.cursor_first cur in
    let buf = ref [] in
    let rec drain () =
      match S.cursor_next cur with
      | None -> ()
      | Some (kbytes, vbytes) ->
        let rowid = Rowid.decode kbytes in
        let texts = fts_decode_content vbytes in
        let row = Array.of_list (List.map (fun s -> Row.V_text s) texts) in
        let keep =
          match where with
          | None -> true
          | Some pred -> value_truthy (eval_expr clock params row pred)
        in
        if keep then buf := (rowid, kbytes, texts) :: !buf;
        drain ()
    in
    drain ();
    S.cursor_close cur;
    Lwt.return (List.rev !buf)
  in
  let n = List.length matches in
  if n = 0
  then Lwt.return 0
  else
    let* tx, owned = acquire_txn store mode in
    Lwt.catch
      (fun () ->
         let* () =
           Lwt_list.iter_s
             (fun (rowid, key, texts) ->
                let col_texts = List.mapi (fun i t -> i, t) texts in
                let* () = S.del tx fts_meta.Cat.fts_content_tree key in
                fts_deindex_document tx ~fts_meta ~rowid ~col_texts)
             matches
         in
         let* () = release_txn ~cat tx owned in
         (* [n > 0] here (the [n = 0] case returned early above). *)
         mark_dirty fts_meta.Cat.fts_name;
         Lwt.return n)
      (fun exn ->
         let* () = if owned then S.rollback tx else Lwt.return_unit in
         Lwt.fail exn)
;;

(* Drop cached CHECK and generated-column expressions for [table_name]
   (used after DROP COLUMN, which can invalidate them). *)
let clear_table_expr_caches table_name =
  let clear cache =
    let to_clear =
      Hashtbl.fold
        (fun (tn, idx, sql) _ acc ->
           if String.equal tn table_name then (tn, idx, sql) :: acc else acc)
        cache
        []
    in
    List.iter (Hashtbl.remove cache) to_clear
  in
  clear check_expr_cache;
  clear generated_expr_cache
;;

(* Convert an AST column definition into a catalog [Row.column]. *)
let column_of_col_def col_def : Row.column =
  { Row.name = col_def.Ast.name
  ; Row.ty =
      (match col_def.Ast.ty with
       | Ast.Ty_int -> Row.Integer
       | Ast.Ty_text -> Row.Text
       | Ast.Ty_real -> Row.Real
       | Ast.Ty_blob -> Row.Blob)
  ; Row.not_null = col_def.Ast.not_null
  ; Row.primary_key = col_def.Ast.primary_key
  ; Row.pk_desc = col_def.Ast.pk_desc
  ; Row.default =
      (match col_def.Ast.default with
       | None -> None
       | Some Ast.L_null -> Some Row.DV_null
       | Some (Ast.L_int n) -> Some (Row.DV_int n)
       | Some (Ast.L_text s) -> Some (Row.DV_text s)
       | Some (Ast.L_real f) -> Some (Row.DV_real f)
       | Some (Ast.L_blob b) -> Some (Row.DV_blob b)
       | Some Ast.L_current_timestamp -> Some Row.DV_current_timestamp
       | Some Ast.L_current_date -> Some Row.DV_current_date
       | Some Ast.L_current_time -> Some Row.DV_current_time)
  ; Row.check_sql = Option.map Ast.expr_to_sql col_def.Ast.check
  ; Row.generated_as =
      Option.map (fun (e, s) -> Ast.expr_to_sql e, s = `Stored) col_def.Ast.generated_as
  }
;;

(* ALTER TABLE ADD COLUMN: add [col_def] to the catalog and persist any inline
   FK reference it declares. *)
let alter_add_column ?txn (cat : Cat.t) ~(table_meta : Cat.table_meta) col_def : int Lwt.t
  =
  let col = column_of_col_def col_def in
  let* result = Cat.add_column ?txn cat ~table_name:table_meta.Cat.name ~column:col in
  match result with
  | Error msg -> Lwt.fail_with msg
  | Ok () ->
    (match col_def.Ast.fk_ref with
     | None -> Lwt.return 0
     | Some (parent_table, parent_col, ast_od, ast_ou, ast_def) ->
       let inferred_parent_col =
         if parent_col = ""
         then (
           match Cat.find_table_cached cat ~name:parent_table with
           | None -> parent_col
           | Some pm ->
             (match
                List.find_opt (fun (c : Row.column) -> c.primary_key) pm.Cat.columns
              with
              | None -> parent_col
              | Some pk -> pk.Row.name))
         else parent_col
       in
       let new_fk : Cat.fk_constraint =
         { Cat.fk_local_cols = [ col_def.Ast.name ]
         ; Cat.fk_parent_table = parent_table
         ; Cat.fk_parent_cols = [ inferred_parent_col ]
         ; Cat.fk_on_delete = ast_od
         ; Cat.fk_on_update = ast_ou
         ; Cat.fk_deferrable = ast_def
         }
       in
       let existing_fks =
         match Cat.find_table_cached cat ~name:table_meta.Cat.name with
         | None -> []
         | Some m -> m.Cat.fk_constraints
       in
       let new_fks = existing_fks @ [ new_fk ] in
       let* () =
         Cat.save_fk_constraints ?txn cat ~table_name:table_meta.Cat.name ~fks:new_fks
       in
       (* The in-memory FK mutation is reverted on ROLLBACK by [add_column]'s
          schema-cache undo, which restores the whole prior [table_meta]. *)
       Cat.set_fk_constraints cat ~table_name:table_meta.Cat.name ~fks:new_fks;
       Lwt.return 0)
;;

(* ALTER TABLE DROP COLUMN: drop dependent indexes, migrate rows to the new
   shape, drop the catalog column, and invalidate cached expressions.

   #282: everything runs through the single writer transaction [tx] supplied by
   [with_ddl_txn] (borrowed from the ambient explicit transaction, or owned in
   autocommit) — no longer three separate [rw_begin]/[with_ro] phases (which
   would self-deadlock inside an explicit transaction).  The row scan reads
   THROUGH [tx] so rows inserted earlier in the same transaction are migrated
   (read-your-own-writes).  Dropped dependent indexes register a schema-cache
   undo so a [ROLLBACK] restores them. *)
let alter_drop_column tx (cat : Cat.t) ~(table_meta : Cat.table_meta) col_name : int Lwt.t
  =
  let table_name = table_meta.Cat.name in
  let col_idx = find_col_idx_by_name table_meta.Cat.columns col_name in
  let new_columns = List.filteri (fun i _ -> i <> col_idx) table_meta.Cat.columns in
  let idxs_on_col =
    List.filter
      (fun (idx : Cat.index_info) -> List.mem col_name idx.Cat.idx_columns)
      (Cat.indexes_for_table cat ~table:table_name)
  in
  let* () =
    Lwt_list.iter_s
      (fun (idx : Cat.index_info) -> Cat.drop_index cat tx ~name:idx.idx_name)
      idxs_on_col
  in
  (* #283: each [Cat.drop_index] above self-registers its own cache undo, so the
     dropped dependent indexes are restored on ROLLBACK without an external block. *)
  (* Drain every row through [tx] (read-your-own-writes) before rewriting, so the
     cursor is closed before we put back the reshaped rows into the same tree. *)
  let alt_tree_id, _, _, _ = Cat.row_storage table_meta in
  let* cur = S.cursor_open tx alt_tree_id in
  let _sr = S.cursor_first cur in
  let rows = ref [] in
  let rec drain () =
    match S.cursor_next cur with
    | None -> ()
    | Some (k, v) ->
      let old_row = decode_with_virtual None [||] table_meta v in
      let new_row =
        Array.of_list (List.filteri (fun i _ -> i <> col_idx) (Array.to_list old_row))
      in
      rows := (Bytes.copy k, new_row) :: !rows;
      drain ()
  in
  drain ();
  S.cursor_close cur;
  let* () =
    Lwt_list.iter_s
      (fun (k, new_row) ->
         let new_bytes = Row.encode new_columns new_row in
         S.put tx alt_tree_id k new_bytes)
      !rows
  in
  let* result = Cat.drop_column ~txn:tx cat ~table_name ~col_name in
  match result with
  | Error msg -> Lwt.fail_with msg
  | Ok () ->
    clear_table_expr_caches table_name;
    Lwt.return 0
;;

(* ALTER TABLE RENAME TABLE: rename in the catalog and remap cached CHECK /
   generated-column entries from the old name to the new one. *)
let alter_rename_table ?txn (cat : Cat.t) ~(table_meta : Cat.table_meta) new_name
  : int Lwt.t
  =
  let* result = Cat.rename_table ?txn cat ~old_name:table_meta.Cat.name ~new_name in
  match result with
  | Error msg -> Lwt.fail_with msg
  | Ok () ->
    let remap tbl_cache =
      let to_add =
        Hashtbl.fold
          (fun (tbl, idx, sql) v acc ->
             if String.equal tbl table_meta.Cat.name
             then (new_name, idx, sql, v) :: acc
             else acc)
          tbl_cache
          []
      in
      List.iter
        (fun (_, idx, sql, _) -> Hashtbl.remove tbl_cache (table_meta.Cat.name, idx, sql))
        to_add;
      List.iter
        (fun (new_t, idx, sql, v) -> Hashtbl.add tbl_cache (new_t, idx, sql) v)
        to_add
    in
    remap check_expr_cache;
    remap generated_expr_cache;
    Lwt.return 0
;;

(* Op_alter_table: dispatch on the ALTER action.

   #282: the ALTER mutators run through [with_ddl_txn], which supplies a single
   writer transaction — borrowed from the ambient explicit transaction
   ([In_txn]) or owned and auto-committed ([Auto]).  Each catalog mutator threads
   that txn (no nested [rw_begin], which previously self-deadlocked inside
   [BEGIN…COMMIT]) and registers a schema-cache undo so a [ROLLBACK] reverts the
   in-memory catalog along with the store. *)
let execute_alter_table store (cat : Cat.t) ~mode ~(table_meta : Cat.table_meta) action
  : int Lwt.t
  =
  with_ddl_txn store cat mode (fun tx ->
    match action with
    | Ast.AA_add_column col_def -> alter_add_column ~txn:tx cat ~table_meta col_def
    | Ast.AA_rename_table new_name -> alter_rename_table ~txn:tx cat ~table_meta new_name
    | Ast.AA_rename_column (old_col, new_col) ->
      let* result =
        Cat.rename_column ~txn:tx cat ~table_name:table_meta.Cat.name ~old_col ~new_col
      in
      (match result with
       | Error msg -> Lwt.fail_with msg
       | Ok () -> Lwt.return 0)
    | Ast.AA_drop_column col_name -> alter_drop_column tx cat ~table_meta col_name)
;;

(* Op_create_index: create the index unless IF NOT EXISTS finds it present. *)
let execute_create_index_op
      store
      (cat : Cat.t)
      ~mode
      ~name
      ~table
      ~tree_id
      ~col_sqls
      ~col_expr_flags
      ~where_expr
      ~where_sql
      ~unique
      ~columns
      ~if_not_exists
  : int Lwt.t
  =
  if if_not_exists && Cat.index_exists cat ~name
  then Lwt.return 0
  else
    let* () =
      execute_create_index
        ~mode
        store
        cat
        ~name
        ~table
        ~tree_id
        ~col_sqls
        ~col_expr_flags
        ~where_expr
        ~where_sql
        ~unique
        ~columns
    in
    Lwt.return 0
;;

(** [execute_with_count] returns the rows-affected count.  For most
    write ops this is 1 (INSERT) or 0 (DDL); for UPDATE it is the
    number of rows whose contents were modified. *)
let execute_with_count
      ?(mode = Auto)
      ?(clock : (unit -> float) option = None)
      ?(params = [||])
      ?(before_hook :
          (tx:S.rw S.txn -> new_row:Row.t option -> old_row:Row.t option -> unit Lwt.t)
            option =
        None)
      ?(after_hook :
          (tx:S.rw S.txn -> new_row:Row.t option -> old_row:Row.t option -> unit Lwt.t)
            option =
        None)
      ?(on_replace_delete_before : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option =
        None)
      ?(on_replace_delete : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option = None)
      ?(on_upsert_update_before :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      ?(on_upsert_update :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      (store : S.t)
      (cat : Cat.t)
      (op : Plan.op)
  : int Lwt.t
  =
  match op with
  | Plan.Op_create_table
      { name
      ; columns
      ; uniq_idxs
      ; if_not_exists
      ; fk_constraints
      ; without_rowid
      ; autoincrement
      } ->
    execute_create_table_op
      store
      cat
      ~mode
      ~name
      ~columns
      ~uniq_idxs
      ~if_not_exists
      ~fk_constraints
      ~without_rowid
      ~autoincrement
  | Plan.Op_col_create_table { name; columns; if_not_exists } ->
    if Cat.table_exists cat ~name
    then
      if if_not_exists
      then Lwt.return 0
      else failwith (Printf.sprintf "table '%s' already exists" name)
    else
      let* () =
        with_ddl_txn store cat mode (fun tx ->
          Cat.create_columnstore_table ~txn:tx cat ~name ~columns)
      in
      Lwt.return 0
  | Plan.Op_insert
      { table_meta; ordinals; values; on_conflict = _; returning = _; upsert_update = _ }
    when Cat.is_columnar table_meta ->
    let* tx, owned = acquire_txn store mode in
    Lwt.catch
      (fun () ->
         let col_store = col_store_of_meta table_meta in
         let n_cols = List.length table_meta.Cat.columns in
         let rows =
           List.map
             (fun vals ->
                let row = Array.make n_cols Row.V_null in
                List.iter2
                  (fun ord expr -> row.(ord) <- eval_expr clock params [||] expr)
                  ordinals
                  vals;
                row)
             values
         in
         Granary_columnar.Col_store.insert_rows col_store (Array.of_list rows);
         let* () = release_txn ~cat tx owned in
         if values <> [] then mark_dirty table_meta.Cat.name;
         Lwt.return (List.length values))
      (fun exn ->
         let* () = if owned then S.rollback tx else Lwt.return_unit in
         Lwt.fail exn)
  | Plan.Op_insert
      { table_meta; ordinals; values; on_conflict; returning = _; upsert_update } ->
    execute_insert_values
      store
      cat
      ~mode
      ~params
      ~clock
      ~before_hook
      ~after_hook
      ~on_replace_delete_before
      ~on_replace_delete
      ~on_upsert_update_before
      ~on_upsert_update
      ~table_meta
      ~ordinals
      ~values
      ~on_conflict
      ~upsert_update
  | Plan.Op_insert_select { table_meta; ordinals; source; on_conflict = _ }
    when Cat.is_columnar table_meta ->
    let col_store = col_store_of_meta table_meta in
    let n_cols = List.length table_meta.Cat.columns in
    let* stream = !to_stream_ref clock params store ~mode ~cat:(Some cat) source in
    let* src_rows = Lwt_stream.to_list stream in
    let batch =
      Array.of_list
        (List.map
           (fun src_row ->
              let dest = Array.make n_cols Row.V_null in
              List.iteri (fun i ord -> dest.(ord) <- src_row.(i)) ordinals;
              dest)
           src_rows)
    in
    let* tx, owned = acquire_txn store mode in
    Lwt.catch
      (fun () ->
         Granary_columnar.Col_store.insert_rows col_store batch;
         let* () = release_txn ~cat tx owned in
         if Array.length batch > 0 then mark_dirty table_meta.Cat.name;
         Lwt.return (Array.length batch))
      (fun exn ->
         let* () = if owned then S.rollback tx else Lwt.return_unit in
         Lwt.fail exn)
  | Plan.Op_insert_select { table_meta; ordinals; source; on_conflict } ->
    execute_insert_select_op
      store
      cat
      ~mode
      ~params
      ~clock
      ~before_hook
      ~after_hook
      ~on_replace_delete_before
      ~on_replace_delete
      ~on_upsert_update_before
      ~on_upsert_update
      ~table_meta
      ~ordinals
      ~source
      ~on_conflict
  | Plan.Op_create_index
      { name
      ; table
      ; tree_id
      ; col_sqls
      ; col_expr_flags
      ; where_expr
      ; where_sql
      ; unique
      ; columns
      ; if_not_exists
      } ->
    execute_create_index_op
      store
      cat
      ~mode
      ~name
      ~table
      ~tree_id
      ~col_sqls
      ~col_expr_flags
      ~where_expr
      ~where_sql
      ~unique
      ~columns
      ~if_not_exists
  | Plan.Op_update
      { table_meta; assignments; where; order; limit; offset; indexes; returning = _ } ->
    if Cat.is_columnar table_meta
    then
      Lwt.fail_with
        (Printf.sprintf
           "UPDATE is not supported on columnar table '%s'"
           table_meta.Cat.name)
    else
      execute_update_op
        store
        cat
        ~mode
        ~params
        ~clock
        ~before_hook
        ~after_hook
        ~table_meta
        ~assignments
        ~where
        ~order
        ~limit
        ~offset
        ~indexes
  | Plan.Op_delete { table_meta; where; order; limit; offset; indexes; returning = _ } ->
    if Cat.is_columnar table_meta
    then
      Lwt.fail_with
        (Printf.sprintf
           "DELETE is not supported on columnar table '%s'"
           table_meta.Cat.name)
    else
      execute_delete_op
        store
        cat
        ~mode
        ~params
        ~clock
        ~before_hook
        ~after_hook
        ~table_meta
        ~where
        ~order
        ~limit
        ~offset
        ~indexes
  | Plan.Op_seq_set { table; seq } ->
    (* #312.1: writable sqlite_sequence SET/INSERT.  Runs through [with_ddl_txn]
       so it participates in any ambient explicit transaction (borrowed [In_txn]
       — reverts on ROLLBACK) or owns its own auto-committed txn ([Auto]). *)
    with_ddl_txn store cat mode (fun tx ->
      let* () = Cat.set_next_rowid_in_txn cat ~name:table ~requested:seq tx in
      (* #317.3: the UPDATE/INSERT form touches exactly one sqlite_sequence row,
         so [changes()] reports 1 — SQLite parity.  (The DELETE/reset form below
         still reports 0: we do not materialise per-table rows to count.) *)
      Lwt.return 1)
  | Plan.Op_seq_reset { table } ->
    (* #312.1: writable sqlite_sequence DELETE.  [None] (bare DELETE, no WHERE)
       resets every AUTOINCREMENT counter — SQLite parity, and what a real
       [sqlite3 .dump] emits before re-INSERTing. *)
    with_ddl_txn store cat mode (fun tx ->
      let* () =
        match table with
        | Some name -> Cat.reset_next_rowid_in_txn cat ~name tx
        | None -> Cat.reset_all_next_rowid_in_txn cat tx
      in
      Lwt.return 0)
  | Plan.Op_drop_table { table_meta; indexes } ->
    execute_drop_table_op store cat ~mode ~table_meta ~indexes
  | Plan.Op_drop_index { idx_info } ->
    let* () = execute_drop_index ~mode store cat ~idx_info in
    Lwt.return 0
  | Plan.Op_create_fts_table { name; columns } ->
    (* #269: thread any ambient explicit txn so CREATE VIRTUAL TABLE … USING fts5
       does not self-deadlock and rolls back atomically. *)
    with_ddl_txn store cat mode (fun tx ->
      let* _ = Cat.create_fts_table ~txn:tx cat ~name ~columns in
      Lwt.return 0)
  | Plan.Op_fts_insert { fts_meta; col_names; col_values; rowid_value } ->
    execute_fts_insert
      store
      cat
      ~mode
      ~clock
      ~params
      fts_meta
      ~col_names
      ~col_values
      ~rowid_value
  | Plan.Op_fts_delete { fts_meta; where } ->
    execute_fts_delete store cat ~mode ~clock ~params fts_meta ~where
  | Plan.Op_alter_table { table_meta; action } ->
    execute_alter_table store cat ~mode ~table_meta action
  | Plan.Op_begin
  | Plan.Op_commit
  | Plan.Op_rollback
  | Plan.Op_savepoint _
  | Plan.Op_release _
  | Plan.Op_rollback_to _ ->
    failwith
      "Exec.execute_with_count: BEGIN/COMMIT/ROLLBACK/SAVEPOINT handled by Db layer"
  | Plan.Op_pragma_rows _ -> Lwt.return 0
  | Plan.Op_pragma_set_user_version { version } ->
    let* tx = S.rw_begin store in
    let* () = Cat.write_user_version_tx tx version in
    let* () = S.commit tx in
    Lwt.return 0
  | Plan.Op_pragma_set_fk { on } ->
    Cat.set_fk_enforcement cat on;
    Lwt.return 0
  | Plan.Op_pragma_set_recursive_triggers { on } ->
    Cat.set_recursive_triggers cat on;
    Lwt.return 0
  | Plan.Op_pragma_set_defer_fk { on } ->
    Cat.set_defer_fks_pragma cat on;
    Lwt.return 0
  | Plan.Op_pragma_wal_checkpoint ->
    let* () = S.checkpoint store in
    Lwt.return 0
  | Plan.Op_pragma_set_wal_autocheckpoint { n } ->
    S.set_wal_autocheckpoint store (Int64.to_int n);
    Lwt.return 0
  | Plan.Op_pragma_set_synchronous { mode } ->
    if mode <> "full" && S.commit_callback_active store
    then
      failwith
        "PRAGMA synchronous: durability cannot be relaxed while a replication \
         commit-sink is active (replication requires synchronous=full)"
    else (
      let d =
        match mode with
        | "full" -> S.Full
        | "off" -> S.Off
        | "batched" ->
          S.Batched
            { commits = S.sync_batch_commits store
            ; interval_ms = S.sync_batch_interval_ms store
            }
        | _ -> failwith (Printf.sprintf "PRAGMA synchronous: unknown mode %s" mode)
      in
      let* () = if mode = "full" then S.flush_unsynced store else Lwt.return_unit in
      S.set_durability store d;
      Lwt.return 0)
  | Plan.Op_pragma_set_wal_batch_commits { n } ->
    S.set_sync_batch_commits store (Int64.to_int n);
    Lwt.return 0
  | Plan.Op_pragma_set_wal_batch_interval_ms { n } ->
    S.set_sync_batch_interval_ms store (Int64.to_int n);
    Lwt.return 0
  | Plan.Op_vacuum ->
    Lwt.fail_with "VACUUM must be executed via Db.execute / Db.vacuum (no Db handle)"
  | Plan.Op_attach _
  | Plan.Op_detach _
  | Plan.Op_database_list
  | Plan.Op_active_database_get
  | Plan.Op_active_database_set _ ->
    Lwt.fail_with
      "ATTACH/DETACH/database_list/active_database must be executed via Db.execute"
  | Plan.Op_create_view _
  | Plan.Op_create_reactive_view _
  | Plan.Op_drop_view _
  | Plan.Op_create_trigger _
  | Plan.Op_drop_trigger _
  | Plan.Op_no_op -> Lwt.return 0
  | Plan.Op_explain _ -> Lwt.return 0
  | Plan.Op_union _
  | Plan.Op_intersect _
  | Plan.Op_except _
  | Plan.Op_const_select _
  | Plan.Op_with_cte _
  | Plan.Op_cte_scan _
  | Plan.Op_window _
  | Plan.Op_pragma_get_user_version
  | Plan.Op_pragma_integrity_check
  | Plan.Op_pragma_get_fk
  | Plan.Op_pragma_get_recursive_triggers
  | Plan.Op_pragma_get_defer_fk
  | Plan.Op_pragma_get_wal_autocheckpoint
  | Plan.Op_changes
  | Plan.Op_last_insert_rowid
  | Plan.Op_total_changes -> failwith "Exec.execute: use Exec.query for read operations"
  | Plan.Op_seq_scan _
  | Plan.Op_col_seq_scan _
  | Plan.Op_filter _
  | Plan.Op_project _
  | Plan.Op_expr_project _
  | Plan.Op_sort _
  | Plan.Op_limit _
  | Plan.Op_index_lookup _
  | Plan.Op_rowid_lookup _
  | Plan.Op_nested_loop_join _
  | Plan.Op_hash_join _
  | Plan.Op_aggregate _
  | Plan.Op_fts_seq_scan _
  | Plan.Op_fts_match_scan _
  | Plan.Op_distinct _
  | Plan.Op_sqlite_master
  | Plan.Op_sqlite_sequence
  | Plan.Op_pragma_get_synchronous
  | Plan.Op_pragma_get_wal_batch_commits
  | Plan.Op_pragma_get_wal_batch_interval_ms ->
    failwith "Exec.execute: use Exec.query for read operations"
;;

(** Compatibility entry point: discards the rows-affected count. *)
let execute
      ?(mode = Auto)
      ?(clock : (unit -> float) option = None)
      ?(params = [||])
      ?(before_hook :
          (tx:S.rw S.txn -> new_row:Row.t option -> old_row:Row.t option -> unit Lwt.t)
            option =
        None)
      ?(after_hook :
          (tx:S.rw S.txn -> new_row:Row.t option -> old_row:Row.t option -> unit Lwt.t)
            option =
        None)
      ?(on_replace_delete_before : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option =
        None)
      ?(on_replace_delete : (tx:S.rw S.txn -> old_row:Row.t -> unit Lwt.t) option = None)
      ?(on_upsert_update_before :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      ?(on_upsert_update :
          (tx:S.rw S.txn -> old_row:Row.t -> new_row:Row.t -> unit Lwt.t) option =
        None)
      (store : S.t)
      (cat : Cat.t)
      (op : Plan.op)
  : unit Lwt.t
  =
  let* _n =
    execute_with_count
      ~mode
      ~clock
      ~params
      ~before_hook
      ~after_hook
      ~on_replace_delete_before
      ~on_replace_delete
      ~on_upsert_update_before
      ~on_upsert_update
      store
      cat
      op
  in
  Lwt.return_unit
;;

(* ------------------------------------------------------------------ *)
(* BM25 scoring helpers                                                 *)
(* ------------------------------------------------------------------ *)

let bm25_score ~k1 ~b ~total_docs ~total_tokens ~n_docs_with_term ~term_freq ~doc_length =
  if total_docs = 0 || n_docs_with_term = 0
  then 0.0
  else (
    let n = Float.of_int total_docs in
    let n_t = Float.of_int n_docs_with_term in
    let tf = Float.of_int term_freq in
    let dl = Float.of_int doc_length in
    let avgdl = Float.of_int total_tokens /. n in
    let idf = Float.log (((n -. n_t +. 0.5) /. (n_t +. 0.5)) +. 1.0) in
    idf *. (tf *. (k1 +. 1.0)) /. (tf +. (k1 *. (1.0 -. b +. (b *. dl /. avgdl)))))
;;

(** Collect all positive (non-negated) terms from a query for BM25. *)
let fts_query_terms query =
  let rec collect = function
    | Fts_query.FQ_term (Fts_query.FT_exact t) -> [ t ]
    | Fts_query.FQ_term (Fts_query.FT_prefix t) -> [ t ]
    | Fts_query.FQ_term (Fts_query.FT_phrase ts) -> ts
    | Fts_query.FQ_and qs | Fts_query.FQ_or qs -> List.concat_map collect qs
    | Fts_query.FQ_not _ -> []
  in
  List.sort_uniq String.compare (collect query)
;;

(** A snippet phrase is the unit SQLite FTS5 reports via xPhraseSize:
    either a single token (exact or prefix) or a multi-token exact
    phrase. The multi-token form requires consecutive token matches
    and is scored ONCE per occurrence (not once per constituent
    token) to mirror SQLite's centering and bm25 behaviour. *)
type snippet_phrase =
  | SP_term of string * [ `Exact | `Prefix ]
  | SP_phrase of string list (* length >= 2; all matched exactly *)

(** Collect snippet phrases from a query in left-to-right order. *)
let fts_query_terms_with_kind query : snippet_phrase list =
  let rec collect = function
    | Fts_query.FQ_term (Fts_query.FT_exact t) -> [ SP_term (t, `Exact) ]
    | Fts_query.FQ_term (Fts_query.FT_prefix t) -> [ SP_term (t, `Prefix) ]
    | Fts_query.FQ_term (Fts_query.FT_phrase ts) ->
      (match ts with
       | [] -> []
       | [ t ] -> [ SP_term (t, `Exact) ]
       | _ -> [ SP_phrase ts ])
    | Fts_query.FQ_and qs | Fts_query.FQ_or qs -> List.concat_map collect qs
    | Fts_query.FQ_not _ -> []
  in
  (* De-duplicate identical phrases (so a query like `foo AND foo` does
     not over-credit token highlights). Preserve first-occurrence order. *)
  let seen = Hashtbl.create 8 in
  let key = function
    | SP_term (t, `Exact) -> "e:" ^ t
    | SP_term (t, `Prefix) -> "p:" ^ t
    | SP_phrase ts -> "P:" ^ String.concat "\x00" ts
  in
  List.filter
    (fun p ->
       let k = key p in
       if Hashtbl.mem seen k
       then false
       else (
         Hashtbl.add seen k ();
         true))
    (collect query)
;;

(** Test whether the phrase at index [pi] matches the token sequence
    starting at [tokens.(i)]. Returns the phrase length on hit (so the
    caller can compute the end position), else [None]. *)
let phrase_match_at
      ~(phrases : snippet_phrase array)
      ~(tokens : Fts_tokenizer.token array)
      (pi : int)
      (i : int)
  : int option
  =
  let n_toks = Array.length tokens in
  let token_at j = tokens.(j).Fts_tokenizer.term in
  match phrases.(pi) with
  | SP_term (t, `Exact) ->
    if i < n_toks && String.equal (token_at i) t then Some 1 else None
  | SP_term (t, `Prefix) ->
    if i < n_toks
    then (
      let tk = token_at i in
      if
        String.length tk >= String.length t
        && String.equal (String.sub tk 0 (String.length t)) t
      then Some 1
      else None)
    else None
  | SP_phrase ts ->
    let len = List.length ts in
    if i + len > n_toks
    then None
    else (
      let rec walk j = function
        | [] -> true
        | t :: rest ->
          if String.equal (token_at (i + j)) t then walk (j + 1) rest else false
      in
      if walk 0 ts then Some len else None)
;;

(** Find the first phrase that matches at token position [i].
    Returns [(phrase_idx, length)] if any. *)
let token_phrase_match
      ~(phrases : snippet_phrase array)
      ~(tokens : Fts_tokenizer.token array)
      (i : int)
  : (int * int) option
  =
  let n = Array.length phrases in
  let rec loop pi =
    if pi >= n
    then None
    else (
      match phrase_match_at ~phrases ~tokens pi i with
      | Some len -> Some (pi, len)
      | None -> loop (pi + 1))
  in
  loop 0
;;

(** Identify FTS5 "sentence start" token positions in a column.
    Position 0 is always a sentence start. Any token preceded (after any
    intervening whitespace) by '.' or ':' also starts a sentence. *)
let fts_sentence_starts ~col_text ~(tokens : Fts_tokenizer.token array) : int array =
  let n = Array.length tokens in
  let buf = Buffer.create 8 in
  for i = 0 to n - 1 do
    let tok = tokens.(i) in
    if i = 0
    then Buffer.add_string buf (string_of_int 0)
    else (
      let start = tok.Fts_tokenizer.start_byte in
      (* Walk backwards skipping ' ', '\t', '\n', '\r'. *)
      let j = ref (start - 1) in
      while
        !j >= 0
        &&
        let c = col_text.[!j] in
        c = ' ' || c = '\t' || c = '\n' || c = '\r'
      do
        decr j
      done;
      if !j >= 0
      then (
        let c = col_text.[!j] in
        if c = '.' || c = ':'
        then (
          if Buffer.length buf > 0 then Buffer.add_char buf ',';
          Buffer.add_string buf (string_of_int i))))
  done;
  if Buffer.length buf = 0
  then [| 0 |]
  else
    Array.of_list
      (List.map int_of_string (String.split_on_char ',' (Buffer.contents buf)))
;;

(** Score a candidate window [i_pos, i_pos + n_token).
    Returns [(score, i_adj)] where:
      - score = 1000 for each new phrase instance seen + 1 for repeats.
      - i_adj = the actual starting position after centering adjustment,
                clamped to [0, n_docsize - n_token] (or 0 if window > doc).
    [a_seen] is reset by the caller before each call.
    [instances] is a sorted list of [(phrase_idx, position, length)] —
    a multi-token phrase counts as a single contiguous instance whose
    extent spans [position, position + length). *)
let fts_snippet_score
      ~(instances : (int * int * int) list)
      ~(a_seen : bool array)
      ~(i_pos : int)
      ~(n_token : int)
      ~(n_docsize : int)
  : int * int
  =
  let i_end = i_pos + n_token in
  let score = ref 0 in
  let i_first = ref (-1) in
  let i_last = ref 0 in
  List.iter
    (fun (ip, io, len) ->
       (* Phrase fully inside the window. SQLite requires the entire
       phrase span to fit; partial overlaps don't count. *)
       if io >= i_pos && io + len <= i_end
       then (
         score := !score + if a_seen.(ip) then 1 else 1000;
         a_seen.(ip) <- true;
         if !i_first < 0 then i_first := io;
         i_last := io + len))
    instances;
  let i_adj =
    if !i_first < 0 then i_pos else !i_first - ((n_token - (!i_last - !i_first)) / 2)
  in
  let i_adj = if i_adj + n_token > n_docsize then n_docsize - n_token else i_adj in
  let i_adj = if i_adj < 0 then 0 else i_adj in
  !score, i_adj
;;

(* Greedy scan for snippet phrase matches: at each token take the first phrase
   that matches, skipping past its length. Returns [(phrase_idx,pos,len)] list. *)
(** Build a highlighted excerpt of [col_text] for the given snippet [spec].
    Replicates SQLite FTS5's snippet() algorithm:
      - For each phrase instance, score the window anchored at its position
        (with centering adjustment), and also the window anchored at the
        latest preceding sentence start (with a +100 or +120 bonus).
      - Pick the (strictly) highest-scoring window; tie → earliest considered.
      - Reconstruct text from byte offsets, wrapping matched tokens (whole
        token for prefix matches) with [start_tag]/[end_tag].
      - Prepend [ellipsis] unless window starts at token 0.
      - Append [ellipsis] unless window covers through the last token.
    [query_terms] is a list of snippet phrases. *)
let snippet_build_instances ~phrases ~tokens ~n_toks =
  let acc = ref [] in
  let i = ref 0 in
  while !i < n_toks do
    match token_phrase_match ~phrases ~tokens !i with
    | None -> incr i
    | Some (ip, len) ->
      acc := (ip, tokens.(!i).Fts_tokenizer.pos, len) :: !acc;
      i := !i + len
  done;
  List.rev !acc
;;

(* No-match snippet: SQLite anchors at sentence start 0 and emits the first
   n_token tokens (no leading ellipsis; trailing ellipsis if doc is longer). *)
let snippet_no_match ~col_text ~tokens ~n_toks ~n_token ~spec =
  if n_toks = 0
  then ""
  else (
    let win_end_excl = min n_toks n_token in
    let last_tok = tokens.(win_end_excl - 1) in
    let prefix_text = String.sub col_text 0 last_tok.Fts_tokenizer.end_byte in
    if win_end_excl >= n_toks then prefix_text else prefix_text ^ spec.Plan.ellipsis)
;;

(* Choose the best snippet window start: score each instance position and each
   preceding sentence start (with a sentence-alignment bonus). *)
(* Score the candidate windows anchored at instance offset [io] — both the
   centered window and (when the column is longer than one window) the latest
   sentence start before [io], with a sentence-alignment bonus — feeding each
   to [consider]. *)
let snippet_score_instance
      ~consider
      ~instances
      ~a_seen
      ~sentence_starts
      ~n_phrases
      ~n_token
      ~n_toks
      io
  =
  (* Non-sentence-aligned: window anchored at this instance, centered. *)
  Array.fill a_seen 0 n_phrases false;
  let score, i_adj =
    fts_snippet_score ~instances ~a_seen ~i_pos:io ~n_token ~n_docsize:n_toks
  in
  consider score i_adj;
  (* Sentence-aligned: latest sentence start strictly before io. *)
  if n_toks > n_token
  then (
    let n_sent = Array.length sentence_starts in
    let jj = ref 0 in
    while !jj < n_sent - 1 && sentence_starts.(!jj + 1) <= io do
      incr jj
    done;
    let s_start = sentence_starts.(!jj) in
    if s_start < io
    then (
      Array.fill a_seen 0 n_phrases false;
      let score, _ =
        fts_snippet_score ~instances ~a_seen ~i_pos:s_start ~n_token ~n_docsize:n_toks
      in
      let bonus = if s_start = 0 then 120 else 100 in
      consider (score + bonus) s_start))
;;

let snippet_best_window ~instances ~tokens ~col_text ~n_phrases ~n_token ~n_toks =
  let a_seen = Array.make (max 1 n_phrases) false in
  let sentence_starts = fts_sentence_starts ~col_text ~tokens in
  let best_score = ref 0 in
  let best_start = ref 0 in
  let consider score start_pos =
    if score > !best_score
    then (
      best_score := score;
      best_start := start_pos)
  in
  List.iter
    (fun (_ip, io, _len) ->
       snippet_score_instance
         ~consider
         ~instances
         ~a_seen
         ~sentence_starts
         ~n_phrases
         ~n_token
         ~n_toks
         io)
    instances;
  !best_start
;;

(* Reconstruct the snippet text for the chosen window, wrapping matched phrase
   instances in start/end tags and emitting leading/trailing ellipses. *)
let snippet_render
      ~col_text
      ~tokens
      ~token_instance_at
      ~i_best_start
      ~n_token
      ~n_toks
      ~spec
  =
  let i_range_end = i_best_start + n_token - 1 in
  let buf = Buffer.create 128 in
  if i_best_start > 0 then Buffer.add_string buf spec.Plan.ellipsis;
  if n_toks > 0
  then (
    let first_in_range = i_best_start in
    let last_in_range = min (n_toks - 1) i_range_end in
    let prev_end = ref tokens.(first_in_range).Fts_tokenizer.start_byte in
    let prev_inst = ref (-1) in
    for i = first_in_range to last_in_range do
      let tok = tokens.(i) in
      let inst = token_instance_at.(i) in
      let gap_len = tok.Fts_tokenizer.start_byte - !prev_end in
      let gap = if gap_len > 0 then String.sub col_text !prev_end gap_len else "" in
      if !prev_inst <> inst
      then (
        (* Close the previous wrap, emit gap outside, open a new wrap if
           entering a phrase instance. *)
        if !prev_inst >= 0 then Buffer.add_string buf spec.Plan.end_tag;
        Buffer.add_string buf gap;
        if inst >= 0 then Buffer.add_string buf spec.Plan.start_tag)
      else
        (* Same wrap state — gap belongs to it (e.g. space inside <b>..</b>). *)
        Buffer.add_string buf gap;
      Buffer.add_string
        buf
        (String.sub
           col_text
           tok.Fts_tokenizer.start_byte
           (tok.Fts_tokenizer.end_byte - tok.Fts_tokenizer.start_byte));
      prev_end := tok.Fts_tokenizer.end_byte;
      prev_inst := inst
    done;
    if !prev_inst >= 0 then Buffer.add_string buf spec.Plan.end_tag;
    (* Trailing: append the rest of the source if the window reaches the last
       token, else a trailing ellipsis. *)
    if i_range_end >= n_toks - 1
    then (
      let last_end = tokens.(last_in_range).Fts_tokenizer.end_byte in
      if last_end < String.length col_text
      then
        Buffer.add_string
          buf
          (String.sub col_text last_end (String.length col_text - last_end)))
    else Buffer.add_string buf spec.Plan.ellipsis);
  Buffer.contents buf
;;

let compute_snippet
      ~col_text
      ~(query_terms : snippet_phrase list)
      ~(spec : Plan.snippet_spec)
  =
  let tokens = Array.of_list (Fts_tokenizer.tokenize_string ~col:0 col_text) in
  let n_toks = Array.length tokens in
  let phrases = Array.of_list query_terms in
  let n_phrases = Array.length phrases in
  let n_token = max 1 spec.Plan.n_tokens in
  let instances = snippet_build_instances ~phrases ~tokens ~n_toks in
  (* Mark each token position with its covering instance index (-1 = none),
     so adjacent occurrences of the same phrase emit separate wraps. *)
  let token_instance_at = Array.make (max 1 n_toks) (-1) in
  List.iteri
    (fun inst_idx (_ip, io, len) ->
       for k = 0 to len - 1 do
         if io + k < n_toks then token_instance_at.(io + k) <- inst_idx
       done)
    instances;
  if instances = [] || n_phrases = 0
  then snippet_no_match ~col_text ~tokens ~n_toks ~n_token ~spec
  else (
    let i_best_start =
      snippet_best_window ~instances ~tokens ~col_text ~n_phrases ~n_token ~n_toks
    in
    snippet_render
      ~col_text
      ~tokens
      ~token_instance_at
      ~i_best_start
      ~n_token
      ~n_toks
      ~spec)
;;

(* ------------------------------------------------------------------ *)
(* substitute_cte: replace Op_cte_scan nodes with Op_pragma_rows       *)
(* to_stream: convert a read op tree into a Row stream                  *)
(* pre_eval_subquery: resolve subquery Plan.expr nodes before row scan  *)
(* ------------------------------------------------------------------ *)

(** Check whether any unresolved subquery nodes remain in a Plan.expr. *)
let rec plan_expr_has_subquery : Plan.expr -> bool = function
  | Plan.P_subquery _ | Plan.P_exists _ | Plan.P_in_select _ -> true
  | Plan.P_binop (_, a, b) -> plan_expr_has_subquery a || plan_expr_has_subquery b
  | Plan.P_not e
  | Plan.P_is_null e
  | Plan.P_is_not_null e
  | Plan.P_neg e
  | Plan.P_bitnot e -> plan_expr_has_subquery e
  | Plan.P_between (x, lo, hi) ->
    plan_expr_has_subquery x || plan_expr_has_subquery lo || plan_expr_has_subquery hi
  | Plan.P_in (x, vs) -> plan_expr_has_subquery x || List.exists plan_expr_has_subquery vs
  | Plan.P_func (_, args) -> List.exists plan_expr_has_subquery args
  | Plan.P_case { scrutinee; branches; else_ } ->
    Option.fold ~none:false ~some:plan_expr_has_subquery scrutinee
    || List.exists
         (fun (c, r) -> plan_expr_has_subquery c || plan_expr_has_subquery r)
         branches
    || Option.fold ~none:false ~some:plan_expr_has_subquery else_
  | Plan.P_cast (e, _) -> plan_expr_has_subquery e
  | Plan.P_collate (e, _) -> plan_expr_has_subquery e
  | _ -> false
;;

(** Extract table_meta from the leftmost seq scan in a plan op. *)
let rec get_outer_scan_meta : Plan.op -> Cat.table_meta option = function
  | Plan.Op_seq_scan { table_meta } -> Some table_meta
  | Plan.Op_col_seq_scan { table_meta } -> Some table_meta
  | Plan.Op_filter { child; _ } -> get_outer_scan_meta child
  | Plan.Op_sort { child; _ } -> get_outer_scan_meta child
  | Plan.Op_limit { child; _ } -> get_outer_scan_meta child
  | Plan.Op_index_lookup { table_meta; _ } -> Some table_meta
  | Plan.Op_rowid_lookup { table_meta; _ } -> Some table_meta
  | _ -> None
;;

(** Substitute outer column refs (table.col) with literal values from the outer row. *)
let rec substitute_outer_in_expr (meta : Cat.table_meta) (row : Row.t) (e : Ast.expr)
  : Ast.expr
  =
  let go = substitute_outer_in_expr meta row in
  match e with
  | Ast.E_tbl_col (tbl, col) when String.equal tbl meta.Cat.name ->
    (try
       let i = find_col_idx_by_name meta.Cat.columns col in
       Ast.E_lit (value_to_literal row.(i))
     with
     | Failure _ -> e)
  | Ast.E_binop (op, a, b) -> Ast.E_binop (op, go a, go b)
  | Ast.E_not a -> Ast.E_not (go a)
  | Ast.E_is_null a -> Ast.E_is_null (go a)
  | Ast.E_is_not_null a -> Ast.E_is_not_null (go a)
  | Ast.E_neg a -> Ast.E_neg (go a)
  | Ast.E_bitnot a -> Ast.E_bitnot (go a)
  | Ast.E_between (x, lo, hi) -> Ast.E_between (go x, go lo, go hi)
  | Ast.E_in (x, vals) -> Ast.E_in (go x, List.map go vals)
  | Ast.E_func (f, args) -> Ast.E_func (f, List.map go args)
  | Ast.E_cast (x, ty) -> Ast.E_cast (go x, ty)
  | Ast.E_case { scrutinee; branches; else_ } ->
    Ast.E_case
      { scrutinee = Option.map go scrutinee
      ; branches = List.map (fun (c, r) -> go c, go r) branches
      ; else_ = Option.map go else_
      }
  | _ -> e
;;

(** Substitute outer column refs in any embedded Ast.stmt nodes inside a
    Plan.expr (correlated subqueries / EXISTS / IN). *)
let rec substitute_outer_in_plan_expr
          (meta : Cat.table_meta)
          (row : Row.t)
          (e : Plan.expr)
  : Plan.expr
  =
  let go = substitute_outer_in_plan_expr meta row in
  match e with
  | Plan.P_exists inner -> Plan.P_exists (substitute_outer_in_stmt meta row inner)
  | Plan.P_in_select (x, inner) ->
    Plan.P_in_select (go x, substitute_outer_in_stmt meta row inner)
  | Plan.P_subquery inner -> Plan.P_subquery (substitute_outer_in_stmt meta row inner)
  | Plan.P_binop (op, a, b) -> Plan.P_binop (op, go a, go b)
  | Plan.P_not a -> Plan.P_not (go a)
  | Plan.P_is_null a -> Plan.P_is_null (go a)
  | Plan.P_is_not_null a -> Plan.P_is_not_null (go a)
  | Plan.P_neg a -> Plan.P_neg (go a)
  | Plan.P_bitnot a -> Plan.P_bitnot (go a)
  | Plan.P_between (x, lo, hi) -> Plan.P_between (go x, go lo, go hi)
  | Plan.P_in (x, vs) -> Plan.P_in (go x, List.map go vs)
  | Plan.P_func (f, args) -> Plan.P_func (f, List.map go args)
  | Plan.P_case { scrutinee; branches; else_ } ->
    Plan.P_case
      { scrutinee = Option.map go scrutinee
      ; branches = List.map (fun (c, r) -> go c, go r) branches
      ; else_ = Option.map go else_
      }
  | Plan.P_cast (e, ty) -> Plan.P_cast (go e, ty)
  | _ -> e

(** Apply substitute_outer_in_expr to WHERE/HAVING/JOIN ON clauses in an AST stmt. *)
and substitute_outer_in_stmt (meta : Cat.table_meta) (row : Row.t) (s : Ast.stmt)
  : Ast.stmt
  =
  let go_e = substitute_outer_in_expr meta row in
  let go_s = substitute_outer_in_stmt meta row in
  match s with
  | Ast.S_select r ->
    Ast.S_select
      { r with
        where = Option.map go_e r.where
      ; having = Option.map go_e r.having
      ; joins = List.map (fun j -> { j with Ast.on = go_e j.Ast.on }) r.joins
      }
  | Ast.S_compound { op; left; right; order; limit; offset } ->
    Ast.S_compound { op; left = go_s left; right = go_s right; order; limit; offset }
  | Ast.S_with_cte { name; def; query; recursive } ->
    Ast.S_with_cte { name; def = go_s def; query = go_s query; recursive }
  | _ -> s
;;

let rec substitute_cte ~(cte_name : string) ~(rows : Row.t list) (op : Plan.op) : Plan.op =
  let go = substitute_cte ~cte_name ~rows in
  match op with
  | Plan.Op_cte_scan { cte_name = n; _ } when String.equal n cte_name ->
    Plan.Op_pragma_rows { rows }
  | Plan.Op_filter r -> Plan.Op_filter { r with child = go r.child }
  | Plan.Op_project r -> Plan.Op_project { r with child = go r.child }
  | Plan.Op_expr_project r -> Plan.Op_expr_project { r with child = go r.child }
  | Plan.Op_sort r -> Plan.Op_sort { r with child = go r.child }
  | Plan.Op_limit r -> Plan.Op_limit { r with child = go r.child }
  | Plan.Op_distinct r -> Plan.Op_distinct { child = go r.child }
  | Plan.Op_aggregate r -> Plan.Op_aggregate { r with child = go r.child }
  | Plan.Op_nested_loop_join r -> Plan.Op_nested_loop_join { r with left = go r.left }
  | Plan.Op_hash_join r ->
    Plan.Op_hash_join { r with left = go r.left; right = go r.right }
  | Plan.Op_union r -> Plan.Op_union { r with left = go r.left; right = go r.right }
  | Plan.Op_intersect r -> Plan.Op_intersect { left = go r.left; right = go r.right }
  | Plan.Op_except r -> Plan.Op_except { left = go r.left; right = go r.right }
  | Plan.Op_with_cte r when not (String.equal r.cte_name cte_name) ->
    Plan.Op_with_cte { r with query = go r.query }
  | Plan.Op_window r -> Plan.Op_window { r with child = go r.child }
  | Plan.Op_insert_select ({ source; _ } as r) ->
    Plan.Op_insert_select { r with source = go source }
  | _ -> op
;;

(* BM25-score FTS [matches] against [query] when rank is requested; otherwise
   tag each with score 0.0.  Each term's own per-doc term-frequency is used.
   Standalone (not in the [to_stream] rec group) so it stays polymorphic in the
   txn kind — #262 calls it with either a borrowed RW txn or a fresh RO snap. *)
let fts_score_matches tx (fts_meta : Cat.fts_table_meta) query matches include_rank =
  if not include_rank
  then Lwt.return (List.map (fun (rowid, positions) -> rowid, positions, 0.0) matches)
  else
    let* total_docs, total_tokens = read_fts_stats tx fts_meta.Cat.fts_index_tree in
    let query_terms = fts_query_terms query in
    let* term_data =
      Lwt_list.map_s
        (fun term ->
           let* pl = fts_posting_list tx ~index_tree:fts_meta.Cat.fts_index_tree term in
           Lwt.return (List.length pl, pl))
        query_terms
    in
    let* doc_lengths =
      Lwt_list.map_s
        (fun (rowid, positions) ->
           let dlen_key = fts_doclen_key rowid in
           let* v = S.get tx fts_meta.Cat.fts_index_tree dlen_key in
           let dl =
             match v with
             | None -> 1
             | Some b ->
               let n, _ = Varint.decode_uint64 b 0 in
               Int64.to_int n
           in
           Lwt.return (rowid, positions, dl))
        matches
    in
    let scored =
      List.map
        (fun (rowid, positions, dl) ->
           let score =
             List.fold_left
               (fun acc (n_docs, term_pl) ->
                  let tf =
                    match List.assoc_opt rowid term_pl with
                    | None -> 0
                    | Some pos -> List.length pos
                  in
                  acc
                  +. bm25_score
                       ~k1:1.2
                       ~b:0.75
                       ~total_docs
                       ~total_tokens
                       ~n_docs_with_term:n_docs
                       ~term_freq:tf
                       ~doc_length:dl)
               0.0
               term_data
           in
           rowid, positions, score)
        doc_lengths
    in
    Lwt.return scored
;;

let rec pre_eval_subquery
          (clock : (unit -> float) option)
          (store : S.t)
          (params : Row.value array)
          (cat_opt : Cat.t option)
          (e : Plan.expr)
  : Plan.expr Lwt.t
  =
  match e with
  | Plan.P_subquery inner_ast ->
    eval_scalar_subquery clock store params cat_opt e inner_ast
  | Plan.P_exists inner_ast -> eval_exists_subquery clock store params cat_opt e inner_ast
  | Plan.P_in_select (x, inner_ast) ->
    eval_in_select clock store params cat_opt e x inner_ast
  | Plan.P_binop (op, a, b) ->
    let* a' = pre_eval_subquery clock store params cat_opt a in
    let* b' = pre_eval_subquery clock store params cat_opt b in
    Lwt.return (Plan.P_binop (op, a', b'))
  | Plan.P_not a ->
    let* a' = pre_eval_subquery clock store params cat_opt a in
    Lwt.return (Plan.P_not a')
  | Plan.P_is_null a ->
    let* a' = pre_eval_subquery clock store params cat_opt a in
    Lwt.return (Plan.P_is_null a')
  | Plan.P_is_not_null a ->
    let* a' = pre_eval_subquery clock store params cat_opt a in
    Lwt.return (Plan.P_is_not_null a')
  | Plan.P_neg a ->
    let* a' = pre_eval_subquery clock store params cat_opt a in
    Lwt.return (Plan.P_neg a')
  | Plan.P_bitnot a ->
    let* a' = pre_eval_subquery clock store params cat_opt a in
    Lwt.return (Plan.P_bitnot a')
  | Plan.P_between (x, lo, hi) ->
    let* x' = pre_eval_subquery clock store params cat_opt x in
    let* lo' = pre_eval_subquery clock store params cat_opt lo in
    let* hi' = pre_eval_subquery clock store params cat_opt hi in
    Lwt.return (Plan.P_between (x', lo', hi'))
  | Plan.P_in (x, vals) ->
    let* x' = pre_eval_subquery clock store params cat_opt x in
    let* vals' = Lwt_list.map_s (pre_eval_subquery clock store params cat_opt) vals in
    Lwt.return (Plan.P_in (x', vals'))
  | Plan.P_func (f, args) ->
    let* args' = Lwt_list.map_s (pre_eval_subquery clock store params cat_opt) args in
    Lwt.return (Plan.P_func (f, args'))
  | Plan.P_case { scrutinee; branches; else_ } ->
    let* scrutinee' =
      match scrutinee with
      | None -> Lwt.return None
      | Some e ->
        let+ e' = pre_eval_subquery clock store params cat_opt e in
        Some e'
    in
    let* branches' =
      Lwt_list.map_s
        (fun (cond, res) ->
           let* cond' = pre_eval_subquery clock store params cat_opt cond in
           let+ res' = pre_eval_subquery clock store params cat_opt res in
           cond', res')
        branches
    in
    let+ else_' =
      match else_ with
      | None -> Lwt.return None
      | Some e ->
        let+ e' = pre_eval_subquery clock store params cat_opt e in
        Some e'
    in
    Plan.P_case { scrutinee = scrutinee'; branches = branches'; else_ = else_' }
  | Plan.P_cast (e, ty) ->
    let* e' = pre_eval_subquery clock store params cat_opt e in
    Lwt.return (Plan.P_cast (e', ty))
  | Plan.P_collate (e, c) ->
    let* e' = pre_eval_subquery clock store params cat_opt e in
    Lwt.return (Plan.P_collate (e', c))
  | _ -> Lwt.return e

(* Scalar subquery: run [inner_ast], yield its first column's first value as a
   literal (NULL if empty); returns [e] unchanged if it fails to bind. *)
and eval_scalar_subquery clock store params cat_opt (e : Plan.expr) inner_ast
  : Plan.expr Lwt.t
  =
  match cat_opt with
  | None -> Lwt.return (Plan.P_lit Ast.L_null)
  | Some cat ->
    let* bound_r = Sema.bind cat inner_ast in
    (match bound_r with
     | Error _ -> Lwt.return e
     | Ok bound ->
       let op = Planner.plan ~cat bound in
       (* #262: run the subquery under the active txn (read-your-own-writes). *)
       let* stream =
         to_stream clock params store ~mode:(current_txn_mode ()) ~cat:(Some cat) op
       in
       let* rows = Lwt_stream.to_list stream in
       let v =
         match rows with
         | [] -> Ast.L_null
         | row :: _ when Array.length row >= 1 -> value_to_literal row.(0)
         | _ -> Ast.L_null
       in
       Lwt.return (Plan.P_lit v))

(* EXISTS subquery: 1 if [inner_ast] yields any row, else 0. *)
and eval_exists_subquery clock store params cat_opt (e : Plan.expr) inner_ast
  : Plan.expr Lwt.t
  =
  match cat_opt with
  | None -> Lwt.return (Plan.P_lit (Ast.L_int 0L))
  | Some cat ->
    let* bound_r = Sema.bind cat inner_ast in
    (match bound_r with
     | Error _ -> Lwt.return e
     | Ok bound ->
       let op = Planner.plan ~cat bound in
       (* #262: run the subquery under the active txn (read-your-own-writes). *)
       let* stream =
         to_stream clock params store ~mode:(current_txn_mode ()) ~cat:(Some cat) op
       in
       let* first = Lwt_stream.get stream in
       Lwt.return (Plan.P_lit (Ast.L_int (if first = None then 0L else 1L))))

(* IN (subquery): materialize [inner_ast]'s first column into the IN value list. *)
and eval_in_select clock store params cat_opt (e : Plan.expr) x inner_ast
  : Plan.expr Lwt.t
  =
  match cat_opt with
  | None -> Lwt.return (Plan.P_in (x, []))
  | Some cat ->
    let* bound_r = Sema.bind cat inner_ast in
    (match bound_r with
     | Error _ -> Lwt.return e
     | Ok bound ->
       let op = Planner.plan ~cat bound in
       (* #262: run the subquery under the active txn (read-your-own-writes). *)
       let* stream =
         to_stream clock params store ~mode:(current_txn_mode ()) ~cat:(Some cat) op
       in
       let* rows = Lwt_stream.to_list stream in
       let vals =
         List.filter_map
           (fun row ->
              if Array.length row >= 1
              then Some (Plan.P_lit (value_to_literal row.(0)))
              else None)
           rows
       in
       let* x' = pre_eval_subquery clock store params cat_opt x in
       Lwt.return (Plan.P_in (x', vals)))

(* ------------------------------------------------------------------ *)
(* Window function helpers                                              *)
(* ------------------------------------------------------------------ *)

and eval_partition_key clock params (row : Row.t) (partition_by : Plan.expr list)
  : Row.value list
  =
  List.map (eval_expr clock params row) partition_by

and partition_keys_equal (a : Row.value list) (b : Row.value list) : bool =
  List.length a = List.length b && List.for_all2 (fun x y -> compare_values x y = 0) a b

and group_by_partition
      clock
      params
      (partition_by : Plan.expr list)
      (indexed_rows : (int * Row.t) list)
  : (Row.value list * (int * Row.t) list) list
  =
  List.fold_left
    (fun acc (idx, row) ->
       let key = eval_partition_key clock params row partition_by in
       match List.find_opt (fun (k, _) -> partition_keys_equal k key) acc with
       | Some _ ->
         List.map
           (fun (k, pairs) ->
              if partition_keys_equal k key then k, pairs @ [ idx, row ] else k, pairs)
           acc
       | None -> acc @ [ key, [ idx, row ] ])
    []
    indexed_rows

and sort_partition_by
      clock
      params
      (order_by : (Plan.expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list)
      (indexed_rows : (int * Row.t) list)
  : (int * Row.t) list
  =
  if order_by = []
  then indexed_rows
  else
    List.sort
      (fun (_, ra) (_, rb) ->
         let rec cmp = function
           | [] -> 0
           | (e, dir, nulls) :: rest ->
             let va = eval_expr clock params ra e in
             let vb = eval_expr clock params rb e in
             let c = compare_with_nulls dir nulls va vb in
             if c <> 0 then c else cmp rest
         in
         cmp order_by)
      indexed_rows

and win_rank
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  let cur_rank = ref 1 in
  for pos = 0 to n - 1 do
    if pos > 0
    then (
      let order_changed =
        List.exists
          (fun (e, dir, nulls) ->
             compare_with_nulls
               dir
               nulls
               (eval_expr clock params sorted_rows.(pos) e)
               (eval_expr clock params sorted_rows.(pos - 1) e)
             <> 0)
          wplan.Plan.order_by
      in
      if order_changed then cur_rank := pos + 1);
    results.(sorted_orig_idxs.(pos)) <- Row.V_int (Int64.of_int !cur_rank)
  done

and win_dense_rank
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  let cur_rank = ref 1 in
  for pos = 0 to n - 1 do
    if pos > 0
    then (
      let order_changed =
        List.exists
          (fun (e, dir, nulls) ->
             compare_with_nulls
               dir
               nulls
               (eval_expr clock params sorted_rows.(pos) e)
               (eval_expr clock params sorted_rows.(pos - 1) e)
             <> 0)
          wplan.Plan.order_by
      in
      if order_changed then incr cur_rank);
    results.(sorted_orig_idxs.(pos)) <- Row.V_int (Int64.of_int !cur_rank)
  done

and win_ntile
      clock
      params
      (wplan : Plan.window_plan_item)
      _sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  let n_buckets =
    match wplan.Plan.args with
    | [ e ] ->
      (match eval_expr clock params [||] e with
       | Row.V_int k -> Int64.to_int k
       | _ -> 1)
    | _ -> 1
  in
  let n_buckets = max 1 n_buckets in
  for pos = 0 to n - 1 do
    let bucket = (pos * n_buckets / n) + 1 in
    results.(sorted_orig_idxs.(pos)) <- Row.V_int (Int64.of_int bucket)
  done

and win_lag_lead
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  let is_lag = wplan.Plan.func = Ast.WF_lag in
  let offset =
    match wplan.Plan.args with
    | _ :: e :: _ ->
      (match eval_expr clock params [||] e with
       | Row.V_int k -> Int64.to_int k
       | _ -> 1)
    | _ -> 1
  in
  let default_expr =
    match wplan.Plan.args with
    | _ :: _ :: e :: _ -> Some e
    | _ -> None
  in
  for pos = 0 to n - 1 do
    let src_pos = if is_lag then pos - offset else pos + offset in
    let v =
      if src_pos >= 0 && src_pos < n
      then (
        match wplan.Plan.args with
        | e :: _ -> eval_expr clock params sorted_rows.(src_pos) e
        | [] -> Row.V_null)
      else (
        match default_expr with
        | Some e -> eval_expr clock params sorted_rows.(pos) e
        | None -> Row.V_null)
    in
    results.(sorted_orig_idxs.(pos)) <- v
  done

and win_first_value
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  let arg_expr =
    match wplan.Plan.args with
    | e :: _ -> e
    | [] -> failwith "FIRST_VALUE requires one argument"
  in
  let first_val =
    if n > 0 then eval_expr clock params sorted_rows.(0) arg_expr else Row.V_null
  in
  for pos = 0 to n - 1 do
    results.(sorted_orig_idxs.(pos)) <- first_val
  done

and win_nth_value
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  let arg_expr =
    match wplan.Plan.args with
    | e :: _ -> e
    | [] -> failwith "NTH_VALUE requires at least one argument"
  in
  let n_arg =
    match wplan.Plan.args with
    | _ :: e :: _ ->
      (match eval_expr clock params [||] e with
       | Row.V_int k -> Int64.to_int k
       | _ -> 1)
    | _ -> 1
  in
  for pos = 0 to n - 1 do
    let v =
      if n_arg >= 1 && n_arg <= pos + 1
      then eval_expr clock params sorted_rows.(n_arg - 1) arg_expr
      else Row.V_null
    in
    results.(sorted_orig_idxs.(pos)) <- v
  done

and win_percent_rank
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  (* PERCENT_RANK = peer_group_start / (n - 1).  Positional adjacency in the
     already-direction-sorted array, so DESC works without knowing direction. *)
  if n = 0
  then ()
  else (
    let peer_start = ref 0 in
    for pos = 0 to n - 1 do
      if pos > 0
      then (
        let order_changed =
          List.exists
            (fun (e, dir, nulls) ->
               compare_with_nulls
                 dir
                 nulls
                 (eval_expr clock params sorted_rows.(pos) e)
                 (eval_expr clock params sorted_rows.(pos - 1) e)
               <> 0)
            wplan.Plan.order_by
        in
        if order_changed then peer_start := pos);
      let pct =
        if n <= 1 then 0.0 else Float.of_int !peer_start /. Float.of_int (n - 1)
      in
      results.(sorted_orig_idxs.(pos)) <- Row.V_real pct
    done)

and win_cume_dist
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
  =
  (* CUME_DIST = (last position in peer group + 1) / n.  Positional adjacency
     in the already-direction-sorted array, so DESC works correctly. *)
  if n = 0
  then ()
  else (
    let pos = ref 0 in
    while !pos < n do
      let peer_end = ref !pos in
      while
        !peer_end + 1 < n
        && List.for_all
             (fun (e, dir, nulls) ->
                compare_with_nulls
                  dir
                  nulls
                  (eval_expr clock params sorted_rows.(!peer_end + 1) e)
                  (eval_expr clock params sorted_rows.(!peer_end) e)
                = 0)
             wplan.Plan.order_by
      do
        incr peer_end
      done;
      let cd = Float.of_int (!peer_end + 1) /. Float.of_int n in
      for i = !pos to !peer_end do
        results.(sorted_orig_idxs.(i)) <- Row.V_real cd
      done;
      pos := !peer_end + 1
    done)

(* Compute one aggregate-window value over the rows in [indices]. *)
and win_agg_over_frame
      agg_func
      (arg_expr : Plan.expr option)
      (arg_vals : Row.value array)
      indices
  : Row.value
  =
  match agg_func with
  | Ast.Agg_count ->
    let cnt =
      if arg_expr = None
      then List.length indices
      else List.length (List.filter (fun i -> not (arg_vals.(i) = Row.V_null)) indices)
    in
    Row.V_int (Int64.of_int cnt)
  | Ast.Agg_sum ->
    List.fold_left
      (fun acc i ->
         match acc, arg_vals.(i) with
         | _, Row.V_null -> acc
         | Row.V_null, v -> v
         | Row.V_int a, Row.V_int b -> Row.V_int (Int64.add a b)
         | Row.V_real a, Row.V_real b -> Row.V_real (a +. b)
         | Row.V_int a, Row.V_real b -> Row.V_real (Int64.to_float a +. b)
         | Row.V_real a, Row.V_int b -> Row.V_real (a +. Int64.to_float b)
         | _, _ -> acc)
      Row.V_null
      indices
  | Ast.Agg_avg ->
    let vals =
      List.filter_map
        (fun i ->
           match arg_vals.(i) with
           | Row.V_int n -> Some (Int64.to_float n)
           | Row.V_real f -> Some f
           | _ -> None)
        indices
    in
    if vals = []
    then Row.V_null
    else Row.V_real (List.fold_left ( +. ) 0.0 vals /. float_of_int (List.length vals))
  | Ast.Agg_min ->
    List.fold_left
      (fun acc i ->
         match arg_vals.(i) with
         | Row.V_null -> acc
         | v ->
           (match acc with
            | Row.V_null -> v
            | acc_v -> if compare_values v acc_v < 0 then v else acc_v))
      Row.V_null
      indices
  | Ast.Agg_max ->
    List.fold_left
      (fun acc i ->
         match arg_vals.(i) with
         | Row.V_null -> acc
         | v ->
           (match acc with
            | Row.V_null -> v
            | acc_v -> if compare_values v acc_v > 0 then v else acc_v))
      Row.V_null
      indices
  | Ast.Agg_group_concat sep ->
    let separator = Option.value sep ~default:"," in
    let parts =
      List.filter_map
        (fun i ->
           match arg_vals.(i) with
           | Row.V_null -> None
           | Row.V_int n -> Some (Int64.to_string n)
           | Row.V_real f -> Some (Printf.sprintf "%.17g" f)
           | Row.V_text s -> Some s
           | Row.V_blob _ -> Some "")
        indices
    in
    if parts = [] then Row.V_null else Row.V_text (String.concat separator parts)

and win_aggregate
      clock
      params
      (wplan : Plan.window_plan_item)
      sorted_rows
      sorted_orig_idxs
      (results : Row.value array)
      n
      agg_func
  =
  let has_order = wplan.Plan.order_by <> [] in
  let arg_expr =
    match wplan.Plan.args with
    | e :: _ -> Some e
    | [] -> None
  in
  let arg_vals =
    Array.init n (fun pos ->
      match arg_expr with
      | Some e -> eval_expr clock params sorted_rows.(pos) e
      | None -> Row.V_null)
  in
  let resolve_bound bound pos =
    match bound with
    | Ast.FB_unbounded_preceding -> 0
    | Ast.FB_preceding k -> max 0 (pos - k)
    | Ast.FB_current_row -> pos
    | Ast.FB_following k -> min (n - 1) (pos + k)
    | Ast.FB_unbounded_following -> n - 1
  in
  for pos = 0 to n - 1 do
    let frame_start, frame_end =
      match wplan.Plan.frame with
      | None ->
        (* Default: UNBOUNDED PRECEDING AND CURRENT ROW with ORDER BY, else
           UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. *)
        let fe = if has_order then pos else n - 1 in
        0, fe
      | Some spec ->
        (* RANGE numeric bounds approximated as ROWS — full value-based RANGE
           semantics not implemented. *)
        resolve_bound spec.Ast.start pos, resolve_bound spec.Ast.end_ pos
    in
    let frame_start = max 0 frame_start in
    let frame_end = min (n - 1) frame_end in
    let indices =
      if frame_start > frame_end
      then []
      else List.init (frame_end - frame_start + 1) (fun i -> frame_start + i)
    in
    results.(sorted_orig_idxs.(pos))
    <- win_agg_over_frame agg_func arg_expr arg_vals indices
  done

and compute_window_for_partition
      clock
      params
      (wplan : Plan.window_plan_item)
      (sorted_indexed : (int * Row.t) list)
      (n_total : int)
  : Row.value array
  =
  let results = Array.make n_total Row.V_null in
  let sorted_rows = Array.of_list (List.map snd sorted_indexed) in
  let sorted_orig_idxs = Array.of_list (List.map fst sorted_indexed) in
  let n = Array.length sorted_rows in
  (match wplan.Plan.func with
   | Ast.WF_row_number ->
     for pos = 0 to n - 1 do
       results.(sorted_orig_idxs.(pos)) <- Row.V_int (Int64.of_int (pos + 1))
     done
   | Ast.WF_rank -> win_rank clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_dense_rank ->
     win_dense_rank clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_ntile -> win_ntile clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_lag | Ast.WF_lead ->
     win_lag_lead clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_first_value ->
     win_first_value clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_last_value ->
     let arg_expr =
       match wplan.Plan.args with
       | e :: _ -> e
       | [] -> failwith "LAST_VALUE requires one argument"
     in
     for pos = 0 to n - 1 do
       results.(sorted_orig_idxs.(pos))
       <- eval_expr clock params sorted_rows.(pos) arg_expr
     done
   | Ast.WF_nth_value ->
     win_nth_value clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_percent_rank ->
     win_percent_rank clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_cume_dist ->
     win_cume_dist clock params wplan sorted_rows sorted_orig_idxs results n
   | Ast.WF_agg agg_func ->
     win_aggregate clock params wplan sorted_rows sorted_orig_idxs results n agg_func);
  results

and stream_seq_scan clock params store mode (table_meta : Cat.table_meta) =
  (* #239: captured at construction (inside [query]'s [with_value] scope). *)
  let s_opt = Lwt.get query_stats_key in
  (* #262: read through the active txn when one is open, so the scan observes
     the transaction's own uncommitted writes. *)
  let* rh = rh_begin store mode in
  (* #238: stream the leaves natively via [seek_ge ""] instead of
     [cursor_open], which drains the WHOLE tree into an OCaml list at open time
     (every (key,value) pair held simultaneously) before the first row is read.
     That eager drain — not value boxing — was the bulk of the scan pipeline's
     per-row allocation (~9.2 KB/row, vs ~1.5 KB at the streaming storage floor
     and ~0.6 KB for the row decode itself).  [seek_ge ""] descends in O(log n)
     and materialises only the entries actually pulled.  [Bytes.empty] is the
     minimum key, so the first [seek_next] returns the first row — matching the
     old [cursor_first]+[cursor_next] semantics. *)
  let seq_tree_id, _, _, _ = Cat.row_storage table_meta in
  let* cur = rh_seek_ge rh seq_tree_id Bytes.empty in
  (* Snapshot lifetime tied to the stream: end on exhaustion OR a mid-scan read
     error so a corrupt page can't leak locks/refcounts/pins (#164). Idempotent. *)
  let ended = ref false in
  let finish () =
    if !ended
    then Lwt.return_unit
    else (
      ended := true;
      S.seek_close cur;
      rh_finish rh)
  in
  let stream =
    Lwt_stream.from (fun () ->
      Lwt.catch
        (fun () ->
           let* kv = S.seek_next cur in
           match kv with
           | None ->
             let%lwt () = finish () in
             Lwt.return_none
           | Some (_key, vbytes) ->
             incr_examined s_opt;
             let row = decode_with_virtual clock params table_meta vbytes in
             Lwt.return_some row)
        (fun exn ->
           let%lwt () = finish () in
           Lwt.fail exn))
  in
  Lwt.return stream

and stream_col_seq_scan _clock _params _store _mode (table_meta : Cat.table_meta) =
  let col_store = col_store_of_meta table_meta in
  let seq = Granary_columnar.Col_store.to_row_seq col_store in
  Lwt.return (Lwt_stream.of_list (List.of_seq seq))

and stream_filter clock params store mode cat pred child =
  (* #257: a correlated subquery in the predicate is re-evaluated per row at
     pull time — outside [query]'s [with_value] scope — so capture the active
     stats here and re-establish the scope around the inner evaluation, letting
     the subquery's leaf scanners attribute their reads to this query. *)
  let s_opt = Lwt.get query_stats_key in
  let* child_stream = to_stream clock params store ~mode ~cat child in
  let* pred' = pre_eval_subquery clock store params cat pred in
  if not (plan_expr_has_subquery pred')
  then
    Lwt.return
      (Lwt_stream.filter
         (fun row -> value_truthy (eval_expr clock params row pred'))
         child_stream)
  else (
    let outer_meta = get_outer_scan_meta child in
    match outer_meta with
    | None -> Lwt.return (Lwt_stream.filter (fun _row -> false) child_stream)
    | Some meta ->
      Lwt.return
        (Lwt_stream.filter_s
           (fun row ->
              let subst_pred = substitute_outer_in_plan_expr meta row pred' in
              let* resolved =
                with_pull_context ~stats:s_opt ~mode (fun () ->
                  pre_eval_subquery clock store params cat subst_pred)
              in
              Lwt.return (value_truthy (eval_expr clock params row resolved)))
           child_stream))

and stream_expr_project clock params store mode cat exprs child =
  (* #257: as in [stream_filter], re-establish the stats scope around per-row
     correlated-subquery evaluation in a projected expression. *)
  let s_opt = Lwt.get query_stats_key in
  let* inner = to_stream clock params store ~mode ~cat child in
  let* exprs' =
    Lwt_list.map_s (fun (e, _alias) -> pre_eval_subquery clock store params cat e) exprs
  in
  let has_corr = List.exists plan_expr_has_subquery exprs' in
  if not has_corr
  then (
    let eval_exprs row = Array.of_list (List.map (eval_expr clock params row) exprs') in
    Lwt.return (Lwt_stream.map eval_exprs inner))
  else (
    let outer_meta = get_outer_scan_meta child in
    match outer_meta with
    | None ->
      let eval_exprs row = Array.of_list (List.map (eval_expr clock params row) exprs') in
      Lwt.return (Lwt_stream.map eval_exprs inner)
    | Some meta ->
      Lwt.return
        (Lwt_stream.map_s
           (fun row ->
              let* vals =
                Lwt_list.map_s
                  (fun e ->
                     let e_subst = substitute_outer_in_plan_expr meta row e in
                     let* resolved =
                       with_pull_context ~stats:s_opt ~mode (fun () ->
                         pre_eval_subquery clock store params cat e_subst)
                     in
                     Lwt.return (eval_expr clock params row resolved))
                  exprs'
              in
              Lwt.return (Array.of_list vals))
           inner))

and stream_sort clock params store mode cat keys child =
  let* inner = to_stream clock params store ~mode ~cat child in
  let* rows = Lwt_stream.to_list inner in
  let* keys' =
    Lwt_list.map_s
      (fun (e, dir, nulls) ->
         let* e' = pre_eval_subquery clock store params cat e in
         Lwt.return (e', dir, nulls))
      keys
  in
  let cmp a b =
    List.fold_left
      (fun acc (key, dir, nulls) ->
         if acc <> 0
         then acc
         else (
           let va = eval_expr clock params a key
           and vb = eval_expr clock params b key in
           compare_with_nulls dir nulls va vb))
      0
      keys'
  in
  Lwt.return (Lwt_stream.of_list (List.sort cmp rows))

and stream_index_lookup
      clock
      params
      store
      mode
      table_tree
      idx_tree
      col_type
      lookup_val
      (table_meta : Cat.table_meta)
  =
  let s_opt = Lwt.get query_stats_key in
  let v = eval_expr clock params [||] lookup_val in
  (* [WHERE col = NULL] never matches (SQL three-valued logic).  A bound
     parameter may be NULL at run time (#228: [col = ?] is now index-eligible);
     return no rows rather than seeking the index's NULL entries. *)
  match v with
  | Row.V_null -> Lwt.return (Lwt_stream.of_list [])
  | _ ->
    let lookup_v =
      match v, col_type with
      | Row.V_null, _ -> Index_key.IK_null
      | Row.V_int n, Row.Integer -> Index_key.IK_int n
      | Row.V_text s, Row.Text -> Index_key.IK_text s
      | Row.V_real f, Row.Real -> Index_key.IK_real f
      | Row.V_blob b, Row.Blob -> Index_key.IK_blob b
      | _, _ -> Index_key.IK_null (* type mismatch: nothing matches *)
    in
    let prefix = Index_key.encode_value lookup_v in
    let plen = Bytes.length prefix in
    let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
    (* #262: read through the active txn so an index lookup sees rows the open
       transaction has inserted/updated but not yet committed. *)
    let* rh = rh_begin store mode in
    (* O(log n) native seek + lazy streaming of just the matching prefix range,
     instead of draining the entire index tree per lookup (#228). *)
    let* cur = rh_seek_ge rh idx_tree seek_key in
    let exhausted = ref false in
    let ended = ref false in
    let finish () =
      if !ended
      then Lwt.return_unit
      else (
        ended := true;
        S.seek_close cur;
        rh_finish rh)
    in
    let stream =
      Lwt_stream.from (fun () ->
        if !exhausted
        then Lwt.return_none
        else
          Lwt.catch
            (fun () ->
               let rec next () =
                 match%lwt S.seek_next cur with
                 | None ->
                   exhausted := true;
                   let%lwt () = finish () in
                   Lwt.return_none
                 | Some (ikey, _ival) ->
                   if
                     Bytes.length ikey >= plen + 8
                     && Bytes.equal (Bytes.sub ikey 0 plen) prefix
                   then (
                     let rowid_bytes = Bytes.sub ikey (Bytes.length ikey - 8) 8 in
                     let rowid = Rowid.decode rowid_bytes in
                     let table_key = Rowid.encode rowid in
                     let%lwt vrow = rh_get rh table_tree table_key in
                     match vrow with
                     | None -> next ()
                     | Some vbytes ->
                       incr_examined s_opt;
                       let row = decode_with_virtual clock params table_meta vbytes in
                       Lwt.return_some row)
                   else (
                     exhausted := true;
                     let%lwt () = finish () in
                     Lwt.return_none)
               in
               next ())
            (fun exn ->
               exhausted := true;
               let%lwt () = finish () in
               Lwt.fail exn))
    in
    Lwt.return stream

(* #243 (T1): point lookup on an INTEGER PRIMARY KEY rowid alias — the column IS
   the table key, so this is a single O(log n) table-tree seek, no index and no
   second fetch.  A NULL or non-integer probe matches nothing, mirroring the
   old __pk Op_index_lookup path (which mapped a type-mismatched value to
   IK_null ⇒ empty), so behavior is unchanged. *)
and stream_rowid_lookup clock params store mode lookup_val (table_meta : Cat.table_meta) =
  let s_opt = Lwt.get query_stats_key in
  let v = eval_expr clock params [||] lookup_val in
  match v with
  | Row.V_int n ->
    (* #262: read through the active txn so a primary-key point lookup sees the
       row when it was written earlier in the same open transaction. *)
    let* rh = rh_begin store mode in
    let rl_tree_id, _, _, _ = Cat.row_storage table_meta in
    let* vrow = rh_get rh rl_tree_id (Rowid.encode n) in
    let* () = rh_finish rh in
    (match vrow with
     | None -> Lwt.return (Lwt_stream.of_list [])
     | Some vbytes ->
       incr_examined s_opt;
       let row = decode_with_virtual clock params table_meta vbytes in
       Lwt.return (Lwt_stream.of_list [ row ]))
  | _ -> Lwt.return (Lwt_stream.of_list [])

(* Probe the right index for one left row [lrow], appending matched (or a
   null-padded row for LEFT JOIN) combinations to [out]. *)
and nlj_probe_left
      clock
      params
      s_opt
      rh
      (right_meta : Cat.table_meta)
      idx_tree
      left_col_idx
      join_kind
      n_right_cols
      out
      lrow
  : unit Lwt.t
  =
  let lkey = lrow.(left_col_idx) in
  if lkey = Row.V_null
  then (
    if join_kind = `Left
    then out := Array.append lrow (Array.make n_right_cols Row.V_null) :: !out;
    Lwt.return_unit)
  else (
    let ik_value = row_value_to_index_value lkey in
    let prefix = Index_key.encode_value ik_value in
    let plen = Bytes.length prefix in
    let seek_key = Bytes.cat prefix (Rowid.encode Int64.min_int) in
    (* O(log n) native seek per probe — avoids draining the whole index per
       left row, which made indexed nested-loop joins O(n^2) (#228/#229). *)
    let* cur = rh_seek_ge rh idx_tree seek_key in
    let found = ref false in
    let rec scan () =
      match%lwt S.seek_next cur with
      | None -> Lwt.return_unit
      | Some (ikey, _) ->
        if Bytes.length ikey >= plen + 8 && Bytes.equal (Bytes.sub ikey 0 plen) prefix
        then (
          let rowid_bytes = Bytes.sub ikey (Bytes.length ikey - 8) 8 in
          let rowid = Rowid.decode rowid_bytes in
          let table_key = Rowid.encode rowid in
          let nlj_tree_id, _, _, _ = Cat.row_storage right_meta in
          let* vrow = rh_get rh nlj_tree_id table_key in
          match vrow with
          | None -> scan ()
          | Some vbytes ->
            incr_examined s_opt;
            let rrow = decode_with_virtual clock params right_meta vbytes in
            out := Array.append lrow rrow :: !out;
            found := true;
            scan ())
        else Lwt.return_unit
    in
    let* () = scan () in
    S.seek_close cur;
    (match join_kind with
     | `Left when not !found ->
       out := Array.append lrow (Array.make n_right_cols Row.V_null) :: !out
     | _ -> ());
    Lwt.return_unit)

and stream_nested_loop_join
      clock
      params
      store
      mode
      cat
      left
      (right_meta : Cat.table_meta)
      idx_tree
      left_col_idx
      join_kind
      n_right_cols
  =
  (* #239: captured under [query]'s [with_value] scope; counts right-side index
     probes (the left input's base scan is counted via [to_stream] below). *)
  let s_opt = Lwt.get query_stats_key in
  let* left_stream = to_stream clock params store ~mode ~cat left in
  let* left_rows = Lwt_stream.to_list left_stream in
  (* #262: probe the inner index through the active txn so the join sees inner
     rows written earlier in the same open transaction. *)
  with_read store mode
  @@ fun rh ->
  let out = ref [] in
  let* () =
    Lwt_list.iter_s
      (nlj_probe_left
         clock
         params
         s_opt
         rh
         right_meta
         idx_tree
         left_col_idx
         join_kind
         n_right_cols
         out)
      left_rows
  in
  Lwt.return (Lwt_stream.of_list (List.rev !out))

(* Build a hash table mapping each right row's join key to its rows; NULL keys
   are excluded (they never match an equi-join probe). *)
and hash_build right_rows right_key : (bytes, Row.t list) Hashtbl.t =
  let tbl = Hashtbl.create 64 in
  List.iter
    (fun rrow ->
       match rrow.(right_key) with
       | Row.V_null -> ()
       | key_v ->
         let key_bytes = Index_key.encode_value (row_value_to_index_value key_v) in
         let prev =
           try Hashtbl.find tbl key_bytes with
           | Not_found -> []
         in
         Hashtbl.replace tbl key_bytes (rrow :: prev))
    right_rows;
  tbl

and stream_hash_join
      clock
      params
      store
      mode
      cat
      left
      right
      left_key
      right_key
      join_kind
      n_right_cols
  =
  let* left_stream = to_stream clock params store ~mode ~cat left in
  let* right_stream = to_stream clock params store ~mode ~cat right in
  let* right_rows = Lwt_stream.to_list right_stream in
  if left_key < 0 || right_key < 0
  then (
    (* Cartesian product fallback (general ON predicate). *)
    let* left_rows = Lwt_stream.to_list left_stream in
    let out = ref [] in
    List.iter
      (fun lrow ->
         let any = ref false in
         List.iter
           (fun rrow ->
              out := Array.append lrow rrow :: !out;
              any := true)
           right_rows;
         match join_kind with
         | `Left when not !any ->
           let null_right = Array.make n_right_cols Row.V_null in
           out := Array.append lrow null_right :: !out
         | _ -> ())
      left_rows;
    Lwt.return (Lwt_stream.of_list (List.rev !out)))
  else (
    let tbl = hash_build right_rows right_key in
    let* left_rows = Lwt_stream.to_list left_stream in
    let out = ref [] in
    List.iter
      (fun lrow ->
         let key_v = lrow.(left_key) in
         let any = ref false in
         (match key_v with
          | Row.V_null -> ()
          | _ ->
            let key_bytes = Index_key.encode_value (row_value_to_index_value key_v) in
            (match Hashtbl.find_opt tbl key_bytes with
             | None -> ()
             | Some rrows ->
               List.iter
                 (fun rrow ->
                    out := Array.append lrow rrow :: !out;
                    any := true)
                 (List.rev rrows)));
         match join_kind with
         | `Left when not !any ->
           let null_right = Array.make n_right_cols Row.V_null in
           out := Array.append lrow null_right :: !out
         | _ -> ())
      left_rows;
    Lwt.return (Lwt_stream.of_list (List.rev !out)))

(* SUM over a group's column [i]: preserve INT vs REAL like SQLite-lite. *)
and agg_sum group_rows i : Row.value =
  let any_real =
    List.exists
      (fun r ->
         match r.(i) with
         | Row.V_real _ -> true
         | _ -> false)
      group_rows
  in
  let any_non_null =
    List.exists
      (fun r ->
         match r.(i) with
         | Row.V_null -> false
         | _ -> true)
      group_rows
  in
  if not any_non_null
  then Row.V_null
  else if any_real
  then (
    let s =
      List.fold_left
        (fun acc r ->
           match r.(i) with
           | Row.V_null -> acc
           | Row.V_int n -> acc +. Int64.to_float n
           | Row.V_real f -> acc +. f
           | _ -> failwith "SUM on non-numeric value")
        0.0
        group_rows
    in
    Row.V_real s)
  else (
    let s =
      List.fold_left
        (fun acc r ->
           match r.(i) with
           | Row.V_null -> acc
           | Row.V_int n -> Int64.add acc n
           | _ -> failwith "SUM on non-numeric value")
        0L
        group_rows
    in
    Row.V_int s)

(* Evaluate one aggregate [spec] over the rows of a group. *)
and aggregate_one (spec : Plan.agg_spec) (group_rows : Row.t list) : Row.value =
  match spec.func, spec.col_ord with
  | Ast.Agg_count, None -> Row.V_int (Int64.of_int (List.length group_rows))
  | Ast.Agg_count, Some i ->
    let n =
      List.fold_left
        (fun acc r ->
           match r.(i) with
           | Row.V_null -> acc
           | _ -> acc + 1)
        0
        group_rows
    in
    Row.V_int (Int64.of_int n)
  | Ast.Agg_sum, Some i -> agg_sum group_rows i
  | Ast.Agg_avg, Some i ->
    let sum, n =
      List.fold_left
        (fun (s, n) r ->
           match r.(i) with
           | Row.V_null -> s, n
           | Row.V_int x -> s +. Int64.to_float x, n + 1
           | Row.V_real f -> s +. f, n + 1
           | _ -> failwith "AVG on non-numeric value")
        (0.0, 0)
        group_rows
    in
    if n = 0 then Row.V_null else Row.V_real (sum /. float_of_int n)
  | Ast.Agg_min, Some i ->
    List.fold_left
      (fun acc r ->
         match r.(i), acc with
         | Row.V_null, _ -> acc
         | v, Row.V_null -> v
         | v, cur -> if compare_values v cur < 0 then v else cur)
      Row.V_null
      group_rows
  | Ast.Agg_max, Some i ->
    List.fold_left
      (fun acc r ->
         match r.(i), acc with
         | Row.V_null, _ -> acc
         | v, Row.V_null -> v
         | v, cur -> if compare_values v cur > 0 then v else cur)
      Row.V_null
      group_rows
  | Ast.Agg_group_concat sep, Some i ->
    let separator = Option.value sep ~default:"," in
    let parts =
      List.filter_map
        (fun r ->
           match r.(i) with
           | Row.V_null -> None
           | Row.V_int n -> Some (Int64.to_string n)
           | Row.V_real f -> Some (Printf.sprintf "%.17g" f)
           | Row.V_text s -> Some s
           | Row.V_blob _ -> Some "")
        group_rows
    in
    if parts = [] then Row.V_null else Row.V_text (String.concat separator parts)
  | Ast.Agg_group_concat _, None -> failwith "GROUP_CONCAT requires a column argument"
  | (Ast.Agg_sum | Ast.Agg_avg | Ast.Agg_min | Ast.Agg_max), None ->
    failwith "non-COUNT aggregate must have a column argument"

(* Partition [rows] into (group_key, group_rows) by [group_cols] (stable). *)
and aggregate_build_groups group_cols rows : (Row.value list * Row.t list) list =
  let group_keys_of_row row = List.map (fun i -> row.(i)) group_cols in
  let compare_group_keys ka kb =
    List.fold_left2 (fun acc a b -> if acc <> 0 then acc else compare_values a b) 0 ka kb
  in
  if group_cols = []
  then [ [], rows ]
  else (
    let sorted =
      List.stable_sort
        (fun a b -> compare_group_keys (group_keys_of_row a) (group_keys_of_row b))
        rows
    in
    let rec group_runs acc cur_key cur_rows = function
      | [] ->
        (match cur_rows with
         | [] -> List.rev acc
         | _ -> List.rev ((cur_key, List.rev cur_rows) :: acc))
      | r :: rest ->
        let k = group_keys_of_row r in
        if cur_rows <> [] && compare_group_keys k cur_key = 0
        then group_runs acc cur_key (r :: cur_rows) rest
        else (
          let acc' = if cur_rows = [] then acc else (cur_key, List.rev cur_rows) :: acc in
          group_runs acc' k [ r ] rest)
    in
    group_runs [] [] [] sorted)

(* Append post-aggregate window-function columns to [after_having] rows. *)
and aggregate_apply_windows clock params agg_windows after_having =
  if agg_windows = []
  then after_having
  else (
    let n_total = List.length after_having in
    let indexed = List.mapi (fun i r -> i, r) after_having in
    let window_arrays =
      List.map
        (fun (wplan : Plan.window_plan_item) ->
           let partitions =
             group_by_partition clock params wplan.Plan.partition_by indexed
           in
           let combined = Array.make n_total Row.V_null in
           List.iter
             (fun (_, partition_indexed) ->
                let sorted =
                  sort_partition_by clock params wplan.Plan.order_by partition_indexed
                in
                let part_results =
                  compute_window_for_partition clock params wplan sorted n_total
                in
                List.iter
                  (fun (orig_idx, _) -> combined.(orig_idx) <- part_results.(orig_idx))
                  sorted)
             partitions;
           combined)
        agg_windows
    in
    List.mapi
      (fun i row ->
         let extras = List.map (fun arr -> arr.(i)) window_arrays in
         Array.append row (Array.of_list extras))
      after_having)

(* #247: build an incremental accumulator for one aggregate [spec]: an
   [(update, finalize)] pair folded over scanned rows.  Returns [None] for any
   spec the fast-path doesn't handle (e.g. a non-COUNT aggregate with no column),
   which makes the caller fall back to the general [stream_aggregate] path.  The
   per-type logic here MUST stay byte-identical to [aggregate_one]/[agg_sum]. *)
and make_agg_acc (spec : Plan.agg_spec) : ((Row.t -> unit) * (unit -> Row.value)) option =
  match spec.Plan.func, spec.Plan.col_ord with
  | Ast.Agg_count, None ->
    let c = ref 0 in
    Some ((fun _ -> incr c), fun () -> Row.V_int (Int64.of_int !c))
  | Ast.Agg_count, Some i ->
    let c = ref 0 in
    Some
      ( (fun row ->
          match row.(i) with
          | Row.V_null -> ()
          | _ -> incr c)
      , fun () -> Row.V_int (Int64.of_int !c) )
  | Ast.Agg_sum, Some i ->
    (* INT vs REAL preserved exactly like [agg_sum]: REAL iff any real seen;
       NULL iff no non-null seen. *)
    let si = ref 0L
    and sf = ref 0.0
    and any_real = ref false
    and any_nn = ref false in
    Some
      ( (fun row ->
          match row.(i) with
          | Row.V_null -> ()
          | Row.V_int n ->
            any_nn := true;
            si := Int64.add !si n;
            sf := !sf +. Int64.to_float n
          | Row.V_real f ->
            any_nn := true;
            any_real := true;
            sf := !sf +. f
          | _ -> failwith "SUM on non-numeric value")
      , fun () ->
          if not !any_nn
          then Row.V_null
          else if !any_real
          then Row.V_real !sf
          else Row.V_int !si )
  | Ast.Agg_avg, Some i ->
    let sf = ref 0.0
    and n = ref 0 in
    Some
      ( (fun row ->
          match row.(i) with
          | Row.V_null -> ()
          | Row.V_int x ->
            sf := !sf +. Int64.to_float x;
            incr n
          | Row.V_real f ->
            sf := !sf +. f;
            incr n
          | _ -> failwith "AVG on non-numeric value")
      , fun () -> if !n = 0 then Row.V_null else Row.V_real (!sf /. float_of_int !n) )
  | Ast.Agg_min, Some i ->
    let best = ref Row.V_null in
    Some
      ( (fun row ->
          match row.(i), !best with
          | Row.V_null, _ -> ()
          | v, Row.V_null -> best := v
          | v, cur -> if compare_values v cur < 0 then best := v)
      , fun () -> !best )
  | Ast.Agg_max, Some i ->
    let best = ref Row.V_null in
    Some
      ( (fun row ->
          match row.(i), !best with
          | Row.V_null, _ -> ()
          | v, Row.V_null -> best := v
          | v, cur -> if compare_values v cur > 0 then best := v)
      , fun () -> !best )
  | Ast.Agg_group_concat sep, Some i ->
    let separator = Option.value sep ~default:"," in
    let parts = ref [] in
    (* newest-first; reversed at finalize to preserve scan order *)
    Some
      ( (fun row ->
          match row.(i) with
          | Row.V_null -> ()
          | Row.V_int n -> parts := Int64.to_string n :: !parts
          | Row.V_real f -> parts := Printf.sprintf "%.17g" f :: !parts
          | Row.V_text s -> parts := s :: !parts
          | Row.V_blob _ -> parts := "" :: !parts)
      , fun () ->
          match !parts with
          | [] -> Row.V_null
          | l -> Row.V_text (String.concat separator (List.rev l)) )
  | (Ast.Agg_sum | Ast.Agg_avg | Ast.Agg_min | Ast.Agg_max | Ast.Agg_group_concat _), None
    -> None

(* #247: cursor-level fast path for a no-GROUP-BY aggregate directly over a
   (optionally filtered) sequential scan.  Folds the accumulators over the scan
   cursor in a single pass, bypassing the child's per-row [Lwt_stream] layers and
   the [Lwt_stream.to_list] full-table materialisation the general path pays.
   Returns [Some stream] (always exactly one output row, matching
   [aggregate_build_groups]'s single implicit group) when applicable, else [None]
   to fall back.  Preserves the streaming/stack-bound property: the fold holds
   only the accumulators, never the rows. *)
and aggregate_fast_path
      clock
      params
      store
      mode
      cat
      child
      group_cols
      aggs
      having
      proj
      agg_windows
  : Row.t Lwt_stream.t option Lwt.t
  =
  if not (agg_fastpath_enabled ())
  then Lwt.return None
  else if group_cols <> [] || having <> None || agg_windows <> []
  then Lwt.return None
  else if
    not
      (List.for_all
         (function
           | Plan.PI_agg_slot _ -> true
           | _ -> false)
         proj)
  then Lwt.return None
  else (
    match child with
    | Plan.Op_seq_scan { table_meta } ->
      run_aggregate_fast_path clock params store mode cat table_meta None aggs proj
    | Plan.Op_filter { pred; child = Plan.Op_seq_scan { table_meta } }
      when not (plan_expr_has_subquery pred) ->
      run_aggregate_fast_path clock params store mode cat table_meta (Some pred) aggs proj
    | _ -> Lwt.return None)

and run_aggregate_fast_path clock params store mode cat table_meta pred_opt aggs proj =
  match
    let accs = List.map make_agg_acc aggs in
    if List.exists Option.is_none accs
    then None
    else Some (Array.of_list (List.map Option.get accs))
  with
  | None -> Lwt.return None
  | Some accs ->
    let* pred' =
      match pred_opt with
      | None -> Lwt.return None
      | Some p ->
        let* p' = pre_eval_subquery clock store params cat p in
        Lwt.return (Some p')
    in
    (* No decode needed when nothing reads a column and there is no filter: a
       pure COUNT-star loop runs at storage-cursor speed. *)
    let max_col =
      List.fold_left
        (fun m (s : Plan.agg_spec) ->
           match s.Plan.col_ord with
           | Some i when i > m -> i
           | _ -> m)
        (-1)
        aggs
    in
    let need_decode = pred_opt <> None || max_col >= 0 in
    (* #247: when no filter reads other columns and there are no virtual columns
       to recompute, decode only the [0, max_col] prefix — skipping trailing
       columns (e.g. a TEXT payload) the aggregate never touches. *)
    let can_prune = pred_opt = None && not (has_virtual_cols table_meta.Cat.columns) in
    let decode_row vbytes =
      if can_prune
      then Row.decode_prefix table_meta.Cat.columns vbytes ~upto:max_col
      else decode_with_virtual clock params table_meta vbytes
    in
    (* #262: fold over the active txn when one is open, so a COUNT/SUM reflects
       rows written earlier in the same uncommitted transaction. *)
    let* rh = rh_begin store mode in
    let agg_tree_id, _, _, _ = Cat.row_storage table_meta in
    let* cur = rh_seek_ge rh agg_tree_id Bytes.empty in
    let ended = ref false in
    let finish () =
      if !ended
      then Lwt.return_unit
      else (
        ended := true;
        S.seek_close cur;
        rh_finish rh)
    in
    let dummy = [||] in
    let s_opt = Lwt.get query_stats_key in
    Lwt.catch
      (fun () ->
         let rec loop () =
           let* kv = S.seek_next cur in
           match kv with
           | None ->
             let* () = finish () in
             let agg_vals = Array.map (fun (_, fin) -> fin ()) accs in
             let out =
               Array.of_list
                 (List.map
                    (function
                      | Plan.PI_agg_slot k -> agg_vals.(k)
                      | Plan.PI_group_col _ | Plan.PI_window_slot _ ->
                        assert false (* excluded above *))
                    proj)
             in
             Lwt.return (Some (Lwt_stream.of_list [ out ]))
           | Some (_key, vbytes) ->
             incr_examined s_opt;
             if need_decode
             then (
               let row = decode_row vbytes in
               let keep =
                 match pred' with
                 | None -> true
                 | Some p -> value_truthy (eval_expr clock params row p)
               in
               if keep then Array.iter (fun (upd, _) -> upd row) accs)
             else Array.iter (fun (upd, _) -> upd dummy) accs;
             loop ()
         in
         loop ())
      (fun exn ->
         let* () = finish () in
         Lwt.fail exn)

and stream_aggregate
      clock
      params
      store
      mode
      cat
      child
      group_cols
      aggs
      having
      proj
      agg_windows
  =
  let* fast =
    aggregate_fast_path
      clock
      params
      store
      mode
      cat
      child
      group_cols
      aggs
      having
      proj
      agg_windows
  in
  match fast with
  | Some stream -> Lwt.return stream
  | None ->
    let* inner = to_stream clock params store ~mode ~cat child in
    let* rows = Lwt_stream.to_list inner in
    let n_group_cols = List.length group_cols in
    let groups = aggregate_build_groups group_cols rows in
    let agg_output_rows =
      List.map
        (fun (group_key, group_rows) ->
           let agg_vals = List.map (fun spec -> aggregate_one spec group_rows) aggs in
           Array.of_list (group_key @ agg_vals))
        groups
    in
    let after_having =
      match having with
      | None -> agg_output_rows
      | Some pred ->
        List.filter
          (fun r -> value_truthy (eval_expr clock params r pred))
          agg_output_rows
    in
    let n_agg_cols = n_group_cols + List.length aggs in
    let with_windows = aggregate_apply_windows clock params agg_windows after_having in
    let final_rows =
      List.map
        (fun agg_row ->
           Array.of_list
             (List.map
                (function
                  | Plan.PI_group_col i -> agg_row.(i)
                  | Plan.PI_agg_slot k -> agg_row.(n_group_cols + k)
                  | Plan.PI_window_slot j -> agg_row.(n_agg_cols + j))
                proj))
        with_windows
    in
    Lwt.return (Lwt_stream.of_list final_rows)

and read_fts_content_rows store mode (fts_meta : Cat.fts_table_meta)
  : (int64 * string list) list Lwt.t
  =
  (* #330: read the FTS content tree as (rowid, column-texts) pairs through [mode]
     (the same shared snapshot / explicit txn the dump uses for table rows), so
     [Db.dump] can emit INSERTs that carry the original rowids and round-trip the
     index exactly.  The content tree is keyed by rowid, so this is the only place
     FTS rowids are surfaced — deliberately out-of-band, not via a SQL projection
     (see #330). *)
  with_read store mode (fun rh ->
    let* cur = rh_cursor_open rh fts_meta.Cat.fts_content_tree in
    let _sr = S.cursor_first cur in
    let acc = ref [] in
    let rec walk () =
      match S.cursor_next cur with
      | None -> ()
      | Some (k, v) ->
        acc := (Rowid.decode k, fts_decode_content v) :: !acc;
        walk ()
    in
    walk ();
    S.cursor_close cur;
    Lwt.return (List.rev !acc))

and stream_fts_seq_scan clock params store mode (fts_meta : Cat.fts_table_meta) where =
  (* #257: captured at construction (inside [query]'s [with_value] scope), same
     pattern as the table scanners; every content row scanned counts as examined
     regardless of the WHERE filter. *)
  let s_opt = Lwt.get query_stats_key in
  (* #262: scan the content tree through the active txn so an in-transaction
     write to the FTS table is visible to the scan. *)
  let* rh = rh_begin store mode in
  let* cur = rh_cursor_open rh fts_meta.Cat.fts_content_tree in
  let _sr = S.cursor_first cur in
  let exhausted = ref false in
  let ended = ref false in
  let finish () =
    if !ended
    then Lwt.return_unit
    else (
      ended := true;
      S.cursor_close cur;
      rh_finish rh)
  in
  let rec read_next () =
    if !exhausted
    then Lwt.return_none
    else (
      match S.cursor_next cur with
      | None ->
        exhausted := true;
        let%lwt () = finish () in
        Lwt.return_none
      | Some (_key, val_bytes) ->
        incr_examined s_opt;
        let texts = fts_decode_content val_bytes in
        let row = Array.of_list (List.map (fun s -> Row.V_text s) texts) in
        let emit =
          match where with
          | None -> true
          | Some pred -> value_truthy (eval_expr clock params row pred)
        in
        if emit then Lwt.return_some row else read_next ())
  in
  Lwt.return
    (Lwt_stream.from (fun () ->
       Lwt.catch read_next (fun exn ->
         exhausted := true;
         let%lwt () = finish () in
         Lwt.fail exn)))

and stream_fts_match_scan
      _clock
      _params
      store
      mode
      (fts_meta : Cat.fts_table_meta)
      query
      proj
      include_rank
      snippets
  =
  (* #257: each matched FTS index row counts as one examined row — the index
     seek (and the content fetch it drives) is the work this scan does.  The
     increment sits on the match, ahead of the content [S.get], so a match whose
     content row is absent still counts: the seek happened regardless. *)
  let s_opt = Lwt.get query_stats_key in
  (* #262: run the index query and content fetches through the active txn (when
     one is open) so an in-transaction write to the FTS table is matched and
     returned.  The body delegates the raw txn to the FTS helpers, so it is made
     polymorphic over the txn kind rather than using a [read_handle]. *)
  let body : type a. a S.txn -> Row.t Lwt_stream.t Lwt.t =
    fun tx ->
    let* matches = fts_execute_query tx ~index_tree:fts_meta.Cat.fts_index_tree query in
    let* scored_matches = fts_score_matches tx fts_meta query matches include_rank in
    let sorted =
      if include_rank
      then List.sort (fun (_, _, s1) (_, _, s2) -> Float.compare s2 s1) scored_matches
      else scored_matches
    in
    let snippet_terms = fts_query_terms_with_kind query in
    let* rows =
      Lwt_list.filter_map_s
        (fun (rowid, _positions, score) ->
           incr_examined s_opt;
           let key = Rowid.encode rowid in
           let* val_opt = S.get tx fts_meta.Cat.fts_content_tree key in
           match val_opt with
           | None -> Lwt.return None
           | Some bytes ->
             let texts = fts_decode_content bytes in
             let full_row = Array.of_list (List.map (fun s -> Row.V_text s) texts) in
             let projected =
               if proj = [] && snippets = []
               then Array.to_list full_row
               else List.map (fun i -> full_row.(i)) proj
             in
             let snippet_vals =
               List.map
                 (fun (spec : Plan.snippet_spec) ->
                    let col_text =
                      let idx =
                        if spec.Plan.col_idx < 0
                        then 0
                        else min spec.Plan.col_idx (max 0 (List.length texts - 1))
                      in
                      if texts = [] then "" else List.nth texts idx
                    in
                    Row.V_text
                      (compute_snippet ~col_text ~query_terms:snippet_terms ~spec))
                 snippets
             in
             let row_values =
               projected
               @ (if include_rank then [ Row.V_real score ] else [])
               @ snippet_vals
             in
             Lwt.return (Some (Array.of_list row_values)))
        sorted
    in
    Lwt.return (Lwt_stream.of_list rows)
  in
  match mode with
  | In_txn tx -> body tx
  | In_ro_txn tx -> body tx
  | Auto -> S.with_ro store body

and stream_pragma_integrity_check store cat =
  let cat_val =
    match cat with
    | None -> failwith "Exec.to_stream: Op_pragma_integrity_check requires catalog"
    | Some c -> c
  in
  let* tables = Cat.list_tables cat_val in
  let errors = ref [] in
  let add_err msg = errors := msg :: !errors in
  let count_entries tx tid =
    let count = ref 0 in
    let* cur = S.cursor_open tx tid in
    let _sr = S.cursor_first cur in
    let rec go () =
      match S.cursor_next cur with
      | None -> Lwt.return_unit
      | Some _ ->
        incr count;
        go ()
    in
    let* () = go () in
    S.cursor_close cur;
    Lwt.return !count
  in
  S.with_ro store
  @@ fun tx ->
  let* () =
    Lwt_list.iter_s
      (fun (meta : Cat.table_meta) ->
         match meta.Cat.storage with
         | Cat.Columnar _ -> Lwt.return_unit
         | Cat.Row { tree_id; _ } ->
           let* row_count = count_entries tx tree_id in
           let idxs = Cat.indexes_for_table cat_val ~table:meta.name in
           Lwt_list.iter_s
             (fun (idx : Cat.index_info) ->
                let is_partial = idx.idx_where_sql <> None in
                let* idx_count = count_entries tx idx.idx_tree_id in
                if (not is_partial) && idx_count <> row_count
                then
                  add_err
                    (Printf.sprintf
                       "index %s on %s: %d entries != %d rows"
                       idx.idx_name
                       meta.name
                       idx_count
                       row_count);
                Lwt.return_unit)
             idxs)
      tables
  in
  let result = List.rev !errors in
  let rows =
    if result = []
    then [ [| Row.V_text "ok" |] ]
    else List.map (fun msg -> [| Row.V_text msg |]) result
  in
  Lwt.return (Lwt_stream.of_list rows)

and stream_sqlite_master store cat =
  let cat_val =
    match cat with
    | None -> failwith "Exec.to_stream: Op_sqlite_master requires catalog"
    | Some c -> c
  in
  let* tables = Cat.list_tables cat_val in
  let table_rows =
    List.map
      (fun (meta : Cat.table_meta) ->
         let sm_tree_id =
           match meta.Cat.storage with
           | Cat.Row { tree_id; _ } -> tree_id
           | Cat.Columnar _ -> 0
         in
         [| Row.V_text "table"
          ; Row.V_text meta.Cat.name
          ; Row.V_text meta.Cat.name
          ; Row.V_int (Int64.of_int sm_tree_id)
          ; Row.V_text (ddl_of_table meta)
         |])
      tables
  in
  let index_rows =
    List.concat_map
      (fun (meta : Cat.table_meta) ->
         List.map
           (fun (idx : Cat.index_info) ->
              [| Row.V_text "index"
               ; Row.V_text idx.Cat.idx_name
               ; Row.V_text idx.Cat.idx_table
               ; Row.V_int (Int64.of_int idx.Cat.idx_tree_id)
               ; Row.V_text (ddl_of_index idx)
              |])
           (Cat.indexes_for_table cat_val ~table:meta.Cat.name))
      tables
  in
  let* views = Cat.load_all_views store in
  let view_rows =
    List.map
      (fun (name, sql) ->
         [| Row.V_text "view"
          ; Row.V_text name
          ; Row.V_text name
          ; Row.V_int 0L
          ; Row.V_text sql
         |])
      views
  in
  let* triggers = Cat.load_all_triggers store in
  let trigger_rows =
    List.map
      (fun (name, sql) ->
         let tbl_name = trigger_table_of_sql name sql in
         [| Row.V_text "trigger"
          ; Row.V_text name
          ; Row.V_text tbl_name
          ; Row.V_int 0L
          ; Row.V_text sql
         |])
      triggers
  in
  let fts_rows =
    List.map
      (fun (m : Cat.fts_table_meta) ->
         [| Row.V_text "table"
          ; Row.V_text m.Cat.fts_name
          ; Row.V_text m.Cat.fts_name
          ; Row.V_int (Int64.of_int m.Cat.fts_content_tree)
          ; Row.V_text (ddl_of_fts m)
         |])
      (Cat.list_fts_tables cat_val)
  in
  (* #312: sqlite_sequence appears in sqlite_master once any AUTOINCREMENT
     table exists (matching SQLite — independent of whether a row has been
     inserted yet). *)
  let seq_rows =
    if
      List.exists
        (fun (m : Cat.table_meta) ->
           match m.Cat.storage with
           | Cat.Row { autoincrement = true; _ } -> true
           | _ -> false)
        tables
    then
      [ [| Row.V_text "table"
         ; Row.V_text "sqlite_sequence"
         ; Row.V_text "sqlite_sequence"
         ; Row.V_int 0L
         ; Row.V_text "CREATE TABLE sqlite_sequence(name,seq)"
        |]
      ]
    else []
  in
  Lwt.return
    (Lwt_stream.of_list
       (table_rows @ seq_rows @ index_rows @ view_rows @ trigger_rows @ fts_rows))

and stream_sqlite_sequence cat =
  let cat_val =
    match cat with
    | None -> failwith "Exec.to_stream: Op_sqlite_sequence requires catalog"
    | Some c -> c
  in
  let* tables = Cat.list_tables cat_val in
  let rows =
    List.filter_map
      (fun (m : Cat.table_meta) ->
         match m.Cat.storage with
         | Cat.Row { autoincrement = true; next_rowid; _ }
           when not (Int64.equal next_rowid Cat.empty_next_rowid) ->
           Some [| Row.V_text m.Cat.name; Row.V_int (Int64.sub next_rowid 1L) |]
         | _ -> None)
      tables
  in
  Lwt.return (Lwt_stream.of_list rows)

and stream_union clock params store mode cat all left right =
  let* ls = to_stream clock params store ~mode ~cat left in
  let* rs = to_stream clock params store ~mode ~cat right in
  let combined = Lwt_stream.append ls rs in
  if all
  then Lwt.return combined
  else
    let* rows = Lwt_stream.to_list combined in
    let seen = Hashtbl.create 64 in
    let deduped =
      List.filter
        (fun row ->
           let k = row_key row in
           if Hashtbl.mem seen k
           then false
           else (
             Hashtbl.replace seen k ();
             true))
        rows
    in
    Lwt.return (Lwt_stream.of_list deduped)

and stream_intersect clock params store mode cat left right =
  let* ls = to_stream clock params store ~mode ~cat left in
  let* rs = to_stream clock params store ~mode ~cat right in
  let* right_list = Lwt_stream.to_list rs in
  let right_set = Hashtbl.create (max 1 (List.length right_list)) in
  List.iter (fun r -> Hashtbl.replace right_set (row_key r) ()) right_list;
  let* left_list = Lwt_stream.to_list ls in
  let seen = Hashtbl.create 64 in
  let result =
    List.filter
      (fun row ->
         let k = row_key row in
         if (not (Hashtbl.mem right_set k)) || Hashtbl.mem seen k
         then false
         else (
           Hashtbl.replace seen k ();
           true))
      left_list
  in
  Lwt.return (Lwt_stream.of_list result)

and stream_except clock params store mode cat left right =
  let* ls = to_stream clock params store ~mode ~cat left in
  let* rs = to_stream clock params store ~mode ~cat right in
  let* right_list = Lwt_stream.to_list rs in
  let right_set = Hashtbl.create (max 1 (List.length right_list)) in
  List.iter (fun r -> Hashtbl.replace right_set (row_key r) ()) right_list;
  let* left_list = Lwt_stream.to_list ls in
  let seen = Hashtbl.create 64 in
  let result =
    List.filter
      (fun row ->
         let k = row_key row in
         if Hashtbl.mem right_set k || Hashtbl.mem seen k
         then false
         else (
           Hashtbl.replace seen k ();
           true))
      left_list
  in
  Lwt.return (Lwt_stream.of_list result)

and stream_insert_returning
      clock
      params
      store
      mode
      cat
      (table_meta : Cat.table_meta)
      ordinals
      values
      on_conflict
      returning
      upsert_update
  =
  match cat with
  | None -> failwith "Exec.query: RETURNING requires catalog context"
  | Some c ->
    let* result_lists =
      Lwt_list.map_s
        (fun row_vals ->
           let n = List.length table_meta.columns in
           let inserted_row = Array.make n Row.V_null in
           List.iter2
             (fun ord e -> inserted_row.(ord) <- eval_expr clock params [||] e)
             ordinals
             row_vals;
           let* inserted =
             execute_insert
               ~mode
               ~clock
               ~on_conflict
               ~upsert_update
               ~prebuilt_row:(Some inserted_row)
               store
               c
               ~table_meta
               ~ordinals
               ~values:row_vals
           in
           if not inserted
           then Lwt.return []
           else (
             let result =
               Array.of_list (List.map (eval_expr clock params inserted_row) returning)
             in
             Lwt.return [ result ]))
        values
    in
    Lwt.return (Lwt_stream.of_list (List.concat result_lists))

and stream_update_returning
      clock
      params
      store
      mode
      cat
      (table_meta : Cat.table_meta)
      assignments
      where
      order
      limit
      offset
      indexes
      returning
  =
  (* Project RETURNING from the rows actually written INSIDE the update's write
     txn (via [collect]), not a separate pre-lock RO snapshot — so concurrent
     `UPDATE ... RETURNING` callers see read-from-the-write values, never stale
     or duplicated ones (#226).  Rows arrive in update order (post
     order/offset/limit), which is also the RETURNING order. *)
  let c =
    match cat with
    | Some c -> c
    | None -> failwith "Exec.to_stream: UPDATE RETURNING requires catalog context"
  in
  let acc = ref [] in
  let collect new_row =
    acc := Array.of_list (List.map (eval_expr clock params new_row) returning) :: !acc
  in
  let* _ =
    execute_update
      ~mode
      ~params
      ~clock
      ~collect:(Some collect)
      store
      c
      ~table_meta
      ~assignments
      ~where
      ~order
      ~limit
      ~offset
      ~indexes
  in
  Lwt.return (Lwt_stream.of_list (List.rev !acc))

and stream_delete_returning
      clock
      params
      store
      mode
      cat
      (table_meta : Cat.table_meta)
      where
      order
      limit
      offset
      indexes
      returning
  =
  (* Project RETURNING from the rows actually deleted INSIDE the delete's write
     txn (via [collect]), not a separate pre-lock RO snapshot (#226). *)
  let c =
    match cat with
    | Some c -> c
    | None -> failwith "Exec.to_stream: DELETE RETURNING requires catalog context"
  in
  let acc = ref [] in
  let collect old_row =
    acc := Array.of_list (List.map (eval_expr clock params old_row) returning) :: !acc
  in
  let* _ =
    execute_delete
      ~mode
      ~params
      ~clock
      ~collect:(Some collect)
      store
      c
      ~table_meta
      ~where
      ~order
      ~limit
      ~offset
      ~indexes
  in
  Lwt.return (Lwt_stream.of_list (List.rev !acc))

and stream_const_select clock params store cat exprs =
  let raw_exprs = List.map fst exprs in
  let* exprs' = Lwt_list.map_s (pre_eval_subquery clock store params cat) raw_exprs in
  let row = Array.of_list (List.map (eval_expr clock params [||]) exprs') in
  Lwt.return (Lwt_stream.of_list [ row ])

and stream_with_cte_recursive clock params store mode cat cte_name def query =
  let base_op, recursive_arm =
    match def with
    | Plan.Op_union { all = true; left; right } -> left, right
    | _ ->
      failwith
        "Exec: recursive CTE def must be UNION ALL — non-UNION-ALL recursive CTEs are \
         not supported"
  in
  let* base_stream = to_stream clock params store ~mode ~cat base_op in
  let* seed_rows = Lwt_stream.to_list base_stream in
  let max_iterations = 1000 in
  let rec iterate depth acc working =
    if working = []
    then Lwt.return acc
    else if depth >= max_iterations
    then
      failwith
        (Printf.sprintf
           "Exec: recursive CTE '%s' exceeded maximum iteration depth of %d"
           cte_name
           max_iterations)
    else (
      let patched_arm = substitute_cte ~cte_name ~rows:working recursive_arm in
      let* new_stream = to_stream clock params store ~mode ~cat patched_arm in
      let* new_rows = Lwt_stream.to_list new_stream in
      iterate (depth + 1) (acc @ new_rows) new_rows)
  in
  let* all_rows = iterate 0 seed_rows seed_rows in
  let patched_query = substitute_cte ~cte_name ~rows:all_rows query in
  to_stream clock params store ~mode ~cat patched_query

and stream_window clock params store mode cat child windows =
  let* child_stream = to_stream clock params store ~mode ~cat child in
  let* all_rows = Lwt_stream.to_list child_stream in
  let n_rows = List.length all_rows in
  if n_rows = 0
  then Lwt.return (Lwt_stream.of_list [])
  else (
    let all_rows_arr = Array.of_list all_rows in
    let n_windows = List.length windows in
    let window_results : Row.value array array =
      Array.init n_windows (fun wi ->
        let wplan = List.nth windows wi in
        let indexed_rows = List.mapi (fun i row -> i, row) all_rows in
        let partitions =
          group_by_partition clock params wplan.Plan.partition_by indexed_rows
        in
        let combined = Array.make n_rows Row.V_null in
        List.iter
          (fun (_, partition_idx_rows) ->
             let sorted =
               sort_partition_by clock params wplan.Plan.order_by partition_idx_rows
             in
             let part_results =
               compute_window_for_partition clock params wplan sorted n_rows
             in
             List.iter
               (fun (orig_idx, _) -> combined.(orig_idx) <- part_results.(orig_idx))
               sorted)
          partitions;
        combined)
    in
    let augmented =
      Array.to_list
        (Array.mapi
           (fun i row ->
              let extras = Array.init n_windows (fun wi -> window_results.(wi).(i)) in
              Array.append row extras)
           all_rows_arr)
    in
    Lwt.return (Lwt_stream.of_list augmented))

and stream_explain clock params store mode cat analyze inner =
  let plan_rows = explain_plan inner in
  let nullify row = Array.append row [| Row.V_null; Row.V_null |] in
  if not analyze
  then Lwt.return (Lwt_stream.of_list (List.map nullify plan_rows))
  else (
    let cat_v =
      match cat with
      | Some c -> c
      | None -> failwith "EXPLAIN ANALYZE requires a catalog"
    in
    let t0 =
      match clock with
      | Some c -> c ()
      | None -> 0.0
    in
    let* n =
      let is_write =
        match inner with
        | Plan.Op_insert _
        | Plan.Op_insert_select _
        | Plan.Op_update _
        | Plan.Op_delete _
        | Plan.Op_create_table _
        | Plan.Op_create_index _
        | Plan.Op_drop_table _
        | Plan.Op_drop_index _
        | Plan.Op_alter_table _
        | Plan.Op_begin
        | Plan.Op_commit
        | Plan.Op_rollback
        | Plan.Op_savepoint _
        | Plan.Op_release _
        | Plan.Op_rollback_to _
        | Plan.Op_create_view _
        | Plan.Op_create_reactive_view _
        | Plan.Op_drop_view _
        | Plan.Op_create_trigger _
        | Plan.Op_drop_trigger _
        | Plan.Op_pragma_set_user_version _
        | Plan.Op_pragma_set_fk _
        | Plan.Op_pragma_set_recursive_triggers _
        | Plan.Op_pragma_set_defer_fk _
        | Plan.Op_pragma_set_wal_autocheckpoint _
        | Plan.Op_pragma_set_synchronous _
        | Plan.Op_pragma_set_wal_batch_commits _
        | Plan.Op_pragma_set_wal_batch_interval_ms _
        | Plan.Op_fts_insert _
        | Plan.Op_fts_delete _
        | Plan.Op_create_fts_table _ -> true
        | _ -> false
      in
      if is_write
      then execute_with_count ~mode ~clock ~params store cat_v inner
      else
        let* s = to_stream clock params store ~mode ~cat inner in
        let* rows = Lwt_stream.to_list s in
        Lwt.return (List.length rows)
    in
    let elapsed_ms =
      match clock with
      | Some c -> (c () -. t0) *. 1000.0
      | None -> 0.0
    in
    let rows =
      List.mapi
        (fun i row ->
           if i = 0
           then Array.append row [| Row.V_int (Int64.of_int n); Row.V_real elapsed_ms |]
           else nullify row)
        plan_rows
    in
    Lwt.return (Lwt_stream.of_list rows))

and to_stream
      (clock : (unit -> float) option)
      (params : Row.value array)
      (store : S.t)
      ?(mode : txn_mode = Auto)
      ?(cat : Cat.t option = None)
      (op : Plan.op)
  : Row.t Lwt_stream.t Lwt.t
  =
  match op with
  | Plan.Op_seq_scan { table_meta } -> stream_seq_scan clock params store mode table_meta
  | Plan.Op_col_seq_scan { table_meta } ->
    stream_col_seq_scan clock params store mode table_meta
  | Plan.Op_filter { pred; child } -> stream_filter clock params store mode cat pred child
  | Plan.Op_project { ordinals; child } ->
    let* inner = to_stream clock params store ~mode ~cat child in
    Lwt.return (Lwt_stream.map (project_row ordinals) inner)
  | Plan.Op_expr_project { exprs; child } ->
    stream_expr_project clock params store mode cat exprs child
  | Plan.Op_sort { keys; child } -> stream_sort clock params store mode cat keys child
  | Plan.Op_limit { limit; offset; child } ->
    let* inner = to_stream clock params store ~mode ~cat child in
    let* rows = Lwt_stream.to_list inner in
    let rows' = List.filteri (fun i _ -> i >= offset && i < offset + limit) rows in
    Lwt.return (Lwt_stream.of_list rows')
  | Plan.Op_distinct { child } ->
    let* inner = to_stream clock params store ~mode ~cat child in
    let seen = Hashtbl.create 64 in
    Lwt.return
      (Lwt_stream.filter
         (fun row ->
            let k = row_key row in
            if Hashtbl.mem seen k
            then false
            else (
              Hashtbl.replace seen k ();
              true))
         inner)
  | Plan.Op_index_lookup
      { table_tree; idx_tree; col_idx = _; col_type; lookup_val; table_meta } ->
    stream_index_lookup
      clock
      params
      store
      mode
      table_tree
      idx_tree
      col_type
      lookup_val
      table_meta
  | Plan.Op_rowid_lookup { table_meta; lookup_val } ->
    stream_rowid_lookup clock params store mode lookup_val table_meta
  | Plan.Op_nested_loop_join
      { left
      ; right_meta
      ; idx_tree
      ; right_col_idx = _
      ; left_col_idx
      ; join_kind
      ; right_col_offset = _
      ; n_right_cols
      } ->
    stream_nested_loop_join
      clock
      params
      store
      mode
      cat
      left
      right_meta
      idx_tree
      left_col_idx
      join_kind
      n_right_cols
  | Plan.Op_hash_join
      { left; right; left_key; right_key; join_kind; right_col_offset = _; n_right_cols }
    ->
    stream_hash_join
      clock
      params
      store
      mode
      cat
      left
      right
      left_key
      right_key
      join_kind
      n_right_cols
  | Plan.Op_aggregate { child; group_cols; aggs; having; proj; windows = agg_windows } ->
    stream_aggregate
      clock
      params
      store
      mode
      cat
      child
      group_cols
      aggs
      having
      proj
      agg_windows
  | Plan.Op_fts_seq_scan { fts_meta; where } ->
    stream_fts_seq_scan clock params store mode fts_meta where
  | Plan.Op_fts_match_scan { fts_meta; query; proj; include_rank; snippets } ->
    stream_fts_match_scan
      clock
      params
      store
      mode
      fts_meta
      query
      proj
      include_rank
      snippets
  | Plan.Op_pragma_rows { rows } -> Lwt.return (Lwt_stream.of_list rows)
  | Plan.Op_pragma_get_user_version ->
    S.with_ro store
    @@ fun tx ->
    let* v = Cat.read_user_version_tx tx in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int v |] ])
  | Plan.Op_pragma_get_fk ->
    let v =
      match cat with
      | None -> false
      | Some cat -> Cat.get_fk_enforcement cat
    in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int (if v then 1L else 0L) |] ])
  | Plan.Op_pragma_get_recursive_triggers ->
    let v =
      match cat with
      | None -> true
      | Some cat -> Cat.get_recursive_triggers cat
    in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int (if v then 1L else 0L) |] ])
  | Plan.Op_pragma_get_wal_autocheckpoint ->
    let n = S.wal_autocheckpoint store in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int (Int64.of_int n) |] ])
  | Plan.Op_pragma_get_synchronous ->
    let s = S.string_of_durability (S.durability store) in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_text s |] ])
  | Plan.Op_pragma_get_wal_batch_commits ->
    let n = S.sync_batch_commits store in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int (Int64.of_int n) |] ])
  | Plan.Op_pragma_get_wal_batch_interval_ms ->
    let n = S.sync_batch_interval_ms store in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int (Int64.of_int n) |] ])
  | Plan.Op_pragma_get_defer_fk ->
    let v =
      match cat with
      | None -> false
      | Some cat -> Cat.get_defer_fks_pragma cat
    in
    Lwt.return (Lwt_stream.of_list [ [| Row.V_int (if v then 1L else 0L) |] ])
  | Plan.Op_pragma_integrity_check -> stream_pragma_integrity_check store cat
  | Plan.Op_sqlite_master -> stream_sqlite_master store cat
  | Plan.Op_sqlite_sequence -> stream_sqlite_sequence cat
  | Plan.Op_union { all; left; right } ->
    stream_union clock params store mode cat all left right
  | Plan.Op_intersect { left; right } ->
    stream_intersect clock params store mode cat left right
  | Plan.Op_except { left; right } -> stream_except clock params store mode cat left right
  | Plan.Op_insert { table_meta; returning; _ }
    when returning <> [] && Cat.is_columnar table_meta ->
    Lwt.fail_with "RETURNING is not supported on columnar tables"
  | Plan.Op_insert { table_meta; ordinals; values; on_conflict; returning; upsert_update }
    when returning <> [] ->
    stream_insert_returning
      clock
      params
      store
      mode
      cat
      table_meta
      ordinals
      values
      on_conflict
      returning
      upsert_update
  | Plan.Op_update { table_meta; returning; _ }
    when returning <> [] && Cat.is_columnar table_meta ->
    Lwt.fail_with "RETURNING is not supported on columnar tables"
  | Plan.Op_update
      { table_meta; assignments; where; order; limit; offset; indexes; returning }
    when returning <> [] ->
    stream_update_returning
      clock
      params
      store
      mode
      cat
      table_meta
      assignments
      where
      order
      limit
      offset
      indexes
      returning
  | Plan.Op_delete { table_meta; returning; _ }
    when returning <> [] && Cat.is_columnar table_meta ->
    Lwt.fail_with "RETURNING is not supported on columnar tables"
  | Plan.Op_delete { table_meta; where; order; limit; offset; indexes; returning }
    when returning <> [] ->
    stream_delete_returning
      clock
      params
      store
      mode
      cat
      table_meta
      where
      order
      limit
      offset
      indexes
      returning
  | Plan.Op_changes ->
    failwith "Exec.to_stream: Op_changes must be intercepted in db.ml query"
  | Plan.Op_last_insert_rowid ->
    failwith "Exec.to_stream: Op_last_insert_rowid must be intercepted in db.ml query"
  | Plan.Op_total_changes ->
    failwith "Exec.to_stream: Op_total_changes must be intercepted in db.ml query"
  | Plan.Op_const_select { exprs } -> stream_const_select clock params store cat exprs
  | Plan.Op_with_cte { cte_name; def; query; recursive = false } ->
    let* def_stream = to_stream clock params store ~mode ~cat def in
    let* cte_rows = Lwt_stream.to_list def_stream in
    let patched = substitute_cte ~cte_name ~rows:cte_rows query in
    to_stream clock params store ~mode ~cat patched
  | Plan.Op_with_cte { cte_name; def; query; recursive = true } ->
    stream_with_cte_recursive clock params store mode cat cte_name def query
  | Plan.Op_cte_scan { cte_name; _ } ->
    failwith
      (Printf.sprintf
         "Exec: unsubstituted Op_cte_scan '%s' — internal planner error"
         cte_name)
  | Plan.Op_window { child; windows; n_input_cols = _ } ->
    stream_window clock params store mode cat child windows
  | Plan.Op_no_op -> Lwt.return (Lwt_stream.of_list [])
  | Plan.Op_explain { analyze; inner } ->
    stream_explain clock params store mode cat analyze inner
  | Plan.Op_create_table _
  | Plan.Op_col_create_table _
  | Plan.Op_create_index _
  | Plan.Op_drop_table _
  | Plan.Op_drop_index _
  | Plan.Op_create_fts_table _
  | Plan.Op_fts_insert _
  | Plan.Op_fts_delete _
  | Plan.Op_alter_table _
  | Plan.Op_create_view _
  | Plan.Op_create_reactive_view _
  | Plan.Op_drop_view _
  | Plan.Op_create_trigger _
  | Plan.Op_drop_trigger _
  | Plan.Op_begin
  | Plan.Op_commit
  | Plan.Op_rollback
  | Plan.Op_savepoint _
  | Plan.Op_release _
  | Plan.Op_rollback_to _
  | Plan.Op_pragma_set_user_version _
  | Plan.Op_pragma_set_fk _
  | Plan.Op_pragma_set_recursive_triggers _
  | Plan.Op_pragma_set_defer_fk _
  | Plan.Op_pragma_set_wal_autocheckpoint _
  | Plan.Op_pragma_wal_checkpoint
  | Plan.Op_vacuum
  | Plan.Op_attach _
  | Plan.Op_detach _
  | Plan.Op_active_database_set _ ->
    failwith "Exec.query: use Exec.execute for write operations"
  | Plan.Op_database_list | Plan.Op_active_database_get ->
    failwith "Exec.query: routed via Db.query (no Db handle)"
  | Plan.Op_insert _ | Plan.Op_insert_select _ | Plan.Op_update _ | Plan.Op_delete _ ->
    failwith "Exec.query: use Exec.execute for write operations"
  | Plan.Op_seq_set _ | Plan.Op_seq_reset _ ->
    failwith "Exec.query: use Exec.execute for write operations"
  | Plan.Op_pragma_set_synchronous _
  | Plan.Op_pragma_set_wal_batch_commits _
  | Plan.Op_pragma_set_wal_batch_interval_ms _ ->
    failwith "Exec.query: use Exec.execute for write operations"
;;

(* Wire the forward reference so execute_with_count can call to_stream for
   Op_insert_select.  This runs once at module initialization time, after both
   functions are fully defined in the let-rec block above. *)
let () = to_stream_ref := to_stream

(* ------------------------------------------------------------------ *)
(* Public query entry point                                             *)
(* ------------------------------------------------------------------ *)

(* #239: [used_index] is a plan-time fact — does the query's base access reach
   the data through an index/rowid/FTS seek, or a full table scan?  Descends
   through the row-shaping wrappers to the base; [true] if ANY base uses a seek.
   A nested-loop join always probes its right table by index, so it counts. *)
let rec op_uses_index (op : Plan.op) : bool =
  match op with
  | Plan.Op_index_lookup _ | Plan.Op_rowid_lookup _ | Plan.Op_fts_match_scan _ -> true
  | Plan.Op_seq_scan _ | Plan.Op_col_seq_scan _ | Plan.Op_fts_seq_scan _ -> false
  | Plan.Op_filter { child; _ }
  | Plan.Op_project { child; _ }
  | Plan.Op_expr_project { child; _ }
  | Plan.Op_sort { child; _ }
  | Plan.Op_limit { child; _ }
  | Plan.Op_distinct { child }
  | Plan.Op_aggregate { child; _ }
  | Plan.Op_window { child; _ } -> op_uses_index child
  | Plan.Op_nested_loop_join _ -> true
  | Plan.Op_hash_join { left; right; _ } -> op_uses_index left || op_uses_index right
  | Plan.Op_union { left; right; _ }
  | Plan.Op_intersect { left; right }
  | Plan.Op_except { left; right } -> op_uses_index left || op_uses_index right
  | Plan.Op_with_cte { query; _ } -> op_uses_index query
  (* Conservative: anything not recognised as a seek-bearing base access (or a
     wrapper over one) reports [false].  KEEP IN SYNC: a NEW index/seek-bearing
     plan op added here would silently report [used_index = false] until a case
     is added above. *)
  | _ -> false
;;

let query
      ?(mode = Auto)
      ?(clock : (unit -> float) option = None)
      ?(params = [||])
      ?(stats : query_stats option)
      (store : S.t)
      (cat : Cat.t)
      (op : Plan.op)
  : Row.t Lwt_stream.t Lwt.t
  =
  let body () =
    match stats with
    | None -> to_stream clock params store ~mode ~cat:(Some cat) op
    | Some s ->
      s.used_index <- op_uses_index op;
      (* Run the whole stream construction under the stats record so the base
         scanners capture it (Lwt sequence-associated storage); wrap the result
         to count rows actually delivered once the caller drains it. *)
      Lwt.with_value query_stats_key (Some s)
      @@ fun () ->
      let* stream = to_stream clock params store ~mode ~cat:(Some cat) op in
      Lwt.return
        (Lwt_stream.map
           (fun row ->
              s.rows_returned <- s.rows_returned + 1;
              row)
           stream)
  in
  (* #262: publish the txn mode to subquery evaluation only when inside an
     explicit transaction.  In [Auto] mode [current_txn_mode] already defaults to
     [Auto], so the common read path pays no [with_value] — preserving the
     zero-overhead scan path (#259). *)
  match mode with
  | Auto -> body ()
  | In_txn _ | In_ro_txn _ -> Lwt.with_value txn_mode_key (Some mode) body
;;

[@@@ai_disclosure "ai-generated"]
[@@@ai_model "claude-opus-4-7"]
[@@@ai_provider "Anthropic"]