rayforce-sys 1.3.0

Raw FFI bindings to the RayforceDB v2 core (librayforce)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
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
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
/*
 *   Copyright (c) 2025-2026 Anton Kundenko <singaraiona@gmail.com>
 *   All rights reserved.

 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:

 *   The above copyright notice and this permission notice shall be included in all
 *   copies or substantial portions of the Software.

 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *   SOFTWARE.
 */

/**   Query bridge: select, update, insert, upsert, join operations.
 *   Extracted from eval.c.
 */

#include "lang/internal.h"
#include "io/parquet.h"
#include "lang/eval.h"
#include "lang/env.h"
#include "ops/ops.h"
#include "ops/internal.h"
#include "ops/hash.h"
#include "ops/glob.h"         /* ray_strpat — str-find in the derived-key program */
#include "ops/idxop.h"        /* ray_index_kind / RAY_IDX_DICT — dict-code row_gid */
#include "ops/agg_engine.h"   /* agg_select_distinct — dict-linked pure-distinct path */
#include "core/runtime.h"     /* __VM — per-thread query/eval context */
#include "ops/rowsel.h"
#include "ops/fused_group.h"
#include "ops/fused_topk.h"
#include "ops/hll.h"
#include "ops/cdfuse.h"       /* ray_cd_fused — fused grouped count-distinct */
#include "ops/temporal.h"
#include "core/profile.h"
#include "table/sym.h"
#include "table/domain.h"
#include "table/dict.h"
#include "mem/heap.h"

#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdatomic.h>

/* ══════════════════════════════════════════
 * Select query — DAG bridge
 * ══════════════════════════════════════════ */

/* sym_id_runtime / sym_cell_runtime_id (cell id → runtime-domain id)
 * and sym_domain_rep (PARTED/MAPCOMMON domain representative) are
 * shared from lang/internal.h since the Task-3/4 sweeps. */

/* Helper: look up a key in a select-clause dict (RAY_DICT).
 * Returns the value expression (unevaluated), or NULL if not found.
 * Dict keys are CELL-DATA of the keys vec — resolve through its domain
 * (clause dicts are parser-built and runtime-domain today; evaluated
 * user dicts may carry any domain). */
static ray_t* dict_get(ray_t* dict, const char* key) {
    if (!dict || dict->type != RAY_DICT) return NULL;
    size_t key_len = strlen(key);
    ray_t* keys = ray_dict_keys(dict);
    ray_t* vals = ray_dict_vals(dict);
    if (!keys || keys->type != RAY_SYM || !vals || vals->type != RAY_LIST)
        return NULL;
    ray_t** vptrs = (ray_t**)ray_data(vals);
    for (int64_t i = 0; i < keys->len; i++) {
        ray_t* s = ray_sym_vec_cell(keys, i);
        if (s && ray_str_len(s) == key_len &&
            memcmp(ray_str_ptr(s), key, key_len) == 0)
            return vptrs[i];
    }
    return NULL;
}

/* Returns the RUNTIME id of `key` when the dict contains it, -1 when
 * absent.  Callers compare the result against dict_pair_view key atoms
 * (runtime-domain by the atom rule) and ray_sym_intern'd clause names —
 * so the match must be reported in the runtime domain, not as a raw
 * cell id. */
static int64_t dict_key_id(ray_t* dict, const char* key) {
    if (!dict || dict->type != RAY_DICT) return -1;
    size_t key_len = strlen(key);
    ray_t* keys = ray_dict_keys(dict);
    if (!keys || keys->type != RAY_SYM) return -1;
    for (int64_t i = 0; i < keys->len; i++) {
        ray_t* s = ray_sym_vec_cell(keys, i);
        if (s && ray_str_len(s) == key_len &&
            memcmp(ray_str_ptr(s), key, key_len) == 0)
            return ray_sym_intern(key, key_len);
    }
    return -1;
}

/* Flatten a RAY_DICT (keys SYM vec + vals LIST) into a transient
 * [k0,v0,k1,v1,...] array view so the existing dict-walking loops in
 * ray_select_fn et al. can iterate without rewriting every site.
 *
 * Growable dict view (cut 2): one exact-size scratch carve sized from
 * keys->len — n ray_t key atoms, then 2n interleaved [k0,v0,...] pointer
 * slots.  Key atoms are materialized by value; value pointers are
 * BORROWED from the dict's vals LIST (the dict must outlive the view),
 * same as before.  `*out_n` is the interleaved element count (2n);
 * `*out_n = -1` now means allocation failure, never overflow — the view
 * can no longer overflow.  Every DICT_VIEW_OPEN pairs with a
 * DICT_VIEW_CLOSE on every exit.
 *
 * (`ray_t` is 8-aligned and its size is a multiple of 8, so the
 * keybuf-then-pointers single-carve layout is safe.)
 *
 * Historical note: the pre-guard version of this helper wrote into
 * fixed stack buffers and silently corrupted the stack on user-controlled
 * dicts with more pairs than fit — the exact-size carve retires both the
 * corruption hazard and the fixed 256-entry cap that guarded against it. */
_Static_assert(sizeof(ray_t) % 8 == 0, "dict view carve layout");
static void dict_pair_view(ray_t* d, ray_t** hdr_out, ray_t*** view_out,
                           int64_t* out_n) {
    *hdr_out = NULL; *view_out = NULL; *out_n = 0;
    if (!d || d->type != RAY_DICT) return;
    ray_t* keys = ray_dict_keys(d);
    ray_t* vals = ray_dict_vals(d);
    if (!keys || keys->type != RAY_SYM || !vals || vals->type != RAY_LIST) return;
    int64_t n = keys->len;
    size_t bytes = (size_t)n * sizeof(ray_t)
                 + (size_t)n * 2 * sizeof(ray_t*);
    uint8_t* blk = (uint8_t*)scratch_alloc(hdr_out, bytes ? bytes : 8);
    if (!blk) { *out_n = -1; return; }
    ray_t*  keybuf = (ray_t*)blk;
    ray_t** elems  = (ray_t**)(blk + (size_t)n * sizeof(ray_t));
    ray_t** vptrs = (ray_t**)ray_data(vals);
    for (int64_t i = 0; i < n; i++) {
        memset(&keybuf[i], 0, sizeof(ray_t));
        keybuf[i].type = -RAY_SYM;
        /* cell-data flows into a SYM ATOM — atoms are runtime-domain by
         * design, so re-express the cell id there (raw-copy fast path
         * while the keys vec is runtime-domain). */
        keybuf[i].i64  = sym_cell_runtime_id(keys, i);
        elems[i*2]   = &keybuf[i];
        elems[i*2+1] = vptrs[i];
    }
    *view_out = elems; *out_n = 2 * n;
}

#define DICT_VIEW_DECL(name)                            \
    ray_t*  name##_hdr = NULL;                          \
    ray_t** name = NULL;                                \
    int64_t name##_n = 0
#define DICT_VIEW_OPEN(d, name)                          \
    dict_pair_view((d), &name##_hdr, &name, &name##_n)
/* Returns true if the open hit OOM (post-cut-2: overflow is impossible).
 * Caller closes the view and returns an "oom" error / decline. */
#define DICT_VIEW_OVERFLOW(name) ((name##_n) < 0)
#define DICT_VIEW_CLOSE(name)    do { scratch_free(name##_hdr); name##_hdr = NULL; } while (0)

/* Convert a RAY_DICT (keys, vals) into a transient interleaved
 * [k0_atom, v0, k1_atom, v1, …] RAY_LIST.  Used by select's group-by
 * aggregation paths which were written for the old in-place pair-array
 * representation of grouping output.  Returns an owned RAY_LIST (rc=1).
 * Atom keys are freshly boxed for typed-vector key columns (sym, i64,
 * etc.); for RAY_LIST keys they are retained borrows. */
static ray_t* groups_to_pair_list(ray_t* d) {
    if (!d || d->type != RAY_DICT) return ray_error("type", "group: expected groups dict, got %s", d ? ray_type_name(d->type) : "null");
    ray_t* keys = ray_dict_keys(d);
    ray_t* vals = ray_dict_vals(d);
    int64_t n = keys ? keys->len : 0;
    ray_t* out = ray_list_new(n * 2);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    ray_t** vptrs = (vals && vals->type == RAY_LIST) ? (ray_t**)ray_data(vals) : NULL;
    for (int64_t i = 0; i < n; i++) {
        ray_t* k = NULL;
        if (!keys) {
            k = NULL;
        } else if (keys->type == RAY_LIST) {
            k = ((ray_t**)ray_data(keys))[i];
            if (k) ray_retain(k);
        } else {
            void* base = ray_data(keys);
            switch (keys->type) {
                /* group-key cell becomes a SYM ATOM: runtime-domain by
                 * the atom rule (no-op while keys is runtime-domain). */
                case RAY_SYM: k = ray_sym(sym_cell_runtime_id(keys, i)); break;
                case RAY_I64:
                case RAY_TIMESTAMP: k = ray_i64(((int64_t*)base)[i]); break;
                case RAY_I32:
                case RAY_DATE:
                case RAY_TIME: k = ray_i32(((int32_t*)base)[i]); break;
                case RAY_I16: k = ray_i16(((int16_t*)base)[i]); break;
                case RAY_BOOL:
                case RAY_U8:  k = ray_u8(((uint8_t*)base)[i]); break;
                case RAY_F64: k = ray_f64(((double*)base)[i]); break;
                case RAY_STR: { size_t sl = 0; const char* sp = ray_str_vec_get(keys, i, &sl);
                                 k = ray_str(sp ? sp : "", sp ? sl : 0); break; }
                case RAY_GUID: k = ray_guid(((uint8_t*)base) + i * 16); break;
                default: k = NULL; break;
            }
        }
        out = ray_list_append(out, k);
        if (k) ray_release(k);
        ray_t* v = vptrs ? vptrs[i] : NULL;
        out = ray_list_append(out, v);
    }
    return out;
}

typedef struct {
    ray_t* col;
    int8_t base_type;
    ray_t** segs;
    ray_t* mc_keys;
    const int64_t* mc_counts;
    int64_t n_segs;
    int64_t seg_idx;
    int64_t seg_start;
    int64_t seg_end;
} query_key_reader_t;

static bool query_key_reader_init(query_key_reader_t* r, ray_t* col) {
    memset(r, 0, sizeof(*r));
    r->col = col;
    r->seg_end = INT64_MAX;
    if (!col) return false;
    if (RAY_IS_PARTED(col->type)) {
        r->base_type = (int8_t)RAY_PARTED_BASETYPE(col->type);
        r->segs = (ray_t**)ray_data(col);
        r->n_segs = col->len;
        r->seg_end = (r->n_segs > 0 && r->segs[0]) ? r->segs[0]->len : 0;
        return true;
    }
    if (col->type == RAY_MAPCOMMON) {
        ray_t** ptrs = (ray_t**)ray_data(col);
        r->mc_keys = ptrs[0];
        ray_t* counts = ptrs[1];
        if (!r->mc_keys || !counts || counts->type != RAY_I64) return false;
        r->base_type = r->mc_keys->type;
        r->mc_counts = (const int64_t*)ray_data(counts);
        r->n_segs = r->mc_keys->len;
        r->seg_end = (r->n_segs > 0) ? r->mc_counts[0] : 0;
        return true;
    }
    r->base_type = col->type;
    return true;
}

static bool query_key_reader_read(query_key_reader_t* r, int64_t row,
                                  int64_t* out, uint8_t* is_null) {
    if (!r || !r->col || !out || !is_null) return false;
    *is_null = 0;
    *out = 0;

    if (!RAY_IS_PARTED(r->col->type) && r->col->type != RAY_MAPCOMMON) {
        *is_null = ray_vec_is_null(r->col, row);
        if (*is_null) return true;
        if (r->base_type == RAY_F64) memcpy(out, &((double*)ray_data(r->col))[row], 8);
        else *out = read_col_i64(ray_data(r->col), row, r->base_type, r->col->attrs);
        return true;
    }

    while (row >= r->seg_end && r->seg_idx + 1 < r->n_segs) {
        r->seg_start = r->seg_end;
        r->seg_idx++;
        int64_t len = 0;
        if (r->col->type == RAY_MAPCOMMON) len = r->mc_counts[r->seg_idx];
        else if (r->segs[r->seg_idx]) len = r->segs[r->seg_idx]->len;
        r->seg_end += len;
    }
    if (row < r->seg_start || row >= r->seg_end) return false;

    if (r->col->type == RAY_MAPCOMMON) {
        if (r->base_type == RAY_F64)
            memcpy(out, (const char*)ray_data(r->mc_keys) + (size_t)r->seg_idx * 8, 8);
        else
            *out = read_col_i64(ray_data(r->mc_keys), r->seg_idx,
                                r->base_type, r->mc_keys->attrs);
        return true;
    }

    ray_t* seg = r->segs[r->seg_idx];
    if (!seg) return false;
    int64_t local = row - r->seg_start;
    *is_null = ray_vec_is_null(seg, local);
    if (*is_null) return true;
    if (r->base_type == RAY_F64) memcpy(out, &((double*)ray_data(seg))[local], 8);
    else *out = read_col_i64(ray_data(seg), local, r->base_type, seg->attrs);
    return true;
}

/* Map a Rayfall builtin name to a DAG binary op constructor */
typedef ray_op_t* (*dag_binary_ctor)(ray_graph_t*, ray_op_t*, ray_op_t*);
typedef ray_op_t* (*dag_unary_ctor)(ray_graph_t*, ray_op_t*);

static bool dag_numeric_type_admitted(int8_t t) {
    if (t <= 0) return true;  /* unknown/uninferred: let executor validate */
    if (RAY_IS_PARTED(t)) t = (int8_t)RAY_PARTED_BASETYPE(t);
    return t == RAY_BOOL || t == RAY_U8 || t == RAY_I16 ||
           t == RAY_I32 || t == RAY_I64 || t == RAY_F32 || t == RAY_F64;
}

static bool dag_pow_type_admitted(int8_t t) {
    return dag_numeric_type_admitted(t);
}

static bool dag_type_is_temporal(int8_t t) {
    if (t <= 0) return false;
    if (RAY_IS_PARTED(t)) t = (int8_t)RAY_PARTED_BASETYPE(t);
    return t == RAY_DATE || t == RAY_TIME || t == RAY_TIMESTAMP;
}

/* Arithmetic on a symbol has no meaning — the eval path rejects it
 * (`cannot add sym and i64`, arith.c).  The DAG used to admit it because
 * `promote` folds RAY_SYM into I64 and the constant folder reads a symbol
 * atom as its interned id, so `(+ 'name 1)` and `(+ sym_col 1)` returned
 * ids plus one without a word.  Declining here sends the projection to the
 * per-row eval fallback, which raises the same error as outside a query.
 * Comparisons and membership keep SYM: they are not arithmetic. */
static const char* dag_arith_verb(const char* fname, size_t fname_len) {
    if (fname_len == 1) {
        switch (fname[0]) {
            case '+': return "add";
            case '-': return "subtract";
            case '*': return "multiply";
            case '/': return "divide";
            case '%': return "mod";
        }
    }
    return fname_len == 3 && memcmp(fname, "pow", 3) == 0 ? "pow" : "div";
}

static bool dag_arith_rejects_sym(const char* fname, size_t fname_len,
                                  int8_t lt, int8_t rt) {
    bool arith = (fname_len == 1 && (fname[0] == '+' || fname[0] == '-' ||
                                     fname[0] == '*' || fname[0] == '/' ||
                                     fname[0] == '%')) ||
                 (fname_len == 3 && (memcmp(fname, "div", 3) == 0 ||
                                     memcmp(fname, "pow", 3) == 0));
    return arith && (lt == RAY_SYM || rt == RAY_SYM);
}

static bool dag_temporal_arith_needs_eval(const char* name, size_t len,
                                          int8_t left_type, int8_t right_type) {
    if (!dag_type_is_temporal(left_type) && !dag_type_is_temporal(right_type))
        return false;
    return len == 1 && (name[0] == '+' || name[0] == '-' || name[0] == '*');
}

/* A comparison between a temporal and a float has no meaning — the eval
 * path rejects it (`cannot compare timestamp and f64`, cmp.c).  The DAG
 * kernel promoted both sides to double, where a TIMESTAMP near 2024 has a
 * 128 ns ULP: against the F64 mean of the column, (> ts (avg ts)) read
 * every row as equal to the mean (#694).  Integer scalars stay on the
 * exact int64 kernel, as they always did. */
static bool dag_cmp_rejects_temporal_float(const char* fname, size_t fname_len,
                                           int8_t lt, int8_t rt) {
    bool cmp = (fname_len == 1 && (fname[0] == '>' || fname[0] == '<')) ||
               (fname_len == 2 && fname[1] == '=' &&
                (fname[0] == '>' || fname[0] == '<' ||
                 fname[0] == '=' || fname[0] == '!'));
    if (!cmp) return false;
    if (RAY_IS_PARTED(lt)) lt = (int8_t)RAY_PARTED_BASETYPE(lt);
    if (RAY_IS_PARTED(rt)) rt = (int8_t)RAY_PARTED_BASETYPE(rt);
    bool lf = lt == RAY_F64 || lt == RAY_F32;
    bool rf = rt == RAY_F64 || rt == RAY_F32;
    return (dag_type_is_temporal(lt) && rf) || (lf && dag_type_is_temporal(rt));
}

/* A column operand of `and` / `or` must be boolean: the kernel read an I64
 * column by its bytes, so `(and x (> y 2))` kept rows a where: on `x`
 * alone rejects (#678).  A literal keeps its scalar truth value, as a
 * where: scalar does.  Sets compile_err and returns false on a bad operand. */
static bool dag_bool_operand_ok(ray_graph_t* g, ray_op_t* a, bool is_and) {
    if (a->opcode == OP_CONST || a->out_type <= 0 || a->out_type == RAY_BOOL)
        return true;
    if (!g->compile_err)
        g->compile_err = ray_error("type", "%s: operand must be boolean, got %s",
                                   is_and ? "and" : "or", ray_type_name(a->out_type));
    return false;
}

static bool dag_unary_numeric_name(const char* name, size_t len) {
    if (len == 3)
        return memcmp(name, "sin", 3) == 0 ||
               memcmp(name, "cos", 3) == 0 ||
               memcmp(name, "tan", 3) == 0;
    if (len == 4)
        return memcmp(name, "asin", 4) == 0 ||
               memcmp(name, "acos", 4) == 0 ||
               memcmp(name, "atan", 4) == 0;
    if (len == 6)
        return memcmp(name, "signum", 6) == 0;
    if (len == 10)
        return memcmp(name, "reciprocal", 10) == 0;
    return false;
}

static dag_binary_ctor resolve_binary_dag(int64_t sym_id) {
    ray_t* s = ray_sym_str(sym_id);
    if (!s) return NULL;
    const char* name = ray_str_ptr(s);
    size_t len = ray_str_len(s);
    if (len == 1) {
        switch (name[0]) {
            case '+': return ray_add;
            case '-': return ray_sub;
            case '*': return ray_mul;
            case '/': return ray_div;
            case '%': return ray_mod;
            case '>': return ray_gt;
            case '<': return ray_lt;
        }
    } else if (len == 2) {
        if (name[0] == '>' && name[1] == '=') return ray_ge;
        if (name[0] == '<' && name[1] == '=') return ray_le;
        if (name[0] == '=' && name[1] == '=') return ray_eq;
        if (name[0] == '!' && name[1] == '=') return ray_ne;
        if (name[0] == 'o' && name[1] == 'r') return ray_or;
        if (name[0] == 'i' && name[1] == 'n') return ray_in;
    } else if (len == 3) {
        if (memcmp(name, "and",  3) == 0) return ray_and;
        if (memcmp(name, "div",  3) == 0) return ray_idiv;
        if (memcmp(name, "pow",  3) == 0) return ray_pow_op;
    } else if (len == 4) {
        if (memcmp(name, "like", 4) == 0) return ray_like;
    } else if (len == 5) {
        if (memcmp(name, "ilike", 5) == 0) return ray_ilike;
    } else if (len == 6) {
        if (memcmp(name, "not-in", 6) == 0) return ray_not_in;
    } else if (len == 8) {
        if (memcmp(name, "str-find", 8) == 0) return ray_str_find_op;
    }
    return NULL;
}

static dag_unary_ctor resolve_unary_dag(int64_t sym_id) {
    ray_t* s = ray_sym_str(sym_id);
    if (!s) return NULL;
    const char* name = ray_str_ptr(s);
    size_t len = ray_str_len(s);
    if (len == 3) {
        if (memcmp(name, "lag", 3) == 0) return ray_lag_op;
        if (memcmp(name, "neg", 3) == 0) return ray_neg;
        if (memcmp(name, "not", 3) == 0) return ray_not;
        if (memcmp(name, "abs", 3) == 0) return ray_abs;
        if (memcmp(name, "exp", 3) == 0) return ray_exp_op;
        if (memcmp(name, "log", 3) == 0) return ray_log_op;
        if (memcmp(name, "sin", 3) == 0) return ray_sin_op;
        if (memcmp(name, "cos", 3) == 0) return ray_cos_op;
        if (memcmp(name, "tan", 3) == 0) return ray_tan_op;
    } else if (len == 4) {
        if (memcmp(name, "lead", 4) == 0) return ray_lead_op;
        if (memcmp(name, "sums", 4) == 0) return ray_sums_op;
        if (memcmp(name, "avgs", 4) == 0) return ray_avgs_op;
        if (memcmp(name, "mins", 4) == 0) return ray_mins_op;
        if (memcmp(name, "maxs", 4) == 0) return ray_maxs_op;
        if (memcmp(name, "prds", 4) == 0) return ray_prds_op;
        if (memcmp(name, "asin", 4) == 0) return ray_asin_op;
        if (memcmp(name, "acos", 4) == 0) return ray_acos_op;
        if (memcmp(name, "atan", 4) == 0) return ray_atan_op;
        if (memcmp(name, "ceil",  4) == 0) return ray_ceil_op;
        if (memcmp(name, "sqrt",  4) == 0) return ray_sqrt_op;
        if (memcmp(name, "trim",  4) == 0) return ray_trim_op;
        if (memcmp(name, "nil?",  4) == 0) return ray_isnull;
    } else if (len == 5) {
        if (memcmp(name, "fills", 5) == 0) return ray_fills_op;
        if (memcmp(name, "floor", 5) == 0) return ray_floor_op;
        if (memcmp(name, "round", 5) == 0) return ray_round_op;
        if (memcmp(name, "upper", 5) == 0) return ray_upper;
        if (memcmp(name, "lower", 5) == 0) return ray_lower;
    } else if (len == 6) {
        if (memcmp(name, "deltas", 6) == 0) return ray_deltas_op;
        if (memcmp(name, "ratios", 6) == 0) return ray_ratios_op;
        if (memcmp(name, "differ", 6) == 0) return ray_differ_op;
        if (memcmp(name, "strlen", 6) == 0) return ray_strlen;
        if (memcmp(name, "signum", 6) == 0) return ray_signum_op;
    } else if (len == 10) {
        if (memcmp(name, "reciprocal", 10) == 0) return ray_reciprocal_op;
    }
    return NULL;
}

static uint16_t resolve_moving_opcode(int64_t sym_id) {
    ray_t* s = ray_sym_str(sym_id);
    if (!s) return 0;
    const char* name = ray_str_ptr(s);
    size_t len = ray_str_len(s);
    if (len == 4) {
        if (memcmp(name, "msum", 4) == 0) return OP_MSUM;
        if (memcmp(name, "mavg", 4) == 0) return OP_MAVG;
        if (memcmp(name, "mmin", 4) == 0) return OP_MMIN;
        if (memcmp(name, "mmax", 4) == 0) return OP_MMAX;
        if (memcmp(name, "mvar", 4) == 0) return OP_MVAR;
        if (memcmp(name, "mdev", 4) == 0) return OP_MDEV;
    } else if (len == 6) {
        if (memcmp(name, "mcount", 6) == 0) return OP_MCOUNT;
    }
    return 0;
}

static bool const_op_i64(ray_graph_t* g, ray_op_t* op, int64_t* out) {
    if (!op || op->opcode != OP_CONST) return false;
    ray_op_ext_t* ext = find_ext(g, op->id);
    if (!ext || !ext->literal || RAY_IS_ERR(ext->literal)) return false;
    ray_t* v = ext->literal;
    switch (v->type) {
        case -RAY_I64:  *out = v->i64; return !RAY_ATOM_IS_NULL(v);
        case -RAY_I32:  *out = (int64_t)v->i32; return !RAY_ATOM_IS_NULL(v);
        case -RAY_I16:  *out = (int64_t)v->i16; return !RAY_ATOM_IS_NULL(v);
        case -RAY_U8:
        case -RAY_BOOL: *out = (int64_t)v->u8; return !RAY_ATOM_IS_NULL(v);
        default: return false;
    }
}

/* Map Rayfall aggregation name to DAG opcode */
static uint16_t resolve_agg_opcode(int64_t sym_id) {
    ray_t* s = ray_sym_str(sym_id);
    if (!s) return 0;
    const char* name = ray_str_ptr(s);
    size_t len = ray_str_len(s);
    if (len == 3 && memcmp(name, "sum",   3) == 0) return OP_SUM;
    if (len == 3 && memcmp(name, "avg",   3) == 0) return OP_AVG;
    if (len == 3 && memcmp(name, "min",   3) == 0) return OP_MIN;
    if (len == 3 && memcmp(name, "max",   3) == 0) return OP_MAX;
    if (len == 3 && memcmp(name, "dev",   3) == 0) return OP_STDDEV_POP;
    if (len == 3 && memcmp(name, "var",   3) == 0) return OP_VAR;
    if (len == 3 && memcmp(name, "all",   3) == 0) return OP_ALL;
    if (len == 3 && memcmp(name, "any",   3) == 0) return OP_ANY;
    if (len == 3 && memcmp(name, "cov",   3) == 0) return OP_COV;
    if (len == 4 && memcmp(name, "prod",  4) == 0) return OP_PROD;
    if (len == 4 && memcmp(name, "last",  4) == 0) return OP_LAST;
    if (len == 4 && memcmp(name, "scov",  4) == 0) return OP_SCOV;
    if (len == 4 && memcmp(name, "wsum",  4) == 0) return OP_WSUM;
    if (len == 4 && memcmp(name, "wavg",  4) == 0) return OP_WAVG;
    if (len == 5 && memcmp(name, "count", 5) == 0) return OP_COUNT;
    if (len == 5 && memcmp(name, "first", 5) == 0) return OP_FIRST;
    if (len == 6 && memcmp(name, "stddev",6) == 0) return OP_STDDEV;
    if (len == 7 && memcmp(name, "dev_pop",      7) == 0) return OP_STDDEV_POP;
    if (len == 7 && memcmp(name, "var_pop",      7) == 0) return OP_VAR_POP;
    if (len == 10 && memcmp(name, "stddev_pop", 10) == 0) return OP_STDDEV_POP;
    if (len == 12 && memcmp(name, "pearson_corr", 12) == 0) return OP_PEARSON_CORR;
    /* Holistic — DAG path skips accumulator slot, fills via post-radix
     * pass over row_gid+grp_cnt (see exec_group + ray_median_per_group_buf). */
    if (len == 3 && memcmp(name, "med",    3) == 0) return OP_MEDIAN;
    if (len == 6 && memcmp(name, "median", 6) == 0) return OP_MEDIAN;
    /* Holistic, binary-shape (col + K literal).  K compiled-time literal,
     * not a DAG input — extracted from the dict expr at planner time and
     * stored in agg_k[].  See ray_topk_per_group_buf for the per-group
     * bounded-heap kernel. */
    if (len == 3 && memcmp(name, "top",    3) == 0) return OP_TOP_N;
    if (len == 3 && memcmp(name, "bot",    3) == 0) return OP_BOT_N;
    if (len == 4 && memcmp(name, "mode",   4) == 0) return OP_MODE;
    if (len == 8 && memcmp(name, "quantile", 8) == 0) return OP_QUANTILE;
    if (len == 10 && memcmp(name, "percentile", 10) == 0) return OP_QUANTILE;
    return 0;
}

/* See the call site in ray_select_impl.  NULL: some output is not answerable
 * from metadata (the caller plans the query as usual). */
static ray_t* select_aggs_from_metadata(ray_t* tbl, ray_t** dict_elems, int64_t dict_n,
                                        int64_t from_id) {
    int64_t nrows = ray_table_nrows(tbl);
    if (nrows <= 0) return NULL;   /* empty tables keep the planner's answers */
    int64_t n_out = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        if (dict_elems[i]->i64 == from_id) continue;
        ray_t* e = dict_elems[i + 1];
        if (!e || e->type != RAY_LIST || ray_len(e) != 2) return NULL;
        ray_t** el = (ray_t**)ray_data(e);
        if (el[0]->type != -RAY_SYM || (el[0]->attrs & ATTR_QUOTED)) return NULL;
        if (el[1]->type != -RAY_SYM || (el[1]->attrs & ATTR_QUOTED)) return NULL;
        ray_t* col = ray_table_get_col(tbl, el[1]->i64);
        if (!col || !ray_is_vec(col) || RAY_IS_PARTED(col->type) || col->type == RAY_MAPCOMMON ||
            (col->attrs & RAY_ATTR_SLICE) || col->len != nrows)
            return NULL;
        uint16_t op = resolve_agg_opcode(el[0]->i64);
        bool int_col = col->type == RAY_I64 || col->type == RAY_I32 ||
                       col->type == RAY_I16 || col->type == RAY_U8;
        int64_t zs, zn, zh; uint64_t zl;
        switch (op) {
        case OP_COUNT: break;
        case OP_MIN: case OP_MAX: {
            if (ray_index_kind(col) != RAY_IDX_CHUNK_ZONE) return NULL;
            ray_index_t* ix = ray_index_payload(col->index);
            if (ix->built_for_len != col->len || ix->u.chunk_zone.is_f64 ||
                !ix->u.chunk_zone.mins || !ix->u.chunk_zone.maxs) return NULL;
            break;
        }
        case OP_SUM:
            if (!int_col || !ray_zone_int_sum(col, &zs, &zn)) return NULL;
            break;
        case OP_AVG:
            if (!int_col || !ray_zone_int_sum128(col, &zh, &zl, &zn)) return NULL;
            break;
        default: return NULL;
        }
        n_out++;
    }
    if (n_out == 0) return NULL;

    ray_t* res = ray_table_new(n_out);
    if (!res || RAY_IS_ERR(res)) return NULL;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id) continue;
        ray_t** el = (ray_t**)ray_data(dict_elems[i + 1]);
        ray_t* col = ray_table_get_col(tbl, el[1]->i64);
        uint16_t op = resolve_agg_opcode(el[0]->i64);
        ray_t* atom = op == OP_COUNT ? ray_i64(nrows)
                    : op == OP_MIN   ? ray_min_fn(col)
                    : op == OP_MAX   ? ray_max_fn(col)
                    : op == OP_SUM   ? ray_sum_fn(col)
                    :                  ray_avg_fn(col);
        if (!atom || RAY_IS_ERR(atom) || !ray_is_atom(atom)) {
            if (atom && RAY_IS_ERR(atom)) ray_error_free(atom); else if (atom) ray_release(atom);
            ray_release(res);
            return NULL;
        }
        ray_t* v = ray_vec_new(-atom->type, 1);
        if (v && !RAY_IS_ERR(v)) v = ray_vec_append(v, &atom->i64);
        if (v && !RAY_IS_ERR(v) && RAY_ATOM_IS_NULL(atom)) ray_vec_set_null(v, 0, true);
        ray_release(atom);
        if (!v || RAY_IS_ERR(v)) { ray_release(res); return NULL; }
        res = ray_table_add_col(res, kid, v);
        ray_release(v);
        if (!res || RAY_IS_ERR(res)) return NULL;
    }
    return res;
}

static bool agg_name_is_percentile(int64_t sym_id) {
    ray_t* s = ray_sym_str(sym_id);
    return s && ray_str_len(s) == 10 &&
           memcmp(ray_str_ptr(s), "percentile", 10) == 0;
}

static int64_t encode_agg_f64_param(double v) {
    int64_t bits = 0;
    memcpy(&bits, &v, sizeof(bits));
    return bits;
}

static bool quantile_literal_prob(ray_t* prob_expr, bool percentile,
                                  double* out_q) {
    if (!prob_expr || !ray_is_atom(prob_expr) ||
        (prob_expr->type == -RAY_SYM && !(prob_expr->attrs & ATTR_QUOTED)) ||
        !is_numeric(prob_expr) || RAY_ATOM_IS_NULL(prob_expr))
        return false;
    double raw = as_f64(prob_expr);
    double scale = percentile ? 100.0 : 1.0;
    if (!__builtin_isfinite(raw) || raw < 0.0 || raw > scale)
        return false;
    if (out_q) *out_q = raw / scale;
    return true;
}

/* True when any GROUP BY key names a BOOL column of `tbl`.  A BOOL-keyed
 * group is emitted in key order (false before true) and query.c reorders the
 * finished result to first-occurrence AFTERWARDS, so a take must not be pushed
 * into the DAG ahead of that fix-up. */
static bool group_keys_have_bool(ray_t* by_expr, ray_t* tbl) {
    if (!by_expr || !tbl || RAY_IS_ERR(tbl) || tbl->type != RAY_TABLE) return true;
    if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED)) {
        ray_t* c = ray_table_get_col(tbl, by_expr->i64);
        return !c || c->type == RAY_BOOL;
    }
    if (ray_is_vec(by_expr) && by_expr->type == RAY_SYM) {
        const int64_t* ids = (const int64_t*)ray_data(by_expr);
        for (int64_t i = 0; i < ray_len(by_expr); i++) {
            ray_t* c = ray_table_get_col(tbl, ids[i]);
            if (!c || c->type == RAY_BOOL) return true;
        }
        return false;
    }
    return true;   /* computed / unresolved key shapes: stay conservative */
}

/* True when `tbl` carries PARTED or MAPCOMMON columns (a parted store).  The
 * parted GROUP dispatch (exec_group_parted) treats a positive group_limit as
 * "stop after group_limit partitions", which assumes every partition yields a
 * group — an EMPTY partition breaks that and under-fills the answer.  Rather
 * than push a hint that path would mis-apply, grouped takes over parted inputs
 * keep the post-execution take. */
static bool table_is_parted(ray_t* tbl) {
    if (!tbl || RAY_IS_ERR(tbl) || tbl->type != RAY_TABLE) return true;
    int64_t nc = ray_table_ncols(tbl);
    for (int64_t c = 0; c < nc; c++) {
        ray_t* col = ray_table_get_col_idx(tbl, c);
        if (col && (RAY_IS_PARTED(col->type) || col->type == RAY_MAPCOMMON))
            return true;
    }
    return false;
}

/* Apply sort (asc/desc) and take clauses to a materialized result table.
 * Used by eval-level paths that bypass the DAG (e.g., LIST/STR group keys).
 * Builds a temporary DAG for sorting (supports per-column direction flags)
 * and applies take via ray_head/ray_tail or ray_take_fn.
 *
 * Top-K fast path: when there is exactly one sort key (a single column
 * name), an atom take with K << nrows, and the result is a flat table
 * with no LIST columns, dispatch to ray_topk_table — bounded-heap
 * selection in O(n log K) instead of full sort + gather. */
/* Decoded take: value the CALLER already evaluated (grouped DAG path: it
 * evaluates take: once to decide the HEAD pushdown).  Passing the DECODED form
 * rather than the ray_t keeps it a stack value with no ownership to leak on the
 * caller's error paths, and re-evaluating a side-effecting or expensive take:
 * expression a second time is avoided either way. */
typedef enum { TAKE_PRE_NONE = 0, TAKE_PRE_ATOM, TAKE_PRE_RANGE } take_pre_kind_t;
typedef struct {
    take_pre_kind_t kind;
    int64_t a, b;   /* ATOM: a = count.  RANGE: [a start, b amount]. */
} take_pre_t;

/* `take_pre` (may be NULL / TAKE_PRE_NONE) — see take_pre_t. */
static ray_t* apply_sort_take(ray_t* result, ray_t** dict_elems, int64_t dict_n,
                              int64_t asc_id, int64_t desc_id, int64_t take_id,
                              const take_pre_t* take_pre) {
    if (!result || RAY_IS_ERR(result)) return result;

    /* Check for sort/take clauses */
    bool has_sort = false;
    ray_t* take_val_expr = NULL;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == asc_id || kid == desc_id) has_sort = true;
        if (kid == take_id) take_val_expr = dict_elems[i + 1];
    }
    if (!has_sort && !take_val_expr) return result;

    if (!has_sort && take_val_expr) {
        /* Re-materialize the caller's already-evaluated value instead of
         * running take_val_expr again; the rest of this branch is unchanged
         * and owns tv exactly as before. */
        ray_t* tv;
        if (take_pre && take_pre->kind == TAKE_PRE_ATOM) {
            tv = ray_i64(take_pre->a);
        } else if (take_pre && take_pre->kind == TAKE_PRE_RANGE) {
            tv = ray_vec_new(RAY_I64, 2);
            if (tv && !RAY_IS_ERR(tv)) {
                ((int64_t*)ray_data(tv))[0] = take_pre->a;
                ((int64_t*)ray_data(tv))[1] = take_pre->b;
                tv->len = 2;
            }
        } else {
            tv = ray_eval(take_val_expr);
        }
        if (!tv || RAY_IS_ERR(tv)) {
            ray_release(result);
            return tv ? tv : ray_error("domain", "select: failed to evaluate `take:`");
        }
        if (ray_is_atom(tv) && (tv->type == -RAY_I64 || tv->type == -RAY_I32)) {
            int64_t atom_n = (tv->type == -RAY_I64) ? tv->i64 : tv->i32;
            ray_release(tv);
            /* INT64_MIN is the i64 null sentinel (0N); negating it below is
             * signed-overflow UB.  Reject it up front, matching ray_take_fn. */
            if (atom_n == INT64_MIN) {
                ray_release(result);
                return ray_error("type", "select: take: count is null or out of range");
            }

            int64_t nrows = (result->type == RAY_TABLE)
                          ? ray_table_nrows(result)
                          : (ray_is_vec(result) ? result->len : 0);
            int64_t start, amount;
            if (atom_n >= 0) {
                start  = 0;
                amount = atom_n < nrows ? atom_n : nrows;
            } else {
                int64_t want = -atom_n;
                amount = want < nrows ? want : nrows;
                start  = nrows - amount;
            }

            ray_t* rng = ray_vec_new(RAY_I64, 2);
            if (!rng || RAY_IS_ERR(rng)) {
                ray_release(result);
                return rng ? rng : ray_error("oom", NULL);
            }
            ((int64_t*)ray_data(rng))[0] = start;
            ((int64_t*)ray_data(rng))[1] = amount;
            rng->len = 2;
            ray_t* sliced = ray_take_fn(result, rng);
            ray_release(result);
            /* No explicit GC here — every top-level statement (run_piped
             * / repl) finishes with a ray_heap_gc() that catches the
             * freed intermediates anyway.  The inner call was double-
             * counting on repeated query execution. */
            ray_release(rng);
            return sliced;
        }
        if (ray_is_vec(tv) && (tv->type == RAY_I64 || tv->type == RAY_I32) && tv->len == 2) {
            ray_t* sliced = ray_take_fn(result, tv);
            ray_release(result);
            ray_release(tv);
            return sliced;
        }
        int8_t tv_t = tv->type;            /* capture BEFORE free */
        ray_release(tv);
        ray_release(result);
        return ray_error("domain", "select: `take:` must be an integer atom or a 2-element integer range, got %s", ray_type_name(tv_t));
    }

    /* ---- Top-K fast path detection ----
     * Conditions:
     *  - Exactly ONE asc:/desc: clause naming a SINGLE scalar column.
     *  - take is an atom in [1, K_MAX], where K_MAX is well under nrows.
     *  - result has no LIST columns (the topk gather handles LIST too,
     *    but skip to keep the surface area small until we have LIST
     *    coverage shapes).  Most large grouped scans are LIST-free.
     *
     * Anything else falls through to the full-sort DAG path below. */
    if (has_sort && take_val_expr && result->type == RAY_TABLE) {
        /* Collect ALL sort keys (across asc:/desc: clauses) into a flat
         * (sym, dir) list.  Single-key takes the radix-encoded fast
         * path; multi-key takes the comparator-based bounded heap. */
        enum { TOPK_MAX_KEYS = 16 };
        int64_t key_syms[TOPK_MAX_KEYS];
        uint8_t key_descs[TOPK_MAX_KEYS];
        uint8_t n_keys = 0;
        int     bad_clause = 0;
        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            uint8_t is_desc = 0;
            if (kid == asc_id) is_desc = 0;
            else if (kid == desc_id) is_desc = 1;
            else continue;
            ray_t* val = dict_elems[i + 1];
            if (!val) { bad_clause = 1; break; }
            if (val->type == -RAY_SYM) {
                if (n_keys >= TOPK_MAX_KEYS) { bad_clause = 1; break; }
                key_syms[n_keys] = val->i64;
                key_descs[n_keys] = is_desc;
                n_keys++;
            } else if (ray_is_vec(val) && val->type == RAY_SYM) {
                for (int64_t c = 0; c < val->len; c++) {
                    if (n_keys >= TOPK_MAX_KEYS) { bad_clause = 1; break; }
                    /* cell-data → runtime name id (column lookup below) */
                    key_syms[n_keys] = sym_cell_runtime_id(val, c);
                    key_descs[n_keys] = is_desc;
                    n_keys++;
                }
                if (bad_clause) break;
            } else {
                /* Computed sort key (expression) — full DAG path handles it. */
                bad_clause = 1;
                break;
            }
        }
        if (!bad_clause && n_keys > 0) {
            /* Probe the take expression — only atom-K with K > 0 qualifies. */
            ray_t* tv = ray_eval(take_val_expr);
            if (tv && !RAY_IS_ERR(tv) && ray_is_atom(tv) &&
                (tv->type == -RAY_I64 || tv->type == -RAY_I32)) {
                int64_t k = (tv->type == -RAY_I64) ? tv->i64 : tv->i32;
                ray_release(tv);
                int64_t nrows = ray_table_nrows(result);
                /* Bound K and the over-cardinality ratio: only useful
                 * when K is well under nrows.  Leave the take=full /
                 * negative-take cases to the existing path. */
                if (k > 0 && k < nrows && k <= FPK_MAX_K) {
                    /* Reject LIST columns — full path handles those. */
                    int has_list = 0;
                    int64_t ncols = ray_table_ncols(result);
                    for (int64_t c = 0; c < ncols; c++) {
                        ray_t* col = ray_table_get_col_idx(result, c);
                        if (col && col->type == RAY_LIST) { has_list = 1; break; }
                    }
                    if (!has_list) {
                        ray_t* topk = NULL;
                        if (n_keys == 1) {
                            ray_t* sort_col = ray_table_get_col(result, key_syms[0]);
                            if (sort_col) {
                                topk = ray_topk_table(result, sort_col,
                                    key_descs[0],
                                    sort_nulls_first(key_descs[0]), k);
                            }
                        } else {
                            ray_t* key_cols[TOPK_MAX_KEYS];
                            uint8_t nfs[TOPK_MAX_KEYS];
                            int ok = 1;
                            for (uint32_t i = 0; i < n_keys; i++) {
                                key_cols[i] = ray_table_get_col(result, key_syms[i]);
                                nfs[i] = sort_nulls_first(key_descs[i]);
                                if (!key_cols[i]) { ok = 0; break; }
                            }
                            if (ok) {
                                topk = ray_topk_table_multi(result, key_cols,
                                    key_descs, nfs, n_keys, k);
                            }
                        }
                        if (topk && !RAY_IS_ERR(topk)) {
                            ray_release(result);
                            /* No explicit GC — the top-level statement
                             * runner's ray_heap_gc() reclaims the freed
                             * intermediates one call later. */
                            return topk;
                        }
                        if (topk && RAY_IS_ERR(topk)) ray_release(topk);
                        /* topk == NULL: unsupported config, fall through. */
                    }
                }
            } else if (tv) {
                ray_release(tv);
            }
        }
    }

    /* Build temporary DAG on the materialized result */
    ray_graph_t* g = ray_graph_new(result);
    if (!g) return result;
    ray_op_t* root = ray_const_table(g, result);

    /* Sort */
    if (has_sort) {
        /* Exact-size scratch carve: bound = total names across the dict's
         * asc:/desc: entries (a -RAY_SYM atom counts 1, a RAY_SYM vector
         * counts its length).  The former fixed [16] arrays silently
         * capped multi-key sorts past the 16th key. */
        int64_t n_sort_max = 0;
        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            if (kid != asc_id && kid != desc_id) continue;
            ray_t* val = dict_elems[i + 1];
            if (val->type == -RAY_SYM) n_sort_max += 1;
            else if (ray_is_vec(val) && val->type == RAY_SYM) n_sort_max += ray_len(val);
        }
        if (n_sort_max < 1) n_sort_max = 1;
        ray_t* sortk_hdr = NULL;
        ray_op_t** sort_keys = (ray_op_t**)scratch_alloc(&sortk_hdr,
                (size_t)n_sort_max * (sizeof(ray_op_t*) + 1));
        if (!sort_keys) { ray_graph_free(g); ray_release(result); return ray_error("oom", NULL); }
        uint8_t* sort_descs = (uint8_t*)(sort_keys + n_sort_max);
        int64_t n_sort = 0;
        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            uint8_t is_desc = 0;
            if (kid == asc_id) is_desc = 0;
            else if (kid == desc_id) is_desc = 1;
            else continue;
            ray_t* val = dict_elems[i + 1];
            if (val->type == -RAY_SYM) {
                ray_t* s = ray_sym_str(val->i64);
                sort_keys[n_sort] = ray_scan(g, ray_str_ptr(s));
                sort_descs[n_sort] = is_desc;
                n_sort++;
            } else if (ray_is_vec(val) && val->type == RAY_SYM) {
                for (int64_t c = 0; c < val->len; c++) {
                    /* cell-data: resolve through the vec's domain */
                    ray_t* s = ray_sym_vec_cell(val, c);
                    sort_keys[n_sort] = ray_scan(g, ray_str_ptr(s));
                    sort_descs[n_sort] = is_desc;
                    n_sort++;
                }
            }
        }
        if (n_sort > 0)
            root = ray_sort_op(g, root, sort_keys, sort_descs, NULL, n_sort);
        scratch_free(sortk_hdr);
    }

    /* Take: avoid the DAG ray_head/ray_tail op — it can't handle
     * tables with LIST columns (from non-agg scatter).  Use
     * ray_take_fn, but convert the atom form into a `[start amount]`
     * range so we get CLAMP semantics (group-by take),
     * not the wrap/pad behavior of atom-n take on a short table. */
    ray_t* take_range   = NULL;    /* [start amount] literal form */
    int    take_is_atom = 0;
    int64_t atom_n      = 0;
    if (take_val_expr) {
        ray_t* tv = ray_eval(take_val_expr);
        if (!tv || RAY_IS_ERR(tv)) {
            ray_graph_free(g); ray_release(result);
            return tv ? tv : ray_error("domain", "select: failed to evaluate `take:`");
        }
        if (ray_is_atom(tv) && (tv->type == -RAY_I64 || tv->type == -RAY_I32)) {
            atom_n = (tv->type == -RAY_I64) ? tv->i64 : tv->i32;
            take_is_atom = 1;
            ray_release(tv);
        } else if (ray_is_vec(tv) && (tv->type == RAY_I64 || tv->type == RAY_I32) && tv->len == 2) {
            take_range = tv;
        } else {
            int8_t tv_t = tv->type;            /* capture BEFORE free */
            ray_release(tv); ray_graph_free(g); ray_release(result);
            return ray_error("domain", "select: `take:` must be an integer atom or a 2-element integer range, got %s", ray_type_name(tv_t));
        }
    }

    root = ray_optimize(g, root);
    ray_t* sorted = ray_execute(g, root);
    ray_graph_free(g);
    ray_release(result);

    if (take_is_atom && sorted && !RAY_IS_ERR(sorted)) {
        /* Build [start, amount] so ray_take_fn uses its range
         * branch, which clamps to the available length. */
        int64_t nrows = (sorted->type == RAY_TABLE)
                      ? ray_table_nrows(sorted)
                      : (ray_is_vec(sorted) ? sorted->len : 0);
        /* INT64_MIN (0N) negated below is signed-overflow UB — reject it. */
        if (atom_n == INT64_MIN) {
            ray_release(sorted);
            return ray_error("type", "select: take: count is null or out of range");
        }
        int64_t start, amount;
        if (atom_n >= 0) {
            start  = 0;
            amount = atom_n < nrows ? atom_n : nrows;
        } else {
            int64_t want = -atom_n;
            amount = want < nrows ? want : nrows;
            start  = nrows - amount;
        }
        ray_t* rng = ray_vec_new(RAY_I64, 2);
        if (!rng || RAY_IS_ERR(rng)) {
            ray_release(sorted);
            return rng ? rng : ray_error("oom", NULL);
        }
        ((int64_t*)ray_data(rng))[0] = start;
        ((int64_t*)ray_data(rng))[1] = amount;
        rng->len = 2;
        ray_t* sliced = ray_take_fn(sorted, rng);
        ray_release(sorted);
        ray_release(rng);
        return sliced;
    }
    if (take_range && sorted && !RAY_IS_ERR(sorted)) {
        ray_t* sliced = ray_take_fn(sorted, take_range);
        ray_release(sorted);
        ray_release(take_range);
        return sliced;
    }
    if (take_range) ray_release(take_range);
    return sorted;
}

static bool unsorted_positive_take_limit(ray_t** dict_elems, int64_t dict_n,
                                         int64_t asc_id, int64_t desc_id,
                                         int64_t take_id, int64_t nrows,
                                         int64_t* out_nrows) {
    bool has_sort = false;
    ray_t* take_val_expr = NULL;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == asc_id || kid == desc_id) has_sort = true;
        if (kid == take_id) take_val_expr = dict_elems[i + 1];
    }
    if (has_sort || !take_val_expr) return false;

    ray_t* tv = ray_eval(take_val_expr);
    if (!tv || RAY_IS_ERR(tv)) {
        if (tv && !RAY_IS_ERR(tv)) ray_release(tv);
        return false;
    }

    bool ok = false;
    int64_t limit = nrows;
    if (ray_is_atom(tv) && (tv->type == -RAY_I64 || tv->type == -RAY_I32)) {
        int64_t k = (tv->type == -RAY_I64) ? tv->i64 : tv->i32;
        if (k >= 0) {
            limit = k < nrows ? k : nrows;
            ok = true;
        }
    }
    ray_release(tv);
    if (ok && out_nrows) *out_nrows = limit;
    return ok;
}

/* --------------------------------------------------------------------------
 * Compile-time local env helpers for lambda / let inlining.
 *
 * compile_expr_dag hangs a small stack of {formal_sym_id → node_id}
 * bindings on the graph.  When the recursive walker encounters a
 * name reference, it checks the env first; if the name is bound,
 * return &g->nodes[node_id] — otherwise fall through to ray_scan.
 *
 * Store IDs, not pointers: g->nodes is a dynamically-resized array,
 * and any realloc between push and lookup would dangle stored
 * pointers.  IDs are stable across reallocs; we re-resolve
 * &g->nodes[id] on every lookup.
 *
 * Shadowing is automatic: nested lambda / let pushes appear later in
 * the stack, and cexpr_env_lookup walks top-down so the innermost
 * binding wins.  Pops are counted — never partial rewinds.
 *
 * No retain/release: op nodes live in g->nodes and are freed
 * uniformly by ray_graph_free.
 * -------------------------------------------------------------------------- */
static ray_op_t* cexpr_env_lookup(ray_graph_t* g, int64_t sym) {
    for (int i = g->cexpr_env_top - 1; i >= 0; i--)
        if (g->cexpr_env[i].sym == sym)
            return &g->nodes[g->cexpr_env[i].node_id];
    return NULL;
}

static bool cexpr_env_push(ray_graph_t* g, int64_t sym, ray_op_t* node) {
    if (g->cexpr_env_top >= 32) return false;
    g->cexpr_env[g->cexpr_env_top].sym     = sym;
    g->cexpr_env[g->cexpr_env_top].node_id = node->id;
    g->cexpr_env_top++;
    return true;
}

static void cexpr_env_pop(ray_graph_t* g, int n) {
    g->cexpr_env_top -= n;
    if (g->cexpr_env_top < 0) g->cexpr_env_top = 0;  /* defensive */
}

/* The projections of the select being compiled that precede the one
 * being compiled now (g->sel_alias_*, published by the projection loop).
 * Latest binding wins, so an alias that shadows an earlier alias — or a
 * source column — is the one a later projection sees. */
static ray_op_t* sel_alias_lookup(ray_graph_t* g, int64_t sym) {
    for (int i = g->sel_alias_n - 1; i >= 0; i--)
        if (g->sel_alias_syms[i] == sym)
            return &g->nodes[g->sel_alias_ids[i]];
    return NULL;
}

/* The op a sort key binds to.  When the sort runs over a projection
 * (`root` is its SELECT), the key is the projected column that scans the
 * source column — an output, or the hidden key appended for it — so the
 * sort reads it by POSITION and no name lookup over the projected table
 * (scans named by source, expressions `_e<c>`) can pick another column.
 * Otherwise a fresh scan resolved by name, as before. */
static ray_op_t* select_sort_key_op(ray_graph_t* g, ray_op_t* root, int64_t sym, const char* name) {
    if (root && root->opcode == OP_SELECT) {
        ray_op_ext_t* se = find_ext(g, root->id);
        if (se && se->base.opcode == OP_SELECT)
            for (uint32_t c = 0; c < se->sort.n_cols; c++) {
                ray_op_t* col = &g->nodes[se->sort.columns[c]];
                if (col->opcode != OP_SCAN) continue;
                ray_op_ext_t* ce = find_ext(g, col->id);
                if (ce && ce->base.opcode == OP_SCAN && ce->sym == sym) return col;
            }
    }
    return ray_scan(g, name);
}

/* Index of the first projected column that is a bare scan of source
 * column `sym`, or -1.  Used to bind a sort key to the projected column
 * that already carries it. */
static int64_t select_scan_output(ray_graph_t* g, ray_op_t** col_ops, int64_t nc, int64_t sym) {
    for (int64_t c = 0; c < nc; c++) {
        if (!col_ops[c] || col_ops[c]->opcode != OP_SCAN) continue;
        ray_op_ext_t* ce = find_ext(g, col_ops[c]->id);
        if (ce && ce->base.opcode == OP_SCAN && ce->sym == sym) return c;
    }
    return -1;
}

/* Takes the error compile_expr_dag left on the graph (see ops.h), or
 * NULL.  Callers that report a compile failure use it so the message
 * names the actual problem when there is one. */
static ray_t* graph_take_compile_err(ray_graph_t* g) {
    ray_t* e = g->compile_err;
    g->compile_err = NULL;
    return e;
}

static int const_str_expr_len(ray_t* expr, size_t* out_len) {
    if (!expr || !out_len) return 0;
    if (expr->type == -RAY_STR && !(expr->attrs & ATTR_QUOTED)) {
        *out_len += ray_str_len(expr);
        return 1;
    }
    if (expr->type != RAY_LIST || ray_len(expr) < 2) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (!elems[0] || elems[0]->type != -RAY_SYM) return 0;
    ray_t* head = ray_sym_str(elems[0]->i64);
    if (!head || ray_str_len(head) != 6
        || memcmp(ray_str_ptr(head), "concat", 6) != 0)
        return 0;
    for (int64_t i = 1; i < ray_len(expr); i++)
        if (!const_str_expr_len(elems[i], out_len)) return 0;
    return 1;
}

static void const_str_expr_copy(ray_t* expr, char* dst, size_t* off) {
    if (expr->type == -RAY_STR) {
        size_t len = ray_str_len(expr);
        memcpy(dst + *off, ray_str_ptr(expr), len);
        *off += len;
        return;
    }
    ray_t** elems = (ray_t**)ray_data(expr);
    for (int64_t i = 1; i < ray_len(expr); i++)
        const_str_expr_copy(elems[i], dst, off);
}

static ray_op_t* compile_const_str_expr(ray_graph_t* g, ray_t* expr) {
    size_t len = 0;
    if (!const_str_expr_len(expr, &len)) return NULL;
    if (len == 0) return ray_const_str(g, "", 0);
    char stack_buf[256];
    char* buf = stack_buf;
    ray_t* heap_buf = NULL;
    if (len > sizeof(stack_buf)) {
        heap_buf = ray_alloc(len);
        if (!heap_buf) return NULL;
        buf = (char*)ray_data(heap_buf);
    }
    size_t off = 0;
    const_str_expr_copy(expr, buf, &off);
    ray_op_t* out = ray_const_str(g, buf, len);
    if (heap_buf) ray_release(heap_buf);
    return out;
}

/* Re-resolve a ray_op_t* by its stable node ID.  Use this whenever
 * a pointer to an op node has been held across another DAG-building
 * call (which may grow g->nodes via graph_alloc_node and invalidate
 * all previously-returned pointers).  The ID is stable; only the
 * backing address may change. */

/* Compile a Rayfall AST expression into a DAG node */
ray_op_t* compile_expr_dag(ray_graph_t* g, ray_t* expr) {
    if (!expr) return NULL;

    /* Atom literal → const node.  Handle non-null scalar literals
     * via the dedicated ctors that carry just the raw value; typed
     * null atoms (e.g. `0Nl`, `0Nf`) must go through ray_const_atom
     * so the null flag in atom->aux rides along — otherwise
     * downstream comparisons lose the null-ness and fall back to
     * sentinel-value equality. */
    if (expr->type == -RAY_I64 && !RAY_ATOM_IS_NULL(expr))
        return ray_const_i64(g, expr->i64);
    if (expr->type == -RAY_F64 && !RAY_ATOM_IS_NULL(expr))
        return ray_const_f64(g, expr->f64);
    if (expr->type == -RAY_BOOL && !RAY_ATOM_IS_NULL(expr))
        return ray_const_bool(g, expr->b8);
    if (expr->type == -RAY_STR && !RAY_ATOM_IS_NULL(expr)) {
        const char *ptr = ray_str_ptr(expr);
        size_t len = ray_str_len(expr);
        return ray_const_str(g, ptr, len);
    }

    /* Name reference → local env first, then column scan, then
     * global env (for set-bound constants).  Local env holds lambda
     * / let bindings and takes precedence so formals shadow columns
     * naturally.  Global env is a last resort — it catches cases
     * like `(set threshold 50)` used inside a lambda body. */
    if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
        ray_op_t* bound = cexpr_env_lookup(g, expr->i64);
        if (bound) return bound;
        ray_op_t* alias = sel_alias_lookup(g, expr->i64);
        if (alias) return alias;
        ray_t* local = ray_env_get_lexical_local(expr->i64);
        if (local) {
            if (ray_is_atom(local)) return ray_const_atom(g, local);
            if (ray_is_vec(local))  return ray_const_vec(g, local);
            return NULL;
        }
        ray_t* s = ray_sym_str(expr->i64);
        if (!s) return NULL;

        /* Dotted name — desugar at compile time by walking the
         * segments: emit a scan for the head column, then for each
         * subsequent segment look up the name's registered DAG-level
         * emitter and chain it.  `col.ss` → scan(col) → extract(SS),
         * `col.date` → scan(col) → date_trunc(DAY), etc.  Segment
         * resolution uses the same name table as the runtime
         * `(ss col)` form, so adding a new accessor means registering
         * one unary builtin (temporal or otherwise) — no bespoke sym
         * → field map here.  Anything we can't lower returns NULL
         * (compile error), avoiding the old crash path where unknown
         * dotted names became scans of non-existent columns. */
        if (ray_sym_is_dotted(expr->i64)) {
            const int64_t* segs;
            int nsegs = ray_sym_segs(expr->i64, &segs);
            if (nsegs < 2) return NULL;
            if (!g->table || g->table->type != RAY_TABLE) return NULL;
            if (!ray_table_get_col(g->table, segs[0])) return NULL;
            ray_t* head_name = ray_sym_str(segs[0]);
            if (!head_name) return NULL;
            ray_op_t* op = ray_scan(g, ray_str_ptr(head_name));
            if (!op) return NULL;
            for (int i = 1; i < nsegs; i++) {
                int field = ray_temporal_field_from_sym(segs[i]);
                if (field >= 0) {
                    op = ray_extract(g, op, field);
                    if (!op) return NULL;
                    continue;
                }
                int trunc_kind = ray_temporal_trunc_from_sym(segs[i]);
                if (trunc_kind >= 0) {
                    op = ray_date_trunc(g, op, trunc_kind);
                    if (!op) return NULL;
                    continue;
                }
                return NULL;
            }
            return op;
        }

        /* Column names on the bound table shadow global env —
         * matches eval-level name-resolution order. */
        if (g->table && g->table->type == RAY_TABLE &&
            ray_table_get_col(g->table, expr->i64))
            return ray_scan(g, ray_str_ptr(s));
        /* Global env: atom literals / typed vectors compile as
         * const nodes.  Lambdas only make sense as call heads
         * and are handled in the list branch below. */
        ray_t* gv = ray_env_get(expr->i64);
        if (gv) {
            if (ray_is_atom(gv)) return ray_const_atom(g, gv);
            if (ray_is_vec(gv))  return ray_const_vec(g, gv);
        }
        /* Unknown name — retain an explicit invalid-scan marker.  Optimizer
         * shortcuts may make the scan dead (count/max/projection paths), but
         * execution must still reject the original column reference. */
        ray_op_t* missing = ray_scan(g, ray_str_ptr(s));
        /* `_dist` is exempt, but only where it can legitimately appear: a
         * rerank produces it during execution, so it is absent from the
         * operand table at compile time.  Exempting it unconditionally let
         * every symptom of an unknown column back in for that one name —
         * a projection under `_dist` shifted the labels left, and an
         * aggregate over it answered its neutral element — in queries with
         * no nearest: clause at all. */
        int64_t dist_sym = ray_sym_find("_dist", 5);
        bool exempt = false;
        if (missing && expr->i64 == dist_sym) {
            for (uint32_t i = 0; i < g->node_count && !exempt; i++)
                if (g->nodes[i].opcode == OP_ANN_RERANK ||
                    g->nodes[i].opcode == OP_KNN_RERANK)
                    exempt = true;
        }
        if (missing && !exempt) {
            missing->flags |= OP_FLAG_INVALID_SCAN;
            ray_op_ext_t* ext = find_ext(g, missing->id);
            if (ext) ext->base.flags |= OP_FLAG_INVALID_SCAN;
        }
        return missing;
    }

    /* Symbol literal (ATTR_QUOTED set; name refs handled above).  Inside a
     * query, a literal symbol that names a from-table COLUMN resolves to that
     * column (B3 Part 2): we consult ONLY g->table's column set — never the
     * local/global env — so a literal never captures a lambda/let local and
     * the rule fires only when a query table is bound.  A literal naming no
     * column stays a const atom node. */
    if (expr->type == -RAY_SYM) {
        /* A literal naming an EARLIER PROJECTION of the same select resolves
         * to it, exactly as the bare name does — and ahead of a source
         * column of the same name, as the bare name does.  The alias store
         * holds projections only, never lambda formals or let bindings, so
         * a literal still never captures those. */
        ray_op_t* alias = sel_alias_lookup(g, expr->i64);
        if (alias) return alias;
        if (g->table && g->table->type == RAY_TABLE &&
            ray_table_get_col(g->table, expr->i64)) {
            ray_t* s = ray_sym_str(expr->i64);
            if (s) return ray_scan(g, ray_str_ptr(s));
        }
        return ray_const_atom(g, expr);
    }

    /* Other atom literal types → const atom node.  Also falls
     * through to here for typed null I64/F64/BOOL/STR atoms
     * (which the fast-path branches above rejected via
     * RAY_ATOM_IS_NULL). */
    if (ray_is_atom(expr) && !(expr->attrs & ATTR_QUOTED))
        return ray_const_atom(g, expr);

    /* Typed-vector literal (e.g. [1 2 3], [AAPL MSFT], ["a" "b"]) →
     * const vector node.  ray_const_vec already stores any ray_t*
     * vec in ext->literal, and the OP_CONST executor returns it
     * directly — so this unlocks every typed literal vector as a
     * DAG operand (crucial for OP_IN set operands). */
    if (ray_is_vec(expr) && !(expr->attrs & ATTR_QUOTED))
        return ray_const_vec(g, expr);

    /* List → function call: (fn arg1 arg2 ...) */
    if (expr->type == RAY_LIST) {
        int64_t n = ray_len(expr);
        if (n == 0) return NULL;
        ray_t** elems = (ray_t**)ray_data(expr);
        ray_t* head = elems[0];

        /* Lambda invocation: `((fn [formals] body) a1 a2 …)`.
         * β-reduce at the DAG-node level — compile each actual
         * arg into its own op (in the current env), push the
         * {formal_i → actual_op_i} frame, recurse into the body
         * (which reads the env via cexpr_env_lookup when it hits
         * a name reference), then pop.  Sub-expression sharing is
         * automatic: multiple uses of a formal all resolve to the
         * single compiled actual op. */
        if (head->type == RAY_LIST) {
            int64_t hn = ray_len(head);
            if (hn != 3) return NULL;
            ray_t** hel = (ray_t**)ray_data(head);
            if (hel[0]->type != -RAY_SYM) return NULL;
            ray_t* hname_str = ray_sym_str(hel[0]->i64);
            if (!hname_str || ray_str_len(hname_str) != 2 ||
                memcmp(ray_str_ptr(hname_str), "fn", 2) != 0) return NULL;

            ray_t* formals = hel[1];
            ray_t* body    = hel[2];
            if (!ray_is_vec(formals) || formals->type != RAY_SYM) return NULL;
            int64_t nf = formals->len;
            if (n - 1 != nf) return NULL;              /* arity mismatch */
            if (nf > 16) return NULL;                  /* too many formals */
            if (g->cexpr_env_top + (int)nf > 32) return NULL; /* env overflow */

            /* Compile actuals in the CURRENT env, before pushing.
             * Snapshot IDs, not pointers — g->nodes can realloc
             * between successive compile_expr_dag calls so any
             * raw ray_op_t* saved from an earlier iteration may
             * dangle by the time we push it. */
            uint32_t actual_ids[16];
            for (int64_t i = 0; i < nf; i++) {
                ray_op_t* a = compile_expr_dag(g, elems[i + 1]);
                if (!a) return NULL;
                actual_ids[i] = a->id;
            }
            int64_t* fids = (int64_t*)ray_data(formals);
            int pushed = 0;
            for (int64_t i = 0; i < nf; i++) {
                if (g->cexpr_env_top >= 32) {
                    cexpr_env_pop(g, pushed);
                    return NULL;
                }
                g->cexpr_env[g->cexpr_env_top].sym     = fids[i];
                g->cexpr_env[g->cexpr_env_top].node_id = actual_ids[i];
                g->cexpr_env_top++;
                pushed++;
            }
            ray_op_t* result = compile_expr_dag(g, body);
            cexpr_env_pop(g, pushed);
            return result;
        }

        /* Named-lambda call: `(f a1 a2 …)` where `f` is globally
         * bound to a RAY_LAMBDA with a single-expression body.
         * Inline exactly like the literal `((fn …) …)` case.
         * Shadowing order matches the value-position name-ref
         * branch: local cexpr_env > table columns > globals.  A
         * column named `f` isn't callable, but we still must honor
         * shadowing so the exec-time error is consistent. */
        if (head->type == -RAY_SYM && !(head->attrs & ATTR_QUOTED) &&
            cexpr_env_lookup(g, head->i64) == NULL &&
            !(g->table && g->table->type == RAY_TABLE &&
              ray_table_get_col(g->table, head->i64))) {
            ray_t* gv = ray_env_get(head->i64);
            if (gv && gv->type == RAY_LAMBDA) {
                ray_t* formals  = LAMBDA_PARAMS(gv);
                ray_t* body_lst = LAMBDA_BODY(gv);
                if (formals && body_lst && body_lst->type == RAY_LIST &&
                    ray_len(body_lst) == 1 &&
                    ray_is_vec(formals) && formals->type == RAY_SYM) {
                    int64_t nf = formals->len;
                    if (n - 1 == nf && nf <= 16 &&
                        g->cexpr_env_top + (int)nf <= 32) {
                        ray_t* body = ((ray_t**)ray_data(body_lst))[0];
                        uint32_t actual_ids[16];
                        for (int64_t i = 0; i < nf; i++) {
                            ray_op_t* a = compile_expr_dag(g, elems[i + 1]);
                            if (!a) return NULL;
                            actual_ids[i] = a->id;
                        }
                        int64_t* fids = (int64_t*)ray_data(formals);
                        int pushed = 0;
                        for (int64_t i = 0; i < nf; i++) {
                            g->cexpr_env[g->cexpr_env_top].sym     = fids[i];
                            g->cexpr_env[g->cexpr_env_top].node_id = actual_ids[i];
                            g->cexpr_env_top++;
                            pushed++;
                        }
                        /* The body was written outside this select: a free
                         * name in it is a global (or a column), never an
                         * output alias of the select that happens to call
                         * the lambda.  Hide the alias store while the body
                         * compiles; the actuals above were compiled in the
                         * projection's own scope and keep seeing aliases. */
                        int saved_aliases = g->sel_alias_n;
                        g->sel_alias_n = 0;
                        ray_op_t* result = compile_expr_dag(g, body);
                        g->sel_alias_n = saved_aliases;
                        cexpr_env_pop(g, pushed);
                        return result;
                    }
                }
            }
        }

        /* Head must be a name referencing a builtin */
        if (head->type != -RAY_SYM) return NULL;
        int64_t fn_sym = head->i64;

        /* Check for xbar */
        ray_t* fn_name_str = ray_sym_str(fn_sym);
        const char* fname = fn_name_str ? ray_str_ptr(fn_name_str) : NULL;
        size_t fname_len = fn_name_str ? ray_str_len(fn_name_str) : 0;

        /* (quote X) — the explicit quote special form.  The reader lowers the
         * `'X` shorthand to an ATTR_QUOTED atom (folded by the literal branches
         * above), but the call form `(quote X)` reaches here as a 2-element
         * list and previously fell through to the unknown-builtin path
         * (→ NULL → "WHERE predicate not supported by DAG compiler"), so an
         * inline `(quote x)` worked in a projection (eval fallback) but errored
         * in a WHERE predicate.  Fold it to the literal datum X.
         *
         * Deliberately NOT applying the in-query column-shadow rule here: by
         * design an inline `(quote col)` call node STAYS the literal symbol,
         * whereas the tick atom `'col` resolves to the column (the AST-shape
         * boundary pinned by test/rfl/query/literal_col_ref.rfl — Part 2
         * unwraps a literal -RAY_SYM atom, not a (quote …) call node).  So a
         * quoted symbol always folds to a const atom; vector / atom data folds
         * to a const node; non-foldable quoted data (e.g. a quoted list)
         * returns NULL. */
        if (fname_len == 5 && memcmp(fname, "quote", 5) == 0) {
            if (n != 2) return NULL;
            ray_t* q = elems[1];
            if (!q) return NULL;
            if (q->type == -RAY_SYM) return ray_const_atom(g, q);
            if (ray_is_vec(q))       return ray_const_vec(g, q);
            if (ray_is_atom(q))      return ray_const_atom(g, q);
            return NULL;
        }

        if (fname_len == 4 && memcmp(fname, "xbar", 4) == 0) {
            if (n != 3) return NULL;
            ray_op_t* col = compile_expr_dag(g, elems[1]);
            if (!col) return NULL;
            uint32_t col_id = col->id;
            ray_op_t* bucket = compile_expr_dag(g, elems[2]);
            if (!bucket) return NULL;
            uint32_t bucket_id = bucket->id;
            col = &g->nodes[col_id];
            /* xbar(x, b) = x - (x % b)  (stays in integer domain).
             *
             * A temporal column keeps its type (ray_xbar_fn returns the
             * column's type, #669): `promote` folds TIMESTAMP into I64 and
             * DATE / TIME into I32, so the bucketed result is cast back to
             * the column's type — same width, so the cast is a retag (or a
             * narrowing from an I64 bucket for DATE / TIME).  The eval path
             * rejects a float bucket on a temporal column; declining here
             * sends the projection to it for the same error.  A TIME bucket
             * on a TIMESTAMP column is in milliseconds (ray_xbar_fn scales
             * it to nanoseconds). */
            int8_t col_t = col->out_type;
            if (RAY_IS_PARTED(col_t)) col_t = (int8_t)RAY_PARTED_BASETYPE(col_t);
            int8_t temporal = dag_type_is_temporal(col_t) ? col_t : 0;
            if (temporal) {
                int8_t bt = bucket->out_type;
                if (RAY_IS_PARTED(bt)) bt = (int8_t)RAY_PARTED_BASETYPE(bt);
                if (bt == RAY_F64 || bt == RAY_F32) return NULL;
                if (temporal == RAY_TIMESTAMP && bt == RAY_TIME) {
                    ray_op_t* scale = ray_const_i64(g, 1000000LL);
                    if (!scale) return NULL;
                    bucket = &g->nodes[bucket_id];
                    bucket = ray_mul(g, bucket, scale);
                    if (!bucket) return NULL;
                    bucket_id = bucket->id;
                    col = &g->nodes[col_id];
                }
            }
            ray_op_t* m = ray_mod(g, col, bucket);
            if (!m) return NULL;
            col = &g->nodes[col_id];
            ray_op_t* r = ray_sub(g, col, m);
            if (!r || !temporal) return r;
            return ray_cast(g, r, temporal);
        }

        /* (if cond then else) — 4 elements (fn + 3 args).  Compiles
         * to OP_IF which is supported by the element-wise fusion
         * pipeline. */
        if (fname_len == 2 && memcmp(fname, "if", 2) == 0) {
            if (n != 4) return NULL;
            ray_op_t* c = compile_expr_dag(g, elems[1]);
            if (!c) return NULL;
            uint32_t c_id = c->id;
            g->if_arm_depth++;
            ray_op_t* t = compile_expr_dag(g, elems[2]);
            uint32_t t_id = t ? t->id : 0;
            ray_op_t* e = t ? compile_expr_dag(g, elems[3]) : NULL;
            g->if_arm_depth--;
            if (!t || !e) return NULL;
            c = &g->nodes[c_id];
            t = &g->nodes[t_id];
            return ray_if(g, c, t, e);
        }

        /* (substr str start len) — 4 elements. */
        if (fname_len == 6 && memcmp(fname, "substr", 6) == 0) {
            if (n != 4) return NULL;
            ray_op_t* str = compile_expr_dag(g, elems[1]);
            if (!str) return NULL;
            uint32_t str_id = str->id;
            ray_op_t* start = compile_expr_dag(g, elems[2]);
            if (!start) return NULL;
            uint32_t start_id = start->id;
            ray_op_t* ln = compile_expr_dag(g, elems[3]);
            if (!ln) return NULL;
            str = &g->nodes[str_id];
            start = &g->nodes[start_id];
            return ray_substr(g, str, start, ln);
        }

        /* (replace str from to) — 4 elements. */
        if (fname_len == 7 && memcmp(fname, "replace", 7) == 0) {
            if (n != 4) return NULL;
            ray_op_t* str = compile_expr_dag(g, elems[1]);
            if (!str) return NULL;
            uint32_t str_id = str->id;
            ray_op_t* from = compile_expr_dag(g, elems[2]);
            if (!from) return NULL;
            uint32_t from_id = from->id;
            ray_op_t* to = compile_expr_dag(g, elems[3]);
            if (!to) return NULL;
            str = &g->nodes[str_id];
            from = &g->nodes[from_id];
            return ray_replace(g, str, from, to);
        }

        /* (concat a b ...) — variadic string concat. */
        if (fname_len == 6 && memcmp(fname, "concat", 6) == 0) {
            ray_op_t* folded = compile_const_str_expr(g, expr);
            if (folded) return folded;
            if (n < 2 || n - 1 > 16) return NULL;
            uint32_t arg_ids[16];
            for (int64_t i = 1; i < n; i++) {
                ray_op_t* a = compile_expr_dag(g, elems[i]);
                if (!a) return NULL;
                arg_ids[i - 1] = a->id;
            }
            ray_op_t* args[16];
            for (int64_t i = 0; i < n - 1; i++)
                args[i] = &g->nodes[arg_ids[i]];
            return ray_concat(g, args, (int)(n - 1));
        }

        /* (as 'TYPE col) — cast.  The type is a sym literal like 'I64 / 'F64. */
        if (fname_len == 2 && memcmp(fname, "as", 2) == 0) {
            if (n != 3) return NULL;
            ray_t* type_expr = elems[1];
            if (type_expr->type != -RAY_SYM) return NULL;
            int8_t tgt = -1;
            ray_t* ts = ray_sym_str(type_expr->i64);
            if (ts) {
                const char* tn = ray_str_ptr(ts);
                size_t tl = ray_str_len(ts);
                if (tl == 3 && memcmp(tn, "I64", 3) == 0)       tgt = RAY_I64;
                else if (tl == 3 && memcmp(tn, "F64", 3) == 0)  tgt = RAY_F64;
                else if (tl == 3 && memcmp(tn, "I32", 3) == 0)  tgt = RAY_I32;
                else if (tl == 3 && memcmp(tn, "I16", 3) == 0)  tgt = RAY_I16;
                else if (tl == 3 && memcmp(tn, "F32", 3) == 0)  tgt = RAY_F32;
                else if (tl == 2 && memcmp(tn, "U8", 2) == 0)   tgt = RAY_U8;
                else if (tl == 4 && memcmp(tn, "BOOL", 4) == 0) tgt = RAY_BOOL;
            }
            if (tgt < 0) return NULL;
            ray_op_t* col = compile_expr_dag(g, elems[2]);
            if (!col) return NULL;
            return ray_cast(g, col, tgt);
        }

        /* (within col range) — inclusive range membership.  Lowers to
         * `(and (>= col lo) (<= col hi))`, reusing the comparison
         * executors so a `within` WHERE predicate inherits range-index
         * rowsel, partition pruning, null handling and type promotion
         * for free.  Two range forms are accepted:
         *   1. a 2-element constructor — `(list lo hi)` / `(enlist lo hi)`
         *      — whose element EXPRESSIONS are compiled individually, so
         *      variable / runtime bounds work (e.g. `(list lo hi)` with
         *      `lo`,`hi` bound by `set`);
         *   2. a constant 2-element vector literal — e.g. `[2 4]`.
         * Anything else returns NULL so the select falls back to the
         * eval-level `within` builtin. */
        if (fname_len == 6 && memcmp(fname, "within", 6) == 0) {
            if (n != 3) return NULL;
            ray_op_t* col = compile_expr_dag(g, elems[1]);
            if (!col) return NULL;
            uint32_t col_id = col->id;

            uint32_t lo_id = 0, hi_id = 0;
            ray_t* range_ast = elems[2];
            int handled = 0;

            /* Form 1: 2-element (list …) / (enlist …) constructor. */
            if (range_ast->type == RAY_LIST && ray_len(range_ast) == 3) {
                ray_t** re = (ray_t**)ray_data(range_ast);
                if (re[0]->type == -RAY_SYM) {
                    ray_t* hs = ray_sym_str(re[0]->i64);
                    size_t hl = hs ? ray_str_len(hs) : 0;
                    const char* hp = hs ? ray_str_ptr(hs) : NULL;
                    if ((hl == 4 && memcmp(hp, "list", 4) == 0) ||
                        (hl == 6 && memcmp(hp, "enlist", 6) == 0)) {
                        ray_op_t* lo = compile_expr_dag(g, re[1]);
                        if (!lo) return NULL;
                        lo_id = lo->id;
                        ray_op_t* hi = compile_expr_dag(g, re[2]);
                        if (!hi) return NULL;
                        hi_id = hi->id;
                        handled = 1;
                    }
                }
            }

            /* Form 2: constant 2-element vector literal.  Extract the two
             * bounds as typed atoms via ray_at_fn so a DATE/TIME/I32 range
             * keeps its element type for the comparison (ray_const_atom
             * retains, so release ours). */
            if (!handled) {
                ray_op_t* rng = compile_expr_dag(g, range_ast);
                if (!rng || rng->opcode != OP_CONST) return NULL;
                ray_op_ext_t* rext = find_ext(g, rng->id);
                if (!rext || !rext->literal) return NULL;
                ray_t* rv = rext->literal;
                if (!ray_is_vec(rv) || rv->len != 2) return NULL;
                ray_t* lo_idx = ray_i64(0);
                ray_t* hi_idx = ray_i64(1);
                ray_t* lo_atom = ray_at_fn(rv, lo_idx);
                ray_t* hi_atom = ray_at_fn(rv, hi_idx);
                ray_release(lo_idx);
                ray_release(hi_idx);
                if (!lo_atom || RAY_IS_ERR(lo_atom) ||
                    !hi_atom || RAY_IS_ERR(hi_atom)) {
                    if (lo_atom) ray_release(lo_atom);
                    if (hi_atom) ray_release(hi_atom);
                    return NULL;
                }
                ray_op_t* lo = ray_const_atom(g, lo_atom);
                ray_op_t* hi = ray_const_atom(g, hi_atom);
                ray_release(lo_atom);
                ray_release(hi_atom);
                if (!lo || !hi) return NULL;
                lo_id = lo->id;
                hi_id = hi->id;
            }

            /* Build (and (>= col lo) (<= col hi)).  Snapshot IDs and
             * re-resolve after each graph alloc — the node array may
             * realloc between constructor calls. */
            ray_op_t* ge = ray_ge(g, &g->nodes[col_id], &g->nodes[lo_id]);
            if (!ge) return NULL;
            uint32_t ge_id = ge->id;
            ray_op_t* le = ray_le(g, &g->nodes[col_id], &g->nodes[hi_id]);
            if (!le) return NULL;
            return ray_and(g, &g->nodes[ge_id], le);
        }

        /* Temporal extract: (year col), (month col), (day col), ... */
        if (n == 2) {
            int64_t field = -1;
            if (fname_len == 4 && memcmp(fname, "year",  4) == 0) field = RAY_EXTRACT_YEAR;
            else if (fname_len == 5 && memcmp(fname, "month", 5) == 0) field = RAY_EXTRACT_MONTH;
            else if (fname_len == 3 && memcmp(fname, "day",   3) == 0) field = RAY_EXTRACT_DAY;
            else if (fname_len == 4 && memcmp(fname, "hour",  4) == 0) field = RAY_EXTRACT_HOUR;
            else if (fname_len == 6 && memcmp(fname, "minute",6) == 0) field = RAY_EXTRACT_MINUTE;
            else if (fname_len == 6 && memcmp(fname, "second",6) == 0) field = RAY_EXTRACT_SECOND;
            else if (fname_len == 9 && memcmp(fname, "dayofweek",9) == 0) field = RAY_EXTRACT_DOW;
            else if (fname_len == 9 && memcmp(fname, "dayofyear",9) == 0) field = RAY_EXTRACT_DOY;
            if (field >= 0) {
                ray_op_t* col = compile_expr_dag(g, elems[1]);
                if (!col) return NULL;
                return ray_extract(g, col, field);
            }
        }

        /* (do e1 e2 ... en) → compile only the last expression.
         * Earlier expressions can't have side-effects in DAG context;
         * if they do, they'll be silently dropped.  Use eval-level
         * for side-effectful scripts. */
        if (fname_len == 2 && memcmp(fname, "do", 2) == 0) {
            if (n < 2) return NULL;
            return compile_expr_dag(g, elems[n - 1]);
        }

        /* (let var val body) — compile `val` in the current env,
         * bind var → val_op by ID (pointer-safe across reallocs),
         * compile `body`, pop.  Same β-reduction mechanism as
         * lambda inlining, just with a single binding. */
        if (fname_len == 3 && memcmp(fname, "let", 3) == 0) {
            if (n != 4) return NULL;
            ray_t* var_expr = elems[1];
            if (var_expr->type != -RAY_SYM) return NULL;
            int64_t var_sym = var_expr->i64;
            ray_op_t* val_op = compile_expr_dag(g, elems[2]);
            if (!val_op) return NULL;
            /* cexpr_env_push already snapshots node->id, which is
             * stable across subsequent graph reallocations. */
            if (!cexpr_env_push(g, var_sym, val_op)) return NULL;
            ray_op_t* body_op = compile_expr_dag(g, elems[3]);
            cexpr_env_pop(g, 1);
            return body_op;
        }

        /* (cond (p1 e1) (p2 e2) ... (else en)) → nested OP_IF. */
        if (fname_len == 4 && memcmp(fname, "cond", 4) == 0) {
            if (n < 2) return NULL;
            /* Walk right-to-left, building an OP_IF chain.  The last
             * clause must be an `else` form. */
            uint32_t chain_id = UINT32_MAX;
            for (int64_t i = n - 1; i >= 1; i--) {
                ray_t* clause = elems[i];
                if (clause->type != RAY_LIST || ray_len(clause) != 2) return NULL;
                ray_t** cpair = (ray_t**)ray_data(clause);
                int is_else = 0;
                if (cpair[0]->type == -RAY_SYM) {
                    ray_t* ns = ray_sym_str(cpair[0]->i64);
                    if (ns && ray_str_len(ns) == 4 &&
                        memcmp(ray_str_ptr(ns), "else", 4) == 0)
                        is_else = 1;
                }
                if (is_else) {
                    if (i != n - 1) return NULL;
                    g->if_arm_depth++;
                    ray_op_t* c = compile_expr_dag(g, cpair[1]);
                    g->if_arm_depth--;
                    if (!c) return NULL;
                    chain_id = c->id;
                } else {
                    if (chain_id == UINT32_MAX) return NULL;
                    ray_op_t* pred = compile_expr_dag(g, cpair[0]);
                    if (!pred) return NULL;
                    uint32_t pred_id = pred->id;
                    g->if_arm_depth++;
                    ray_op_t* body = compile_expr_dag(g, cpair[1]);
                    g->if_arm_depth--;
                    if (!body) return NULL;
                    pred = &g->nodes[pred_id];
                    ray_op_t* chain = &g->nodes[chain_id];
                    ray_op_t* r = ray_if(g, pred, body, chain);
                    if (!r) return NULL;
                    chain_id = r->id;
                }
            }
            if (chain_id == UINT32_MAX) return NULL;
            return &g->nodes[chain_id];
        }

        /* Variadic `and`/`or`: fold into a balanced binary tree.
         * `(and a b c d)` → `(and (and a b) (and c d))` — depth log2(N).
         * Without this, n>=4 falls through `compile_expr_dag` and the
         * caller (e.g. select WHERE) reports "WHERE predicate not
         * supported by DAG compiler".  The fused-expr executor evaluates
         * the resulting tree as a sequence of binary AND/OR instructions
         * sharing scratch registers — no extra column allocations vs
         * what hand-nested binary forms already do.
         *
         * Balanced tree (rather than left-fold) keeps the canonical
         * shape symmetric and minimises dependency-chain depth, which
         * future OoO / parallel-instruction executors can exploit. */
        /* (and X) / (or X) — single conjunct = identity.  Matches the
         * eval-level monoid identity rule in ray_and_vary_fn /
         * ray_or_vary_fn; without it, `where: (and X)` would fall
         * through to compile_expr_dag returning NULL → domain error. */
        if (n == 2) {
            bool is_and1 = (fname_len == 3 && memcmp(fname, "and", 3) == 0);
            bool is_or1  = (fname_len == 2 && memcmp(fname, "or",  2) == 0);
            if (is_and1 || is_or1) {
                return compile_expr_dag(g, elems[1]);
            }
        }
        if (n >= 4) {
            bool is_and = (fname_len == 3 && memcmp(fname, "and", 3) == 0);
            bool is_or  = (fname_len == 2 && memcmp(fname, "or",  2) == 0);
            if (is_and || is_or) {
                int64_t k = n - 1;
                if (k > 64) return NULL;          /* depth/space guard */
                uint32_t arg_ids[64];
                for (int64_t i = 0; i < k; i++) {
                    ray_op_t* a = compile_expr_dag(g, elems[i + 1]);
                    if (!a) return NULL;
                    if (!dag_bool_operand_ok(g, a, is_and)) return NULL;
                    arg_ids[i] = a->id;
                }
                dag_binary_ctor ctor = is_and ? ray_and : ray_or;
                /* Iterative pairwise reduction: at each round, fold
                 * adjacent pairs into a single node, halving the count.
                 * Equivalent to recursive bisect but avoids a helper. */
                int64_t cnt = k;
                while (cnt > 1) {
                    int64_t out = 0;
                    for (int64_t i = 0; i + 1 < cnt; i += 2) {
                        /* make_binary re-resolves both inputs via stored
                         * IDs after its own potential realloc, so the
                         * pointers we pass here are safe to use. */
                        ray_op_t* l = &g->nodes[arg_ids[i]];
                        ray_op_t* r = &g->nodes[arg_ids[i + 1]];
                        ray_op_t* combined = ctor(g, l, r);
                        if (!combined) return NULL;
                        arg_ids[out++] = combined->id;
                    }
                    if (cnt & 1)                    /* carry odd tail */
                        arg_ids[out++] = arg_ids[cnt - 1];
                    cnt = out;
                }
                return &g->nodes[arg_ids[0]];
            }
        }

        /* Binary op? */
        if (n == 3) {
            uint16_t mov_op = resolve_moving_opcode(fn_sym);
            if (mov_op) {
                ray_op_t* wop = compile_expr_dag(g, elems[1]);
                int64_t window = 0;
                if (!const_op_i64(g, wop, &window) || window < 1)
                    return NULL;
                ray_op_t* arg = compile_expr_dag(g, elems[2]);
                if (!arg) return NULL;
                switch (mov_op) {
                    case OP_MSUM:   return ray_msum_op(g, arg, window);
                    case OP_MAVG:   return ray_mavg_op(g, arg, window);
                    case OP_MMIN:   return ray_mmin_op(g, arg, window);
                    case OP_MMAX:   return ray_mmax_op(g, arg, window);
                    case OP_MCOUNT: return ray_mcount_op(g, arg, window);
                    case OP_MVAR:   return ray_mvar_op(g, arg, window);
                    case OP_MDEV:   return ray_mdev_op(g, arg, window);
                    default: return NULL;
                }
            }
            dag_binary_ctor ctor = resolve_binary_dag(fn_sym);
            if (ctor) {
                ray_op_t* left = compile_expr_dag(g, elems[1]);
                if (!left) return NULL;
                uint32_t left_id = left->id;
                ray_op_t* right = compile_expr_dag(g, elems[2]);
                if (!right) return NULL;
                left = &g->nodes[left_id];
                if ((ctor == ray_and || ctor == ray_or) &&
                    (!dag_bool_operand_ok(g, left, ctor == ray_and) ||
                     !dag_bool_operand_ok(g, right, ctor == ray_and)))
                    return NULL;
                if (dag_temporal_arith_needs_eval(fname, fname_len,
                                                  left->out_type,
                                                  right->out_type))
                    return NULL;
                if (g->if_arm_depth == 0 &&
                    dag_arith_rejects_sym(fname, fname_len,
                                          left->out_type, right->out_type)) {
                    if (!g->compile_err)
                        g->compile_err = ray_error("type", "cannot %s %s and %s",
                            dag_arith_verb(fname, fname_len),
                            ray_type_name((int8_t)-left->out_type),
                            ray_type_name((int8_t)-right->out_type));
                    return NULL;
                }
                if (g->if_arm_depth == 0 &&
                    dag_cmp_rejects_temporal_float(fname, fname_len,
                                                   left->out_type, right->out_type)) {
                    if (!g->compile_err)
                        g->compile_err = ray_error("type", "cannot compare %s and %s",
                            ray_type_name((int8_t)-left->out_type),
                            ray_type_name((int8_t)-right->out_type));
                    return NULL;
                }
                if (fname_len == 3 && memcmp(fname, "pow", 3) == 0 &&
                    (!dag_pow_type_admitted(left->out_type) ||
                     !dag_pow_type_admitted(right->out_type)))
                    return NULL;
                return ctor(g, left, right);
            }
        }

        /* Unary op or aggregation? */
        if (n == 2) {
            /* Check for unary DAG ops */
            dag_unary_ctor uctor = resolve_unary_dag(fn_sym);
            if (uctor) {
                ray_op_t* arg = compile_expr_dag(g, elems[1]);
                if (!arg) return NULL;
                if (dag_unary_numeric_name(fname, fname_len) &&
                    !dag_numeric_type_admitted(arg->out_type))
                    return NULL;
                return uctor(g, arg);
            }
            /* Aggregation functions return DAG agg nodes */
            uint16_t agg_op = resolve_agg_opcode(fn_sym);
            if (agg_op) {
                ray_op_t* arg = compile_expr_dag(g, elems[1]);
                if (!arg) return NULL;
                /* Canonical aggregand type-admission (same table as the scalar
                 * builtins): reject non-numeric (SYM/STR/GUID) and, for sum,
                 * absolute-temporal (DATE/TIMESTAMP) inputs at plan time so the
                 * DAG path never silently aggregates raw symbol ids / string
                 * bytes / date counts.  Returning NULL routes the select to the
                 * eval-level path, where the scalar builtin raises `type`. */
                if (arg->out_type > 0 && !agg_type_admitted(agg_op, arg->out_type))
                    return NULL;
                switch (agg_op) {
                    case OP_SUM:         return ray_sum(g, arg);
                    case OP_AVG:         return ray_avg(g, arg);
                    case OP_MIN:         return ray_min_op(g, arg);
                    case OP_MAX:         return ray_max_op(g, arg);
                    case OP_COUNT:       return ray_count(g, arg);
                    case OP_ALL:         return ray_all(g, arg);
                    case OP_ANY:         return ray_any(g, arg);
                    case OP_FIRST:       return ray_first(g, arg);
                    case OP_LAST:        return ray_last(g, arg);
                    case OP_PROD:        return ray_prod(g, arg);
                    case OP_STDDEV:      return ray_stddev(g, arg);
                    case OP_STDDEV_POP:  return ray_stddev_pop(g, arg);
                    case OP_VAR:         return ray_var(g, arg);
                    case OP_VAR_POP:     return ray_var_pop(g, arg);
                    case OP_MEDIAN:      return ray_median(g, arg);
                    default: return NULL;
                }
            }
        }
    }

    return NULL;
}

/* Mount every column of `tbl` into the current (already-pushed) local scope
 * as name -> vector, so query expressions resolve column references by
 * ordinary name lookup — including references produced at runtime that a
 * static AST walk could not see.  ray_env_set_local retains each column;
 * the matching ray_env_pop_scope releases them all at once.  Columns whose
 * names are reserved cannot occur for a real table column, so a rejected
 * bind is simply skipped. */
/* Active query from-table for tree-walk literal-column resolution (B3
 * Part 2).  bind_all_columns sets it (returning the previous value); each
 * caller restores that saved value via `g_active_query_table = _aqt;`
 * before every matching ray_env_pop_scope (including error returns).
 * Column-membership scoped and query-only by construction — see
 * ray_active_query_table. */
static _Thread_local ray_t* g_active_query_table = NULL;
/* The per-row evaluation in progress, if any: the row, the table it indexes
 * and the scope depth just above its own query frame.  A literal column
 * name stands for the row's cell only while the active table is that table
 * and no nested query has opened a scope since — a nested select, a
 * per-group evaluation or a where-mask over any table (the same one
 * included) binds whole columns and must see whole columns. */
static _Thread_local int64_t g_active_query_row       = -1;
static _Thread_local ray_t*  g_active_query_row_tbl   = NULL;
static _Thread_local int32_t g_active_query_row_depth = 0;

ray_t* ray_active_query_table(void) { return g_active_query_table; }

/* Mount columns AND publish `tbl` as the active query table; returns the
 * previously-active table so the caller can restore it after the matching
 * ray_env_pop_scope (sub-selects nest, so save/restore is required). */
static ray_t* bind_all_columns(ray_t* tbl) {
    ray_t* prev = g_active_query_table;
    g_active_query_table = tbl;
    int64_t ncols = ray_table_ncols(tbl);
    for (int64_t c = 0; c < ncols; c++) {
        int64_t cn = ray_table_col_name(tbl, c);
        ray_t*  cv = ray_table_get_col_idx(tbl, c);
        if (cv) ray_env_set_query_local(cn, cv);
    }
    return prev;
}

static ray_t* eval_where_mask(ray_t* where_expr, ray_t* tbl, const char* label) {
    if (ray_env_push_query_scope() != RAY_OK) return ray_error("oom", NULL);
    ray_t* _aqt = bind_all_columns(tbl);
    ray_t* mask = ray_eval(where_expr);
    if (mask && !RAY_IS_ERR(mask))
        mask = ray_lazy_materialize(mask);
    g_active_query_table = _aqt;
    ray_env_pop_scope();

    if (!mask)
        return ray_error("domain", "%s: WHERE predicate evaluation failed", label);
    if (RAY_IS_ERR(mask))
        return mask;

    /* the one where: rule — same as the compiled OP_FILTER path */
    return ray_where_mask_coerce(mask, ray_table_nrows(tbl), label);
}

static ray_op_t* compile_where_predicate(ray_graph_t* g, ray_t* tbl,
                                         ray_t* where_expr,
                                         const char* label,
                                         ray_t** out_err) {
    if (out_err) *out_err = NULL;
    ray_op_t* pred = compile_expr_dag(g, where_expr);
    if (pred) return pred;
    /* A predicate the compiler rejected outright (`(and x ...)` over an
     * I64 column) is an error, not a fallback: the interpreter's `and` is
     * a scalar truth test, which would have kept every row. */
    if (g->compile_err) {
        if (out_err) *out_err = graph_take_compile_err(g);
        else { ray_release(g->compile_err); g->compile_err = NULL; }
        return NULL;
    }

    ray_t* mask = eval_where_mask(where_expr, tbl, label);
    if (!mask || RAY_IS_ERR(mask)) {
        if (out_err) *out_err = mask ? mask :
            ray_error("domain", "%s: WHERE predicate evaluation failed", label);
        return NULL;
    }

    pred = ray_const_vec(g, mask);
    ray_release(mask);
    if (!pred && out_err) *out_err = ray_error("oom", NULL);
    return pred;
}

static int is_agg_expr(ray_t* expr);  /* defined below */

/* Return 1 if expr references a table column in a position where the
 * column is expected to flow through row-by-row (not reduced by an
 * enclosing aggregation).  Used to decide whether a non-agg expression
 * is expected to produce a row-aligned result — pure constants and
 * aggregation-reduced expressions (e.g. `(+ 1 (sum p))`) legitimately
 * produce scalars/short-length results that must be broadcast.
 *
 * The walker stops recursing when it hits an aggregation call: any
 * column refs inside get reduced to a scalar, so they don't drive the
 * row-alignment expectation.
 *
 * Lambda call forms `((fn ...) actuals)` are also treated as
 * "unknown shape" — even if the actuals reference columns, the
 * body may reduce them via an enclosed aggregation.  Returning 0
 * here means the scatter will rely purely on the runtime shape
 * check (row-aligned → gather, else broadcast) instead of
 * erroring.  This loses a bug-catching net for lambda calls whose
 * body IS row-preserving but returns a mismatched-length result,
 * but that's a niche case compared to the common "lambda wrapping
 * an agg" pattern users actually write. */
static int expr_refs_row_column(ray_t* expr, ray_t* tbl) {
    if (!expr) return 0;
    if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
        if (ray_env_has_lexical_local(expr->i64)) return 0;
        if (ray_table_get_col(tbl, expr->i64)) return 1;
        /* Dotted name whose head is a column is a row-aligned ref —
         * `Timestamp.ss` flows through row-by-row the same as plain
         * `Timestamp` would, so the scatter must treat it as one. */
        if (ray_sym_is_dotted(expr->i64)) {
            const int64_t* segs;
            int nsegs = ray_sym_segs(expr->i64, &segs);
            if (nsegs >= 1 && ray_table_get_col(tbl, segs[0])) return 1;
        }
        return 0;
    }
    if (expr->type == RAY_LIST) {
        /* If this call is itself an aggregation, its column refs
         * collapse to a scalar — don't recurse.  The whole subtree
         * is treated as a constant from the row-alignment POV. */
        if (is_agg_expr(expr)) return 0;
        ray_t** elems = (ray_t**)ray_data(expr);
        int64_t n = ray_len(expr);
        if (n == 0) return 0;
        /* Lambda call form: head is itself a LIST.  We can't tell
         * from the outside whether the body is row-preserving or
         * aggregating, so surrender row-alignment enforcement. */
        if (elems[0]->type == RAY_LIST) return 0;
        /* Skip elems[0] — it's the function name, not a column. */
        for (int64_t i = 1; i < n; i++)
            if (expr_refs_row_column(elems[i], tbl)) return 1;
    }
    return 0;
}

/* Check if an expression is an aggregation call (head is an agg function) */
static int is_agg_expr(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    if (expr->type == RAY_DICT) return 0;
    int64_t n = ray_len(expr);
    if (n < 2) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (elems[0]->type != -RAY_SYM) return 0;
    return resolve_agg_opcode(elems[0]->i64) != 0;
}

/* True iff the expression contains an aggregation call anywhere in
 * its subtree.  Used by the post-DAG scatter to detect non-agg
 * expressions whose subexpressions ARE aggregates (e.g.
 * `(- (max v1) (min v2))`) — those must be evaluated per-group
 * rather than broadcast from a single full-table eval, otherwise the
 * inner aggs collapse globally and every group gets the same value. */
static int expr_contains_agg(ray_t* expr) {
    if (!expr) return 0;
    if (expr->type != RAY_LIST) return 0;
    if (is_agg_expr(expr)) return 1;
    ray_t** elems = (ray_t**)ray_data(expr);
    int64_t n = ray_len(expr);
    for (int64_t i = 0; i < n; i++)
        if (expr_contains_agg(elems[i])) return 1;
    return 0;
}

static int is_group_dag_agg_expr(ray_t* expr);  /* defined below */
static bool simplify_agg_idiom(ray_t* val_expr, ray_t* tbl,
                               uint16_t* out_op, ray_t** out_arg);

/* ── Arith-of-aggs decomposition ──────────────────────────────────────────
 * A select-by output like (/ (sum a) (as 'F64 (sum b))) is not itself a
 * DAG agg, so it used to fall to the post-DAG scatter, which re-aggregates
 * every group's rows through the generic evaluator (per-group slices) —
 * ~10ms/1M rows where the fused group pass costs ~2ms.  Instead: replace
 * each UNARY DAG-aggregable subcall with a hidden slot symbol, compile the
 * hidden slots as ordinary DAG aggs (they ride the same single group
 * pass), and evaluate the rewritten wrapper once over the n_groups-row
 * result.  Eligibility: >=1 extractable agg; every table-column reference
 * sits inside one; agg arguments contain no nested aggs; connectives may
 * be arbitrary calls over literals/globals. */
/* Slice-group arming gate: keep the hint only for aggregate shapes the
 * direct slice path can consume.  Arming other shapes is legal, but then
 * exec_group must fold the slices back into a selection first. */
static int sg_agg_expr_ok(ray_t* expr) {
    if (!expr || !is_list(expr) || ray_len(expr) < 2) return 0;
    ray_t** e = (ray_t**)ray_data(expr);
    if (!e[0] || e[0]->type != -RAY_SYM) return 0;
    uint16_t op = resolve_agg_opcode(e[0]->i64);
    if (!op) return 0;
    if (op == OP_COUNT) return 1;          /* group size; arg unused */
    ray_t* arg = e[1];
    bool arg_bare = arg && arg->type == -RAY_SYM &&
                    !(arg->attrs & ATTR_QUOTED);
    if (agg_is_binary_agg(op)) {
        if (ray_len(expr) < 3) return 0;
        ray_t* arg2 = e[2];
        return arg_bare && arg2 && arg2->type == -RAY_SYM &&
               !(arg2->attrs & ATTR_QUOTED);
    }
    if (op == OP_STDDEV || op == OP_STDDEV_POP ||
        op == OP_VAR || op == OP_VAR_POP)
        return arg_bare;
    if (op != OP_SUM && op != OP_AVG) return 0;
    if (arg_bare) return 1;
    if (arg && is_list(arg) && ray_len(arg) == 3) {
        ray_t** m = (ray_t**)ray_data(arg);
        static int64_t s_mul = -1;
        if (s_mul < 0) s_mul = ray_sym_intern("*", 1);
        if (m[0] && m[0]->type == -RAY_SYM && m[0]->i64 == s_mul &&
            m[1] && m[1]->type == -RAY_SYM && !(m[1]->attrs & ATTR_QUOTED) &&
            m[2] && m[2]->type == -RAY_SYM && !(m[2]->attrs & ATTR_QUOTED))
            return 1;
    }
    return 0;
}

/* Aggregate shapes the hidden-slot compiler accepts: unary aggregates,
 * binary aggregates over two bare columns (pearson, covariance, wsum, ...),
 * and quantile with its literal probability.  List-valued top/bottom K and
 * anything wider keep the per-group scatter path, whose element-wise
 * semantics differ from a post-group column expression. */
static int hidden_agg_shape_ok(ray_t* expr) {
    int64_t n = ray_len(expr);
    if (n == 2) return 1;
    if (n != 3) return 0;
    ray_t** e = (ray_t**)ray_data(expr);
    uint16_t op = resolve_agg_opcode(e[0]->i64);
    if (agg_is_binary_agg(op))
        return e[1] && e[1]->type == -RAY_SYM && !(e[1]->attrs & ATTR_QUOTED) &&
               e[2] && e[2]->type == -RAY_SYM && !(e[2]->attrs & ATTR_QUOTED);
    return op == OP_QUANTILE;
}

static bool agg_arith_head_is_control(int64_t sym) {
    static const char* const forms[] = { "if", "while", "times",
                                         "do", "let", "set", "try" };
    ray_t* s = ray_sym_str(sym);
    if (!s) return false;
    const char* p = ray_str_ptr(s);
    size_t l = ray_str_len(s);
    for (size_t i = 0; i < sizeof forms / sizeof forms[0]; i++)
        if (l == strlen(forms[i]) && memcmp(p, forms[i], l) == 0) return true;
    return false;
}

static ray_t* agg_arith_rewrite(ray_t* expr, ray_t* tbl,
                                ray_t** hexprs, int64_t* hnames,
                                int* n_hidden, int cap, int* ok) {
    if (!*ok || !expr) { *ok = 0; return NULL; }
    if (expr->type == RAY_LIST && is_group_dag_agg_expr(expr) &&
        hidden_agg_shape_ok(expr)) {
        ray_t** el = (ray_t**)ray_data(expr);
        /* nested agg inside the agg argument: not a DAG shape — bail */
        if (expr_contains_agg(el[1])) { *ok = 0; return NULL; }
        if (ray_len(expr) > 2 && expr_contains_agg(el[2])) { *ok = 0; return NULL; }
        if (*n_hidden >= cap) { *ok = 0; return NULL; }
        char buf[24];
        int bn = snprintf(buf, sizeof buf, "__ha%d", *n_hidden);
        int64_t hn = ray_sym_intern(buf, (size_t)bn);
        hexprs[*n_hidden] = expr;      /* borrowed from the query dict */
        hnames[*n_hidden] = hn;
        (*n_hidden)++;
        return ray_sym(hn);            /* non-quoted → resolves via env */
    }
    if (expr->type != RAY_LIST) { *ok = 0; return NULL; }
    int64_t n = ray_len(expr);
    ray_t** el = (ray_t**)ray_data(expr);
    /* Lambdas capture their own parameter scope — an agg inside a lambda
     * body must not be extracted (its argument is a param, not a column).
     * Reject the whole output; it keeps the per-group scatter path. */
    if (n > 0 && el[0] && el[0]->type == -RAY_SYM &&
        el[0]->i64 == ray_sym_intern("fn", 2)) { *ok = 0; return NULL; }
    /* `if` (and the loops) take one condition, not a vector of them:
     * evaluated once over the n_groups-row result they would pick a
     * single branch for every group.  The scope-making forms (do, let,
     * set, try) evaluate their body outside the scope the hidden slots
     * are bound in.  Reject such outputs so they keep the per-group
     * evaluation, where the condition is a scalar and names are plain. */
    if (n > 0 && el[0] && el[0]->type == -RAY_SYM &&
        !(el[0]->attrs & ATTR_QUOTED) && agg_arith_head_is_control(el[0]->i64))
        { *ok = 0; return NULL; }
    ray_t* out = ray_list_new(0);
    if (!out || RAY_IS_ERR(out)) { *ok = 0; return out && RAY_IS_ERR(out) ? NULL : NULL; }
    for (int64_t i = 0; i < n && *ok; i++) {
        ray_t* child = el[i];
        ray_t* rep = NULL;
        int rep_owned = 0;
        if (child && child->type == RAY_LIST) {
            rep = agg_arith_rewrite(child, tbl, hexprs, hnames,
                                    n_hidden, cap, ok);
            rep_owned = 1;
        } else if (child && child->type == -RAY_SYM &&
                   !(child->attrs & ATTR_QUOTED) && i > 0) {
            /* bare symbol argument: a table column reference outside an
             * agg needs per-row semantics — ineligible.  Dotted names
             * (ts.yyyy) are column paths we cannot verify here — reject.
             * Globals (NS_HOUR etc.) resolve identically at post-eval. */
            if (ray_sym_is_dotted(child->i64) ||
                ray_table_get_col(tbl, child->i64)) { *ok = 0; }
            else rep = child;
        } else {
            rep = child;   /* call head / literal / quoted sym */
        }
        if (*ok && rep) {
            out = ray_list_append(out, rep);
            if (RAY_IS_ERR(out)) *ok = 0;
        }
        if (rep_owned && rep) ray_release(rep);
    }
    if (!*ok) { if (out && !RAY_IS_ERR(out)) ray_release(out); return NULL; }
    return out;
}

static ray_t* try_decompose_agg_arith(ray_t* val_expr, ray_t* tbl,
                                      ray_t** hexprs, int64_t* hnames,
                                      int* n_hidden, int cap) {
    if (!val_expr || val_expr->type != RAY_LIST) return NULL;
    if (is_group_dag_agg_expr(val_expr)) return NULL;  /* plain agg path */
    if (!expr_contains_agg(val_expr)) return NULL;
    int ok = 1;
    int saved = *n_hidden;
    ray_t* rw = agg_arith_rewrite(val_expr, tbl, hexprs, hnames,
                                  n_hidden, cap, &ok);
    if (!ok || !rw || *n_hidden == saved) {
        *n_hidden = saved;
        if (rw && !RAY_IS_ERR(rw)) ray_release(rw);
        return NULL;
    }
    return rw;
}

/* Dry run of the arith-of-aggs decomposition: true when `expr` is an
 * arithmetic combination of DAG-shaped aggregates that the hidden-slot path
 * evaluates after grouping.  Routing consults this before deciding that a
 * non-aggregate output needs the per-group scatter or eval-level grouping;
 * the real rewrite runs later in the classification pass. */
static int is_decomposable_agg_compound(ray_t* expr, ray_t* tbl) {
    enum { PROBE_CAP = 64 };
    ray_t* hexprs[PROBE_CAP];
    int64_t hnames[PROBE_CAP];
    int n_hidden = 0;
    ray_t* rw = try_decompose_agg_arith(expr, tbl, hexprs, hnames,
                                        &n_hidden, PROBE_CAP);
    if (!rw) return 0;
    ray_release(rw);
    return 1;
}

static int expr_contains_call_named(ray_t* expr, const char* name, size_t name_len) {
    if (!expr) return 0;
    if (expr->type != RAY_LIST) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    int64_t n = ray_len(expr);
    if (n <= 0) return 0;
    ray_t* head = elems[0];
    if (head && head->type == -RAY_SYM) {
        ray_t* s = ray_sym_str(head->i64);
        if (s && ray_str_len(s) == name_len &&
            memcmp(ray_str_ptr(s), name, name_len) == 0)
            return 1;
    }
    for (int64_t i = 0; i < n; i++)
        if (expr_contains_call_named(elems[i], name, name_len))
            return 1;
    return 0;
}

static ray_t* query_materialize_parted_col(ray_t* col);
static bool table_has_parted_columns(ray_t* tbl);
static ray_t* query_materialize_parted_table(ray_t* tbl);

/* A projection that is a bare, unquoted column name of `tbl` IS that column.
 * The eval fallback otherwise evaluates it per row and collects the cells;
 * before the collector collapsed SYM atoms, `name: name` beside a projection
 * the DAG declines (temporal arithmetic, say) came back as a LIST of boxed
 * sym atoms, and a join on it matched nothing (rayforce-py#24).  Taking the
 * column directly is what the DAG path does for the same expression, keeps
 * every column kind (a LIST column included) as it is, and skips the per-row
 * walk.  Owned ref, or NULL when the expression is not such a name or
 * the column is not a plain full-length vector (parted / mapcommon / slice
 * columns keep the per-row read, which handles them cell by cell). */
static ray_t* select_fallback_passthrough_col(ray_t* expr, ray_t* tbl, int64_t nrows) {
    if (!expr || expr->type != -RAY_SYM || (expr->attrs & ATTR_QUOTED)) return NULL;
    if (ray_env_has_lexical_local(expr->i64)) return NULL;   /* a formal shadows the column, as on the DAG */
    ray_t* col = ray_table_get_col(tbl, expr->i64);
    if (!col || col->type < 0) return NULL;
    if (RAY_IS_PARTED(col->type) || col->type == RAY_MAPCOMMON ||
        (col->attrs & RAY_ATTR_SLICE) || ray_len(col) != nrows)
        return NULL;
    ray_retain(col);
    return col;
}

/* True when a projection's TOP-LEVEL call is a "whole-column verb": a
 * length-changing / reordering builtin (distinct, asc, desc, reverse) that
 * consumes an entire column vector and returns a vector.  The DAG compiler
 * has no bucket for these — they are neither element-wise ops nor
 * scalar-reducing aggregations — so `compile_expr_dag` returns NULL and the
 * projection lands in the eval fallback.  There the default per-row scatter
 * feeds the verb one scalar cell at a time: `distinct` errors ("argument must
 * be a list") and `asc`/`desc`/`reverse` silently return the column untouched
 * (reordering a 1-element slice is a no-op).  Both are wrong; such a
 * projection must instead be evaluated once against the full column
 * (see eval_expr_whole_column).  Matched on the top-level head only —
 * a whole-column verb nested under an element-wise op has ambiguous length
 * semantics and is left to the per-row path. */
static int is_whole_column_projection(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    if (ray_len(expr) < 2) return 0;
    ray_t* head = ((ray_t**)ray_data(expr))[0];
    if (!head || head->type != -RAY_SYM) return 0;
    ray_t* s = ray_sym_str(head->i64);
    if (!s) return 0;
    size_t l = ray_str_len(s);
    const char* p = ray_str_ptr(s);
    return (l == 8 && memcmp(p, "distinct", 8) == 0) ||
           (l == 7 && memcmp(p, "reverse", 7) == 0) ||
           (l == 3 && memcmp(p, "asc", 3) == 0) ||
           (l == 4 && memcmp(p, "desc", 4) == 0);
}

/* True when a grouped aggregate expression can be lowered to OP_GROUP.
 * `(count (distinct col))` is semantically an aggregate, but `distinct`
 * is not a row-aligned DAG input inside GROUP.  Route it through the
 * per-group eval fallback so `distinct` sees each group's slice. */
static int is_group_dag_agg_expr(ray_t* expr) {
    if (!is_agg_expr(expr)) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    uint16_t op = resolve_agg_opcode(elems[0]->i64);
    if (op == OP_QUANTILE) {
        if (ray_len(expr) < 3) return 0;
        if (!quantile_literal_prob(elems[2],
                                   agg_name_is_percentile(elems[0]->i64),
                                   NULL))
            return 0;
    }
    return !expr_contains_call_named(elems[1], "distinct", 8);
}

static bool expr_literal_is_temporal(ray_t* expr) {
    if (!expr) return false;
    int8_t t = expr->type < 0 ? (int8_t)-expr->type : expr->type;
    return dag_type_is_temporal(t);
}

static bool expr_dag_result_is_temporal(ray_t* expr, ray_t* tbl) {
    if (expr_literal_is_temporal(expr)) return true;

    ray_graph_t* pg = ray_graph_new(tbl);
    if (!pg) return false;

    ray_op_t* op = compile_expr_dag(pg, expr);
    bool temporal = op && dag_type_is_temporal(op->out_type);
    ray_graph_free(pg);
    return temporal;
}

static bool expr_contains_temporal_arith(ray_t* expr, ray_t* tbl) {
    if (!expr || expr->type != RAY_LIST) return false;

    ray_t** elems = (ray_t**)ray_data(expr);
    int64_t n = ray_len(expr);
    if (n >= 3 && elems[0] && elems[0]->type == -RAY_SYM) {
        ray_t* name = ray_sym_str(elems[0]->i64);
        size_t len = name ? ray_str_len(name) : 0;
        const char* s = name ? ray_str_ptr(name) : NULL;
        if (len == 1 && (s[0] == '+' || s[0] == '-' || s[0] == '*')) {
            if (expr_dag_result_is_temporal(elems[1], tbl) ||
                expr_dag_result_is_temporal(elems[2], tbl))
                return true;
        }
    }

    for (int64_t i = 0; i < n; i++)
        if (expr_contains_temporal_arith(elems[i], tbl))
            return true;
    return false;
}

static int is_group_dag_agg_expr_dag_safe(ray_t* expr, ray_t* tbl) {
    if (!is_group_dag_agg_expr(expr)) return 0;

    ray_t** elems = (ray_t**)ray_data(expr);
    uint16_t op = resolve_agg_opcode(elems[0]->i64);
    ray_t* agg_arg = elems[1];
    {
        uint16_t new_op;
        ray_t* new_arg;
        if (simplify_agg_idiom(expr, tbl, &new_op, &new_arg)) {
            op = new_op;
            agg_arg = new_arg;
        }
    }

    if (expr_contains_temporal_arith(agg_arg, tbl))
        return 0;
    if (agg_is_binary_agg(op)) {
        if (ray_len(expr) < 3) return 1;
        if (expr_contains_temporal_arith(elems[2], tbl))
            return 0;
    }
    return 1;
}

static int is_single_group_key_projection(ray_t* by_expr, ray_t* val_expr) {
    int64_t key_id = -1;
    if (by_expr && by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED)) {
        key_id = by_expr->i64;
    } else if (by_expr && by_expr->type == RAY_SYM && ray_len(by_expr) == 1) {
        key_id = ((int64_t*)ray_data(by_expr))[0];
    }

    return key_id >= 0 &&
           val_expr &&
           val_expr->type == -RAY_SYM &&
           !(val_expr->attrs & ATTR_QUOTED) &&
           val_expr->i64 == key_id;
}

static int atom_i64_const(ray_t* v, int64_t* out) {
    if (!v || !ray_is_atom(v) ||
        (v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) ||
        RAY_ATOM_IS_NULL(v))
        return 0;
    switch (v->type) {
    case -RAY_BOOL:
    case -RAY_U8: *out = v->u8; return 1;
    case -RAY_I16: *out = v->i16; return 1;
    case -RAY_I32:
    case -RAY_DATE:
    case -RAY_TIME: *out = v->i32; return 1;
    case -RAY_I64:
    case -RAY_TIMESTAMP: *out = v->i64; return 1;
    default: return 0;
    }
}

static int expr_affine_of_sym(ray_t* expr, int64_t sym, int64_t* bias) {
    if (!expr) return 0;
    if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED) &&
        expr->i64 == sym) {
        *bias = 0;
        return 1;
    }
    if (expr->type != RAY_LIST || ray_len(expr) != 3) return 0;
    ray_t** e = (ray_t**)ray_data(expr);
    if (!e[0] || e[0]->type != -RAY_SYM) return 0;
    ray_t* op = ray_sym_str(e[0]->i64);
    if (!op || ray_str_len(op) != 1) return 0;
    char opc = ray_str_ptr(op)[0];
    int lhs_sym = e[1] && e[1]->type == -RAY_SYM &&
                  !(e[1]->attrs & ATTR_QUOTED) && e[1]->i64 == sym;
    int rhs_sym = e[2] && e[2]->type == -RAY_SYM &&
                  !(e[2]->attrs & ATTR_QUOTED) && e[2]->i64 == sym;
    int64_t c = 0;
    if (opc == '+') {
        if (lhs_sym && atom_i64_const(e[2], &c)) {
            *bias = c;
            return 1;
        }
        if (rhs_sym && atom_i64_const(e[1], &c)) {
            *bias = c;
            return 1;
        }
    } else if (opc == '-') {
        if (lhs_sym && atom_i64_const(e[2], &c)) {
            *bias = -c;
            return 1;
        }
    }
    return 0;
}

static int key_type_i64_projectable(int8_t t) {
    return t == RAY_BOOL || t == RAY_U8 || t == RAY_I16 ||
           t == RAY_I32 || t == RAY_I64 || t == RAY_DATE ||
           t == RAY_TIME || t == RAY_TIMESTAMP;
}

static int64_t key_col_read_i64(ray_t* col, int64_t row) {
    const void* d = ray_data(col);
    switch (col->type) {
    case RAY_BOOL:
    case RAY_U8: return ((const uint8_t*)d)[row];
    case RAY_I16: return ((const int16_t*)d)[row];
    case RAY_I32:
    case RAY_DATE:
    case RAY_TIME: return ((const int32_t*)d)[row];
    case RAY_I64:
    case RAY_TIMESTAMP: return ((const int64_t*)d)[row];
    default: return 0;
    }
}

/* Effective element type of a group-key column, seeing through a PARTED
 * segment-list and a MAPCOMMON broadcast column to the underlying scalar
 * type.  Used to decide whether a parted by-dict rename key can stream. */
static int8_t query_col_base_type(ray_t* col) {
    if (!col) return 0;
    if (RAY_IS_PARTED(col->type)) return (int8_t)RAY_PARTED_BASETYPE(col->type);
    if (col->type == RAY_MAPCOMMON) {
        ray_t** p = (ray_t**)ray_data(col);
        return (p && p[0]) ? p[0]->type : 0;
    }
    return col->type;
}

/* A scalar key type the streaming per-partition group kernel packs into an
 * 8-byte slot.  STR/LIST/GUID keys route to the eval-level toy grouper (which
 * gathers materialized key columns by name) and MUST NOT be deferred over a
 * parted source — the deferred alias column never exists there, so that path
 * would silently drop the key. */
static int query_key_type_streamable(int8_t t) {
    return key_type_i64_projectable(t) || t == RAY_SYM ||
           t == RAY_F64 || t == RAY_F32;
}

static bool parse_gt_name_i64(ray_t* expr, int64_t* out_name, int64_t* out_threshold) {
    if (!expr || expr->type != RAY_LIST || ray_len(expr) != 3)
        return false;
    ray_t** e = (ray_t**)ray_data(expr);
    if (!e[0] || e[0]->type != -RAY_SYM)
        return false;
    ray_t* op = ray_sym_str(e[0]->i64);
    if (!op || ray_str_len(op) != 1 || ray_str_ptr(op)[0] != '>')
        return false;
    if (!e[1] || e[1]->type != -RAY_SYM || (e[1]->attrs & ATTR_QUOTED))
        return false;
    if (!e[2] || !ray_is_atom(e[2]) ||
        (e[2]->type == -RAY_SYM && !(e[2]->attrs & ATTR_QUOTED)))
        return false;
    int64_t threshold;
    switch (e[2]->type) {
        case -RAY_I64: threshold = e[2]->i64; break;
        case -RAY_I32:
        case -RAY_DATE:
        case -RAY_TIME: threshold = e[2]->i32; break;
        case -RAY_I16: threshold = e[2]->i16; break;
        case -RAY_U8:
        case -RAY_BOOL: threshold = e[2]->u8; break;
        default: return false;
    }
    *out_name = e[1]->i64;
    *out_threshold = threshold;
    return true;
}

static bool can_defer_single_key_where(ray_t* by_expr, ray_t* where_expr,
                                       ray_t* tbl) {
    if (!by_expr || !where_expr || !tbl ||
        by_expr->type != -RAY_SYM || (by_expr->attrs & ATTR_QUOTED) ||
        where_expr->type != RAY_LIST || ray_len(where_expr) != 3)
        return false;

    ray_t** e = (ray_t**)ray_data(where_expr);
    if (!e[0] || e[0]->type != -RAY_SYM) return false;
    ray_t* op = ray_sym_str(e[0]->i64);
    if (!op) return false;
    size_t op_len = ray_str_len(op);
    const char* op_s = ray_str_ptr(op);
    bool cmp = (op_len == 1 && (op_s[0] == '<' || op_s[0] == '>')) ||
               (op_len == 2 &&
                ((op_s[0] == '=' && op_s[1] == '=') ||
                 (op_s[0] == '!' && op_s[1] == '=') ||
                 (op_s[0] == '<' && op_s[1] == '=') ||
                 (op_s[0] == '>' && op_s[1] == '=')));
    if (!cmp) return false;

    ray_t* lhs = e[1];
    ray_t* rhs = e[2];
    bool lhs_key = lhs && lhs->type == -RAY_SYM &&
                   !(lhs->attrs & ATTR_QUOTED) &&
                   lhs->i64 == by_expr->i64;
    bool rhs_key = rhs && rhs->type == -RAY_SYM &&
                   !(rhs->attrs & ATTR_QUOTED) &&
                   rhs->i64 == by_expr->i64;
    if (lhs_key == rhs_key) return false;

    ray_t* atom = lhs_key ? rhs : lhs;
    if (!atom || !ray_is_atom(atom) ||
        (atom->type == -RAY_SYM && !(atom->attrs & ATTR_QUOTED)) ||
        RAY_ATOM_IS_NULL(atom))
        return false;

    ray_t* key_col = ray_table_get_col(tbl, by_expr->i64);
    if (!key_col) return false;
    int8_t kt = key_col->type;
    if (!RAY_IS_PARTED(kt)) return false;
    if (RAY_IS_PARTED(kt)) kt = (int8_t)RAY_PARTED_BASETYPE(kt);
    return kt == RAY_SYM || kt == RAY_BOOL || kt == RAY_U8 ||
           kt == RAY_I16 || kt == RAY_I32 || kt == RAY_I64 ||
           kt == RAY_DATE || kt == RAY_TIME || kt == RAY_TIMESTAMP ||
           kt == RAY_F32 || kt == RAY_F64;
}

static ray_t* filter_group_result(ray_t* result, ray_t* where_expr) {
    if (!result || RAY_IS_ERR(result) || !where_expr) return result;
    if (result->type != RAY_TABLE) return result;
    if (ray_is_lazy(result)) {
        result = ray_lazy_materialize(result);
        if (!result || RAY_IS_ERR(result)) return result;
    }

    ray_graph_t* fg = ray_graph_new(result);
    if (!fg) {
        ray_release(result);
        return ray_error("oom", NULL);
    }
    ray_op_t* root = ray_const_table(fg, result);
    ray_t* pred_err = NULL;
    ray_op_t* pred = compile_where_predicate(fg, result, where_expr,
                                             "select having", &pred_err);
    if (!pred) {
        ray_graph_free(fg);
        ray_release(result);
        return pred_err ? pred_err :
            ray_error("domain", "select having: failed to evaluate group filter predicate");
    }
    root = ray_filter(fg, root, pred);
    root = ray_optimize(fg, root);
    ray_t* filtered = ray_execute(fg, root);
    if (filtered && !RAY_IS_ERR(filtered) && ray_is_lazy(filtered))
        filtered = ray_lazy_materialize(filtered);
    ray_graph_free(fg);
    ray_release(result);
    return filtered ? filtered : ray_error("domain", "select having: group filter produced no result");
}

static bool match_group_count_emit_filter(ray_t* from_expr, ray_t* where_expr,
                                          ray_group_emit_filter_t* out) {
    int64_t filter_name, threshold;
    if (!parse_gt_name_i64(where_expr, &filter_name, &threshold))
        return false;
    if (!from_expr || from_expr->type != RAY_LIST || ray_len(from_expr) != 2)
        return false;
    ray_t** fe = (ray_t**)ray_data(from_expr);
    if (!fe[0] || fe[0]->type != -RAY_SYM)
        return false;
    ray_t* fname = ray_sym_str(fe[0]->i64);
    if (!fname || ray_str_len(fname) != 6 ||
        memcmp(ray_str_ptr(fname), "select", 6) != 0)
        return false;
    ray_t* inner = fe[1];
    if (!inner || inner->type != RAY_DICT)
        return false;
    ray_t* by = dict_get(inner, "by");
    if (!by) return false;

    DICT_VIEW_DECL(iv);
    DICT_VIEW_OPEN(inner, iv);
    if (DICT_VIEW_OVERFLOW(iv)) { DICT_VIEW_CLOSE(iv); return false; }
    int64_t from_id  = dict_key_id(inner, "from");
    int64_t where_id = dict_key_id(inner, "where");
    int64_t by_id    = dict_key_id(inner, "by");
    int64_t take_id  = dict_key_id(inner, "take");
    int64_t asc_id   = dict_key_id(inner, "asc");
    int64_t desc_id  = dict_key_id(inner, "desc");

    uint8_t agg_index = 0;
    for (int64_t i = 0; i + 1 < iv_n; i += 2) {
        int64_t kid = iv[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id)
            continue;
        ray_t* val = iv[i + 1];
        if (!is_group_dag_agg_expr(val))
            continue;
        ray_t** ae = (ray_t**)ray_data(val);
        uint16_t op = resolve_agg_opcode(ae[0]->i64);
        if (kid == filter_name && op == OP_COUNT) {
            out->enabled = 1;
            out->agg_index = agg_index;
            out->min_count_exclusive = threshold;
            /* Keeps the LARGEST counts (count > threshold): consumers read
             * .desc as the direction, so state it rather than relying on a
             * zero default. */
            out->desc = 1;
            DICT_VIEW_CLOSE(iv);
            return true;
        }
        agg_index++;
    }
    DICT_VIEW_CLOSE(iv);
    return false;
}

static bool positive_take_i64(ray_t* expr, int64_t* out) {
    if (!expr) return false;
    ray_t* v = ray_eval(expr);
    if (!v || RAY_IS_ERR(v)) return false;
    bool ok = false;
    int64_t n = 0;
    if (v->type == -RAY_I64) { n = v->i64; ok = n > 0; }
    else if (v->type == -RAY_I32) { n = v->i32; ok = n > 0; }
    ray_release(v);
    if (!ok) return false;
    *out = n;
    return true;
}

static bool match_group_desc_count_take(ray_t** dict_elems, int64_t dict_n,
                                        int64_t from_id, int64_t where_id,
                                        int64_t by_id, int64_t take_id,
                                        int64_t asc_id, int64_t desc_id,
                                        ray_group_emit_filter_t* out) {
    /* Detects `(select … by … <asc:|desc:> AGGCOL take: N)` where AGGCOL
     * is the name of an output agg col with op ∈ {COUNT, SUM, MIN, MAX}
     * and N is a positive atom ≤ 1024.  Returns the filter pre-filled so
     * the consumer (group/fused_group materialize) can heap-extract the
     * top-N groups by AGGCOL.value before emitting rows.  AVG and
     * higher-order aggs (STDDEV/VAR/PEARSON/MEDIAN) fall through — their
     * ordering doesn't reduce to a single int64 row slot read.
     *
     * The 1024 cap matches the stack-resident heap budget shared by the
     * three concrete consumer sites (mk_apply_count_emit_filter,
     * v2_emit's per-partition compact, the n_keys>1 macro path).  Larger
     * N drops through to the full sort + take so the heap doesn't
     * overflow the stack. */
    ray_t* take_expr = NULL;
    int64_t order_name = -1;
    uint8_t want_desc = 1;
    bool seen_dir = false;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == take_id) take_expr = dict_elems[i + 1];
        else if (kid == desc_id || kid == asc_id) {
            if (seen_dir) return false;  /* both asc: and desc: → ambiguous */
            seen_dir = true;
            ray_t* v = dict_elems[i + 1];
            if (!v || v->type != -RAY_SYM) return false;
            order_name = v->i64;
            want_desc = (kid == desc_id) ? 1 : 0;
        }
    }
    int64_t take_n = 0;
    if (order_name < 0 || !positive_take_i64(take_expr, &take_n))
        return false;
    if (take_n > 1024) return false;

    uint8_t agg_index = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id)
            continue;
        ray_t* val = dict_elems[i + 1];
        if (!is_group_dag_agg_expr(val))
            continue;
        ray_t** ae = (ray_t**)ray_data(val);
        uint16_t op = resolve_agg_opcode(ae[0]->i64);
        if (kid == order_name &&
            (op == OP_COUNT || op == OP_SUM ||
             op == OP_MIN   || op == OP_MAX)) {
            out->enabled = 1;
            out->agg_index = agg_index;
            out->min_count_exclusive = 0;
            out->top_count_take = take_n;
            out->agg_op = op;
            out->desc = want_desc;
            return true;
        }
        agg_index++;
    }
    return false;
}

/* True for `(fn arg ...)` where fn resolves to a RAY_UNARY marked
 * RAY_FN_AGGR — i.e. a builtin aggregator (sum/avg/min/max/count and
 * the non-whitelisted med/dev/var/stddev/etc).  Used to route these
 * through the streaming-style per-group AGG branch rather than the
 * full ray_eval per-group fallback.  This is a SUPERSET of is_agg_expr:
 * it includes everything resolve_agg_opcode names plus the AGGR
 * builtins that lack a streaming-engine opcode. */
static int is_aggr_unary_call(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    int64_t n = ray_len(expr);
    if (n < 2) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (elems[0]->type != -RAY_SYM) return 0;
    ray_t* fn_obj = ray_env_get(elems[0]->i64);
    if (!fn_obj || fn_obj->type != RAY_UNARY) return 0;
    return (fn_obj->attrs & RAY_FN_AGGR) != 0;
}

static int is_streaming_aggr_unary_call(ray_t* expr) {
    if (!is_aggr_unary_call(expr)) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    return !expr_contains_call_named(elems[1], "distinct", 8);
}

static int is_plain_count_expr(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    int64_t n = ray_len(expr);
    if (n < 2) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (!elems[0] || elems[0]->type != -RAY_SYM) return 0;
    if (resolve_agg_opcode(elems[0]->i64) != OP_COUNT) return 0;
    return !expr_contains_call_named(elems[1], "distinct", 8);
}

/* NOTE: binary-aggregator gates (is_aggr_binary_call /
 * is_streaming_aggr_binary_call) are not needed at the planner-call
 * sites for the canonical fast path — `(pearson_corr x y)` flows
 * through is_agg_expr → is_group_dag_agg_expr → the OP_GROUP planning
 * block that emits ray_group_build.  Eval-fallback (aggr_unary_per_group_buf
 * twin for two-input shapes, LIST keys, etc.) will need them; add
 * alongside that path when it's wired. */

/* Detect `(count (distinct <inner>))` exactly — the only shape that
 * routes through the OP_COUNT_DISTINCT fast path per group.  Returns
 * the inner expression on success, NULL otherwise.  More complex
 * forms like `(count (distinct (+ col 1)))` are accepted; the inner
 * expr is full-table-evaluable.  Anything where the outer call is
 * not a plain `(count …)` or the inner is not a plain `(distinct …)`
 * is rejected so the eval fallback handles it. */
/* AST-level idiom rewrites for per-group aggregator slot.
 *
 * Mirrors the DAG-level rewrites in src/ops/idiom.c, but at the AST
 * stage — idiom.c's DAG pass walks `inputs[]` only, so it never reaches
 * agg subtrees that live in OP_GROUP's ext->agg_ins[].  Without this,
 * `(select {m: (first (asc v)) by: k from: T})` errors `domain` while
 * the equivalent `(min v)` works.
 *
 * Patterns recognised (parallel to idiom.c's ray_idioms table):
 *   (first (asc col))    -> (min col)    if col is null-free
 *   (last  (asc col))    -> (max col)    if col is null-free
 *   (count (asc col))    -> (count col)
 *   (count (desc col))   -> (count col)
 *   (count (reverse col))-> (count col)
 *
 * The null-free precondition for first/last matches idiom.c's
 * pre_no_nulls_on_asc_input — first(asc null-bearing) returns the null
 * (xasc puts nulls first) while min(...) skips nulls.
 *
 * On match: *op_out and *arg_out point to the simpler op + col expr;
 * caller builds agg_ins[i] from *arg_out.  Returns true if rewritten. */
static bool simplify_agg_idiom(ray_t* val_expr, ray_t* tbl,
                                uint16_t* op_out, ray_t** arg_out) {
    if (!val_expr || val_expr->type != RAY_LIST || ray_len(val_expr) < 2) return false;
    ray_t** outer = (ray_t**)ray_data(val_expr);
    if (!outer[0] || outer[0]->type != -RAY_SYM) return false;
    ray_t* outer_nm = ray_sym_str(outer[0]->i64);
    if (!outer_nm) return false;
    const char* op_s = ray_str_ptr(outer_nm);
    size_t op_n = ray_str_len(outer_nm);

    ray_t* inner = outer[1];
    if (!inner || inner->type != RAY_LIST || ray_len(inner) < 2) return false;
    ray_t** inner_e = (ray_t**)ray_data(inner);
    if (!inner_e[0] || inner_e[0]->type != -RAY_SYM) return false;
    ray_t* inner_nm = ray_sym_str(inner_e[0]->i64);
    if (!inner_nm) return false;
    const char* wrap_s = ray_str_ptr(inner_nm);
    size_t wrap_n = ray_str_len(inner_nm);
    ray_t* col_expr = inner_e[1];

    bool wrap_is_asc     = (wrap_n == 3 && memcmp(wrap_s, "asc", 3) == 0);
    bool wrap_is_desc    = (wrap_n == 4 && memcmp(wrap_s, "desc", 4) == 0);
    bool wrap_is_reverse = (wrap_n == 7 && memcmp(wrap_s, "reverse", 7) == 0);
    if (!wrap_is_asc && !wrap_is_desc && !wrap_is_reverse) return false;

    /* (count (asc|desc|reverse col)) -> (count col) — cardinality preserved */
    if (op_n == 5 && memcmp(op_s, "count", 5) == 0) {
        *op_out = OP_COUNT;
        *arg_out = col_expr;
        return true;
    }

    /* (first|last (asc col)) -> (min|max col) — only when col is null-free */
    if (!wrap_is_asc) return false;
    bool is_first = (op_n == 5 && memcmp(op_s, "first", 5) == 0);
    bool is_last  = (op_n == 4 && memcmp(op_s, "last",  4) == 0);
    if (!is_first && !is_last) return false;

    /* Null-free precondition: col_expr must be a column ref naming a
     * null-free col of tbl.  Mirrors idiom.c:pre_no_nulls_on_asc_input. */
    if (!col_expr || col_expr->type != -RAY_SYM || (col_expr->attrs & ATTR_QUOTED))
        return false;
    ray_t* col = ray_table_get_col(tbl, col_expr->i64);
    if (!col || ray_vec_has_nulls(col)) return false;

    *op_out = is_first ? OP_MIN : OP_MAX;
    *arg_out = col_expr;
    return true;
}

static ray_t* match_count_distinct(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return NULL;
    int64_t n = ray_len(expr);
    if (n != 2) return NULL;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (!elems[0] || elems[0]->type != -RAY_SYM) return NULL;
    ray_t* nm = ray_sym_str(elems[0]->i64);
    if (!nm || ray_str_len(nm) != 5 ||
        memcmp(ray_str_ptr(nm), "count", 5) != 0) return NULL;

    ray_t* inner = elems[1];
    if (!inner || inner->type != RAY_LIST) return NULL;
    int64_t in_n = ray_len(inner);
    if (in_n != 2) return NULL;
    ray_t** in_elems = (ray_t**)ray_data(inner);
    if (!in_elems[0] || in_elems[0]->type != -RAY_SYM) return NULL;
    ray_t* dnm = ray_sym_str(in_elems[0]->i64);
    if (!dnm || ray_str_len(dnm) != 8 ||
        memcmp(ray_str_ptr(dnm), "distinct", 8) != 0) return NULL;
    return in_elems[1];
}

/* Count the OUTPUT entries of a select dict view: every key/value pair
 * whose key is not a clause keyword.  This is the natural bound for all
 * per-output collection arrays in the select compile path (the former
 * fixed-16 slot arrays).  dict_n is the element count of the flattened
 * [k0 v0 k1 v1 ...] view, so outputs <= dict_n/2 (dict_n is now unbounded).
 * Contract: callers must pass a non-OOM dict view (dict_n >= 0); an
 * OOM view (dict_n == -1) yields 0, which callers clamp to 1. */
static int64_t select_output_count(ray_t** dict_elems, int64_t dict_n) {
    int64_t from_id    = ray_sym_intern("from",    4);
    int64_t where_id   = ray_sym_intern("where",   5);
    int64_t by_id      = ray_sym_intern("by",      2);
    int64_t take_id    = ray_sym_intern("take",    4);
    int64_t asc_id     = ray_sym_intern("asc",     3);
    int64_t desc_id    = ray_sym_intern("desc",    4);
    int64_t nearest_id = ray_sym_intern("nearest", 7);
    int64_t n = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id ||
            kid == nearest_id) continue;
        n++;
    }
    return n;
}

/* Upper bound on the aggregate subcalls a compound output decomposes
 * into (try_decompose_agg_arith extracts each is_group_dag_agg_expr
 * node as one hidden slot).  Pure structural walk — cheap, compile-time. */
static int64_t count_agg_subexprs(ray_t* expr) {
    if (!expr) return 0;
    if (is_group_dag_agg_expr(expr)) return 1;
    if (expr->type != RAY_LIST) return 0;
    ray_t** e = (ray_t**)ray_data(expr);
    int64_t n = ray_len(expr), c = 0;
    for (int64_t i = 0; i < n; i++) c += count_agg_subexprs(e[i]);
    return c;
}

/* Walk expr once, gather unique column-ref symbol ids that resolve to
 * columns of `tbl`.  Dotted refs (`Timestamp.ss`) record the head
 * segment.  Caps at `max_out` entries (16 is plenty for s: clauses);
 * returns the count gathered.  Used by the per-group fallback to slice
 * each ref exactly once per group instead of re-walking the AST. */
static int collect_col_refs(ray_t* expr, ray_t* tbl,
                            int64_t* out_syms, int max_out, int n) {
    if (!expr || n >= max_out) return n;
    if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
        int64_t want = -1;
        if (ray_table_get_col(tbl, expr->i64)) {
            want = expr->i64;
        } else if (ray_sym_is_dotted(expr->i64)) {
            const int64_t* segs;
            int nsegs = ray_sym_segs(expr->i64, &segs);
            if (nsegs >= 1 && ray_table_get_col(tbl, segs[0])) want = segs[0];
        }
        if (want >= 0) {
            for (int i = 0; i < n; i++) if (out_syms[i] == want) return n;
            if (n < max_out) out_syms[n++] = want;
        }
        return n;
    }
    if (expr->type == RAY_LIST) {
        ray_t** elems = (ray_t**)ray_data(expr);
        int64_t cnt = ray_len(expr);
        for (int64_t i = 0; i < cnt && n < max_out; i++)
            n = collect_col_refs(elems[i], tbl, out_syms, max_out, n);
    }
    return n;
}

/* A group-key expression that reads exactly ONE flat SYM column is a
 * function of the symbol alone.  Evaluate it once per symbol actually
 * present in the column (du distinct ids, first-seen order, as a SYM
 * vector adopting the column's domain) and spread the du results over the
 * rows — instead of running every string op over every row, where each
 * substr/str-find over SYM interns its output per row.  Returns a fresh
 * nrows-long key vector, or NULL when the shape does not apply (caller
 * falls back to the plain evaluation).  Only when the distinct count is
 * well below the row count: the expression costs O(du) here, the spread
 * O(nrows) plain reads. */
/* Is `e` a pure, row-local function of the column `col_sym` (plus
 * let-bound names)?  Heads must come from a fixed list of elementwise
 * operators — anything positional (differ, fills, deltas, moving windows),
 * a user function, or a free symbol that is not the column (a global
 * vector of table length) disqualifies the per-symbol evaluation. */
static bool derived_key_head_ok(int64_t head) {
    static const char* const ok[] = {
        "let", "if", "cond", "and", "or", "not",
        "==", "!=", "<", "<=", ">", ">=", "+", "-", "*", "/", "%",
        "abs", "neg", "floor", "ceil", "round", "xbar", "as",
        "substr", "str-find", "strlen", "within", "nil?", "upper", "lower", "like",
    };
    ray_t* name = ray_sym_str(head);
    if (!name || RAY_IS_ERR(name)) return false;
    size_t n = ray_str_len(name);
    const char* q = ray_str_ptr(name);
    for (size_t i = 0; i < sizeof(ok) / sizeof(ok[0]); i++)
        if (strlen(ok[i]) == n && memcmp(ok[i], q, n) == 0) return true;
    return false;
}
static bool derived_key_expr_ok(ray_t* e, ray_t* tbl, int64_t col_sym, int64_t* bound, int nbound) {
    if (!e) return false;
    if (e->type == -RAY_SYM) {
        /* A symbol literal — unless it names a column of the table, which
         * the DAG compiler resolves to that column. */
        if (e->attrs & ATTR_QUOTED) return ray_table_get_col(tbl, e->i64) == NULL;
        if (e->i64 == col_sym) return true;
        for (int i = 0; i < nbound; i++) if (bound[i] == e->i64) return true;
        return false;                                       /* free name */
    }
    if (e->type != RAY_LIST) return true;                   /* atom / vector literal */
    int64_t n = ray_len(e);
    if (n < 1) return false;
    ray_t** el = (ray_t**)ray_data(e);
    if (el[0]->type != -RAY_SYM || (el[0]->attrs & ATTR_QUOTED) || !derived_key_head_ok(el[0]->i64))
        return false;
    /* The name must still mean the builtin: the DAG compiler inlines a
     * global lambda of that name ahead of the builtin, and a let-bound
     * name shadows it too. */
    for (int i = 0; i < nbound; i++) if (bound[i] == el[0]->i64) return false;
    {
        ray_t* gv = ray_env_get(el[0]->i64);          /* borrowed */
        if (gv && gv->type == RAY_LAMBDA) return false;
    }
    ray_t* hs = ray_sym_str(el[0]->i64);
    size_t hl = ray_str_len(hs);
    const char* hp = ray_str_ptr(hs);
    if (hl == 3 && memcmp(hp, "let", 3) == 0) {
        if (n != 4 || el[1]->type != -RAY_SYM || nbound >= 32) return false;
        if (!derived_key_expr_ok(el[2], tbl, col_sym, bound, nbound)) return false;
        bound[nbound] = el[1]->i64;
        return derived_key_expr_ok(el[3], tbl, col_sym, bound, nbound + 1);
    }
    if (hl == 4 && memcmp(hp, "cond", 4) == 0) {
        for (int64_t i = 1; i < n; i++) {
            if (el[i]->type != RAY_LIST || ray_len(el[i]) != 2) return false;
            ray_t** cp = (ray_t**)ray_data(el[i]);
            bool is_else = cp[0]->type == -RAY_SYM && !(cp[0]->attrs & ATTR_QUOTED) &&
                           ray_str_len(ray_sym_str(cp[0]->i64)) == 4 &&
                           memcmp(ray_str_ptr(ray_sym_str(cp[0]->i64)), "else", 4) == 0;
            if (!is_else && !derived_key_expr_ok(cp[0], tbl, col_sym, bound, nbound)) return false;
            if (!derived_key_expr_ok(cp[1], tbl, col_sym, bound, nbound)) return false;
        }
        return true;
    }
    for (int64_t i = 1; i < n; i++)
        if (!derived_key_expr_ok(el[i], tbl, col_sym, bound, nbound)) return false;
    return true;
}

/* Name for a computed group key: the last bare (unquoted) symbol of the
 * form, else `key` — the rule the eval-level group path applies. */
static int64_t derived_key_name(ray_t* by_expr) {
    if (by_expr && by_expr->type == RAY_LIST && by_expr->len >= 2) {
        ray_t** be = (ray_t**)ray_data(by_expr);
        for (int64_t i = by_expr->len - 1; i >= 1; i--)
            if (be[i]->type == -RAY_SYM && !(be[i]->attrs & ATTR_QUOTED)) return be[i]->i64;
    }
    return ray_sym_intern("key", 3);
}
/* The column of a computed group key `v` over `tbl`, caller owns it.  The
 * caller has pushed a query scope binding `tbl`'s columns; a partitioned
 * column the key reads is bound flattened for the evaluation and restored
 * after.  Compiled as a projection where it can be: group-key expressions
 * have the same row-wise semantics as projections, and eval-level `if`
 * tests whole-vector truthiness and would choose one branch for every
 * input row. */
static ray_t* group_key_eval(ray_t* v, ray_t* tbl) {
    /* Exact-size carve: collect_col_refs dedups against real table
     * columns, so ray_table_ncols(tbl) is a hard upper bound. */
    int64_t ncols_max = ray_table_ncols(tbl);
    if (ncols_max < 1) ncols_max = 1;
    ray_t* refs_hdr = NULL;
    int64_t* ref_syms = (int64_t*)scratch_alloc(&refs_hdr,
            (size_t)ncols_max * (sizeof(int64_t) + sizeof(ray_t*)));
    if (!ref_syms) return ray_error("oom", NULL);
    ray_t** materialized_refs = (ray_t**)(ref_syms + ncols_max);
    int n_refs = collect_col_refs(v, tbl, ref_syms, (int)ncols_max, 0);
    for (int ri = 0; ri < n_refs; ri++) materialized_refs[ri] = NULL;
    ray_t* col_vec = NULL;
    for (int ri = 0; ri < n_refs && !col_vec; ri++) {
        ray_t* ref_col = ray_table_get_col(tbl, ref_syms[ri]);
        if (ref_col && (RAY_IS_PARTED(ref_col->type) ||
                        ref_col->type == RAY_MAPCOMMON)) {
            ray_t* flat = query_materialize_parted_col(ref_col);
            if (!flat || RAY_IS_ERR(flat)) {
                col_vec = flat ? flat : ray_error("oom", NULL);
                break;
            }
            materialized_refs[ri] = flat;
            ray_env_set_query_local(ref_syms[ri], flat);
        }
    }
    if (!col_vec) {
        ray_graph_t* key_graph = ray_graph_new(tbl);
        if (!key_graph) col_vec = ray_error("oom", NULL);
        else {
            ray_op_t* key_op = compile_expr_dag(key_graph, v);
            if (key_op) {
                key_op = ray_optimize(key_graph, key_op);
                col_vec = ray_execute(key_graph, key_op);
                if (!col_vec) col_vec = ray_error("domain", "by-dict key execution failed");
            }
            ray_graph_free(key_graph);
        }
        if (!col_vec) col_vec = ray_eval(v);
        if (ray_is_lazy(col_vec))
            col_vec = ray_lazy_materialize(col_vec);
    }
    for (int ri = 0; ri < n_refs; ri++) {
        if (materialized_refs[ri]) {
            ray_t* ref_col = ray_table_get_col(tbl, ref_syms[ri]);
            if (ref_col) ray_env_set_query_local(ref_syms[ri], ref_col);
            ray_release(materialized_refs[ri]);
        }
    }
    scratch_free(refs_hdr);
    return col_vec;
}

#define DERIVED_KEY_MAX_DOMAIN (64LL * 1024 * 1024)
/* The distinct-symbol evaluation, in chunks and over STR.
 *
 * Feeding the DAG a SYM column whose ids live in a FILE domain makes every
 * string step of the expression a permanent cost: substr/str-find intern
 * each distinct result into the global runtime table (with the dotted
 * split, about 1 KB per URL) and an `if` that keeps the column itself
 * translates its cells into runtime ids too.  Over a 20M-entry vocabulary
 * that is tens of GB that never come back.
 *
 * Here the referenced column is presented to the same DAG as a STR column
 * built from the vocabulary bytes, CHUNK rows at a time: every
 * intermediate is then a chunk-sized STR vector that dies with the chunk,
 * and only the FINAL key strings are interned — once per distinct value,
 * into a domain of the key vector's own (released with the result; the
 * global table never grows by a derived value; positions = first-seen
 * order of the keys, the order unsorted groups come out in).  Applies only when the
 * expression's result over the SYM column is SYM (so the result has the
 * type the caller expects).  File prefixes use the raw snapshot, the
 * runtime domain its borrowed atom array, and a private in-memory domain
 * resolves its stable atoms through the tail path.  Any other shape
 * returns NULL and the caller takes the one-shot SYM evaluation. */
/* Chunk length in distinct values: 256 dispatch rounds of morsels (2M
 * rows).  Enough rows for the pooled string ops to spread over the
 * workers, while every chunk-scoped STR intermediate stays in the
 * low hundreds of MB at URL-sized strings. */
#define DERIVED_KEY_CHUNK ((int64_t)RAY_MORSEL_ELEMS * RAY_DISPATCH_MORSELS * 256)
#ifdef DEBUG
static int64_t g_derived_key_chunk_test = 0;
/* Test seam: a positive value replaces the chunk length until reset to 0,
 * so a test can drive several chunks through a small vocabulary. */
void ray_derived_key_chunk_set_for_test(int64_t rows) {
    g_derived_key_chunk_test = rows > 0 ? rows : 0;
}
#endif
static int64_t derived_key_chunk_rows(void) {
#ifdef DEBUG
    if (g_derived_key_chunk_test > 0) return g_derived_key_chunk_test;
#endif
    return DERIVED_KEY_CHUNK;
}

/* ---- chunk STR build: the entries at positions dv[lo, lo+n) as one STR
 * vector over one pool.  Lengths and byte pointers are read on the workers
 * (the raw snapshot needs no lock); a position past the file prefix — a
 * runtime-appended entry — is resolved through the domain on the calling
 * thread.  A serial prefix sum places the pooled rows, then the workers
 * write the descriptors and copy the pooled bytes into disjoint ranges. */
typedef struct {
    const void*                 dv;
    uint8_t                     dattrs;
    int64_t                     lo;
    const ray_sym_domain_raw_t* raw;
    const char**                ptr;
    uint32_t*                   len;
    uint32_t*                   off;
    ray_str_t*                  dst;
    char*                       pool;
    ray_t* const*               rstr;   /* runtime domain: the borrowed atom array */
    uint32_t                    rcnt;
    atomic_int                  late;
} dk_chunk_ctx_t;

static void dk_chunk_scan_fn(void* vctx, uint32_t wid, int64_t start, int64_t end) {
    (void)wid;
    dk_chunk_ctx_t* c = (dk_chunk_ctx_t*)vctx;
    int late = 0;
    for (int64_t i = start; i < end; i++) {
        int64_t pos = ray_read_sym(c->dv, c->lo + i, RAY_SYM, c->dattrs);
        if (c->rstr) {
            /* runtime domain: the atom array borrowed once per chunk (no
             * lock — nothing interns into the global table while the chunk
             * is read) */
            ray_t* a = (pos >= 0 && pos < (int64_t)c->rcnt) ? c->rstr[pos] : NULL;
            if (a && ray_str_len(a) <= UINT32_MAX) {
                c->ptr[i] = ray_str_ptr(a);
                c->len[i] = (uint32_t)ray_str_len(a);
            } else {
                c->ptr[i] = NULL;
                c->len[i] = UINT32_MAX;
                late++;
            }
        } else if (pos >= 0 && pos < c->raw->count) {
            size_t sl = 0;
            c->ptr[i] = ray_sym_domain_raw_str(c->raw, pos, &sl);
            c->len[i] = (uint32_t)sl;
        } else {
            c->ptr[i] = NULL;
            c->len[i] = UINT32_MAX;
            late++;
        }
    }
    if (late) atomic_fetch_add_explicit(&c->late, late, memory_order_relaxed);
}

static void dk_chunk_fill_fn(void* vctx, uint32_t wid, int64_t start, int64_t end) {
    (void)wid;
    const dk_chunk_ctx_t* c = (const dk_chunk_ctx_t*)vctx;
    for (int64_t i = start; i < end; i++) {
        ray_str_t* d = &c->dst[i];
        uint32_t l = c->len[i];
        memset(d, 0, sizeof(*d));
        d->len = l;
        if (l == 0) continue;
        if (l <= RAY_STR_INLINE_MAX) {
            memcpy(d->data, c->ptr[i], l);
        } else {
            memcpy(c->pool + c->off[i], c->ptr[i], l);
            memcpy(d->prefix, c->ptr[i], 4);
            d->pool_off = c->off[i];
        }
    }
}

/* The bytes of entries dv[lo, lo+n) as (ptr, len) views: read on the
 * workers off the raw snapshot (FILE) or the borrowed atom array (the
 * runtime domain), a position past either resolved through the domain on
 * the calling thread.  False on a length past UINT32_MAX. */
static bool dk_chunk_views(const void* dv, uint8_t dattrs, int64_t lo, int64_t n,
                           const ray_sym_domain_raw_t* raw, struct ray_sym_domain_s* dom,
                           const char** ptr, uint32_t* len) {
    dk_chunk_ctx_t c;
    memset(&c, 0, sizeof(c));
    c.dv = dv; c.dattrs = dattrs; c.lo = lo; c.raw = raw;
    c.ptr = ptr;
    c.len = len;
    if (dom == ray_sym_runtime_domain()) {
        ray_t** rs = NULL; uint32_t rc = 0;
        ray_sym_strings_borrow(&rs, &rc);
        c.rstr = rs; c.rcnt = rc;
    }
    atomic_store_explicit(&c.late, 0, memory_order_relaxed);
    ray_pool_t* pool = ray_pool_get();
    if (ray_pool_par_dispatch_ok(pool, n, RAY_PARALLEL_THRESHOLD))
        ray_pool_dispatch(pool, dk_chunk_scan_fn, &c, n);
    else
        dk_chunk_scan_fn(&c, 0, 0, n);
    if (atomic_load_explicit(&c.late, memory_order_relaxed)) {
        for (int64_t i = 0; i < n; i++) {
            if (len[i] != UINT32_MAX) continue;
            int64_t pos = ray_read_sym(dv, lo + i, RAY_SYM, dattrs);
            ray_t* a = ray_sym_domain_str(dom, pos);
            size_t sl = a ? ray_str_len(a) : 0;
            if (sl > UINT32_MAX) return false;
            ptr[i] = a ? ray_str_ptr(a) : "";
            len[i] = (uint32_t)sl;
        }
    }
    return true;
}

static ray_t* derived_key_chunk_strs(const void* dv, uint8_t dattrs, int64_t lo, int64_t n,
                                     const ray_sym_domain_raw_t* raw,
                                     struct ray_sym_domain_s* dom) {
    ray_t* aux_hdr = NULL;
    size_t ptr_sz = (size_t)n * sizeof(const char*);
    size_t u32_sz = (size_t)n * sizeof(uint32_t);
    char* mem = (char*)scratch_alloc(&aux_hdr, ptr_sz + 2 * u32_sz);
    if (!mem) return NULL;
    dk_chunk_ctx_t c;
    memset(&c, 0, sizeof(c));
    c.ptr = (const char**)mem;
    c.len = (uint32_t*)(mem + ptr_sz);
    c.off = (uint32_t*)(mem + ptr_sz + u32_sz);
    if (!dk_chunk_views(dv, dattrs, lo, n, raw, dom, c.ptr, c.len)) { scratch_free(aux_hdr); return NULL; }
    ray_pool_t* pool = ray_pool_get();
    bool par = ray_pool_par_dispatch_ok(pool, n, RAY_PARALLEL_THRESHOLD);
    uint64_t total = 0;
    for (int64_t i = 0; i < n; i++) {
        if (c.len[i] <= RAY_STR_INLINE_MAX) continue;
        c.off[i] = (uint32_t)total;
        total += c.len[i];
        if (total > UINT32_MAX) { scratch_free(aux_hdr); return NULL; }
    }
    ray_t* sv = ray_vec_new(RAY_STR, n);
    if (!sv || RAY_IS_ERR(sv)) { if (sv) ray_error_free(sv); scratch_free(aux_hdr); return NULL; }
    sv->len = n;
    if (total > 0) {
        ray_t* sp = ray_alloc((size_t)total);
        if (!sp || RAY_IS_ERR(sp)) { ray_release(sv); scratch_free(aux_hdr); return NULL; }
        sp->type = RAY_U8;
        sp->len  = (int64_t)total;
        sv->str_pool = sp;
        c.pool = (char*)ray_data(sp);
    }
    c.dst = (ray_str_t*)ray_data(sv);
    if (par) ray_pool_dispatch(pool, dk_chunk_fill_fn, &c, n);
    else     dk_chunk_fill_fn(&c, 0, 0, n);
    scratch_free(aux_hdr);
    return sv;
}

/* ---- chunk result interning: each distinct key string of a chunk is
 * interned once, all of them under one lock, in the order of their first
 * row (the order the ids — and with them the group order — came out in
 * when one serial pass did the dedupe), and its id spread over the rows
 * that hold it.  The hashes are the intern table's own (ray_hash_bytes).
 * The dedupe runs on the workers: the rows are split into DK_NPART
 * partitions by the top bits of their hash (a count and a stable scatter
 * over row ranges), and each partition is deduped by one task in an
 * open-addressing table of its own, rows in row order, so every string's
 * representative is its first row.  One serial pass over the rows then
 * lists the representatives in row order for the batch intern. */
#define DK_PART_BITS 6
#define DK_NPART     (1 << DK_PART_BITS)
/* A row as its partition's dedupe reads it: everything in one place, the
 * partition's rows contiguous. */
typedef struct dk_item_s {
    const char* p;
    uint32_t    h;
    uint32_t    l;
    int32_t     row;
    int32_t     pad;
} dk_item_t;
typedef struct {
    const char* const* sp;
    const uint32_t*    sl;
    const uint32_t*    hash;
    int64_t            n;
    int64_t            nt;      /* row ranges of the count / scatter passes */
    int64_t*           cnt;     /* [nt][DK_NPART]: counts, then write cursors */
    struct dk_item_s*  items;   /* rows grouped by partition, row order within */
    const int64_t*     pbase;   /* [DK_NPART + 1]: partition starts in items */
    const int64_t*     tbase;   /* [DK_NPART + 1]: partition tables in tab */
    uint64_t*          tab;     /* hash << 32 | index in the partition; all ones = empty */
    int32_t*           rep;     /* first row holding the same string */
    int32_t*           ord;     /* at a representative row: its ordinal */
    const int64_t*     did;     /* id per ordinal */
    int64_t*           out;
    int64_t*           rcnt;    /* [nt]: representatives per row range, then their base */
    const char**       dstr;    /* the representatives' strings, lengths, hashes */
    size_t*            dlen;
    uint32_t*          dhsh;
} dk_dedupe_ctx_t;

static inline uint32_t dk_part_of(uint32_t h) { return h >> (32 - DK_PART_BITS); }

static void dk_count_fn(void* vctx, uint32_t wid, int64_t t0, int64_t t1) {
    (void)wid;
    dk_dedupe_ctx_t* c = (dk_dedupe_ctx_t*)vctx;
    for (int64_t t = t0; t < t1; t++) {
        int64_t* k = c->cnt + t * DK_NPART;
        memset(k, 0, DK_NPART * sizeof(int64_t));
        int64_t lo = c->n * t / c->nt, hi = c->n * (t + 1) / c->nt;
        for (int64_t i = lo; i < hi; i++) k[dk_part_of(c->hash[i])]++;
    }
}

static void dk_scatter_fn(void* vctx, uint32_t wid, int64_t t0, int64_t t1) {
    (void)wid;
    dk_dedupe_ctx_t* c = (dk_dedupe_ctx_t*)vctx;
    for (int64_t t = t0; t < t1; t++) {
        int64_t* k = c->cnt + t * DK_NPART;
        int64_t lo = c->n * t / c->nt, hi = c->n * (t + 1) / c->nt;
        for (int64_t i = lo; i < hi; i++) {
            uint32_t h = c->hash[i];
            dk_item_t* it = &c->items[k[dk_part_of(h)]++];
            it->p = c->sp[i];
            it->h = h;
            it->l = c->sl[i];
            it->row = (int32_t)i;
            it->pad = 0;
        }
    }
}

static void dk_dedupe_fn(void* vctx, uint32_t wid, int64_t p0, int64_t p1) {
    (void)wid;
    dk_dedupe_ctx_t* c = (dk_dedupe_ctx_t*)vctx;
    for (int64_t p = p0; p < p1; p++) {
        int64_t lo = c->pbase[p], hi = c->pbase[p + 1];
        if (lo == hi) continue;
        uint64_t* tab = c->tab + c->tbase[p];
        uint64_t mask = (uint64_t)(c->tbase[p + 1] - c->tbase[p]) - 1;
        memset(tab, 0xff, (size_t)(mask + 1) * sizeof(uint64_t));
        const dk_item_t* items = c->items + lo;
        for (int64_t k = 0; k < hi - lo; k++) {
            const dk_item_t* it = &items[k];
            uint32_t h = it->h;
            uint32_t l = it->l;
            uint64_t s = ((uint64_t)h * 0x9E3779B97F4A7C15ull >> 32) & mask;
            for (;;) {
                uint64_t e = tab[s];
                if (e == UINT64_MAX) {
                    tab[s] = (uint64_t)h << 32 | (uint32_t)k;
                    c->rep[it->row] = it->row;
                    break;
                }
                const dk_item_t* rt = &items[(uint32_t)e];
                if ((uint32_t)(e >> 32) == h && rt->l == l &&
                    (l == 0 || memcmp(rt->p, it->p, l) == 0)) {
                    c->rep[it->row] = rt->row;
                    break;
                }
                s = (s + 1) & mask;
            }
        }
    }
}

static void dk_reps_count_fn(void* vctx, uint32_t wid, int64_t t0, int64_t t1) {
    (void)wid;
    const dk_dedupe_ctx_t* c = (const dk_dedupe_ctx_t*)vctx;
    for (int64_t t = t0; t < t1; t++) {
        int64_t lo = c->n * t / c->nt, hi = c->n * (t + 1) / c->nt, k = 0;
        for (int64_t i = lo; i < hi; i++) k += c->rep[i] == (int32_t)i;
        c->rcnt[t] = k;
    }
}

static void dk_reps_list_fn(void* vctx, uint32_t wid, int64_t t0, int64_t t1) {
    (void)wid;
    const dk_dedupe_ctx_t* c = (const dk_dedupe_ctx_t*)vctx;
    for (int64_t t = t0; t < t1; t++) {
        int64_t lo = c->n * t / c->nt, hi = c->n * (t + 1) / c->nt, d = c->rcnt[t];
        for (int64_t i = lo; i < hi; i++) {
            if (c->rep[i] != (int32_t)i) continue;
            c->ord[i] = (int32_t)d;
            c->dstr[d] = c->sp[i]; c->dlen[d] = c->sl[i]; c->dhsh[d] = c->hash[i];
            d++;
        }
    }
}

static void dk_assign_fn(void* vctx, uint32_t wid, int64_t lo, int64_t hi) {
    (void)wid;
    const dk_dedupe_ctx_t* c = (const dk_dedupe_ctx_t*)vctx;
    for (int64_t i = lo; i < hi; i++) c->out[i] = c->did[c->ord[c->rep[i]]];
}

/* Intern the n strings (sp[i], sl[i]) with hashes hash[i] into the key
 * domain kdom; out[i] gets the position of string i.  n stays below
 * INT32_MAX (a chunk).  The partitioned rows and their tables live only
 * through the dedupe; the arrays the batch intern reads are sized by the
 * distinct count.  The domain is this execution's own and the
 * representatives are handed over in first-row order, so a key's position
 * is its first-seen rank — the order unsorted groups come out in, the same
 * at every thread count. */
static bool dk_intern_views(const char* const* sp, const uint32_t* sl, const uint32_t* hash,
                            int64_t n, struct ray_sym_domain_s* kdom, int64_t* out) {
    if (n <= 0) return true;
    ray_pool_t* pool = ray_pool_get();
    bool par = ray_pool_par_dispatch_ok(pool, n, RAY_PARALLEL_THRESHOLD);
    int64_t nt = 1;
    if (par) {
        nt = (int64_t)ray_pool_total_workers(pool) * 4;
        if (nt > n / 4096) nt = n / 4096;
        if (nt < 1) nt = 1;
    }
    size_t cnt_sz  = (size_t)nt * DK_NPART * sizeof(int64_t);
    size_t base_sz = (size_t)(DK_NPART + 1) * sizeof(int64_t);
    size_t i32_sz  = (size_t)n * sizeof(int32_t);
    ray_t *hdr = NULL, *ihdr = NULL, *thdr = NULL, *dhdr = NULL;
    char* mem = (char*)scratch_alloc(&hdr, cnt_sz + 2 * base_sz + 2 * i32_sz);
    dk_item_t* items = (dk_item_t*)scratch_alloc(&ihdr, (size_t)n * sizeof(dk_item_t));
    if (!mem || !items) { scratch_free(hdr); scratch_free(ihdr); return false; }
    int64_t* cnt   = (int64_t*)mem;  mem += cnt_sz;
    int64_t* pbase = (int64_t*)mem;  mem += base_sz;
    int64_t* tbase = (int64_t*)mem;  mem += base_sz;
    int32_t* rep   = (int32_t*)mem;  mem += i32_sz;
    int32_t* ord   = (int32_t*)mem;

    dk_dedupe_ctx_t c = {
        .sp = sp, .sl = sl, .hash = hash, .n = n, .nt = nt, .cnt = cnt, .items = items,
        .pbase = pbase, .tbase = tbase, .rep = rep, .ord = ord, .out = out,
    };
    if (par) ray_pool_dispatch_n(pool, dk_count_fn, &c, (uint32_t)nt);
    else     dk_count_fn(&c, 0, 0, nt);
    /* partition starts, per-range write cursors, table extents (a power
     * of two at least twice the partition's rows) */
    int64_t run = 0, tsz = 0;
    for (int p = 0; p < DK_NPART; p++) {
        pbase[p] = run;
        tbase[p] = tsz;
        int64_t pc = 0;
        for (int64_t t = 0; t < nt; t++) {
            int64_t k = cnt[t * DK_NPART + p];
            cnt[t * DK_NPART + p] = run + pc;
            pc += k;
        }
        run += pc;
        int64_t slots = 16;
        while (slots < 2 * pc) slots <<= 1;
        tsz += pc ? slots : 0;
    }
    pbase[DK_NPART] = run;
    tbase[DK_NPART] = tsz;
    c.tab = (uint64_t*)scratch_alloc(&thdr, (size_t)(tsz ? tsz : 1) * sizeof(uint64_t));
    if (!c.tab) { scratch_free(ihdr); scratch_free(hdr); return false; }
    if (par) ray_pool_dispatch_n(pool, dk_scatter_fn, &c, (uint32_t)nt);
    else     dk_scatter_fn(&c, 0, 0, nt);
    if (par) ray_pool_dispatch_n(pool, dk_dedupe_fn, &c, DK_NPART);
    else     dk_dedupe_fn(&c, 0, 0, DK_NPART);
    ray_profile_tick("derived key: keys deduplicated");
    scratch_free(thdr);
    scratch_free(ihdr);

    /* the representatives in row order: ordinals, then their strings —
     * counted per row range on the workers, a prefix over the ranges, then
     * numbered and listed per range (the count array is free by now) */
    c.rcnt = cnt;
    if (par) ray_pool_dispatch_n(pool, dk_reps_count_fn, &c, (uint32_t)nt);
    else     dk_reps_count_fn(&c, 0, 0, nt);
    int64_t nd = 0;
    for (int64_t t = 0; t < nt; t++) { int64_t k = cnt[t]; cnt[t] = nd; nd += k; }
    size_t dstr_sz = (size_t)nd * sizeof(const char*);
    size_t dlen_sz = (size_t)nd * sizeof(size_t);
    size_t did_sz  = (size_t)nd * sizeof(int64_t);
    char* dm = (char*)scratch_alloc(&dhdr, dstr_sz + dlen_sz + did_sz + (size_t)nd * sizeof(uint32_t));
    if (!dm) { scratch_free(hdr); return false; }
    const char** dstr = (const char**)dm;  dm += dstr_sz;
    size_t*      dlen = (size_t*)dm;       dm += dlen_sz;
    int64_t*     did  = (int64_t*)dm;      dm += did_sz;
    uint32_t*    dhsh = (uint32_t*)dm;
    c.dstr = dstr; c.dlen = dlen; c.dhsh = dhsh;
    if (par) ray_pool_dispatch_n(pool, dk_reps_list_fn, &c, (uint32_t)nt);
    else     dk_reps_list_fn(&c, 0, 0, nt);
    /* Key strings are values of this query, not names: they go into the
     * key vector's own domain (released with it), never into the global
     * table — a host extracted from millions of URLs costs one entry
     * there for as long as the result lives, and nothing afterwards. */
    ray_profile_tick("derived key: representatives listed");
    bool ok = ray_sym_domain_intern_batch(kdom, nd, dstr, dlen, dhsh, did);
    ray_profile_tick("derived key: key domain interned");
    if (ok) {
        c.did = did;
        if (par) ray_pool_dispatch(pool, dk_assign_fn, &c, n);
        else     dk_assign_fn(&c, 0, 0, n);
        ray_profile_tick("derived key: keys interned");
    }
    scratch_free(dhdr);
    scratch_free(hdr);
    return ok;
}

/* The STR evaluation's key vector: its views and hashes (workers), then
 * the view interning above. */
typedef struct {
    const ray_str_t* desc;
    const char*      pool;
    const char**     sp;
    uint32_t*        sl;
    uint32_t*        hash;
} dk_hash_ctx_t;

static void dk_hash_fn(void* vctx, uint32_t wid, int64_t start, int64_t end) {
    (void)wid;
    const dk_hash_ctx_t* c = (const dk_hash_ctx_t*)vctx;
    for (int64_t i = start; i < end; i++) {
        const ray_str_t* d = &c->desc[i];
        const char* p = ray_str_t_ptr(d, c->pool);
        c->sp[i] = p;
        c->sl[i] = d->len;
        c->hash[i] = (uint32_t)ray_hash_bytes(p, d->len);
    }
}

static bool derived_key_intern_chunk(ray_t* kc, int64_t n, struct ray_sym_domain_s* kdom,
                                     int64_t* out) {
    dk_hash_ctx_t hc;
    memset(&hc, 0, sizeof(hc));
    str_resolve(kc, &hc.desc, &hc.pool);
    ray_t* hdr = NULL;
    char* mem = (char*)scratch_alloc(&hdr, (size_t)n * (sizeof(const char*) + 2 * sizeof(uint32_t)));
    if (!mem) return false;
    hc.sp   = (const char**)mem;
    hc.sl   = (uint32_t*)(mem + (size_t)n * sizeof(const char*));
    hc.hash = hc.sl + n;
    ray_pool_t* rp = ray_pool_get();
    if (ray_pool_par_dispatch_ok(rp, n, RAY_PARALLEL_THRESHOLD))
        ray_pool_dispatch(rp, dk_hash_fn, &hc, n);
    else
        dk_hash_fn(&hc, 0, 0, n);
    bool ok = dk_intern_views(hc.sp, hc.sl, hc.hash, n, kdom, out);
    scratch_free(hdr);
    return ok;
}

/* A chunk result that is already SYM (a symbol-literal arm, say): its
 * cells as strings through their own domain, interned into the key
 * domain like any other key string. */
static bool derived_key_intern_sym_cells(ray_t* kc, int64_t n, struct ray_sym_domain_s* kdom,
                                         int64_t* out) {
    ray_t* hdr = NULL;
    char* mem = (char*)scratch_alloc(&hdr, (size_t)n * (sizeof(const char*) + 2 * sizeof(uint32_t)));
    if (!mem) return false;
    const char** sp = (const char**)mem;
    uint32_t* sl = (uint32_t*)(mem + (size_t)n * sizeof(const char*));
    uint32_t* hash = sl + n;
    for (int64_t i = 0; i < n; i++) {
        ray_t* a = ray_sym_vec_cell(kc, i);
        size_t l = a ? ray_str_len(a) : 0;
        if (l > UINT32_MAX) { scratch_free(hdr); return false; }
        sp[i] = a ? ray_str_ptr(a) : "";
        sl[i] = (uint32_t)l;
        hash[i] = (uint32_t)ray_hash_bytes(sp[i], l);
    }
    bool ok = dk_intern_views(sp, sl, hash, n, kdom, out);
    scratch_free(hdr);
    return ok;
}

/* ---- the spread pass of derived_key_over_sym_domain: each row takes the
 * key of its symbol's slot (workers, disjoint ranges). */
typedef struct {
    const void*    cd;
    uint8_t        attrs;
    int64_t        dn;
    const int32_t* pos;
    const int64_t* key;      /* spread: key per slot; NULL = write the slot */
    int64_t*       out;
    bool           sparse;   /* a selection: an unslotted id (-1) takes slot 0 */
} dk_rows_ctx_t;

static void dk_spread_fn(void* vctx, uint32_t wid, int64_t start, int64_t end) {
    (void)wid;
    const dk_rows_ctx_t* c = (const dk_rows_ctx_t*)vctx;
    if (c->sparse) {
        /* rows outside the selection have no slot: they take slot 0 (the
         * grouping never reads them — it honours the same selection) */
        if (c->key) {
            for (int64_t r = start; r < end; r++) {
                int32_t s = c->pos[ray_read_sym(c->cd, r, RAY_SYM, c->attrs)];
                c->out[r] = c->key[s >= 0 ? s : 0];
            }
        } else {
            for (int64_t r = start; r < end; r++) {
                int32_t s = c->pos[ray_read_sym(c->cd, r, RAY_SYM, c->attrs)];
                c->out[r] = s >= 0 ? s : 0;
            }
        }
        return;
    }
    if (c->key) {
        for (int64_t r = start; r < end; r++)
            c->out[r] = c->key[c->pos[ray_read_sym(c->cd, r, RAY_SYM, c->attrs)]];
    } else {
        for (int64_t r = start; r < end; r++)
            c->out[r] = c->pos[ray_read_sym(c->cd, r, RAY_SYM, c->attrs)];
    }
}

/* ---- the rows of a selection, in row order (every row when sel is NULL):
 * a cursor over the rowsel's morsel flags — a NONE morsel is skipped, an
 * ALL morsel is a row range, a MIX morsel its index list. */
typedef struct {
    ray_t*          sel;
    int64_t         nrows;
    int64_t         seg, n_segs;
    const uint8_t*  flags;
    const uint32_t* offs;
    const uint16_t* idx;
    int64_t         base;
    int64_t         r, end;      /* a range: the next row and its end */
    int64_t         k, kend;     /* an index list: the next entry and its end */
    int             mode;        /* 0 = between morsels, 1 = range, 2 = indices */
} dk_sel_it_t;

static void dk_sel_it_init(dk_sel_it_t* it, ray_t* sel, int64_t nrows) {
    memset(it, 0, sizeof(*it));
    it->sel = sel; it->nrows = nrows; it->seg = -1;
    if (!sel) { it->mode = 1; it->r = 0; it->end = nrows; return; }
    ray_rowsel_t* m = ray_rowsel_meta(sel);
    it->n_segs = m->n_segs;
    it->flags = ray_rowsel_flags(sel);
    it->offs  = ray_rowsel_offsets(sel);
    it->idx   = ray_rowsel_idx(sel);
}

/* The next selected row, or -1 past the last. */
static inline int64_t dk_sel_it_next(dk_sel_it_t* it) {
    for (;;) {
        if (it->mode == 1) { if (it->r < it->end) return it->r++; }
        else if (it->mode == 2) { if (it->k < it->kend) return it->base + it->idx[it->k++]; }
        if (!it->sel || ++it->seg >= it->n_segs) { it->mode = 0; return -1; }
        uint8_t f = it->flags[it->seg];
        it->base = it->seg * RAY_MORSEL_ELEMS;
        if (f == RAY_SEL_NONE) { it->mode = 0; continue; }
        if (f == RAY_SEL_ALL) {
            it->mode = 1; it->r = it->base;
            it->end = it->base + RAY_MORSEL_ELEMS;
            if (it->end > it->nrows) it->end = it->nrows;
        } else {
            it->mode = 2; it->k = it->offs[it->seg]; it->kend = it->offs[it->seg + 1];
        }
    }
}

/* ---- the key as a per-value program ------------------------------------
 * The chunk DAG runs every operator of the key as a pass of its own over
 * the chunk: a dispatch, a result vector and a barrier per operator — two
 * dozen of them for a URL-host key, the `if` merges among them.  When every
 * operator is one whose STR-column semantics restate per value, the key is
 * compiled instead into a straight-line program over three register kinds
 * (a string view — bytes of the value itself or of a literal —, an I64, a
 * BOOL) and run on the workers, a batch of values at a time (each
 * instruction across the batch): no chunk-sized intermediate, and the
 * result is a view of the value or a literal, hashed where it is computed.
 * Each operator keeps the chunk DAG's semantics over STR:
 *   - the empty string is the null string: nil? is true, strlen is null;
 *   - str-find: the 0-based offset of the first match of the compiled
 *     search pattern (ray_strpat_find, as exec_str_find), else null;
 *   - substr: 1-based; a start below 1 counts from 1, a negative or overlong
 *     length runs to the end, a start past the end gives "", a null start
 *     or length gives "" (substr_view_fn);
 *   - + - * give null for a null operand and wrap otherwise;
 *   - I64 comparisons read null as INT64_MIN; strings compare by bytes;
 *   - within is (and (>= x lo) (<= x hi)) over a literal two-I64 range;
 *   - if/cond pick an arm.  Both arms are computed, as the DAG computes
 *     them: every operator here is total and pure.
 * Anything else — another operator, a symbol literal, a number that is
 * not an I64, mixed kinds — returns -1 and the key stays with the DAG. */
enum { DKP_STR = 0, DKP_I64, DKP_BOOL };
enum {
    DKO_COL, DKO_LSTR, DKO_LIT,
    DKO_EQ, DKO_NE, DKO_LT, DKO_LE, DKO_GT, DKO_GE, DKO_SEQ, DKO_SNE,
    DKO_ADD, DKO_SUB, DKO_MUL, DKO_AND, DKO_OR, DKO_NOT,
    DKO_NILS, DKO_NILI, DKO_IF, DKO_FIND, DKO_SUBSTR, DKO_STRLEN,
};
#define DKP_MAX_INS  128
#define DKP_MAX_PATS 16
#define DKP_MAX_LET  32
typedef struct {
    uint8_t     op, kind;
    uint16_t    a, b, c;
    int64_t     imm;        /* I64 / BOOL literal; pattern index of a FIND */
    const char* sp;         /* STR literal bytes */
    uint32_t    sl;
} dkp_ins_t;
typedef struct {
    int          n, col, npat, res, nex;
    uint8_t      ex[DKP_MAX_INS];     /* the non-literal instructions, in order */
    dkp_ins_t    ins[DKP_MAX_INS];
    ray_strpat_t pat[DKP_MAX_PATS];
} dkp_prog_t;
typedef struct { const char* p; int64_t v; } dkp_reg_t;   /* STR: bytes, length */

static int dkp_emit(dkp_prog_t* P, int op, int kind, int a, int b, int c) {
    if (P->n >= DKP_MAX_INS) return -1;
    dkp_ins_t* x = &P->ins[P->n];
    memset(x, 0, sizeof(*x));
    x->op = (uint8_t)op; x->kind = (uint8_t)kind;
    x->a = (uint16_t)a; x->b = (uint16_t)b; x->c = (uint16_t)c;
    return P->n++;
}

static int dkp_lit(dkp_prog_t* P, int kind, int64_t v) {
    int r = dkp_emit(P, DKO_LIT, kind, 0, 0, 0);
    if (r >= 0) P->ins[r].imm = v;
    return r;
}

static bool dkp_sym_is(ray_t* e, const char* name) {
    if (!e || e->type != -RAY_SYM) return false;
    ray_t* s = ray_sym_str(e->i64);
    size_t n = strlen(name);
    return s && ray_str_len(s) == n && memcmp(ray_str_ptr(s), name, n) == 0;
}

/* Compile e; returns its register, or -1 when the program cannot hold it.
 * bsym/breg are the let bindings in scope, innermost last (the DAG
 * compiler's lookup order: let binding, then the column). */
static int dkp_compile(dkp_prog_t* P, ray_t* e, int64_t col, int64_t* bsym, int* breg, int nb) {
    if (!e) return -1;
    #define DKP_K(r) (P->ins[(r)].kind)
    switch (e->type) {
    case -RAY_SYM:
        if (e->attrs & ATTR_QUOTED) return -1;
        for (int i = nb - 1; i >= 0; i--) if (bsym[i] == e->i64) return breg[i];
        if (e->i64 != col) return -1;
        if (P->col < 0) P->col = dkp_emit(P, DKO_COL, DKP_STR, 0, 0, 0);
        return P->col;
    case -RAY_I64:  return dkp_lit(P, DKP_I64, e->i64);
    case -RAY_BOOL: return dkp_lit(P, DKP_BOOL, e->b8 ? 1 : 0);
    case -RAY_STR: {
        size_t l = ray_str_len(e);
        if (l > UINT32_MAX) return -1;
        int r = dkp_emit(P, DKO_LSTR, DKP_STR, 0, 0, 0);
        if (r >= 0) { P->ins[r].sp = ray_str_ptr(e); P->ins[r].sl = (uint32_t)l; }
        return r;
    }
    case RAY_LIST: break;
    default: return -1;
    }
    int64_t n = ray_len(e);
    if (n < 1) return -1;
    ray_t** el = (ray_t**)ray_data(e);
    ray_t* h = el[0];
    if (!h || h->type != -RAY_SYM || (h->attrs & ATTR_QUOTED)) return -1;
    ray_t* hs = ray_sym_str(h->i64);
    if (!hs) return -1;
    const char* hp = ray_str_ptr(hs);
    size_t hl = ray_str_len(hs);
    #define DKP_IS(lit) (hl == sizeof(lit) - 1 && memcmp(hp, lit, hl) == 0)
    #define DKP_ARG(i) dkp_compile(P, el[(i)], col, bsym, breg, nb)

    if (DKP_IS("let")) {
        if (n != 4 || !el[1] || el[1]->type != -RAY_SYM || nb >= DKP_MAX_LET) return -1;
        int v = DKP_ARG(2);
        if (v < 0) return -1;
        bsym[nb] = el[1]->i64;
        breg[nb] = v;
        return dkp_compile(P, el[3], col, bsym, breg, nb + 1);
    }
    if (DKP_IS("if")) {
        if (n != 4) return -1;
        int c = DKP_ARG(1), t = DKP_ARG(2), f = DKP_ARG(3);
        if (c < 0 || t < 0 || f < 0 || DKP_K(c) != DKP_BOOL || DKP_K(t) != DKP_K(f)) return -1;
        return dkp_emit(P, DKO_IF, DKP_K(t), c, t, f);
    }
    if (DKP_IS("cond")) {
        /* right to left into an if chain; the last clause is the else */
        int acc = -1;
        for (int64_t i = n - 1; i >= 1; i--) {
            ray_t* cl = el[i];
            if (!cl || cl->type != RAY_LIST || ray_len(cl) != 2) return -1;
            ray_t** cp = (ray_t**)ray_data(cl);
            if (dkp_sym_is(cp[0], "else")) {
                if (i != n - 1) return -1;
                acc = dkp_compile(P, cp[1], col, bsym, breg, nb);
                if (acc < 0) return -1;
                continue;
            }
            if (acc < 0) return -1;
            int c = dkp_compile(P, cp[0], col, bsym, breg, nb);
            int t = dkp_compile(P, cp[1], col, bsym, breg, nb);
            if (c < 0 || t < 0 || DKP_K(c) != DKP_BOOL || DKP_K(t) != DKP_K(acc)) return -1;
            acc = dkp_emit(P, DKO_IF, DKP_K(t), c, t, acc);
            if (acc < 0) return -1;
        }
        return acc;
    }
    if (DKP_IS("and") || DKP_IS("or")) {
        if (n < 2 || n > 65) return -1;
        int op = DKP_IS("and") ? DKO_AND : DKO_OR;
        int acc = DKP_ARG(1);
        if (acc < 0 || DKP_K(acc) != DKP_BOOL) return -1;
        for (int64_t i = 2; i < n; i++) {
            int r = DKP_ARG(i);
            if (r < 0 || DKP_K(r) != DKP_BOOL) return -1;
            acc = dkp_emit(P, op, DKP_BOOL, acc, r, 0);
            if (acc < 0) return -1;
        }
        return acc;
    }
    if (DKP_IS("not")) {
        if (n != 2) return -1;
        int a = DKP_ARG(1);
        if (a < 0 || DKP_K(a) != DKP_BOOL) return -1;
        return dkp_emit(P, DKO_NOT, DKP_BOOL, a, 0, 0);
    }
    if (DKP_IS("nil?")) {
        if (n != 2) return -1;
        int a = DKP_ARG(1);
        if (a < 0 || DKP_K(a) == DKP_BOOL) return -1;
        return dkp_emit(P, DKP_K(a) == DKP_STR ? DKO_NILS : DKO_NILI, DKP_BOOL, a, 0, 0);
    }
    if (DKP_IS("strlen")) {
        if (n != 2) return -1;
        int a = DKP_ARG(1);
        if (a < 0 || DKP_K(a) != DKP_STR) return -1;
        return dkp_emit(P, DKO_STRLEN, DKP_I64, a, 0, 0);
    }
    if (DKP_IS("str-find")) {
        if (n != 3 || !el[2] || el[2]->type != -RAY_STR || P->npat >= DKP_MAX_PATS) return -1;
        int a = DKP_ARG(1);
        if (a < 0 || DKP_K(a) != DKP_STR) return -1;
        if (!ray_strpat_compile(ray_str_ptr(el[2]), ray_str_len(el[2]), &P->pat[P->npat])) return -1;
        int r = dkp_emit(P, DKO_FIND, DKP_I64, a, 0, 0);
        if (r >= 0) P->ins[r].imm = P->npat++;
        return r;
    }
    if (DKP_IS("substr")) {
        if (n != 4) return -1;
        int a = DKP_ARG(1), b = DKP_ARG(2), c = DKP_ARG(3);
        if (a < 0 || b < 0 || c < 0 || DKP_K(a) != DKP_STR || DKP_K(b) != DKP_I64 ||
            DKP_K(c) != DKP_I64) return -1;
        return dkp_emit(P, DKO_SUBSTR, DKP_STR, a, b, c);
    }
    if (DKP_IS("within")) {
        ray_t* rg = n == 3 ? el[2] : NULL;
        if (!rg || !ray_is_vec(rg) || rg->type != RAY_I64 || rg->len != 2 ||
            (rg->attrs & RAY_ATTR_SLICE)) return -1;
        int x = DKP_ARG(1);
        if (x < 0 || DKP_K(x) != DKP_I64) return -1;
        const int64_t* rv = (const int64_t*)ray_data(rg);
        int lo = dkp_lit(P, DKP_I64, rv[0]);
        int hi = dkp_lit(P, DKP_I64, rv[1]);
        if (lo < 0 || hi < 0) return -1;
        int ge = dkp_emit(P, DKO_GE, DKP_BOOL, x, lo, 0);
        int le = dkp_emit(P, DKO_LE, DKP_BOOL, x, hi, 0);
        if (ge < 0 || le < 0) return -1;
        return dkp_emit(P, DKO_AND, DKP_BOOL, ge, le, 0);
    }
    int bop = DKP_IS("==") ? DKO_EQ : DKP_IS("!=") ? DKO_NE : DKP_IS("<") ? DKO_LT :
              DKP_IS("<=") ? DKO_LE : DKP_IS(">") ? DKO_GT : DKP_IS(">=") ? DKO_GE :
              DKP_IS("+") ? DKO_ADD : DKP_IS("-") ? DKO_SUB : DKP_IS("*") ? DKO_MUL : -1;
    if (bop < 0 || n != 3) return -1;
    int a = DKP_ARG(1), b = DKP_ARG(2);
    if (a < 0 || b < 0 || DKP_K(a) != DKP_K(b)) return -1;
    if (DKP_K(a) == DKP_STR) {
        if (bop != DKO_EQ && bop != DKO_NE) return -1;
        return dkp_emit(P, bop == DKO_EQ ? DKO_SEQ : DKO_SNE, DKP_BOOL, a, b, 0);
    }
    if (DKP_K(a) != DKP_I64) return -1;
    return dkp_emit(P, bop, bop >= DKO_ADD ? DKP_I64 : DKP_BOOL, a, b, 0);
    #undef DKP_ARG
    #undef DKP_IS
    #undef DKP_K
}

/* The program of a key over the column col_sym whose result is a string,
 * or false. */
static bool dkp_build(dkp_prog_t* P, ray_t* by_expr, int64_t col_sym) {
    memset(P, 0, sizeof(*P));
    P->col = -1;
    int64_t bsym[DKP_MAX_LET];
    int breg[DKP_MAX_LET];
    int r = dkp_compile(P, by_expr, col_sym, bsym, breg, 0);

    P->res = r;
    for (int k = 0; k < P->n; k++)
        if (P->ins[k].op != DKO_LIT && P->ins[k].op != DKO_LSTR) P->ex[P->nex++] = (uint8_t)k;
    return r >= 0 && P->ins[r].kind == DKP_STR;
}

/* The first occurrence of a literal search pattern — what ray_strpat_find
 * returns for a pattern without `?` or classes (memmem there) — found by
 * memchr on its first byte: the values are short, and memmem's setup
 * outweighs the scan over a URL-sized haystack. */
static inline int64_t dkp_find_literal(const char* s, size_t sn, const char* nd, size_t nl) {
    if (nl == 0) return 0;
    if (nl > sn) return NULL_I64;
    const char* hay = s;
    size_t rem = sn;
    while (rem >= nl) {
        const char* hit = (const char*)memchr(hay, nd[0], rem - nl + 1);
        if (!hit) return NULL_I64;
        if (memcmp(hit + 1, nd + 1, nl - 1) == 0) return (int64_t)(hit - s);
        rem -= (size_t)(hit - hay) + 1;
        hay = hit + 1;
    }
    return NULL_I64;
}

/* One batch of m (<= DKP_BATCH) values through the program: each
 * instruction over the whole batch before the next one (register k of
 * value t is R[k * DKP_BATCH + t]), so the dispatch is paid per batch, not
 * per value.  Literal registers were filled once by the caller. */
#define DKP_BATCH 64
static void dkp_run_batch(const dkp_prog_t* P, dkp_reg_t* R, const char* const* sp,
                          const uint32_t* sl, int m) {
    for (int j = 0; j < P->nex; j++) {
        int k = P->ex[j];
        const dkp_ins_t* x = &P->ins[k];
        dkp_reg_t* o = R + (size_t)k * DKP_BATCH;
        const dkp_reg_t* a = R + (size_t)x->a * DKP_BATCH;
        const dkp_reg_t* b = R + (size_t)x->b * DKP_BATCH;
        const dkp_reg_t* c = R + (size_t)x->c * DKP_BATCH;
        switch (x->op) {
        case DKO_COL:
            for (int t = 0; t < m; t++) { o[t].p = sp[t]; o[t].v = sl[t]; }
            break;
        case DKO_EQ:  for (int t = 0; t < m; t++) o[t].v = a[t].v == b[t].v; break;
        case DKO_NE:  for (int t = 0; t < m; t++) o[t].v = a[t].v != b[t].v; break;
        case DKO_LT:  for (int t = 0; t < m; t++) o[t].v = a[t].v <  b[t].v; break;
        case DKO_LE:  for (int t = 0; t < m; t++) o[t].v = a[t].v <= b[t].v; break;
        case DKO_GT:  for (int t = 0; t < m; t++) o[t].v = a[t].v >  b[t].v; break;
        case DKO_GE:  for (int t = 0; t < m; t++) o[t].v = a[t].v >= b[t].v; break;
        case DKO_SEQ:
        case DKO_SNE: {
            int64_t want = x->op == DKO_SEQ;
            for (int t = 0; t < m; t++) {
                bool eq = a[t].v == b[t].v &&
                          (a[t].v == 0 || memcmp(a[t].p, b[t].p, (size_t)a[t].v) == 0);
                o[t].v = eq ? want : !want;
            }
            break;
        }
        case DKO_ADD:
            for (int t = 0; t < m; t++)
                o[t].v = (a[t].v == NULL_I64 || b[t].v == NULL_I64) ? NULL_I64
                       : (int64_t)((uint64_t)a[t].v + (uint64_t)b[t].v);
            break;
        case DKO_SUB:
            for (int t = 0; t < m; t++)
                o[t].v = (a[t].v == NULL_I64 || b[t].v == NULL_I64) ? NULL_I64
                       : (int64_t)((uint64_t)a[t].v - (uint64_t)b[t].v);
            break;
        case DKO_MUL:
            for (int t = 0; t < m; t++)
                o[t].v = (a[t].v == NULL_I64 || b[t].v == NULL_I64) ? NULL_I64
                       : (int64_t)((uint64_t)a[t].v * (uint64_t)b[t].v);
            break;
        case DKO_AND:  for (int t = 0; t < m; t++) o[t].v = a[t].v & b[t].v; break;
        case DKO_OR:   for (int t = 0; t < m; t++) o[t].v = a[t].v | b[t].v; break;
        case DKO_NOT:  for (int t = 0; t < m; t++) o[t].v = !a[t].v; break;
        case DKO_NILS: for (int t = 0; t < m; t++) o[t].v = a[t].v == 0; break;
        case DKO_NILI: for (int t = 0; t < m; t++) o[t].v = a[t].v == NULL_I64; break;
        case DKO_IF:   for (int t = 0; t < m; t++) o[t] = a[t].v ? b[t] : c[t]; break;
        case DKO_FIND: {
            const ray_strpat_t* pt = &P->pat[x->imm];
            if (pt->literal) {
                for (int t = 0; t < m; t++)
                    o[t].v = dkp_find_literal(a[t].p, (size_t)a[t].v, pt->pat, pt->pat_len);
            } else {
                for (int t = 0; t < m; t++) {
                    size_t pos = 0;
                    o[t].v = ray_strpat_find(pt, a[t].p, (size_t)a[t].v, &pos) ? (int64_t)pos : NULL_I64;
                }
            }
            break;
        }
        case DKO_SUBSTR:
            for (int t = 0; t < m; t++) {
                int64_t st = b[t].v, ln = c[t].v, l = a[t].v;
                o[t].p = a[t].p; o[t].v = 0;
                if (st == NULL_I64 || ln == NULL_I64) continue;
                st -= 1;
                if (st < 0) st = 0;
                if (st >= l) continue;
                if (ln < 0 || ln > l - st) ln = l - st;
                if (ln <= 0) continue;
                o[t].p = a[t].p + st;
                o[t].v = ln;
            }
            break;
        case DKO_STRLEN: for (int t = 0; t < m; t++) o[t].v = a[t].v ? a[t].v : NULL_I64; break;
        default: break;
        }
    }
}

/* Workers: each value's key, in place over its view, and the key's hash.
 * regs holds one register file per worker. */
typedef struct {
    const dkp_prog_t* prog;
    const char**      sp;
    uint32_t*         sl;
    uint32_t*         hash;
    dkp_reg_t*        regs;
} dkp_run_ctx_t;

static void dkp_run_fn(void* vctx, uint32_t wid, int64_t start, int64_t end) {
    const dkp_run_ctx_t* c = (const dkp_run_ctx_t*)vctx;
    const dkp_prog_t* P = c->prog;
    dkp_reg_t* R = c->regs + (size_t)wid * (size_t)P->n * DKP_BATCH;
    const dkp_reg_t* res = R + (size_t)P->res * DKP_BATCH;
    /* literals once: the batches run the other instructions only */
    for (int k = 0; k < P->n; k++) {
        dkp_reg_t* o = R + (size_t)k * DKP_BATCH;
        if (P->ins[k].op == DKO_LIT)
            for (int t = 0; t < DKP_BATCH; t++) o[t].v = P->ins[k].imm;
        else if (P->ins[k].op == DKO_LSTR)
            for (int t = 0; t < DKP_BATCH; t++) { o[t].p = P->ins[k].sp; o[t].v = P->ins[k].sl; }
    }
    for (int64_t i = start; i < end; i += DKP_BATCH) {
        int m = end - i < DKP_BATCH ? (int)(end - i) : DKP_BATCH;
        dkp_run_batch(P, R, c->sp + i, c->sl + i, m);
        for (int t = 0; t < m; t++) {
            const char* p = res[t].v ? res[t].p : "";
            uint32_t l = (uint32_t)res[t].v;
            c->sp[i + t] = p;
            c->sl[i + t] = l;
            c->hash[i + t] = (uint32_t)ray_hash_bytes(p, l);
        }
    }
}

/* The keys of dv[lo, lo+n) through the program, interned into the key
 * domain kdom as kd[0, n). */
static bool dkp_chunk_keys(const dkp_prog_t* P, const void* dv, uint8_t dattrs, int64_t lo,
                           int64_t n, const ray_sym_domain_raw_t* raw,
                           struct ray_sym_domain_s* dom, struct ray_sym_domain_s* kdom,
                           int64_t* kd) {
    ray_pool_t* pool = ray_pool_get();
    bool par = ray_pool_par_dispatch_ok(pool, n, RAY_PARALLEL_THRESHOLD);
    uint32_t nwork = par ? ray_pool_total_workers(pool) : 1;
    ray_t *hdr = NULL, *rhdr = NULL;
    char* mem = (char*)scratch_alloc(&hdr, (size_t)n * (sizeof(const char*) + 2 * sizeof(uint32_t)));
    dkp_reg_t* regs = (dkp_reg_t*)scratch_alloc(&rhdr, (size_t)nwork * (size_t)P->n *
                                                       DKP_BATCH * sizeof(dkp_reg_t));
    if (!mem || !regs) { scratch_free(hdr); scratch_free(rhdr); return false; }
    dkp_run_ctx_t c = { .prog = P, .regs = regs };
    c.sp   = (const char**)mem;
    c.sl   = (uint32_t*)(mem + (size_t)n * sizeof(const char*));
    c.hash = c.sl + n;
    bool ok = dk_chunk_views(dv, dattrs, lo, n, raw, dom, c.sp, c.sl);
    if (ok) {
        if (par) ray_pool_dispatch(pool, dkp_run_fn, &c, n);
        else     dkp_run_fn(&c, 0, 0, n);
        ray_profile_tick("derived key: program over values");
        ok = dk_intern_views(c.sp, c.sl, c.hash, n, kdom, kd);
    }
    scratch_free(rhdr);
    scratch_free(hdr);
    return ok;
}

static ray_t* derived_key_str_chunks(ray_t* by_expr, int64_t col_sym, ray_t* dom_vec,
                                     struct ray_sym_domain_s* dom, int64_t du) {
    if (!dom || du <= 0) return NULL;
    ray_sym_domain_raw_t raw = {0};
    (void)ray_sym_domain_raw_pin(dom, &raw);

    /* The result type the SYM evaluation would give: compile (only) the
     * expression against the SYM column. */
    int8_t sym_out = 0;
    {
        ray_t* probe = ray_table_new(0);
        /* add_col retains the column itself; the caller keeps its own ref. */
        if (probe && !RAY_IS_ERR(probe)) probe = ray_table_add_col(probe, col_sym, dom_vec);
        if (!probe || RAY_IS_ERR(probe)) { if (probe) ray_error_free(probe); goto unpin_null; }
        ray_graph_t* gp = ray_graph_new(probe);
        if (gp) {
            ray_op_t* kop = compile_expr_dag(gp, by_expr);
            /* A key that compiles to a constant — a literal, or a let whose
             * body is one — evaluates to that literal: a string literal is
             * typed SYM by the compiler but executes as the STR it is.  The
             * one-shot evaluation (and the row path behind it) keeps that
             * type; this path would intern it into a symbol. */
            if (kop && kop->opcode != OP_CONST) sym_out = kop->out_type;
            ray_graph_free(gp);
        }
        ray_release(probe);
    }
    if (sym_out != RAY_SYM) goto unpin_null;

    ray_t* key_dom = ray_vec_new(RAY_SYM, du);
    if (!key_dom || RAY_IS_ERR(key_dom)) { if (key_dom) ray_error_free(key_dom); goto unpin_null; }
    key_dom->len = du;
    int64_t* kd = (int64_t*)ray_data(key_dom);
    const void* dv = ray_data(dom_vec);
    /* The key strings live in a domain of their own, owned by the key
     * vector (and by every vector that adopts it): the query's derived
     * values never enter the global table, and the whole dictionary goes
     * when the last result column holding it does.  Its positions are
     * this execution's first-seen ranks of the keys (dk_intern_views). */
    {
        struct ray_sym_domain_s* kdom = ray_sym_domain_new();
        if (!kdom) { ray_release(key_dom); goto unpin_null; }
        key_dom->sym_domain = kdom;               /* the vector takes the ref */
    }
    struct ray_sym_domain_s* kdom = key_dom->sym_domain;

    /* The per-value program when the key compiles to one, the chunk DAG
     * otherwise. */
    dkp_prog_t prog;
    bool use_prog = dkp_build(&prog, by_expr, col_sym);

    const int64_t chunk = derived_key_chunk_rows();
    for (int64_t lo = 0; lo < du; lo += chunk) {
        int64_t n = du - lo < chunk ? du - lo : chunk;
        if (use_prog) {
            if (!dkp_chunk_keys(&prog, dv, dom_vec->attrs, lo, n, &raw, dom, kdom, kd + lo)) goto fail;
            continue;
        }
        ray_t* sv = derived_key_chunk_strs(dv, dom_vec->attrs, lo, n, &raw, dom);
        if (!sv) goto fail;
        ray_t* mini = ray_table_new(0);
        if (mini && !RAY_IS_ERR(mini)) mini = ray_table_add_col(mini, col_sym, sv);
        ray_release(sv);
        if (!mini || RAY_IS_ERR(mini)) { if (mini) ray_error_free(mini); goto fail; }
        ray_t* kc = NULL;
        ray_graph_t* g2 = ray_graph_new(mini);
        if (g2) {
            ray_op_t* kop = compile_expr_dag(g2, by_expr);
            if (kop) kop = ray_optimize(g2, kop);
            if (kop) kc = ray_execute(g2, kop);
            ray_graph_free(g2);
        }
        ray_release(mini);
        if (kc && !RAY_IS_ERR(kc) && ray_is_lazy(kc)) kc = ray_lazy_materialize(kc);
        if (!kc || RAY_IS_ERR(kc)) { if (kc) ray_error_free(kc); goto fail; }
        if (!ray_is_vec(kc) || kc->len != n) { ray_release(kc); goto fail; }
        ray_profile_tick("derived key: chunk DAG");
        if (kc->type == RAY_STR) {
            if (!derived_key_intern_chunk(kc, n, kdom, kd + lo)) { ray_release(kc); goto fail; }
        } else if (RAY_IS_SYM(kc->type)) {
            /* The STR evaluation still produced symbols (e.g. a literal
             * symbol branch): their strings, into the key domain. */
            if (!derived_key_intern_sym_cells(kc, n, kdom, kd + lo)) { ray_release(kc); goto fail; }
        } else {
            ray_release(kc);
            goto fail;
        }
        ray_release(kc);
    }
    ray_sym_domain_raw_unpin(dom);
    return key_dom;
fail:
    ray_release(key_dom);
unpin_null:
    ray_sym_domain_raw_unpin(dom);
    return NULL;
}

/* ---- aggregates over the vocabulary -------------------------------------
 * A grouping whose key is derived from one SYM column C and whose every
 * aggregate reads only C — count(C), min(C), max(C), sum/avg(strlen C) —
 * is decided by the distinct values of C, not by the rows: a group's count
 * is the sum of its values' row counts, its min the smallest of its values,
 * its strlen sum the length-weighted count.  So: one grouping of the rows by
 * C itself (the engine's fastest shape: a column key, a count) gives the
 * distinct values with their counts; the key expression runs once per
 * distinct value (derived_key_str_chunks); and the aggregates are rewritten
 * over that per-value table.  Nothing per row is evaluated or accumulated
 * beyond the count histogram.  count(C) counts null rows too, avg(strlen C)
 * divides by the non-null rows, min/max skip null — the row-wise rules.
 * Returns NULL for every shape it does not take (the caller runs the row
 * path). */
enum { DKV_COUNT = 1, DKV_MIN, DKV_MAX, DKV_SUM_LEN, DKV_AVG_LEN };
#define DKV_MAX_AGGS 32

static ray_t* dkv_sym(const char* name) { return ray_sym(ray_sym_intern(name, strlen(name))); }

/* (head a) / (head a b) as an expression list; the arguments are consumed. */
static ray_t* dkv_call(const char* head, ray_t* a, ray_t* b) {
    ray_t* l = ray_list_new(b ? 3 : 2);
    if (!l || RAY_IS_ERR(l)) { if (a) ray_release(a); if (b) ray_release(b); return NULL; }
    ray_t* h = dkv_sym(head);
    l = ray_list_append(l, h); ray_release(h);
    l = ray_list_append(l, a); ray_release(a);
    if (b) { l = ray_list_append(l, b); ray_release(b); }
    if (!l || RAY_IS_ERR(l)) { if (l) ray_error_free(l); return NULL; }
    return l;
}

/* (select {keys[i]: vals[i] …}); keys are runtime sym ids, vals consumed. */
static ray_t* dkv_select(const int64_t* keys, ray_t** vals, int64_t n) {
    ray_t* kv = ray_sym_vec_new(RAY_SYM_W64, n);
    ray_t* vl = ray_list_new(n);
    if (!kv || RAY_IS_ERR(kv) || !vl || RAY_IS_ERR(vl)) {
        for (int64_t i = 0; i < n; i++) if (vals[i]) ray_release(vals[i]);
        if (kv && !RAY_IS_ERR(kv)) ray_release(kv);
        if (vl && !RAY_IS_ERR(vl)) ray_release(vl);
        return NULL;
    }
    kv->len = n;
    for (int64_t i = 0; i < n; i++) {
        ((int64_t*)ray_data(kv))[i] = keys[i];
        vl = ray_list_append(vl, vals[i]);
        ray_release(vals[i]);
    }
    ray_t* d = ray_dict_new(kv, vl);
    if (!d || RAY_IS_ERR(d)) { if (d) ray_error_free(d); return NULL; }
    ray_t* sel = ray_list_new(2);
    if (!sel || RAY_IS_ERR(sel)) { ray_release(d); return NULL; }
    ray_t* h = dkv_sym("select");
    sel = ray_list_append(sel, h); ray_release(h);
    sel = ray_list_append(sel, d); ray_release(d);
    return sel;
}

static bool dkv_is_col(ray_t* e, int64_t col) {
    return e && e->type == -RAY_SYM && !(e->attrs & ATTR_QUOTED) && e->i64 == col;
}

/* Which decomposable aggregate over C is `v`, or 0. */
static int dkv_agg_kind(ray_t* v, int64_t col) {
    if (!v || v->type != RAY_LIST || ray_len(v) != 2) return 0;
    ray_t** e = (ray_t**)ray_data(v);
    if (!e[0] || e[0]->type != -RAY_SYM) return 0;
    ray_t* hs = ray_sym_str(e[0]->i64);
    if (!hs) return 0;
    const char* h = ray_str_ptr(hs); size_t hl = ray_str_len(hs);
    bool is = false;
    #define DKV_IS(lit) (hl == sizeof(lit) - 1 && memcmp(h, lit, hl) == 0)
    if (DKV_IS("count") || DKV_IS("min") || DKV_IS("max")) {
        if (!dkv_is_col(e[1], col)) return 0;
        return DKV_IS("count") ? DKV_COUNT : DKV_IS("min") ? DKV_MIN : DKV_MAX;
    }
    is = DKV_IS("sum") || DKV_IS("avg");
    if (!is) return 0;
    ray_t* a = e[1];
    if (!a || a->type != RAY_LIST || ray_len(a) != 2) return 0;
    ray_t** ae = (ray_t**)ray_data(a);
    if (!ae[0] || ae[0]->type != -RAY_SYM) return 0;
    ray_t* as = ray_sym_str(ae[0]->i64);
    if (!as || ray_str_len(as) != 6 || memcmp(ray_str_ptr(as), "strlen", 6) != 0) return 0;
    if (!dkv_is_col(ae[1], col)) return 0;
    return DKV_IS("sum") ? DKV_SUM_LEN : DKV_AVG_LEN;
    #undef DKV_IS
}

/* Per distinct value (workers): its non-null row count and its
 * length-weighted row count, the length read off the pinned mapping.  A
 * position past the file prefix is marked (lenw = INT64_MIN) and resolved
 * through the domain on the calling thread. */
typedef struct {
    const void*    hc;
    uint8_t        attrs;
    const int64_t* cnt;
    int64_t*       nn;
    int64_t*       lenw;
    struct ray_sym_domain_s* dom;
    ray_sym_domain_raw_t raw;
    bool           raw_ok;
    ray_t* const*  rstr;      /* runtime domain: the borrowed atom array */
    uint32_t       rcnt;
    atomic_int     late;
} dkv_len_ctx_t;

static void dkv_len_fn(void* vctx, uint32_t wid, int64_t lo, int64_t hi) {
    (void)wid;
    dkv_len_ctx_t* c = (dkv_len_ctx_t*)vctx;
    int late = 0;
    for (int64_t i = lo; i < hi; i++) {
        int64_t pos = ray_read_sym(c->hc, i, RAY_SYM, c->attrs);
        c->nn[i] = pos > 0 ? c->cnt[i] : 0;
        if (pos <= 0) { c->lenw[i] = 0; continue; }
        if (c->rstr) {
            ray_t* a = pos < (int64_t)c->rcnt ? c->rstr[pos] : NULL;
            if (a) c->lenw[i] = c->cnt[i] * (int64_t)ray_str_len(a);
            else { c->lenw[i] = INT64_MIN; late++; }
        } else if (c->raw_ok && pos < c->raw.count) {
            size_t l; (void)ray_sym_domain_raw_str(&c->raw, pos, &l);
            c->lenw[i] = c->cnt[i] * (int64_t)l;
        } else {
            c->lenw[i] = INT64_MIN;
            late++;
        }
    }
    if (late) atomic_fetch_add_explicit(&c->late, late, memory_order_relaxed);
}

/* H (distinct values of C with their counts) in C's domain-position order.
 * The grouping that produced H emits in an order that depends on the core
 * count; the key domain is filled in H's order, and the rewrite's output
 * is put in key-position order, so it comes out the same at every core
 * count (the first-seen order of the key values over the vocabulary).  Positions are distinct, so the order is the
 * rank of each position among those present: a bitmap over the domain
 * (set on the workers), a running popcount per word, then a scatter on the
 * workers.  Returns a new table, or NULL (H kept). */
typedef struct {
    const void*     cd;
    uint8_t         attrs;
    int64_t         dom_count;
    uint64_t*       bits;
    const int64_t*  wrank;
    const int64_t*  hnd;
    void*           ncd;
    uint8_t         nattrs;
    int64_t*        nnd;
    atomic_int      bad;
} dkv_order_ctx_t;

static void dkv_order_mark_fn(void* vctx, uint32_t wid, int64_t lo, int64_t hi) {
    (void)wid;
    dkv_order_ctx_t* c = (dkv_order_ctx_t*)vctx;
    for (int64_t i = lo; i < hi; i++) {
        int64_t pos = ray_read_sym(c->cd, i, RAY_SYM, c->attrs);
        if (pos < 0 || pos >= c->dom_count) {
            atomic_store_explicit(&c->bad, 1, memory_order_relaxed);
            return;
        }
        atomic_fetch_or_explicit((_Atomic(uint64_t)*)&c->bits[pos >> 6],
                                 (uint64_t)1 << (pos & 63), memory_order_relaxed);
    }
}

static void dkv_order_scatter_fn(void* vctx, uint32_t wid, int64_t lo, int64_t hi) {
    (void)wid;
    const dkv_order_ctx_t* c = (const dkv_order_ctx_t*)vctx;
    for (int64_t i = lo; i < hi; i++) {
        int64_t pos = ray_read_sym(c->cd, i, RAY_SYM, c->attrs);
        int64_t w = pos >> 6;
        int64_t r = c->wrank[w] + __builtin_popcountll(c->bits[w] & (((uint64_t)1 << (pos & 63)) - 1));
        ray_write_sym(c->ncd, r, (uint64_t)pos, RAY_SYM, c->nattrs);
        c->nnd[r] = c->hnd[i];
    }
}

static ray_t* dkv_order_by_position(ray_t* H, int64_t dom_count) {
    ray_t* Hc = ray_table_get_col_idx(H, 0);
    ray_t* Hn = ray_table_get_col_idx(H, 1);
    int64_t du = ray_table_nrows(H);
    if (dom_count <= 0 || du <= 1) return NULL;
    int64_t nw = (dom_count + 63) / 64;
    ray_t *bh = NULL, *rh = NULL;
    uint64_t* bits  = (uint64_t*)scratch_calloc(&bh, (size_t)nw * sizeof(uint64_t));
    int64_t*  wrank = (int64_t*)scratch_alloc(&rh, (size_t)nw * sizeof(int64_t));
    if (!bits || !wrank) { scratch_free(bh); scratch_free(rh); return NULL; }
    dkv_order_ctx_t c;
    memset(&c, 0, sizeof(c));
    c.cd = ray_data(Hc); c.attrs = Hc->attrs; c.dom_count = dom_count;
    c.bits = bits; c.wrank = wrank;
    atomic_store_explicit(&c.bad, 0, memory_order_relaxed);
    ray_pool_t* pool = ray_pool_get();
    bool par = ray_pool_par_dispatch_ok(pool, du, RAY_PARALLEL_THRESHOLD);
    if (par) ray_pool_dispatch(pool, dkv_order_mark_fn, &c, du);
    else     dkv_order_mark_fn(&c, 0, 0, du);
    if (atomic_load_explicit(&c.bad, memory_order_relaxed)) { scratch_free(bh); scratch_free(rh); return NULL; }
    int64_t run = 0;
    for (int64_t w = 0; w < nw; w++) {
        wrank[w] = run;
        run += __builtin_popcountll(bits[w]);
    }
    ray_t* nc = ray_sym_vec_new(Hc->attrs & RAY_SYM_W_MASK, du);
    ray_t* nn = ray_vec_new(RAY_I64, du);
    if (!nc || RAY_IS_ERR(nc) || !nn || RAY_IS_ERR(nn)) {
        if (nc && !RAY_IS_ERR(nc)) ray_release(nc);
        if (nn && !RAY_IS_ERR(nn)) ray_release(nn);
        scratch_free(bh); scratch_free(rh);
        return NULL;
    }
    ray_sym_vec_adopt_domain(nc, Hc);
    nc->len = du; nn->len = du;
    c.ncd = ray_data(nc);
    c.nattrs = nc->attrs;
    c.nnd = (int64_t*)ray_data(nn);
    c.hnd = (const int64_t*)ray_data(Hn);
    if (par) ray_pool_dispatch(pool, dkv_order_scatter_fn, &c, du);
    else     dkv_order_scatter_fn(&c, 0, 0, du);
    if (Hc->attrs & RAY_ATTR_HAS_NULLS) nc->attrs |= RAY_ATTR_HAS_NULLS;
    scratch_free(bh); scratch_free(rh);
    ray_t* out = ray_table_new(2);
    if (!out || RAY_IS_ERR(out)) { ray_release(nc); ray_release(nn); return NULL; }
    out = ray_table_add_col(out, ray_table_col_name(H, 0), nc);
    out = ray_table_add_col(out, ray_table_col_name(H, 1), nn);
    ray_release(nc); ray_release(nn);
    if (!out || RAY_IS_ERR(out)) return NULL;
    return out;
}

static ray_t* derived_key_vocab_aggs(ray_t* tbl, ray_t* by_expr, ray_t* where_expr,
                                     ray_t** dict_elems, int64_t dict_n,
                                     int64_t from_id, int64_t by_id, int64_t where_id,
                                     int64_t take_id, int64_t asc_id, int64_t desc_id,
                                     int64_t nearest_id) {
    if (!by_expr || by_expr->type != RAY_LIST || !tbl) return NULL;
    int64_t ref_syms[2];
    if (collect_col_refs(by_expr, tbl, ref_syms, 2, 0) != 1) return NULL;
    int64_t col = ref_syms[0];
    int64_t bound[32];
    if (!derived_key_expr_ok(by_expr, tbl, col, bound, 0)) return NULL;
    ray_t* C = ray_table_get_col(tbl, col);
    int64_t nrows = ray_table_nrows(tbl);
    if (!C || C->type != RAY_SYM || !ray_is_vec(C) || C->len != nrows || nrows < 65536) return NULL;
    struct ray_sym_domain_s* dom = ray_sym_vec_domain(C);
    if (!dom) return NULL;

    /* Every output is one of the decomposable aggregates over C; sort/take
     * clauses may only name output aliases. */
    int64_t alias[DKV_MAX_AGGS]; int kind[DKV_MAX_AGGS]; int n_aggs = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == by_id || kid == where_id || kid == take_id ||
            kid == asc_id || kid == desc_id) continue;
        if (kid == nearest_id) return NULL;
        if (n_aggs >= DKV_MAX_AGGS) return NULL;
        int k = dkv_agg_kind(dict_elems[i + 1], col);
        if (!k) return NULL;
        alias[n_aggs] = kid; kind[n_aggs] = k; n_aggs++;
    }
    if (n_aggs == 0) return NULL;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid != asc_id && kid != desc_id) continue;
        ray_t* v = dict_elems[i + 1];
        int64_t nk = (v && v->type == -RAY_SYM) ? 1 : (v && v->type == RAY_SYM) ? ray_len(v) : -1;
        if (nk < 0) return NULL;
        for (int64_t j = 0; j < nk; j++) {
            int64_t sid = (v->type == -RAY_SYM) ? v->i64 : sym_cell_runtime_id(v, j);
            bool ok = false;
            for (int a = 0; a < n_aggs; a++) if (alias[a] == sid) ok = true;
            if (!ok) return NULL;
        }
    }

    /* 1. the rows grouped by C itself: distinct values with their counts */
    int64_t s_t = ray_sym_intern("__dkv_t", 7), s_cnt = ray_sym_intern("__dkv_cnt", 9);
    int64_t s_s = ray_sym_intern("__dkv_s", 7), s_k = ray_sym_intern("__dkv_k", 7);
    int64_t s_ref = ray_sym_intern("__dkv_ref", 9), s_nn = ray_sym_intern("__dkv_nn", 8);
    int64_t s_lenw = ray_sym_intern("__dkv_lenw", 10);
    ray_t* H = NULL;
    {
        int64_t keys[4]; ray_t* vals[4]; int64_t n = 0;
        keys[n] = from_id; vals[n++] = ray_sym(s_t);
        keys[n] = by_id;   vals[n++] = ray_sym(col);
        keys[n] = s_cnt;   vals[n++] = dkv_call("count", ray_sym(col), NULL);
        if (where_expr) { keys[n] = where_id; ray_retain(where_expr); vals[n++] = where_expr; }
        for (int64_t i = 0; i < n; i++) if (!vals[i]) { for (int64_t j = 0; j < n; j++) if (vals[j]) ray_release(vals[j]); return NULL; }
        ray_t* q = dkv_select(keys, vals, n);
        if (!q) return NULL;
        if (ray_env_push_query_scope() != RAY_OK) { ray_release(q); return NULL; }
        ray_env_set_query_local(s_t, tbl);
        H = ray_eval(q);
        ray_env_pop_scope();
        ray_release(q);
    }
    if (!H || RAY_IS_ERR(H) || H->type != RAY_TABLE || ray_table_ncols(H) != 2) {
        if (H && !RAY_IS_ERR(H)) ray_release(H); else if (H) ray_error_free(H);
        return NULL;
    }
    ray_t* Hc = ray_table_get_col_idx(H, 0);
    ray_t* Hn = ray_table_get_col_idx(H, 1);
    int64_t du = ray_table_nrows(H);
    if (!Hc || !Hn || Hc->type != RAY_SYM || Hn->type != RAY_I64 || du <= 0 ||
        ray_sym_vec_domain(Hc) != dom) { ray_release(H); return NULL; }
    {
        ray_t* Ho = dkv_order_by_position(H, ray_sym_domain_count(dom));
        if (Ho) {
            ray_release(H);
            H = Ho;
            Hc = ray_table_get_col_idx(H, 0);
            Hn = ray_table_get_col_idx(H, 1);
        }
    }

    ray_profile_tick("derived key: distinct values");
    /* 2. the key once per distinct value */
    ray_t* key_dom = derived_key_str_chunks(by_expr, col, Hc, dom, du);
    if (!key_dom || RAY_IS_ERR(key_dom)) { if (key_dom) ray_error_free(key_dom); ray_release(H); return NULL; }

    /* 3. the per-value table: key, count, the value, its non-null count,
     *    its length-weighted count */
    ray_t* S = NULL;
    {
        ray_t* nn   = ray_vec_new(RAY_I64, du);
        ray_t* lenw = ray_vec_new(RAY_I64, du);
        if (!nn || RAY_IS_ERR(nn) || !lenw || RAY_IS_ERR(lenw)) {
            if (nn && !RAY_IS_ERR(nn)) ray_release(nn);
            if (lenw && !RAY_IS_ERR(lenw)) ray_release(lenw);
            ray_release(key_dom); ray_release(H); return NULL;
        }
        nn->len = du; lenw->len = du;
        dkv_len_ctx_t lc = {
            .hc = ray_data(Hc), .attrs = Hc->attrs, .cnt = (const int64_t*)ray_data(Hn),
            .nn = (int64_t*)ray_data(nn), .lenw = (int64_t*)ray_data(lenw), .dom = dom,
        };
        lc.raw_ok = ray_sym_domain_raw_pin(dom, &lc.raw);
        if (dom == ray_sym_runtime_domain()) {
            ray_t** rs = NULL; uint32_t rcnt = 0;
            ray_sym_strings_borrow(&rs, &rcnt);
            lc.rstr = rs; lc.rcnt = rcnt;
        }
        atomic_store_explicit(&lc.late, 0, memory_order_relaxed);
        ray_pool_t* pool = ray_pool_get();
        if (ray_pool_par_dispatch_ok(pool, du, RAY_PARALLEL_THRESHOLD))
            ray_pool_dispatch(pool, dkv_len_fn, &lc, du);
        else
            dkv_len_fn(&lc, 0, 0, du);
        if (atomic_load_explicit(&lc.late, memory_order_relaxed)) {
            /* positions past the file prefix: resolved through the domain on
             * the calling thread */
            for (int64_t i = 0; i < du; i++) {
                if (lc.lenw[i] != INT64_MIN) continue;
                int64_t pos = ray_read_sym(lc.hc, i, RAY_SYM, lc.attrs);
                ray_t* a = ray_sym_domain_str(dom, pos);
                lc.lenw[i] = lc.cnt[i] * (a ? (int64_t)ray_str_len(a) : 0);
            }
        }
        S = ray_table_new(5);
        if (S && !RAY_IS_ERR(S)) S = ray_table_add_col(S, s_k, key_dom);
        if (S && !RAY_IS_ERR(S)) S = ray_table_add_col(S, s_cnt, Hn);
        if (S && !RAY_IS_ERR(S)) S = ray_table_add_col(S, s_ref, Hc);
        if (S && !RAY_IS_ERR(S)) S = ray_table_add_col(S, s_nn, nn);
        if (S && !RAY_IS_ERR(S)) S = ray_table_add_col(S, s_lenw, lenw);
        ray_release(nn); ray_release(lenw);
    }
    ray_release(key_dom);
    ray_release(H);
    if (!S || RAY_IS_ERR(S)) { if (S) ray_error_free(S); return NULL; }

    ray_profile_tick("derived key: per-value table");
    /* 4. the aggregates rewritten over the per-value table, sort/take as
     *    written */
    ray_t* R = NULL;
    {
        int64_t keys[DKV_MAX_AGGS + 8]; ray_t* vals[DKV_MAX_AGGS + 8]; int64_t n = 0;
        bool bad = false;
        for (int64_t i = 0; i + 1 < dict_n && !bad; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            ray_t* v = NULL;
            if (kid == from_id)       v = ray_sym(s_s);
            else if (kid == by_id)    v = ray_sym(s_k);
            else if (kid == where_id) continue;
            else if (kid == take_id || kid == asc_id || kid == desc_id) { v = dict_elems[i + 1]; ray_retain(v); }
            else {
                int k = 0;
                for (int a = 0; a < n_aggs; a++) if (alias[a] == kid) k = kind[a];
                switch (k) {
                case DKV_COUNT:   v = dkv_call("sum", ray_sym(s_cnt), NULL); break;
                case DKV_MIN:     v = dkv_call("min", ray_sym(s_ref), NULL); break;
                case DKV_MAX:     v = dkv_call("max", ray_sym(s_ref), NULL); break;
                case DKV_SUM_LEN: v = dkv_call("sum", ray_sym(s_lenw), NULL); break;
                case DKV_AVG_LEN: v = dkv_call("/", dkv_call("sum", ray_sym(s_lenw), NULL),
                                                    dkv_call("sum", ray_sym(s_nn), NULL)); break;
                default: v = NULL;
                }
            }
            if (!v) { bad = true; break; }
            keys[n] = kid; vals[n++] = v;
        }
        if (bad) { for (int64_t j = 0; j < n; j++) ray_release(vals[j]); ray_release(S); return NULL; }
        ray_t* q = dkv_select(keys, vals, n);
        if (!q) { ray_release(S); return NULL; }
        if (ray_env_push_query_scope() != RAY_OK) { ray_release(q); ray_release(S); return NULL; }
        ray_env_set_query_local(s_s, S);
        R = ray_eval(q);
        ray_env_pop_scope();
        ray_release(q);
    }
    ray_release(S);
    if (!R || RAY_IS_ERR(R)) { if (R) ray_error_free(R); return NULL; }
    if (ray_is_lazy(R)) R = ray_lazy_materialize(R);
    if (!R || RAY_IS_ERR(R) || R->type != RAY_TABLE || ray_table_ncols(R) == 0) {
        if (R && !RAY_IS_ERR(R)) ray_release(R); else if (R) ray_error_free(R);
        return NULL;
    }
    /* The engine emits a post-aggregate expression (the avg quotient)
     * after the plain aggregates: put the outputs back in the order they
     * were written, key first. */
    int64_t rc = ray_table_ncols(R);
    if (rc != n_aggs + 1) { ray_release(R); return NULL; }
    ray_t* O = ray_table_new(rc);
    if (!O || RAY_IS_ERR(O)) { if (O) ray_error_free(O); ray_release(R); return NULL; }
    O = ray_table_add_col(O, ray_table_col_name(R, 0), ray_table_get_col_idx(R, 0));
    for (int a = 0; a < n_aggs && O && !RAY_IS_ERR(O); a++) {
        ray_t* col = ray_table_get_col(R, alias[a]);
        if (!col) { ray_release(O); ray_release(R); return NULL; }
        O = ray_table_add_col(O, alias[a], col);
    }
    ray_release(R);
    if (!O || RAY_IS_ERR(O)) { if (O) ray_error_free(O); return NULL; }
    /* the key column is named as the row path names a computed key */
    int64_t kname = derived_key_name(by_expr);
    for (int64_t c = 1; c < rc; c++)
        if (ray_table_col_name(O, c) == kname) { kname = ray_sym_intern("key", 3); break; }
    ray_table_set_col_name(O, 0, kname);
    /* Unsorted output comes out in the order of the key domain's positions
     * — this execution's first-seen ranks over S, i.e. the vocabulary order
     * of the values — whatever order the grouping over S emitted in (its
     * strategy, and with it the order, depends on the core count).  A sort
     * or take clause fixes the order itself. */
    bool plain = true;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == take_id || kid == asc_id || kid == desc_id) plain = false;
    }
    int64_t orows = ray_table_nrows(O);
    ray_t* okey = ray_table_get_col_idx(O, 0);
    if (plain && orows > 1 && okey && okey->type == RAY_SYM) {
        struct ray_sym_domain_s* kdom = ray_sym_vec_domain(okey);
        int64_t kn = ray_sym_domain_count(kdom);
        ray_t *shdr = NULL, *ihdr = NULL;
        int64_t* slot = (int64_t*)scratch_alloc(&shdr, (size_t)(kn > 0 ? kn : 1) * sizeof(int64_t));
        int64_t* idx  = (int64_t*)scratch_alloc(&ihdr, (size_t)orows * sizeof(int64_t));
        bool ok = slot && idx;
        if (ok) {
            memset(slot, 0xff, (size_t)kn * sizeof(int64_t));
            const void* kd = ray_data(okey);
            for (int64_t i = 0; i < orows && ok; i++) {
                int64_t pos = ray_read_sym(kd, i, RAY_SYM, okey->attrs);
                if (pos < 0 || pos >= kn || slot[pos] >= 0) ok = false;   /* not a grouping by this key */
                else slot[pos] = i;
            }
        }
        if (ok) {
            int64_t m = 0;
            for (int64_t pos = 0; pos < kn; pos++) if (slot[pos] >= 0) idx[m++] = slot[pos];
            ok = m == orows;
        }
        if (ok) {
            ray_t* P = ray_table_new(rc);
            for (int64_t c = 0; c < rc && P && !RAY_IS_ERR(P); c++) {
                ray_t* src = ray_table_get_col_idx(O, c);
                ray_t* g = src ? gather_by_idx(src, idx, orows) : NULL;
                if (!g || RAY_IS_ERR(g)) { if (g) ray_error_free(g); ray_release(P); P = NULL; break; }
                P = ray_table_add_col(P, ray_table_col_name(O, c), g);
                ray_release(g);
            }
            if (P && !RAY_IS_ERR(P)) { ray_release(O); O = P; }
            else if (P) ray_error_free(P);
        }
        scratch_free(shdr); scratch_free(ihdr);
    }
    agg_route_note_key_domain();
    return O;
}

/* `sel` (a rowsel over tbl, or NULL): only the rows it selects decide
 * which distinct symbols are evaluated — the grouping that consumes the key
 * honours the same selection, so a symbol that occurs in no selected row
 * is never evaluated (nor its key interned). */
static ray_t* derived_key_over_sym_domain(ray_t* by_expr, ray_t* tbl, ray_t* sel) {
    if (!by_expr || by_expr->type != RAY_LIST || !tbl) return NULL;
    int64_t ref_syms[2];
    int nref = collect_col_refs(by_expr, tbl, ref_syms, 2, 0);
    if (nref != 1) return NULL;
    int64_t bound[32];
    if (!derived_key_expr_ok(by_expr, tbl, ref_syms[0], bound, 0)) return NULL;
    ray_t* C = ray_table_get_col(tbl, ref_syms[0]);
    int64_t nrows = ray_table_nrows(tbl);
    if (!C || C->type != RAY_SYM || !ray_is_vec(C) || C->len != nrows || nrows < 4096)
        return NULL;
    int64_t sel_rows = nrows;
    if (sel) {
        /* a rowsel block (ray_rowsel_new: an untyped block, its meta first) */
        ray_rowsel_t* sm = ray_rowsel_meta(sel);
        if (!sm || sm->nrows != nrows) return NULL;
        sel_rows = sm->total_pass;
        if (sel_rows < 4096) return NULL;
    }
    struct ray_sym_domain_s* dom = ray_sym_vec_domain(C);
    int64_t dn = dom ? ray_sym_domain_count(dom) : 0;
    /* The slot table is dn ints: refuse a domain far wider than the table
     * (a process-wide symbol domain behind a small in-memory column). */
    if (dn <= 0 || dn > DERIVED_KEY_MAX_DOMAIN || dn > 16 * nrows) return NULL;

    /* Probe the first block before touching anything domain-sized: a
     * distinct share above 3/4 over the first 65536 rows means a
     * near-unique column, for which the per-symbol evaluation cannot win.
     * A 128k-slot open-addressing set on scratch — no dn-sized memory. */
    const void* cd = ray_data(C);
    {
        const int64_t probe_rows = sel_rows < 65536 ? sel_rows : 65536;
        int64_t PROBE_SLOTS = 1024;               /* power of two, load <= 1/2 */
        while (PROBE_SLOTS < 2 * probe_rows) PROBE_SLOTS <<= 1;
        ray_t* set_hdr = NULL;
        int64_t* set = (int64_t*)scratch_alloc(&set_hdr, (size_t)PROBE_SLOTS * sizeof(int64_t));
        if (!set) return NULL;
        memset(set, 0xff, (size_t)PROBE_SLOTS * sizeof(int64_t));
        int64_t seen = 0;
        dk_sel_it_t pit;
        dk_sel_it_init(&pit, sel, nrows);
        for (int64_t r = 0; r < probe_rows; r++) {
            int64_t row = dk_sel_it_next(&pit);
            if (row < 0) break;
            int64_t id = ray_read_sym(cd, row, C->type, C->attrs);
            if (id < 0 || id >= dn) { seen = probe_rows; break; }
            uint64_t h = (((uint64_t)id * 0x9E3779B97F4A7C15ull) >> 32) & (uint64_t)(PROBE_SLOTS - 1);
            while (set[h] >= 0 && set[h] != id) h = (h + 1) & (uint64_t)(PROBE_SLOTS - 1);
            if (set[h] < 0) { set[h] = id; seen++; }
        }
        scratch_free(set_hdr);
        if (seen * 4 > probe_rows * 3) return NULL;
    }

    /* Pass 1: first-seen slot per symbol id; bail on an id outside the
     * domain (a foreign domain) or once du crosses the gate.  The null
     * symbol is id 0, inside the domain: it gets a slot and the expression
     * sees it once, exactly as the row-wise evaluation sees it per row. */
    ray_t* pos_hdr = NULL;
    int32_t* pos = (int32_t*)scratch_alloc(&pos_hdr, (size_t)dn * sizeof(int32_t));
    if (!pos) return NULL;
    memset(pos, 0xff, (size_t)dn * sizeof(int32_t));
    ray_t* dom_vec = ray_sym_vec_new(ray_sym_dict_width(dn), dn);
    if (!dom_vec || RAY_IS_ERR(dom_vec)) { if (dom_vec) ray_error_free(dom_vec); scratch_free(pos_hdr); return NULL; }
    ray_sym_vec_adopt_domain(dom_vec, C);
    int64_t du = 0, du_max = sel_rows / 2;
    if (du_max > INT32_MAX) du_max = INT32_MAX;       /* slots are int32 */
    /* Slots in first-seen row order (over the selected rows): the interned
     * key ids follow the slot order, and with them the order the groups
     * come out in — the same order the row-wise evaluation gives. */
    bool ok = true;
    dk_sel_it_t it;
    dk_sel_it_init(&it, sel, nrows);
    for (;;) {
        int64_t r = dk_sel_it_next(&it);
        if (r < 0) break;
        int64_t id = ray_read_sym(cd, r, C->type, C->attrs);
        if (id < 0 || id >= dn) { ok = false; break; }
        if (pos[id] < 0) {
            if (du >= du_max) { ok = false; break; }
            pos[id] = (int32_t)du;
            write_col_i64(ray_data(dom_vec), du, id, dom_vec->type, dom_vec->attrs);
            du++;
        }
    }
    if (!ok || du == 0) { ray_release(dom_vec); scratch_free(pos_hdr); return NULL; }
    dom_vec->len = du;
    dk_rows_ctx_t rc;
    memset(&rc, 0, sizeof(rc));
    rc.cd = cd; rc.attrs = C->attrs; rc.dn = dn;
    rc.sparse = sel != NULL;
    ray_pool_t* rpool = ray_pool_get();
    bool rows_par = ray_pool_par_dispatch_ok(rpool, nrows, RAY_PARALLEL_THRESHOLD);

    /* Evaluate the expression over the du distinct symbols through the
     * same DAG compiler the row-wise key would take, against a one-column
     * table holding the distinct vector under the referenced name. */
    ray_t* key_dom = derived_key_str_chunks(by_expr, ref_syms[0], dom_vec, dom, du);
    if (key_dom && RAY_IS_ERR(key_dom)) { ray_error_free(key_dom); key_dom = NULL; }
    if (key_dom) {
        ray_release(dom_vec);
    } else {
        ray_t* mini = ray_table_new(0);
        if (mini && !RAY_IS_ERR(mini)) mini = ray_table_add_col(mini, ref_syms[0], dom_vec);
        ray_release(dom_vec);
        if (!mini || RAY_IS_ERR(mini)) { if (mini) ray_error_free(mini); scratch_free(pos_hdr); return NULL; }
        ray_graph_t* g2 = ray_graph_new(mini);
        if (g2) {
            ray_op_t* kop = compile_expr_dag(g2, by_expr);
            if (kop) kop = ray_optimize(g2, kop);
            if (kop) key_dom = ray_execute(g2, kop);
            ray_graph_free(g2);
        }
        ray_release(mini);
        if (key_dom && !RAY_IS_ERR(key_dom) && ray_is_lazy(key_dom)) key_dom = ray_lazy_materialize(key_dom);
        if (!key_dom || RAY_IS_ERR(key_dom)) { if (key_dom) ray_error_free(key_dom); scratch_free(pos_hdr); return NULL; }
        if (!ray_is_vec(key_dom) || key_dom->len != du) { ray_release(key_dom); scratch_free(pos_hdr); return NULL; }
    }

    /* Pass 2: spread by slot — each row takes the key of its symbol's slot,
     * written straight into the result (no index vector, no gather). */
    if (!ray_is_vec(key_dom) || key_dom->len != du) { ray_release(key_dom); scratch_free(pos_hdr); return NULL; }
    ray_t* spread = NULL;
    if (key_dom->type == RAY_SYM && (key_dom->attrs & RAY_SYM_W_MASK) == RAY_SYM_W64) {
        spread = ray_sym_vec_new(RAY_SYM_W64, nrows);
        if (!spread || RAY_IS_ERR(spread)) { if (spread) ray_error_free(spread); ray_release(key_dom); scratch_free(pos_hdr); return NULL; }
        spread->len = nrows;
        ray_sym_vec_adopt_domain(spread, key_dom);
        rc.pos = pos; rc.key = (const int64_t*)ray_data(key_dom); rc.out = (int64_t*)ray_data(spread);
        if (rows_par) ray_pool_dispatch(rpool, dk_spread_fn, &rc, nrows);
        else          dk_spread_fn(&rc, 0, 0, nrows);
        scratch_free(pos_hdr);
        ray_release(key_dom);
    } else {
        /* Any other key vector (the one-shot SYM evaluation's width, or a
         * non-SYM result): index by slot and gather. */
        ray_t* ids = ray_vec_new(RAY_I64, nrows);
        if (!ids || RAY_IS_ERR(ids)) { if (ids) ray_error_free(ids); ray_release(key_dom); scratch_free(pos_hdr); return NULL; }
        ids->len = nrows;
        rc.pos = pos; rc.key = NULL; rc.out = (int64_t*)ray_data(ids);
        if (rows_par) ray_pool_dispatch(rpool, dk_spread_fn, &rc, nrows);
        else          dk_spread_fn(&rc, 0, 0, nrows);
        scratch_free(pos_hdr);
        spread = ray_at_fn(key_dom, ids);
        ray_release(ids);
        ray_release(key_dom);
        if (spread && !RAY_IS_ERR(spread) && ray_is_lazy(spread)) spread = ray_lazy_materialize(spread);
        if (!spread || RAY_IS_ERR(spread)) { if (spread) ray_error_free(spread); return NULL; }
        if (!ray_is_vec(spread) || spread->len != nrows) { ray_release(spread); return NULL; }
    }
    agg_route_note_key_domain();
    return spread;
}

/* Bind a single column-id to a slice of its column under `idx_list`.
 * Helper used inside the per-group hot loop (slices the table's column
 * via ray_at_fn, hands the slice to env_bind_local which retains, then
 * drops our ref).  Returns 0 on success, error ray_t* on failure. */
static ray_t* bind_col_slice(int64_t sym, ray_t* col, ray_t* idx_list) {
    /* For typed-vec col + RAY_I64 idx vec, gather directly so the bound
     * slice is the same typed vector as the source — `(at v idx)` would
     * box every element into a RAY_LIST of atoms, which breaks any
     * per-group expression that expects a numeric vec (`desc`, `take`,
     * `asc`, etc.).  Fall back to ray_at_fn for LIST inputs and other
     * shapes the gather kernel doesn't cover. */
    ray_t* slice = NULL;
    if (col && ray_is_vec(col) && idx_list &&
        idx_list->type == RAY_I64 && ray_is_vec(idx_list)) {
        const int64_t* idx_data = (const int64_t*)ray_data(idx_list);
        slice = gather_by_idx(col, (int64_t*)idx_data, ray_len(idx_list));
    }
    if (!slice) slice = ray_at_fn(col, idx_list);
    if (!slice || RAY_IS_ERR(slice)) {
        return slice ? slice : ray_error("oom", NULL);
    }
    ray_env_set_query_local(sym, slice);
    ray_release(slice);
    return NULL;
}

/* Convert a partly-filled typed vec (indices 0..fill-1 valid) back into
 * a LIST of n_groups owned atom refs (only first `fill` initialized).
 * Used by the per-group eval fallback when the probe-typed-direct path
 * detects a mid-loop type mismatch and has to demote to a list. */
static ray_t* typed_vec_to_list(ray_t* tv, int64_t fill, int64_t n_groups) {
    ray_t* list_col = ray_alloc(n_groups * sizeof(ray_t*));
    if (!list_col) return ray_error("oom", NULL);
    list_col->type = RAY_LIST;
    list_col->len = 0;
    ray_t** out = (ray_t**)ray_data(list_col);
    for (int64_t k = 0; k < fill; k++) {
        int allocated = 0;
        ray_t* atom = collection_elem(tv, k, &allocated);
        if (!allocated && atom) ray_retain(atom);
        out[k] = atom;
        list_col->len = k + 1;
    }
    return list_col;
}

/* Inner per-group eval body shared by the LIST-`groups` and `idx_buf`
 * variants.  Pre-collects unique column refs, pushes ONE local scope
 * around the whole loop, and probes the first cell:
 *   - scalar atom of a typed-vec primitive → write directly into a
 *     pre-allocated typed vec (no list intermediate, no post-collapse);
 *   - otherwise → collect into a LIST column.
 * If the typed-direct path hits a mid-loop type mismatch, it demotes
 * to a LIST cleanly (one-time cost).  `feeder` produces the per-group
 * idx_list ray_t* (caller controls its lifetime / reuse); the closure
 * over `feeder_state` lets the buf variant reuse a single I64 wrapper.
 *
 * Returns either a typed vec (homogeneous scalars) or a LIST col. */
typedef ray_t* (*idx_feeder_fn)(int64_t gi, void* state);

static ray_t* nonagg_eval_per_group_core(ray_t* expr, ray_t* tbl,
                                         idx_feeder_fn feeder, void* fstate,
                                         int64_t n_groups) {
    /* Exact-size carve: collect_col_refs dedups against real table columns,
     * so ray_table_ncols(tbl) is a hard upper bound — no silent truncation
     * past the former [16] cap. */
    int64_t ncols_max = ray_table_ncols(tbl);
    if (ncols_max < 1) ncols_max = 1;
    ray_t* refs_hdr = NULL;
    int64_t* col_syms = (int64_t*)scratch_alloc(&refs_hdr,
            (size_t)ncols_max * (sizeof(int64_t) + sizeof(ray_t*)));
    if (!col_syms) return ray_error("oom", NULL);
    int n_cols = collect_col_refs(expr, tbl, col_syms, (int)ncols_max, 0);
    ray_t** cols = (ray_t**)(col_syms + ncols_max);
    for (int i = 0; i < n_cols; i++)
        cols[i] = ray_table_get_col(tbl, col_syms[i]);

    if (ray_env_push_query_scope() != RAY_OK) { scratch_free(refs_hdr); return ray_error("oom", NULL); }

    /* B3 Part 2: publish `tbl` as the active query table so a literal
     * column-name symbol inside `expr` resolves to its column during the
     * tree-walk eval below (mirrors the DAG path's bind_all_columns).  Save
     * the previous value and restore it before EVERY exit from this scope
     * (including error returns), exactly as the bind_all_columns sites do. */
    ray_t* _aqt = g_active_query_table;
    g_active_query_table = tbl;

    ray_t* result = NULL;       /* typed vec OR list col */
    int direct_typed = 0;       /* non-zero → result is a typed vec */
    int8_t typed_t = 0;         /* atom type sentinel for the typed path */
    if (n_groups == 0) {
        /* No group to walk, but the column must still exist.  Probe the
         * expression once over an EMPTY slice of every referenced column so
         * the result keeps the type a non-empty result would have had; an
         * expression that cannot evaluate on zero rows yields a LIST column,
         * the same shape the per-group path falls back to. */
        ray_t* empty_idx = ray_vec_new(RAY_I64, 0);
        int8_t vt = 0;
        if (empty_idx && !RAY_IS_ERR(empty_idx)) {
            empty_idx->len = 0;
            int bound = 1;
            for (int i = 0; i < n_cols && bound; i++) {
                ray_t* err = bind_col_slice(col_syms[i], cols[i], empty_idx);
                if (err) { ray_error_free(err); bound = 0; }
            }
            if (bound) {
                ray_t* cell = ray_eval(expr);
                if (cell && !RAY_IS_ERR(cell) && ray_is_lazy(cell)) cell = ray_lazy_materialize(cell);
                if (cell && !RAY_IS_ERR(cell)) {
                    int8_t t = cell->type;
                    if (t < 0 && t != -RAY_SYM && t != -RAY_STR) vt = (int8_t)(-t);
                    ray_release(cell);
                } else if (cell) ray_error_free(cell);
            }
            ray_release(empty_idx);
        } else if (empty_idx) ray_error_free(empty_idx);
        result = vt ? ray_vec_new(vt, 0) : ray_list_new(0);
        if (result && !RAY_IS_ERR(result)) result->len = 0;
        g_active_query_table = _aqt;
        ray_env_pop_scope();
        scratch_free(refs_hdr);
        return result ? result : ray_error("oom", NULL);
    }

    for (int64_t gi = 0; gi < n_groups; gi++) {
        ray_t* idx_list = feeder(gi, fstate);
        if (!idx_list) {
            g_active_query_table = _aqt;
            ray_env_pop_scope();
            if (result) ray_release(result);
            scratch_free(refs_hdr);
            return ray_error("oom", NULL);
        }
        for (int i = 0; i < n_cols; i++) {
            ray_t* err = bind_col_slice(col_syms[i], cols[i], idx_list);
            if (err) {
                g_active_query_table = _aqt;
                ray_env_pop_scope();
                if (result) ray_release(result);
                scratch_free(refs_hdr);
                return err;
            }
        }
        ray_t* cell = ray_eval(expr);
        if (!cell || RAY_IS_ERR(cell)) {
            g_active_query_table = _aqt;
            ray_env_pop_scope();
            if (result) ray_release(result);
            scratch_free(refs_hdr);
            return cell ? cell : ray_error("domain", "select by: per-group expression evaluation failed");
        }
        /* Materialise lazy cells before storing.  Per-group projection
         * eval can return a RAY_LAZY (e.g. (reverse v) returns a fresh
         * lazy chain).  Lazy values stored as-is in a LIST get their
         * graph stolen by the first ray_lazy_materialize via fmt_obj,
         * leaving subsequent reads with a half-dead lazy whose execute
         * fails with "nyi".  Eager materialisation here keeps each cell
         * concrete and re-readable. */
        if (ray_is_lazy(cell)) {
            cell = ray_lazy_materialize(cell);
            if (!cell || RAY_IS_ERR(cell)) {
                g_active_query_table = _aqt;
                ray_env_pop_scope();
                if (result) ray_release(result);
                scratch_free(refs_hdr);
                return cell ? cell : ray_error("domain", "select by: failed to materialize per-group cell");
            }
        }

        if (gi == 0) {
            int8_t t = cell->type;
            int collapsable = (t < 0);
            if (collapsable && t == -RAY_STR) {
                /* STR has no typed-store arm: collect through the string
                 * pool, appending one cell per row (len == rows so far). */
                result = ray_vec_new(RAY_STR, n_groups);
                if (!result || RAY_IS_ERR(result)) {
                    g_active_query_table = _aqt;
                    ray_env_pop_scope(); ray_release(cell);
                    scratch_free(refs_hdr);
                    return result ? result : ray_error("oom", NULL);
                }
                const char* sp = ray_str_ptr(cell);
                result = ray_str_vec_append(result, sp ? sp : "", sp ? ray_str_len(cell) : 0);
                if (!result || RAY_IS_ERR(result)) {
                    g_active_query_table = _aqt;
                    ray_env_pop_scope(); ray_release(cell);
                    scratch_free(refs_hdr);
                    return result ? result : ray_error("oom", NULL);
                }
                direct_typed = 1; typed_t = t;
                ray_release(cell);
            } else if (collapsable) {
                int8_t vt = (int8_t)(-t);
                result = ray_vec_new(vt, n_groups);
                if (!result || RAY_IS_ERR(result)) {
                    g_active_query_table = _aqt;
                    ray_env_pop_scope(); ray_release(cell);
                    scratch_free(refs_hdr);
                    return result ? result : ray_error("oom", NULL);
                }
                result->len = n_groups;
                if (store_typed_elem(result, 0, cell) == 0) {
                    direct_typed = 1; typed_t = t;
                    ray_release(cell);
                } else {
                    /* type unsupported by store_typed_elem → fall to list */
                    ray_release(result); result = NULL;
                    collapsable = 0;
                }
            }
            if (!collapsable) {
                result = ray_alloc(n_groups * sizeof(ray_t*));
                if (!result) {
                    g_active_query_table = _aqt;
                    ray_env_pop_scope(); ray_release(cell);
                    scratch_free(refs_hdr);
                    return ray_error("oom", NULL);
                }
                result->type = RAY_LIST;
                result->len = 0;
                ((ray_t**)ray_data(result))[0] = cell;
                result->len = 1;
            }
            continue;
        }

        if (direct_typed) {
            int stored = 0;
            if (cell->type == typed_t) {
                if (typed_t == -RAY_STR) {
                    const char* sp = ray_str_ptr(cell);
                    ray_t* nv = ray_str_vec_append(result, sp ? sp : "", sp ? ray_str_len(cell) : 0);
                    if (!nv || RAY_IS_ERR(nv)) {
                        g_active_query_table = _aqt;
                        ray_env_pop_scope(); ray_release(cell);
                        scratch_free(refs_hdr);
                        return nv ? nv : ray_error("oom", NULL);
                    }
                    result = nv; stored = 1;
                } else {
                    stored = (store_typed_elem(result, gi, cell) == 0);
                }
            }
            if (stored) {
                ray_release(cell);
            } else {
                /* Demote: convert typed vec [0..gi-1] to list, append cell, continue as list. */
                ray_t* list_col = typed_vec_to_list(result, gi, n_groups);
                ray_release(result);
                if (RAY_IS_ERR(list_col)) {
                    g_active_query_table = _aqt;
                    ray_env_pop_scope(); ray_release(cell);
                    scratch_free(refs_hdr);
                    return list_col;
                }
                result = list_col;
                ((ray_t**)ray_data(result))[gi] = cell;  /* takes ownership */
                result->len = gi + 1;
                direct_typed = 0;
            }
        } else {
            ((ray_t**)ray_data(result))[gi] = cell;  /* takes ownership */
            result->len = gi + 1;
        }
    }

    g_active_query_table = _aqt;
    ray_env_pop_scope();
    scratch_free(refs_hdr);
    return result;
}

/* idx_feeder for the eval-fallback's LIST `groups` layout. */
typedef struct { ray_t** items; } groups_state_t;
static ray_t* groups_idx_feed(int64_t gi, void* st) {
    groups_state_t* s = (groups_state_t*)st;
    return s->items[gi * 2 + 1];
}

static ray_t* nonagg_eval_per_group(ray_t* expr, ray_t* tbl,
                                    ray_t* groups, int64_t n_groups) {
    groups_state_t st = { .items = (ray_t**)ray_data(groups) };
    return nonagg_eval_per_group_core(expr, tbl, groups_idx_feed, &st, n_groups);
}

/* idx_feeder for the DAG fast-path's idx_buf+offsets+grp_cnt layout.
 * Reuses a single RAY_I64 wrapper across all groups: just retargets the
 * data pointer-equivalent by memcpy'ing into its data area and adjusting
 * `len`.  Saves n_groups vec allocs/frees. */
typedef struct {
    const int64_t* idx_buf;
    const int64_t* offsets;
    const int64_t* grp_cnt;
    ray_t*         scratch;     /* RAY_I64 vec, sized to max grp_cnt */
} buf_state_t;

static ray_t* buf_idx_feed(int64_t gi, void* st) {
    buf_state_t* s = (buf_state_t*)st;
    int64_t cnt = s->grp_cnt[gi];
    s->scratch->len = cnt;
    if (cnt > 0) {
        memcpy(ray_data(s->scratch), &s->idx_buf[s->offsets[gi]],
               (size_t)cnt * sizeof(int64_t));
    }
    return s->scratch;
}

static ray_t* nonagg_eval_per_group_buf(ray_t* expr, ray_t* tbl,
                                        const int64_t* idx_buf,
                                        const int64_t* offsets,
                                        const int64_t* grp_cnt,
                                        int64_t n_groups) {
    int64_t max_cnt = 0;
    for (int64_t gi = 0; gi < n_groups; gi++)
        if (grp_cnt[gi] > max_cnt) max_cnt = grp_cnt[gi];
    ray_t* scratch = ray_vec_new(RAY_I64, max_cnt > 0 ? max_cnt : 1);
    if (!scratch || RAY_IS_ERR(scratch))
        return scratch ? scratch : ray_error("oom", NULL);
    buf_state_t st = { idx_buf, offsets, grp_cnt, scratch };
    ray_t* res = nonagg_eval_per_group_core(expr, tbl, buf_idx_feed, &st, n_groups);
    ray_release(scratch);
    return res;
}

/* The value a literal column-name symbol stands for while a query is
 * active (see the literal rule in eval.c): the whole column, or — while a
 * per-row evaluation is running — that column's cell in the current row,
 * so `'price` reads what `price` reads wherever the expression is
 * evaluated.  Owned ref; NULL when no query is active or the name is not a
 * column of it. */
ray_t* ray_active_query_literal(int64_t sym) {
    ray_t* qt = g_active_query_table;
    if (!qt || qt->type != RAY_TABLE) return NULL;
    ray_t* col = ray_table_get_col(qt, sym);
    if (!col) return NULL;
    if (g_active_query_row < 0 || qt != g_active_query_row_tbl ||
        g_active_query_row >= ray_len(col) ||
        ray_env_query_scope_above(g_active_query_row_depth)) {
        ray_retain(col); return col;
    }
    int allocated = 0;
    ray_t* cell = collection_elem(col, g_active_query_row, &allocated);
    if (!cell || RAY_IS_ERR(cell)) return cell;
    if (!allocated) ray_retain(cell);
    return cell;
}

static ray_t* eval_expr_per_row(ray_t* expr, ray_t* tbl, int64_t nrows) {
    /* Exact-size carve, mirrors nonagg_eval_per_group_core: ncols(tbl) is
     * a hard upper bound for collect_col_refs's deduplicated table-column
     * output — no silent truncation past the former [16] cap. */
    int64_t ncols_max = ray_table_ncols(tbl);
    if (ncols_max < 1) ncols_max = 1;
    ray_t* refs_hdr = NULL;
    int64_t* col_syms = (int64_t*)scratch_alloc(&refs_hdr,
            (size_t)ncols_max * (sizeof(int64_t) + sizeof(ray_t*)));
    if (!col_syms) return ray_error("oom", NULL);
    int n_cols = collect_col_refs(expr, tbl, col_syms, (int)ncols_max, 0);
    ray_t** cols = (ray_t**)(col_syms + ncols_max);
    for (int i = 0; i < n_cols; i++)
        cols[i] = ray_table_get_col(tbl, col_syms[i]);

    if (ray_env_push_query_scope() != RAY_OK) { scratch_free(refs_hdr); return ray_error("oom", NULL); }

    /* B3 Part 2: publish `tbl` as the active query table so a literal
     * column-name symbol inside `expr` resolves to its column during the
     * tree-walk eval below (mirrors the DAG path's bind_all_columns).  Save
     * the previous value and restore it before EVERY exit from this scope
     * (including error returns), exactly as the bind_all_columns sites do. */
    ray_t* _aqt = g_active_query_table;
    int64_t _aqr = g_active_query_row;
    ray_t*  _aqrt = g_active_query_row_tbl;
    int32_t _aqrd = g_active_query_row_depth;
    g_active_query_table     = tbl;
    g_active_query_row_tbl   = tbl;
    g_active_query_row_depth = ray_env_scope_depth();   /* just above our own frame */

    ray_t* result = NULL;
    int direct_typed = 0;
    int8_t typed_t = 0;

    for (int64_t row = 0; row < nrows; row++) {
        for (int i = 0; i < n_cols; i++) {
            int allocated = 0;
            ray_t* arg = collection_elem(cols[i], row, &allocated);
            if (!arg || RAY_IS_ERR(arg)) {
                g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                ray_env_pop_scope();
                if (result) ray_release(result);
                scratch_free(refs_hdr);
                return arg ? arg : ray_error("domain", "select: failed to read column cell for per-row eval");
            }
            ray_env_set_query_local(col_syms[i], arg);
            if (allocated) ray_release(arg);
        }

        g_active_query_row = row;
        ray_t* cell = ray_eval(expr);
        if (!cell || RAY_IS_ERR(cell)) {
            g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
            ray_env_pop_scope();
            if (result) ray_release(result);
            scratch_free(refs_hdr);
            return cell ? cell : ray_error("domain", "select: per-row expression evaluation failed");
        }

        if (row == 0) {
            int8_t t = cell->type;
            int collapsable = (t < 0);
            if (collapsable && t == -RAY_STR) {
                /* STR has no typed-store arm: collect through the string
                 * pool, appending one cell per row (len == rows so far). */
                result = ray_vec_new(RAY_STR, nrows);
                if (!result || RAY_IS_ERR(result)) {
                    g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                    ray_env_pop_scope();
                    ray_release(cell);
                    scratch_free(refs_hdr);
                    return result ? result : ray_error("oom", NULL);
                }
                const char* sp = ray_str_ptr(cell);
                result = ray_str_vec_append(result, sp ? sp : "", sp ? ray_str_len(cell) : 0);
                if (!result || RAY_IS_ERR(result)) {
                    g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                    ray_env_pop_scope();
                    ray_release(cell);
                    scratch_free(refs_hdr);
                    return result ? result : ray_error("oom", NULL);
                }
                direct_typed = 1; typed_t = t;
                ray_release(cell);
            } else if (collapsable) {
                result = ray_vec_new((int8_t)-t, nrows);
                if (!result || RAY_IS_ERR(result)) {
                    g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                    ray_env_pop_scope();
                    ray_release(cell);
                    scratch_free(refs_hdr);
                    return result ? result : ray_error("oom", NULL);
                }
                result->len = nrows;
                if (store_typed_elem(result, 0, cell) == 0) {
                    direct_typed = 1;
                    typed_t = t;
                    ray_release(cell);
                } else {
                    ray_release(result);
                    result = NULL;
                    collapsable = 0;
                }
            }
            if (!collapsable) {
                result = ray_alloc(nrows * sizeof(ray_t*));
                if (!result) {
                    g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                    ray_env_pop_scope();
                    ray_release(cell);
                    scratch_free(refs_hdr);
                    return ray_error("oom", NULL);
                }
                result->type = RAY_LIST;
                result->len = 1;
                ((ray_t**)ray_data(result))[0] = cell;
            }
            continue;
        }

        if (direct_typed) {
            int stored = 0;
            if (cell->type == typed_t) {
                if (typed_t == -RAY_STR) {
                    const char* sp = ray_str_ptr(cell);
                    ray_t* nv = ray_str_vec_append(result, sp ? sp : "", sp ? ray_str_len(cell) : 0);
                    if (!nv || RAY_IS_ERR(nv)) {
                        g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                        ray_env_pop_scope();
                        ray_release(cell);
                        scratch_free(refs_hdr);
                        return nv ? nv : ray_error("oom", NULL);
                    }
                    result = nv; stored = 1;
                } else {
                    stored = (store_typed_elem(result, row, cell) == 0);
                }
            }
            if (stored) {
                ray_release(cell);
            } else {
                ray_t* list_col = typed_vec_to_list(result, row, nrows);
                ray_release(result);
                if (RAY_IS_ERR(list_col)) {
                    g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
                    ray_env_pop_scope();
                    ray_release(cell);
                    scratch_free(refs_hdr);
                    return list_col;
                }
                result = list_col;
                ((ray_t**)ray_data(result))[row] = cell;
                result->len = row + 1;
                direct_typed = 0;
            }
        } else {
            ((ray_t**)ray_data(result))[row] = cell;
            result->len = row + 1;
        }
    }

    g_active_query_table = _aqt; g_active_query_row = _aqr; g_active_query_row_tbl = _aqrt; g_active_query_row_depth = _aqrd;
    ray_env_pop_scope();
    scratch_free(refs_hdr);
    if (!result) {
        result = ray_alloc(0);
        if (!result) return ray_error("oom", NULL);
        result->type = RAY_LIST;
        result->len = 0;
    }
    return result;
}

/* Evaluate a whole-column projection (is_whole_column_projection) ONCE against
 * the full table: every column is bound as a complete vector (via
 * bind_all_columns, exactly as the DAG path does) and the expression is
 * evaluated a single time, so `distinct`/`asc`/`desc`/`reverse` receive the
 * whole column instead of one scalar cell.  The returned vector (caller owns)
 * defines its output column's length, which may differ from the table's row
 * count — the caller enforces cross-column length agreement.  Mirrors
 * eval_expr_per_row's scope save/restore, including on every error exit. */
static ray_t* eval_expr_whole_column(ray_t* expr, ray_t* tbl) {
    if (ray_env_push_query_scope() != RAY_OK) return ray_error("oom", NULL);
    ray_t* _aqt = bind_all_columns(tbl);
    /* Whole columns here, even when a per-row evaluation of an outer select
     * is in progress (a nested select inside one of its outputs). */
    int64_t _aqr = g_active_query_row;
    g_active_query_row = -1;
    ray_t* result = ray_eval(expr);
    /* distinct/asc/desc/reverse return a lazy DAG chain (RAY_LAZY) — force it
     * to a concrete vector while the source columns it captured are still
     * bound in this scope. */
    if (result && !RAY_IS_ERR(result))
        result = ray_lazy_materialize(result);
    g_active_query_table = _aqt;
    g_active_query_row = _aqr;
    ray_env_pop_scope();
    if (!result)
        return ray_error("domain", "select: whole-column expression evaluation failed");
    return result;
}

/* Zero groups still need the aggregate's column.  Run the aggregate once over
 * an empty vector of the source's type so the empty column keeps the type a
 * non-empty result would have had; if that probe cannot run, an empty LIST
 * column stands in, the shape the per-group path itself falls back to. */
static ray_t* empty_agg_column(ray_t* fn_name, ray_t* src) {
    ray_t* out = NULL;
    ray_t* fn_obj = fn_name ? ray_env_get(fn_name->i64) : NULL;
    if (fn_obj && fn_obj->type == RAY_UNARY && src && ray_is_vec(src) && src->type != RAY_LIST) {
        ray_t* empty = ray_vec_new(src->type, 0);
        if (empty && !RAY_IS_ERR(empty)) {
            empty->len = 0;
            ray_t* v = ((ray_unary_fn)(uintptr_t)fn_obj->i64)(empty);
            if (v && !RAY_IS_ERR(v) && ray_is_lazy(v)) v = ray_lazy_materialize(v);
            if (v && !RAY_IS_ERR(v)) {
                int8_t t = v->type;
                if (t < 0 && t != -RAY_SYM && t != -RAY_STR) out = ray_vec_new((int8_t)(-t), 0);
                ray_release(v);
            } else if (v) ray_error_free(v);
            ray_release(empty);
        } else if (empty) ray_error_free(empty);
    }
    if (!out || RAY_IS_ERR(out)) { if (out) ray_error_free(out); out = ray_list_new(0); }
    if (out && !RAY_IS_ERR(out)) out->len = 0;
    return out;
}

/* Streaming-style per-group AGG body, DAG flavor.  For an expression
 * like `(med v)` (head is RAY_FN_AGGR + RAY_UNARY, second elem is a
 * column ref or full-table-eval-able sub-expression), slice src per
 * group via ray_at_fn, call the unary fn directly, store the scalar
 * result into a pre-sized typed vec.  Mirrors the eval-fallback's AGG
 * branch (`query.c:~1955`) but with the idx_buf+offsets+grp_cnt
 * layout the DAG path produces. */
/* Infer an empty grouped column from the aggregate's empty-input result.
 * Preserve source domains/widths when the result has the source element type. */
static ray_t* empty_unary_group_result(ray_unary_fn fn, ray_t* source) {
    if (!source) return ray_error("domain", "aggregation source missing");
    ray_t* input = ray_group_gather(source, NULL, 0);
    if (!input || RAY_IS_ERR(input)) return input ? input : ray_error("oom", NULL);
    ray_t* value = fn(input);
    if (value && !RAY_IS_ERR(value) && ray_is_lazy(value)) value = ray_lazy_materialize(value);
    if (!value || RAY_IS_ERR(value)) {
        ray_release(input);
        if (!value) return ray_error("oom", NULL);
        /* No group invokes this aggregate. An empty-input domain/type error
         * only means its result type cannot be inferred; preserve the former
         * empty LIST result. Resource failures still abort the query. */
        const char* code = ray_err_code(value);
        if (strcmp(code, "domain") && strcmp(code, "type")) return value;
        ray_release(value);
        ray_t* out = ray_list_new(0);
        return out ? out : ray_error("oom", NULL);
    }
    ray_t* out;
    if (ray_is_atom(value) && -value->type == input->type) {
        out = input; input = NULL;
    } else out = ray_is_atom(value) ? ray_vec_new(-value->type, 0) : ray_list_new(0);
    ray_release(input); ray_release(value);
    return out ? out : ray_error("oom", NULL);
}

static ray_t* aggr_unary_per_group_buf(ray_t* expr, ray_t* tbl,
                                       const int64_t* idx_buf,
                                       const int64_t* offsets,
                                       const int64_t* grp_cnt,
                                       int64_t n_groups) {
    ray_t** elems = (ray_t**)ray_data(expr);
    ray_t* fn_name = elems[0];
    ray_t* col_expr = elems[1];

    ray_t* fn_obj = ray_env_get(fn_name->i64);
    if (!fn_obj || fn_obj->type != RAY_UNARY)
        return ray_error("type", "select by: aggregate must be a unary function, got %s", fn_obj ? ray_type_name(fn_obj->type) : "null");
    ray_unary_fn uf = (ray_unary_fn)(uintptr_t)fn_obj->i64;

    /* Resolve the source column: either a direct column ref (no copy)
     * or a full-table eval of the sub-expression. */
    ray_t* src = NULL;
    if (col_expr->type == -RAY_SYM && !(col_expr->attrs & ATTR_QUOTED)) {
        src = ray_table_get_col(tbl, col_expr->i64);
        if (src) ray_retain(src);
    }
    if (!src) {
        /* Bind table cols and eval — same pattern as the existing path. */
        if (ray_env_push_query_scope() != RAY_OK) return ray_error("oom", NULL);
        ray_t* _aqt = bind_all_columns(tbl);
        src = ray_eval(col_expr);
        g_active_query_table = _aqt;
        ray_env_pop_scope();
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("domain", "select by: failed to evaluate aggregation source");
        /* col_expr can evaluate to a LAZY handle (e.g. (asc col)).  We own
         * it, and the per-group ray_at_fn below materializes lazy args at
         * entry — CONSUMING the handle while our src still points at it
         * (the next iteration reads freed memory, the final release double
         * frees).  Materialize once here instead. */
        if (ray_is_lazy(src)) {
            src = ray_lazy_materialize(src);
            if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("domain", "select by: failed to materialize aggregation source");
        }
    }

    if (n_groups == 0) {
        ray_t* out = empty_unary_group_result(uf, src);
        ray_release(src); return out;
    }

    /* Reusable I64 idx wrapper. */
    int64_t max_cnt = 0;
    for (int64_t gi = 0; gi < n_groups; gi++)
        if (grp_cnt[gi] > max_cnt) max_cnt = grp_cnt[gi];
    ray_t* idx_vec = ray_vec_new(RAY_I64, max_cnt > 0 ? max_cnt : 1);
    if (!idx_vec || RAY_IS_ERR(idx_vec)) {
        ray_release(src);
        return idx_vec ? idx_vec : ray_error("oom", NULL);
    }

    ray_t* agg_vec = NULL;
    int8_t agg_atom_t = 0;

    for (int64_t gi = 0; gi < n_groups; gi++) {
        idx_vec->len = grp_cnt[gi];
        if (grp_cnt[gi] > 0) {
            memcpy(ray_data(idx_vec), &idx_buf[offsets[gi]],
                   (size_t)grp_cnt[gi] * sizeof(int64_t));
        }
        ray_t* subset = ray_at_fn(src, idx_vec);
        if (!subset || RAY_IS_ERR(subset)) continue;
        ray_t* agg_val = uf(subset);
        ray_release(subset);
        if (agg_val && !RAY_IS_ERR(agg_val) && ray_is_lazy(agg_val))
            agg_val = ray_lazy_materialize(agg_val);
        if (!agg_val || RAY_IS_ERR(agg_val)) continue;

        if (!agg_vec) {
            agg_atom_t = agg_val->type;
            int8_t vt = (int8_t)(-agg_atom_t);
            agg_vec = ray_vec_new(vt, n_groups);
            if (!agg_vec || RAY_IS_ERR(agg_vec)) {
                ray_release(agg_val); ray_release(idx_vec); ray_release(src);
                return agg_vec ? agg_vec : ray_error("oom", NULL);
            }
            agg_vec->len = n_groups;
        }
        if (agg_val->type != agg_atom_t || store_typed_elem(agg_vec, gi, agg_val) != 0) {
            /* Fallback: shouldn't happen for well-behaved aggregators; if it
             * does, demote to a list so we don't return a partly-typed vec.
             * Convert what we have so far to a list, then reattempt as a
             * generic non-streaming eval. */
            ray_release(agg_val);
            ray_release(idx_vec); ray_release(src);
            ray_release(agg_vec);
            return nonagg_eval_per_group_buf(expr, tbl, idx_buf, offsets, grp_cnt, n_groups);
        }
        ray_release(agg_val);
    }

    ray_release(idx_vec);
    if (!agg_vec) {
        /* No group produced a value.  With zero groups the column must still
         * carry the type a value would have had; with groups that were all
         * empty keep the I64 default sized to n_groups. */
        if (n_groups == 0) agg_vec = empty_agg_column(fn_name, src);
        else {
            agg_vec = ray_vec_new(RAY_I64, n_groups);
            if (agg_vec && !RAY_IS_ERR(agg_vec)) agg_vec->len = n_groups;
        }
    }
    ray_release(src);
    return agg_vec;
}

/* Recognise `(med col)`.  Used to gate the fast median per-group path
 * below — `med` is RAY_FN_AGGR + RAY_UNARY so it normally routes
 * through aggr_unary_per_group_buf, which allocates one ray vector
 * per group via ray_at_fn and then another scratch inside ray_med_fn.
 * For 10k+ groups that's 20k+ allocs; the bucket-scatter path skips it. */
static int is_med_call(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    if (ray_len(expr) != 2) return 0;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (!elems[0] || elems[0]->type != -RAY_SYM) return 0;
    ray_t* nm = ray_sym_str(elems[0]->i64);
    if (!nm) return 0;
    return ray_str_len(nm) == 3 && memcmp(ray_str_ptr(nm), "med", 3) == 0;
}

/* Fast median per group: read values straight out of the source column
 * via idx_buf+offsets+grp_cnt into a reusable double scratch buffer
 * sized at max group, then ray_median_dbl_inplace.  Returns the f64
 * median vec of length n_groups, or NULL on type miss (caller falls
 * back to the generic aggr_unary_per_group_buf path). */
/* Thin wrapper around the parallel ray_median_per_group_buf kernel
 * (src/ops/group.c).  Resolves the source column from `(med col_expr)`,
 * then delegates to the kernel which runs one ray_pool_dispatch_n task
 * per group — gathers values into a shared scratch buffer and runs
     * ray_median_dbl_inplace in parallel.  See the kernel header comment
     * for the design: it follows the exact holistic-aggregate shape
     * without paying a per-group vector-grow cost. */
static ray_t* aggr_med_per_group_buf(ray_t* expr, ray_t* tbl,
                                     const int64_t* idx_buf,
                                     const int64_t* offsets,
                                     const int64_t* grp_cnt,
                                     int64_t n_groups) {
    ray_t** elems = (ray_t**)ray_data(expr);
    ray_t* col_expr = elems[1];

    ray_t* src = NULL;
    if (col_expr->type == -RAY_SYM && !(col_expr->attrs & ATTR_QUOTED)) {
        src = ray_table_get_col(tbl, col_expr->i64);
        if (src) ray_retain(src);
    }
    if (!src) {
        if (ray_env_push_query_scope() != RAY_OK) return ray_error("oom", NULL);
        ray_t* _aqt = bind_all_columns(tbl);
        src = ray_eval(col_expr);
        g_active_query_table = _aqt;
        ray_env_pop_scope();
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("domain", "select by: failed to evaluate median source column");
    }

    ray_t* out = ray_median_per_group_buf(src, idx_buf, offsets, grp_cnt, n_groups);
    ray_release(src);
    return out;  /* NULL → unsupported type; caller falls back */
}

/* Per-group count(distinct) parallel kernel — one task per group, each
 * task does its own dedup with a scratch hash table.  Skips the
 * gather_by_idx + exec_count_distinct allocation that the serial path
 * pays per group.
 *
 * Caller resolves `src` (typed vector with per-row layout matching
 * idx_buf rows).  Worker tasks read via idx_buf[offsets[gi]+i] directly,
 * no intermediate compaction.
 *
 * Specialised on element width (1/2/4/8 bytes + F64) so the inner read
 * folds to a typed pointer dereference.
 *
 * A null counts as one distinct value, matching the serial path and the
 * ungrouped builtin — `distinct` keeps at most one null, so `count` over it
 * sees exactly one.  Nothing special is needed to make that happen: an
 * integer null is INT_MIN, nonzero and unique once widened, so it hashes as
 * an ordinary key; canon_f64_key folds every NaN payload to one; and a SYM
 * null is id 0, which the table cannot hold and which therefore rides the
 * side flag the literal 0 already uses. */
typedef struct {
    int8_t          in_type;
    uint8_t         in_attrs;
    const void*     base;
    uint8_t         esz;        /* 1/2/4/8 */
    bool            is_f64;
    const int64_t*  idx_buf;
    const int64_t*  offsets;
    const int64_t*  grp_cnt;
    int64_t*        odata;
    int64_t         sym_cap_bound; /* SYM: cap per-group HT at 2*domain_count (else 0) */
    _Atomic(int32_t) oom;
} cdpg_buf_par_ctx_t;

#define CDPG_BUF_HASH_K1 0x9E3779B97F4A7C15ULL

/* Single-array HT: empty slot is encoded as 0; the (extremely rare)
 * v == 0 input goes into a separate saw_zero flag.  Compared to the
 * previous (set + used) two-array layout this halves the memory
 * footprint per slot (8 B vs 9 B) and — more importantly — collapses
 * two cache-line accesses (used byte + set int64) into a single
 * int64 read on the hot path.  Hits high-cardinality count_distinct
 * grouped queries where the per-group HT churn was thrashing L2. */
#define CDPG_BUF_INSERT(VAL_EXPR) do {                              \
    int64_t _ins_v = (int64_t)(VAL_EXPR);                           \
    if (RAY_UNLIKELY(_ins_v == 0)) {                                \
        if (!saw_zero) { saw_zero = 1; distinct++; }                \
        break;                                                      \
    }                                                               \
    uint64_t _ins_h = (uint64_t)_ins_v * CDPG_BUF_HASH_K1;          \
    _ins_h ^= _ins_h >> 33;                                         \
    uint64_t _ins_slot = _ins_h & mask;                             \
    for (;;) {                                                      \
        int64_t _ins_cur = set[_ins_slot];                          \
        if (_ins_cur == 0) {                                        \
            set[_ins_slot] = _ins_v;                                \
            distinct++;                                             \
            break;                                                  \
        }                                                           \
        if (_ins_cur == _ins_v) break;                              \
        _ins_slot = (_ins_slot + 1) & mask;                         \
    }                                                               \
} while (0)

static void cdpg_buf_par_fn(void* vctx, uint32_t worker_id,
                            int64_t start, int64_t end) {
    (void)worker_id;
    cdpg_buf_par_ctx_t* ctx = (cdpg_buf_par_ctx_t*)vctx;
    if (atomic_load_explicit(&ctx->oom, memory_order_relaxed)) return;

    for (int64_t gi = start; gi < end; gi++) {
        int64_t cnt = ctx->grp_cnt[gi];
        if (cnt == 0) { ctx->odata[gi] = 0; continue; }
        const int64_t* idxs = &ctx->idx_buf[ctx->offsets[gi]];

        uint64_t cap = (uint64_t)cnt * 2;
        if (cap < 32) cap = 32;
        uint64_t c = 1;
        while (c && c < cap) c <<= 1;
        if (!c) {
            atomic_store_explicit(&ctx->oom, 1, memory_order_relaxed);
            return;
        }
        cap = c;
        if (ctx->sym_cap_bound && cap > (uint64_t)ctx->sym_cap_bound)
            cap = (uint64_t)ctx->sym_cap_bound;   /* SYM distinct <= domain_count */
        uint64_t mask = cap - 1;

        ray_t* set_hdr  = NULL;
        int64_t* set    = (int64_t*)scratch_calloc(&set_hdr,
                                                   (size_t)cap * sizeof(int64_t));
        if (!set) {
            atomic_store_explicit(&ctx->oom, 1, memory_order_relaxed);
            return;
        }

        int64_t distinct = 0;
        int saw_zero = 0;

        if (ctx->is_f64) {
            const double* d = (const double*)ctx->base;
            for (int64_t i = 0; i < cnt; i++) {
                int64_t r = idxs[i];
                CDPG_BUF_INSERT(canon_f64_key(d[r]));
            }
        } else if (ctx->esz == 8) {
            /* The null sentinels are INT_MIN: nonzero, and unique once widened
             * to int64_t, so they hash as an ordinary key and a null counts
             * exactly once with no special case. */
            const int64_t* d = (const int64_t*)ctx->base;
            for (int64_t i = 0; i < cnt; i++)
                CDPG_BUF_INSERT(d[idxs[i]]);
        } else if (ctx->esz == 4) {
            const int32_t* d = (const int32_t*)ctx->base;
            for (int64_t i = 0; i < cnt; i++)
                CDPG_BUF_INSERT((int64_t)d[idxs[i]]);
        } else if (ctx->esz == 2) {
            const int16_t* d = (const int16_t*)ctx->base;
            for (int64_t i = 0; i < cnt; i++)
                CDPG_BUF_INSERT((int64_t)d[idxs[i]]);
        } else { /* esz == 1 — BOOL/U8 are non-nullable */
            const uint8_t* d = (const uint8_t*)ctx->base;
            for (int64_t i = 0; i < cnt; i++) {
                CDPG_BUF_INSERT((int64_t)d[idxs[i]]);
            }
        }

        scratch_free(set_hdr);
        ctx->odata[gi] = distinct;
    }
}
#undef CDPG_BUF_INSERT

/* Per-group row slices (grp_cnt / offsets / idx_buf) from row_gid, for
 * the per-group-slice consumers (count_distinct_per_group_buf,
 * nonagg_eval_per_group_buf, ...).  A row whose gid is negative belongs to
 * no group; with a selection only the rows it keeps are visited.
 *
 * Tasks are contiguous runs of morsels, one per worker (fewer when the
 * histograms would outgrow SLICE_HIST_CELLS), cut so each holds about the
 * same number of visited rows.  Pass 1 counts each task's rows per group
 * into its own histogram row; a row-wise prefix turns the rows into
 * cursors; pass 2 scatters.  Tasks run in row order within a group, so the
 * rows of a group come out ascending.  The histograms are sized by the
 * worker count, not the table: one row per 8192-row task had made them
 * (and the prefix over them) grow with the table (#710). */
#define SLICE_SERIAL_ROWS (1 << 17)   /* fewer visited rows: one task */
#define SLICE_HIST_CELLS  (1 << 23)   /* bound on n_tasks * n_groups */

typedef struct {
    const int64_t*  row_gid;
    int64_t         nrows;
    int64_t         n_groups;
    int64_t*        hist;        /* [n_tasks * n_groups]: counts, then cursors */
    int64_t*        idx_buf;     /* pass 2 only */
    const int64_t*  seg_lo;      /* [n_tasks + 1]: each task's morsels */
    const uint8_t*  sel_flg;     /* NULL: every row */
    const uint32_t* sel_offs;
    const uint16_t* sel_idx;
} slice_build_ctx_t;

static inline void slice_build_task(const slice_build_ctx_t* c, int64_t t, bool scatter) {
    int64_t* h = c->hist + t * c->n_groups;
    const int64_t* row_gid = c->row_gid;
    int64_t* out = c->idx_buf;
    for (int64_t seg = c->seg_lo[t]; seg < c->seg_lo[t + 1]; seg++) {
        int64_t s_lo = seg * RAY_MORSEL_ELEMS;
        uint8_t f = c->sel_flg ? c->sel_flg[seg] : RAY_SEL_ALL;
        if (f == RAY_SEL_NONE) continue;
        if (f == RAY_SEL_ALL) {
            int64_t s_hi = s_lo + RAY_MORSEL_ELEMS;
            if (s_hi > c->nrows) s_hi = c->nrows;
            for (int64_t r = s_lo; r < s_hi; r++) {
                int64_t gi = row_gid[r];
                if (gi < 0) continue;
                if (scatter) out[h[gi]++] = r;
                else h[gi]++;
            }
            continue;
        }
        for (uint32_t i = c->sel_offs[seg]; i < c->sel_offs[seg + 1]; i++) {
            int64_t r = s_lo + c->sel_idx[i];
            int64_t gi = row_gid[r];
            if (gi < 0) continue;
            if (scatter) out[h[gi]++] = r;
            else h[gi]++;
        }
    }
}

static void slice_count_fn(void* vctx, uint32_t worker_id, int64_t t0, int64_t t1) {
    (void)worker_id;
    for (int64_t t = t0; t < t1; t++) slice_build_task((const slice_build_ctx_t*)vctx, t, false);
}

static void slice_scatter_fn(void* vctx, uint32_t worker_id, int64_t t0, int64_t t1) {
    (void)worker_id;
    for (int64_t t = t0; t < t1; t++) slice_build_task((const slice_build_ctx_t*)vctx, t, true);
}

/* Build the slices into grp_cnt / offsets (caller's [n_groups] arrays) and
 * *idx_hdr, using pos[n_groups] as scratch.  sel may be NULL.  Returns
 * false on allocation failure. */
static bool slice_build(const int64_t* row_gid, int64_t nrows, ray_t* sel,
                        int64_t n_groups, int64_t* grp_cnt, int64_t* offsets,
                        int64_t* pos, ray_t** idx_hdr) {
    *idx_hdr = NULL;
    int64_t n_segs = (nrows + RAY_MORSEL_ELEMS - 1) / RAY_MORSEL_ELEMS;
    slice_build_ctx_t c = { .row_gid = row_gid, .nrows = nrows, .n_groups = n_groups };
    int64_t visited = nrows;
    if (sel) {
        ray_rowsel_t* sm = ray_rowsel_meta(sel);
        if ((int64_t)sm->n_segs < n_segs) n_segs = sm->n_segs;
        c.sel_flg  = ray_rowsel_flags(sel);
        c.sel_offs = ray_rowsel_offsets(sel);
        c.sel_idx  = ray_rowsel_idx(sel);
        visited = sm->total_pass;
    }
    ray_pool_t* pool = ray_pool_get();
    int64_t n_tasks = 1;
    if (pool && visited >= SLICE_SERIAL_ROWS && n_groups > 0 &&
        ray_pool_par_dispatch_ok(pool, visited, SLICE_SERIAL_ROWS)) {
        n_tasks = ray_pool_total_workers(pool);
        if (n_tasks * n_groups > SLICE_HIST_CELLS) n_tasks = SLICE_HIST_CELLS / n_groups;
        if (n_tasks > n_segs) n_tasks = n_segs;
        if (n_tasks < 2) n_tasks = 1;
    }
    ray_t* hist_hdr = NULL;
    ray_t* seg_hdr = NULL;
    int64_t* hist = (int64_t*)scratch_calloc(&hist_hdr,
        (size_t)n_tasks * (size_t)(n_groups > 0 ? n_groups : 1) * sizeof(int64_t));
    int64_t* seg_lo = (int64_t*)scratch_alloc(&seg_hdr, (size_t)(n_tasks + 1) * sizeof(int64_t));
    if (!hist || !seg_lo) {
        if (hist_hdr) scratch_free(hist_hdr);
        if (seg_hdr) scratch_free(seg_hdr);
        return false;
    }
    /* cut the morsels into n_tasks runs of about visited / n_tasks rows */
    seg_lo[0] = 0;
    if (n_tasks > 1 && c.sel_flg) {
        int64_t t = 1, acc = 0;
        for (int64_t seg = 0; seg < n_segs && t < n_tasks; seg++) {
            uint8_t f = c.sel_flg[seg];
            if (f == RAY_SEL_ALL) {
                int64_t s_hi = (seg + 1) * RAY_MORSEL_ELEMS;
                acc += (s_hi > nrows ? nrows : s_hi) - seg * RAY_MORSEL_ELEMS;
            } else if (f != RAY_SEL_NONE) {
                acc += c.sel_offs[seg + 1] - c.sel_offs[seg];
            }
            while (t < n_tasks && acc * n_tasks >= visited * t) seg_lo[t++] = seg + 1;
        }
        while (t < n_tasks) seg_lo[t++] = n_segs;
    } else {
        for (int64_t t = 1; t < n_tasks; t++) seg_lo[t] = n_segs * t / n_tasks;
    }
    seg_lo[n_tasks] = n_segs;
    c.hist = hist;
    c.seg_lo = seg_lo;

    if (n_tasks > 1) ray_pool_dispatch_n(pool, slice_count_fn, &c, (uint32_t)n_tasks);
    else             slice_count_fn(&c, 0, 0, 1);

    /* row-wise prefix: totals per group, offsets, then each task's cursor */
    memset(grp_cnt, 0, (size_t)n_groups * sizeof(int64_t));
    for (int64_t t = 0; t < n_tasks; t++) {
        const int64_t* h = hist + t * n_groups;
        for (int64_t gi = 0; gi < n_groups; gi++) grp_cnt[gi] += h[gi];
    }
    int64_t total = 0;
    for (int64_t gi = 0; gi < n_groups; gi++) { offsets[gi] = total; total += grp_cnt[gi]; }
    if (n_groups > 0) memcpy(pos, offsets, (size_t)n_groups * sizeof(int64_t));
    for (int64_t t = 0; t < n_tasks; t++) {
        int64_t* h = hist + t * n_groups;
        for (int64_t gi = 0; gi < n_groups; gi++) { int64_t k = h[gi]; h[gi] = pos[gi]; pos[gi] += k; }
    }

    *idx_hdr = ray_alloc((size_t)(total > 0 ? total : 1) * sizeof(int64_t));
    if (!*idx_hdr) { scratch_free(hist_hdr); scratch_free(seg_hdr); return false; }
    c.idx_buf = (int64_t*)ray_data(*idx_hdr);
    if (n_tasks > 1) ray_pool_dispatch_n(pool, slice_scatter_fn, &c, (uint32_t)n_tasks);
    else             slice_scatter_fn(&c, 0, 0, 1);
    scratch_free(hist_hdr);
    scratch_free(seg_hdr);
    return true;
}

static ray_t* query_materialize_parted_col(ray_t* col) {
    if (!col) return NULL;
    if (col->type == RAY_MAPCOMMON) return materialize_mapcommon(col);
    if (!RAY_IS_PARTED(col->type)) {
        ray_retain(col);
        return col;
    }

    int8_t base = (int8_t)RAY_PARTED_BASETYPE(col->type);
    ray_t** segs = (ray_t**)ray_data(col);
    int64_t total = ray_parted_nrows(col);
    if (base == RAY_STR) return parted_flatten_str(segs, col->len, total);
    if (base == RAY_LIST) return parted_flatten_list(segs, col->len, total);

    uint8_t attrs = (base == RAY_SYM) ? parted_sym_max_attrs(segs, col->len) : 0;
    ray_t* flat = typed_vec_new(base, attrs, total);
    if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
    flat->len = total;

    int64_t off = 0;
    uint8_t seg_nulls = 0;
    for (int64_t s = 0; s < col->len; s++) {
        ray_t* seg = segs[s];
        if (!seg || seg->len <= 0) continue;
        parted_copy_cells(ray_data(flat), base, attrs, off, seg, 0, seg->len);
        seg_nulls |= seg->attrs & RAY_ATTR_HAS_NULLS;
        off += seg->len;
    }
    /* Null propagation: parted_copy_cells copies the sentinel
     * bit-patterns but not the HAS_NULLS flag — OR it across segments so the
     * flat result carries it whenever any segment did. */
    flat->attrs |= seg_nulls;
    /* The memcpy above copied SYM cell ids verbatim from the partition
     * segments — resolve over their domain (all segments share the root
     * symfile's domain; sym_domain_rep returns the first SYM segment). */
    if (flat->type == RAY_SYM) ray_sym_vec_adopt_domain(flat, sym_domain_rep(col));
    return flat;
}

/* Planner rewrite for `(select {K: K c: (count (distinct X)) from: T
 * [where: W] by: K [desc: c take: N]})`.
 *
 * Original execution: outer group-by K builds idx_buf → per-group dedup
 * over X (via cdpg_buf_par_fn or per-group HLL).  That pays the outer
 * group-by + idx_buf scatter even when the per-group dedup is the
 * dominant cost.
 *
 * Rewrite: group by (K, X) once — this deduplicates (K, X) tuples in a
 * single pass that lands on the v2 multi-key kernel — then count rows
 * per K on the (typically much smaller) dedup table.  For q08 on the
 * 10M-row hits table, the (K, X) pass produces ~700 K tuples; the final
 * group-by walks just that.
 *
 * Returns NULL on shape miss (caller falls through to the existing
 * count-distinct path); returns a result table on success.  Gates:
 *  - plain K and X columns with supported grouping key types
 *  - at most 15 K columns, leaving one component for X
 *  - WHERE optional; if present, must be supported by the fused predicate
 *  - desc/take optional, must be on the cd output column when present */
static ray_t* try_count_distinct_v2_rewrite(
    ray_t* tbl,
    ray_t* by_expr,
    ray_t* where_expr,
    ray_t** dict_elems, int64_t dict_n,
    int64_t from_id, int64_t where_id, int64_t by_id,
    int64_t take_id, int64_t asc_id, int64_t desc_id,
    int64_t nearest_id)
{
    if (!tbl || tbl->type != RAY_TABLE) return NULL;
    /* by: accepts a single bare column name ((by: K), single-key), a
     * [K1 K2 ...] symbol vector (multi-key, output columns keep the source
     * names), or a {Name: Col Name: Col ...} dict (multi-key composite with
     * aliases).  In every case we collect the source column syms into
     * K_syms[].  The vector form is how a composite key is normally written
     * (`by: [MobilePhone MobilePhoneModel]`); without this branch it fell
     * to the general per-group path, seven times slower than the dict form
     * of the same query. */
    int64_t K_syms[15];  /* leave room for X in the composite */
    int n_K = 0;
    if (by_expr && by_expr->type == -RAY_SYM &&
        !(by_expr->attrs & ATTR_QUOTED)) {
        K_syms[n_K++] = by_expr->i64;
    } else if (by_expr && by_expr->type == RAY_SYM && ray_is_vec(by_expr) &&
               !(by_expr->attrs & ATTR_QUOTED)) {
        int64_t n = ray_len(by_expr);
        if (n == 0 || n > 15) return NULL;
        for (int64_t i = 0; i < n; i++)
            K_syms[n_K++] = ray_read_sym(ray_data(by_expr), i, by_expr->type, by_expr->attrs);
    } else if (by_expr && by_expr->type == RAY_DICT) {
        DICT_VIEW_DECL(byv);
        DICT_VIEW_OPEN(by_expr, byv);
        if (DICT_VIEW_OVERFLOW(byv)) { DICT_VIEW_CLOSE(byv); return NULL; }
        int64_t pairs = byv_n / 2;
        if (pairs == 0 || pairs > 15) { DICT_VIEW_CLOSE(byv); return NULL; }
        for (int64_t i = 0; i < pairs; i++) {
            ray_t* v = byv[i * 2 + 1];
            if (!v || v->type != -RAY_SYM || (v->attrs & ATTR_QUOTED)) {
                DICT_VIEW_CLOSE(byv);
                return NULL;  /* non-column-ref value — out of scope */
            }
            K_syms[n_K++] = v->i64;
        }
        DICT_VIEW_CLOSE(byv);
    } else {
        return NULL;
    }

    /* Walk the dict — accept exactly one `(count (distinct col_ref))`
     * agg and an optional identity key projection.  Any other agg /
     * projection / take-on-something-else aborts the rewrite. */
    int64_t cd_X_sym = -1;
    int64_t cd_c_sym = -1;
    int n_cd = 0, n_other = 0;
    int64_t desc_col_sym = -1;  /* if desc:, its column-sym target */
    int64_t asc_col_sym  = -1;
    int     has_take = 0;
    int64_t take_n   = -1;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        ray_t*  val = dict_elems[i + 1];
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == nearest_id) continue;
        if (kid == take_id) {
            int64_t v;
            if (atom_i64_const(val, &v) && v > 0) {
                has_take = 1;
                take_n   = v;
            } else {
                return NULL;  /* non-trivial take */
            }
            continue;
        }
        if (kid == asc_id) {
            if (val && val->type == -RAY_SYM && !(val->attrs & ATTR_QUOTED))
                asc_col_sym = val->i64;
            else return NULL;
            continue;
        }
        if (kid == desc_id) {
            if (val && val->type == -RAY_SYM && !(val->attrs & ATTR_QUOTED))
                desc_col_sym = val->i64;
            else return NULL;
            continue;
        }
        ray_t* cd_inner = match_count_distinct(val);
        if (cd_inner && cd_inner->type == -RAY_SYM &&
            !(cd_inner->attrs & ATTR_QUOTED))
        {
            cd_X_sym = cd_inner->i64;
            cd_c_sym = kid;
            n_cd++;
        } else if (val && val->type == -RAY_SYM &&
                   !(val->attrs & ATTR_QUOTED)) {
            /* identity key projection (e.g. {K: K} or one element of a
             * multi-key dict) — accepted iff the referenced column is
             * one of the by keys. */
            int matched = 0;
            for (int j = 0; j < n_K; j++)
                if (K_syms[j] == val->i64) { matched = 1; break; }
            if (!matched) n_other++;
        } else {
            n_other++;
        }
    }
    if (n_cd != 1 || n_other > 0) return NULL;
    if (cd_X_sym < 0 || cd_c_sym < 0) return NULL;

    /* desc/asc must target the count output column. */
    if (desc_col_sym >= 0 && desc_col_sym != cd_c_sym) return NULL;
    if (asc_col_sym  >= 0 && asc_col_sym  != cd_c_sym) return NULL;
    if (desc_col_sym >= 0 && asc_col_sym  >= 0) return NULL;

    /* General grouping retains full keys when a tuple does not fit the
     * packed kernel. Its byte-width limit must not restrict this rewrite. */
    ray_t* K_cols[15];
    for (int j = 0; j < n_K; j++) {
        K_cols[j] = ray_table_get_col(tbl, K_syms[j]);
        if (!K_cols[j]) return NULL;
        int8_t kct_j = K_cols[j]->type;
        if (RAY_IS_PARTED(kct_j) || kct_j == RAY_MAPCOMMON) return NULL;
        int kct_ok_j = (kct_j == RAY_SYM  || kct_j == RAY_BOOL || kct_j == RAY_U8 ||
                        kct_j == RAY_I16  || kct_j == RAY_I32  || kct_j == RAY_I64 ||
                        kct_j == RAY_DATE || kct_j == RAY_TIME || kct_j == RAY_TIMESTAMP || kct_j == RAY_F32 || kct_j == RAY_F64 ||
                        kct_j == RAY_STR || kct_j == RAY_GUID || kct_j == RAY_LIST);
        if (!kct_ok_j) return NULL;
    }
    ray_t* X_col = ray_table_get_col(tbl, cd_X_sym);
    if (!X_col) return NULL;
    int8_t xct = X_col->type;
    if (RAY_IS_PARTED(xct) || xct == RAY_MAPCOMMON) return NULL;
    /* X gets the same per-type acceptability check as the K columns
     * (validated in the loop above).  SYM is allowed — mk_compile packs
     * it by storage width into the composite key. */
    int xct_ok = (xct == RAY_SYM  || xct == RAY_BOOL || xct == RAY_U8 ||
                  xct == RAY_I16  || xct == RAY_I32  || xct == RAY_I64 ||
                  xct == RAY_DATE || xct == RAY_TIME || xct == RAY_TIMESTAMP || xct == RAY_F32 || xct == RAY_F64 ||
                  xct == RAY_STR || xct == RAY_GUID || xct == RAY_LIST);
    if (!xct_ok) return NULL;

    if (where_expr && !ray_fused_group_supported(where_expr, tbl))
        return NULL;

    /* === Fused single-pass kernel (spec Part B) ===
     * Single narrow key, plain column cd-inner, no WHERE, flat columns:
     * ray_cd_fused replaces the two group-by passes below with one
     * partitioned scatter + per-partition dedup.  It declines (NULL) on
     * any shape it does not handle — small inputs, unsupported types,
     * nullable columns, allocation pressure — and NULL is never an
     * error: the two-pass rewrite below then runs unchanged.
     *
     * The kernel calls ray_pool_dispatch unconditionally, and the pool is
     * single-producer, so it must never run from inside an in-flight
     * dispatch (i.e. from a worker thread).  ray_pool_par_dispatch_ok
     * carries exactly that check (n_workers > 0 && !ray_parallel_flag)
     * plus the row-count floor. */
    if (n_K == 1 && !where_expr) {
        int64_t nrows = ray_table_nrows(tbl);
        if (ray_pool_par_dispatch_ok(ray_pool_get(), nrows, CDF_MIN_ROWS)) {
            ray_t* fr = ray_cd_fused(K_cols[0], X_col, nrows);
            if (fr) {
                if (RAY_IS_ERR(fr)) return fr;
                /* fr = (k, u, _first) I64 columns in first-seen key order.
                 * Emit {K: keys, c: counts}: the key column is rebuilt at
                 * the source column's own type/width (so a SYM key stays a
                 * SYM over the source domain and renders as text), and
                 * `_first` — an internal ordering artifact — is dropped by
                 * building a fresh 2-column table. */
                ray_t* fk = ray_table_get_col_idx(fr, 0);
                ray_t* fu = ray_table_get_col_idx(fr, 1);
                int64_t ng = fk ? fk->len : 0;
                ray_t* kv = col_vec_new(K_cols[0], ng > 0 ? ng : 1);
                if (!kv || RAY_IS_ERR(kv)) {
                    ray_release(fr);
                    return kv ? kv : ray_error("oom", NULL);
                }
                if (kv->type == RAY_SYM)
                    ray_sym_vec_adopt_domain(kv, sym_domain_rep(K_cols[0]));
                kv->len = ng;
                {
                    const int64_t* src = (const int64_t*)ray_data(fk);
                    void* dst = ray_data(kv);
                    for (int64_t i = 0; i < ng; i++) {
                        if (kv->type == RAY_F32) { uint32_t bits = (uint32_t)src[i]; memcpy((float*)dst + i, &bits, 4); }
                        else if (kv->type == RAY_F64) memcpy((double*)dst + i, &src[i], 8);
                        else write_col_i64(dst, i, src[i], kv->type, kv->attrs);
                    }
                    if (ray_vec_may_have_nulls(K_cols[0])) kv->attrs |= RAY_ATTR_HAS_NULLS;
                }
                ray_t* out = ray_table_new(2);
                if (out && !RAY_IS_ERR(out))
                    out = ray_table_add_col(out, K_syms[0], kv);
                if (out && !RAY_IS_ERR(out))
                    out = ray_table_add_col(out, cd_c_sym, fu);
                ray_release(kv);
                ray_release(fr);
                if (!out) return ray_error("oom", NULL);
                if (RAY_IS_ERR(out)) return out;
                return apply_sort_take(out, dict_elems, dict_n,
                                       asc_id, desc_id, take_id, NULL);
            }
        }
    }

    /* === Inner pass: group by (K1, ..., Kn, X) on the source table === */
    ray_graph_t* g_in = ray_graph_new(tbl);
    if (!g_in) return NULL;
    ray_t* K_names[15];
    ray_op_t* K_scans[15];
    for (int j = 0; j < n_K; j++) {
        K_names[j] = ray_sym_str(K_syms[j]);
        if (!K_names[j]) { ray_graph_free(g_in); return NULL; }
        K_scans[j] = ray_scan(g_in, ray_str_ptr(K_names[j]));
        if (!K_scans[j]) { ray_graph_free(g_in); return NULL; }
    }
    ray_t* X_name = ray_sym_str(cd_X_sym);
    if (!X_name) { ray_graph_free(g_in); return NULL; }
    ray_op_t* X_scan = ray_scan(g_in, ray_str_ptr(X_name));
    if (!X_scan) { ray_graph_free(g_in); return NULL; }
    /* Exact-size carve: the inner group's key list is K_scans[0..n_K) plus
     * the distinct-target X_scan — n_K+1 is the exact count, not a cap. */
    ray_t* ki_hdr = NULL;
    ray_op_t** keys_in = (ray_op_t**)scratch_alloc(&ki_hdr,
            (size_t)(n_K + 1) * sizeof(ray_op_t*));
    if (!keys_in) { ray_graph_free(g_in); return ray_error("oom", NULL); }
    for (int j = 0; j < n_K; j++) keys_in[j] = K_scans[j];
    keys_in[n_K] = X_scan;
    uint16_t  agg_ops_in[1] = { OP_COUNT };
    ray_op_t* agg_ins_in[1] = { K_scans[0] };  /* count agg input is irrelevant */
    /* WHERE (when present) is applied as a general OP_FILTER pre-pass:
     * exec_node on a TABLE input refines g_in->selection in place, and the
     * subsequent OP_GROUP execution honours that selection (the v2 group
     * engine consumes it directly).  This replaces the retired
     * OP_FILTERED_GROUP fused node; results are identical. */
    if (where_expr) {
        ray_op_t* pred = compile_expr_dag(g_in, where_expr);
        if (!pred) { scratch_free(ki_hdr); ray_graph_free(g_in); return NULL; }
        ray_op_t* froot = ray_filter(g_in, ray_const_table(g_in, tbl), pred);
        if (!froot) { scratch_free(ki_hdr); ray_graph_free(g_in); return NULL; }
        froot = ray_optimize(g_in, froot);
        ray_t* fres = exec_node(g_in, froot);
        if (!fres || RAY_IS_ERR(fres)) {
            if (g_in->selection) { ray_release(g_in->selection); g_in->selection = NULL; }
            scratch_free(ki_hdr);
            ray_graph_free(g_in);
            return (fres && RAY_IS_ERR(fres)) ? fres : NULL;
        }
    }
    ray_op_t* inner = ray_group(g_in, keys_in, n_K + 1,
                                agg_ops_in, agg_ins_in, 1);
    if (!inner) { scratch_free(ki_hdr); ray_graph_free(g_in); return NULL; }
    ray_t* dedup = ray_execute(g_in, inner);
    scratch_free(ki_hdr);
    ray_graph_free(g_in);
    if (!dedup) return NULL;
    if (RAY_IS_ERR(dedup)) return dedup;
    if (dedup->type != RAY_TABLE) { ray_release(dedup); return NULL; }

    /* === Outer pass: group dedup table by (K1, ..., Kn) with COUNT === */
    ray_graph_t* g_out = ray_graph_new(dedup);
    if (!g_out) { ray_release(dedup); return ray_error("oom", NULL); }
    ray_op_t* K_scans2[15];
    for (int j = 0; j < n_K; j++) {
        K_scans2[j] = ray_scan(g_out, ray_str_ptr(K_names[j]));
        if (!K_scans2[j]) { ray_graph_free(g_out); ray_release(dedup); return NULL; }
    }
    ray_op_t* keys_out[15];
    for (int j = 0; j < n_K; j++) keys_out[j] = K_scans2[j];
    uint16_t  agg_ops_out[1] = { OP_COUNT };
    ray_op_t* agg_ins_out[1] = { K_scans2[0] };

    /* Apply desc:c take:N via the group emit_filter so the second pass
     * can heap-trim to top-N without materialising every (K, count) row. */
    ray_group_emit_filter_t prev_emit = ray_group_emit_filter_get();
    ray_group_emit_filter_t emit_f = {0};
    int emit_set = 0;
    if (desc_col_sym == cd_c_sym && has_take && take_n > 0) {
        emit_f.enabled = true;
        emit_f.agg_index = 0;
        emit_f.top_count_take = take_n;
        emit_f.min_count_exclusive = 0;
        /* This rewrite fires only for `desc: c take: N` (see the
         * desc_col_sym test above) — consumers read .desc as the direction,
         * so set it explicitly instead of leaning on a zero default that
         * used to be coerced to desc inside group.c. */
        emit_f.desc = 1;
        emit_f.target_depth = __VM ? __VM->eval_depth : 0;   /* executed right here */
        ray_group_emit_filter_set(emit_f);
        emit_set = 1;
    }
    ray_op_t* outer = ray_group(g_out, keys_out, n_K,
                                agg_ops_out, agg_ins_out, 1);
    if (!outer) {
        if (emit_set) ray_group_emit_filter_set(prev_emit);
        ray_graph_free(g_out);
        ray_release(dedup);
        return ray_error("oom", NULL);
    }
    ray_t* result = ray_execute(g_out, outer);
    if (emit_set) ray_group_emit_filter_set(prev_emit);
    ray_graph_free(g_out);
    ray_release(dedup);
    if (!result || RAY_IS_ERR(result)) return result;
    if (result->type != RAY_TABLE) return result;

    /* Rename the count output column to the user's requested c_sym
     * alias.  The outer pass counts a key column, so ray_group names
     * the agg output "<K1>_count" — the result has the n_K key columns
     * plus this one count column.  The count column is the one whose
     * name matches none of the K syms. */
    {
        int64_t nc = ray_table_ncols(result);
        for (int64_t ci = 0; ci < nc; ci++) {
            int64_t cn = ray_table_col_name(result, ci);
            int is_key = 0;
            for (int j = 0; j < n_K; j++) if (cn == K_syms[j]) { is_key = 1; break; }
            if (!is_key && cn != cd_c_sym) {
                ray_table_set_col_name(result, ci, cd_c_sym);
                break;
            }
        }
    }
    /* Apply the user's desc:/asc:/take: clauses on the dedup'd, counted
     * output.  The emit_filter trims to top-N during the outer pass but
     * leaves rows in HT-iteration order, so without this final sort the
     * `desc: c` ordering is silently dropped — the result set is right
     * but its row order isn't.  apply_sort_take is a no-op when the
     * clauses are absent. */
    return apply_sort_take(result, dict_elems, dict_n, asc_id, desc_id, take_id, NULL);
}

/* Per-group count(distinct) using the existing OP_COUNT_DISTINCT kernel.
 * Mirrors aggr_unary_per_group_buf but slices the source column once per
 * group and calls exec_count_distinct directly — bypasses the full
 * ray_eval per-group path that re-walks the (count (distinct …)) AST
 * for each slice.
 *
 * `inner_expr` is the operand to `distinct` extracted via
 * match_count_distinct (typically a column ref, possibly a dotted-name
 * or computed sub-expression).  Returns an I64 vector of length
 * n_groups with the per-group distinct count. */
static ray_t* count_distinct_per_group_buf(ray_t* inner_expr, ray_t* tbl,
                                           const int64_t* idx_buf,
                                           const int64_t* offsets,
                                           const int64_t* grp_cnt,
                                           int64_t n_groups) {
    /* Resolve the source vector — either a direct column ref (zero copy)
     * or a full-table eval of the inner sub-expression. */
    ray_t* src = NULL;
    if (inner_expr && inner_expr->type == -RAY_SYM &&
        !(inner_expr->attrs & ATTR_QUOTED)) {
        src = ray_table_get_col(tbl, inner_expr->i64);
        if (src) ray_retain(src);
    }
    if (!src) {
        if (ray_env_push_query_scope() != RAY_OK) return ray_error("oom", NULL);
        ray_t* _aqt = bind_all_columns(tbl);
        src = ray_eval(inner_expr);
        g_active_query_table = _aqt;
        ray_env_pop_scope();
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("domain", "select by: failed to evaluate count-distinct source");
    }
    if (src && !RAY_IS_ERR(src) && (RAY_IS_PARTED(src->type) || src->type == RAY_MAPCOMMON)) {
        ray_t* flat = query_materialize_parted_col(src);
        ray_release(src);
        src = flat;
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("oom", NULL);
    }

    if (src->type == RAY_STR || src->type == RAY_GUID || src->type == RAY_LIST) {
        ray_t* wide = agg_count_distinct_indexed(src, idx_buf, offsets, grp_cnt, n_groups);
        ray_release(src);
        return wide;
    }

    ray_t* out = ray_vec_new(RAY_I64, n_groups);
    if (!out || RAY_IS_ERR(out)) {
        ray_release(src);
        return out ? out : ray_error("oom", NULL);
    }
    out->len = n_groups;
    int64_t* odata = (int64_t*)ray_data(out);

    /* COUNT(DISTINCT col) per group is EXACT.  Streaming HLL +
     * per-group HLL buf fast paths returned approximation here and
     * have been removed.  HLL remains in the codebase for explicit
     * opt-in approximate entries and internal cardinality estimation
     * only.  Exact parallel dedup follows below. */

    /* Parallel path: dispatch one task per group when src has a flat
     * numeric / SYM layout we can read with a typed pointer.  Each task
     * does its own dedup with a scratch hash table — no gather_by_idx
     * allocation, no exec_count_distinct overhead per group.
     *
     * Q11/Q14/Q15-class workloads see ≥ 10× speedup at 28 workers
     * because the serial loop was leaving the cores idle.  Fall through
     * to the existing serial path on type mismatch, error, or OOM. */
    {
        int8_t st = src->type;
        bool flat_ok = (st == RAY_BOOL || st == RAY_U8 ||
                        st == RAY_I16  || st == RAY_I32 || st == RAY_I64 ||
                        st == RAY_F64  || st == RAY_DATE || st == RAY_TIME ||
                        st == RAY_TIMESTAMP || RAY_IS_SYM(st));
        ray_pool_t* pool = ray_pool_get();
        if (flat_ok && pool && ray_pool_total_workers(pool) >= 2 && n_groups >= 4) {
            /* SYM distinct values per group are bounded by domain_count, so a
             * per-group HT sized 2*cnt (the group's ROW count) is wasteful — a
             * huge low-card group builds a multi-MB cache-busting table to find
             * a handful of codes.  Cap it at 2*domain_count for small domains. */
            int64_t sym_cap_bound = 0;
            if (RAY_IS_SYM(st)) {
                int64_t dc = ray_sym_domain_count(ray_sym_vec_domain(src));
                if (dc > 0 && dc <= 65536) {
                    uint64_t b = 32; while (b < (uint64_t)dc * 2) b <<= 1;
                    sym_cap_bound = (int64_t)b;
                }
            }
            cdpg_buf_par_ctx_t pctx = {
                .in_type   = st,
                .in_attrs  = src->attrs,
                .base      = ray_data(src),
                .esz       = ray_sym_elem_size(st, src->attrs),
                .is_f64    = (st == RAY_F64),
                .idx_buf   = idx_buf,
                .offsets   = offsets,
                .grp_cnt   = grp_cnt,
                .odata     = odata,
                .sym_cap_bound = sym_cap_bound,
                .oom       = 0,
            };
            ray_group_dispatch(cdpg_buf_par_fn, &pctx, grp_cnt, n_groups);
            if (!atomic_load_explicit(&pctx.oom, memory_order_relaxed)) {
                ray_release(src);
                return out;
            }
            /* OOM in scratch — fall through to serial path with the
             * already-allocated `out` (will be overwritten). */
        }
    }

    for (int64_t gi = 0; gi < n_groups; gi++) {
        int64_t cnt = grp_cnt[gi];
        if (cnt == 0) { odata[gi] = 0; continue; }
        /* gather_by_idx preserves the source's typed layout (I64 stays
         * I64, SYM stays SYM with adaptive width, etc.) — exactly what
         * exec_count_distinct expects.  ray_at_fn would coerce numeric
         * vec + numeric idx vec into a RAY_LIST of atoms, breaking the
         * type-dispatch in exec_count_distinct. */
        ray_t* subset = gather_by_idx(src,
            (int64_t*)&idx_buf[offsets[gi]], cnt);
        if (!subset || RAY_IS_ERR(subset)) {
            ray_t* err = subset ? subset : ray_error("oom", NULL);
            ray_release(src); ray_release(out);
            return err;
        }
        ray_t* cv = exec_count_distinct(NULL, NULL, subset);
        ray_release(subset);
        if (!cv || RAY_IS_ERR(cv)) {
            ray_t* err = cv ? cv : ray_error("oom", NULL);
            ray_release(src); ray_release(out);
            return err;
        }
        /* exec_count_distinct returns an i64 atom. */
        odata[gi] = (cv->type == -RAY_I64) ? cv->i64
                  : (cv->type == -RAY_I32) ? (int64_t)cv->i32 : 0;
        ray_release(cv);
    }

    ray_release(src);
    return out;
}

/* Variant for the LIST-`groups` layout used by the eval-fallback
 * (ray_group_indices_fn output is a 2-list of {key, idx_list} pairs).  Slices
 * via ray_at_fn the same way and dispatches to exec_count_distinct. */
static ray_t* count_distinct_per_group_groups(ray_t* inner_expr, ray_t* tbl,
                                              ray_t* groups, int64_t n_groups) {
    {
    if (!groups || groups->type != RAY_LIST || n_groups < 0)
        return ray_error("type", "count distinct: expected a groups list, got %s", groups ? ray_type_name(groups->type) : "null");
    ray_t** items0 = (ray_t**)ray_data(groups);
    int64_t total = 0;
    for (int64_t gi = 0; gi < n_groups; gi++) {
        ray_t* idx_list = items0[gi * 2 + 1];
        total += idx_list ? ray_len(idx_list) : 0;
    }
    ray_t *idx_hdr = NULL, *off_hdr = NULL, *cnt_hdr = NULL;
    int64_t* idx_buf = (int64_t*)scratch_alloc(&idx_hdr,
        (size_t)(total > 0 ? total : 1) * sizeof(int64_t));
    int64_t* offsets = (int64_t*)scratch_alloc(&off_hdr,
        (size_t)(n_groups > 0 ? n_groups : 1) * sizeof(int64_t));
    int64_t* counts = (int64_t*)scratch_alloc(&cnt_hdr,
        (size_t)(n_groups > 0 ? n_groups : 1) * sizeof(int64_t));
    if (!idx_buf || !offsets || !counts) {
        if (idx_hdr) scratch_free(idx_hdr);
        if (off_hdr) scratch_free(off_hdr);
        if (cnt_hdr) scratch_free(cnt_hdr);
        return ray_error("oom", NULL);
    }
    int64_t pos = 0;
    for (int64_t gi = 0; gi < n_groups; gi++) {
        ray_t* idx_list = items0[gi * 2 + 1];
        int64_t cnt = idx_list ? ray_len(idx_list) : 0;
        offsets[gi] = pos;
        counts[gi] = cnt;
        if (cnt > 0) {
            if (idx_list->type == RAY_I64) {
                memcpy(idx_buf + pos, ray_data(idx_list),
                       (size_t)cnt * sizeof(int64_t));
            } else {
                for (int64_t k = 0; k < cnt; k++) {
                    int alloc = 0;
                    ray_t* e = collection_elem(idx_list, k, &alloc);
                    idx_buf[pos + k] = e ? as_i64(e) : 0;
                    if (alloc && e) ray_release(e);
                }
            }
        }
        pos += cnt;
    }
    ray_t* out = count_distinct_per_group_buf(
        inner_expr, tbl, idx_buf, offsets, counts, n_groups);
    scratch_free(idx_hdr);
    scratch_free(off_hdr);
    scratch_free(cnt_hdr);
    return out;
    }

    ray_t* src = NULL;
    if (inner_expr && inner_expr->type == -RAY_SYM &&
        !(inner_expr->attrs & ATTR_QUOTED)) {
        src = ray_table_get_col(tbl, inner_expr->i64);
        if (src) ray_retain(src);
    }
    if (!src) {
        if (ray_env_push_query_scope() != RAY_OK) return ray_error("oom", NULL);
        ray_t* _aqt = bind_all_columns(tbl);
        src = ray_eval(inner_expr);
        g_active_query_table = _aqt;
        ray_env_pop_scope();
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("domain", "select by: failed to evaluate count-distinct source");
    }
    if (src && !RAY_IS_ERR(src) && (RAY_IS_PARTED(src->type) || src->type == RAY_MAPCOMMON)) {
        ray_t* flat = query_materialize_parted_col(src);
        ray_release(src);
        src = flat;
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("oom", NULL);
    }

    ray_t* out = ray_vec_new(RAY_I64, n_groups);
    if (!out || RAY_IS_ERR(out)) { ray_release(src); return out ? out : ray_error("oom", NULL); }
    out->len = n_groups;
    int64_t* odata = (int64_t*)ray_data(out);

    ray_t** items = (ray_t**)ray_data(groups);
    for (int64_t gi = 0; gi < n_groups; gi++) {
        ray_t* idx_list = items[gi * 2 + 1];
        if (!idx_list) { odata[gi] = 0; continue; }
        int64_t cnt = ray_len(idx_list);
        if (cnt == 0) { odata[gi] = 0; continue; }

        /* idx_list from ray_group_indices_fn is an I64 vector — gather_by_idx
         * needs a raw int64_t* + count, so resolve the pointer either
         * directly (typed I64 vec) or by walking the LIST cells. */
        ray_t* subset = NULL;
        ray_t* tmp_hdr = NULL;
        if (idx_list->type == RAY_I64) {
            subset = gather_by_idx(src, (int64_t*)ray_data(idx_list), cnt);
        } else {
            /* Fallback: copy indices into a scratch buffer.  Rare path —
             * shouldn't trigger for well-formed ray_group_indices_fn output. */
            int64_t* tmp = (int64_t*)scratch_alloc(&tmp_hdr,
                (size_t)cnt * sizeof(int64_t));
            if (!tmp) {
                ray_release(src); ray_release(out);
                return ray_error("oom", NULL);
            }
            for (int64_t k = 0; k < cnt; k++) {
                int alloc = 0;
                ray_t* e = collection_elem(idx_list, k, &alloc);
                tmp[k] = e ? as_i64(e) : 0;
                if (alloc && e) ray_release(e);
            }
            subset = gather_by_idx(src, tmp, cnt);
            scratch_free(tmp_hdr);
        }
        if (!subset || RAY_IS_ERR(subset)) {
            ray_t* err = subset ? subset : ray_error("oom", NULL);
            ray_release(src); ray_release(out);
            return err;
        }
        ray_t* cv = exec_count_distinct(NULL, NULL, subset);
        ray_release(subset);
        if (!cv || RAY_IS_ERR(cv)) {
            ray_t* err = cv ? cv : ray_error("oom", NULL);
            ray_release(src); ray_release(out);
            return err;
        }
        odata[gi] = (cv->type == -RAY_I64) ? cv->i64
                  : (cv->type == -RAY_I32) ? (int64_t)cv->i32 : 0;
        ray_release(cv);
    }

    ray_release(src);
    return out;
}

/* Width-agnostic key reader: read row `idx` of a group-key column as
 * int64_t.  Same coverage as the KEY_READ macro inside ray_select_fn,
 * lifted to file scope so the parallel row→gid probe worker can use it. */
static inline int64_t key_read_i64(const void* d, int64_t idx,
                                   int8_t bt, uint8_t attrs) {
    switch (bt) {
    case RAY_BOOL:
    case RAY_U8:        return ((const uint8_t*)d)[idx];
    case RAY_I16:       return ((const int16_t*)d)[idx];
    case RAY_I32:
    case RAY_DATE:
    case RAY_TIME:      return ((const int32_t*)d)[idx];
    case RAY_I64:
    case RAY_TIMESTAMP: return ((const int64_t*)d)[idx];
    case RAY_F32: { uint32_t u;
        memcpy(&u, &((const float*)d)[idx], 4);
        return (int64_t)u; }
    case RAY_F64: { int64_t u;
        memcpy(&u, &((const double*)d)[idx], 8);
        return u; }
    /* SYM: raw index — callers compare cells of the source column
     * against cells of a group-key column DERIVED from it (same
     * domain by construction), never against foreign-domain ids. */
    case RAY_SYM:       return ray_read_sym(d, idx, bt, attrs);
    default:            return 0; /* caller validates type */
    }
}

/* Parallel row→gid probe.  Hash table is read-only by the time the probe
 * runs (the insert phase that built it is single-threaded), so each
 * worker can process its row range independently with no synchronisation.
 *
 * The probe's per-row work is one cache-cold load + a short linear-probe
 * walk in a hash sized to 2 × n_groups.  At Q14 scale (611 K groups,
 * ~18 MB hash) the serial loop spends most of its time waiting on cache
 * misses; spreading the rows across 28 cores gives near-linear speedup
 * because each core has its own cache hierarchy. */
typedef struct {
    /* Hash table contents (read-only). */
    const int64_t* hk_keys;
    const int32_t* hk_gid_p1;     /* one of these is non-NULL */
    const int64_t* hk_gid64;
    uint64_t       mask;
    /* Group-key column being probed. */
    const void* orig_key_data;
    int8_t      okt;
    uint8_t     okt_attrs;
    /* Per-row output. */
    int64_t* row_gid;
    /* Optional selection — when non-NULL, skip rows that don't pass.
     * Lets row_gid build and selection mask happen in one parallel pass
     * (saves the serial sel_mask loop downstream).  Both can be NULL
     * (full-table probe). */
    ray_t*          selection;
    const uint8_t*  sel_flg;
    const uint32_t* sel_offs;
    const uint16_t* sel_idx;
    uint32_t        sel_n_segs;
} rgid_probe_ctx_t;

static void rgid_probe_fn(void* ctx_, uint32_t worker_id,
                          int64_t start, int64_t end) {
    (void)worker_id;
    rgid_probe_ctx_t* x = (rgid_probe_ctx_t*)ctx_;
    int use_i64 = (x->hk_gid64 != NULL);
    uint64_t mask = x->mask;

    /* Fused selection-aware probe: when a rowsel is attached, walk per
     * morsel segment and only do the HT probe for rows that actually
     * passed the WHERE filter.  Filtered-out rows write -1 directly,
     * skipping the (potentially expensive) hash + linear-probe lookup.
     *
     * This eliminates the separate serial sel_mask pass downstream that
     * was costing ~3 ms on Q11 (5 M rows / 4880 segments scalar walk). */
    if (x->selection) {
        const uint8_t*  flg  = x->sel_flg;
        const uint32_t* offs = x->sel_offs;
        const uint16_t* lidx = x->sel_idx;
        uint32_t seg_lo = (uint32_t)(start / RAY_MORSEL_ELEMS);
        uint32_t seg_hi = (uint32_t)((end + RAY_MORSEL_ELEMS - 1) / RAY_MORSEL_ELEMS);
        if (seg_hi > x->sel_n_segs) seg_hi = x->sel_n_segs;
        for (uint32_t seg = seg_lo; seg < seg_hi; seg++) {
            int64_t s_lo = (int64_t)seg * RAY_MORSEL_ELEMS;
            int64_t s_hi = s_lo + RAY_MORSEL_ELEMS;
            if (s_lo < start) s_lo = start;
            if (s_hi > end)   s_hi = end;
            uint8_t f = flg[seg];
            if (f == RAY_SEL_NONE) {
                for (int64_t r = s_lo; r < s_hi; r++) x->row_gid[r] = -1;
                continue;
            }
            if (f == RAY_SEL_ALL) {
                for (int64_t r = s_lo; r < s_hi; r++) {
                    int64_t rv = key_read_i64(x->orig_key_data, r, x->okt, x->okt_attrs);
                    uint64_t h = (uint64_t)rv * 0x9E3779B97F4A7C15ULL;
                    h ^= h >> 33;
                    uint64_t sl = h & mask;
                    int64_t found = -1;
                    for (;;) {
                        int64_t cur_p1 = use_i64 ? x->hk_gid64[sl]
                                                 : (int64_t)x->hk_gid_p1[sl];
                        if (cur_p1 == 0) break;
                        if (x->hk_keys[sl] == rv) { found = cur_p1 - 1; break; }
                        sl = (sl + 1) & mask;
                    }
                    x->row_gid[r] = found;
                }
                continue;
            }
            /* RAY_SEL_MIX: build in-segment bitmap, then probe + mask. */
            uint8_t in_seg[RAY_MORSEL_ELEMS / 8] = {0};
            uint32_t off = offs[seg];
            uint32_t cnt = offs[seg + 1] - off;
            for (uint32_t i = 0; i < cnt; i++) {
                uint16_t loc = lidx[off + i];
                in_seg[loc >> 3] |= (uint8_t)(1u << (loc & 7));
            }
            int64_t base = (int64_t)seg * RAY_MORSEL_ELEMS;
            for (int64_t r = s_lo; r < s_hi; r++) {
                uint16_t loc = (uint16_t)(r - base);
                if (!(in_seg[loc >> 3] & (1u << (loc & 7)))) {
                    x->row_gid[r] = -1;
                    continue;
                }
                int64_t rv = key_read_i64(x->orig_key_data, r, x->okt, x->okt_attrs);
                uint64_t h = (uint64_t)rv * 0x9E3779B97F4A7C15ULL;
                h ^= h >> 33;
                uint64_t sl = h & mask;
                int64_t found = -1;
                for (;;) {
                    int64_t cur_p1 = use_i64 ? x->hk_gid64[sl]
                                             : (int64_t)x->hk_gid_p1[sl];
                    if (cur_p1 == 0) break;
                    if (x->hk_keys[sl] == rv) { found = cur_p1 - 1; break; }
                    sl = (sl + 1) & mask;
                }
                x->row_gid[r] = found;
            }
        }
        return;
    }

    for (int64_t r = start; r < end; r++) {
        int64_t rv = key_read_i64(x->orig_key_data, r, x->okt, x->okt_attrs);
        uint64_t h = (uint64_t)rv * 0x9E3779B97F4A7C15ULL;
        h ^= h >> 33;
        uint64_t s = h & mask;
        int64_t found = -1;
        for (;;) {
            int64_t cur_p1 = use_i64 ? x->hk_gid64[s] : (int64_t)x->hk_gid_p1[s];
            if (cur_p1 == 0) break;
            if (x->hk_keys[s] == rv) { found = cur_p1 - 1; break; }
            s = (s + 1) & mask;
        }
        x->row_gid[r] = found;
    }
}

/* STR-key sibling of rgid_probe_fn: probe each row's STR descriptor against
 * the read-only group-key slot table to assign row_gid[r].  The int path
 * above was parallel but the STR path was a serial 10M-row loop — the
 * dominant cost of STR group + count(distinct) (q13).  The slot table,
 * descriptors and pools are read-only here and row_gid writes are disjoint
 * per row, so workers need no synchronisation.  No selection handling: this
 * mirrors the serial STR loop, which probes the full row range. */
typedef struct {
    const int32_t*   slot_gid_p1;   /* group-key slot table (gid+1; 0=empty) */
    uint64_t         mask;
    const ray_str_t* gdesc;         /* group-key descriptors */
    const char*      gpool;
    const ray_str_t* sdesc;         /* row (scan) descriptors */
    const char*      spool;
    int64_t*         row_gid;       /* per-row output */
} rgid_probe_str_ctx_t;

/* Parallel dict-code row_gid gather (disjoint writes, no sync). */
typedef struct { const int32_t* scan_codes; const int32_t* code_to_gid; int64_t n_distinct; int64_t* row_gid; } dictgid_ctx_t;
static void dictgid_fn(void* ctx_, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    dictgid_ctx_t* c = (dictgid_ctx_t*)ctx_;
    for (int64_t r = start; r < end; r++) {
        int32_t cc = c->scan_codes[r];
        c->row_gid[r] = (cc >= 0 && cc < c->n_distinct) ? c->code_to_gid[cc] : -1;
    }
}

static void rgid_probe_str_fn(void* ctx_, uint32_t worker_id,
                              int64_t start, int64_t end) {
    (void)worker_id;
    rgid_probe_str_ctx_t* x = (rgid_probe_str_ctx_t*)ctx_;
    uint64_t mask = x->mask;
    for (int64_t r = start; r < end; r++) {
        uint64_t h = ray_str_t_hash(&x->sdesc[r], x->spool);
        uint64_t s = h & mask;
        int64_t found = -1;
        for (;;) {
            int32_t g_p1 = x->slot_gid_p1[s];
            if (g_p1 == 0) break;
            int32_t g = g_p1 - 1;
            if (ray_str_t_eq(&x->gdesc[g], x->gpool, &x->sdesc[r], x->spool)) {
                found = g; break;
            }
            s = (s + 1) & mask;
        }
        x->row_gid[r] = found;
    }
}

/* Forward declarations for eval-level groupby fallback */

/* R8: cheap predicate for whether atom_broadcast_vec can handle this
 * atom AND the atom is a self-evaluating literal (not a name binding
 * that needs ray_eval to resolve to a column or computed value).  Used
 * by the all-literal pre-check so we don't half-apply a partial set of
 * broadcasts and then have to roll back.
 *
 * A name reference (unflagged -RAY_SYM, ATTR_QUOTED clear) distinguishes
 * `m2: m` (the SYM `m` references a column) from `one: 1` (the I64 literal
 * 1) and from `lit: 'm` (a quoted/literal symbol).  Without that filter we'd
 * eagerly broadcast the column reference and skip the per-group gather
 * the chained passthrough relies on. */
static int can_atom_broadcast(ray_t* a) {
    if (!a || !ray_is_atom(a)) return 0;
    if (a->type == -RAY_SYM && !(a->attrs & ATTR_QUOTED)) return 0;
    int8_t vt = (int8_t)(-a->type);
    switch (vt) {
    case RAY_BOOL: case RAY_U8:
    case RAY_I16:  case RAY_I32:
    case RAY_I64: case RAY_F32: case RAY_F64: case RAY_GUID: case RAY_STR:
    case RAY_DATE: case RAY_TIME: case RAY_TIMESTAMP:
    case RAY_SYM:
        return 1;
    default:
        return 0;
    }
}

/* R8: build a typed N-cell vector all containing the value of atom `a`.
 *
 * The non-agg scatter path used to bind a `{lit: 1, c: count(...) by: K}`
 * style query into a per-group RAY_LIST of N retained atoms, which
 * ballooned Q35 from ~21 ms to ~140 ms (one ray_retain + list slot
 * per group, scaling with output cardinality, not row count).  Allocate
 * once and fill — Q35 falls back into parity with Q34.
 *
 * Returns NULL for unsupported atom kinds;
 * caller falls back to the per-cell LIST path. */
static ray_t* atom_broadcast_vec(ray_t* a, int64_t n) {
    /* n == 0 is a legitimate request, not a failure: a grouped select that
     * matched no row still owes its caller a typed, empty column for every
     * literal projection.  Returning NULL here made the sole call site
     * (which treats NULL as allocation failure) report `oom` for an
     * ordinary `select {by: k where: <no match> lit: 1 ...}`. */
    if (!a || !ray_is_atom(a) || n < 0) return NULL;
    int8_t vec_type = (int8_t)(-a->type);
    if (vec_type <= 0) return NULL;

    /* SYM atoms produced by ray_sym(id) carry no width attr (always 0 →
     * W8), so we can't trust a->attrs when the id exceeds one byte.
     * Pick the narrowest width that fits a->i64. */
    uint8_t sym_w = 0;
    if (vec_type == RAY_SYM) {
        uint64_t id = (uint64_t)a->i64;
        sym_w = id <= 0xFFu        ? RAY_SYM_W8
              : id <= 0xFFFFu      ? RAY_SYM_W16
              : id <= 0xFFFFFFFFu  ? RAY_SYM_W32
                                   : RAY_SYM_W64;
    }
    ray_t* v;
    if (vec_type == RAY_SYM) {
        v = ray_sym_vec_new(sym_w, n);
    } else {
        v = ray_vec_new(vec_type, n);
    }
    if (!v || RAY_IS_ERR(v)) return NULL;
    v->len = n;
    if (n == 0) return v;   /* typed and empty; nothing to broadcast into */

    void* dst = ray_data(v);
    switch (vec_type) {
    case RAY_BOOL:
    case RAY_U8: {
        memset(dst, a->b8, (size_t)n);
        break;
    }
    case RAY_I16: {
        int16_t val = a->i16;
        int16_t* d = (int16_t*)dst;
        for (int64_t i = 0; i < n; i++) d[i] = val;
        break;
    }
    case RAY_I32:
    case RAY_DATE:
    case RAY_TIME: {
        int32_t val = a->i32;
        int32_t* d = (int32_t*)dst;
        for (int64_t i = 0; i < n; i++) d[i] = val;
        break;
    }
    case RAY_I64:
    case RAY_TIMESTAMP: {
        int64_t val = a->i64;
        int64_t* d = (int64_t*)dst;
        for (int64_t i = 0; i < n; i++) d[i] = val;
        break;
    }
    case RAY_F32: {
        float* d = dst;
        for (int64_t i = 0; i < n; i++) d[i] = (float)a->f64;
        break;
    }
    case RAY_GUID:
        for (int64_t i = 0; i < n; i++)
            memcpy((char*)dst + (size_t)i * 16, ray_data(a->obj), 16);
        break;
    case RAY_STR:
        for (int64_t i = 0; i < n; i++) {
            ray_t* next = ray_str_vec_set(v, i, ray_str_ptr(a), ray_str_len(a));
            if (!next || RAY_IS_ERR(next)) { ray_release(v); return NULL; }
            v = next;
        }
        break;
    case RAY_F64: {
        double val = a->f64;
        double* d = (double*)dst;
        for (int64_t i = 0; i < n; i++) d[i] = val;
        break;
    }
    case RAY_SYM: {
        /* Width was selected above to fit a->i64, not read from a->attrs
         * (atom-built syms never set the width attr). */
        if (sym_w == RAY_SYM_W8) {
            memset(dst, (uint8_t)a->i64, (size_t)n);
        } else if (sym_w == RAY_SYM_W16) {
            uint16_t val = (uint16_t)a->i64;
            uint16_t* d = (uint16_t*)dst;
            for (int64_t i = 0; i < n; i++) d[i] = val;
        } else if (sym_w == RAY_SYM_W32) {
            uint32_t val = (uint32_t)a->i64;
            uint32_t* d = (uint32_t*)dst;
            for (int64_t i = 0; i < n; i++) d[i] = val;
        } else { /* W64 */
            int64_t val = a->i64;
            int64_t* d = (int64_t*)dst;
            for (int64_t i = 0; i < n; i++) d[i] = val;
        }
        break;
    }
    default:
        ray_release(v);
        return NULL;
    }

    /* Propagate atom-null: an entirely-null broadcast keeps the null bit
     * of every cell so `is_null` and aggregations retain their semantics.
     * Payloads already contain typed sentinels; aux belongs to owners/domains. */
    if (RAY_ATOM_IS_NULL(a)) {
        v->attrs |= RAY_ATTR_HAS_NULLS;

    }
    return v;
}

static ray_t* scalar_result_col(ray_t* value) {
    if (!value || RAY_IS_ERR(value)) return value ? value : ray_error("domain", "select: scalar aggregate produced no result");

    if (ray_is_atom(value)) {
        ray_t* col = atom_broadcast_vec(value, 1);
        if (col) return col;

        ray_t* boxed = ray_alloc(sizeof(ray_t*));
        if (!boxed) return ray_error("oom", NULL);
        boxed->type = RAY_LIST;
        boxed->len = 1;
        ((ray_t**)ray_data(boxed))[0] = value;
        ray_retain(value);
        return boxed;
    }

    if (ray_len(value) != 1)
        return ray_error("length", "select: scalar aggregate produced %lld rows", (long long)ray_len(value));
    ray_retain(value);
    return value;
}

static ray_t* eval_scalar_agg_outputs(ray_t** dict_elems, int64_t dict_n,
                                      ray_t* tbl,
                                      int64_t from_id, int64_t where_id,
                                      int64_t by_id, int64_t take_id,
                                      int64_t asc_id, int64_t desc_id,
                                      int64_t nearest_id) {
    ray_t* result = ray_table_new(0);
    if (!result || RAY_IS_ERR(result))
        return result ? result : ray_error("oom", NULL);

    int64_t nrows = ray_table_nrows(tbl);
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id ||
            kid == nearest_id) continue;

        ray_t* val_expr = dict_elems[i + 1];
        if (!is_agg_expr(val_expr)) {
            ray_release(result);
            return ray_error("domain", "select: scalar aggregate fallback saw non-aggregate output");
        }

        ray_t** agg_elems = (ray_t**)ray_data(val_expr);
        if (ray_len(val_expr) != 2) {
            ray_release(result);
            return ray_error("domain", "select: scalar aggregate fallback only supports unary aggregates");
        }

        ray_t* fn_obj = ray_env_get(agg_elems[0]->i64);
        if (!fn_obj || fn_obj->type != RAY_UNARY) {
            ray_release(result);
            return ray_error("type", "select: aggregate must be a unary function, got %s",
                             fn_obj ? ray_type_name(fn_obj->type) : "null");
        }

        /* A whole-column verb (distinct/asc/desc/reverse) at the head of the
         * aggregate's argument consumes the entire column — per-row scatter
         * would feed it one scalar cell at a time, making `count` report the
         * row count instead of the distinct count (issue #405).  Mirror the
         * projection fallback's routing and evaluate it once. */
        ray_t* src = is_whole_column_projection(agg_elems[1])
                   ? eval_expr_whole_column(agg_elems[1], tbl)
                   : eval_expr_per_row(agg_elems[1], tbl, nrows);
        if (!src || RAY_IS_ERR(src)) {
            ray_release(result);
            return src ? src : ray_error("domain", "select: failed to evaluate aggregation source");
        }

        ray_unary_fn uf = (ray_unary_fn)(uintptr_t)fn_obj->i64;
        ray_t* agg_value = uf(src);
        ray_release(src);
        if (agg_value && !RAY_IS_ERR(agg_value) && ray_is_lazy(agg_value))
            agg_value = ray_lazy_materialize(agg_value);
        if (!agg_value || RAY_IS_ERR(agg_value)) {
            ray_release(result);
            return agg_value ? agg_value : ray_error("domain", "select: scalar aggregate evaluation failed");
        }

        ray_t* col = scalar_result_col(agg_value);
        ray_release(agg_value);
        if (!col || RAY_IS_ERR(col)) {
            ray_release(result);
            return col ? col : ray_error("domain", "select: failed to materialize scalar aggregate");
        }
        result = ray_table_add_col(result, kid, col);
        ray_release(col);
        if (!result || RAY_IS_ERR(result))
            return result ? result : ray_error("oom", NULL);
    }

    return result;
}

/* (select {from: t [where: pred] [by: key] [col: expr ...]})
 * Special form — receives unevaluated dict arg. */
ray_t* ray_select(ray_t** args, int64_t n);
static ray_t* ray_select_impl(ray_t** args, int64_t n, bool aliases_resolved);
ray_t* ray_update(ray_t** args, int64_t n);
ray_t* ray_insert(ray_t** args, int64_t n);
ray_t* ray_upsert(ray_t** args, int64_t n);

/* Streaming parted ORDER BY (defined below, near the asof/segment helpers):
 * concat pre-sorted partitions instead of flatten+sort.  Returns an owned
 * result on the eligible path, or NULL to decline to the flat path. */
static ray_t* try_stream_parted_order_by(
    ray_t* dict, ray_t* tbl, ray_t** dict_elems, int64_t dict_n,
    int64_t from_id, int64_t where_id, int64_t by_id, int64_t take_id,
    int64_t asc_id, int64_t desc_id, int64_t nearest_id);

static ray_t* try_stream_parted_order_by_topk(
    ray_t* dict, ray_t* tbl, ray_t** dict_elems, int64_t dict_n,
    int64_t from_id, int64_t where_id, int64_t by_id, int64_t take_id,
    int64_t asc_id, int64_t desc_id, int64_t nearest_id);

ray_t* ray_select_fn(ray_t** args, int64_t n) {
    return ray_select(args, n);
}

/* Map a window function name (C string of given length) to a RAY_WIN_* kind.
 * Only the 7 spec-exposed names are reachable: min/max/sum/avg/count/first/last.
 * Returns 255 on no match. */
static uint8_t win_kind_from_name(const char* s, size_t len) {
    switch (len) {
    case 3:
        if (memcmp(s, "sum", 3) == 0) return RAY_WIN_SUM;
        if (memcmp(s, "avg", 3) == 0) return RAY_WIN_AVG;
        if (memcmp(s, "min", 3) == 0) return RAY_WIN_MIN;
        if (memcmp(s, "max", 3) == 0) return RAY_WIN_MAX;
        break;
    case 4:
        if (memcmp(s, "last", 4) == 0) return RAY_WIN_LAST_VALUE;
        break;
    case 5:
        if (memcmp(s, "count", 5) == 0) return RAY_WIN_COUNT;
        if (memcmp(s, "first", 5) == 0) return RAY_WIN_FIRST_VALUE;
        break;
    default: break;
    }
    return 255;
}

/*
 * (window {from: T part: [col …] order: [col …] frame: 'whole|'running|N
 *          funcs: {outname: (fn inputcol) …}})
 *
 * Builds a DAG OP_WINDOW node over the table T, executes it, then renames the
 * auto-generated _w0/_w1/… output columns to the user-supplied names.
 *
 * Special form: args[0] is the unevaluated dict literal AST.
 * `from:` is the only clause that requires full eval (it's a variable ref).
 * `part:`/`order:` arrive as RAY_SYM vectors or -RAY_SYM atoms (column refs).
 * `frame:` is either a quoted -RAY_SYM atom ('whole or 'running), or a
 * positive integer N for a fixed trailing N-row frame including the current
 * row.
 * `funcs:` is a nested RAY_DICT whose values are list expressions (fn col).
 */
ray_t* ray_window_fn(ray_t** args, int64_t n) {
    if (n < 1)
        return ray_error("arity", "window: expects a query dict, got %lld args", (long long)n);
    ray_t* dict = args[0];
    if (!dict || dict->type != RAY_DICT)
        return ray_error("type", "window: argument must be a dict, got %s",
                         dict ? ray_type_name(dict->type) : "null");

    /* ── from: ── */
    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr)
        return ray_error("domain", "window: missing `from:` clause");
    ray_t* tbl = ray_eval(from_expr);
    if (!tbl || RAY_IS_ERR(tbl))
        return tbl ? tbl : ray_error("domain", "window: `from:` evaluation failed");
    if (tbl->type != RAY_TABLE) {
        int8_t t = tbl->type; ray_release(tbl);
        return ray_error("type", "window: `from:` must evaluate to a table, got %s",
                         ray_type_name(t));
    }

    /* ── funcs: (required) ── */
    ray_t* funcs_expr = dict_get(dict, "funcs");
    if (!funcs_expr || funcs_expr->type != RAY_DICT) {
        ray_release(tbl);
        return ray_error("domain", "window: missing or invalid `funcs:` clause (must be a dict)");
    }
    DICT_VIEW_DECL(fv);
    DICT_VIEW_OPEN(funcs_expr, fv);
    if (DICT_VIEW_OVERFLOW(fv)) {
        DICT_VIEW_CLOSE(fv); ray_release(tbl);
        return ray_error("oom", NULL);
    }
    int n_funcs = (int)(fv_n / 2);
    if (n_funcs == 0) {
        DICT_VIEW_CLOSE(fv); ray_release(tbl);
        return ray_error("domain", "window: `funcs:` must have at least 1 entry, got %d", n_funcs);
    }
    /* ── frame: ── */
    ray_t* frame_expr = dict_get(dict, "frame");
    uint8_t frame_start = RAY_BOUND_UNBOUNDED_PRECEDING;
    uint8_t frame_end = RAY_BOUND_UNBOUNDED_FOLLOWING;  /* default: whole partition */
    int64_t frame_start_n = 0;
    if (frame_expr) {
        if (frame_expr->type == -RAY_SYM) {
            ray_t* fs = ray_sym_str(frame_expr->i64);
            if (fs) {
                const char* fsp = ray_str_ptr(fs);
                size_t      fsl = ray_str_len(fs);
                if (fsl == 7 && memcmp(fsp, "running", 7) == 0)
                    frame_end = RAY_BOUND_CURRENT_ROW;
                else if (!(fsl == 5 && memcmp(fsp, "whole", 5) == 0)) {
                    DICT_VIEW_CLOSE(fv); ray_release(tbl);
                    return ray_error("domain", "window: frame must be 'whole, 'running, or a positive row count");
                }
            }
        } else {
            int64_t width = 0;
            switch (frame_expr->type) {
                case -RAY_I64: width = frame_expr->i64; break;
                case -RAY_I32: width = frame_expr->i32; break;
                case -RAY_I16: width = frame_expr->i16; break;
                case -RAY_U8:
                case -RAY_BOOL: width = frame_expr->u8; break;
                default:
                    DICT_VIEW_CLOSE(fv); ray_release(tbl);
                    return ray_error("domain", "window: frame must be 'whole, 'running, or a positive row count");
            }
            if (width < 1) {
                DICT_VIEW_CLOSE(fv); ray_release(tbl);
                return ray_error("range", "window: trailing frame row count must be >= 1");
            }
            frame_start = RAY_BOUND_N_PRECEDING;
            frame_end = RAY_BOUND_CURRENT_ROW;
            frame_start_n = width - 1;
        }
    }

    /* ── Build DAG ── */
    ray_graph_t* g = ray_graph_new(tbl);
    if (!g) { DICT_VIEW_CLOSE(fv); ray_release(tbl); return ray_error("oom", NULL); }
    ray_op_t* tbl_node = ray_const_table(g, tbl);

    /* part: – SYM vector or atom naming partition columns. Exact-size scratch
     * carve replaces the fixed part_ops[16] stack array; bound is 1 for an
     * atom column ref, else the SYM vector's length. */
    ray_t* pe = dict_get(dict, "part");
    int64_t n_part_max = (pe && pe->type == RAY_SYM) ? pe->len : 1;
    if (n_part_max < 1) n_part_max = 1;
    ray_t* part_hdr = NULL;
    ray_op_t** part_ops = (ray_op_t**)scratch_alloc(&part_hdr, (size_t)n_part_max * sizeof(ray_op_t*));
    if (!part_ops) { DICT_VIEW_CLOSE(fv); ray_graph_free(g); ray_release(tbl); return ray_error("oom", NULL); }
    int64_t n_part = 0;
    if (pe) {
        if (pe->type == -RAY_SYM && !(pe->attrs & ATTR_QUOTED)) {
            ray_t* s = ray_sym_str(pe->i64);
            if (s) part_ops[n_part++] = ray_scan(g, ray_str_ptr(s));
        } else if (pe->type == RAY_SYM) {
            for (int64_t i = 0; i < pe->len; i++) {
                int64_t sid = sym_cell_runtime_id(pe, i);
                ray_t*  s   = ray_sym_str(sid);
                if (s) part_ops[n_part++] = ray_scan(g, ray_str_ptr(s));
            }
        }
    }
    /* order: – SYM vector or atom naming sort columns (all ascending).
     * One carve holds both order_ops (pointers) and order_descs (bytes,
     * trailing), sized the same way as part_ops. */
    ray_t* oe = dict_get(dict, "order");
    int64_t n_order_max = (oe && oe->type == RAY_SYM) ? oe->len : 1;
    if (n_order_max < 1) n_order_max = 1;
    ray_t* order_hdr = NULL;
    ray_op_t** order_ops = (ray_op_t**)scratch_alloc(&order_hdr,
            (size_t)n_order_max * (sizeof(ray_op_t*) + 1));
    if (!order_ops) {
        DICT_VIEW_CLOSE(fv); scratch_free(part_hdr); ray_graph_free(g); ray_release(tbl);
        return ray_error("oom", NULL);
    }
    uint8_t* order_descs = (uint8_t*)(order_ops + n_order_max);
    memset(order_descs, 0, (size_t)n_order_max);
    int64_t n_order = 0;
    if (oe) {
        if (oe->type == -RAY_SYM && !(oe->attrs & ATTR_QUOTED)) {
            ray_t* s = ray_sym_str(oe->i64);
            if (s) order_ops[n_order++] = ray_scan(g, ray_str_ptr(s));
        } else if (oe->type == RAY_SYM) {
            for (int64_t i = 0; i < oe->len; i++) {
                int64_t sid = sym_cell_runtime_id(oe, i);
                ray_t*  s   = ray_sym_str(sid);
                if (s) order_ops[n_order++] = ray_scan(g, ray_str_ptr(s));
            }
        }
    }
    /* Parse each funcs: entry: key = output name atom, value = (fn input_col)
     * list. One carve holds func_inputs (pointers) + func_params (int64_t) +
     * func_kinds (bytes, trailing) sized to n_funcs — all three are only
     * needed through the ray_window_op call below, so they're freed right
     * after it. out_name_ids gets its own carve: it's read again after
     * ray_execute() to rename the output columns, so it must outlive the
     * other three (freed at the very end of the function instead). */
    ray_t* funcs_hdr = NULL;
    size_t func_ptr_sz = (size_t)n_funcs * sizeof(ray_op_t*);
    size_t func_i64_sz = (size_t)n_funcs * sizeof(int64_t);
    void*  func_scratch = scratch_alloc(&funcs_hdr, func_ptr_sz + func_i64_sz + (size_t)n_funcs);
    if (!func_scratch) {
        DICT_VIEW_CLOSE(fv); scratch_free(order_hdr); scratch_free(part_hdr); ray_graph_free(g); ray_release(tbl);
        return ray_error("oom", NULL);
    }
    ray_op_t** func_inputs  = (ray_op_t**)func_scratch;
    int64_t*   func_params  = (int64_t*)((char*)func_scratch + func_ptr_sz);
    uint8_t*   func_kinds   = (uint8_t*)((char*)func_scratch + func_ptr_sz + func_i64_sz);

    ray_t* outname_hdr = NULL;
    int64_t* out_name_ids = (int64_t*)scratch_alloc(&outname_hdr, (size_t)n_funcs * sizeof(int64_t));
    if (!out_name_ids) {
        DICT_VIEW_CLOSE(fv); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
        ray_graph_free(g); ray_release(tbl);
        return ray_error("oom", NULL);
    }

    for (int fi = 0; fi < n_funcs; fi++) {
        ray_t* key_atom = fv[fi * 2];
        ray_t* val_expr = fv[fi * 2 + 1];

        out_name_ids[fi] = (key_atom && key_atom->type == -RAY_SYM) ? key_atom->i64 : -1;

        /* val_expr must be a list: (fn_name input_col [param]) */
        if (!val_expr || val_expr->type != RAY_LIST || ray_len(val_expr) < 2) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain",
                "window: `funcs:` value must be a function call like (min v), got wrong shape at entry %d",
                fi);
        }
        ray_t** fel       = (ray_t**)ray_data(val_expr);
        ray_t*  fn_expr   = fel[0];
        ray_t*  col_expr  = fel[1];

        /* Function name → RAY_WIN_* kind */
        if (!fn_expr || fn_expr->type != -RAY_SYM) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", "window: function name must be a symbol");
        }
        ray_t* fn_s = ray_sym_str(fn_expr->i64);
        if (!fn_s) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", "window: unknown function");
        }
        uint8_t kind = win_kind_from_name(ray_str_ptr(fn_s), ray_str_len(fn_s));
        if (kind == 255) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", "window: unsupported window function '%s'", ray_str_ptr(fn_s));
        }
        if ((kind == RAY_WIN_FIRST_VALUE || kind == RAY_WIN_LAST_VALUE) && n_order == 0) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", "window: first/last require order:");
        }
        func_kinds[fi]  = kind;
        func_params[fi] = 0;

        /* Input column name */
        if (!col_expr || col_expr->type != -RAY_SYM) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", "window: function input must be a column name symbol");
        }
        ray_t* col_s = ray_sym_str(col_expr->i64);
        if (!col_s) {
            DICT_VIEW_CLOSE(fv); scratch_free(outname_hdr); scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", "window: unknown input column");
        }
        func_inputs[fi] = ray_scan(g, ray_str_ptr(col_s));
    }
    DICT_VIEW_CLOSE(fv);  /* fv dead past the funcs loop */

    /* ── Assemble and execute ── */
    ray_op_t* win_op = ray_window_op(g, tbl_node,
                                     n_part  ? part_ops  : NULL, (uint32_t)n_part,
                                     n_order ? order_ops : NULL,
                                     n_order ? order_descs : NULL, (uint32_t)n_order,
                                     func_kinds, func_inputs, func_params,
                                     (uint32_t)n_funcs,
                                     RAY_FRAME_ROWS,
                                     frame_start, frame_end,
                                     frame_start_n, 0);
    scratch_free(funcs_hdr); scratch_free(order_hdr); scratch_free(part_hdr);
    if (!win_op) {
        scratch_free(outname_hdr); ray_graph_free(g); ray_release(tbl);
        return ray_error("oom", NULL);
    }

    win_op = ray_optimize(g, win_op);
    ray_t* result = ray_execute(g, win_op);
    ray_graph_free(g);
    ray_release(tbl);

    if (!result || RAY_IS_ERR(result)) { scratch_free(outname_hdr); return result; }

    /* Rename _w0/_w1/… to the user-specified output names */
    int64_t base_ncols = ray_table_ncols(result) - n_funcs;
    for (int fi = 0; fi < n_funcs; fi++) {
        if (out_name_ids[fi] >= 0)
            ray_table_set_col_name(result, base_ncols + fi, out_name_ids[fi]);
    }
    scratch_free(outname_hdr);

    return result;
}

typedef enum {
    COUNT_CMP_EQ = 1,
    COUNT_CMP_NE,
    COUNT_CMP_LT,
    COUNT_CMP_LE,
    COUNT_CMP_GT,
    COUNT_CMP_GE,
} count_cmp_op_t;

typedef struct {
    const ray_t* col;
    int64_t     rhs;
    count_cmp_op_t op;
    int64_t*    counts;
} count_compare_ctx_t;

static inline int64_t count_atom_i64(ray_t* a) {
    if (a->type == -RAY_BOOL) return (int64_t)a->b8;
    if (a->type == -RAY_U8) return (int64_t)a->u8;
    if (a->type == -RAY_I16) return (int64_t)a->i16;
    if (a->type == -RAY_I32 || a->type == -RAY_DATE || a->type == -RAY_TIME)
        return (int64_t)a->i32;
    if (a->type == -RAY_I64 || a->type == -RAY_TIMESTAMP || a->type == -RAY_SYM)
        return a->i64;
    return 0;
}

static inline int count_compare_i64(int64_t lhs, int64_t rhs, count_cmp_op_t op) {
    switch (op) {
    case COUNT_CMP_EQ: return lhs == rhs;
    case COUNT_CMP_NE: return lhs != rhs;
    case COUNT_CMP_LT: return lhs <  rhs;
    case COUNT_CMP_LE: return lhs <= rhs;
    case COUNT_CMP_GT: return lhs >  rhs;
    case COUNT_CMP_GE: return lhs >= rhs;
    default: return 0;
    }
}

static count_cmp_op_t count_cmp_flip(count_cmp_op_t op) {
    switch (op) {
    case COUNT_CMP_LT: return COUNT_CMP_GT;
    case COUNT_CMP_LE: return COUNT_CMP_GE;
    case COUNT_CMP_GT: return COUNT_CMP_LT;
    case COUNT_CMP_GE: return COUNT_CMP_LE;
    default: return op;
    }
}

static void count_compare_task(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    count_compare_ctx_t* ctx = (count_compare_ctx_t*)vctx;
    const ray_t* col = ctx->col;
    const void* data = ray_data((ray_t*)col);
    int64_t rhs = ctx->rhs;
    count_cmp_op_t op = ctx->op;
    int64_t local = 0;

    switch (col->type) {
    case RAY_BOOL:
    case RAY_U8: {
        const uint8_t* x = (const uint8_t*)data;
        if ((op == COUNT_CMP_EQ || op == COUNT_CMP_NE) && rhs >= 0 && rhs <= 255) {
            uint8_t needle = (uint8_t)rhs;
            if (op == COUNT_CMP_EQ) {
                for (int64_t i = start; i < end; i++)
                    local += x[i] == needle;
            } else {
                for (int64_t i = start; i < end; i++)
                    local += x[i] != needle;
            }
            break;
        }
        for (int64_t i = start; i < end; i++)
            local += count_compare_i64((int64_t)x[i], rhs, op);
        break;
    }
    case RAY_I16: {
        const int16_t* x = (const int16_t*)data;
        for (int64_t i = start; i < end; i++) local += count_compare_i64((int64_t)x[i], rhs, op);
        break;
    }
    case RAY_I32:
    case RAY_DATE:
    case RAY_TIME: {
        const int32_t* x = (const int32_t*)data;
        for (int64_t i = start; i < end; i++) local += count_compare_i64((int64_t)x[i], rhs, op);
        break;
    }
    case RAY_I64:
    case RAY_TIMESTAMP: {
        const int64_t* x = (const int64_t*)data;
        for (int64_t i = start; i < end; i++) local += count_compare_i64(x[i], rhs, op);
        break;
    }
    case RAY_SYM: {
        uint8_t esz = col_esz(col);
        for (int64_t i = start; i < end; i++)
            local += count_compare_i64(read_by_esz(data, i, esz), rhs, op);
        break;
    }
    default:
        break;
    }
    ctx->counts[worker_id] += local;
}

static int try_count_simple_compare(ray_t* tbl, ray_t* where_expr, int64_t* out_count) {
    if (!tbl || !where_expr || where_expr->type != RAY_LIST || ray_len(where_expr) != 3)
        return 0;
    ray_t** we = (ray_t**)ray_data(where_expr);
    if (!we[0] || we[0]->type != -RAY_SYM) return 0;
    ray_t* op_name = ray_sym_str(we[0]->i64);
    if (!op_name) return 0;
    const char* op_s = ray_str_ptr(op_name);
    size_t op_len = ray_str_len(op_name);
    count_cmp_op_t op = 0;
    if (op_len == 1) {
        if (op_s[0] == '<') op = COUNT_CMP_LT;
        else if (op_s[0] == '>') op = COUNT_CMP_GT;
    } else if (op_len == 2) {
        if (op_s[0] == '=' && op_s[1] == '=') op = COUNT_CMP_EQ;
        else if (op_s[0] == '!' && op_s[1] == '=') op = COUNT_CMP_NE;
        else if (op_s[0] == '<' && op_s[1] == '=') op = COUNT_CMP_LE;
        else if (op_s[0] == '>' && op_s[1] == '=') op = COUNT_CMP_GE;
    }
    if (!op) return 0;

    ray_t* col_expr = we[1];
    ray_t* rhs_expr = we[2];
    if (col_expr && ray_is_atom(col_expr) &&
        !(col_expr->type == -RAY_SYM && !(col_expr->attrs & ATTR_QUOTED)) &&
        rhs_expr && rhs_expr->type == -RAY_SYM && !(rhs_expr->attrs & ATTR_QUOTED)) {
        col_expr = we[2];
        rhs_expr = we[1];
        op = count_cmp_flip(op);
    }
    if (!col_expr || col_expr->type != -RAY_SYM || (col_expr->attrs & ATTR_QUOTED))
        return 0;
    if (!rhs_expr || !ray_is_atom(rhs_expr) ||
        (rhs_expr->type == -RAY_SYM && !(rhs_expr->attrs & ATTR_QUOTED)) ||
        RAY_ATOM_IS_NULL(rhs_expr))
        return 0;

    /* B3 Part 2: a QUOTED -RAY_SYM rhs that NAMES a from-table column does
     * NOT compare against the literal symbol — inside a query it resolves to
     * that column (the in-query collision rule applied by the full select
     * path).  This fast path only knows how to compare against a constant, so
     * bail out and let the materialised select path handle the column→column
     * comparison consistently.  A quoted sym naming no column stays a literal
     * and is handled here as before. */
    if (rhs_expr->type == -RAY_SYM && (rhs_expr->attrs & ATTR_QUOTED) &&
        ray_table_get_col(tbl, rhs_expr->i64))
        return 0;

    ray_t* col = ray_table_get_col(tbl, col_expr->i64);
    if (!col || col->len != ray_table_nrows(tbl)) return 0;
    if (ray_vec_may_have_nulls(col)) return 0;

    switch (col->type) {
    case RAY_BOOL:
    case RAY_U8:
    case RAY_I16:
    case RAY_I32:
    case RAY_I64:
    case RAY_DATE:
    case RAY_TIME:
    case RAY_TIMESTAMP:
        if (!is_numeric(rhs_expr) && !is_temporal(rhs_expr)) return 0;
        break;
    case RAY_SYM:
        if (rhs_expr->type != -RAY_SYM) return 0;
        break;
    default:
        return 0;
    }

    /* The raw compare below is right only for a literal in the column's own
     * units.  A float literal decoded to 0 here, and a different temporal
     * type compared days, milliseconds and nanoseconds as one unit.  A
     * TIMESTAMP column takes a DATE or TIME literal on its nanosecond scale,
     * as the eval path does; every other mix goes the general way. */
    if (rhs_expr->type == -RAY_F64 || rhs_expr->type == -RAY_F32) return 0;
    bool rhs_ns = false;
    if (is_temporal(rhs_expr) && rhs_expr->type != -col->type) {
        if (col->type != RAY_TIMESTAMP) return 0;
        rhs_ns = true;
    }

    int64_t rhs = count_atom_i64(rhs_expr);
    if (rhs_ns) {
        /* Days or milliseconds to nanoseconds, saturating: a date beyond
         * the TIMESTAMP range still compares past every cell. */
        int64_t unit = rhs_expr->type == -RAY_DATE ? 86400000000000LL : 1000000LL;
        if (__builtin_mul_overflow((int64_t)rhs_expr->i32, unit, &rhs))
            rhs = rhs_expr->i32 < 0 ? INT64_MIN + 1 : INT64_MAX;
    }
    /* SYM cells are positions in the COLUMN's domain; the literal atom
     * carries a runtime id — re-express it (sym-domain Phase 2; no-op
     * for runtime-domain columns).  Absent ⇒ -1: never equals any cell
     * (cells are non-negative), so EQ counts 0 and NE counts all —
     * exactly the absent-literal-matches-nothing contract. */
    if (col->type == RAY_SYM &&
        ray_sym_vec_domain(col) != ray_sym_runtime_domain()) {
        ray_t* s = ray_sym_str(rhs);
        rhs = s ? ray_sym_vec_lookup(col, ray_str_ptr(s), ray_str_len(s)) : -1;
    }
    ray_pool_t* pool = ray_pool_get();
    uint32_t nworkers = pool ? ray_pool_total_workers(pool) : 1;
    ray_t* counts_block = ray_alloc((size_t)nworkers * sizeof(int64_t));
    if (!counts_block) return 0;
    int64_t* counts = (int64_t*)ray_data(counts_block);
    memset(counts, 0, (size_t)nworkers * sizeof(int64_t));
    count_compare_ctx_t ctx = {
        .col = col,
        .rhs = rhs,
        .op = op,
        .counts = counts,
    };
    int64_t nrows = col->len;
    if (pool && nrows >= RAY_PARALLEL_THRESHOLD)
        ray_pool_dispatch(pool, count_compare_task, &ctx, nrows);
    else
        count_compare_task(&ctx, 0, 0, nrows);

    int64_t total = 0;
    for (uint32_t i = 0; i < nworkers; i++) total += counts[i];
    ray_release(counts_block);
    *out_count = total;
    return 1;
}

/* A where: value that is not already a BOOL mask of `tn` rows goes through
 * the one where: rule (ray_where_mask_coerce): a scalar (`false`,
 * `(== (count x) 5)`) is spread over the rows, any other vector (a numeric
 * or symbol column) is a type error.  Consumes
 * `m`. */
static ray_t* where_mask_spread(ray_t* m, int64_t tn, const char* label) {
    if (!m || RAY_IS_ERR(m)) return m;
    if (m->type == RAY_BOOL && m->len == tn) return m;
    return ray_where_mask_coerce(m, tn, label);
}

ray_t* ray_try_count_select_expr(ray_t* expr, int* handled) {
    if (handled) *handled = 0;
    if (!expr || expr->type != RAY_LIST || ray_len(expr) != 2) return NULL;
    ray_t** elems = (ray_t**)ray_data(expr);
    if (!elems[0] || elems[0]->type != -RAY_SYM) return NULL;
    ray_t* name = ray_sym_str(elems[0]->i64);
    if (!name || ray_str_len(name) != 6 ||
        memcmp(ray_str_ptr(name), "select", 6) != 0)
        return NULL;

    ray_t* dict = elems[1];
    if (!dict || dict->type != RAY_DICT) return NULL;

    ray_t* from_expr = dict_get(dict, "from");
    ray_t* where_expr = dict_get(dict, "where");
    if (!from_expr) return NULL;
    if (where_expr && where_expr->type == RAY_LIST && ray_len(where_expr) >= 3) {
        ray_t** we = (ray_t**)ray_data(where_expr);
        if (we[0] && we[0]->type == -RAY_SYM) {
            ray_t* wn = ray_sym_str(we[0]->i64);
            if (wn && ray_str_len(wn) == 3 &&
                memcmp(ray_str_ptr(wn), "and", 3) == 0)
                return NULL;
        }
    }

    int64_t from_id    = dict_key_id(dict, "from");
    int64_t where_id   = dict_key_id(dict, "where");
    int64_t by_id      = dict_key_id(dict, "by");
    int64_t take_id    = dict_key_id(dict, "take");
    int64_t asc_id     = dict_key_id(dict, "asc");
    int64_t desc_id    = dict_key_id(dict, "desc");
    int64_t nearest_id = dict_key_id(dict, "nearest");

    DICT_VIEW_DECL(dv);
    DICT_VIEW_OPEN(dict, dv);
    if (DICT_VIEW_OVERFLOW(dv)) { DICT_VIEW_CLOSE(dv); return NULL; }
    for (int64_t i = 0; i + 1 < dv_n; i += 2) {
        int64_t kid = dv[i]->i64;
        if (kid == by_id || kid == take_id || kid == asc_id ||
            kid == desc_id || kid == nearest_id)
            { DICT_VIEW_CLOSE(dv); return NULL; }
        if (kid != from_id && kid != where_id)
            { DICT_VIEW_CLOSE(dv); return NULL; }
    }
    DICT_VIEW_CLOSE(dv);  /* dv dead past the clause-keyword scan */

    ray_t* tbl = ray_eval(from_expr);
    if (!tbl || RAY_IS_ERR(tbl)) return tbl ? tbl : ray_error("type", "select count: `from:` evaluation failed");
    if (tbl->type != RAY_TABLE) {
        int8_t tbl_t = tbl->type;            /* capture BEFORE free */
        ray_release(tbl);
        return ray_error("type", "select count: `from:` must be a table, got %s", ray_type_name(tbl_t));
    }

    if (!where_expr) {
        if (handled) *handled = 1;
        int64_t nrows = ray_table_nrows(tbl);
        ray_release(tbl);
        return ray_i64(nrows);
    }

    int64_t direct_count = 0;
    if (try_count_simple_compare(tbl, where_expr, &direct_count)) {
        if (handled) *handled = 1;
        ray_release(tbl);
        return ray_i64(direct_count);
    }

    ray_graph_t* g = ray_graph_new(tbl);
    if (!g) {
        ray_release(tbl);
        return ray_error("oom", NULL);
    }
    ray_op_t* pred = compile_expr_dag(g, where_expr);
    if (!pred && g->compile_err) {
        ray_t* cerr = graph_take_compile_err(g);
        ray_graph_free(g);
        ray_release(tbl);
        return cerr;
    }
    if (!pred) {
        ray_t* pred_vec = eval_where_mask(where_expr, tbl, "select count");
        if (!pred_vec || RAY_IS_ERR(pred_vec)) {
            ray_graph_free(g);
            ray_release(tbl);
            return pred_vec ? pred_vec :
                ray_error("type", "select count: WHERE predicate evaluation failed");
        }
        if (handled) *handled = 1;
        int64_t nrows = ray_table_nrows(tbl);
        ray_t* sel = ray_rowsel_from_pred(pred_vec);
        if (sel) {
            ray_rowsel_t* sm = ray_rowsel_meta(sel);
            nrows = sm ? sm->total_pass : 0;
            ray_release(sel);
        }
        ray_release(pred_vec);
        ray_graph_free(g);
        ray_release(tbl);
        return ray_i64(nrows);
    }
    int has_scan = 0;
    ray_op_t* stk[64];
    int sp = 0;
    stk[sp++] = pred;
    while (sp > 0) {
        ray_op_t* cur = stk[--sp];
        if (cur->opcode == OP_SCAN) { has_scan = 1; break; }
        for (uint8_t a = 0; a < cur->arity && sp < 64; a++) {
            ray_op_t* ch = op_child(g, cur, a);
            if (ch) stk[sp++] = ch;
        }
    }
    if (!has_scan) {
        ray_graph_free(g);
        ray_release(tbl);
        return NULL;
    }

    int64_t pre_nrows = ray_table_nrows(tbl);

    /* Fused predicate→selection: count directly off the streamed rowsel,
     * skipping the full BOOL vec + the BOOL→rowsel scan.  Unsupported shapes,
     * or >RAY_POOL_MAX_TASKS fall through to exec_node. */
    {
        bool all_pass = false;
        ray_t* fsel = exec_pred_to_selection(g, pred, pre_nrows, &all_pass);
        if (all_pass || fsel) {
            int64_t nrows = pre_nrows;
            if (fsel) {
                ray_rowsel_t* sm = ray_rowsel_meta(fsel);
                nrows = sm ? sm->total_pass : 0;
                ray_release(fsel);
            }
            if (handled) *handled = 1;
            ray_graph_free(g);
            ray_release(tbl);
            return ray_i64(nrows);
        }
        /* unsupported / OOM — fall through to the bool path. */
    }

    ray_t* pred_vec = exec_node(g, pred);
    if (!pred_vec || RAY_IS_ERR(pred_vec)) {
        ray_graph_free(g);
        ray_release(tbl);
        return pred_vec ? pred_vec : ray_error("type", "select count: WHERE predicate evaluation failed");
    }
    int64_t tbl_nrows = ray_table_nrows(tbl);
    pred_vec = where_mask_spread(pred_vec, tbl_nrows, "select count");
    if (!pred_vec || RAY_IS_ERR(pred_vec)) {
        ray_graph_free(g);
        ray_release(tbl);
        return pred_vec ? pred_vec : ray_error("oom", NULL);
    }
    if (pred_vec->type != RAY_BOOL || pred_vec->len != tbl_nrows) {
        int8_t pred_t = pred_vec->type;            /* capture BEFORE free */
        ray_release(pred_vec);
        ray_graph_free(g);
        ray_release(tbl);
        return ray_error("type", "select count: WHERE must produce a bool mask matching row count, got %s", ray_type_name(pred_t));
    }

    if (handled) *handled = 1;
    int64_t nrows = tbl_nrows;
    ray_t* sel = ray_rowsel_from_pred(pred_vec);
    if (sel) {
        ray_rowsel_t* sm = ray_rowsel_meta(sel);
        nrows = sm ? sm->total_pass : 0;
        ray_release(sel);
    }
    ray_release(pred_vec);
    ray_graph_free(g);
    ray_release(tbl);
    return ray_i64(nrows);
}

/* Walk `expr` and collect column-name symbols (unflagged name-ref atoms,
 * ATTR_QUOTED clear, that resolve to a real column in `tbl`).  Also follows
 * the head of dotted names so a `Timestamp.date` reference contributes its
 * base column.
 * `out_syms` is treated as an append-only set (dedup against existing
 * entries) up to `max_out`; returns the new count.  Used to determine
 * the subset of input columns the rest of a (select …) clause actually
 * touches, so a prefilter materialise can skip everything else. */
static int collect_col_refs_set(ray_t* expr, ray_t* tbl,
                                int64_t* out_syms, int max_out, int n) {
    if (!expr || n >= max_out) return n;
    if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
        int64_t want = -1;
        if (ray_table_get_col(tbl, expr->i64)) {
            want = expr->i64;
        } else if (ray_sym_is_dotted(expr->i64)) {
            const int64_t* segs;
            int nsegs = ray_sym_segs(expr->i64, &segs);
            if (nsegs >= 1 && ray_table_get_col(tbl, segs[0])) want = segs[0];
        }
        if (want >= 0) {
            for (int i = 0; i < n; i++) if (out_syms[i] == want) return n;
            if (n < max_out) out_syms[n++] = want;
        }
        return n;
    }
    if (expr->type == RAY_LIST) {
        ray_t** elems = (ray_t**)ray_data(expr);
        int64_t cnt = ray_len(expr);
        for (int64_t i = 0; i < cnt && n < max_out; i++)
            n = collect_col_refs_set(elems[i], tbl, out_syms, max_out, n);
        return n;
    }
    if (expr->type == RAY_DICT) {
        DICT_VIEW_DECL(dv);
        DICT_VIEW_OPEN(expr, dv);
        /* Post cut-2 the view can no longer overflow — dv_n<0 is OOM only,
         * so this is no longer a silent >256-entry truncation; on OOM we
         * return the refs gathered so far. */
        if (DICT_VIEW_OVERFLOW(dv)) { DICT_VIEW_CLOSE(dv); return n; }
        for (int64_t i = 0; i + 1 < dv_n && n < max_out; i += 2)
            n = collect_col_refs_set(dv[i + 1], tbl, out_syms, max_out, n);
        DICT_VIEW_CLOSE(dv);
        return n;
    }
    if (expr->type == RAY_SYM) {
        /* Sym vector — each element is a column name (e.g. multi-col
         * asc:/desc:/by: tuples).  Pull syms out at the storage width. */
        int64_t len = ray_len(expr);
        for (int64_t i = 0; i < len && n < max_out; i++) {
            /* cell-data → runtime name id (column lookup) */
            int64_t s = sym_cell_runtime_id(expr, i);
            if (ray_table_get_col(tbl, s)) {
                int dup = 0;
                for (int j = 0; j < n; j++) if (out_syms[j] == s) { dup = 1; break; }
                if (!dup && n < max_out) out_syms[n++] = s;
            }
        }
        return n;
    }
    return n;
}

/* Build a narrow projection of `src_tbl` containing only the columns in
 * `keep_syms[0..n_keep)`, preserving the original column order.
 * Schema/cols share the source vec/list headers (retain'd internally
 * by ray_table_add_col); no row data is copied — projection is a
 * metadata-only operation.  Returns an owned ray_t* or an error. */
static ray_t* project_table_cols(ray_t* src_tbl, const int64_t* keep_syms,
                                 int n_keep) {
    ray_t* nt = ray_table_new(n_keep);
    if (!nt || RAY_IS_ERR(nt)) return nt ? nt : ray_error("oom", NULL);
    for (int i = 0; i < n_keep; i++) {
        ray_t* col = ray_table_get_col(src_tbl, keep_syms[i]);
        if (!col) { ray_release(nt); return ray_error("domain", "select: projected column not found"); }
        ray_t* nt2 = ray_table_add_col(nt, keep_syms[i], col);
        if (!nt2 || RAY_IS_ERR(nt2)) {
            if (nt2 && nt2 != nt) ray_release(nt2);
            else ray_release(nt);
            return nt2 ? nt2 : ray_error("oom", NULL);
        }
        nt = nt2;
    }
    return nt;
}

/* Walk an expr collecting unique UNQUOTED -RAY_SYM ids (candidate column-name
 * refs).  Over-approximate — function names get included (harmless: the consumer
 * keeps only columns whose name matches) — but COMPLETE for static refs, which
 * is what matters: a referenced column must never be dropped. */
static int collect_syms(ray_t* e, int64_t* out, int max, int n) {
    if (!e || n >= max) return n;
    if (e->type == -RAY_SYM) {
        if (e->attrs & ATTR_QUOTED) return n;        /* literal sym, not a ref */
        for (int i = 0; i < n; i++) if (out[i] == e->i64) return n;   /* dedup */
        out[n++] = e->i64;
        return n;
    }
    if (e->type == RAY_LIST) {
        ray_t** el = (ray_t**)ray_data(e);
        int64_t len = ray_len(e);
        for (int64_t i = 0; i < len && n < max; i++) n = collect_syms(el[i], out, max, n);
    } else if (e->type == RAY_DICT) {
        ray_t* vals = ray_dict_vals(e);
        if (vals && vals->type == RAY_LIST) {
            ray_t** v = (ray_t**)ray_data(vals);
            int64_t len = ray_len(vals);
            for (int64_t i = 0; i < len && n < max; i++) n = collect_syms(v[i], out, max, n);
        }
    }
    return n;
}

/* Over-count of the sym atoms collect_syms could gather from `e`.  Mirrors
 * collect_syms' recursion shape (unquoted -RAY_SYM leaves; recurse LIST elems
 * and DICT vals) but WITHOUT dedup — an over-count is exactly what a keep-set
 * carve wants (dedup only shrinks the real count, so the carve stays large
 * enough).  Used once per compile to size proj_buf to a bound collect_syms
 * cannot saturate. */
static int64_t count_syms(ray_t* e) {
    if (!e) return 0;
    if (e->type == -RAY_SYM) return (e->attrs & ATTR_QUOTED) ? 0 : 1;
    if (e->type == RAY_LIST) {
        ray_t** el = (ray_t**)ray_data(e);
        int64_t len = ray_len(e), c = 0;
        for (int64_t i = 0; i < len; i++) c += count_syms(el[i]);
        return c;
    }
    if (e->type == RAY_DICT) {
        ray_t* vals = ray_dict_vals(e);
        if (vals && vals->type == RAY_LIST) {
            ray_t** v = (ray_t**)ray_data(vals);
            int64_t len = ray_len(vals), c = 0;
            for (int64_t i = 0; i < len; i++) c += count_syms(v[i]);
            return c;
        }
    }
    return 0;
}

/* Buffer size that proj_compute_keep's keep-set can never saturate: walk the
 * same non-`from` value exprs collect_syms visits and count every sym atom
 * (an over-count of the distinct total — dedup only shrinks it), then add 1.
 *
 * The +1 is load-bearing.  collect_syms fills `out[0..n-1]` and the guard
 * declines when `n >= max`.  The distinct count it produces is ≤ this
 * occurrence over-count B, so with `max = B + 1` we always have `n ≤ B < max`
 * — the guard is unreachable and a legitimate full keep-set is published, not
 * declined.  Sizing to exactly B would re-trip the guard whenever a query has
 * no duplicate refs (n == B == max), which is precisely the q11 shape.  Floor
 * keeps it ≥ 1 slot. */
static int64_t proj_keep_bound(ray_t* dict) {
    ray_t* keys = ray_dict_keys(dict);
    ray_t* vals = ray_dict_vals(dict);
    if (!keys || keys->type != RAY_SYM || !vals || vals->type != RAY_LIST) return 1;
    int64_t from_id = dict_key_id(dict, "from");
    int64_t n = keys->len;
    ray_t** vptrs = (ray_t**)ray_data(vals);
    int64_t cnt = 0;
    for (int64_t i = 0; i < n; i++) {
        if (sym_cell_runtime_id(keys, i) == from_id) continue;  /* the subquery */
        cnt += count_syms(vptrs[i]);
    }
    return cnt + 1;
}

/* Projection-pushdown keep-set: for an EXPLICITLY-projected select (one with
 * value exprs, so it references only named columns), collect the column syms its
 * non-`from` clauses reference.  Returns false (→ carry all) for an implicit-all
 * select, which references every column. */
static bool proj_compute_keep(ray_t* dict, int64_t* out, int max, int* out_n) {
    int64_t from_id    = dict_key_id(dict, "from");
    int64_t where_id   = dict_key_id(dict, "where");
    int64_t by_id      = dict_key_id(dict, "by");
    int64_t take_id    = dict_key_id(dict, "take");
    int64_t asc_id     = dict_key_id(dict, "asc");
    int64_t desc_id    = dict_key_id(dict, "desc");
    int64_t nearest_id = dict_key_id(dict, "nearest");
    DICT_VIEW_DECL(pv);
    DICT_VIEW_OPEN(dict, pv);
    if (DICT_VIEW_OVERFLOW(pv)) { DICT_VIEW_CLOSE(pv); return false; }
    int n = 0;
    bool has_value_expr = false;
    for (int64_t i = 0; i + 1 < pv_n; i += 2) {
        int64_t kid = pv[i]->i64;
        if (kid == from_id) continue;                /* the subquery itself */
        if (kid != where_id && kid != by_id && kid != take_id &&
            kid != asc_id && kid != desc_id && kid != nearest_id)
            has_value_expr = true;                   /* an explicit projection */
        n = collect_syms(pv[i + 1], out, max, n);
    }
    DICT_VIEW_CLOSE(pv);  /* pv dead past the clause scan */
    if (!has_value_expr) return false;               /* implicit-all → carry all */
    /* Completeness safety net: with proj_buf sized from proj_keep_bound (an
     * over-count of exactly the sym atoms this loop can collect) `n >= max` is
     * unreachable.  It is kept anyway: should the bound ever under-count, a
     * saturated buffer may have dropped a referenced column, and a referenced
     * column must never be silently dropped — so decline the pushdown and
     * carry all columns (correct, just unoptimized) rather than truncate. */
    if (n >= max) return false;
    *out_n = n;
    return true;
}

/* Filter-compaction keep-set for a `select {where: … by: [k1 k2 …]}` whose sole
 * consumer is the keys-only multi-key DISTINCT path (agg_select_distinct) under
 * an active projection hint.  In that shape the WHERE filter's result table need
 * only carry the columns the distinct will read — the group keys plus the outer
 * select's published proj_keep — so ray_execute_inner gathers those instead of
 * the whole table.  Returns the deduped keep count, or 0 (carry all) for any
 * shape that could read a dropped column.
 *
 * Correctness — the distinct path (see the distinct_only block below) is taken,
 * and emits exactly keys ∪ proj_keep, iff ALL hold: (a) a projection hint is
 * active at this eval_depth (so the distinct projects to keys ∪ proj_keep rather
 * than first-of-group of every column); (b) the by: is a multi-key SYM vector;
 * (c) the dict has only from/where/by (no value/take/sort clauses); (d) every
 * source column is a gatherable type.  Any failure returns 0 and the full table
 * is carried, exactly as before.  Types are read from the source table — the
 * WHERE filter preserves each column's type and the projected set is a subset. */
static int filt_compact_keep(ray_t* dict, ray_t* by_expr, ray_t* tbl,
                             int64_t from_id, int64_t where_id, int64_t by_id,
                             int64_t* out, int max) {
    ray_vm_t* vm = __VM;
    if (!vm || !vm->proj_active || vm->eval_depth != vm->proj_depth + 1)
        return 0;                                    /* (a) */
    if (!by_expr || by_expr->type != RAY_SYM || ray_len(by_expr) <= 1)
        return 0;                                    /* (b) */
    DICT_VIEW_DECL(pv);
    DICT_VIEW_OPEN(dict, pv);
    if (DICT_VIEW_OVERFLOW(pv)) { DICT_VIEW_CLOSE(pv); return 0; }
    for (int64_t i = 0; i + 1 < pv_n; i += 2) {      /* (c) */
        int64_t kid = pv[i]->i64;
        if (kid != from_id && kid != where_id && kid != by_id) { DICT_VIEW_CLOSE(pv); return 0; }
    }
    DICT_VIEW_CLOSE(pv);  /* pv dead past the clause-shape check */
    int64_t nk = ray_len(by_expr);
    const int64_t* key_syms = (const int64_t*)ray_data(by_expr);
    for (int64_t k = 0; k < nk; k++) {               /* (d) keys */
        ray_t* kc = ray_table_get_col(tbl, key_syms[k]);
        if (!kc) return 0;
        switch (kc->type) {
            case RAY_I64: case RAY_I32: case RAY_I16: case RAY_U8:
            case RAY_BOOL: case RAY_DATE: case RAY_TIME:
            case RAY_TIMESTAMP: case RAY_SYM: case RAY_STR: break;
            default: return 0;
        }
    }
    int64_t nc = ray_table_ncols(tbl);
    for (int64_t c = 0; c < nc; c++) {               /* (d) all source columns */
        ray_t* cc = ray_table_get_col_idx(tbl, c);
        if (!cc) return 0;
        switch (cc->type) {
            case RAY_I64: case RAY_I32: case RAY_I16: case RAY_U8:
            case RAY_BOOL: case RAY_DATE: case RAY_TIME:
            case RAY_TIMESTAMP: case RAY_SYM: case RAY_F64:
            case RAY_GUID: case RAY_STR: case RAY_LIST: break;
            default: return 0;
        }
    }
    /* keep = keys ∪ proj_keep.  The proj_keep union is LOAD-BEARING, not
     * defensive: a plain multi-key `select {by: …}` emits first-of-group of the
     * NON-key columns too (agg_select_distinct gathers them), and proj_keep is
     * exactly the set of those the outer consumer references — dropping the
     * union would silently drop needed columns.  Keys are written first so an
     * (unreachable) >max overflow can only lose proj_keep entries, never keys. */
    int n = 0;
    for (int64_t k = 0; k < nk && n < max; k++) {
        bool dup = false;
        for (int j = 0; j < n; j++) if (out[j] == key_syms[k]) { dup = true; break; }
        if (!dup) out[n++] = key_syms[k];
    }
    for (int j = 0; j < vm->proj_keep_n && n < max; j++) {
        int64_t s = vm->proj_keep[j];
        bool dup = false;
        for (int q = 0; q < n; q++) if (out[q] == s) { dup = true; break; }
        if (!dup) out[n++] = s;
    }
    return n;
}

/* Materialize pure temporal arithmetic with the unit-aware evaluator once,
 * then let the normal typed GROUP pipeline reduce that column. The expression
 * DAG's raw integer arithmetic remains guarded for mixed temporal units. */
static bool temporal_materialize_pure(ray_t* expr, ray_t* tbl) {
    if (!expr) return false;
    if (ray_is_atom(expr)) {
        if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
            ray_t* col = ray_table_get_col(tbl, expr->i64);
            return col && !RAY_IS_PARTED(col->type) && col->type != RAY_MAPCOMMON;
        }
        return true;
    }
    if (expr->type != RAY_LIST || expr->len != 3) return false;
    ray_t** es = ray_data(expr);
    if (!es[0] || es[0]->type != -RAY_SYM) return false;
    ray_t* name = ray_sym_str(es[0]->i64);
    if (!name || ray_str_len(name) != 1) return false;
    char c = ray_str_ptr(name)[0];
    return (c == '+' || c == '-' || c == '*') &&
        temporal_materialize_pure(es[1], tbl) && temporal_materialize_pure(es[2], tbl);
}
static ray_t* try_temporal_group_materialize(ray_t* dict, ray_t* tbl) {
    if (!dict_get(dict, "by")) return NULL;
    ray_t* vals = ray_dict_vals(dict);
    ray_t* keys = ray_dict_keys(dict);
    ray_t* rewritten = NULL;
    ray_t* extended = NULL;
    uint32_t seq = 0;
    ray_t* replacement = NULL;
    ray_t* failure = NULL;
    #define MAT_CHECK(value) do { if (!(value) || RAY_IS_ERR(value)) { failure = (value); (value) = NULL; goto oom; } } while (0)
    for (int64_t i = 0; i < ray_dict_len(dict); i++) {
        ray_t* expr = ray_list_get(vals, i);
        if (!expr || !is_group_dag_agg_expr(expr)) continue;
        ray_t** es = ray_data(expr);
        uint16_t kind = resolve_agg_opcode(es[0]->i64);
        int argc = agg_is_binary_agg(kind) ? 2 : 1;
        replacement = NULL;
        for (int arg = 1; arg <= argc; arg++) {
            if (arg >= expr->len || !expr_contains_temporal_arith(es[arg], tbl) ||
                !temporal_materialize_pure(es[arg], tbl)) continue;
            if (ray_env_push_query_scope() != RAY_OK) goto oom;
            ray_t* previous = bind_all_columns(tbl);
            ray_t* value = ray_eval(es[arg]);
            g_active_query_table = previous;
            ray_env_pop_scope();
            if (value && !RAY_IS_ERR(value) && ray_is_lazy(value)) value = ray_lazy_materialize(value);
            if (!value || RAY_IS_ERR(value)) {
                if (replacement) ray_release(replacement);
                if (rewritten) ray_release(rewritten);
                if (extended) ray_release(extended);
                return value ? value : ray_error("oom", NULL);
            }
            if (!ray_is_vec(value) || value->len != ray_table_nrows(tbl)) { ray_release(value); continue; }
            if (!extended) { extended = tbl; ray_retain(extended); }
            char name[48]; int64_t sym;
            do {
                int len = snprintf(name, sizeof(name), "_temporal_agg_%u", seq++);
                sym = ray_sym_intern(name, (size_t)len);
            } while (ray_table_get_col(extended, sym));
            extended = ray_table_add_col(extended, sym, value); ray_release(value);
            MAT_CHECK(extended);
            if (!replacement) {
                replacement = ray_list_new(expr->len);
                MAT_CHECK(replacement);
                for (int64_t j = 0; j < expr->len; j++) {
                    replacement = ray_list_append(replacement, es[j]); MAT_CHECK(replacement);
                }
            }
            ray_t* ref = ray_sym(sym);
            MAT_CHECK(ref);
            replacement = ray_list_set(replacement, arg, ref); ray_release(ref);
            MAT_CHECK(replacement);
        }
        if (replacement) {
            if (!rewritten) { rewritten = dict; ray_retain(rewritten); }
            int alloc = 0;
            ray_t* key = collection_elem(keys, i, &alloc);
            rewritten = ray_dict_upsert(rewritten, key, replacement);
            if (alloc) ray_release(key);
            ray_release(replacement); replacement = NULL;
            MAT_CHECK(rewritten);
        }
    }
    if (!rewritten) { if (extended) ray_release(extended); return NULL; }
    ray_t* from = ray_sym(ray_sym_intern("from", 4));
    rewritten = ray_dict_upsert(rewritten, from, extended);
    ray_release(from); ray_release(extended); extended = NULL;
    MAT_CHECK(rewritten);
    ray_t* result = ray_select_impl(&rewritten, 1, true);
    ray_release(rewritten);
    return result;
oom:
    if (replacement) ray_release(replacement);
    if (rewritten) ray_release(rewritten);
    if (extended) ray_release(extended);
    return failure ? failure : ray_error("oom", NULL);
    #undef MAT_CHECK
}

/* --------------------------------------------------------------------------
 * Grouped select: a projection may refer to an earlier projection.
 *
 * In a grouped select every output is an aggregate (or a per-group value)
 * over the SOURCE rows, so a later output cannot read an earlier one as a
 * column the way the ungrouped path does through the compile-time env.
 * The reference is resolved at the expression level instead: each output
 * value is rewritten with every earlier alias replaced by that alias's
 * (already rewritten) expression, and the rewritten dict goes through the
 * ordinary classification — `nn: (+ notional 1)` after
 * `notional: (sum (* price volume))` becomes `(+ (sum (* price volume)) 1)`,
 * which the arith-of-aggs decomposition evaluates over the group result.
 *
 * Two places keep the source column instead of the alias.  An alias is
 * visible only to the outputs after it, so its own definition reads the
 * source column of the same name.  And inside an AGGREGATE's argument a
 * name that is a source column stays the source column — an aggregate
 * consumes rows, an alias is one value per group, so `s: (sum s) mx: (max s)`
 * asks for the max of the rows, not of a sum.  Outside aggregates the alias
 * wins, as it does in an ungrouped select.  where:, by: and the sort keys
 * are not outputs and are never rewritten.  A literal symbol naming an
 * earlier alias resolves like the bare name; any other literal is left
 * alone.  Nothing is rewritten inside a lambda, a quote or a let, whose own
 * bindings could shadow the alias.
 * -------------------------------------------------------------------------- */
/* Binds output `kid` = `col` in `tbl` for the projections still to be
 * evaluated: replaces the column of that name when there is one, appends
 * otherwise.  Consumes one ref of `tbl`, returns an owned (possibly
 * copied) table; `col` is retained by the table, the caller keeps its ref. */
static ray_t* select_fallback_bind_alias(ray_t* tbl, int64_t kid, ray_t* col) {
    int64_t ncols = ray_table_ncols(tbl);
    for (int64_t c = 0; c < ncols; c++) {
        if (ray_table_col_name(tbl, c) != kid) continue;
        ray_t* t2 = ray_cow(tbl);   /* a failed copy leaves `tbl` untouched */
        if (!t2 || RAY_IS_ERR(t2)) { ray_release(tbl); return t2; }
        ray_table_set_col_idx(t2, c, col);
        if (ray_table_get_col_idx(t2, c) != col) { ray_release(t2); return ray_error("oom", NULL); }
        return t2;
    }
    return ray_table_add_col(tbl, kid, col);
}

/* The one value an aggregate-only projection produced, as a column of n
 * rows.  An atom is broadcast; a one-element vector (the interpreter's
 * shape for some scalar results, like exec.c's for `(+ (sum x) 0)`) is
 * widened the way OP_SELECT widens it.  Any other vector is the
 * projection's own column and passes through unchanged, so a row-long
 * global or a lambda call that returns a row-aligned result keeps its
 * length.  Consumes `v`. */
static ray_t* select_fallback_broadcast(ray_t* v, int64_t n) {
    if (!v || RAY_IS_ERR(v)) return v;
    if (ray_is_atom(v)) {
        ray_t* out = atom_broadcast_vec(v, n);
        if (!out) out = ray_error("type", "select: cannot broadcast a %s value to a column",
                                  ray_type_name(v->type));
        ray_release(v);
        return out;
    }
    if (v->type <= 0 || !ray_is_vec(v) || v->len != 1 || n == 1) return v;
    ray_t* n_obj = make_i64(n);
    ray_t* wide = n_obj ? ray_take_fn(v, n_obj) : ray_error("oom", NULL);
    if (n_obj) ray_release(n_obj);
    ray_release(v);
    return wide;
}

static bool select_alias_skip_form(ray_t* head) {
    if (!head || head->type != -RAY_SYM || (head->attrs & ATTR_QUOTED)) return false;
    ray_t* s = ray_sym_str(head->i64);
    if (!s) return false;
    const char* p = ray_str_ptr(s);
    size_t l = ray_str_len(s);
    return (l == 2 && memcmp(p, "fn", 2) == 0) ||
           (l == 3 && memcmp(p, "let", 3) == 0) ||
           (l == 5 && memcmp(p, "quote", 5) == 0);
}

/* expr_contains_agg for the eval fallback's one-value rule: an aggregate
 * inside a lambda, let or quote form does not count.  A lambda call is a
 * per-row projection on the fallback (`((fn [c] (sum c)) 'ts)` sums one
 * cell per row, parted_f64_agg.rfl) and its body folds its own formal,
 * not a column, so it is neither evaluated once nor pre-reduced. */
static int select_fallback_has_agg(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    int64_t n = ray_len(expr);
    if (n == 0) return 0;
    ray_t** el = (ray_t**)ray_data(expr);
    if (select_alias_skip_form(el[0])) return 0;
    if (is_agg_expr(expr)) return 1;
    for (int64_t i = 0; i < n; i++)
        if (select_fallback_has_agg(el[i])) return 1;
    return 0;
}

/* 1 when the leaves of `expr` are atom literals and the names in `scalars`,
 * the earlier outputs already known to be one value; `*via` is set when a
 * name was among them.  A column, a global, a vector literal or a lambda,
 * let or quote form may carry rows, and answers 0. */
static int select_fallback_scalar_leaves(ray_t* expr, const int64_t* scalars,
                                         int64_t ns, int* via) {
    if (!expr) return 0;
    if (ray_is_atom(expr)) {
        if (expr->type != -RAY_SYM || (expr->attrs & ATTR_QUOTED)) return 1;
        for (int64_t i = 0; i < ns; i++)
            if (scalars[i] == expr->i64) { *via = 1; return 1; }
        return 0;
    }
    if (expr->type != RAY_LIST) return 0;
    int64_t n = ray_len(expr);
    if (n == 0) return 0;
    ray_t** el = (ray_t**)ray_data(expr);
    if (el[0]->type != -RAY_SYM || select_alias_skip_form(el[0])) return 0;
    for (int64_t i = 1; i < n; i++)
        if (!select_fallback_scalar_leaves(el[i], scalars, ns, via)) return 0;
    return 1;
}

/* 1 when `expr`, an output with no aggregate in it, is one value for the
 * whole select: an atom literal, or an expression over earlier one-value
 * outputs (`y: (+ m 1)` after `m: (max i)`).  A call over literals alone
 * (`(til 8)`) may build a column of the input's length and stays a row
 * projection. */
static int select_fallback_scalar_proj(ray_t* expr, const int64_t* scalars, int64_t ns) {
    int via = 0;
    return select_fallback_scalar_leaves(expr, scalars, ns, &via) &&
           (via || ray_is_atom(expr));
}

static bool select_alias_head_is_agg(ray_t* head) {
    if (!head || head->type != -RAY_SYM || (head->attrs & ATTR_QUOTED)) return false;
    if (resolve_agg_opcode(head->i64) != 0) return true;
    ray_t* s = ray_sym_str(head->i64);
    return s && ray_str_len(s) == 8 && memcmp(ray_str_ptr(s), "distinct", 8) == 0;
}

/* The first aggregate call in `expr` that sits inside another aggregate's
 * argument, or NULL.  In a grouped select the inner one is already one
 * value per group, so the outer one has no rows left to fold; evaluated
 * anyway it collapses over the whole table and every group gets the same
 * number.  A lambda or let body is its own scope and is not descended. */
static ray_t* grouped_output_nested_agg(ray_t* expr, bool inside_agg) {
    if (!expr || expr->type != RAY_LIST || expr->len < 1) return NULL;
    ray_t** el = (ray_t**)ray_data(expr);
    if (select_alias_skip_form(el[0])) return NULL;
    bool agg = is_agg_expr(expr) != 0;
    if (agg && inside_agg) return expr;
    for (int64_t i = 1; i < expr->len; i++) {
        ray_t* hit = grouped_output_nested_agg(el[i], inside_agg || agg);
        if (hit) return hit;
    }
    return NULL;
}

/* ── Grouped alias planning ───────────────────────────────────────────────
 * A grouped select's outputs may name earlier outputs.  Two kinds of alias:
 *
 *   ROW    — the expression aggregates nothing (`vals: price`, `s: sym`,
 *            `p2: (* price 2)`): a value per source row.  Substituted by
 *            value where referenced, so `m: (max vals)` is `(max price)`.
 *   GROUP  — the expression contains an aggregate, or refers to a GROUP
 *            alias: one value per group.  Never substituted.  An output
 *            that refers to one outside an aggregate becomes a DERIVED
 *            output: it leaves the engine's dict and is evaluated after
 *            grouping, over the group result, where the alias is a column
 *            (by reference — copying the expression made a chain like
 *            `a3: (+ a2 a1)` double in size at every step).  Aggregates
 *            inside a derived output are computed by the engine under
 *            hidden names and read back from the result.  Inside an
 *            aggregate's argument a GROUP alias has no rows left to fold
 *            and is rejected.
 *
 * A lambda's formals shadow aliases in its body; `let` and `quote` forms
 * are not descended. */

enum { SEL_ALIAS_ROW = 0, SEL_ALIAS_GROUP = 1 };
#define SEL_ALIAS_MAX_NODES 65536

typedef struct {
    int64_t  n_derived;
    int64_t* derived_names;   /* [n_derived] */
    ray_t**  derived_exprs;   /* [n_derived], owned */
    int64_t  n_hidden;
    int64_t* hidden_names;    /* [n_hidden]: engine outputs to drop from the result */
    bool     sort_after;      /* asc:/desc:/take: taken out of the engine's dict */
    ray_t*   hdr;             /* scratch block behind the arrays */
} select_alias_plan_t;

static void select_alias_plan_free(select_alias_plan_t* p) {
    for (int64_t i = 0; i < p->n_derived; i++)
        if (p->derived_exprs[i]) ray_release(p->derived_exprs[i]);
    if (p->hdr) scratch_free(p->hdr);
    memset(p, 0, sizeof(*p));
}

static bool select_sym_is(ray_t* s, const char* name, size_t len) {
    if (!s || s->type != -RAY_SYM || (s->attrs & ATTR_QUOTED)) return false;
    ray_t* str = ray_sym_str(s->i64);
    return str && ray_str_len(str) == len && memcmp(ray_str_ptr(str), name, len) == 0;
}

static bool select_lambda_form(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST || expr->len < 3) return false;
    ray_t** el = (ray_t**)ray_data(expr);
    return select_sym_is(el[0], "fn", 2) && ray_is_vec(el[1]) && el[1]->type == RAY_SYM;
}

/* Aggregate calls anywhere in `expr` (is_agg_expr, the predicate
 * select_extract_aggs extracts on — wider than the DAG-shaped count that
 * sizes the engine's own hidden slots). */
static int64_t count_agg_calls(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST) return 0;
    int64_t c = is_agg_expr(expr) ? 1 : 0;
    ray_t** e = (ray_t**)ray_data(expr);
    for (int64_t i = 0; i < expr->len; i++) c += count_agg_calls(e[i]);
    return c;
}

/* True when `expr` reads a column of `tbl` (bare or literal name; a
 * lambda's formals shadow).  Used on a ROW alias's expression when it is
 * substituted outside an aggregate: the source column it carries is then
 * read per row, whatever name it came in under. */
static bool select_expr_reads_col(ray_t* expr, ray_t* tbl, const int64_t* shadow, int n_shadow) {
    if (!expr) return false;
    if (expr->type == -RAY_SYM) {
        for (int i = 0; i < n_shadow; i++) if (shadow[i] == expr->i64) return false;
        return ray_table_get_col(tbl, expr->i64) != NULL;
    }
    if (expr->type != RAY_LIST || expr->len < 1) return false;
    ray_t** el = (ray_t**)ray_data(expr);
    if (select_sym_is(el[0], "quote", 5)) return false;
    if (select_lambda_form(expr)) {
        int64_t nf = ray_len(el[1]);
        ray_t* hdr = NULL;
        int64_t* merged = (int64_t*)scratch_alloc(&hdr, (size_t)(n_shadow + nf + 1) * sizeof(int64_t));
        if (!merged) return true;   /* cannot tell: report the safe answer */
        for (int i = 0; i < n_shadow; i++) merged[i] = shadow[i];
        for (int64_t i = 0; i < nf; i++) merged[n_shadow + i] = sym_cell_runtime_id(el[1], i);
        bool r = false;
        for (int64_t i = 2; i < expr->len && !r; i++)
            r = select_expr_reads_col(el[i], tbl, merged, n_shadow + (int)nf);
        scratch_free(hdr);
        return r;
    }
    for (int64_t i = 0; i < expr->len; i++)
        if (select_expr_reads_col(el[i], tbl, shadow, n_shadow)) return true;
    return false;
}

static int64_t expr_node_count(ray_t* expr, int64_t cap) {
    if (!expr) return 0;
    if (expr->type != RAY_LIST) return 1;
    int64_t n = 1;
    ray_t** el = (ray_t**)ray_data(expr);
    for (int64_t i = 0; i < expr->len && n < cap; i++)
        n += expr_node_count(el[i], cap - n);
    return n;
}

/* Rebuilds `expr` with element i replaced by f(el[i]) for i >= from; the
 * list is copied only once an element changes.  `sub` returns an owned
 * expression.  Shared by the three walkers below. */
typedef ray_t* (*select_alias_map_fn)(ray_t* e, void* ctx);

static ray_t* select_alias_map_list(ray_t* expr, int64_t from, select_alias_map_fn fn, void* ctx) {
    ray_t** el = (ray_t**)ray_data(expr);
    ray_t* out = NULL;
    for (int64_t i = from; i < expr->len; i++) {
        ray_t* e2 = fn(el[i], ctx);
        if (!e2 || RAY_IS_ERR(e2)) { if (out) ray_release(out); return e2 ? e2 : ray_error("oom", NULL); }
        if (e2 != el[i] && !out) {
            out = ray_list_new(expr->len);
            if (!out || RAY_IS_ERR(out)) { ray_release(e2); return out ? out : ray_error("oom", NULL); }
            for (int64_t j = 0; j < i; j++) {
                out = ray_list_append(out, el[j]);
                if (!out || RAY_IS_ERR(out)) { ray_release(e2); return out ? out : ray_error("oom", NULL); }
            }
        }
        if (out) {
            out = ray_list_append(out, e2);
            ray_release(e2);
            if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
        } else {
            ray_release(e2);
        }
    }
    if (!out) { ray_retain(expr); return expr; }
    return out;
}

/* ── Eval-fallback pre-reduction of aggregates ────────────────────────────
 * A projection that mixes a row column with an aggregate in one expression
 * (`(+ i (max i))`, `(if (> i (avg i)) 1 0)`) is scattered per row by the
 * eval fallback, where every column is bound to one cell: the aggregate
 * then folds that cell alone and `(max i)` reads as `i`.  Before the
 * scatter, every maximal aggregate call in the expression is evaluated once
 * over the whole table (eval_expr_whole_column, earlier outputs already
 * bound as columns) and its value spliced into a rewritten copy of the
 * expression, so the per-row pass combines each row with the value the DAG
 * path would have broadcast.  A value the evaluator would not take as
 * itself — a symbol (a name, or a literal the active-query rule maps to a
 * column) or a list (a call) — is wrapped in `(quote …)`; a one-element
 * vector is the scalar it stands for, as select_fallback_broadcast reads
 * it.  Lambda, let and quote forms are left alone: a lambda's body folds
 * its own formal per row, not a column.  The user's AST is never mutated
 * (select_alias_map_list copies a list only once a child changes).
 * Returns an owned expression, or an error. */
typedef struct { ray_t* tbl; } select_fallback_prereduce_ctx_t;

static ray_t* select_fallback_prereduce(ray_t* expr, void* vctx) {
    select_fallback_prereduce_ctx_t* c = (select_fallback_prereduce_ctx_t*)vctx;
    if (!expr || expr->type != RAY_LIST || expr->len < 1) { ray_retain(expr); return expr; }
    ray_t** el = (ray_t**)ray_data(expr);
    if (select_alias_skip_form(el[0])) { ray_retain(expr); return expr; }
    if (!is_agg_expr(expr))
        return select_alias_map_list(expr, el[0]->type == RAY_LIST ? 0 : 1,
                                     select_fallback_prereduce, c);
    ray_t* v = eval_expr_whole_column(expr, c->tbl);
    if (!v || RAY_IS_ERR(v)) return v ? v : ray_error("oom", NULL);
    if (v->type > 0 && v->type != RAY_LIST && ray_is_vec(v) && v->len == 1) {
        int allocated = 0;
        ray_t* a = collection_elem(v, 0, &allocated);
        if (a && !RAY_IS_ERR(a) && !allocated) ray_retain(a);
        ray_release(v);
        if (!a || RAY_IS_ERR(a)) return a ? a : ray_error("oom", NULL);
        v = a;
    }
    if (v->type == -RAY_SYM) {
        /* a literal copy (ray_quote_fn's shape): no name resolution, and
         * collect_col_refs does not take it for a column of the same name */
        ray_t* q = ray_sym(v->i64);
        ray_release(v);
        if (!q || RAY_IS_ERR(q)) return q ? q : ray_error("oom", NULL);
        q->attrs |= ATTR_QUOTED;
        v = q;
    } else if (v->type != RAY_LIST) {
        return v;
    }
    ray_t* head = ray_sym(ray_sym_intern("quote", 5));
    ray_t* wrap = head && !RAY_IS_ERR(head) ? ray_list_new(2) : NULL;
    if (wrap && !RAY_IS_ERR(wrap)) wrap = ray_list_append(wrap, head);
    if (wrap && !RAY_IS_ERR(wrap)) wrap = ray_list_append(wrap, v);
    if (head && !RAY_IS_ERR(head)) ray_release(head);
    ray_release(v);
    if (!wrap || RAY_IS_ERR(wrap)) return wrap ? wrap : ray_error("oom", NULL);
    return wrap;
}

typedef struct {
    const int64_t* names;
    ray_t**        exprs;
    const uint8_t* kinds;
    int            n_alias;
    ray_t*         tbl;
    bool           in_agg;
    const int64_t* shadow;
    int            n_shadow;
    bool           changed;
    bool           refs_group;
    bool           refs_row;     /* a source column read outside an aggregate */
    const int64_t* keys;         /* by: names — columns of the group result, not per-row reads */
    int            n_keys;
} select_alias_subst_ctx_t;

static bool select_sym_in(int64_t sym, const int64_t* syms, int n) {
    for (int i = 0; i < n; i++) if (syms[i] == sym) return true;
    return false;
}

static ray_t* select_alias_subst(ray_t* expr, void* vctx) {
    select_alias_subst_ctx_t* c = (select_alias_subst_ctx_t*)vctx;
    if (!expr) return NULL;
    if (expr->type == -RAY_SYM) {
        for (int i = 0; i < c->n_shadow; i++)
            if (c->shadow[i] == expr->i64) { ray_retain(expr); return expr; }
        /* inside an aggregate's argument a source column is the source column */
        if (c->in_agg && ray_table_get_col(c->tbl, expr->i64)) { ray_retain(expr); return expr; }
        for (int i = c->n_alias - 1; i >= 0; i--) {
            if (c->names[i] != expr->i64) continue;
            if (c->kinds[i] == SEL_ALIAS_GROUP) {
                if (c->in_agg) {
                    ray_t* s = ray_sym_str(expr->i64);
                    return ray_error("domain",
                        "select by: `%.*s` is an aggregate of the group and cannot be aggregated again",
                        s ? (int)ray_str_len(s) : 1, s ? ray_str_ptr(s) : "?");
                }
                c->refs_group = true;
                ray_retain(expr); return expr;
            }
            c->changed = true;
            if (!c->in_agg && select_expr_reads_col(c->exprs[i], c->tbl, c->keys, c->n_keys)) c->refs_row = true;
            ray_retain(c->exprs[i]);
            return c->exprs[i];
        }
        if (!c->in_agg && !select_sym_in(expr->i64, c->keys, c->n_keys) &&
            ray_table_get_col(c->tbl, expr->i64)) c->refs_row = true;
        ray_retain(expr); return expr;
    }
    if (expr->type != RAY_LIST || expr->len < 1) { ray_retain(expr); return expr; }
    ray_t** el = (ray_t**)ray_data(expr);
    if (select_sym_is(el[0], "let", 3) || select_sym_is(el[0], "quote", 5)) { ray_retain(expr); return expr; }
    if (select_lambda_form(expr)) {
        int64_t nf = ray_len(el[1]);
        ray_t* sh_hdr = NULL;
        int64_t* merged = (int64_t*)scratch_alloc(&sh_hdr, (size_t)(c->n_shadow + nf + 1) * sizeof(int64_t));
        if (!merged) return ray_error("oom", NULL);
        for (int i = 0; i < c->n_shadow; i++) merged[i] = c->shadow[i];
        for (int64_t i = 0; i < nf; i++) merged[c->n_shadow + i] = sym_cell_runtime_id(el[1], i);
        select_alias_subst_ctx_t inner = *c;
        inner.shadow = merged; inner.n_shadow = c->n_shadow + (int)nf;
        ray_t* out = select_alias_map_list(expr, 2, select_alias_subst, &inner);
        scratch_free(sh_hdr);
        c->changed |= inner.changed; c->refs_group |= inner.refs_group; c->refs_row |= inner.refs_row;
        return out;
    }
    select_alias_subst_ctx_t inner = *c;
    inner.in_agg = c->in_agg || select_alias_head_is_agg(el[0]);
    /* a call whose head is itself a form — `((fn [x] …) 1)` — is walked
     * from the head, which is where such a lambda's body lives */
    ray_t* out = select_alias_map_list(expr, el[0]->type == RAY_LIST ? 0 : 1,
                                       select_alias_subst, &inner);
    c->changed |= inner.changed; c->refs_group |= inner.refs_group; c->refs_row |= inner.refs_row;
    return out;
}

/* Replaces every aggregate call in a derived output by a hidden engine
 * output `__ad<k>` (added to `*engine`) and returns the rewritten
 * expression, owned.  Lambda, let and quote forms are left alone. */
typedef struct {
    ray_t** engine;
    select_alias_plan_t* plan;
    const int64_t* shadow;   /* lambda formals in scope: an aggregate over one stays put */
    int n_shadow;
} select_extract_ctx_t;

static bool select_expr_mentions(ray_t* expr, const int64_t* syms, int n) {
    if (!expr) return false;
    if (expr->type == -RAY_SYM) {
        for (int i = 0; i < n; i++) if (syms[i] == expr->i64) return true;
        return false;
    }
    if (expr->type != RAY_LIST) return false;
    ray_t** el = (ray_t**)ray_data(expr);
    for (int64_t i = 0; i < expr->len; i++)
        if (select_expr_mentions(el[i], syms, n)) return true;
    return false;
}

static ray_t* select_extract_aggs(ray_t* expr, void* vctx) {
    select_extract_ctx_t* c = (select_extract_ctx_t*)vctx;
    if (!expr || expr->type != RAY_LIST || expr->len < 1) { ray_retain(expr); return expr; }
    ray_t** el = (ray_t**)ray_data(expr);
    if (select_sym_is(el[0], "let", 3) || select_sym_is(el[0], "quote", 5)) { ray_retain(expr); return expr; }
    if (select_lambda_form(expr)) {
        int64_t nf = ray_len(el[1]);
        ray_t* hdr = NULL;
        int64_t* merged = (int64_t*)scratch_alloc(&hdr, (size_t)(c->n_shadow + nf + 1) * sizeof(int64_t));
        if (!merged) return ray_error("oom", NULL);
        for (int i = 0; i < c->n_shadow; i++) merged[i] = c->shadow[i];
        for (int64_t i = 0; i < nf; i++) merged[c->n_shadow + i] = sym_cell_runtime_id(el[1], i);
        select_extract_ctx_t inner = *c;
        inner.shadow = merged; inner.n_shadow = c->n_shadow + (int)nf;
        ray_t* out = select_alias_map_list(expr, 2, select_extract_aggs, &inner);
        scratch_free(hdr);
        return out;
    }
    if (is_agg_expr(expr) && !select_expr_mentions(expr, c->shadow, c->n_shadow)) {
        char buf[24];
        int bn = snprintf(buf, sizeof buf, "__ad%lld", (long long)c->plan->n_hidden);
        int64_t hn = ray_sym_intern(buf, (size_t)bn);
        ray_t* key = ray_sym(hn);
        if (!key || RAY_IS_ERR(key)) return key ? key : ray_error("oom", NULL);
        *c->engine = ray_dict_upsert(*c->engine, key, expr);   /* retains expr */
        ray_release(key);
        if (!*c->engine || RAY_IS_ERR(*c->engine)) { ray_t* e = *c->engine; *c->engine = NULL; return e ? e : ray_error("oom", NULL); }
        c->plan->hidden_names[c->plan->n_hidden++] = hn;
        return ray_sym(hn);
    }
    return select_alias_map_list(expr, el[0]->type == RAY_LIST ? 0 : 1, select_extract_aggs, c);
}

/* Plans the grouped select: NULL when the dict has no by: or nothing refers
 * to an earlier alias (nothing to do); an error; or an owned dict for the
 * engine (ROW aliases substituted, derived outputs and — when there are
 * any — asc:/desc:/take: removed, hidden aggregates added, from: replaced by
 * the evaluated table) with `plan` filled in for select_apply_derived. */
/* Does `expr` mention (bare or literal) any of the first `n` names? */
static bool select_expr_mentions_key(ray_t* expr, ray_t* keys, int64_t n) {
    if (!expr) return false;
    if (expr->type == -RAY_SYM) {
        for (int64_t i = 0; i < n; i++) if (sym_cell_runtime_id(keys, i) == expr->i64) return true;
        return false;
    }
    if (expr->type != RAY_LIST) return false;
    ray_t** el = (ray_t**)ray_data(expr);
    for (int64_t i = 0; i < expr->len; i++)
        if (select_expr_mentions_key(el[i], keys, n)) return true;
    return false;
}

static ray_t* select_plan_grouped_aliases(ray_t* dict, ray_t* tbl, select_alias_plan_t* plan) {
    memset(plan, 0, sizeof(*plan));
    if (!dict || dict->type != RAY_DICT || !dict_get(dict, "by")) return NULL;
    ray_t* keys = ray_dict_keys(dict);
    ray_t* vals = ray_dict_vals(dict);
    if (!keys || keys->type != RAY_SYM || !vals || vals->type != RAY_LIST) return NULL;
    int64_t nd = ray_dict_len(dict);
    /* Most grouped selects name no earlier output: leave before any
     * allocation unless some value mentions a key that precedes it. */
    {
        bool any = false;
        for (int64_t i = 1; i < nd && !any; i++)
            any = select_expr_mentions_key(((ray_t**)ray_data(vals))[i], keys, i);
        if (!any) return NULL;
    }
    static const char* const reserved[] = { "from", "where", "by", "take", "asc", "desc", "nearest" };
    int64_t rid[7];
    for (int i = 0; i < 7; i++) rid[i] = ray_sym_intern(reserved[i], strlen(reserved[i]));
    int64_t hidden_max = 1;
    for (int64_t i = 0; i < nd; i++)
        hidden_max += count_agg_calls(((ray_t**)ray_data(vals))[i]);
    ray_t* work_hdr = NULL;
    int64_t* names = (int64_t*)scratch_alloc(&work_hdr,
        (size_t)(nd > 0 ? nd : 1) * (sizeof(int64_t) + sizeof(ray_t*) + sizeof(uint8_t)));
    if (!names) return ray_error("oom", NULL);
    ray_t** exprs = (ray_t**)(names + nd);
    uint8_t* kinds = (uint8_t*)(exprs + nd);
    plan->derived_names = (int64_t*)scratch_alloc(&plan->hdr,
        (size_t)(nd > 0 ? nd : 1) * (sizeof(int64_t) + sizeof(ray_t*)) + (size_t)hidden_max * sizeof(int64_t));
    if (!plan->derived_names) { scratch_free(work_hdr); return ray_error("oom", NULL); }
    plan->derived_exprs = (ray_t**)(plan->derived_names + nd);
    plan->hidden_names  = (int64_t*)(plan->derived_exprs + nd);
    /* The by: names: a group key is a column of the group result (one value
     * per group), so reading it beside a GROUP alias is not a per-row read. */
    ray_t* by_expr = dict_get(dict, "by");
    ray_t* keys_hdr = NULL;
    int64_t* key_syms = NULL;
    int n_key_syms = 0;
    {
        int64_t nk = 0;
        if (by_expr && by_expr->type == -RAY_SYM) nk = 1;
        else if (by_expr && ray_is_vec(by_expr) && by_expr->type == RAY_SYM) nk = ray_len(by_expr);
        else if (by_expr && by_expr->type == RAY_DICT) nk = ray_dict_len(by_expr);
        key_syms = (int64_t*)scratch_alloc(&keys_hdr, (size_t)(nk > 0 ? nk : 1) * sizeof(int64_t));
        if (!key_syms) { scratch_free(work_hdr); select_alias_plan_free(plan); return ray_error("oom", NULL); }
        if (by_expr && by_expr->type == -RAY_SYM) key_syms[n_key_syms++] = by_expr->i64;
        else if (by_expr && ray_is_vec(by_expr) && by_expr->type == RAY_SYM)
            for (int64_t k = 0; k < nk; k++) key_syms[n_key_syms++] = sym_cell_runtime_id(by_expr, k);
        else if (by_expr && by_expr->type == RAY_DICT) {
            ray_t* bk = ray_dict_keys(by_expr);
            if (bk && bk->type == RAY_SYM)
                for (int64_t k = 0; k < nk; k++) key_syms[n_key_syms++] = sym_cell_runtime_id(bk, k);
        }
    }
    int n_alias = 0;
    ray_t* engine = NULL;            /* owned once anything changes */
    ray_t* err = NULL;
    for (int64_t i = 0; i < nd && !err; i++) {
        int64_t kid = sym_cell_runtime_id(keys, i);
        bool is_reserved = false;
        for (int r = 0; r < 7; r++) if (rid[r] == kid) { is_reserved = true; break; }
        if (is_reserved) continue;
        ray_t* v = ((ray_t**)ray_data(vals))[i];
        select_alias_subst_ctx_t sc = { names, exprs, kinds, n_alias, tbl, false, NULL, 0, false, false, false, key_syms, n_key_syms };
        ray_t* v1 = select_alias_subst(v, &sc);
        if (!v1 || RAY_IS_ERR(v1)) { err = v1 ? v1 : ray_error("oom", NULL); break; }
        if (sc.refs_group && sc.refs_row) {
            /* one value per group beside a value per row: the output has
             * no single shape; the column needs an aggregate around it */
            ray_t* s = ray_sym_str(kid);
            ray_release(v1);
            err = ray_error("domain", "select by: output `%.*s` combines a source column with a per-group value; aggregate the column",
                            s ? (int)ray_str_len(s) : 1, s ? ray_str_ptr(s) : "?");
            break;
        }
        if (sc.changed && expr_node_count(v1, SEL_ALIAS_MAX_NODES) >= SEL_ALIAS_MAX_NODES) {
            ray_t* s = ray_sym_str(kid);
            ray_release(v1);
            err = ray_error("limit", "select by: output `%.*s` grows too large once its aliases are expanded",
                            s ? (int)ray_str_len(s) : 1, s ? ray_str_ptr(s) : "?");
            break;
        }
        ray_t* key = ray_sym(kid);
        if (!key || RAY_IS_ERR(key)) { ray_release(v1); err = key ? key : ray_error("oom", NULL); break; }
        if (sc.refs_group) {
            /* derived: out of the engine's dict, aggregates inside it in */
            if (!engine) { engine = dict; ray_retain(engine); }
            engine = ray_dict_remove(engine, key);
            if (!engine || RAY_IS_ERR(engine)) { ray_release(key); ray_release(v1); err = engine ? engine : ray_error("oom", NULL); engine = NULL; break; }
            select_extract_ctx_t xc = { &engine, plan, NULL, 0 };
            ray_t* v2 = select_extract_aggs(v1, &xc);
            ray_release(v1);
            if (!v2 || RAY_IS_ERR(v2)) { ray_release(key); err = v2 ? v2 : ray_error("oom", NULL); break; }
            plan->derived_names[plan->n_derived] = kid;
            plan->derived_exprs[plan->n_derived] = v2;      /* owned by the plan */
            plan->n_derived++;
            names[n_alias] = kid; exprs[n_alias] = v2; kinds[n_alias] = SEL_ALIAS_GROUP;
            n_alias++;
            ray_release(key);
            continue;
        }
        if (sc.changed) {
            if (!engine) { engine = dict; ray_retain(engine); }
            engine = ray_dict_upsert(engine, key, v1);      /* retains v1 */
            if (!engine || RAY_IS_ERR(engine)) { ray_release(key); ray_release(v1); err = engine ? engine : ray_error("oom", NULL); engine = NULL; break; }
        }
        ray_release(key);
        /* bind AFTER the value: visible to later outputs only.  exprs[]
         * borrows v1 from the dict it now lives in. */
        names[n_alias] = kid;
        exprs[n_alias] = v1;
        kinds[n_alias] = expr_contains_agg(v1) ? SEL_ALIAS_GROUP : SEL_ALIAS_ROW;
        n_alias++;
        ray_release(v1);
    }
    scratch_free(work_hdr);
    scratch_free(keys_hdr);
    if (err) { if (engine) ray_release(engine); select_alias_plan_free(plan); return err; }
    if (!engine) { select_alias_plan_free(plan); return NULL; }
    /* Sort keys and take: may name a derived output: apply them after. */
    if (plan->n_derived > 0) {
        for (int r = 3; r <= 5; r++) {
            ray_t* key = ray_sym(rid[r]);
            if (!key || RAY_IS_ERR(key)) { ray_release(engine); select_alias_plan_free(plan); return key ? key : ray_error("oom", NULL); }
            if (dict_get(dict, reserved[r])) plan->sort_after = true;
            engine = ray_dict_remove(engine, key);
            ray_release(key);
            if (!engine || RAY_IS_ERR(engine)) { select_alias_plan_free(plan); return engine ? engine : ray_error("oom", NULL); }
        }
    }
    ray_t* from_key = ray_sym(rid[0]);
    if (!from_key || RAY_IS_ERR(from_key)) { ray_release(engine); select_alias_plan_free(plan); return from_key ? from_key : ray_error("oom", NULL); }
    engine = ray_dict_upsert(engine, from_key, tbl);
    ray_release(from_key);
    if (!engine || RAY_IS_ERR(engine)) { select_alias_plan_free(plan); return engine ? engine : ray_error("oom", NULL); }
    return engine;
}

/* A derived output is evaluated once over the group result with every
 * column bound (vector semantics) unless it holds a form that takes one
 * condition or one value — `if` and the other control forms, a lambda
 * body — in which case it is evaluated row by row. */
static bool select_derived_needs_rows(ray_t* expr) {
    if (!expr || expr->type != RAY_LIST || expr->len < 1) return false;
    ray_t** el = (ray_t**)ray_data(expr);
    if (el[0]->type != -RAY_SYM || (el[0]->attrs & ATTR_QUOTED)) return true;   /* ((fn …) …) and friends */
    if (agg_arith_head_is_control(el[0]->i64) || select_sym_is(el[0], "cond", 4)) return true;
    ray_t* gv = ray_env_get(el[0]->i64);
    if (gv && gv->type == RAY_LAMBDA) return true;
    for (int64_t i = 1; i < expr->len; i++)
        if (select_derived_needs_rows(el[i])) return true;
    return false;
}

/* Evaluates the derived outputs over the engine's result (in order, each
 * one visible to the next), drops the hidden aggregate columns, restores
 * the dict's output order and applies the sort keys and take: that were
 * held back.  Consumes `result`. */
static ray_t* select_apply_derived(ray_t* result, ray_t* dict, select_alias_plan_t* plan) {
    if (!result || RAY_IS_ERR(result)) return result;
    if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
    if (!result || RAY_IS_ERR(result)) return result;
    if (result->type != RAY_TABLE) return result;
    int64_t n_groups = ray_table_nrows(result);
    for (int64_t d = 0; d < plan->n_derived; d++) {
        ray_t* expr = plan->derived_exprs[d];
        ray_t* col = select_derived_needs_rows(expr)
            ? eval_expr_per_row(expr, result, n_groups)
            : eval_expr_whole_column(expr, result);
        if (!col || RAY_IS_ERR(col)) { ray_release(result); return col ? col : ray_error("oom", NULL); }
        if (ray_is_lazy(col)) col = ray_lazy_materialize(col);
        if (!col || RAY_IS_ERR(col)) { ray_release(result); return col ? col : ray_error("oom", NULL); }
        if (!(ray_is_vec(col) || col->type == RAY_LIST) || ray_len(col) != n_groups) {
            ray_t* nm = ray_sym_str(plan->derived_names[d]);
            ray_release(col); ray_release(result);
            return ray_error("domain", "select by: output `%.*s` did not evaluate to one value per group",
                             nm ? (int)ray_str_len(nm) : 1, nm ? ray_str_ptr(nm) : "?");
        }
        result = select_fallback_bind_alias(result, plan->derived_names[d], col);
        ray_release(col);
        if (!result || RAY_IS_ERR(result)) return result ? result : ray_error("oom", NULL);
    }
    /* Key columns first (whatever the engine emitted that is not an output),
     * then the outputs in the dict's order, hidden aggregates dropped. */
    DICT_VIEW_DECL(dv);
    DICT_VIEW_OPEN(dict, dv);
    if (DICT_VIEW_OVERFLOW(dv)) { DICT_VIEW_CLOSE(dv); ray_release(result); return ray_error("oom", NULL); }
    int64_t from_id = ray_sym_intern("from", 4), where_id = ray_sym_intern("where", 5), by_id = ray_sym_intern("by", 2);
    int64_t take_id = ray_sym_intern("take", 4), asc_id = ray_sym_intern("asc", 3), desc_id = ray_sym_intern("desc", 4);
    int64_t nearest_id = ray_sym_intern("nearest", 7);
    int64_t ncols = ray_table_ncols(result);
    ray_t* out = ray_table_new(ncols);
    if (!out || RAY_IS_ERR(out)) { DICT_VIEW_CLOSE(dv); ray_release(result); return out ? out : ray_error("oom", NULL); }
    for (int64_t c = 0; c < ncols && out && !RAY_IS_ERR(out); c++) {
        int64_t cn = ray_table_col_name(result, c);
        bool is_output = false, is_hidden = false;
        for (int64_t i = 0; i + 1 < dv_n; i += 2)
            if (dv[i]->i64 == cn && cn != from_id && cn != where_id && cn != by_id && cn != take_id && cn != asc_id && cn != desc_id && cn != nearest_id) { is_output = true; break; }
        for (int64_t h = 0; h < plan->n_hidden; h++) if (plan->hidden_names[h] == cn) { is_hidden = true; break; }
        if (is_output || is_hidden) continue;
        out = ray_table_add_col(out, cn, ray_table_get_col_idx(result, c));
    }
    for (int64_t i = 0; i + 1 < dv_n && out && !RAY_IS_ERR(out); i += 2) {
        int64_t cn = dv[i]->i64;
        if (cn == from_id || cn == where_id || cn == by_id || cn == take_id || cn == asc_id || cn == desc_id || cn == nearest_id) continue;
        ray_t* col = ray_table_get_col(result, cn);
        if (!col) continue;   /* an output the engine folded away (e.g. a key projection) */
        out = ray_table_add_col(out, cn, col);
    }
    if (!out || RAY_IS_ERR(out)) { DICT_VIEW_CLOSE(dv); ray_release(result); return out ? out : ray_error("oom", NULL); }
    ray_release(result);
    result = out;
    if (plan->sort_after)
        result = apply_sort_take(result, dv, dv_n, asc_id, desc_id, take_id, NULL);
    DICT_VIEW_CLOSE(dv);
    return result;
}

ray_t* ray_select(ray_t** args, int64_t n) {
    return ray_select_impl(args, n, false);
}

/* The hidden name a bare computed by: key is grouped under (#707). */
#define SELECT_COMPUTED_KEY "__by_key"

/* A bare computed by: key (`by: (% v 3)`) groups on the DAG path only beside
 * plain aggregates.  Any other output (a `distinct` aggregate, a row
 * expression) would send it to the eval-level grouping, which is slower
 * (10-20% at 10M rows); the by-dict form `{k: (% v 3)}` materializes the key
 * once and keeps the DAG path's speed for every output shape.  So
 * that is the query this plans: the same dict with by: as a one-key by-dict
 * under a hidden name, from: the table already evaluated, and a sort key
 * naming the derived key column redirected to the hidden one.  The caller
 * renames the hidden column to `*key_name`, the name the DAG path gives a
 * computed key.  NULL when the shape needs no rewrite. */
static ray_t* select_plan_computed_key(ray_t* dict, ray_t* tbl, int64_t* key_name) {
    ray_t* by_expr = dict_get(dict, "by");
    if (!by_expr || by_expr->type != RAY_LIST || ray_len(by_expr) < 1) return NULL;
    ray_t* keys = ray_dict_keys(dict);
    ray_t* vals = ray_dict_vals(dict);
    if (!keys || keys->type != RAY_SYM || !vals || vals->type != RAY_LIST) return NULL;
    static const char* const reserved[] = { "from", "where", "by", "take", "asc", "desc", "nearest" };
    int64_t rid[7];
    for (int i = 0; i < 7; i++) rid[i] = ray_sym_intern(reserved[i], strlen(reserved[i]));
    int64_t nd = ray_dict_len(dict);
    int64_t hidden = ray_sym_intern(SELECT_COMPUTED_KEY, strlen(SELECT_COMPUTED_KEY));
    int64_t kname = derived_key_name(by_expr);
    bool needs_eval = false;
    for (int64_t i = 0; i < nd; i++) {
        int64_t kid = sym_cell_runtime_id(keys, i);
        bool clause = false;
        for (int r = 0; r < 7; r++) if (kid == rid[r]) clause = true;
        if (clause) continue;
        if (kid == kname) kname = ray_sym_intern("key", 3);
        ray_t* v = ((ray_t**)ray_data(vals))[i];
        /* the routing's own test for an output the DAG group cannot serve */
        if (is_single_group_key_projection(by_expr, v)) continue;
        if (is_group_dag_agg_expr(v)) continue;
        if (is_decomposable_agg_compound(v, tbl)) continue;
        needs_eval = true;
    }
    if (!needs_eval || ray_table_get_col(tbl, hidden)) return NULL;

    ray_t* bk = ray_vec_new(RAY_SYM, 1);
    ray_t* bv = ray_list_new(1);
    if (bk && !RAY_IS_ERR(bk)) { ((int64_t*)ray_data(bk))[0] = hidden; bk->len = 1; }
    if (bv && !RAY_IS_ERR(bv)) bv = ray_list_append(bv, by_expr);
    ray_t* by_dict = ray_dict_new(bk, bv);
    if (!by_dict || RAY_IS_ERR(by_dict)) return by_dict ? by_dict : ray_error("oom", NULL);

    ray_t* engine = dict;
    ray_retain(engine);
    ray_t* k_by = ray_sym(rid[2]);
    engine = k_by && !RAY_IS_ERR(k_by) ? ray_dict_upsert(engine, k_by, by_dict) : engine;
    if (k_by) ray_release(k_by);
    ray_release(by_dict);
    ray_t* k_from = ray_sym(rid[0]);
    if (engine && !RAY_IS_ERR(engine) && k_from && !RAY_IS_ERR(k_from))
        engine = ray_dict_upsert(engine, k_from, tbl);
    if (k_from) ray_release(k_from);
    /* asc:/desc: name result columns: the derived key is the hidden one */
    for (int r = 4; r <= 5 && engine && !RAY_IS_ERR(engine); r++) {
        ray_t* sv = dict_get(engine, reserved[r]);
        ray_t* nv = NULL;
        if (sv && sv->type == -RAY_SYM && sv->i64 == kname) {
            nv = ray_sym(hidden);
            if (nv && !RAY_IS_ERR(nv)) nv->attrs = sv->attrs;
        } else if (sv && sv->type == RAY_SYM) {
            int64_t n = ray_len(sv);
            bool hit = false;
            for (int64_t j = 0; j < n; j++) if (sym_cell_runtime_id(sv, j) == kname) hit = true;
            if (hit) {
                nv = ray_vec_new(RAY_SYM, n);
                if (nv && !RAY_IS_ERR(nv)) {
                    for (int64_t j = 0; j < n; j++) {
                        int64_t id = sym_cell_runtime_id(sv, j);
                        ((int64_t*)ray_data(nv))[j] = id == kname ? hidden : id;
                    }
                    nv->len = n;
                }
            }
        }
        if (!nv) continue;
        if (RAY_IS_ERR(nv)) { ray_release(engine); return nv; }
        ray_t* k_s = ray_sym(rid[r]);
        if (!k_s || RAY_IS_ERR(k_s)) { ray_release(nv); ray_release(engine); return k_s ? k_s : ray_error("oom", NULL); }
        engine = ray_dict_upsert(engine, k_s, nv);
        ray_release(k_s);
        ray_release(nv);
    }
    if (!engine || RAY_IS_ERR(engine)) return engine ? engine : ray_error("oom", NULL);
    *key_name = kname;
    return engine;
}

/* Names the hidden key column of a select_plan_computed_key result. */
static ray_t* select_name_computed_key(ray_t* r, int64_t key_name) {
    if (r && !RAY_IS_ERR(r) && ray_is_lazy(r)) r = ray_lazy_materialize(r);
    if (!r || RAY_IS_ERR(r) || r->type != RAY_TABLE) return r;
    int64_t hidden = ray_sym_intern(SELECT_COMPUTED_KEY, strlen(SELECT_COMPUTED_KEY));
    int64_t nc = ray_table_ncols(r);
    for (int64_t c = 0; c < nc; c++)
        if (ray_table_col_name(r, c) == hidden) { ray_table_set_col_name(r, c, key_name); break; }
    return r;
}

static ray_t* ray_select_impl(ray_t** args, int64_t n, bool aliases_resolved) {
    if (n < 1) return ray_error("arity", "select: expects a query dict, got %lld args", (long long)n);
    ray_t* dict = args[0];
    if (!dict || dict->type != RAY_DICT)
        return ray_error("type", "select: query must be a dict, got %s", dict ? ray_type_name(dict->type) : "null");

    /* Evaluate 'from:' to get the source table */
    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr) return ray_error("domain", "select: missing `from:` clause");
    ray_t* where_expr = dict_get(dict, "where");
    ray_group_emit_filter_t prev_emit_filter = ray_group_emit_filter_get();
    ray_group_emit_filter_t emit_filter = {0};
    bool emit_filter_set = match_group_count_emit_filter(
        from_expr, where_expr, &emit_filter);
    if (emit_filter_set) {
        /* armed for the DIRECT child select evaluated by the from: below */
        emit_filter.target_depth = (__VM ? __VM->eval_depth : 0) + 1;
        ray_group_emit_filter_set(emit_filter);
    }
    /* Projection pushdown: publish the columns this select references so an
     * IMMEDIATE nested `select {by:}` distinct in `from:` (eval_depth+1) carries
     * only those.  Stack buffer stays live across the from: eval; save/restore
     * keeps it scoped and nesting-safe. */
    /* proj_buf holds the pushdown keep-set (over-approximated column-ref syms
     * from collect_syms).  Sized from proj_keep_bound — an over-count of the
     * sym atoms proj_compute_keep can collect — so the keep-set never
     * saturates.  (The old dict-key-count carve under-sized this: an agg
     * output `(count X)` yields 2 syms for 1 dict key and `desc:`/`asc:` add an
     * alias sym, so the count filled the buffer and pushdown was wrongly
     * declined — q11 +340%.)  Published to pvm->proj_keep only across the
     * from: eval; freed right after the restore. */
    int64_t proj_hdr_keys = proj_keep_bound(dict);
    ray_t*  proj_hdr = NULL;
    int64_t* proj_buf = (int64_t*)scratch_alloc(&proj_hdr,
        (size_t)(proj_hdr_keys > 0 ? proj_hdr_keys : 1) * sizeof(int64_t));
    int proj_n = 0;
    ray_vm_t* pvm = __VM;
    int32_t        pj_d = pvm ? pvm->proj_depth   : 0;
    const int64_t* pj_k = pvm ? pvm->proj_keep    : NULL;
    int            pj_n = pvm ? pvm->proj_keep_n  : 0;
    bool           pj_a = pvm ? pvm->proj_active  : false;
    if (pvm && proj_buf && proj_compute_keep(dict, proj_buf, (int)proj_hdr_keys, &proj_n)) {
        pvm->proj_keep = proj_buf; pvm->proj_keep_n = proj_n;
        pvm->proj_depth = pvm->eval_depth; pvm->proj_active = true;
    }
    ray_t* tbl = ray_eval(from_expr);
    if (pvm) { pvm->proj_keep = pj_k; pvm->proj_keep_n = pj_n;
               pvm->proj_depth = pj_d; pvm->proj_active = pj_a; }
    scratch_free(proj_hdr);   /* proj_buf dead past the from: eval + restore */
    if (emit_filter_set)
        ray_group_emit_filter_set(prev_emit_filter);
    if (RAY_IS_ERR(tbl)) return tbl;
    if (tbl->type == RAY_DICT) {
        bool complete;
        ray_t* scan = ray_parquet_select_source(tbl,dict,&complete);
        if (scan) { ray_release(tbl); tbl = scan; }
        if (complete) return tbl;
        if (RAY_IS_ERR(tbl)) return tbl;
    }
    if (tbl->type != RAY_TABLE) { int8_t tbl_t = tbl->type; ray_release(tbl); return ray_error("type", "select: `from:` must evaluate to a table, got %s", ray_type_name(tbl_t)); }

    if (!aliases_resolved) {
        select_alias_plan_t plan;
        ray_t* engine = select_plan_grouped_aliases(dict, tbl, &plan);
        if (engine && RAY_IS_ERR(engine)) { ray_release(tbl); return engine; }
        if (engine) {
            ray_t* r = ray_select_impl(&engine, 1, true);
            if (plan.n_derived > 0) r = select_apply_derived(r, dict, &plan);
            select_alias_plan_free(&plan);
            ray_release(engine);
            ray_release(tbl);
            return r;
        }
    }

    {
        int64_t key_name = -1;
        ray_t* engine = select_plan_computed_key(dict, tbl, &key_name);
        if (engine && RAY_IS_ERR(engine)) { ray_release(tbl); return engine; }
        if (engine) {
            ray_t* r = ray_select_impl(&engine, 1, true);
            ray_release(engine);
            ray_release(tbl);
            return select_name_computed_key(r, key_name);
        }
    }

    ray_t* temporal_result = try_temporal_group_materialize(dict, tbl);
    if (temporal_result) { ray_release(tbl); return temporal_result; }

    ray_t* by_expr = dict_get(dict, "by");
    ray_t* take_expr = dict_get(dict, "take");
    ray_t* nearest_expr = dict_get(dict, "nearest");

    /* Collect output columns (keys that are not reserved).  The dict's
     * physical layout is [keys, vals] but the iteration loops below were
     * written for the old interleaved [k0,v0,...] form — open a transient
     * pair view so the existing code keeps working. */
    DICT_VIEW_DECL(dv);
    DICT_VIEW_OPEN(dict, dv);
    if (DICT_VIEW_OVERFLOW(dv)) {
        ray_release(tbl);
        DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
    }
    int64_t dict_n = dv_n;
    ray_t** dict_elems = dv;
    int64_t from_id    = dict_key_id(dict, "from");
    int64_t where_id   = dict_key_id(dict, "where");
    int64_t by_id      = dict_key_id(dict, "by");
    int64_t take_id    = dict_key_id(dict, "take");
    int64_t asc_id     = dict_key_id(dict, "asc");
    int64_t desc_id    = dict_key_id(dict, "desc");
    int64_t nearest_id = dict_key_id(dict, "nearest");

    /* Check for asc/desc presence */
    bool has_sort = false;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == asc_id || kid == desc_id) { has_sort = true; break; }
    }

    /* `nearest` is mutually exclusive with `asc`/`desc`/`by` — ANN
     * ordering is an index scan, not a column sort, and cannot be
     * composed with group-by in this phase. */
    if (nearest_expr) {
        if (has_sort) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); return ray_error("domain",
                "select: `nearest` cannot be combined with asc/desc");
        }
        if (by_expr) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); return ray_error("domain",
                "select: `nearest` cannot be combined with `by`");
        }
    }

    /* Count-distinct planner rewrite: `(select {K: K c: (count (distinct X))
     * from: T [where: W] by: K [desc: c take: N]})` decomposes cleanly to
     * a two-stage group-by — first dedup (K, X) pairs, then count rows
     * per K.  The dedup pass lands on the v2 multi-key kernel; the
     * second pass walks a much smaller table.  Skips the outer-group +
     * idx_buf scatter that the per-group dedup path otherwise pays. */
    if (!nearest_expr) {
        ray_t* rw = try_count_distinct_v2_rewrite(
            tbl, by_expr, where_expr, dict_elems, dict_n,
            from_id, where_id, by_id, take_id, asc_id, desc_id, nearest_id);
        if (rw) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); return rw;
        }
    }

    /* Count output columns */
    int n_out = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid != from_id && kid != where_id && kid != by_id &&
            kid != take_id && kid != asc_id && kid != desc_id &&
            kid != nearest_id)
            n_out++;
    }

    /* Simple case: no clauses at all → return table as-is */
    if (n_out == 0 && !where_expr && !by_expr && !take_expr && !has_sort && !nearest_expr)
        { DICT_VIEW_CLOSE(dv); return tbl; }

    /* Whole-table aggregates answered from column metadata:
     * `(select {from: T a: (sum c) b: (min d) …})` with no other clause, each
     * output one of count / min / max / sum / avg over a plain column that
     * the metadata answers exactly — count is the row count; min/max read
     * the chunk-zone extrema of an integer or temporal column; sum and avg
     * read its per-chunk sums (avg only when no partial sum can leave
     * double's integer range, so the value is the row-wise one).  Any
     * output the metadata cannot answer leaves the query to the planner. */
    if (!where_expr && !by_expr && !take_expr && !has_sort && !nearest_expr && n_out > 0 &&
        tbl->type == RAY_TABLE) {
        ray_t* meta_res = select_aggs_from_metadata(tbl, dict_elems, dict_n, from_id);
        if (meta_res) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv);
            return meta_res;
        }
    }

    /* Streaming parted ORDER BY: `(select {…} from: PARTED asc/desc: KEY)`
     * with no by:/take:/nearest: over a table whose partitions are already
     * internally sorted on KEY and whose partition key-ranges are globally
     * ordered.  The sorted result is then just the ordered CONCATENATION of
     * the per-partition selects — no whole-table flatten, no O(total) sort
     * scratch.  Declines (returns NULL) to the flat path whenever any
     * eligibility gate fails; correctness never depends on the fast path. */
    if (has_sort && !by_expr && !take_expr && !nearest_expr) {
        ray_t* streamed = try_stream_parted_order_by(
            dict, tbl, dict_elems, dict_n,
            from_id, where_id, by_id, take_id, asc_id, desc_id, nearest_id);
        if (streamed) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv);
            return streamed;
        }
    }

    /* Streaming parted ORDER BY + LIMIT: `(select {COLS…} from: PARTED
     * [where: W] asc/desc: KEY take: k)` over a parted table.  Runs top-k on
     * each partition's segment (bounded), concats the ≤P·k candidates, and
     * takes the global top-k — output-bounded O(k), no whole-table flatten.
     * Broader eligibility than the pre-sorted concat path (no per-partition
     * sortedness required).  Declines (NULL) for non-parted / ineligible
     * shapes, so flat tables fall through to the fused top-k below. */
    if (has_sort && take_expr && !by_expr && !nearest_expr) {
        ray_t* streamed = try_stream_parted_order_by_topk(
            dict, tbl, dict_elems, dict_n,
            from_id, where_id, by_id, take_id, asc_id, desc_id, nearest_id);
        if (streamed) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv);
            return streamed;
        }
    }

    /* Fused filter + top-K: shape `(select {col1: c1 col2: c2 …
     * from: T where: <pred> asc/desc: <key> take: <K>})` with no by/
     * nearest clause and all output columns being plain SYM names of
     * source columns.  Bypasses the FILTER → SORT_TAKE pipeline: streams
     * predicate eval and bounded-heap insert in one pass, no
     * intermediate filtered table materialised.  Closes a large
     * latency gap on ORDER BY + LIMIT shapes that were previously
     * dominated by the filtered-table materialisation step. */
    if (take_expr && (has_sort || where_expr) && !by_expr && !nearest_expr) {
        if (!where_expr || ray_fused_topk_supported(where_expr, tbl)) {
            /* Walk the dict and check: exactly one asc/desc clause naming
             * a single scalar column, take is an atom K, and every
             * output column is a -RAY_SYM source-column reference (no
             * agg, no expression). */
            /* fast-path budget: >16 declines to the general sort (not a correctness cap) */
            int64_t sort_key_syms[16];
            uint8_t sort_descs[16];
            uint8_t n_sort_keys = 0;
            int     bad_clause = 0;
            int64_t out_syms[256];
            int64_t out_aliases[256];
            uint8_t n_out_syms = 0;
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                ray_t*  v   = dict_elems[i + 1];
                if (kid == from_id || kid == where_id || kid == take_id ||
                    kid == nearest_id) continue;
                if (kid == asc_id || kid == desc_id) {
                    uint8_t is_desc = (kid == desc_id) ? 1 : 0;
                    if (v && v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) {
                        if (n_sort_keys >= 16) { bad_clause = 1; break; }
                        sort_key_syms[n_sort_keys] = v->i64;
                        sort_descs[n_sort_keys] = is_desc;
                        n_sort_keys++;
                    } else if (v && v->type == RAY_SYM) {
                        int64_t nv = ray_len(v);
                        if (n_sort_keys + nv > 16) { bad_clause = 1; break; }
                        /* SYM vectors may be compact-width W8/W16/W32/W64;
                         * sym_cell_runtime_id does the width-specialised
                         * load AND re-expresses the cell id as a runtime
                         * name id for the column lookups below. */
                        for (int64_t kk = 0; kk < nv; kk++) {
                            sort_key_syms[n_sort_keys] =
                                sym_cell_runtime_id(v, kk);
                            sort_descs[n_sort_keys] = is_desc;
                            n_sort_keys++;
                        }
                    } else {
                        bad_clause = 1; break;
                    }
                    continue;
                }
                if (kid == by_id) { bad_clause = 1; break; }
                /* Output column must be a trivial projection of a source
                 * column.  The dict key is the alias the result publishes;
                 * the value names the source column to gather from. */
                if (n_out_syms >= 255) { bad_clause = 1; break; }
                /* Projections resolve left to right: a name bound by an
                 * earlier output is that output's value, not the source
                 * column.  The fused paths gather source columns, so such
                 * a shape is left to the planner. */
                if (v && v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) {
                    bool alias_ref = false;
                    for (uint8_t o = 0; o < n_out_syms; o++)
                        if (out_aliases[o] == v->i64 && out_syms[o] != v->i64) alias_ref = true;
                    if (alias_ref) { bad_clause = 1; break; }
                }
                if (v && v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) {
                    ray_t* oc = ray_table_get_col(tbl, v->i64);
                    if (!oc) { bad_clause = 1; break; }
                    int8_t ot = oc->type;
                    if (RAY_IS_PARTED(ot) || ot == RAY_MAPCOMMON)
                        { bad_clause = 1; break; }
                    if (!ray_is_vec(oc))
                        { bad_clause = 1; break; }
                    out_syms[n_out_syms]    = v->i64;
                    out_aliases[n_out_syms] = kid;
                    n_out_syms++;
                } else {
                    bad_clause = 1;
                    break;
                }
            }
            if (!bad_clause && n_out_syms == 0 && n_out == 0) {
                int64_t nc = ray_table_ncols(tbl);
                if (nc > 255) {
                    bad_clause = 1;
                } else {
                    for (int64_t c = 0; c < nc; c++) {
                        ray_t* oc = ray_table_get_col_idx(tbl, c);
                        if (!oc) { bad_clause = 1; break; }
                        int8_t ot = oc->type;
                        if (RAY_IS_PARTED(ot) || ot == RAY_MAPCOMMON)
                            { bad_clause = 1; break; }
                        if (!ray_is_vec(oc))
                            { bad_clause = 1; break; }
                        int64_t cn = ray_table_col_name(tbl, c);
                        out_syms[n_out_syms] = cn;
                        out_aliases[n_out_syms] = cn;
                        n_out_syms++;
                    }
                }
            }
            /* Sort keys: only verify the column exists.  Nulls are now
             * handled by the null-aware leg in fpk_cmp, which takes its
             * placement from sort_nulls_first like every other sort path:
             * a null is the smallest value, so first for ASC, last for DESC.
             * Output columns are also handled — the fused materialiser
             * propagates null bitmaps via ray_vec_set_null. */
            if (!bad_clause) {
                for (uint32_t i = 0; i < n_sort_keys && !bad_clause; i++) {
                    ray_t* kc = ray_table_get_col(tbl, sort_key_syms[i]);
                    if (!kc) bad_clause = 1;
                }
            }
            /* Without a sort the positional path takes only a literal K:
             * an expression is left to the general path, which evaluates
             * it exactly once. */
            if (n_sort_keys == 0 && !(ray_is_atom(take_expr) &&
                  (take_expr->type == -RAY_I64 || take_expr->type == -RAY_I32)))
                bad_clause = 1;
            if (!bad_clause && n_out_syms > 0) {
                ray_t* tv = ray_eval(take_expr);
                if (tv && !RAY_IS_ERR(tv) && ray_is_atom(tv) &&
                    (tv->type == -RAY_I64 || tv->type == -RAY_I32)) {
                    int64_t k = (tv->type == -RAY_I64) ? tv->i64 : tv->i32;
                    ray_release(tv);
                    /* Positional take under a filter, and an ascending
                     * take on a column known to be sorted (no nulls): the
                     * answer is the first |k| passing rows from one end of
                     * the table, found without scanning the rest. */
                    bool sorted_asc = false;
                    if (n_sort_keys == 1 && !sort_descs[0]) {
                        ray_t* kc = ray_table_get_col(tbl, sort_key_syms[0]);
                        sorted_asc = kc && (kc->attrs & RAY_ATTR_SORTED) &&
                                     !(kc->attrs & RAY_ATTR_SLICE) &&
                                     !ray_vec_may_have_nulls(kc);
                    }
                    if (where_expr && (n_sort_keys == 0 || (sorted_asc && k > 0)) &&
                        k != 0 && k != INT64_MIN &&
                        (k < 0 ? -k : k) <= FPK_MAX_K &&
                        (k < 0 ? -k : k) < ray_table_nrows(tbl)) {
                        ray_t* res = ray_fused_take_select(tbl, where_expr, k,
                                                           out_syms, out_aliases,
                                                           n_out_syms);
                        if (res && !RAY_IS_ERR(res)) {
                            ray_release(tbl);
                            DICT_VIEW_CLOSE(dv); return res;
                        }
                        if (res && RAY_IS_ERR(res)) ray_release(res);
                    }
                    if (n_sort_keys > 0 && k > 0 && k <= FPK_MAX_K && k < ray_table_nrows(tbl)) {
                        ray_t* res = ray_fused_topk_select(tbl, where_expr,
                                                           sort_key_syms,
                                                           sort_descs,
                                                           n_sort_keys, k,
                                                           out_syms,
                                                           out_aliases,
                                                           n_out_syms);
                        if (res && !RAY_IS_ERR(res)) {
                            ray_release(tbl);
                            DICT_VIEW_CLOSE(dv); return res;
                        }
                        if (res && RAY_IS_ERR(res)) ray_release(res);
                    }
                } else if (tv) {
                    ray_release(tv);
                }
            }
        }
    }

    /* Dict-form by-clause pre-evaluation: MUST happen before we
     * build the DAG, so the graph sees the augmented table with
     * the materialised dict-val columns already present.
     * (select {... by: {o: OrderId b: (xbar Ts 1000)} ...})
     * Dict values can be any expression; we eval each against tbl
     * with its columns bound as locals, add the result as a new
     * column named after the dict key, then rewrite by_expr as a
     * plain RAY_SYM vector of the dict keys so the rest of
     * ray_select_fn sees a standard multi-key group-by. */
    ray_t* by_sym_vec_owned = NULL;
    /* Parted by-dict streaming: when the by-dict's computed/renamed keys are
     * deferred (not eagerly flattened) so the per-partition group kernel can
     * stream them, deferred_bydict points at the original by-dict AST (whose
     * value ASTs the group-key compile site reads) and by_expr is rewritten to
     * a SYM vec of the ALIAS names.  parted_bydict_deferred stays false on the
     * FLAT path so nothing there changes. */
    bool   parted_bydict_deferred = false;
    bool   computed_single_key = false;   /* by: is one expression, compiled or const */
    /* Sort keys that are source columns no output scans: carried through
     * the projection (appended after the outputs) so the sort can read
     * them, dropped from the result after it ran. */
    int     n_hidden_sort = 0;
    ray_t* deferred_bydict = NULL;
    int64_t deferred_nk = 0;
    int64_t dep_key_base_sym = -1;
    /* Dependent affine group keys (by-dict xbar-bucket shapes): unbounded now
     * that the by-dict "1..16 keys" gate is lifted, so these can't be fixed
     * [16].  They must survive to the post-group emit (~10130), past this
     * function's many return sites — rather than heap-carve a pair that needs a
     * free at each exit, they ride the sel_slots_hdr collector block carved far
     * below (freed at every return past it).  The by-dict block collects into a
     * transient buffer (dep_src_*) that dep_src_hdr bridges to the copy-in point
     * where that block is carved. */
    int64_t* dep_key_names = NULL;    /* [n_dep_keys], into sel_slots_hdr */
    int64_t* dep_key_biases = NULL;   /* [n_dep_keys], into sel_slots_hdr */
    ray_t*   dep_src_hdr = NULL;       /* transient collector, freed at the copy-in / early exit */
    int64_t* dep_src_names = NULL;
    int64_t* dep_src_biases = NULL;
    int64_t n_dep_keys = 0;

    /* B3 Part 2: a LITERAL scalar by-key symbol (`by: 'g`) resolves like a
     * name-ref — the same column-only, query-only rule the projection/where
     * paths apply.  The dozens of downstream by-key checks expect a name-ref
     * (ATTR_QUOTED clear), so normalize ANY literal scalar by-key to a
     * name-ref once, here — regardless of whether the column exists.  A
     * literal naming a column then resolves to it; a literal naming no
     * column produces the SAME clean "column not found" error as a bare
     * `by: nonexistent` (rather than flowing into ray_elem_size on a literal
     * symbol and tripping UBSan).  The owned name-ref rides in
     * by_sym_vec_owned and is released with the other owned by-forms at
     * function exit.  Multi-key (RAY_DICT/list) and dotted by-forms are not
     * scalar -RAY_SYM literals and are left untouched. */
    if (by_expr && by_expr->type == -RAY_SYM && (by_expr->attrs & ATTR_QUOTED)) {
        ray_t* nref = ray_sym(by_expr->i64);
        if (nref && !RAY_IS_ERR(nref)) {
            by_sym_vec_owned = nref;
            by_expr = nref;
        } else if (nref) {
            ray_release(nref);
        }
    }

    /* Selection saved across the path-A graph free for count(distinct
     * col_ref) non-aggs.  Path B leaves this NULL because the
     * materialised filtered_tbl already encodes the selection in row
     * positions.  Declared here at function scope so the cleanup at
     * the bottom of ray_select_fn can release it. */
    ray_t* saved_selection = NULL;
    ray_t* post_group_where_expr = NULL;
    DICT_VIEW_DECL(byv);
    if (by_expr && by_expr->type == RAY_DICT) {
        ray_t* by_dict_ast = by_expr;   /* original by-dict; value ASTs read again at compile */
        DICT_VIEW_OPEN(by_expr, byv);
        if (DICT_VIEW_OVERFLOW(byv)) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL);
        }
        int64_t dlen = byv_n;
        int64_t nk = dlen / 2;
        if (nk == 0) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("domain", "by-dict must have at least 1 key");
        }
        ray_t** d_elems = byv;

        int64_t base_sym = -1;
        int64_t base_key_name = -1;
        bool dep_candidate = true;
        for (int64_t i = 0; i < nk && dep_candidate; i++) {
            ray_t* k = d_elems[i * 2];
            ray_t* v = d_elems[i * 2 + 1];
            if (!k || k->type != -RAY_SYM) {
                dep_candidate = false;
                break;
            }
            if (v && v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) {
                if (base_sym < 0) {
                    base_sym = v->i64;
                    base_key_name = k->i64;
                }
                if (v->i64 == base_sym)
                    continue;
            }
        }
        if (dep_candidate && base_sym >= 0) {
            if (base_key_name != base_sym)
                dep_candidate = false;
        }
        if (dep_candidate && base_sym >= 0) {
            ray_t* base_col = ray_table_get_col(tbl, base_sym);
            dep_candidate = base_col && key_type_i64_projectable(base_col->type) &&
                            !ray_vec_has_nulls(base_col);
        }
        /* Exact-size carve (unbounded): at most nk dependent-key entries can
         * ever be collected (one per by-dict pair).  On the dependent-key
         * success path this buffer is handed to dep_src_hdr and survives to the
         * sel_slots_hdr copy-in below; otherwise it is freed within this block. */
        ray_t* local_dep_hdr = NULL;
        int64_t* local_dep_names = NULL;
        int64_t* local_dep_biases = NULL;
        int64_t local_n_dep = 0;
        if (dep_candidate && base_sym >= 0) {
            local_dep_names = (int64_t*)scratch_alloc(&local_dep_hdr,
                    (size_t)nk * 2 * sizeof(int64_t));
            if (!local_dep_names) {
                ray_release(tbl);
                DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL);
            }
            local_dep_biases = local_dep_names + nk;
            for (int64_t i = 0; i < nk && dep_candidate; i++) {
                ray_t* k = d_elems[i * 2];
                ray_t* v = d_elems[i * 2 + 1];
                bool duplicate_key = false;
                for (int64_t j = 0; j < i && !duplicate_key; j++)
                    if (d_elems[j * 2]->i64 == k->i64) duplicate_key = true;
                if (duplicate_key) {
                    dep_candidate = false;
                    break;
                }
                bool already_in_tbl = (ray_table_get_col(tbl, k->i64) != NULL);
                bool trivial_self = (v->type == -RAY_SYM && v->i64 == k->i64);
                if (already_in_tbl && !trivial_self) {
                    dep_candidate = false;
                    break;
                }
                int64_t bias = 0;
                if (!expr_affine_of_sym(v, base_sym, &bias)) {
                    dep_candidate = false;
                    break;
                }
                if (k->i64 != base_key_name || bias != 0) {
                    local_dep_names[local_n_dep] = k->i64;
                    local_dep_biases[local_n_dep] = bias;
                    local_n_dep++;
                }
            }
        }
        if (dep_candidate && base_sym >= 0 && local_n_dep > 0) {
            by_sym_vec_owned = ray_vec_new(RAY_SYM, 1);
            if (!by_sym_vec_owned || RAY_IS_ERR(by_sym_vec_owned)) {
                scratch_free(local_dep_hdr);
                ray_release(tbl);
                DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL);
            }
            ((int64_t*)ray_data(by_sym_vec_owned))[0] = base_key_name;
            by_sym_vec_owned->len = 1;
            by_expr = by_sym_vec_owned;
            dep_key_base_sym = base_key_name;
            n_dep_keys = local_n_dep;
            /* Hand the collected names/biases to the function-scope bridge; the
             * copy into the sel_slots_hdr collector block (and this buffer's
             * free) happen once that block is carved below.  local_dep_hdr is
             * now owned by dep_src_hdr — do NOT free it here. */
            dep_src_hdr = local_dep_hdr;
            dep_src_names = local_dep_names;
            dep_src_biases = local_dep_biases;
            goto by_dict_done;
        }
        scratch_free(local_dep_hdr);

        /* ── Parted by-dict streaming (defer computed/renamed keys) ───────────
         * For a PARTED input, do NOT eagerly flatten + ray_eval + add_col each
         * computed/renamed by-dict value.  Instead keep by_expr as a SYM vec of
         * the ALIAS names and hand the value ASTs to the group-key compile site,
         * which calls compile_expr_dag per key: a bare column ref degenerates to
         * exactly the ray_scan the flat multi-key path uses; a computed expr
         * becomes a real DAG node that exec_group_parted streams per partition
         * (materialising it into a synthetic per-partition column).  The result
         * key columns are renamed to the aliases after execution.
         *
         * Gated hard so the FLAT path stays byte-identical and correctness never
         * depends on the optimisation — any gate failure falls through to the
         * unchanged eager materialize below:
         *   - input actually parted.  A WHERE is now permitted: it installs a
         *     rowsel over the parted table's global row space (segment order,
         *     no flatten) and exec_group_parted applies it PER PARTITION,
         *     composing with the per-partition materialization of the deferred
         *     computed keys (both index the same segment rows).  A single-key
         *     keys-only HAVING is still split off to post_group_where_expr
         *     below; a value-column WHERE (the streaming case) stays a
         *     selection and rides the per-partition path;
         *   - at least one output and EVERY output a plain group aggregate
         *     (non-agg / count-distinct / compound-arith outputs re-read the
         *     materialized key columns post-group — the deferred alias column is
         *     absent, so those shapes keep the eager path);
         *   - every key a bare scalar-column rename or a computed expression,
         *     never a STR/LIST/GUID key (which routes to the eval toy grouper
         *     that gathers key columns by name and would drop the deferred key);
         *   - no duplicate key and no alias shadowing an input column (same
         *     name-only validation the eager loop performs). */
        bool by_input_parted = false;
        {
            int64_t in_nc = ray_table_ncols(tbl);
            for (int64_t c = 0; c < in_nc; c++) {
                ray_t* col = ray_table_get_col_idx(tbl, c);
                if (col && (RAY_IS_PARTED(col->type) ||
                            col->type == RAY_MAPCOMMON)) {
                    by_input_parted = true; break;
                }
            }
        }
        if (by_input_parted && n_out > 0) {
            bool defer_ok = true;
            for (int64_t i = 0; i + 1 < dict_n && defer_ok; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id ||
                    kid == take_id || kid == asc_id || kid == desc_id ||
                    kid == nearest_id) continue;
                if (!is_group_dag_agg_expr(dict_elems[i + 1])) defer_ok = false;
            }
            for (int64_t i = 0; i < nk && defer_ok; i++) {
                ray_t* k = d_elems[i * 2];
                ray_t* v = d_elems[i * 2 + 1];
                if (!k || k->type != -RAY_SYM) { defer_ok = false; break; }
                for (int64_t j = 0; j < i; j++)
                    if (d_elems[j * 2]->i64 == k->i64) { defer_ok = false; break; }
                if (!defer_ok) break;
                bool trivial_self = (v && v->type == -RAY_SYM && v->i64 == k->i64);
                if (ray_table_get_col(tbl, k->i64) && !trivial_self) {
                    defer_ok = false; break;   /* alias shadows an input column */
                }
                if (v && v->type == RAY_LIST) continue;  /* computed expr */
                if (v && v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) {
                    ray_t* src = ray_table_get_col(tbl, v->i64);
                    if (!src ||
                        !query_key_type_streamable(query_col_base_type(src))) {
                        defer_ok = false; break;
                    }
                    continue;
                }
                defer_ok = false;   /* literal / quoted / unsupported by-val */
            }
            if (defer_ok) {
                by_sym_vec_owned = ray_vec_new(RAY_SYM, nk);
                if (!by_sym_vec_owned || RAY_IS_ERR(by_sym_vec_owned)) {
                    ray_release(tbl);
                    DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL);
                }
                int64_t* sv = (int64_t*)ray_data(by_sym_vec_owned);
                for (int64_t i = 0; i < nk; i++) sv[i] = d_elems[i * 2]->i64;
                by_sym_vec_owned->len = nk;
                by_expr = by_sym_vec_owned;
                parted_bydict_deferred = true;
                deferred_bydict = by_dict_ast;
                deferred_nk = nk;
                goto by_dict_done;
            }
        }

        bool has_computed_by_val = false;
        for (int64_t i = 0; i < nk; i++) {
            ray_t* k = d_elems[i * 2];
            ray_t* v = d_elems[i * 2 + 1];
            if (!k || k->type != -RAY_SYM) continue;
            /* A bare unquoted source-column ref ({s: sym}) is a pure
             * RENAME, not a computed key: the generic by-dict path below
             * adds the source column under the alias (same vector object,
             * same attached index) and the query flows down the main
             * GROUP pipeline.  Routing renames through the computed-key
             * prefilter forced a filtered materialize of every referenced
             * column and hid the key column's index from the group side. */
            bool bare_ref = v && v->type == -RAY_SYM &&
                            !(v->attrs & ATTR_QUOTED) &&
                            ray_table_get_col(tbl, v->i64) != NULL;
            if (!(v && v->type == -RAY_SYM && v->i64 == k->i64) && !bare_ref) {
                has_computed_by_val = true;
                break;
            }
        }
        ray_group_emit_filter_t prefilter_top_count;
        memset(&prefilter_top_count, 0, sizeof(prefilter_top_count));
        bool prefilter_top_n_match =
            match_group_desc_count_take(dict_elems, dict_n, from_id, where_id,
                                        by_id, take_id, asc_id, desc_id,
                                        &prefilter_top_count);
        /* A computed by-key (e.g. `(xbar EventTime …)`) is materialised over
         * every input row before grouping, so a selective WHERE wins from the
         * project→filter→eval-key prefilter REGARDLESS of how the result is
         * ordered.  Gating on the top-N-by-agg shape left the common
         * "group by time-bucket, order asc by the bucket" shape (q42) on the
         * slow full-materialisation path; the prefilter body below only needs
         * a WHERE and is order-agnostic, so the top-N match is not required. */
        bool prefilter_computed_by = has_computed_by_val;
        /* Multi-key WHERE shape — same kind of win even with bare-ref
         * by-vals when at least one aggregate has a *distinct* input
         * column (SUM / MIN / MAX / AVG on something other than a
         * by-key).  mk_par_v2's wide composite path then reads those
         * extra inputs from the *original* wide table at the sparse
         * positions left by the WHERE filter; for very wide tables with
         * selective WHEREs the
         * gather wastes a cache line per touched column per passing
         * row.  Count-only shapes (q14: SearchEngineID/SearchPhrase
         * keys, count of SearchPhrase) don't carry the extra agg col
         * — narrowing costs the projection without saving the gather. */
        bool prefilter_multi_key_where = false;
        if (prefilter_top_n_match && !has_computed_by_val && nk >= 2) {
            int64_t count_sym = ray_sym_intern("count", 5);
            for (int64_t i = 0; i + 1 < dict_n &&
                                !prefilter_multi_key_where; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id ||
                    kid == take_id || kid == asc_id || kid == desc_id ||
                    kid == nearest_id) continue;
                ray_t* val = dict_elems[i + 1];
                if (!is_group_dag_agg_expr(val)) continue;
                ray_t** ae = (ray_t**)ray_data(val);
                if (!ae[0] || ae[0]->type != -RAY_SYM) continue;
                if (ae[0]->i64 == count_sym) continue;
                prefilter_multi_key_where = true;  /* sum/min/max/avg */
            }
        }
        /* Computed by-val + WHERE: eagerly evaluating a non-trivial
         * group key (e.g. q42's `(xbar EventTime 60000000000)`) over
         * every input row wastes work proportional to the WHERE's
         * selectivity.  Project the input table down to just the
         * columns the rest of the (select …) clause actually touches
         * (WHERE refs, by-val refs, agg-input refs, sort-key refs),
         * filter the narrow projection through WHERE once, then
         * evaluate by-val expressions on the small dense result.  The
         * downstream group/sort/take then sees a fully-filtered table
         * — fewer rows, fewer columns, no per-row redundant work.
         *
         * Narrowing matters: for wide tables, materialising the full
         * filtered table dominates what was meant to be a cheap prefilter
         * (single-col filter is O(passing × esz), full filter scales with
         * every projected column). */
        if (where_expr && (prefilter_computed_by || prefilter_multi_key_where)) {
            /* Exact-size carve: collect_col_refs_set dedups against real
             * table columns, so ray_table_ncols(tbl) is a hard upper bound —
             * no silent cap on wide tables past the former [256]. */
            int64_t ncols_max = ray_table_ncols(tbl);
            if (ncols_max < 1) ncols_max = 1;
            ray_t* keep_hdr = NULL;
            int64_t* keep_syms = (int64_t*)scratch_alloc(&keep_hdr,
                    (size_t)ncols_max * sizeof(int64_t));
            if (!keep_syms) { ray_release(tbl); DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL); }
            int n_keep = 0;
            n_keep = collect_col_refs_set(where_expr, tbl,
                                          keep_syms, (int)ncols_max, n_keep);
            for (int64_t i = 0; i + 1 < dict_n && n_keep < ncols_max; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == take_id ||
                    kid == nearest_id) continue;
                /* asc:/desc:/by: keep the value's referenced source cols
                 * (the by-dict's dict val may be a computed expression
                 * referencing other source cols, the asc/desc value is
                 * a -RAY_SYM or RAY_SYM vec of source col names).  All
                 * other entries are output cols — agg or non-agg
                 * expressions whose refs we also need post-filter. */
                n_keep = collect_col_refs_set(dict_elems[i + 1], tbl,
                                              keep_syms, (int)ncols_max, n_keep);
            }
            int can_project = (n_keep > 0 && n_keep < ncols_max);
            ray_t* narrow_tbl = NULL;
            if (can_project) {
                narrow_tbl = project_table_cols(tbl, keep_syms, n_keep);
                if (!narrow_tbl || RAY_IS_ERR(narrow_tbl)) {
                    if (narrow_tbl) ray_release(narrow_tbl);
                    narrow_tbl = NULL;
                    can_project = 0;
                }
            }
            scratch_free(keep_hdr);
            ray_t* prefilter_input = can_project ? narrow_tbl : tbl;
            ray_graph_t* fg = ray_graph_new(prefilter_input);
            if (!fg) {
                if (narrow_tbl) ray_release(narrow_tbl);
                ray_release(tbl);
                DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL);
            }
            ray_op_t* froot = ray_const_table(fg, prefilter_input);
            ray_t* pred_err = NULL;
            ray_op_t* pred = compile_where_predicate(fg, prefilter_input,
                                                     where_expr, "select",
                                                     &pred_err);
            if (!pred) {
                ray_graph_free(fg);
                if (narrow_tbl) ray_release(narrow_tbl);
                ray_release(tbl);
                DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv);
                return pred_err ? pred_err :
                    ray_error("domain", "select: failed to evaluate `where:` predicate");
            }
            froot = ray_filter(fg, froot, pred);
            /* Deliberately skip ray_optimize: its predicate pushdown
             * pass splits OP_AND into chained OP_FILTERs, each
             * materialising a per-conjunct bool vec and refining a
             * rowsel.  For wide AND-of-comparison WHEREs that costs
             * one parallel pass per conjunct (~50MB of intermediate
             * bool-vec writes for q42's 5-clause WHERE on 10M rows).
             * Single ray_filter with the unsplit AND-tree evaluates
             * the whole predicate inline in one parallel pass. */
            ray_t* filtered = ray_execute(fg, froot);
            ray_graph_free(fg);
            if (narrow_tbl) ray_release(narrow_tbl);
            if (!filtered || RAY_IS_ERR(filtered)) {
                ray_release(tbl);
                DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return filtered ? filtered : ray_error("domain", "select: `where:` filter produced no result");
            }
            if (ray_is_lazy(filtered))
                filtered = ray_lazy_materialize(filtered);
            if (!filtered || RAY_IS_ERR(filtered)) {
                ray_release(tbl);
                DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return filtered ? filtered : ray_error("domain", "select: failed to materialize filtered result");
            }
            ray_release(tbl);
            tbl = filtered;
            where_expr = NULL;
        }

        ray_env_push_query_scope();
        int64_t in_ncols = ray_table_ncols(tbl);
        for (int64_t c = 0; c < in_ncols; c++) {
            int64_t cn = ray_table_col_name(tbl, c);
            ray_t* cv = ray_table_get_col_idx(tbl, c);
            if (cv) ray_env_set_query_local(cn, cv);
        }

        by_sym_vec_owned = ray_vec_new(RAY_SYM, nk);
        if (!by_sym_vec_owned || RAY_IS_ERR(by_sym_vec_owned)) {
            ray_env_pop_scope();
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return ray_error("oom", NULL);
        }
        int64_t* sv_data = (int64_t*)ray_data(by_sym_vec_owned);
        by_sym_vec_owned->len = nk;

        bool failed = false;
        ray_t* fail_err = NULL;
        int64_t expected_len = ray_table_nrows(tbl);
        for (int64_t i = 0; i < nk; i++) {
            ray_t* k = d_elems[i * 2];
            ray_t* v = d_elems[i * 2 + 1];
            if (!k || k->type != -RAY_SYM) {
                fail_err = ray_error("domain", "by-dict key must be a symbol name");
                failed = true; break;
            }
            /* Duplicate key guard: {g: A g: B} would otherwise append
             * two cols both named g, then group on the first g twice
             * (silently dropping B).  Reject explicitly. */
            bool duplicate_key = false;
            for (int64_t j = 0; j < i && !duplicate_key; j++)
                if (d_elems[j * 2]->i64 == k->i64) duplicate_key = true;
            if (duplicate_key) {
                fail_err = ray_error("domain", "by-dict has duplicate key");
                failed = true; break;
            }
            /* Collision check: if the dict key already exists in the
             * input table, ray_table_add_col would append a second
             * column with the same name and ray_table_get_col finds
             * the ORIGINAL, so the group-by would silently scan the
             * user's existing column instead of our materialised
             * one.  The one allowed exception is {x: x}, a trivial
             * self-alias: the input column is already exactly what
             * we want to group on. */
            bool already_in_tbl = (ray_table_get_col(tbl, k->i64) != NULL);
            bool trivial_self = (v->type == -RAY_SYM && v->i64 == k->i64);
            if (already_in_tbl && !trivial_self) {
                fail_err = ray_error("domain",
                    "by-dict alias shadows an existing input column");
                failed = true; break;
            }
            if (trivial_self) {
                /* No eval / no add: just group on the existing col. */
                sv_data[i] = k->i64;
                continue;
            }
            ray_t* col_vec = group_key_eval(v, tbl);
            if (!col_vec || RAY_IS_ERR(col_vec)) {
                fail_err = col_vec ? col_vec : ray_error("domain", "by-dict val eval");
                failed = true; break;
            }
            if (!ray_is_vec(col_vec) || ray_len(col_vec) != expected_len) {
                ray_release(col_vec);
                fail_err = ray_error("length", "by-dict val must be a column vector");
                failed = true; break;
            }
            ray_t* new_tbl = ray_table_add_col(tbl, k->i64, col_vec);
            ray_release(col_vec);
            if (!new_tbl || RAY_IS_ERR(new_tbl)) {
                fail_err = new_tbl ? new_tbl : ray_error("oom", NULL);
                failed = true; break;
            }
            tbl = new_tbl;
            /* Re-bind the newly added column under its dict key so
             * later dict vals can reference earlier keys. */
            ray_env_set_query_local(k->i64, col_vec);
            sv_data[i] = k->i64;
        }
        ray_env_pop_scope();
        if (failed) {
            ray_release(by_sym_vec_owned);
            ray_release(tbl);
            DICT_VIEW_CLOSE(dv); DICT_VIEW_CLOSE(byv); return fail_err;
        }
        by_expr = by_sym_vec_owned;
    }
by_dict_done:
    /* byv is dead past the by-dict block (reached here by fall-through or the
     * dependent-key `goto` above); free its carve once (DICT_VIEW_CLOSE nulls
     * byv_hdr itself, so later exits are safe re-closing it; function-scoped
     * decl, dv-threaded returns below only close dv). */
    DICT_VIEW_CLOSE(byv);

    /* Build DAG */
    ray_graph_t* g = ray_graph_new(tbl);
    if (!g) {
        scratch_free(dep_src_hdr);   /* bridged dep-key collector (dep path only; NULL otherwise) */
        if (by_sym_vec_owned) ray_release(by_sym_vec_owned);
        ray_release(tbl); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
    }

    ray_op_t* root = ray_const_table(g, tbl);

    /* Non-agg expression tracking for post-DAG scatter (used in GROUP BY).
     * Arith-of-aggs decomposition (see try_decompose_agg_arith): hidden agg
     * subexpressions ride the DAG group pass as extra slots; each compound
     * output keeps its rewritten wrapper for one post-group eval over the
     * n_groups-row result.  All per-output collector arrays are carved from
     * one exact-size scratch block (bounds computed once from the dict view),
     * living until the function's result return — freed beside every
     * ray_graph_free(g) exit that follows. */
    int64_t n_out_max = select_output_count(dict_elems, dict_n);
    int64_t hidden_max = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id ||
            kid == nearest_id) continue;
        hidden_max += count_agg_subexprs(dict_elems[i + 1]);
    }
    if (n_out_max < 1) n_out_max = 1;          /* zero-size alloc guard */
    if (hidden_max < 1) hidden_max = 1;
    /* Group keys and aggregate slots also collect into this same block: the
     * key count is bounded by the by-clause and the agg count by outputs plus
     * hidden decomposed slots.  All bounds are known here, so one exact-size
     * carve serves every per-output/per-key collector and is freed on every
     * exit that follows (no separate per-scope headers to leak). */
    int64_t nk_max = (by_expr && ray_is_vec(by_expr) && by_expr->type == RAY_SYM)
                         ? ray_len(by_expr) : 1;
    if (nk_max < 1) nk_max = 1;
    int64_t aggs_max = n_out_max + hidden_max;
    /* One block for all collector arrays (8-byte elems first, uint16 tail).
     * The dependent-key names/biases (dep_key_*, at most n_dep_keys each, both
     * int64) tail the 8-byte region so they share this block's lifetime — freed
     * at every return past here, so no per-exit free of a separate carve. */
    ray_t* sel_slots_hdr = NULL;
    int64_t* nonagg_names = (int64_t*)scratch_alloc(&sel_slots_hdr,
        (size_t)n_out_max * (3 * sizeof(int64_t) + 2 * sizeof(ray_t*)) +
        (size_t)hidden_max * (sizeof(ray_t*) + sizeof(int64_t)) +
        (size_t)nk_max * sizeof(ray_op_t*) +
        (size_t)aggs_max * (2 * sizeof(ray_op_t*) + 2 * sizeof(int64_t) + sizeof(uint16_t)) +
        (size_t)2 * (size_t)n_dep_keys * sizeof(int64_t) +
        (size_t)(nk_max + 2 * aggs_max) * sizeof(uint32_t));
    if (!nonagg_names) {
        scratch_free(dep_src_hdr);
        if (by_sym_vec_owned) ray_release(by_sym_vec_owned);
        ray_graph_free(g); ray_release(tbl); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
    }
    ray_t**  nonagg_exprs     = (ray_t**)(nonagg_names + n_out_max);
    int64_t* compound_names   = (int64_t*)(nonagg_exprs + n_out_max);
    int64_t* compound_pos     = (int64_t*)(compound_names + n_out_max);
    ray_t**  compound_rw      = (ray_t**)(compound_pos + n_out_max);
    ray_t**  hidden_agg_exprs = (ray_t**)(compound_rw + n_out_max);
    int64_t* hidden_agg_names = (int64_t*)(hidden_agg_exprs + hidden_max);
    ray_op_t** key_ops        = (ray_op_t**)(hidden_agg_names + hidden_max);
    ray_op_t** agg_ins        = key_ops + nk_max;
    ray_op_t** agg_ins2       = agg_ins + aggs_max;
    int64_t*   agg_k          = (int64_t*)(agg_ins2 + aggs_max);
    int64_t*   agg_names      = agg_k + aggs_max;
    dep_key_names             = agg_names + aggs_max;      /* [n_dep_keys] */
    dep_key_biases            = dep_key_names + n_dep_keys; /* [n_dep_keys] */
    /* Node ids of key_ops / agg_ins / agg_ins2.  g->nodes grows by realloc
     * as expressions compile, so a pointer taken from one compile is stale
     * after the next; the ids are captured with each compile and the
     * pointers re-resolved from them right before the group node is built. */
    uint32_t*  key_ids        = (uint32_t*)(dep_key_biases + n_dep_keys); /* [nk_max] */
    uint32_t*  agg_in_ids     = key_ids + nk_max;                         /* [aggs_max] */
    uint32_t*  agg_in2_ids    = agg_in_ids + aggs_max;                    /* [aggs_max] */
    uint16_t*  agg_ops        = (uint16_t*)(agg_in2_ids + aggs_max);
    /* Copy the bridged dependent-key names/biases into this block, then release
     * the transient collector.  n_dep_keys == 0 (non-dep path) → no-op, and
     * dep_src_hdr stays NULL. */
    for (int64_t i = 0; i < n_dep_keys; i++) {
        dep_key_names[i]  = dep_src_names[i];
        dep_key_biases[i] = dep_src_biases[i];
    }
    scratch_free(dep_src_hdr); dep_src_hdr = NULL;
    int64_t n_nonaggs = 0;  /* bounded by n_out_max (nonagg_names/_exprs size) */
    int     n_hidden_aggs = 0;
    int     n_compound = 0;
    int     n_aggs_real = 0;           /* DAG agg slots before hidden ones */
    int synth_count_col = 0;  /* 1 if we synthesized OP_COUNT for group boundaries */
    int has_list_agg = 0;     /* 1 if any aggregate emits a LIST column (top/bot) */

    if (where_expr && by_expr && !nearest_expr &&
        can_defer_single_key_where(by_expr, where_expr, tbl)) {
        post_group_where_expr = where_expr;
        where_expr = NULL;
    }

    /* OP_FILTERED_GROUP has been retired.  The (select … where … by …)
     * shape now routes unconditionally through the general FILTER +
     * OP_GROUP subgraph, which reaches the v2 group-by engine (it
     * consumes the WHERE selection in place). */

    /* Apply WHERE filter. */
    if (where_expr) {
        /* When the WHERE is `(and a b c …)`, compile each conjunct as its
         * own OP_FILTER.
         * exec_filter on a TABLE input refines g->selection in place via
         * ray_rowsel_refine, so subsequent conjuncts only need to make
         * their predicate's output bool VALID for surviving rows — the
         * refine pass only reads pred[r] when r is still in the rowsel.
         *
         * This unlocks short-circuit-style behaviour for predicate
         * evaluators that opt into selection-aware execution
         * (exec_like).  Cheap conjuncts (binary_range comparisons) still
         * evaluate over the full table, but the AND-merge is replaced
         * by progressively-refined rowsel chaining — saving the OP_AND
         * binary AND-of-bool-vecs work. */
        int and_chained = 0;
        if (where_expr->type == RAY_LIST
            && ray_len(where_expr) >= 3)
        {
            ray_t** elems = (ray_t**)ray_data(where_expr);
            ray_t* head = elems[0];
            if (head && head->type == -RAY_SYM) {
                ray_t* hs = ray_sym_str(head->i64);
                if (hs && ray_str_len(hs) == 3
                    && memcmp(ray_str_ptr(hs), "and", 3) == 0)
                {
                    int64_t k = ray_len(where_expr) - 1;
                    /* Each conjunct must be a vec-producing predicate —
                     * compile to a non-OP_CONST node whose output type
                     * resolves to a vector.  Pure-literal conjuncts
                     * (e.g. `(== 1 0)`) compile to OP_CONST atoms that
                     * the OP_FILTER lazy path doesn't handle; fall back
                     * to the existing OP_AND tree, which ray_optimize
                     * already constant-folds. */
                    int all_ok = 1;
                    /* `reorder_safe` flips to 0 when any conjunct contains
                     * an op that can fault, error or have observable
                     * side effects — division, modulo, call into a user
                     * function, etc.  In that case we still chain the
                     * conjuncts as separate filters (the selection-aware
                     * win), but evaluate them in user-given order so an
                     * earlier guard like `(!= y 0)` keeps protecting a
                     * later `(< (/ x y) 10)`. */
                    int reorder_safe = 1;
                    ray_op_t* compiled[64];
                    int       cost[64];
                    if (k > 64) all_ok = 0;
                    /* Each conjunct must compile, must not be a pure
                     * constant, and must transitively reference at
                     * least one OP_SCAN (column read).  Pure-literal
                     * conjuncts compile to atom/scalar outputs that the
                     * lazy OP_FILTER path can't refine into a rowsel —
                     * fall back to the OP_AND tree (ray_optimize already
                     * constant-folds those).  Compute a coarse cost
                     * estimate per conjunct so we can sort cheap-first;
                     * cheap predicates make subsequent expensive ones
                     * evaluate over a far smaller rowsel. */
                    for (int64_t i = 0; i < k && all_ok; i++) {
                        ray_op_t* p = compile_expr_dag(g, elems[i + 1]);
                        if (!p || p->opcode == OP_CONST) { all_ok = 0; break; }
                        /* Walk the sub-DAG via stack DFS, looking for
                         * any OP_SCAN; tally cost contributions along
                         * the way.  Bound the depth to keep this check
                         * cheap on degenerate trees. */
                        int has_scan = 0;
                        int c = 0;
                        ray_op_t* stk[64];
                        int sp = 0;
                        stk[sp++] = p;
                        while (sp > 0) {
                            ray_op_t* cur = stk[--sp];
                            if (cur->opcode == OP_SCAN) has_scan = 1;
                            /* Coarse per-node cost: comparison ops are
                             * cheap, OP_LIKE / OP_IN / function calls
                             * are expensive.  Standard cheap-first
                             * conjunct ordering. */
                            switch (cur->opcode) {
                            case OP_LIKE:
                                c += 50;  /* substring-search is ~10× a
                                             plain compare */
                                break;
                            case OP_IN: case OP_NOT_IN:
                                c += 20;
                                break;
                            case OP_NOT:
                                c += 1;
                                break;
                            case OP_EQ: case OP_NE: case OP_LT: case OP_LE:
                            case OP_GT: case OP_GE: case OP_AND: case OP_OR:
                                c += 5;
                                break;
                            case OP_SCAN: case OP_CONST:
                                break;  /* leaves — no compute cost */
                            default:
                                c += 5;
                                /* Unknown op — could be arithmetic that
                                 * faults (OP_DIV / OP_MOD divide-by-zero),
                                 * a user-defined call with side effects,
                                 * or any op whose error behavior depends
                                 * on what the prior conjuncts already
                                 * filtered out.  Keep this conjunct in
                                 * its user-given position so a guard like
                                 * `(!= y 0)` continues to short-circuit
                                 * a later `(< (/ x y) 10)`. */
                                reorder_safe = 0;
                                break;
                            }
                            for (uint8_t a = 0; a < cur->arity && sp < 64; a++) {
                                ray_op_t* ch = op_child(g, cur, a);
                                if (ch) stk[sp++] = ch;
                            }
                        }
                        if (!has_scan) { all_ok = 0; break; }
                        compiled[i] = p;
                        cost[i] = c;
                    }
                    if (all_ok) {
                        /* Selection-sort by cost ascending — small k so
                         * O(k²) is fine and avoids dragging a bigger
                         * sort into the query path.  Skipped when the
                         * conjunct set contains a fault-able / side-
                         * effecting op (see `reorder_safe` below) so
                         * user-given short-circuit order is preserved. */
                        int order[64];
                        for (int64_t i = 0; i < k; i++) order[i] = (int)i;
                        if (reorder_safe) {
                            for (int64_t i = 0; i < k - 1; i++) {
                                int min_i = (int)i;
                                for (int64_t j = i + 1; j < k; j++)
                                    if (cost[order[j]] < cost[order[min_i]])
                                        min_i = (int)j;
                                if (min_i != (int)i) {
                                    int tmp = order[i];
                                    order[i] = order[min_i];
                                    order[min_i] = tmp;
                                }
                            }
                        }
                        for (int64_t i = 0; i < k; i++)
                            root = ray_filter(g, root, compiled[order[i]]);
                        and_chained = 1;
                    }
                }
            }
        }
        if (!and_chained) {
            ray_t* pred_err = NULL;
            ray_op_t* pred = compile_where_predicate(g, tbl, where_expr,
                                                     "select", &pred_err);
            if (!pred) {
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                return pred_err ? pred_err :
                    ray_error("domain", "select: failed to evaluate `where:` predicate");
            }
            root = ray_filter(g, root, pred);
        }
    }

    /* Apply NEAREST (ANN/KNN) re-ranking.  Mutually exclusive with
     * asc/desc/by (already rejected above).  Runs after WHERE so the
     * filter feeds the rerank executor directly.  `take k` becomes the
     * target result count; the rerank executor handles the take internally
     * so the bottom-of-function take block is skipped when nearest is set. */
    float* nearest_query_owned = NULL;   /* freed after ray_execute below */
    ray_t* nearest_handle_owned = NULL;  /* HNSW handle kept alive for the
                                          * DAG's lifetime; released after
                                          * ray_execute.  Without this, an
                                          * inline `(ann (hnsw-build ...) ...)`
                                          * drops the handle's rc to 0 before
                                          * exec runs — the rc→0 hook frees
                                          * the index and the ext's stored
                                          * pointer dangles. */
    if (nearest_expr) {
        if (nearest_expr->type != RAY_LIST || ray_len(nearest_expr) < 3) {
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain",
                "nearest: expected (ann <handle> <query> [ef]) or (knn <col> <query> [metric])");
        }
        int64_t nlen = ray_len(nearest_expr);
        ray_t** nlist = (ray_t**)ray_data(nearest_expr);
        ray_t* head = nlist[0];
        if (head->type != -RAY_SYM) {
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain",
                "nearest: first element must be the symbol `ann` or `knn`");
        }
        int64_t ann_sym_id = ray_sym_intern("ann", 3);
        int64_t knn_sym_id = ray_sym_intern("knn", 3);

        /* Resolve k from take (default 10). */
        int64_t k_req = 10;
        if (take_expr) {
            ray_t* tv = ray_eval(take_expr);
            if (!tv || RAY_IS_ERR(tv)) {
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return tv ? tv : ray_error("domain", "nearest: failed to evaluate `take:`");
            }
            if (tv->type == -RAY_I64)      k_req = tv->i64;
            else if (tv->type == -RAY_I32) k_req = tv->i32;
            else {
                ray_release(tv);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "nearest: take must be an integer atom");
            }
            ray_release(tv);
            if (k_req <= 0) {
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "nearest: take must be positive");
            }
        }

        /* Evaluate the query vector (arg index 2). */
        ray_t* qvec = ray_eval(nlist[2]);
        if (!qvec || RAY_IS_ERR(qvec)) {
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return qvec ? qvec : ray_error("domain", "nearest: failed to evaluate query vector");
        }
        if (!ray_is_vec(qvec) ||
            (qvec->type != RAY_F32 && qvec->type != RAY_F64 &&
             qvec->type != RAY_I32 && qvec->type != RAY_I64)) {
            ray_release(qvec);
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "nearest: query must be a numeric vector");
        }
        int32_t dim = (int32_t)qvec->len;
        if (dim <= 0) {
            ray_release(qvec);
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("length", "nearest: query vector is empty");
        }

        /* Copy query into a fresh float[] that the DAG op borrows; freed
         * after ray_execute completes. */
        nearest_query_owned = (float*)ray_calloc_raw((size_t)dim * sizeof(float));
        if (!nearest_query_owned) {
            ray_release(qvec);
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
        }
        switch (qvec->type) {
            case RAY_F32:
                memcpy(nearest_query_owned, ray_data(qvec), (size_t)dim * sizeof(float));
                break;
            case RAY_F64: {
                double* s = (double*)ray_data(qvec);
                for (int32_t j = 0; j < dim; j++) nearest_query_owned[j] = (float)s[j];
                break;
            }
            case RAY_I32: {
                int32_t* s = (int32_t*)ray_data(qvec);
                for (int32_t j = 0; j < dim; j++) nearest_query_owned[j] = (float)s[j];
                break;
            }
            case RAY_I64: {
                int64_t* s = (int64_t*)ray_data(qvec);
                for (int32_t j = 0; j < dim; j++) nearest_query_owned[j] = (float)s[j];
                break;
            }
        }
        ray_release(qvec);

        if (head->i64 == ann_sym_id) {
            ray_t* hobj = ray_eval(nlist[1]);
            if (!hobj || RAY_IS_ERR(hobj)) {
                ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return hobj ? hobj : ray_error("domain", "nearest (ann): failed to evaluate HNSW handle");
            }
            if (hobj->type != -RAY_I64 || !(hobj->attrs & RAY_ATTR_HNSW)) {
                ray_release(hobj); ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type",
                    "nearest (ann): first arg must be an HNSW handle (from hnsw-build)");
            }
            ray_hnsw_t* idx = (ray_hnsw_t*)(uintptr_t)hobj->i64;
            if (!idx) {
                /* Defensive: attr set but pointer cleared — treat as invalid. */
                ray_release(hobj); ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type",
                    "nearest (ann): HNSW handle has been freed");
            }
            if (idx->dim != dim) {
                ray_release(hobj); ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("length",
                    "nearest (ann): query dim does not match index dim");
            }
            int32_t ef = HNSW_DEFAULT_EF_S;
            if (nlen >= 4) {
                ray_t* ev = ray_eval(nlist[3]);
                if (!ev || RAY_IS_ERR(ev)) {
                    ray_release(hobj); ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ev ? ev : ray_error("domain",
                        "nearest (ann): ef expression failed to evaluate");
                }
                if (ev->type == -RAY_I64)      ef = (int32_t)ev->i64;
                else if (ev->type == -RAY_I32) ef = ev->i32;
                else {
                    ray_release(ev); ray_release(hobj);
                    ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type",
                        "nearest (ann): ef must be an integer atom");
                }
                ray_release(ev);
            }
            if ((int64_t)ef < k_req) ef = (int32_t)k_req;
            root = ray_ann_rerank(g, root, idx, nearest_query_owned, dim, k_req, ef);
            /* Steal the retain from ray_eval — the ext now borrows `idx`
             * through hobj.  Released in the common exit path after
             * ray_execute has completed. */
            nearest_handle_owned = hobj;
        } else if (head->i64 == knn_sym_id) {
            ray_t* col_expr = nlist[1];
            if (col_expr->type != -RAY_SYM) {
                ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type",
                    "nearest (knn): first arg must be an unquoted column name");
            }
            int64_t col_sym = col_expr->i64;
            ray_hnsw_metric_t metric = RAY_HNSW_COSINE;
            if (nlen >= 4) {
                ray_t* mv = nlist[3];
                if (mv && mv->type == -RAY_SYM) {
                    int64_t mid = mv->i64;
                    if (mid == ray_sym_find("l2", 2))          metric = RAY_HNSW_L2;
                    else if (mid == ray_sym_find("ip", 2))     metric = RAY_HNSW_IP;
                    else if (mid == ray_sym_find("cosine", 6)) metric = RAY_HNSW_COSINE;
                    else {
                        ray_free_raw(nearest_query_owned);
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain",
                            "nearest (knn): metric must be 'cosine, 'l2, or 'ip");
                    }
                }
            }
            root = ray_knn_rerank(g, root, col_sym, nearest_query_owned, dim, k_req, metric);
        } else {
            ray_free_raw(nearest_query_owned);
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain",
                "nearest: expected `ann` or `knn` as the first element");
        }
        if (!root) {
            if (nearest_handle_owned) ray_release(nearest_handle_owned);
            ray_free_raw(nearest_query_owned);
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
        }

        /* When the user didn't specify output columns, project only the
         * source schema — NOT the rerank's synthetic `_dist`.  This keeps
         * `(select {from: t nearest: ...})` shape-compatible with
         * `(select {from: t})`; users who want `_dist` must name it
         * explicitly (e.g. `{from: t d: _dist ...}`).
         *
         * Must handle arbitrarily wide tables (up to ray_select's uint8
         * limit of 255 cols) — a silent 16-col cap would let `_dist`
         * leak through for real-world tables. */
        if (n_out == 0) {
            int64_t src_ncols = ray_table_ncols(tbl);
            if (src_ncols > 255) {
                if (nearest_handle_owned) ray_release(nearest_handle_owned);
                ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("limit",
                    "nearest: implicit projection exceeds 255 source columns — "
                    "specify output columns explicitly");
            }
            if (src_ncols > 0) {
                ray_op_t** col_ops = (ray_op_t**)ray_calloc_raw(
                    (size_t)src_ncols * sizeof(ray_op_t*));
                if (!col_ops) {
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                int nc = 0;
                bool scan_err = false;
                for (int64_t c = 0; c < src_ncols; c++) {
                    int64_t name_id = ray_table_col_name(tbl, c);
                    ray_t* s = ray_sym_str(name_id);
                    if (!s) continue;
                    ray_op_t* scan_op = ray_scan(g, ray_str_ptr(s));
                    if (!scan_op) { scan_err = true; break; }
                    col_ops[nc++] = scan_op;
                }
                if (scan_err) {
                    ray_free_raw(col_ops);
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                root = ray_select_op(g, root, col_ops, nc);
                ray_free_raw(col_ops);
                if (!root) {
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
            }
        }
    }

    /* Pre-compute the top-count-take emit filter so the no-WHERE
     * count-key DAG decision (around line 7541) can see it.  The
     * actual thread-local set is still deferred to immediately
     * before ray_execute (see below) so state-leakage on error
     * paths in between is bounded.  Without this hoist the decision
     * read at compile time always sees an unset filter and the
     * fp_try_i32_mg_top_count fast path is unreachable for
     * `select count by k take N desc` shapes. */
    ray_group_emit_filter_t pre_top_emit = {0};
    bool pre_top_emit_matched = false;
    if (by_expr) {
        ray_group_emit_filter_t cur_emit = ray_group_emit_filter_get();
        if (!cur_emit.enabled &&
            match_group_desc_count_take(dict_elems, dict_n, from_id, where_id,
                                        by_id, take_id, asc_id, desc_id,
                                        &pre_top_emit))
            pre_top_emit_matched = true;
    }

    /* GROUP BY */
    if (by_expr) {
        /* Resolve a "single key" sym id when by_expr is either a
         * scalar -RAY_SYM name or a single-element RAY_SYM vector.
         * The eval_group branch and several downstream sites used to
         * read `by_expr->i64` directly, which is garbage when by_expr
         * is a vector — use by_key_sym instead. */
        int64_t by_key_sym = -1;
        if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED))
            by_key_sym = by_expr->i64;
        else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1)
            by_key_sym = ((int64_t*)ray_data(by_expr))[0];

        /* Detect non-aggregate expressions before routing so we can
         * decide whether GUID keys go to the DAG HT path or fall back
         * to eval-level. */
        int any_nonagg = 0;
        /* Like any_nonagg but EXCLUDING literal broadcasts and count(distinct).
         * count(distinct) is not a simple inline DAG agg yet IS served by the
         * DAG per-group count-distinct kernel (ray_count_distinct_per_group via
         * row_gid).  Literal broadcasts are added after grouping without row
         * mapping.  STR keys can therefore take the wide-key DAG path for these
         * shapes; only a genuine row-dependent non-agg projection forces them
         * to eval-level. */
        int any_true_nonagg = 0;
        if (n_out > 0) {
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id ||
                    kid == take_id || kid == asc_id || kid == desc_id) continue;
                if (is_single_group_key_projection(by_expr, dict_elems[i + 1]))
                    continue;
                if (is_group_dag_agg_expr(dict_elems[i + 1])) continue;
                /* Arithmetic over aggregates is served by hidden agg slots
                 * plus one post-group evaluation on any key shape; it must
                 * not push multi-key queries onto eval-level grouping. */
                if (is_decomposable_agg_compound(dict_elems[i + 1], tbl)) continue;
                any_nonagg = 1;
                if (can_atom_broadcast(dict_elems[i + 1])) continue;
                if (!match_count_distinct(dict_elems[i + 1])) { any_true_nonagg = 1; break; }
                /* count(distinct): DAG-capable, keep scanning for true non-aggs */
            }
        }

        /* LIST keys with plain aggregates use v2 structural indexed grouping.
         * Eval retains dynamic projections and partitioned LIST shapes.
         * GUID is packed via row-indirection in
         * the HT layout (wide_key_mask), so it uses the parallel DAG
         * path *except* for queries with non-aggregate expressions
         * (the non-agg scatter still requires 8-byte-packable key
         * reads through its KEY_READ macro). */
        int use_eval_group = 0;
        if (by_key_sym >= 0) {
            ray_t* key_col = ray_table_get_col(tbl, by_key_sym);
            if (key_col) {
                int8_t kct = key_col->type;
                if (RAY_IS_PARTED(kct)) kct = (int8_t)RAY_PARTED_BASETYPE(kct);
                if (kct == RAY_LIST && (any_nonagg || n_out == 0 ||
                    RAY_IS_PARTED(key_col->type) || !ray_agg_engine_v2))
                    use_eval_group = 1;
                else if (kct == RAY_GUID && (any_nonagg || n_out == 0))
                    /* RAY_GUID routes to eval-level ray_group_indices_fn only
                     * for (a) non-agg expression queries (existing
                     * behavior) and (b) the "no output columns" form
                     * `(select {from: t by: guid})` which otherwise
                     * lands in the DAG no-agg-no-nonagg branch whose
                     * first-occurrence scanner is O(N × n_groups) and
                     * truncates wide keys to 8 bytes via ray_read_sym.
                     * Pure-agg group-bys with GUID keys still take the
                     * DAG path (exec_group handles wide keys correctly
                     * and stays parallel / segment-streamed on parted
                     * tables). */
	                    use_eval_group = 1;
                else if (kct == RAY_STR && (any_true_nonagg || n_out == 0))
                    /* STR keys take the wide-key DAG path for pure-agg AND
                     * count(distinct) queries; only a genuine non-agg
                     * projection (or the no-output form) needs eval-level. */
	                    use_eval_group = 1;
	            }
	        }
        if (!use_eval_group && by_expr->type == RAY_SYM && ray_len(by_expr) > 1) {
            int64_t nk = ray_len(by_expr);
            int64_t* sym_ids = (int64_t*)ray_data(by_expr);
            for (int64_t k = 0; k < nk; k++) {
                ray_t* key_col = ray_table_get_col(tbl, sym_ids[k]);
                if (!key_col) continue;
                int8_t kct = key_col->type;
                if (RAY_IS_PARTED(kct)) kct = (int8_t)RAY_PARTED_BASETYPE(kct);
                /* STR keys (dict-encoded OR plain) take the parallel DAG path:
                 * dict-STR substitutes int32 codes; plain/computed STR groups
                 * via the wide-key HT (row-indirected key slots).  Both beat the
                 * O(N*ngroups) eval toy-grouper (33% allocator churn). LIST
                 * composites with plain aggregates use v2 structural grouping. */
                if (kct == RAY_LIST && (any_nonagg || n_out == 0 ||
                    RAY_IS_PARTED(key_col->type) || !ray_agg_engine_v2)) {
                    use_eval_group = 1;
                    break;
                }
            }
            /* (Removed) bounded-multikey count-take serial routing.  The
             * eval-level scan it routed to is O(N × n_groups): for every
             * input row it linearly searches all groups found so far to
             * locate the matching one.  An A/B sweep (1..16 keys absent,
             * 2-key unsorted `take:N` count-only, group counts 9..1M,
             * nrows 5k..2M) showed the parallel multi-key DAG path is
             * faster at EVERY measured point — from ~1.5x at the smallest
             * (5k rows, 9 groups) to 50-75x at larger sizes/group counts.
             * There is no input size at which the serial scan wins, so
             * the former `nrows < 100000` gate sent small inputs to a
             * strictly-dominated path.  The candidate now always flows to
             * the parallel multi-key path below. */
            /* No-agg-no-nonagg multi-key (`select {by: [k1 k2]}`): the DAG
             * no-agg branch's computed-key fallback re-evaluates the by
             * SYM-vector as a literal symbol list — ignoring WHERE and the
             * real key columns, so it returns one group per key NAME instead
             * of per distinct composite value.  Route to eval-level grouping
             * (mirrors the GUID n_out==0 case above), which groups the
             * filtered rows by the actual composite key. */
            if (!use_eval_group && n_out == 0)
                use_eval_group = 1;
        }
        /* Non-aggregation expressions (arithmetic, lambda, etc.) are
         * handled post-DAG: aggs go through the parallel GROUP pipeline,
         * then non-agg results are evaluated on the full table and
         * scattered per-group into LIST columns.  The fast scatter only
         * handles single scalar-key by-clauses — multi-key and
         * computed-key shapes route through eval-level group, which
         * gives the non-agg pass a well-defined row→group mapping
         * (composite list keys group correctly via atom_eq's structural
         * compare for RAY_LIST). */
        if (!use_eval_group && any_nonagg) {
            int single_scalar_key = 0;
            if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED)) {
                single_scalar_key = 1;
            } else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1) {
                single_scalar_key = 1;
            }
            if (!single_scalar_key) use_eval_group = 1;
        }
        if (use_eval_group) {
            /* Apply WHERE filter first (if any), then eval-level groupby */
            ray_t* eval_tbl = tbl;
            if (where_expr) {
                root = ray_optimize(g, root);
                /* Projection-aware compaction: when this WHERE filter feeds only
                 * the keys-only multi-key DISTINCT path under an active outer
                 * projection hint, publish the keys∪proj_keep keep-set so the
                 * filter finalization (ray_execute_inner) gathers just those
                 * columns.  Scoped to this ray_execute via save/restore. */
                /* keep-set = by-keys ∪ proj_keep; carve exactly that many
                 * int64 (union ≤ nk + proj_keep_n) so the write never
                 * truncates a group key or a proj_keep entry. */
                ray_vm_t* fvm = __VM;
                ray_t*   filt_hdr = NULL;
                int64_t* filt_buf = NULL;
                const int64_t* sv_fk = fvm ? fvm->filt_keep   : NULL;
                int            sv_fn = fvm ? fvm->filt_keep_n : 0;
                int32_t        sv_fd = fvm ? fvm->filt_depth  : 0;
                if (fvm) {
                    int64_t filt_cap = (by_expr ? ray_len(by_expr) : 0) + fvm->proj_keep_n;
                    if (filt_cap < 1) filt_cap = 1;
                    filt_buf = (int64_t*)scratch_alloc(&filt_hdr, (size_t)filt_cap * sizeof(int64_t));
                    if (filt_buf) {
                        int fk_n = filt_compact_keep(dict, by_expr, tbl,
                                                     from_id, where_id, by_id,
                                                     filt_buf, (int)filt_cap);
                        if (fk_n > 0) {
                            fvm->filt_keep = filt_buf; fvm->filt_keep_n = fk_n;
                            fvm->filt_depth = fvm->eval_depth;
                        }
                    }
                }
                ray_t* fres = ray_execute(g, root);
                if (fvm) { fvm->filt_keep = sv_fk; fvm->filt_keep_n = sv_fn;
                           fvm->filt_depth = sv_fd; }
                scratch_free(filt_hdr);   /* filt_buf dead past the restore */
                ray_graph_free(g); g = NULL;
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: `where:` filter produced no result"); }
                if (ray_is_lazy(fres)) fres = ray_lazy_materialize(fres);
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: failed to materialize filtered result"); }
                eval_tbl = fres;
            } else {
                ray_graph_free(g); g = NULL;
            }
            if (by_key_sym < 0 && by_expr->type == RAY_SYM && ray_len(by_expr) > 1) {
                int64_t nk = ray_len(by_expr);
                int64_t* key_syms = (int64_t*)ray_data(by_expr);
                int64_t nrows = ray_table_nrows(eval_tbl);
                if (nk <= 0) {
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "eval-level multi-key groupby requires at least one key");
                }
                /* Pure-eval grouping carries no graph-representation limit —
                 * carve the key-column pointers to the exact key count.  This
                 * scratch lives until every return path in this block. */
                ray_t* keycols_hdr = NULL;
                ray_t** key_cols = (ray_t**)scratch_alloc(&keycols_hdr, (size_t)nk * sizeof(ray_t*));
                if (!key_cols) {
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                for (int64_t k = 0; k < nk; k++) {
                    key_cols[k] = ray_table_get_col(eval_tbl, key_syms[k]);
                    if (!key_cols[k]) {
                        scratch_free(keycols_hdr);
                        if (eval_tbl != tbl) ray_release(eval_tbl);
                        ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "group key column not found");
                    }
                }

                /* Pure multi-key DISTINCT (only from/where/by — no aggregates,
                 * no take/sort) over int/SYM keys: route to the dict-linked agg
                 * path.  It groups on each column's RAW positions (no global
                 * interning of the SYM vocabulary) and emits columns that adopt
                 * the source domain — the column ships with its dict.  The
                 * legacy composite path below would box every cell per-row into
                 * a runtime-domain atom, interning the whole vocabulary. */
                bool distinct_only = true;
                for (int64_t i = 0; i + 1 < dict_n && distinct_only; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    if (kid != from_id && kid != where_id && kid != by_id)
                        distinct_only = false;
                }
                /* keys: int/SYM (read as int64) or STR (wide hash/eq in
                 * agg_group_keys) — agg_select_distinct handles both. */
                for (int64_t k = 0; k < nk && distinct_only; k++) {
                    switch (key_cols[k]->type) {
                        case RAY_I64: case RAY_I32: case RAY_I16: case RAY_U8:
                        case RAY_BOOL: case RAY_DATE: case RAY_TIME:
                        case RAY_TIMESTAMP: case RAY_SYM: case RAY_STR: break;
                        default: distinct_only = false; break;
                    }
                }
                /* agg_select_distinct gathers fixed-width/SYM/STR/LIST columns;
                 * parted/mapcommon/nested fall back to the legacy path. */
                int64_t dchk_nc = ray_table_ncols(eval_tbl);
                for (int64_t c = 0; c < dchk_nc && distinct_only; c++) {
                    ray_t* cc = ray_table_get_col_idx(eval_tbl, c);
                    if (!cc) { distinct_only = false; break; }
                    switch (cc->type) {
                        case RAY_I64: case RAY_I32: case RAY_I16: case RAY_U8:
                        case RAY_BOOL: case RAY_DATE: case RAY_TIME:
                        case RAY_TIMESTAMP: case RAY_SYM: case RAY_F64:
                        case RAY_GUID: case RAY_STR: case RAY_LIST: break;
                        default: distinct_only = false; break;
                    }
                }
                /* Cut-3: agg_group_keys (called by agg_select_distinct) carves
                 * its key-data table and takes a uint32_t key count, so this
                 * fast path serves ANY key count — no 16-key ceiling.  A raw
                 * sym-vector by: (by_expr->type == RAY_SYM here) reaches it
                 * regardless of the by-dict's key-count check (which guards
                 * RAY_DICT shapes only). */
                if (distinct_only) {
                    /* Consume the projection hint (if a consumer published one) —
                     * carry only its referenced non-key columns.  Consume-once. */
                    const int64_t* keep = NULL; int keep_n = 0;
                    if (__VM && __VM->proj_active &&
                        __VM->eval_depth == __VM->proj_depth + 1) {  /* immediate from: only */
                        keep = __VM->proj_keep; keep_n = __VM->proj_keep_n;
                        __VM->proj_active = false;   /* consume-once */
                    }
                    ray_t* dres = agg_select_distinct(eval_tbl, key_cols, key_syms,
                                                      (uint32_t)nk, nrows, keep, keep_n);
                    scratch_free(keycols_hdr);
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return dres;
                }

                /* NOTE: this multi-key count-only pre-take block is currently
                 * unreachable for any shape that returns a correct result. Since
                 * the routing change above, every all-count-agg >1-key by:
                 * answers through the parallel DAG/v2 path instead (see
                 * test/rfl/query/width_matrix.rfl's ROUTING NOTE on the `rmk`
                 * cell). The only remaining trigger into this block is a
                 * RAY_LIST-typed by-key column, which hits a pre-existing
                 * vec_new(LIST) bug building the key output column. Left in
                 * place — not correctness-load-bearing — pending a follow-up
                 * that either fixes the LIST-key case or deletes this block. */
                int64_t pre_take_groups = nrows;
                bool has_pre_take = unsorted_positive_take_limit(
                    dict_elems, dict_n, asc_id, desc_id, take_id,
                    nrows, &pre_take_groups);
                if (has_pre_take && pre_take_groups <= 1024) {
                    /* Per-key reader and per-output name buffers carved to the
                     * exact key / output counts (no fixed slot cap).  Both live
                     * until this pre-take block returns or falls through to the
                     * composite path below. */
                    ray_t* keyrd_hdr = NULL;
                    query_key_reader_t* key_readers = (query_key_reader_t*)scratch_alloc(
                        &keyrd_hdr, (size_t)nk * sizeof(query_key_reader_t));
                    int64_t cnt_out_max = select_output_count(dict_elems, dict_n);
                    ray_t* cntnames_hdr = NULL;
                    int64_t* count_names = (int64_t*)scratch_alloc(
                        &cntnames_hdr, (size_t)(cnt_out_max > 0 ? cnt_out_max : 1) * sizeof(int64_t));
                    if (!key_readers || !count_names) {
                        scratch_free(keyrd_hdr); scratch_free(cntnames_hdr);
                        scratch_free(keycols_hdr);
                        if (eval_tbl != tbl) ray_release(eval_tbl);
                        ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    for (int64_t k = 0; k < nk; k++) {
                        if (!query_key_reader_init(&key_readers[k], key_cols[k])) {
                            scratch_free(keyrd_hdr); scratch_free(cntnames_hdr);
                            scratch_free(keycols_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "unsupported group key");
                        }
                    }
                    int n_count_out = 0;
                    bool count_only = true;
                    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                        int64_t kid = dict_elems[i]->i64;
                        if (kid == from_id || kid == where_id || kid == by_id ||
                            kid == take_id || kid == asc_id || kid == desc_id) continue;
                        ray_t* val_expr_item = dict_elems[i + 1];
                        if (!is_plain_count_expr(val_expr_item)) {
                            /* Not a plain count expr — use the general path
                             * (never silently drop outputs). */
                            count_only = false;
                            break;
                        }
                        count_names[n_count_out++] = kid;
                    }
                    if (count_only && n_count_out > 0) {
                        int64_t cap = pre_take_groups;
                        ray_t* vals_hdr = ray_alloc((size_t)cap * (size_t)nk * sizeof(int64_t));
                        ray_t* null_hdr = ray_alloc((size_t)cap * (size_t)nk);
                        ray_t* cnt_hdr  = ray_alloc((size_t)cap * sizeof(int64_t));
                        if (!vals_hdr || !null_hdr || !cnt_hdr) {
                            if (vals_hdr) ray_free(vals_hdr);
                            if (null_hdr) ray_free(null_hdr);
                            if (cnt_hdr) ray_free(cnt_hdr);
                            scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                        }
                        int64_t* key_vals = (int64_t*)ray_data(vals_hdr);
                        uint8_t* key_null = (uint8_t*)ray_data(null_hdr);
                        int64_t* counts = (int64_t*)ray_data(cnt_hdr);
                        memset(key_null, 0, (size_t)cap * (size_t)nk);
                        memset(counts, 0, (size_t)cap * sizeof(int64_t));

                        /* Per-row key value / null scratch, carved once to the
                         * exact key count and reused across the row loop. */
                        ray_t* rowbuf_hdr = NULL;
                        int64_t* rv = (int64_t*)scratch_alloc(&rowbuf_hdr,
                            (size_t)nk * (sizeof(int64_t) + 1));
                        if (!rv) {
                            ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                            scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                        }
                        uint8_t* rn = (uint8_t*)(rv + nk);

                        int64_t found = 0;
                        for (int64_t r = 0; r < nrows; r++) {
                            for (int64_t k = 0; k < nk; k++) {
                                if (!query_key_reader_read(&key_readers[k], r, &rv[k], &rn[k])) {
                                    ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                                    scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                                    if (eval_tbl != tbl) ray_release(eval_tbl);
                                    ray_release(tbl);
                                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "unsupported group key");
                                }
                            }

                            int64_t gi = -1;
                            for (int64_t gidx = 0; gidx < found; gidx++) {
                                bool match = true;
                                for (int64_t k = 0; k < nk; k++) {
                                    size_t off = (size_t)gidx * (size_t)nk + (size_t)k;
                                    if (key_null[off] != rn[k] ||
                                        (!rn[k] && key_vals[off] != rv[k])) {
                                        match = false;
                                        break;
                                    }
                                }
                                if (match) { gi = gidx; break; }
                            }
                            if (gi < 0 && found < cap) {
                                gi = found++;
                                for (int64_t k = 0; k < nk; k++) {
                                    size_t off = (size_t)gi * (size_t)nk + (size_t)k;
                                    key_vals[off] = rv[k];
                                    key_null[off] = rn[k];
                                }
                            }
                            if (gi >= 0) counts[gi]++;
                        }

                        ray_t* result = ray_table_new(nk + n_count_out);
                        if (!result || RAY_IS_ERR(result)) {
                            ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                            scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result ? result : ray_error("oom", NULL);
                        }
                        for (int64_t k = 0; k < nk; k++) {
                            ray_t* src = key_cols[k];
                            int8_t kt = src->type;
                            if (RAY_IS_PARTED(kt)) kt = (int8_t)RAY_PARTED_BASETYPE(kt);
                            else if (kt == RAY_MAPCOMMON) kt = key_readers[k].base_type;
                            ray_t* key_vec = (kt == RAY_SYM)
                                ? ray_sym_vec_new(
                                      (src->type == RAY_SYM)
                                          ? (src->attrs & RAY_SYM_W_MASK)
                                          : RAY_SYM_W64,
                                      found)
                                : ray_vec_new(kt, found);
                            if (!key_vec || RAY_IS_ERR(key_vec)) {
                                ray_release(result);
                                ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                                scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                                if (eval_tbl != tbl) ray_release(eval_tbl);
                                ray_release(tbl);
                                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return key_vec ? key_vec : ray_error("oom", NULL);
                            }
                            /* key_vals holds RAW cell ids read from ONE
                             * source column — the output resolves over
                             * that column's dictionary.  (The ray_sym()
                             * atoms below are transient carriers into
                             * store_typed_elem, not escaping results.) */
                            if (kt == RAY_SYM)
                                ray_sym_vec_adopt_domain(key_vec, sym_domain_rep(src));
                            key_vec->len = found;
                            for (int64_t gi = 0; gi < found; gi++) {
                                size_t off = (size_t)gi * (size_t)nk + (size_t)k;
                                ray_t* atom = NULL;
                                switch (kt) {
                                    case RAY_SYM: atom = ray_sym(key_vals[off]); break;
                                    case RAY_I64:
                                    case RAY_TIMESTAMP: atom = ray_i64(key_vals[off]); break;
                                    case RAY_I32:
                                    case RAY_DATE:
                                    case RAY_TIME: atom = ray_i32((int32_t)key_vals[off]); break;
                                    case RAY_I16: atom = ray_i16((int16_t)key_vals[off]); break;
                                    case RAY_BOOL:
                                    case RAY_U8: atom = ray_u8((uint8_t)key_vals[off]); break;
                                    case RAY_F64: {
                                        double dv;
                                        memcpy(&dv, &key_vals[off], 8);
                                        atom = ray_f64(dv);
                                        break;
                                    }
                                    default: atom = ray_i64(key_vals[off]); break;
                                }
                                if (atom) {
                                    if (key_null[off]) atom->aux[0] |= 1;
                                    store_typed_elem(key_vec, gi, atom);
                                    ray_release(atom);
                                }
                            }
                            result = ray_table_add_col(result, key_syms[k], key_vec);
                            ray_release(key_vec);
                            if (RAY_IS_ERR(result)) {
                                ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                                scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                                if (eval_tbl != tbl) ray_release(eval_tbl);
                                ray_release(tbl);
                                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                            }
                        }
                        for (int ai = 0; ai < n_count_out; ai++) {
                            ray_t* cv = ray_vec_new(RAY_I64, found);
                            if (!cv || RAY_IS_ERR(cv)) {
                                ray_release(result);
                                ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                                scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                                if (eval_tbl != tbl) ray_release(eval_tbl);
                                ray_release(tbl);
                                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cv ? cv : ray_error("oom", NULL);
                            }
                            cv->len = found;
                            memcpy(ray_data(cv), counts, (size_t)found * sizeof(int64_t));
                            result = ray_table_add_col(result, count_names[ai], cv);
                            ray_release(cv);
                            if (RAY_IS_ERR(result)) {
                                ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                                scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                                if (eval_tbl != tbl) ray_release(eval_tbl);
                                ray_release(tbl);
                                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                            }
                        }
                        ray_free(vals_hdr); ray_free(null_hdr); ray_free(cnt_hdr);
                        scratch_free(rowbuf_hdr); scratch_free(keyrd_hdr); scratch_free(cntnames_hdr); scratch_free(keycols_hdr);
                        if (eval_tbl != tbl) ray_release(eval_tbl);
                        ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                    }
                    /* Not a count-only shape — release the pre-take scratch and
                     * fall through to the composite grouping path. */
                    scratch_free(keyrd_hdr); scratch_free(cntnames_hdr);
                }

                ray_t* composite_keys = ray_list_new(nrows);
                if (!composite_keys || RAY_IS_ERR(composite_keys)) {
                    scratch_free(keycols_hdr);
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return composite_keys ? composite_keys : ray_error("oom", NULL);
                }
                for (int64_t r = 0; r < nrows; r++) {
                    ray_t* row_key = ray_list_new(nk);
                    if (!row_key || RAY_IS_ERR(row_key)) {
                        ray_release(composite_keys);
                        scratch_free(keycols_hdr);
                        if (eval_tbl != tbl) ray_release(eval_tbl);
                        ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return row_key ? row_key : ray_error("oom", NULL);
                    }
                    for (int64_t k = 0; k < nk; k++) {
                        int alloc = 0;
                        ray_t* cell = collection_elem(key_cols[k], r, &alloc);
                        if (!cell || RAY_IS_ERR(cell)) {
                            ray_release(row_key);
                            ray_release(composite_keys);
                            scratch_free(keycols_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cell ? cell : ray_error("domain", "select by: failed to read composite group key cell");
                        }
                        row_key = ray_list_append(row_key, cell);
                        if (alloc) ray_release(cell);
                        if (!row_key || RAY_IS_ERR(row_key)) {
                            ray_release(composite_keys);
                            scratch_free(keycols_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return row_key ? row_key : ray_error("oom", NULL);
                        }
                    }
                    composite_keys = ray_list_append(composite_keys, row_key);
                    ray_release(row_key);
                    if (!composite_keys || RAY_IS_ERR(composite_keys)) {
                        scratch_free(keycols_hdr);
                        if (eval_tbl != tbl) ray_release(eval_tbl);
                        ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return composite_keys ? composite_keys : ray_error("oom", NULL);
                    }
                }

                ray_t* groups_dict = ray_group_indices_fn(composite_keys);
                ray_release(composite_keys);
                if (!groups_dict || RAY_IS_ERR(groups_dict)) {
                    scratch_free(keycols_hdr);
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return groups_dict ? groups_dict : ray_error("domain", "select by: failed to compute composite groups");
                }
                ray_t* groups = groups_to_pair_list(groups_dict);
                ray_release(groups_dict);
                if (!groups || RAY_IS_ERR(groups)) {
                    scratch_free(keycols_hdr);
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return groups ? groups : ray_error("domain", "select by: failed to build groups pair list");
                }
                int64_t n_groups = ray_len(groups) / 2;
                int64_t out_groups = n_groups;
                bool take_preapplied = unsorted_positive_take_limit(
                    dict_elems, dict_n, asc_id, desc_id, take_id,
                    n_groups, &out_groups);

                int n_agg_out = 0;
                /* Per-output collectors carved to the exact output count (no
                 * fixed slot cap); results slots zeroed so unassigned entries
                 * are NULL for the error-path release loops. */
                int64_t agg_nout_max = select_output_count(dict_elems, dict_n);
                if (agg_nout_max < 1) agg_nout_max = 1;
                ray_t* aggnames_hdr = NULL;
                int64_t* agg_names = (int64_t*)scratch_alloc(&aggnames_hdr, (size_t)agg_nout_max * sizeof(int64_t));
                ray_t* aggres_hdr = NULL;
                ray_t** agg_results = (ray_t**)scratch_calloc(&aggres_hdr, (size_t)agg_nout_max * sizeof(ray_t*));
                if (!agg_names || !agg_results) {
                    scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                    ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    if (kid == from_id || kid == where_id || kid == by_id ||
                        kid == take_id || kid == asc_id || kid == desc_id) continue;
                    ray_t* val_expr_item = dict_elems[i + 1];

                    /* Per-group count(distinct) — bypass full ray_eval per
                     * group and dispatch directly to exec_count_distinct on
                     * each group's slice.  Same kernel the standalone
                     * `(count (distinct col))` fast path uses. */
                        ray_t* cd_inner = match_count_distinct(val_expr_item);
                    if (cd_inner) {
                        ray_t* per_group = count_distinct_per_group_groups(
                            cd_inner, eval_tbl, groups, out_groups);
                        if (!per_group || RAY_IS_ERR(per_group)) {
                            for (int ai = 0; ai < n_agg_out; ai++) if (agg_results[ai]) ray_release(agg_results[ai]);
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return per_group ? per_group : ray_error("domain", "select by: count-distinct aggregation failed");
                        }
                        agg_names[n_agg_out] = kid;
                        agg_results[n_agg_out] = per_group;
                        n_agg_out++;
                        continue;
                    }

                    if (is_streaming_aggr_unary_call(val_expr_item)) {
                        ray_t** agg_elems = (ray_t**)ray_data(val_expr_item);
                        ray_t* agg_fn_name = agg_elems[0];
                        ray_t* agg_col_expr = agg_elems[1];
                        ray_t* src_col_val = NULL;
                        if (agg_col_expr->type == -RAY_SYM && !(agg_col_expr->attrs & ATTR_QUOTED)) {
                            src_col_val = ray_table_get_col(eval_tbl, agg_col_expr->i64);
                            if (src_col_val) ray_retain(src_col_val);
                        }
                        if (!src_col_val) {
                            src_col_val = ray_eval(agg_col_expr);
                            /* Materialize a lazy result up front: the
                             * per-group ray_at_fn consumes lazy args
                             * (see aggr_unary_per_group_buf). */
                            if (src_col_val && ray_is_lazy(src_col_val))
                                src_col_val = ray_lazy_materialize(src_col_val);
                            if (!src_col_val || RAY_IS_ERR(src_col_val)) {
                                for (int ai = 0; ai < n_agg_out; ai++) if (agg_results[ai]) ray_release(agg_results[ai]);
                                scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                                ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return src_col_val ? src_col_val : ray_error("domain", "select by: failed to evaluate aggregation source column");
                            }
                        }

                        ray_t* agg_vec = NULL;
                        ray_t** grp_items = (ray_t**)ray_data(groups);

                        /* Median fast path: flatten `groups` LIST<(key,
                         * idx_list)> into the (idx_buf, offsets, grp_cnt)
                         * layout that ray_median_per_group_buf expects,
                         * then run the parallel kernel (one task per
                         * group via ray_pool_dispatch_n, shared flat
                         * scratch buffer of size sum(grp_cnt), per-task
                         * quickselect on its slice).  Numeric inputs
                         * only — returns NULL on type miss → fall back
                         * to the generic per-group ray_at_fn + ray_med_fn
                         * loop below.  Uses out_groups so a preapplied
                         * take limits the work to the kept prefix. */
                        if (is_med_call(val_expr_item)) {
                            ray_t* ix_hdr = NULL;
                            ray_t* off_hdr = NULL;
                            ray_t* cnt_hdr = NULL;
                            int64_t total = 0;
                            for (int64_t gi = 0; gi < out_groups; gi++)
                                total += ray_len(grp_items[gi * 2 + 1]);
                            int64_t* ix  = (int64_t*)scratch_alloc(&ix_hdr,
                                (size_t)(total > 0 ? total : 1) * sizeof(int64_t));
                            int64_t* off = (int64_t*)scratch_alloc(&off_hdr,
                                (size_t)(out_groups > 0 ? out_groups : 1) * sizeof(int64_t));
                            int64_t* cnt = (int64_t*)scratch_alloc(&cnt_hdr,
                                (size_t)(out_groups > 0 ? out_groups : 1) * sizeof(int64_t));
                            if (ix && off && cnt) {
                                int64_t pos = 0;
                                for (int64_t gi = 0; gi < out_groups; gi++) {
                                    ray_t* idx_list = grp_items[gi * 2 + 1];
                                    int64_t c = ray_len(idx_list);
                                    off[gi] = pos;
                                    cnt[gi] = c;
                                    if (c > 0)
                                        memcpy(ix + pos, ray_data(idx_list),
                                               (size_t)c * sizeof(int64_t));
                                    pos += c;
                                }
                                agg_vec = ray_median_per_group_buf(
                                    src_col_val, ix, off, cnt, out_groups);
                            }
                            if (ix_hdr) scratch_free(ix_hdr);
                            if (off_hdr) scratch_free(off_hdr);
                            if (cnt_hdr) scratch_free(cnt_hdr);
                        }
                        if (!agg_vec) {
                            for (int64_t gi = 0; gi < out_groups; gi++) {
                                ray_t* idx_list = grp_items[gi * 2 + 1];
                                ray_t* subset = ray_at_fn(src_col_val, idx_list);
                                if (!subset || RAY_IS_ERR(subset)) continue;
                                ray_t* agg_val = NULL;
                                ray_t* fn_obj = ray_env_get(agg_fn_name->i64);
                                if (fn_obj && fn_obj->type == RAY_UNARY) {
                                    ray_unary_fn uf = (ray_unary_fn)(uintptr_t)fn_obj->i64;
                                    agg_val = uf(subset);
                                }
                                ray_release(subset);
                                if (agg_val && !RAY_IS_ERR(agg_val) && ray_is_lazy(agg_val))
                                    agg_val = ray_lazy_materialize(agg_val);
                                if (!agg_val || RAY_IS_ERR(agg_val)) continue;
                                if (!agg_vec) {
                                    int8_t vt = -(agg_val->type);
                                    agg_vec = ray_vec_new(vt, out_groups);
                                    if (!agg_vec || RAY_IS_ERR(agg_vec)) { ray_release(agg_val); break; }
                                    agg_vec->len = out_groups;
                                }
                                store_typed_elem(agg_vec, gi, agg_val);
                                ray_release(agg_val);
                            }
                        }
                        if (!agg_vec && out_groups == 0) agg_vec = empty_agg_column(agg_fn_name, src_col_val);
                        ray_release(src_col_val);
                        agg_names[n_agg_out] = kid;
                        agg_results[n_agg_out] = agg_vec;
                        n_agg_out++;
                    } else {
                        ray_t* per_group = nonagg_eval_per_group(val_expr_item, eval_tbl, groups, n_groups);
                        if (!per_group || RAY_IS_ERR(per_group)) {
                            for (int ai = 0; ai < n_agg_out; ai++) if (agg_results[ai]) ray_release(agg_results[ai]);
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return per_group ? per_group : ray_error("domain", "select by: per-group projection evaluation failed");
                        }
                        agg_names[n_agg_out] = kid;
                        agg_results[n_agg_out] = per_group;
                        n_agg_out++;
                    }
                }

                ray_t* result = ray_table_new(nk + n_agg_out);
                if (!result || RAY_IS_ERR(result)) {
                    for (int ai = 0; ai < n_agg_out; ai++) if (agg_results[ai]) ray_release(agg_results[ai]);
                    scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                    ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result ? result : ray_error("oom", NULL);
                }
                ray_t** grp_items = (ray_t**)ray_data(groups);
                for (int64_t k = 0; k < nk; k++) {
                    ray_t* src = key_cols[k];
                    int8_t kt = src->type;
                    if (RAY_IS_PARTED(kt)) kt = (int8_t)RAY_PARTED_BASETYPE(kt);
                    ray_t* key_vec = NULL;
                    if (kt == RAY_STR) {
                        key_vec = ray_vec_new(RAY_STR, out_groups);
                        for (int64_t gi = 0; gi < out_groups && key_vec && !RAY_IS_ERR(key_vec); gi++) {
                            ray_t* row_key = grp_items[gi * 2];
                            ray_t* cell = (row_key && row_key->type == RAY_LIST && k < row_key->len)
                                        ? ((ray_t**)ray_data(row_key))[k] : NULL;
                            const char* sp = cell ? ray_str_ptr(cell) : "";
                            size_t slen = cell ? ray_str_len(cell) : 0;
                            key_vec = ray_str_vec_append(key_vec, sp ? sp : "", sp ? slen : 0);
                        }
                    } else {
                        key_vec = (kt == RAY_SYM)
                                ? ray_sym_vec_new(src->attrs & RAY_SYM_W_MASK, out_groups)
                                : ray_vec_new(kt, out_groups);
                        if (key_vec && !RAY_IS_ERR(key_vec)) {
                            key_vec->len = out_groups;
                            memset(ray_data(key_vec), 0, (size_t)out_groups * ray_sym_elem_size(kt, key_vec->attrs));
                            for (int64_t gi = 0; gi < out_groups; gi++) {
                                ray_t* row_key = grp_items[gi * 2];
                                ray_t* cell = (row_key && row_key->type == RAY_LIST && k < row_key->len)
                                            ? ((ray_t**)ray_data(row_key))[k] : NULL;
                                if (cell) store_typed_elem(key_vec, gi, cell);
                            }
                        }
                    }
                    if (!key_vec || RAY_IS_ERR(key_vec)) {
                        for (int ai = 0; ai < n_agg_out; ai++) if (agg_results[ai]) ray_release(agg_results[ai]);
                        scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                        ray_release(result); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return key_vec ? key_vec : ray_error("oom", NULL);
                    }
                    result = ray_table_add_col(result, key_syms[k], key_vec);
                    ray_release(key_vec);
                    if (RAY_IS_ERR(result)) {
                        for (int ai = 0; ai < n_agg_out; ai++) if (agg_results[ai]) ray_release(agg_results[ai]);
                        scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                    }
                }
                for (int ai = 0; ai < n_agg_out; ai++) {
                    if (agg_results[ai]) {
                        result = ray_table_add_col(result, agg_names[ai], agg_results[ai]);
                        ray_release(agg_results[ai]);
                        if (RAY_IS_ERR(result)) { scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result; }
                    }
                }
                /* Aggregate results consumed and key columns emitted — release
                 * their exact-size collectors before the tail paths. */
                scratch_free(aggnames_hdr); scratch_free(aggres_hdr); scratch_free(keycols_hdr);

                /* No-agg-no-nonagg form (`select {by:[k1 k2]}`): carry the
                 * first-of-group value of every non-key column, matching the
                 * single-key first-of-group semantics — otherwise the multi-key
                 * shape would return only the key columns and silently drop the
                 * rest.  grp_items[gi*2+1] is the I64 row-index list of group
                 * gi; its first element is that group's first row. */
                if (n_out == 0 && out_groups > 0) {
                    ray_t* fi_hdr = ray_alloc((size_t)out_groups * sizeof(int64_t));
                    if (!fi_hdr) {
                        ray_release(result); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    int64_t* fi = (int64_t*)ray_data(fi_hdr);
                    for (int64_t gi = 0; gi < out_groups; gi++) {
                        ray_t* il = grp_items[gi * 2 + 1];
                        fi[gi] = (il && ray_len(il) > 0) ? ((int64_t*)ray_data(il))[0] : 0;
                    }
                    int64_t nc = ray_table_ncols(eval_tbl);
                    for (int64_t c = 0; c < nc && !RAY_IS_ERR(result); c++) {
                        int64_t cn = ray_table_col_name(eval_tbl, c);
                        bool is_key = false;
                        for (int64_t k = 0; k < nk; k++) if (key_syms[k] == cn) { is_key = true; break; }
                        if (is_key) continue;
                        ray_t* sc = ray_table_get_col_idx(eval_tbl, c);
                        if (!sc) continue;
                        ray_t* dst = NULL;
                        if (sc->type == RAY_STR) {
                            dst = ray_vec_new(RAY_STR, out_groups);
                            bool src_has_nulls = ray_vec_may_have_nulls(sc);
                            for (int64_t gi = 0; gi < out_groups && dst && !RAY_IS_ERR(dst); gi++) {
                                if (src_has_nulls && ray_vec_is_null(sc, fi[gi])) {
                                    dst = ray_str_vec_append(dst, "", 0);
                                    if (dst && !RAY_IS_ERR(dst)) ray_vec_set_null(dst, dst->len - 1, true);
                                } else {
                                    size_t slen = 0;
                                    const char* sp = ray_str_vec_get(sc, fi[gi], &slen);
                                    dst = ray_str_vec_append(dst, sp ? sp : "", sp ? slen : 0);
                                }
                            }
                        } else if (sc->type == RAY_LIST) {
                            dst = ray_alloc((size_t)out_groups * sizeof(ray_t*));
                            if (dst) {
                                dst->type = RAY_LIST; dst->len = out_groups;
                                ray_t** dout = (ray_t**)ray_data(dst);
                                ray_t** sitems = (ray_t**)ray_data(sc);
                                for (int64_t gi = 0; gi < out_groups; gi++) { dout[gi] = sitems[fi[gi]]; ray_retain(dout[gi]); }
                            }
                        } else {
                            dst = ray_vec_new(sc->type, out_groups);
                            if (dst && !RAY_IS_ERR(dst)) {
                                dst->len = out_groups;
                                for (int64_t gi = 0; gi < out_groups; gi++) {
                                    int a = 0; ray_t* v = collection_elem(sc, fi[gi], &a);
                                    store_typed_elem(dst, gi, v);
                                    if (a) ray_release(v);
                                }
                            }
                        }
                        if (!dst || RAY_IS_ERR(dst)) {
                            if (dst) ray_release(dst);
                            ray_free(fi_hdr); ray_release(result); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                        }
                        result = ray_table_add_col(result, cn, dst);
                        ray_release(dst);
                    }
                    ray_free(fi_hdr);
                    if (RAY_IS_ERR(result)) {
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                    }
                }

                ray_release(groups);
                if (eval_tbl != tbl) ray_release(eval_tbl);
                ray_release(tbl);
                if (take_preapplied) {
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                }
                result = apply_sort_take(result, dict_elems, dict_n,
                                         asc_id, desc_id, take_id, NULL);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
            }

            /* A computed key (`by: (% v 3)`) or a literal one lands here when
             * an output is not a plain aggregate (a `distinct` aggregate, a
             * row expression).  Evaluate it over the selected rows, as a
             * by-dict value is evaluated, into a hidden column and
             * group on that; the source columns stay as they are for the
             * outputs, and the result's key column is named as the DAG path
             * names a computed key (#707). */
            int64_t key_out_name = by_key_sym;
            if (by_key_sym < 0 && by_expr->type != RAY_SYM) {
                int64_t kn = ray_table_nrows(eval_tbl);
                ray_t* kv = NULL;
                if (ray_env_push_query_scope() != RAY_OK) kv = ray_error("oom", NULL);
                else {
                    int64_t enc = ray_table_ncols(eval_tbl);
                    for (int64_t c = 0; c < enc; c++) {
                        ray_t* cv = ray_table_get_col_idx(eval_tbl, c);
                        if (cv) ray_env_set_query_local(ray_table_col_name(eval_tbl, c), cv);
                    }
                    kv = group_key_eval(by_expr, eval_tbl);
                    ray_env_pop_scope();
                }
                if (kv && !RAY_IS_ERR(kv) && ray_is_atom(kv)) {
                    ray_t* wide = atom_broadcast_vec(kv, kn);
                    ray_release(kv);
                    kv = wide ? wide : ray_error("type", "by: cannot broadcast the key");
                }
                if (kv && !RAY_IS_ERR(kv) && (!ray_is_vec(kv) || ray_len(kv) != kn)) {
                    int64_t got = ray_len(kv);
                    ray_release(kv);
                    kv = ray_error("length", "by: key has %lld rows, the table %lld",
                                   (long long)got, (long long)kn);
                }
                if (!kv || RAY_IS_ERR(kv)) {
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return kv ? kv : ray_error("domain", "by: key produced no result");
                }
                by_key_sym = ray_sym_intern("__by_key", 8);
                if (ray_table_get_col(eval_tbl, by_key_sym)) {
                    ray_release(kv);
                    if (eval_tbl != tbl) ray_release(eval_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                    return ray_error("domain", "by: the input has a column named __by_key");
                }
                ray_retain(eval_tbl);   /* add_col consumes it: copy, never edit the input */
                ray_t* keyed = ray_table_add_col(eval_tbl, by_key_sym, kv);
                ray_release(kv);
                if (eval_tbl != tbl) ray_release(eval_tbl);
                if (!keyed || RAY_IS_ERR(keyed)) {
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return keyed ? keyed : ray_error("oom", NULL);
                }
                eval_tbl = keyed;
                key_out_name = derived_key_name(by_expr);
                for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    if (kid == from_id || kid == where_id || kid == by_id ||
                        kid == take_id || kid == asc_id || kid == desc_id) continue;
                    if (kid == key_out_name) { key_out_name = ray_sym_intern("key", 3); break; }
                }
            }
	            /* eval_group path supports only scalar / [col] by-forms and the
	             * computed key materialized above. */
	            if (by_key_sym < 0) {
	                if (eval_tbl != tbl) ray_release(eval_tbl);
	                ray_release(tbl);
	                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("nyi", "eval-level groupby requires scalar key");
	            }
            ray_t* key_col = ray_table_get_col(eval_tbl, by_key_sym);

            /* Fast path: (select {from: t by: k}) with no aggs and
             * no non-agg expressions — we only need first-of-group
             * for each non-key column, not full per-group index
             * lists. Scan the key column once, record the first
             * row index of each distinct key in a hash table, then
             * gather that index list from every other column. This
             * avoids ray_group_indices_fn's per-group ray_vec_append churn
             * which dominated the cost on 10M-row / 1M-group
             * workloads. */
            if (n_out == 0 && key_col && key_col->type == RAY_GUID) {
                int64_t n = key_col->len;
                const uint8_t* kb = (const uint8_t*)ray_data(key_col);
                uint32_t cap = 64;
                while ((uint64_t)cap < (uint64_t)n * 2 && cap < (1u << 28)) cap <<= 1;
                uint32_t mask = cap - 1;
                ray_t* ht_hdr = ray_alloc((size_t)cap * sizeof(uint32_t));
                if (!ht_hdr) { if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                uint32_t* ht = (uint32_t*)ray_data(ht_hdr);
                memset(ht, 0xFF, (size_t)cap * sizeof(uint32_t));

                int64_t fi_cap = n < 1024 ? 1024 : (n < (1 << 20) ? n : (1 << 20));
                if (fi_cap < 256) fi_cap = 256;
                ray_t* fi_hdr = ray_alloc((size_t)fi_cap * sizeof(int64_t));
                if (!fi_hdr) { ray_free(ht_hdr); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                int64_t* fi = (int64_t*)ray_data(fi_hdr);
                int64_t ngroups = 0;

                for (int64_t i = 0; i < n; i++) {
                    if ((i & 65535) == 0) {
                        if (ray_interrupted()) {
                            ray_free(fi_hdr);
                            ray_free(ht_hdr);
                            if (eval_tbl != tbl) ray_release(eval_tbl);
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("cancel", "interrupted");
                        }
                        ray_progress_update("select", "by: first-of-group",
                                            (uint64_t)i, (uint64_t)n);
                    }
                    const uint8_t* cur = kb + (size_t)i * 16;
                    uint64_t h; memcpy(&h, cur, 8); h ^= h >> 33; h *= 0xff51afd7ed558ccdULL;
                    uint32_t slot = (uint32_t)(h & mask);
                    uint32_t gi = UINT32_MAX;
                    while (ht[slot] != UINT32_MAX) {
                        uint32_t cand = ht[slot];
                        if (memcmp(kb + (size_t)fi[cand] * 16, cur, 16) == 0) { gi = cand; break; }
                        slot = (slot + 1) & mask;
                    }
                    if (gi == UINT32_MAX) {
                        if (ngroups >= fi_cap) {
                            int64_t new_cap = fi_cap * 2;
                            ray_t* new_hdr = ray_alloc((size_t)new_cap * sizeof(int64_t));
                            if (!new_hdr) { ray_free(fi_hdr); ray_free(ht_hdr); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                            memcpy(ray_data(new_hdr), fi, (size_t)ngroups * sizeof(int64_t));
                            ray_free(fi_hdr);
                            fi_hdr = new_hdr;
                            fi = (int64_t*)ray_data(fi_hdr);
                            fi_cap = new_cap;
                        }
                        fi[ngroups] = i;
                        ht[slot] = (uint32_t)ngroups;
                        ngroups++;
                    }
                }
                ray_free(ht_hdr);

                /* Build result table: key column first (gathered from
                 * the original at fi[]), then every other column the
                 * same way. Allocation failures and width mismatches
                 * must propagate — partial results silently dropping
                 * columns would be a correctness bug. */
                int64_t nc_src = ray_table_ncols(eval_tbl);
                ray_t* res = ray_table_new(nc_src);
                ray_t* first_err = NULL;
                if (!res || RAY_IS_ERR(res)) {
                    first_err = res && RAY_IS_ERR(res) ? res : ray_error("oom", NULL);
                    res = NULL;
                    goto fog_cleanup;
                }

                for (int64_t pass = 0; pass < nc_src + 1 && !first_err; pass++) {
                    int64_t cn;
                    if (pass == 0) cn = by_key_sym;
                    else {
                        cn = ray_table_col_name(eval_tbl, pass - 1);
                        if (cn == by_key_sym) continue;
                    }
                    ray_t* sc = ray_table_get_col(eval_tbl, cn);
                    if (!sc) continue;
                    ray_t* dst = NULL;
                    int8_t sct = sc->type;
                    if (RAY_IS_PARTED(sct)) sct = (int8_t)RAY_PARTED_BASETYPE(sct);

                    if (sct == RAY_STR) {
                        dst = ray_vec_new(RAY_STR, ngroups);
                        for (int64_t gi = 0; gi < ngroups && dst && !RAY_IS_ERR(dst); gi++) {
                            size_t slen = 0;
                            const char* sp = ray_str_vec_get(sc, fi[gi], &slen);
                            dst = ray_str_vec_append(dst, sp ? sp : "", sp ? slen : 0);
                        }
                    } else if (sct == RAY_LIST) {
                        dst = ray_list_new((int32_t)ngroups);
                        if (dst && !RAY_IS_ERR(dst)) {
                            ray_t** sitems = (ray_t**)ray_data(sc);
                            ray_t** dout = (ray_t**)ray_data(dst);
                            for (int64_t gi = 0; gi < ngroups; gi++) {
                                dout[gi] = sitems[fi[gi]];
                                ray_retain(dout[gi]);
                            }
                            dst->len = ngroups;
                        }
                    } else if (sct == RAY_SYM) {
                        /* Preserve the source sym-width from attrs so
                         * narrow sym columns (1/2/4-byte indices)
                         * memcpy the same esz on both sides. */
                        dst = ray_sym_vec_new(sc->attrs & RAY_SYM_W_MASK, ngroups);
                        if (dst && !RAY_IS_ERR(dst)) {
                            /* raw-copies cell ids from ONE source column —
                             * the output resolves over its dictionary */
                            ray_sym_vec_adopt_domain(dst, sym_domain_rep(sc));
                            dst->len = ngroups;
                            uint8_t esz = ray_sym_elem_size(sct, dst->attrs);
                            const char* sb = (const char*)ray_data(sc);
                            char* db = (char*)ray_data(dst);
                            bool src_has_nulls = ray_vec_may_have_nulls(sc);
                            if (src_has_nulls) {
                                for (int64_t gi = 0; gi < ngroups; gi++) {
                                    memcpy(db + (size_t)gi * esz,
                                           sb + (size_t)fi[gi] * esz, esz);
                                    if (ray_vec_is_null(sc, fi[gi]))
                                        ray_vec_set_null(dst, gi, true);
                                }
                            } else {
                                for (int64_t gi = 0; gi < ngroups; gi++)
                                    memcpy(db + (size_t)gi * esz,
                                           sb + (size_t)fi[gi] * esz, esz);
                            }
                        }
                    } else {
                        dst = ray_vec_new(sct, ngroups);
                        if (dst && !RAY_IS_ERR(dst)) {
                            dst->len = ngroups;
                            uint8_t esz = ray_sym_elem_size(sct, sc->attrs);
                            const char* sb = (const char*)ray_data(sc);
                            char* db = (char*)ray_data(dst);
                            bool src_has_nulls = ray_vec_may_have_nulls(sc);
                            if (src_has_nulls) {
                                for (int64_t gi = 0; gi < ngroups; gi++) {
                                    memcpy(db + (size_t)gi * esz,
                                           sb + (size_t)fi[gi] * esz, esz);
                                    if (ray_vec_is_null(sc, fi[gi]))
                                        ray_vec_set_null(dst, gi, true);
                                }
                            } else {
                                for (int64_t gi = 0; gi < ngroups; gi++)
                                    memcpy(db + (size_t)gi * esz,
                                           sb + (size_t)fi[gi] * esz, esz);
                            }
                        }
                    }

                    if (!dst || RAY_IS_ERR(dst)) {
                        first_err = (dst && RAY_IS_ERR(dst)) ? dst : ray_error("oom", NULL);
                        if (dst && !RAY_IS_ERR(dst)) ray_release(dst);
                        break;
                    }
                    res = ray_table_add_col(res, cn, dst);
                    ray_release(dst);
                    if (RAY_IS_ERR(res)) { first_err = res; res = NULL; break; }
                }

            fog_cleanup:
                ray_free(fi_hdr);
                if (eval_tbl != tbl) ray_release(eval_tbl);
                ray_release(tbl);
                if (first_err) {
                    if (res) ray_release(res);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return first_err;
                }
                res = apply_sort_take(res, dict_elems, dict_n,
                                      asc_id, desc_id, take_id, NULL);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return res;
            }

            ray_t* groups_dict = ray_group_indices_fn(key_col);
            if (RAY_IS_ERR(groups_dict)) { if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return groups_dict; }
            /* Flatten the dict into the legacy [k0,v0,…] interleaved LIST
             * representation that the rest of this branch was written for. */
            ray_t* groups = groups_to_pair_list(groups_dict);
            ray_release(groups_dict);
            if (RAY_IS_ERR(groups)) { if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return groups; }

            int64_t gn = ray_len(groups);
            int64_t n_groups = gn / 2;

            /* Empty groups with no explicit aggs: return empty table with full schema */
            if (n_groups == 0 && n_out == 0) {
                ray_release(groups);
                int64_t nc0 = ray_table_ncols(eval_tbl);
                ray_t* empty = ray_table_new(nc0);
                if (!RAY_IS_ERR(empty)) {
                    /* Key column first */
                    { ray_t* sc = ray_table_get_col(eval_tbl, by_key_sym);
                      if (sc) {
                        ray_t* ev = ray_group_gather(sc, NULL, 0);
                        if (ev && !RAY_IS_ERR(ev)) { empty = ray_table_add_col(empty, by_key_sym, ev); ray_release(ev); }
                      }
                    }
                    for (int64_t c = 0; c < nc0; c++) {
                        int64_t cn = ray_table_col_name(eval_tbl, c);
                        if (cn == by_key_sym) continue;
                        ray_t* sc = ray_table_get_col_idx(eval_tbl, c);
                        ray_t* ev = (sc->type == RAY_STR) ? ray_vec_new(RAY_STR, 0) :
                                    (sc->type == RAY_LIST) ? ray_list_new(0) :
                                    ray_vec_new(sc->type, 0);
                        if (ev && !RAY_IS_ERR(ev)) { empty = ray_table_add_col(empty, cn, ev); ray_release(ev); }
                    }
                }
                if (eval_tbl != tbl) ray_release(eval_tbl);
                ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return empty;
            }

            /* Collect aggregation results.  Per-output collectors carved to the
             * exact output count (no fixed slot cap); result slots zeroed so
             * unassigned entries are NULL for the error-path release loops. */
            int n_agg_out = 0;
            int64_t agg_nout_max = select_output_count(dict_elems, dict_n);
            if (agg_nout_max < 1) agg_nout_max = 1;
            ray_t* aggnames_hdr = NULL;
            int64_t* agg_names = (int64_t*)scratch_alloc(&aggnames_hdr, (size_t)agg_nout_max * sizeof(int64_t));
            ray_t* aggres_hdr = NULL;
            ray_t** agg_results = (ray_t**)scratch_calloc(&aggres_hdr, (size_t)agg_nout_max * sizeof(ray_t*));
            if (!agg_names || !agg_results) {
                scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
            }
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id || kid == take_id || kid == asc_id || kid == desc_id) continue;
                ray_t* val_expr_item = dict_elems[i + 1];

                /* Per-group count(distinct) — bypass full ray_eval per
                 * group and dispatch directly to exec_count_distinct. */
                {
                    ray_t* cd_inner = match_count_distinct(val_expr_item);
                    if (cd_inner) {
                        ray_t* per_group = count_distinct_per_group_groups(
                            cd_inner, eval_tbl, groups, n_groups);
                        if (!per_group || RAY_IS_ERR(per_group)) {
                            for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return per_group ? per_group : ray_error("domain", "select by: count-distinct aggregation failed");
                        }
                        agg_names[n_agg_out] = kid;
                        agg_results[n_agg_out] = per_group;
                        n_agg_out++;
                        continue;
                    }
                }

                if (is_streaming_aggr_unary_call(val_expr_item)) {
                    /* Streaming-style per-group AGG branch.  Accepts both
                     * the resolve_agg_opcode whitelist (sum/avg/min/max/...)
                     * and the broader RAY_FN_AGGR + RAY_UNARY set
                     * (med/dev/var/stddev/...) — for the eval-fallback path
                     * the only thing the body needs is a unary fn pointer
                     * to call directly with the per-group slice. */
                    ray_t** agg_elems = (ray_t**)ray_data(val_expr_item);
                    ray_t* agg_fn_name = agg_elems[0];
                    ray_t* agg_col_expr = agg_elems[1];

                    /* Resolve source column from filtered table */
                    ray_t* src_col_val = NULL;
                    if (agg_col_expr->type == -RAY_SYM && !(agg_col_expr->attrs & ATTR_QUOTED)) {
                        src_col_val = ray_table_get_col(eval_tbl, agg_col_expr->i64);
                        if (src_col_val) ray_retain(src_col_val);
                    }
                    if (!src_col_val) {
                        src_col_val = ray_eval(agg_col_expr);
                        /* Materialize a lazy result up front: the
                         * per-group ray_at_fn consumes lazy args
                         * (see aggr_unary_per_group_buf). */
                        if (src_col_val && !RAY_IS_ERR(src_col_val) && ray_is_lazy(src_col_val))
                            src_col_val = ray_lazy_materialize(src_col_val);
                        if (RAY_IS_ERR(src_col_val)) {
                            for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return src_col_val;
                        }
                    }

                    /* For each group, compute aggregation */
                    ray_t* agg_vec = NULL;
                    ray_t** grp_items = (ray_t**)ray_data(groups);
                    if (n_groups == 0) {
                        ray_t* fn_obj = ray_env_get(agg_fn_name->i64);
                        agg_vec = fn_obj && fn_obj->type == RAY_UNARY
                            ? empty_unary_group_result((ray_unary_fn)(uintptr_t)fn_obj->i64, src_col_val)
                            : ray_error("type", "aggregate must be a unary function");
                        if (!agg_vec || RAY_IS_ERR(agg_vec)) {
                            ray_release(src_col_val);
                            for (int ai = 0; ai < n_agg_out; ai++) ray_release(agg_results[ai]);
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                            return agg_vec ? agg_vec : ray_error("oom", NULL);
                        }
                    }


                    /* Median fast path — flatten `groups` into
                     * (idx_buf, offsets, grp_cnt) then call the parallel
                     * ray_median_per_group_buf kernel.  See twin site
                     * above for the design rationale. */
                    if (n_groups > 0 && is_med_call(val_expr_item)) {
                        ray_t* ix_hdr = NULL;
                        ray_t* off_hdr = NULL;
                        ray_t* cnt_hdr = NULL;
                        int64_t total = 0;
                        for (int64_t gi = 0; gi < n_groups; gi++)
                            total += ray_len(grp_items[gi * 2 + 1]);
                        int64_t* ix  = (int64_t*)scratch_alloc(&ix_hdr,
                            (size_t)(total > 0 ? total : 1) * sizeof(int64_t));
                        int64_t* off = (int64_t*)scratch_alloc(&off_hdr,
                            (size_t)n_groups * sizeof(int64_t));
                        int64_t* cnt = (int64_t*)scratch_alloc(&cnt_hdr,
                            (size_t)n_groups * sizeof(int64_t));
                        if (ix && off && cnt) {
                            int64_t pos = 0;
                            for (int64_t gi = 0; gi < n_groups; gi++) {
                                ray_t* idx_list = grp_items[gi * 2 + 1];
                                int64_t c = ray_len(idx_list);
                                off[gi] = pos;
                                cnt[gi] = c;
                                if (c > 0)
                                    memcpy(ix + pos, ray_data(idx_list),
                                           (size_t)c * sizeof(int64_t));
                                pos += c;
                            }
                            agg_vec = ray_median_per_group_buf(
                                src_col_val, ix, off, cnt, n_groups);
                        }
                        if (ix_hdr) scratch_free(ix_hdr);
                        if (off_hdr) scratch_free(off_hdr);
                        if (cnt_hdr) scratch_free(cnt_hdr);
                        if (agg_vec && !RAY_IS_ERR(agg_vec)) {
                            ray_release(src_col_val);
                            agg_names[n_agg_out] = kid;
                            agg_results[n_agg_out] = agg_vec;
                            n_agg_out++;
                            continue;
                        }
                        agg_vec = NULL;  /* type miss → fall through */
                    }

                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        ray_t* idx_list = grp_items[gi * 2 + 1];
                        ray_t* subset = ray_at_fn(src_col_val, idx_list);
                        if (RAY_IS_ERR(subset)) continue;
                        ray_t* agg_val = NULL;
                        ray_t* fn_obj = ray_env_get(agg_fn_name->i64);
                        if (fn_obj && fn_obj->type == RAY_UNARY) {
                            ray_unary_fn uf = (ray_unary_fn)(uintptr_t)fn_obj->i64;
                            agg_val = uf(subset);
                        }
                        ray_release(subset);
                        if (agg_val && !RAY_IS_ERR(agg_val) && ray_is_lazy(agg_val))
                            agg_val = ray_lazy_materialize(agg_val);
                        if (!agg_val || RAY_IS_ERR(agg_val)) continue;

                        if (!agg_vec) {
                            int8_t vt = -(agg_val->type);
                            agg_vec = ray_vec_new(vt, n_groups);
                            if (RAY_IS_ERR(agg_vec)) { ray_release(agg_val); break; }
                            agg_vec->len = n_groups;
                        }
                        store_typed_elem(agg_vec, gi, agg_val);
                        ray_release(agg_val);
                    }
                    if (!agg_vec && n_groups == 0) agg_vec = empty_agg_column(agg_fn_name, src_col_val);
                    ray_release(src_col_val);
                    agg_names[n_agg_out] = kid;
                    agg_results[n_agg_out] = agg_vec;
                    n_agg_out++;
                } else {
                    if (is_agg_expr(val_expr_item)) {
                        ray_t* per_group = nonagg_eval_per_group(
                            val_expr_item, eval_tbl, groups, n_groups);
                        if (RAY_IS_ERR(per_group)) {
                            for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return per_group;
                        }
                        agg_names[n_agg_out] = kid;
                        agg_results[n_agg_out] = per_group;
                        n_agg_out++;
                        continue;
                    }

                    /* Non-aggregation expression: evaluate on full table,
                     * then gather per-group subsets into a LIST column
                     * (non-agg produces list-of-vectors). */
                    if (ray_env_push_query_scope() != RAY_OK) {
                        for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                        scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    ray_t* _aqt = bind_all_columns(eval_tbl);
                    ray_t* full_val = ray_eval(val_expr_item);
                    g_active_query_table = _aqt;
                    ray_env_pop_scope();
                    if (RAY_IS_ERR(full_val)) {
                        for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                        scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return full_val;
                    }

                    /* Build LIST column: pre-allocate, then gather per group.
                     * Direct pointer assignment avoids ray_list_append overhead. */
                    ray_t* list_col = ray_alloc(n_groups * sizeof(ray_t*));
                    if (!list_col || RAY_IS_ERR(list_col)) {
                        ray_release(full_val);
                        for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                        scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    list_col->type = RAY_LIST;
                    /* Track filled length incrementally — see the DAG
                     * scatter above for rationale (no memset, exact
                     * cleanup via v->len walk in ray_release). */
                    list_col->len = 0;
                    ray_t** list_out = (ray_t**)ray_data(list_col);

                    /* Decide per-group disposition of full_val:
                     *   - expression references a column → result must
                     *     be row-aligned; a typed-vec or LIST whose len
                     *     matches eval_tbl's nrows → gather, otherwise
                     *     that's a genuine bug and we error out.
                     *   - expression is constant (no column refs) →
                     *     broadcast as-is to every group cell. */
                    int64_t eval_nrows = ray_table_nrows(eval_tbl);
                    int refs_column = expr_refs_row_column(val_expr_item, eval_tbl);
                    int is_indexable =
                        ray_is_vec(full_val) || full_val->type == RAY_LIST;
                    int full_is_row_aligned =
                        is_indexable && full_val->len == eval_nrows;

                    if (refs_column && !full_is_row_aligned) {
                        /* Non-streaming aggregation fallback: the full-table
                         * eval didn't produce a row-aligned shape (e.g. a
                         * user lambda returned a scalar from a vector arg),
                         * so collect per-group and post-apply the expression
                         * to each group's slice.  Each cell can be any shape;
                         * homogeneous-scalar cells collapse to a typed vec. */
                        ray_release(full_val);
                        ray_release(list_col);  /* len=0, walks nothing */
                        ray_t* per_group = nonagg_eval_per_group(
                            val_expr_item, eval_tbl, groups, n_groups);
                        if (RAY_IS_ERR(per_group)) {
                            for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                            scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return per_group;
                        }
                        /* core produces typed vec or list as appropriate */
                        agg_names[n_agg_out] = kid;
                        agg_results[n_agg_out] = per_group;
                        n_agg_out++;
                        continue;
                    }

                    ray_t** gi_items = (ray_t**)ray_data(groups);
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        ray_t* idx_list = gi_items[gi * 2 + 1];
                        ray_t* cell;
                        if (full_is_row_aligned) {
                            cell = gather_by_idx(full_val,
                                (int64_t*)ray_data(idx_list), idx_list->len);
                        } else {
                            /* Pure constant (no column refs) → broadcast */
                            ray_retain(full_val);
                            cell = full_val;
                        }
                        list_out[gi] = cell;
                        list_col->len = gi + 1;  /* commit slot */
                    }
                    ray_release(full_val);
                    agg_names[n_agg_out] = kid;
                    agg_results[n_agg_out] = list_col;
                    n_agg_out++;
                }
            }

            /* Build result table: key column + aggregation columns */
            ray_t* result = ray_table_new(1 + n_agg_out);
            if (RAY_IS_ERR(result)) { for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); } scratch_free(aggnames_hdr); scratch_free(aggres_hdr); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result; }

            /* Key column: build a typed vector matching the source column type */
            ray_t** grp_items = (ray_t**)ray_data(groups);
            ray_t* key_col_src = ray_table_get_col(eval_tbl, by_key_sym);
            {
                int8_t ktype = key_col_src ? key_col_src->type : RAY_I64;
                if (RAY_IS_PARTED(ktype)) ktype = (int8_t)RAY_PARTED_BASETYPE(ktype);
                ray_t* key_vec;
                if (ktype == RAY_STR) {
                    key_vec = ray_vec_new(RAY_STR, n_groups);
                    for (int64_t gi = 0; gi < n_groups && key_vec && !RAY_IS_ERR(key_vec); gi++) {
                        ray_t* k = grp_items[gi * 2];
                        const char* sp = ray_str_ptr(k);
                        size_t slen = ray_str_len(k);
                        key_vec = ray_str_vec_append(key_vec, sp ? sp : "", sp ? slen : 0);
                    }
                } else if (ktype == RAY_LIST) {
                    key_vec = ray_list_new(n_groups);
                    for (int64_t gi = 0; gi < n_groups && key_vec && !RAY_IS_ERR(key_vec); gi++)
                        key_vec = ray_list_append(key_vec, grp_items[gi * 2]);
                } else {
                    uint8_t kattrs = key_col_src ? key_col_src->attrs : 0;
                    if (ktype == RAY_SYM)
                        key_vec = ray_sym_vec_new(kattrs & RAY_SYM_W_MASK, n_groups);
                    else
                        key_vec = ray_vec_new(ktype, n_groups);
                    if (key_vec && !RAY_IS_ERR(key_vec)) {
                        key_vec->len = n_groups;
                        /* Zero-fill data region so skipped GUID/null slots are safe */
                        memset(ray_data(key_vec), 0, (size_t)n_groups * ray_sym_elem_size(ktype, key_vec->attrs));
                        for (int64_t gi = 0; gi < n_groups; gi++)
                            store_typed_elem(key_vec, gi, grp_items[gi * 2]);
                    }
                }
                if (!key_vec || RAY_IS_ERR(key_vec)) {
                    for (int i = 0; i < n_agg_out; i++) { if (agg_results[i]) ray_release(agg_results[i]); }
                    scratch_free(aggnames_hdr); scratch_free(aggres_hdr);
                    ray_release(result); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return key_vec ? key_vec : ray_error("oom", NULL);
                }
                result = ray_table_add_col(result, key_out_name, key_vec);
                ray_release(key_vec);
            }

            for (int i = 0; i < n_agg_out; i++) {
                if (agg_results[i])
                    result = ray_table_add_col(result, agg_names[i], agg_results[i]);
                if (agg_results[i]) ray_release(agg_results[i]);
            }
            /* Aggregate results consumed — release their exact-size collectors. */
            scratch_free(aggnames_hdr); scratch_free(aggres_hdr);

            /* No explicit aggs: gather first-of-group for all non-key columns */
            if (n_agg_out == 0 && n_groups > 0) {
                ray_t** gi_items = (ray_t**)ray_data(groups);
                /* Collect first index per group */
                int64_t fi_stack[256];
                ray_t* fi_hdr = NULL;
                int64_t* fi = (n_groups <= 256) ? fi_stack : NULL;
                if (!fi) {
                    fi_hdr = ray_alloc((size_t)n_groups * sizeof(int64_t));
                    if (!fi_hdr) { ray_release(result); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                    fi = (int64_t*)ray_data(fi_hdr);
                }
                for (int64_t gi = 0; gi < n_groups; gi++) {
                    ray_t* il = gi_items[gi * 2 + 1];
                    int a = 0; ray_t* i0 = collection_elem(il, 0, &a);
                    fi[gi] = as_i64(i0);
                    if (a) ray_release(i0);
                }
                int64_t nc = ray_table_ncols(eval_tbl);
                for (int64_t c = 0; c < nc && !RAY_IS_ERR(result); c++) {
                    int64_t cn = ray_table_col_name(eval_tbl, c);
                    if (cn == by_key_sym) continue;
                    ray_t* sc = ray_table_get_col_idx(eval_tbl, c);
                    ray_t* dst = NULL;
                    if (sc->type == RAY_STR) {
                        dst = ray_vec_new(RAY_STR, n_groups);
                        bool src_has_nulls = ray_vec_may_have_nulls(sc);
                        if (src_has_nulls) {
                            for (int64_t gi = 0; gi < n_groups && dst && !RAY_IS_ERR(dst); gi++) {
                                if (ray_vec_is_null(sc, fi[gi])) {
                                    dst = ray_str_vec_append(dst, "", 0);
                                    if (dst && !RAY_IS_ERR(dst))
                                        ray_vec_set_null(dst, dst->len - 1, true);
                                } else {
                                    size_t slen = 0;
                                    const char* sp = ray_str_vec_get(sc, fi[gi], &slen);
                                    dst = ray_str_vec_append(dst, sp ? sp : "", sp ? slen : 0);
                                }
                            }
                        } else {
                            for (int64_t gi = 0; gi < n_groups && dst && !RAY_IS_ERR(dst); gi++) {
                                size_t slen = 0;
                                const char* sp = ray_str_vec_get(sc, fi[gi], &slen);
                                dst = ray_str_vec_append(dst, sp ? sp : "", sp ? slen : 0);
                            }
                        }
                    } else if (sc->type == RAY_LIST) {
                        dst = ray_alloc(n_groups * sizeof(ray_t*));
                        if (dst) {
                            dst->type = RAY_LIST; dst->len = n_groups;
                            ray_t** dout = (ray_t**)ray_data(dst);
                            ray_t** sitems = (ray_t**)ray_data(sc);
                            for (int64_t gi = 0; gi < n_groups; gi++) { dout[gi] = sitems[fi[gi]]; ray_retain(dout[gi]); }
                        }
                    } else {
                        dst = ray_vec_new(sc->type, n_groups);
                        if (dst && !RAY_IS_ERR(dst)) {
                            /* len BEFORE the loop: store_typed_elem's null
                             * path routes through ray_vec_set_null which
                             * silently drops out-of-range writes — post-
                             * loop assignment would lose the null bit on
                             * every nullable row in this gather. */
                            dst->len = n_groups;
                            for (int64_t gi = 0; gi < n_groups; gi++) {
                                int a = 0; ray_t* v = collection_elem(sc, fi[gi], &a);
                                store_typed_elem(dst, gi, v);
                                if (a) ray_release(v);
                            }
                        }
                    }
                    if (!dst || RAY_IS_ERR(dst)) {
                        if (dst) ray_release(dst);
                        ray_release(result);
                        result = ray_error("oom", NULL);
                        break;
                    }
                    result = ray_table_add_col(result, cn, dst);
                    ray_release(dst);
                }
                if (fi_hdr) ray_free(fi_hdr);
            }

            ray_release(groups);
            if (eval_tbl != tbl) ray_release(eval_tbl);
            ray_release(tbl);
            result = apply_sort_take(result, dict_elems, dict_n,
                                     asc_id, desc_id, take_id, NULL);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
        }

        /* Pre-scan: any non-aggregation expressions that need a flat
         * (post-filter) table?  Most non-agg expressions evaluate via
         * ray_eval over the whole table and require a materialized
         * filtered_tbl when WHERE is present.
         *
         * The exception is `(count (distinct col_ref))`: its scatter
         * runs through ray_count_distinct_per_group, which reads the
         * source column directly and skips rows where row_gid[r] < 0.
         * As long as the row→gid build masks filtered-out rows to -1
         * (using the selection saved across the path-A graph free),
         * count(distinct col_ref) doesn't need the materialization.
         * That's worth ~100 ms on Q14 (937 K rows × 105 cols filtered
         * → 937 K rows × 105 cols copy). */
        int table_is_parted = 0;
        {
            int64_t ncols = ray_table_ncols(tbl);
            for (int64_t c = 0; c < ncols; c++) {
                ray_t* col = ray_table_get_col_idx(tbl, c);
                if (col && RAY_IS_PARTED(col->type)) { table_is_parted = 1; break; }
            }
        }
        /* Classification + arith-of-aggs pre-decomposition.  The
         * decomposition used to run inside the output-compile loop below,
         * AFTER has_nonagg_needing_flat was computed — so a decomposable
         * compound like (/ (sum a) (as 'F64 (sum b))) still forced the
         * WHERE materialize path (and, on parted sources, a full column
         * flatten) that its rewritten form no longer needs.  Decompose
         * here, before the routing decisions, and let the compile loop
         * consume the precomputed rewrites via compound_pos.  This pass
         * also classifies every output for routing: dag-aggs and
         * decomposed compounds are tallied into n_grp_agg_outputs, a bare
         * count(distinct sym) sets has_cd_output, and sg_shapes_ok tracks
         * whether every agg slot seen so far is kernel-shaped.  Everything
         * else marks has_nonagg_needing_flat.  All per-output arrays are
         * sized to n_out_max (select_output_count), not a fixed slot
         * cap. */
        int has_nonagg_needing_flat = 0;
        int n_grp_agg_outputs = 0;   /* dag-aggs + decomposed compounds */
        int has_cd_output = 0;
        int sg_shapes_ok = 1;        /* every agg slot kernel-shaped */
        {
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id ||
                    kid == take_id || kid == asc_id || kid == desc_id) continue;
                ray_t* expr = dict_elems[i + 1];
                if (is_single_group_key_projection(by_expr, expr))
                    continue;
                if (grouped_output_nested_agg(expr, false)) {
                    ray_t* nm = ray_sym_str(kid);
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                    return ray_error("domain",
                        "select by: output `%.*s` aggregates an aggregate of the group",
                        nm ? (int)ray_str_len(nm) : 1, nm ? ray_str_ptr(nm) : "?");
                }
                if (is_group_dag_agg_expr_dag_safe(expr, tbl)) {
                    /* dag-aggs claim output slots in order.  Not a flat
                     * forcer. */
                    n_grp_agg_outputs++;
                    if (!sg_agg_expr_ok(expr)) sg_shapes_ok = 0;
                    continue;
                }
                ray_t* cd_inner = match_count_distinct(expr);
                int is_simple_cd = cd_inner && cd_inner->type == -RAY_SYM &&
                                   !(cd_inner->attrs & ATTR_QUOTED);
                if (is_simple_cd) { has_cd_output = 1; continue; }
                if (n_compound < n_out_max) {
                    int h_before = n_hidden_aggs;
                    ray_t* rw = try_decompose_agg_arith(
                        expr, tbl, hidden_agg_exprs, hidden_agg_names,
                        &n_hidden_aggs, (int)hidden_max);
                    if (rw) {
                        for (int hi = h_before; hi < n_hidden_aggs; hi++)
                            if (!sg_agg_expr_ok(hidden_agg_exprs[hi]))
                                sg_shapes_ok = 0;
                        compound_pos[n_compound] = i;
                        compound_names[n_compound] = kid;
                        compound_rw[n_compound] = rw;
                        n_compound++;
                        n_grp_agg_outputs++;
                        continue;
                    }
                }
                has_nonagg_needing_flat = 1;
            }
        }

        /* The post-DAG scatter reads key columns directly and runs ray_eval
         * over the whole input.  Simple count(distinct col_ref) is handled
         * below by materializing only the group key and distinct column when
         * needed; other non-aggs still require a flat table view. */
        if (has_nonagg_needing_flat && table_is_parted) {
            ray_t* flat_tbl = ray_table_new(ray_table_ncols(tbl));
            if (!flat_tbl || RAY_IS_ERR(flat_tbl)) {
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return flat_tbl ? flat_tbl : ray_error("oom", NULL);
            }
            int64_t ncols = ray_table_ncols(tbl);
            for (int64_t c = 0; c < ncols; c++) {
                ray_t* col = ray_table_get_col_idx(tbl, c);
                int64_t name = ray_table_col_name(tbl, c);
                ray_t* flat_col = query_materialize_parted_col(col);
                if (!flat_col || RAY_IS_ERR(flat_col)) {
                    ray_release(flat_tbl); ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return flat_col ? flat_col : ray_error("oom", NULL);
                }
                flat_tbl = ray_table_add_col(flat_tbl, name, flat_col);
                ray_release(flat_col);
                if (!flat_tbl || RAY_IS_ERR(flat_tbl)) {
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return flat_tbl ? flat_tbl : ray_error("oom", NULL);
                }
            }
            ray_graph_free(g);
            ray_release(tbl);
            tbl = flat_tbl;
            g = ray_graph_new(tbl);
            if (!g) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
            root = ray_const_table(g, tbl);
            if (where_expr) {
                ray_t* pred_err = NULL;
                ray_op_t* pred = compile_where_predicate(g, tbl, where_expr,
                                                         "select", &pred_err);
                if (!pred) {
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                    return pred_err ? pred_err :
                        ray_error("domain", "select: failed to evaluate `where:` predicate");
                }
                root = ray_filter(g, root, pred);
            }
            table_is_parted = 0;
        }

        /* A single key expression whose every aggregate reads the one SYM
         * column it derives from is decided over that column's distinct
         * values (derived_key_vocab_aggs), which applies the WHERE itself:
         * try it before the filter below runs over every row for nothing. */
        if (!parted_bydict_deferred && by_expr->type == RAY_LIST) {
            ray_t* vres = derived_key_vocab_aggs(tbl, by_expr, where_expr, dict_elems, dict_n,
                                                 from_id, by_id, where_id, take_id,
                                                 asc_id, desc_id, nearest_id);
            if (vres) {
                ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                return vres;
            }
        }

        /* WHERE + BY handling.  Two paths:
         *
         *   (A) Fused path — applicable when there are no non-agg
         *       output expressions and the source table is flat
         *       (not parted).  Execute the filter node in-place
         *       via exec_node; OP_FILTER on a TABLE input installs
         *       a lazy RAY_SEL bitmap on g->selection and returns
         *       the original uncompacted table.  The subsequent
         *       ray_group call builds its own key/agg scans over
         *       g->table, and exec_group honours g->selection in
         *       the radix / DA / sequential paths — so no rows are
         *       materialized twice.  This is the fast path for
         *       `select ... by ... where` queries.
         *
         *   (B) Materialize path — applicable when (A) is not.
         *       Pre-execute the filter and flatten into a new
         *       table, then rebuild the graph.  Needed because
         *       the non-agg scatter runs ray_eval over a flat
         *       single-segment table, and parted tables need
         *       segment-level flattening before group anyway.
         *
         * (This also fixes a pre-existing WHERE-vs-by bug: any
         * WHERE clause on a `select ... by` query was silently
         * ignored before the filter was wired through the group
         * pipeline.) */
        if (where_expr) {
            /* Fused/lazy WHERE: exec_node(OP_FILTER) installs a rowsel on
             * g->selection over the (possibly parted) table's global row space
             * and returns it uncompacted; the group DAG built below then
             * consumes that selection.  A PARTED source no longer forces the
             * materialize path: exec_group_parted slices the global selection
             * per partition and streams (see src/ops/group.c).  A non-agg
             * output still needs the flat scatter view, so those keep the
             * materialize path (has_nonagg_needing_flat already flattened the
             * parted table above, clearing table_is_parted).  A count-distinct
             * output over a PARTED source keeps the materialize path: its
             * post-group scatter reads the flat filtered table, which the
             * streaming per-partition kernel does not produce — only pure
             * group aggregates fuse-and-stream over parted input. */
            bool can_fuse = !has_nonagg_needing_flat &&
                            (!table_is_parted || !has_cd_output);
            if (can_fuse) {
                root = ray_optimize(g, root);
                /* Slice-group fusion: when the WHERE predicate is exactly
                 * membership (in/==) on the single bare group-key column
                 * and that column carries a fresh hash or part index, skip
                 * the filter scan entirely — exec_group aggregates the
                 * key slices directly (or folds them into the equivalent
                 * selection).  count-distinct outputs stay on the filter
                 * path: their post-group scatter masks rows through
                 * saved_selection, which this fusion never produces. */
                bool sg_armed = false;
                if (!has_cd_output && n_grp_agg_outputs > 0 && sg_shapes_ok) {
                    int64_t by_name = -1;
                    if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED))
                        by_name = by_expr->i64;
                    else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1)
                        by_name = ((int64_t*)ray_data(by_expr))[0];
                    ray_t* by_col = (by_name >= 0)
                        ? ray_table_get_col(tbl, by_name) : NULL;
                    if (by_col)
                        sg_armed = ray_slice_group_probe(g, root, by_col);
                }
                if (sg_armed) goto where_done;
                /* exec_node populates g->selection as a side effect
                 * of OP_FILTER on a table input, and returns the
                 * uncompacted table (== g->table).  Discard the
                 * result — we only needed the side effect. */
                ray_t* fres = exec_node(g, root);
                if (!fres || RAY_IS_ERR(fres)) {
                    if (g->selection) {
                        ray_release(g->selection);
                        g->selection = NULL;
                    }
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: `where:` filter produced no result");
                }
                /* OP_CONST/OP_FILTER both retain, so the returned
                 * table has an extra refcount we must release.
                 * g->table still owns tbl via the graph, so this
                 * only drops the exec-node-side retain. */
                ray_release(fres);
                /* Retain a copy of the selection so it survives the
                 * later ray_graph_free.  count(distinct col_ref) needs
                 * this in the n_nonaggs scatter to mask filtered-out
                 * rows in the row→gid build. */
                if (g->selection) {
                    saved_selection = g->selection;
                    ray_retain(saved_selection);
                }
            } else {
                root = ray_optimize(g, root);
                ray_t* fres = ray_execute(g, root);
                ray_graph_free(g); g = NULL;
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: `where:` filter produced no result"); }
                if (ray_is_lazy(fres)) fres = ray_lazy_materialize(fres);
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: failed to materialize filtered result"); }
                ray_release(tbl);
                tbl = fres;
                g = ray_graph_new(tbl);
                if (!g) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                root = ray_const_table(g, tbl);
            }
        where_done: ;
        }

        /* Compile group key(s) — key_ops carved with the top-level collectors */
        int64_t n_keys = 0;

        if (parted_bydict_deferred) {
            /* Parted by-dict: compile each deferred value AST directly.  A bare
             * column ref lowers to the same OP_SCAN the SYM-vec path emits; a
             * computed expr lowers to a DAG node exec_group_parted streams.  The
             * key op's own output name is ignored — the result key columns are
             * renamed to the by-dict ALIAS names post-execution (search
             * parted_bydict_deferred near the output rename). */
            DICT_VIEW_DECL(dfv);
            DICT_VIEW_OPEN(deferred_bydict, dfv);
            if (DICT_VIEW_OVERFLOW(dfv)) {
                DICT_VIEW_CLOSE(dfv); ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
            }
            for (int64_t i = 0; i < deferred_nk && n_keys < nk_max; i++) {
                key_ops[n_keys] = compile_expr_dag(g, dfv[i * 2 + 1]);
                if (!key_ops[n_keys]) { ray_t* cerr = graph_take_compile_err(g); DICT_VIEW_CLOSE(dfv); ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cerr ? cerr : ray_error("domain", "select by: failed to compile group key expression"); }
                n_keys++;
            }
            DICT_VIEW_CLOSE(dfv);
        } else if (by_expr->type == RAY_SYM) {
            /* Multiple keys as SYM vector: [col1 col2 ...] —
             * cell-data resolved through the vec's domain. */
            int64_t nk = ray_len(by_expr);
            for (int64_t i = 0; i < nk && n_keys < nk_max; i++) {
                ray_t* name_str = ray_sym_vec_cell(by_expr, i);
                if (!name_str) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: unknown group key symbol"); }
                key_ops[n_keys] = ray_scan(g, ray_str_ptr(name_str));
                if (!key_ops[n_keys]) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: group key column not found"); }
                n_keys++;
            }
        } else {
            /* Single key expression.  Over a lone SYM column evaluate it per
             * distinct symbol and feed the spread key as a constant node,
             * named the way the eval-level path names a computed key.  (The
             * grouping decided over the distinct values, derived_key_vocab_aggs,
             * was tried ahead of the WHERE.) */
            ray_t* dom_key = derived_key_over_sym_domain(by_expr, tbl, g->selection);
            if (dom_key) {
                key_ops[0] = ray_const_vec(g, dom_key);
                ray_release(dom_key);
            } else {
                key_ops[0] = compile_expr_dag(g, by_expr);
            }
            /* Only a real expression is renamed: a bare column symbol lands
             * here too and keeps its own name. */
            computed_single_key = (key_ops[0] != NULL && by_expr->type == RAY_LIST);
            if (!key_ops[0]) { ray_t* cerr = graph_take_compile_err(g); ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cerr ? cerr : ray_error("domain", "select by: failed to compile group key expression"); }
            n_keys = 1;
        }

        for (int64_t ki = 0; ki < n_keys; ki++)
            key_ids[ki] = key_ops[ki]->id;

        /* Collect aggregation expressions from output columns.
         * Non-agg expressions are tracked separately for post-DAG scatter.
         * agg_ins2[] is parallel to agg_ins[] — NULL for unary aggs,
         * non-NULL for binary aggs (currently OP_PEARSON_CORR).  The
         * has_binary_agg flag selects the extended group builder below.  agg_k[] carries
         * a scalar literal alongside the column for holistic aggs that
         * need one (top/bot K, quantile probability bits); zero in
         * unrelated slots.  agg_ops/agg_ins/
         * agg_ins2/agg_k are carved with the top-level collectors. */
        int64_t n_aggs = 0;
        int has_binary_agg = 0;
        int has_agg_k = 0;

        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            if (kid == from_id || kid == where_id || kid == by_id || kid == take_id || kid == asc_id || kid == desc_id) continue;

            ray_t* val_expr = dict_elems[i + 1];
            if (is_group_dag_agg_expr_dag_safe(val_expr, tbl)) {
                ray_t** agg_elems = (ray_t**)ray_data(val_expr);
                uint16_t op = resolve_agg_opcode(agg_elems[0]->i64);
                ray_t* agg_arg = agg_elems[1];
                /* AST-level idiom rewrite — see simplify_agg_idiom comment.
                 * Resolves (first (asc col)) / (last (asc col)) and
                 * (count (asc|desc|reverse col)) before agg_ins is built. */
                {
                    uint16_t new_op;
                    ray_t* new_arg;
                    if (simplify_agg_idiom(val_expr, tbl, &new_op, &new_arg)) {
                        op = new_op;
                        agg_arg = new_arg;
                    }
                }
                agg_ops[n_aggs] = op;
                /* Compile the aggregation input (the column reference) */
                agg_ins[n_aggs] = compile_expr_dag(g, agg_arg);
                agg_in_ids[n_aggs] = agg_ins[n_aggs] ? agg_ins[n_aggs]->id : RAY_OP_NONE;
                if (!agg_ins[n_aggs]) {
                    ray_t* cerr = graph_take_compile_err(g);
                    ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                    return cerr ? cerr : ray_error("domain", "select by: failed to compile aggregation argument");
                }
                agg_names[n_aggs] = kid;
                /* Canonical aggregand type-admission (matches the scalar
                 * builtins): reject non-numeric / absolute-temporal inputs so a
                 * by-group sum/avg/var never silently folds symbol ids etc. */
                if (agg_ins[n_aggs]->out_type > 0 &&
                    !agg_type_admitted(op, agg_ins[n_aggs]->out_type)) {
                    int8_t in_t = agg_ins[n_aggs]->out_type;            /* capture BEFORE free */
                    ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "select by: aggregation does not admit input type %s", ray_type_name(in_t));
                }
                agg_ins2[n_aggs] = NULL;
                agg_in2_ids[n_aggs] = RAY_OP_NONE;
                agg_k[n_aggs] = 0;
                if (agg_is_binary_agg(op)) {
                    if (ray_len(val_expr) < 3) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("arity", "select by: binary aggregation requires two column arguments"); }
                    agg_ins2[n_aggs] = compile_expr_dag(g, agg_elems[2]);
                    agg_in2_ids[n_aggs] = agg_ins2[n_aggs] ? agg_ins2[n_aggs]->id : RAY_OP_NONE;
                    if (!agg_ins2[n_aggs]) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: failed to compile binary aggregation second argument"); }
                    if (agg_ins2[n_aggs]->out_type > 0 &&
                        !agg_type_admitted(op, agg_ins2[n_aggs]->out_type)) {
                        int8_t in_t = agg_ins2[n_aggs]->out_type;
                        ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "select by: binary aggregation does not admit second input type %s", ray_type_name(in_t));
                    }
                    has_binary_agg = 1;
                } else if (op == OP_TOP_N || op == OP_BOT_N) {
                    has_list_agg = 1;
                    if (ray_len(val_expr) < 3) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("arity", "select by: top/bot aggregation requires a K argument"); }
                    ray_t* k_expr = agg_elems[2];
                    int64_t k_val;
                    if (k_expr->type == -RAY_I64)       k_val = k_expr->i64;
                    else if (k_expr->type == -RAY_I32)  k_val = (int64_t)(int32_t)k_expr->i64;
                    else { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "top/bot K must be integer literal"); }
                    if (k_val < 1) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("range", "top/bot K must be >= 1"); }
                    if (k_val > 1024) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("range", "top/bot K capped at 1024"); }
                    agg_k[n_aggs] = k_val;
                    has_agg_k = 1;
                } else if (op == OP_QUANTILE) {
                    if (ray_len(val_expr) < 3) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("arity", "select by: quantile requires a probability argument"); }
                    double q_val = 0.0;
                    if (!quantile_literal_prob(agg_elems[2],
                                               agg_name_is_percentile(agg_elems[0]->i64),
                                               &q_val)) {
                        ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                        return ray_error("domain", "select by: quantile probability must be a non-null numeric literal in range");
                    }
                    agg_k[n_aggs] = encode_agg_f64_param(q_val);
                    has_agg_k = 1;
                }
                n_aggs++;
            } else {
                if (is_single_group_key_projection(by_expr, val_expr))
                    continue;
                /* Arith-of-aggs: decomposed by the classification pass
                 * above (hidden DAG agg slots + one post-group wrapper
                 * eval); consume the precomputed rewrite here.  Outputs
                 * that failed decomposition fall to the scatter. */
                {
                    bool pre_compound = false;
                    for (int ci = 0; ci < n_compound; ci++)
                        if (compound_pos[ci] == i) { pre_compound = true; break; }
                    if (pre_compound) continue;
                }
                nonagg_names[n_nonaggs] = kid;
                nonagg_exprs[n_nonaggs] = val_expr;
                n_nonaggs++;
            }
        }

        n_aggs_real = n_aggs;
        /* Compile the hidden decomposed agg slots (unary DAG aggs by
         * construction — see agg_arith_rewrite). */
        for (int hi = 0; hi < n_hidden_aggs; hi++) {
            ray_t** he = (ray_t**)ray_data(hidden_agg_exprs[hi]);
            uint16_t hop = resolve_agg_opcode(he[0]->i64);
            agg_ops[n_aggs] = hop;
            agg_ins[n_aggs] = compile_expr_dag(g, he[1]);
            agg_in_ids[n_aggs] = agg_ins[n_aggs] ? agg_ins[n_aggs]->id : RAY_OP_NONE;
            if (!agg_ins[n_aggs]) {
                for (int ci = 0; ci < n_compound; ci++)
                    ray_release(compound_rw[ci]);
                ray_t* cerr = graph_take_compile_err(g);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                return cerr ? cerr : ray_error("domain", "select by: failed to compile aggregation argument");
            }
            if (agg_ins[n_aggs]->out_type > 0 &&
                !agg_type_admitted(hop, agg_ins[n_aggs]->out_type)) {
                int8_t in_t = agg_ins[n_aggs]->out_type;
                for (int ci = 0; ci < n_compound; ci++)
                    ray_release(compound_rw[ci]);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "select by: aggregation does not admit input type %s", ray_type_name(in_t));
            }
            agg_ins2[n_aggs] = NULL;
            agg_in2_ids[n_aggs] = RAY_OP_NONE;
            agg_k[n_aggs] = 0;
            if (agg_is_binary_agg(hop)) {
                /* hidden_agg_shape_ok admitted two bare column arguments */
                agg_ins2[n_aggs] = compile_expr_dag(g, he[2]);
                agg_in2_ids[n_aggs] = agg_ins2[n_aggs] ? agg_ins2[n_aggs]->id : RAY_OP_NONE;
                if (!agg_ins2[n_aggs]) {
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: failed to compile binary aggregation second argument");
                }
                if (agg_ins2[n_aggs]->out_type > 0 &&
                    !agg_type_admitted(hop, agg_ins2[n_aggs]->out_type)) {
                    int8_t in_t = agg_ins2[n_aggs]->out_type;
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "select by: binary aggregation does not admit second input type %s", ray_type_name(in_t));
                }
                has_binary_agg = 1;
            } else if (hop == OP_TOP_N || hop == OP_BOT_N) {
                has_list_agg = 1;
                if (ray_len(hidden_agg_exprs[hi]) < 3) {
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("arity", "select by: top/bot aggregation requires a K argument");
                }
                int64_t k_val = 0;
                ray_t* k_expr = he[2];
                if (k_expr->type == -RAY_I64)       k_val = k_expr->i64;
                else if (k_expr->type == -RAY_I32)  k_val = (int64_t)(int32_t)k_expr->i64;
                else {
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "top/bot K must be integer literal");
                }
                if (k_val < 1 || k_val > 1024) {
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("range", "top/bot K must be between 1 and 1024");
                }
                agg_k[n_aggs] = k_val;
                has_agg_k = 1;
            } else if (hop == OP_QUANTILE) {
                if (ray_len(hidden_agg_exprs[hi]) < 3) {
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("arity", "select by: quantile requires a probability argument");
                }
                double q_val = 0.0;
                if (!quantile_literal_prob(he[2],
                                           agg_name_is_percentile(he[0]->i64),
                                           &q_val)) {
                    for (int ci = 0; ci < n_compound; ci++)
                        ray_release(compound_rw[ci]);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: quantile probability must be a non-null numeric literal in range");
                }
                agg_k[n_aggs] = encode_agg_f64_param(q_val);
                has_agg_k = 1;
            }
            n_aggs++;
        }

        /* Re-resolve the collected op pointers: every compile above may have
         * moved g->nodes. */
        for (int64_t ki = 0; ki < n_keys; ki++)
            key_ops[ki] = &g->nodes[key_ids[ki]];
        for (int64_t ai = 0; ai < n_aggs; ai++) {
            agg_ins[ai]  = &g->nodes[agg_in_ids[ai]];
            agg_ins2[ai] = agg_in2_ids[ai] != RAY_OP_NONE ? &g->nodes[agg_in2_ids[ai]] : NULL;
        }

        if (n_aggs > 0 || n_nonaggs > 0) {
            if (n_aggs > 0) {
                if (has_agg_k || has_binary_agg) {
                    /* Extended aggregate metadata: scalar parameters for
                     * top/bot/quantile and optional second inputs for binary
                     * aggregators. */
                    root = ray_group_build(g, key_ops, n_keys, agg_ops,
                                           agg_ins,
                                           has_binary_agg ? agg_ins2 : NULL,
                                           has_agg_k ? agg_k : NULL,
                                           n_aggs);
                } else {
                    /* Plain unary aggs (incl. max+min, median+stddev,
                     * sum+count) — v2 handles all of these.  Build a plain
                     * OP_GROUP node. */
                    root = ray_group(g, key_ops, n_keys, agg_ops, agg_ins, n_aggs);
                }
            } else {
                /* No aggs but non-agg expressions exist — still need group
                 * boundaries.  Use GROUP+COUNT on the key to get group keys.
                 * The count column will be dropped after execution. */
                uint16_t cnt_op = OP_COUNT;
                ray_op_t* cnt_in = key_ops[0];
                root = ray_group(g, key_ops, n_keys, &cnt_op, &cnt_in, 1);
                synth_count_col = 1;
            }
        } else {
            /* No explicit aggregations — apply WHERE filter first (if any),
             * then use DAG GROUP+COUNT for fast hash-parallel group boundaries,
             * then gather first-of-group from the filtered table. */
            ray_t* filtered_tbl = tbl;
            if (where_expr) {
                root = ray_optimize(g, root);
                ray_t* fres = ray_execute(g, root);
                ray_graph_free(g); g = NULL;
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: `where:` filter produced no result"); }
                if (ray_is_lazy(fres)) fres = ray_lazy_materialize(fres);
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: failed to materialize filtered result"); }
                filtered_tbl = fres;
                /* Rebuild graph on filtered table for GROUP+COUNT */
                g = ray_graph_new(filtered_tbl);
                if (!g) { if (filtered_tbl != tbl) ray_release(filtered_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                n_keys = 0;
                if (by_expr->type == RAY_SYM) {
                    int64_t nk = ray_len(by_expr);
                    for (int64_t i = 0; i < nk && n_keys < nk_max; i++) {
                        /* cell-data via the vec's domain */
                        ray_t* ns = ray_sym_vec_cell(by_expr, i);
                        if (ns) key_ops[n_keys++] = ray_scan(g, ray_str_ptr(ns));
                    }
                } else {
                    key_ops[0] = compile_expr_dag(g, by_expr);
                    if (key_ops[0]) n_keys = 1;
                }
            }

            uint16_t cnt_op = OP_COUNT;
            ray_op_t* cnt_in = key_ops[0];
            root = ray_group(g, key_ops, n_keys, &cnt_op, &cnt_in, 1);
            root = ray_optimize(g, root);
            ray_t* grouped = ray_execute(g, root);
            ray_graph_free(g); g = NULL;
            if (!grouped || RAY_IS_ERR(grouped)) { if (filtered_tbl != tbl) ray_release(filtered_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return grouped; }
            if (ray_is_lazy(grouped)) grouped = ray_lazy_materialize(grouped);

            int64_t n_groups = ray_table_nrows(grouped);

            /* Resolve key column sym early — needed for empty result schema.
             * A dotted name like `Timestamp.date` compiles to a scan + trunc
             * chain, not a direct column lookup, so it must land in the
             * computed-key fallback path below (key_sym stays -1).  Otherwise
             * downstream `ray_table_get_col(filtered_tbl, key_sym)` would
             * return NULL for the non-existent "Timestamp.date" column and
             * the subsequent deref would crash. */
            int64_t key_sym = -1;
            if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED)
                && !ray_sym_is_dotted(by_expr->i64))
                key_sym = by_expr->i64;
            else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1)
                key_sym = ((int64_t*)ray_data(by_expr))[0];

            if (n_groups == 0) {
                ray_release(grouped);
                int64_t nc0 = ray_table_ncols(filtered_tbl);
                ray_t* empty = ray_table_new(nc0 + 1);
                if (!RAY_IS_ERR(empty)) {
                    /* Key column.  For a plain/column key, key_sym
                     * names a real source column and we mirror its
                     * type.  For a computed key (dotted, xbar, ...)
                     * we evaluate by_expr against the filtered (empty)
                     * table to learn the key's type and name without
                     * duplicating schema derivation logic. */
                    int64_t empty_key_name = key_sym;
                    ray_t* empty_key_vec = NULL;
                    if (key_sym >= 0) {
                        ray_t* sc = ray_table_get_col(filtered_tbl, key_sym);
                        if (sc) {
                            empty_key_vec = ray_group_gather(sc, NULL, 0);
                        }
                    } else {
                        /* Match the computed-key fallback's naming
                         * rules (dotted tail / last name arg) and
                         * collision handling. */
                        int64_t ck_name = -1;
                        int64_t ck_full = -1;
                        int64_t ck_head = -1;
                        if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED)) {
                            ck_full = by_expr->i64;
                            if (ray_sym_is_dotted(by_expr->i64)) {
                                const int64_t* segs;
                                int nsegs = ray_sym_segs(by_expr->i64, &segs);
                                if (nsegs > 0) { ck_name = segs[nsegs - 1]; ck_head = segs[0]; }
                            } else {
                                ck_name = by_expr->i64;
                            }
                        } else if (by_expr->type == RAY_LIST && by_expr->len >= 2) {
                            ray_t** be = (ray_t**)ray_data(by_expr);
                            for (int64_t i = by_expr->len - 1; i >= 1; i--) {
                                if (be[i]->type == -RAY_SYM && !(be[i]->attrs & ATTR_QUOTED)) {
                                    ck_name = be[i]->i64;
                                    break;
                                }
                            }
                        }
                        if (ck_name < 0) ck_name = ray_sym_intern("key", 3);
                        if (ck_head >= 0 && ck_full >= 0 && ck_name != ck_full) {
                            for (int64_t c = 0; c < nc0; c++) {
                                int64_t cn = ray_table_col_name(filtered_tbl, c);
                                if (cn == ck_name && cn != ck_head) {
                                    ck_name = ck_full;
                                    break;
                                }
                            }
                        }
                        empty_key_name = ck_name;

                        /* Evaluate by_expr against the (empty) filtered table
                         * to get a length-0 key vector typed like the
                         * non-empty path would produce it. */
                        ray_env_push_query_scope();
                        for (int64_t c = 0; c < nc0; c++) {
                            ray_env_set_query_local(ray_table_col_name(filtered_tbl, c),
                                                    ray_table_get_col_idx(filtered_tbl, c));
                        }
                        ray_t* ck_vec = ray_eval(by_expr);
                        ray_env_pop_scope();
                        if (ck_vec && !RAY_IS_ERR(ck_vec) && ray_is_vec(ck_vec)) {
                            int8_t kt = ck_vec->type;
                            empty_key_vec = (kt == RAY_STR)
                                            ? ray_vec_new(RAY_STR, 0)
                                            : (kt == RAY_LIST)
                                              ? ray_list_new(0)
                                              : ray_vec_new(kt, 0);
                        }
                        if (ck_vec && !RAY_IS_ERR(ck_vec)) ray_release(ck_vec);
                    }
                    if (empty_key_vec && !RAY_IS_ERR(empty_key_vec)) {
                        empty = ray_table_add_col(empty, empty_key_name, empty_key_vec);
                        ray_release(empty_key_vec);
                    }

                    for (int64_t c = 0; c < nc0; c++) {
                        int64_t cn = ray_table_col_name(filtered_tbl, c);
                        if (cn == empty_key_name) continue;
                        ray_t* sc = ray_table_get_col_idx(filtered_tbl, c);
                        ray_t* ev = (sc->type == RAY_STR) ? ray_vec_new(RAY_STR, 0) :
                                    (sc->type == RAY_LIST) ? ray_list_new(0) :
                                    ray_vec_new(sc->type, 0);
                        if (!RAY_IS_ERR(ev)) { empty = ray_table_add_col(empty, cn, ev); ray_release(ev); }
                    }
                }
                if (filtered_tbl != tbl) ray_release(filtered_tbl);
                ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return empty;
            }

            /* Build first_idx: scan filtered key column once, record first
             * occurrence of each group key value. */
            if (key_sym < 0) {
                /* Computed group key (e.g., xbar) — fall back to eval-level groupby */
                ray_release(grouped);
                int64_t tbl_ncols = ray_table_ncols(filtered_tbl);
                ray_env_push_query_scope();
                for (int64_t c = 0; c < tbl_ncols; c++) {
                    int64_t cn = ray_table_col_name(filtered_tbl, c);
                    ray_t* cv = ray_table_get_col_idx(filtered_tbl, c);
                    ray_env_set_query_local(cn, cv);
                }
                ray_t* computed_key = derived_key_over_sym_domain(by_expr, filtered_tbl, NULL);
                if (!computed_key) computed_key = ray_eval(by_expr);
                ray_env_pop_scope();
                if (!computed_key || RAY_IS_ERR(computed_key)) {
                    if (filtered_tbl != tbl) ray_release(filtered_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return computed_key ? computed_key : ray_error("domain", "select by: failed to evaluate group key expression");
                }
                ray_t* groups2_dict = ray_group_indices_fn(computed_key);
                if (!groups2_dict || RAY_IS_ERR(groups2_dict)) {
                    ray_release(computed_key);
                    if (filtered_tbl != tbl) ray_release(filtered_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return groups2_dict ? groups2_dict : ray_error("domain", "select by: failed to compute groups");
                }
                ray_t* groups2 = groups_to_pair_list(groups2_dict);
                ray_release(groups2_dict);
                if (RAY_IS_ERR(groups2)) {
                    ray_release(computed_key);
                    if (filtered_tbl != tbl) ray_release(filtered_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return groups2;
                }
                int64_t ng2 = ray_len(groups2) / 2;
                if (ng2 == 0) { ray_release(groups2); ray_release(computed_key); if (filtered_tbl != tbl) ray_release(filtered_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_table_new(0); }
                ray_t** gi2 = (ray_t**)ray_data(groups2);

                /* fi2 must sweep EVERY group, not just the first 256 —
                 * the downstream result-column loops iterate up to ng2
                 * and indexed reads beyond a fixed-size stack slot would
                 * pick up uninitialised bytes.  Stack-fast for small
                 * group counts, heap-fallback once we need more. */
                int64_t fi2_stack[256];
                ray_t*  fi2_hdr = NULL;
                int64_t* fi2 = fi2_stack;
                if (ng2 > 256) {
                    fi2_hdr = ray_alloc((size_t)ng2 * sizeof(int64_t));
                    if (!fi2_hdr) {
                        ray_release(groups2); ray_release(computed_key);
                        if (filtered_tbl != tbl) ray_release(filtered_tbl);
                        ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    fi2 = (int64_t*)ray_data(fi2_hdr);
                }
                for (int64_t g2 = 0; g2 < ng2; g2++) {
                    int alloc2 = 0;
                    ray_t* i02 = collection_elem(gi2[g2 * 2 + 1], 0, &alloc2);
                    fi2[g2] = as_i64(i02);
                    if (alloc2) ray_release(i02);
                }
                /* Name for the synthesized key column:
                 *  - dotted sym `a.b.c` → tail segment (`c`) so `Timestamp.ss`
                 *    surfaces as an `ss` column (pretty in the common case).
                 *    If the tail collides with an *unrelated* source column
                 *    (not the head of the dotted path), fall back to the
                 *    full dotted name so we don't silently drop real data.
                 *  - list expr `(xbar N col)` / `(+ col 1)` → last name-typed
                 *    argument, so the transform's output deliberately
                 *    replaces the source column (matches xbar convention).
                 *  - fall back to an interned "key" if nothing more specific
                 *    can be derived. */
                int64_t ckey_name = -1;
                int64_t ckey_full = -1;       /* full dotted sym, for collision fallback */
                int64_t ckey_head = -1;       /* head segment of dotted expr (input column) */
                if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED)) {
                    ckey_full = by_expr->i64;
                    if (ray_sym_is_dotted(by_expr->i64)) {
                        const int64_t* segs;
                        int nsegs = ray_sym_segs(by_expr->i64, &segs);
                        if (nsegs > 0) {
                            ckey_name = segs[nsegs - 1];
                            ckey_head = segs[0];
                        }
                    } else {
                        ckey_name = by_expr->i64;
                    }
                } else if (by_expr->type == RAY_LIST && by_expr->len >= 2) {
                    ray_t** be = (ray_t**)ray_data(by_expr);
                    for (int64_t i = by_expr->len - 1; i >= 1; i--) {
                        if (be[i]->type == -RAY_SYM && !(be[i]->attrs & ATTR_QUOTED)) {
                            ckey_name = be[i]->i64;
                            break;
                        }
                    }
                }
                if (ckey_name < 0) ckey_name = ray_sym_intern("key", 3);

                /* Collision check for dotted tail: if the tail name matches
                 * a source column that isn't the head of the dotted expr,
                 * the old code silently dropped that source column from the
                 * result.  Promote to the full dotted sym so both stay. */
                if (ckey_head >= 0 && ckey_full >= 0 && ckey_name != ckey_full) {
                    for (int64_t c = 0; c < tbl_ncols; c++) {
                        int64_t cn = ray_table_col_name(filtered_tbl, c);
                        if (cn == ckey_name && cn != ckey_head) {
                            ckey_name = ckey_full;
                            break;
                        }
                    }
                }

                ray_t* res2 = ray_table_new(tbl_ncols + 1);
                /* Key column: computed_key's first-of-group values, which
                 * are the distinct grouping-key values surfaced to the
                 * user.  Using the source column at fi2 indices would lose
                 * the transform (e.g. raw Timestamp instead of its `.ss`). */
                if (ray_is_vec(computed_key)) {
                    ray_t* kv = ray_vec_new(computed_key->type, ng2);
                    if (!RAY_IS_ERR(kv)) {
                        /* len BEFORE store loop — ray_vec_set_null (called
                         * by store_typed_elem for null atoms) range-checks
                         * idx against vec->len and silently no-ops
                         * otherwise. */
                        kv->len = ng2;
                        for (int64_t g2 = 0; g2 < ng2; g2++) {
                            int a2 = 0;
                            ray_t* v2 = collection_elem(computed_key, fi2[g2], &a2);
                            store_typed_elem(kv, g2, v2);
                            if (a2) ray_release(v2);
                        }
                        res2 = ray_table_add_col(res2, ckey_name, kv);
                        ray_release(kv);
                    }
                }
                for (int64_t c = 0; c < tbl_ncols; c++) {
                    int64_t cn = ray_table_col_name(filtered_tbl, c);
                    /* Avoid duplicating a column name already used by the
                     * key: e.g. `by: Timestamp` (plain, non-dotted) would
                     * collide with the source Timestamp column. */
                    if (cn == ckey_name) continue;
                    ray_t* sc = ray_table_get_col_idx(filtered_tbl, c);
                    ray_t* dc = ray_vec_new(sc->type, ng2);
                    dc->len = ng2;    /* see note above — hoisted for null bits */
                    for (int64_t g2 = 0; g2 < ng2; g2++) { int a2 = 0; ray_t* v2 = collection_elem(sc, fi2[g2], &a2); store_typed_elem(dc, g2, v2); if (a2) ray_release(v2); }
                    res2 = ray_table_add_col(res2, cn, dc); ray_release(dc);
                }
                if (fi2_hdr) ray_free(fi2_hdr);
                ray_release(groups2); ray_release(computed_key);
                if (filtered_tbl != tbl) ray_release(filtered_tbl);
                ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return res2;
            }

            ray_t* orig_key_col = ray_table_get_col(filtered_tbl, key_sym);
            int64_t nrows_orig = orig_key_col ? orig_key_col->len : 0;

            /* Read group key values from grouped table BEFORE releasing it.
             * grp_key_col points into grouped — must not access after release. */
            ray_t* grp_key_col = ray_table_get_col(grouped, key_sym);
            int8_t kt = orig_key_col ? orig_key_col->type : 0;

            /* Heap-allocate gk_vals when n_groups > 256 */
            int64_t gk_stack[256];
            ray_t* gk_heap_hdr = NULL;
            int64_t* gk_vals = gk_stack;
            if (n_groups > 256) {
                gk_heap_hdr = ray_alloc((size_t)n_groups * sizeof(int64_t));
                if (!gk_heap_hdr) { ray_release(grouped); if (filtered_tbl != tbl) ray_release(filtered_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                gk_vals = (int64_t*)ray_data(gk_heap_hdr);
            }

            /* Copy group key values while grouped is still alive.
             * STR/LIST/GUID keys are routed through eval-level fallback
             * above, so only integer-like types reach here.  Use
             * read_col_i64 for non-F64 types — it dispatches on the
             * column type (I32/I16/I8/BOOL/SYM adaptive width etc.),
             * whereas ray_read_sym interprets `attrs` as SYM width and
             * silently truncates to 1 byte for plain integer columns
             * where attrs doesn't carry width bits.
             *
             * We also record a per-group null flag.  The DAG GROUP path
             * stores null keys with value=0 and differentiates via a
             * null mask — if we hashed raw bits only, a null group would
             * collide with non-null value 0 (for I64 / I32 / SYM / DATE /
             * TIME etc.), and for F64 with +0.0, since an all-zero key slot
             * reads back as +0.0.  The null flag keeps those groups
             * distinct. */
            uint8_t gk_null_stack[256];
            ray_t*  gk_null_hdr = NULL;
            uint8_t* gk_null = gk_null_stack;
            if (n_groups > 256) {
                gk_null_hdr = ray_alloc((size_t)n_groups * sizeof(uint8_t));
                if (!gk_null_hdr) {
                    if (gk_heap_hdr) ray_free(gk_heap_hdr);
                    ray_release(grouped);
                    if (filtered_tbl != tbl) ray_release(filtered_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                gk_null = (uint8_t*)ray_data(gk_null_hdr);
            }
            memset(gk_null, 0, (size_t)n_groups * sizeof(uint8_t));

            if (grp_key_col) {
                bool gk_has_nulls = ray_vec_may_have_nulls(grp_key_col);
                if (gk_has_nulls) {
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        if (kt == RAY_F64) {
                            /* -0.0 -> +0.0, matching group.c's key-read
                             * normalisation (group_key_f64_bits, #407):
                             * gk_vals is compared BIT-WISE against the source
                             * column below while the probe hashes through
                             * ray_hash_f64 (which normalises), so both sides
                             * must carry canonical bits or a -0.0 row would
                             * never match its own +0.0 group. */
                            double dv = clear_neg_zero(((double*)ray_data(grp_key_col))[gi]);
                            memcpy(&gk_vals[gi], &dv, 8);
                        } else
                            gk_vals[gi] = read_col_i64(ray_data(grp_key_col), gi, kt, grp_key_col->attrs);
                        if (ray_vec_is_null(grp_key_col, gi))
                            gk_null[gi] = 1;
                    }
                } else {
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        if (kt == RAY_F64) {           /* canonical -0.0, see above */
                            double dv = clear_neg_zero(((double*)ray_data(grp_key_col))[gi]);
                            memcpy(&gk_vals[gi], &dv, 8);
                        } else
                            gk_vals[gi] = read_col_i64(ray_data(grp_key_col), gi, kt, grp_key_col->attrs);
                    }
                }
            }
            ray_release(grouped); /* grp_key_col is now invalid */

            /* Allocate first_idx */
            int64_t first_idx_stack[256];
            ray_t* fi_heap_hdr = NULL;
            int64_t* first_idx = first_idx_stack;
            if (n_groups > 256) {
                fi_heap_hdr = ray_alloc((size_t)n_groups * sizeof(int64_t));
                if (!fi_heap_hdr) { if (gk_heap_hdr) ray_free(gk_heap_hdr); if (filtered_tbl != tbl) ray_release(filtered_tbl); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                first_idx = (int64_t*)ray_data(fi_heap_hdr);
            }

            /* Build {key_bits -> group_index} hash table from gk_vals so the
             * scan below is O(nrows_orig + n_groups) instead of
             * O(nrows_orig * n_groups).  Without this a 1M-row / 1M-group
             * float-key grouping hangs for tens of seconds — I64 has a
             * low-cardinality direct-array fast path upstream, but F64
             * and other non-GUID scalar keys fall through to this scan. */
            for (int64_t gi = 0; gi < n_groups; gi++) first_idx[gi] = -1;
            {
                uint32_t fi_cap = 64;
                while ((uint64_t)fi_cap < (uint64_t)n_groups * 2 && fi_cap < (1u << 30))
                    fi_cap <<= 1;
                uint32_t fi_mask = fi_cap - 1;
                ray_t* fi_ht_hdr = ray_alloc((size_t)fi_cap * sizeof(uint32_t));
                if (!fi_ht_hdr) {
                    if (gk_heap_hdr) ray_free(gk_heap_hdr);
                    if (fi_heap_hdr) ray_free(fi_heap_hdr);
                    if (filtered_tbl != tbl) ray_release(filtered_tbl);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                uint32_t* fi_ht = (uint32_t*)ray_data(fi_ht_hdr);
                memset(fi_ht, 0xFF, (size_t)fi_cap * sizeof(uint32_t));

                /* Insert every group key into the HT keyed by bit pattern.
                 * For F64 keys, hash via the float path; memcpy bit pattern
                 * out of gk_vals to dodge strict-aliasing.  Null groups
                 * get a distinct hash so they don't collide with zero-valued
                 * groups (null keys are stored as an all-zero key slot,
                 * which reads back as integer 0 / +0.0). */
                for (int64_t gi = 0; gi < n_groups; gi++) {
                    uint64_t h;
                    if (gk_null[gi]) {
                        h = ray_hash_i64((int64_t)0xDEADBEEFCAFEBABEULL);
                    } else if (kt == RAY_F64) {
                        double dv;
                        memcpy(&dv, &gk_vals[gi], 8);
                        h = ray_hash_f64(dv);
                    } else {
                        h = ray_hash_i64(gk_vals[gi]);
                    }
                    uint32_t slot = (uint32_t)(h & fi_mask);
                    while (fi_ht[slot] != UINT32_MAX) slot = (slot + 1) & fi_mask;
                    fi_ht[slot] = (uint32_t)gi;
                }

                /* Single linear scan of the source column; for each row
                 * hash-lookup its group index and record the first row
                 * that maps to it.  Terminate early once every group has
                 * a first-row. */
                bool orig_nulls_flag = orig_key_col
                    && ray_vec_may_have_nulls(orig_key_col);
                int64_t found = 0;
                for (int64_t r = 0; r < nrows_orig && found < n_groups; r++) {
                    bool r_null = orig_nulls_flag && ray_vec_is_null(orig_key_col, r);
                    int64_t ov;
                    if (kt == RAY_F64) {           /* canonical -0.0, see above */
                        double dv = clear_neg_zero(((double*)ray_data(orig_key_col))[r]);
                        memcpy(&ov, &dv, 8);
                    } else ov = read_col_i64(ray_data(orig_key_col), r, kt, orig_key_col->attrs);
                    uint64_t h;
                    if (r_null) {
                        h = ray_hash_i64((int64_t)0xDEADBEEFCAFEBABEULL);
                    } else if (kt == RAY_F64) {
                        double dv;
                        memcpy(&dv, &ov, 8);
                        h = ray_hash_f64(dv);
                    } else {
                        h = ray_hash_i64(ov);
                    }
                    uint32_t slot = (uint32_t)(h & fi_mask);
                    while (fi_ht[slot] != UINT32_MAX) {
                        uint32_t cand = fi_ht[slot];
                        bool match = (r_null && gk_null[cand])
                                     || (!r_null && !gk_null[cand] && gk_vals[cand] == ov);
                        if (match) {
                            if (first_idx[cand] < 0) {
                                first_idx[cand] = r;
                                found++;
                            }
                            break;
                        }
                        slot = (slot + 1) & fi_mask;
                    }
                }
                ray_free(fi_ht_hdr);
            }
            if (gk_null_hdr) ray_free(gk_null_hdr);
            if (gk_heap_hdr) ray_free(gk_heap_hdr);

            /* Now build the result table using first_idx gathered above.
             * key_sym and n_groups are already set. */

            /* Build result table: key column first, then others */
            int64_t ncols = ray_table_ncols(filtered_tbl);
            ray_t* result = ray_table_new(ncols);
            if (RAY_IS_ERR(result)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result; }

            /* Add key column first */
            ray_t* key_vec_src = ray_table_get_col(filtered_tbl, key_sym);
            if (key_vec_src->type == RAY_STR) {
                ray_t* key_vec_dst = ray_vec_new(RAY_STR, n_groups);
                if (!key_vec_dst || RAY_IS_ERR(key_vec_dst)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return key_vec_dst ? key_vec_dst : ray_error("oom", NULL); }
                for (int64_t gi = 0; gi < n_groups; gi++) {
                    size_t slen = 0;
                    const char* sp = ray_str_vec_get(key_vec_src, first_idx[gi], &slen);
                    key_vec_dst = ray_str_vec_append(key_vec_dst, sp ? sp : "", sp ? slen : 0);
                    if (RAY_IS_ERR(key_vec_dst)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return key_vec_dst; }
                }
                result = ray_table_add_col(result, key_sym, key_vec_dst);
                ray_release(key_vec_dst);
            } else {
                ray_t* key_vec_dst = ray_vec_new(key_vec_src->type, n_groups);
                if (RAY_IS_ERR(key_vec_dst)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return key_vec_dst; }
                /* Set len BEFORE the store loop: store_typed_elem routes
                 * null atoms through ray_vec_set_null, which range-checks
                 * idx against vec->len and silently returns RAY_ERR_RANGE
                 * otherwise.  Postponing len=n_groups until after the loop
                 * therefore dropped the null bit on every nullable key row
                 * — the result would read back the raw zero/-0.0 bits with
                 * no HAS_NULLS flag, corrupting the grouped key column. */
                key_vec_dst->len = n_groups;
                for (int64_t gi = 0; gi < n_groups; gi++) {
                    int alloc = 0;
                    ray_t* val = collection_elem(key_vec_src, first_idx[gi], &alloc);
                    store_typed_elem(key_vec_dst, gi, val);
                    if (alloc) ray_release(val);
                }
                result = ray_table_add_col(result, key_sym, key_vec_dst);
                ray_release(key_vec_dst);
            }

            /* Add non-key columns */
            for (int64_t c = 0; c < ncols; c++) {
                int64_t col_name = ray_table_col_name(filtered_tbl, c);
                if (col_name == key_sym) continue;
                ray_t* src_col = ray_table_get_col_idx(filtered_tbl, c);
                int8_t ct = src_col->type;

                if (ct == RAY_STR) {
                    /* String column: build STR vector */
                    ray_t* dst = ray_vec_new(RAY_STR, n_groups);
                    if (!dst || RAY_IS_ERR(dst)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return dst ? dst : ray_error("oom", NULL); }
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        size_t slen = 0;
                        const char* sp = ray_str_vec_get(src_col, first_idx[gi], &slen);
                        dst = ray_str_vec_append(dst, sp ? sp : "", sp ? slen : 0);
                        if (RAY_IS_ERR(dst)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return dst; }
                    }
                    result = ray_table_add_col(result, col_name, dst);
                    ray_release(dst);
                } else if (ct == RAY_LIST) {
                    /* List column: pick items */
                    ray_t* dst = ray_alloc(n_groups * sizeof(ray_t*));
                    if (!dst) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
                    dst->type = RAY_LIST;
                    dst->len = n_groups;
                    ray_t** dout = (ray_t**)ray_data(dst);
                    ray_t** src_items = (ray_t**)ray_data(src_col);
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        dout[gi] = src_items[first_idx[gi]];
                        ray_retain(dout[gi]);
                    }
                    result = ray_table_add_col(result, col_name, dst);
                    ray_release(dst);
                } else {
                    /* Typed vector: copy elements at first indices.
                     * len must be set before the store loop so null bits
                     * propagate through store_typed_elem → ray_vec_set_null
                     * (same reason as the key column above). */
                    ray_t* dst = ray_vec_new(ct, n_groups);
                    if (RAY_IS_ERR(dst)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); ray_release(result); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return dst; }
                    dst->len = n_groups;
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        int alloc = 0;
                        ray_t* val = collection_elem(src_col, first_idx[gi], &alloc);
                        store_typed_elem(dst, gi, val);
                        if (alloc) ray_release(val);
                    }
                    result = ray_table_add_col(result, col_name, dst);
                    ray_release(dst);
                }
                if (RAY_IS_ERR(result)) { if (fi_heap_hdr) ray_free(fi_heap_hdr); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result; }
            }

            if (fi_heap_hdr) ray_free(fi_heap_hdr);
            if (filtered_tbl != tbl) ray_release(filtered_tbl);
            ray_release(tbl);
            result = apply_sort_take(result, dict_elems, dict_n,
                                     asc_id, desc_id, take_id, NULL);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
        }
    } else if (n_out > 0) {
        /* No `by:` but explicit output expressions.
         *
         * Two sub-cases:
         *   (a) All outputs are aggregates → scalar reduction.  Route
         *       through ray_group(n_keys=0) so the result is ONE row,
         *       not the input row count broadcast.  The naive ray_select
         *       path lowers `(sum c)` to OP_SUM as a column expression;
         *       OP_SELECT then broadcasts the scalar atom to nrows
         *       (exec.c: vec->type < 0 → broadcast_scalar), producing
         *       N copies of the same value.
         *   (b) At least one non-agg output → keep the existing
         *       projection (broadcast-as-column) with per-row
         *       evaluation semantics.
         *
         * Mixed agg+non-agg without `by:` continues to flow through (b);
         * the semantics there imply LIST/scalar mixing that is out of
         * scope for this fix. */
        int has_agg = 0;
        int has_nonagg_out = 0;
        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            if (kid == from_id || kid == where_id || kid == by_id ||
                kid == take_id || kid == asc_id || kid == desc_id || kid == nearest_id) continue;
            if (is_agg_expr(dict_elems[i + 1])) has_agg = 1;
            else has_nonagg_out = 1;
        }

        if (has_agg && !has_nonagg_out && !nearest_expr) {
            /* Scalar reduction.  Pre-execute the WHERE filter (already
             * wired as ray_filter at the top) so OP_FILTER on the table
             * input populates g->selection, which exec_group then
             * honours in its n_keys==0 fast path. */
            if (where_expr) {
                root = ray_optimize(g, root);
                ray_t* fres = exec_node(g, root);
                if (!fres || RAY_IS_ERR(fres)) {
                    if (g->selection) {
                        ray_release(g->selection);
                        g->selection = NULL;
                    }
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: `where:` filter produced no result");
                }
                ray_release(fres);
            }

            /* Exact-size scratch carve: bound by select_output_count (every
             * output here is an aggregate, so outputs == aggs).  The former
             * fixed [16] arrays silently stopped compiling past the 16th
             * output (loop guard `s_n_aggs < 16`) — the remaining outputs
             * were never wired into the group node. */
            int64_t s_max = select_output_count(dict_elems, dict_n);
            if (s_max < 1) s_max = 1;
            ray_t* sagg_hdr = NULL;
            ray_op_t** s_agg_ins = (ray_op_t**)scratch_alloc(&sagg_hdr,
                    (size_t)s_max * (2 * sizeof(ray_op_t*) + sizeof(uint16_t)));
            if (!s_agg_ins) {
                if (g->selection) { ray_release(g->selection); g->selection = NULL; }
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
            }
            ray_op_t** s_agg_ins2 = s_agg_ins + s_max;
            uint16_t*  s_agg_ops  = (uint16_t*)(s_agg_ins2 + s_max);
            int64_t    s_n_aggs = 0;
            int        s_has_binary = 0;
            int        s_use_eval_fallback = 0;
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id ||
                    kid == take_id || kid == asc_id || kid == desc_id || kid == nearest_id) continue;
                ray_t*  val_expr  = dict_elems[i + 1];
                ray_t** agg_elems = (ray_t**)ray_data(val_expr);
                uint16_t op = resolve_agg_opcode(agg_elems[0]->i64);
                s_agg_ops[s_n_aggs] = op;
                s_agg_ins[s_n_aggs] = compile_expr_dag(g, agg_elems[1]);
                s_agg_ins2[s_n_aggs] = NULL;
                if (!s_agg_ins[s_n_aggs]) {
                    s_use_eval_fallback = 1;
                    break;
                }
                /* Canonical aggregand type-admission (same table as the scalar
                 * builtins): reject non-numeric (SYM/STR/GUID) and, for sum,
                 * absolute-temporal (DATE/TIMESTAMP) inputs so the DAG never
                 * silently aggregates raw symbol ids / string bytes / dates. */
                if (s_agg_ins[s_n_aggs]->out_type > 0 &&
                    !agg_type_admitted(op, s_agg_ins[s_n_aggs]->out_type)) {
                    int8_t in_t = s_agg_ins[s_n_aggs]->out_type;            /* capture BEFORE free */
                    if (g->selection) { ray_release(g->selection); g->selection = NULL; }
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(sagg_hdr);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "select: aggregation does not admit input type %s", ray_type_name(in_t));
                }
                if (agg_is_binary_agg(op)) {
                    if (ray_len(val_expr) < 3) {
                        if (g->selection) { ray_release(g->selection); g->selection = NULL; }
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(sagg_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("arity", "select: binary aggregation requires two column arguments");
                    }
                    s_agg_ins2[s_n_aggs] = compile_expr_dag(g, agg_elems[2]);
                    if (!s_agg_ins2[s_n_aggs]) {
                        s_use_eval_fallback = 1;
                        break;
                    }
                    if (s_agg_ins2[s_n_aggs]->out_type > 0 &&
                        !agg_type_admitted(op, s_agg_ins2[s_n_aggs]->out_type)) {
                        int8_t in_t = s_agg_ins2[s_n_aggs]->out_type;
                        if (g->selection) { ray_release(g->selection); g->selection = NULL; }
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(sagg_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "select: binary aggregation does not admit second input type %s", ray_type_name(in_t));
                    }
                    s_has_binary = 1;
                }
                s_n_aggs++;
            }
            if (s_use_eval_fallback) {
                ray_t* eval_tbl = tbl;
                int release_eval_tbl = 0;
                if (g->selection) {
                    eval_tbl = sel_compact(g, tbl, g->selection, NULL, 0);
                    if (!eval_tbl || RAY_IS_ERR(eval_tbl)) {
                        if (g->selection) { ray_release(g->selection); g->selection = NULL; }
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(sagg_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return eval_tbl ? eval_tbl : ray_error("oom", NULL);
                    }
                    release_eval_tbl = 1;
                }
                ray_t* result = eval_scalar_agg_outputs(dict_elems, dict_n, eval_tbl,
                                                        from_id, where_id, by_id,
                                                        take_id, asc_id, desc_id,
                                                        nearest_id);
                if (release_eval_tbl) ray_release(eval_tbl);
                if (g->selection) { ray_release(g->selection); g->selection = NULL; }
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sagg_hdr);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
            }
            if (s_has_binary)
                root = ray_group_build(g, NULL, 0, s_agg_ops, s_agg_ins,
                                       s_agg_ins2, NULL, s_n_aggs);
            else
                root = ray_group(g, NULL, 0, s_agg_ops, s_agg_ins, s_n_aggs);
            scratch_free(sagg_hdr);
        } else {
            /* Projection only (no group by) — select specific columns.
             * Exact-size scratch carve bound by select_output_count — the
             * former fixed col_ops[16] silently routed >16 outputs to the
             * eval fallback via the width check below; now only the other
             * use_eval_fallback trigger (a failed compile) applies. */
            int64_t nc_max = select_output_count(dict_elems, dict_n);
            /* plus one slot per sort key name: any of them may have to be
             * carried through the projection as a hidden column */
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid != asc_id && kid != desc_id) continue;
                ray_t* val = dict_elems[i + 1];
                if (val->type == -RAY_SYM) nc_max += 1;
                else if (ray_is_vec(val) && val->type == RAY_SYM) nc_max += ray_len(val);
            }
            if (nc_max < 1) nc_max = 1;
            ray_t* colops_hdr = NULL;
            ray_op_t** col_ops = (ray_op_t**)scratch_alloc(&colops_hdr,
                    (size_t)nc_max * (sizeof(ray_op_t*) + sizeof(int64_t) + sizeof(uint32_t)));
            if (!col_ops) {
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
            }
            int64_t nc = 0;
            int use_eval_fallback = 0;
            /* Projections see the projections before them: once an output
             * expression has compiled, its alias is published on the graph
             * (g->sel_alias_*), where a later projection's name reference
             * or literal symbol finds it ahead of the source table's
             * columns.  The binding is made AFTER the expression compiles,
             * so an alias that shadows a source column still reads the
             * source in its own definition and the new value in every
             * later one.  where:, by: and the sort keys are compiled
             * outside this window and stay alias-blind. */
            int64_t*  alias_syms = (int64_t*)(col_ops + nc_max);
            uint32_t* alias_ids  = (uint32_t*)(alias_syms + nc_max);
            g->sel_alias_syms = alias_syms;
            g->sel_alias_ids  = alias_ids;
            g->sel_alias_n    = 0;
            for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                int64_t kid = dict_elems[i]->i64;
                if (kid == from_id || kid == where_id || kid == by_id || kid == take_id || kid == asc_id || kid == desc_id || kid == nearest_id) continue;
                col_ops[nc] = compile_expr_dag(g, dict_elems[i + 1]);
                if (!col_ops[nc]) {
                    use_eval_fallback = 1;
                    break;
                }
                alias_syms[nc] = kid;
                alias_ids[nc]  = col_ops[nc]->id;
                nc++;
                g->sel_alias_n = (int)nc;
            }
            g->sel_alias_syms = NULL;
            g->sel_alias_ids  = NULL;
            g->sel_alias_n    = 0;
            /* Sort keys name SOURCE columns (like where: and by:, they are
             * alias-blind).  A key some output scans bare is read from that
             * output (the sort binds to the projected column by position,
             * see exec_sort); a key no output scans is projected too, after
             * the outputs, and dropped from the result afterwards.  Output
             * aliases are not consulted: `{b: a ... asc: b}` sorts by the
             * source column b, not by the output that renames a. */
            if (!use_eval_fallback && has_sort) {
                for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    if (kid != asc_id && kid != desc_id) continue;
                    ray_t* val = dict_elems[i + 1];
                    int64_t nk = val->type == -RAY_SYM ? 1
                               : (ray_is_vec(val) && val->type == RAY_SYM) ? val->len : 0;
                    for (int64_t k = 0; k < nk; k++) {
                        int64_t ks = val->type == -RAY_SYM ? val->i64 : sym_cell_runtime_id(val, k);
                        if (select_scan_output(g, col_ops, nc, ks) >= 0) continue;
                        if (!ray_table_get_col(tbl, ks) || nc >= nc_max) continue;
                        ray_t* nm = ray_sym_str(ks);
                        ray_op_t* sc = nm ? ray_scan(g, ray_str_ptr(nm)) : NULL;
                        if (!sc) continue;
                        col_ops[nc] = sc;
                        alias_syms[nc] = ks;
                        alias_ids[nc] = sc->id;
                        nc++;
                        n_hidden_sort++;
                    }
                }
            }
            if (use_eval_fallback) {
                /* A projection the compiler rejected outright is an error,
                 * not a fallback: scattered per row, (> ts (avg ts)) would
                 * average one cell and compare it with itself. */
                if (g->compile_err) {
                    ray_t* cerr = graph_take_compile_err(g);
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(colops_hdr);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cerr;
                }
                /* The fallback evaluates projections directly over `tbl`,
                 * bypassing the DAG's ray_execute — so a WHERE clause (wired
                 * into `root` as ray_filter) would be silently ignored.
                 * Materialize the filtered table first and project over it,
                 * mirroring the group-by path's materialize branch. */
                if (where_expr) {
                    root = ray_optimize(g, root);
                    ray_t* fres = ray_execute(g, root);
                    ray_graph_free(g); g = NULL;
                    if (fres && !RAY_IS_ERR(fres) && ray_is_lazy(fres))
                        fres = ray_lazy_materialize(fres);
                    if (!fres || RAY_IS_ERR(fres)) {
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return fres ? fres : ray_error("domain", "select: `where:` filter produced no result");
                    }
                    ray_release(tbl);
                    tbl = fres;
                    g = ray_graph_new(tbl);
                    if (!g) {
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                }
                /* The per-row reader has no arm for segmented columns, so a
                 * projection over a .db.parted.get table raised `type` here.
                 * Flatten a parted / mapcommon table once, as update does, so
                 * every projection below sees plain vectors. */
                if (table_has_parted_columns(tbl)) {
                    ray_t* flat = query_materialize_parted_table(tbl);
                    if (!flat || RAY_IS_ERR(flat)) {
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return flat ? flat : ray_error("oom", NULL);
                    }
                    ray_release(tbl);
                    tbl = flat;
                    ray_graph_free(g);
                    g = ray_graph_new(tbl);
                    if (!g) {
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                }
                ray_t* result = ray_table_new(0);
                if (!result || RAY_IS_ERR(result)) {
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(colops_hdr);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result ? result : ray_error("oom", NULL);
                }
                int64_t nrows = ray_table_nrows(tbl);
                /* A projection with an aggregate in it and no column ref
                 * outside the aggregate's argument (expr_contains_agg &&
                 * !expr_refs_row_column) is one value for the whole select.
                 * Scattered per row it saw a one-cell slice
                 * of every column, so `(max i)` beside a projection the DAG
                 * declines came back as `i` itself (#692).  Such a
                 * projection is evaluated once over the whole table and the
                 * value broadcast as the DAG path broadcasts it (exec.c
                 * OP_SELECT): to nrows beside a projection that flows a row
                 * column through, and to one row when no projection does
                 * (case (a) above: an all-aggregate select is one row).
                 * A literal, or an expression over earlier one-value
                 * outputs, carries no rows either (#698); a select of
                 * literals alone keeps the input's rows. */
                int any_row_proj = 0, any_agg = 0;
                ray_t* scal_hdr = NULL;
                int64_t* scalars = (int64_t*)scratch_alloc(&scal_hdr,
                        (size_t)(dict_n / 2 + 1) * sizeof(int64_t));
                if (!scalars) {
                    ray_release(result);
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    scratch_free(colops_hdr);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                int64_t n_scalars = 0;
                for (int64_t i = 0; i + 1 < dict_n && !any_row_proj; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    if (kid == from_id || kid == where_id || kid == by_id ||
                        kid == take_id || kid == asc_id || kid == desc_id ||
                        kid == nearest_id) continue;
                    ray_t* e = dict_elems[i + 1];
                    int has_agg = select_fallback_has_agg(e);
                    any_agg |= has_agg;
                    any_row_proj = has_agg ? expr_refs_row_column(e, tbl)
                                           : !select_fallback_scalar_proj(e, scalars, n_scalars);
                    if (!any_row_proj) scalars[n_scalars++] = kid;
                }
                scratch_free(scal_hdr);
                if (!any_agg) any_row_proj = 1;
                int64_t bcast_len = any_row_proj ? nrows : 1;
                int64_t out_len = -1;   /* length of the first materialized column */
                for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    if (kid == from_id || kid == where_id || kid == by_id ||
                        kid == take_id || kid == asc_id || kid == desc_id ||
                        kid == nearest_id) continue;
                    /* Whole-column verbs (distinct/asc/desc/reverse) consume the
                     * entire column and must be evaluated once, not scattered
                     * per-row; so must a one-value projection (an aggregate,
                     * alone or under scalar arithmetic / a scalar `if`), whose
                     * value is then broadcast.  Everything else — literals,
                     * lambda calls, and a row column mixed with an aggregate
                     * in one expression — keeps the row-by-row semantics.
                     * With no row projection at all nothing is scattered: an
                     * alias bound below as a one-row column must not be read
                     * per row.  A bare column name is the column itself. */
                    ray_t* expr = dict_elems[i + 1];
                    int whole_verb = is_whole_column_projection(expr);
                    int one_value = !whole_verb &&
                                    (!any_row_proj ||
                                     (select_fallback_has_agg(expr) && !expr_refs_row_column(expr, tbl)));
                    /* A row column mixed with an aggregate in one
                     * expression: its aggregates are reduced once over
                     * the whole table before the scatter, which would
                     * otherwise fold a single cell (`(+ i (max i))` came
                     * back as `(* 2 i)`; select_fallback_prereduce). */
                    ray_t* reduced = NULL;
                    if (!whole_verb && !one_value && select_fallback_has_agg(expr)) {
                        select_fallback_prereduce_ctx_t pc = { tbl };
                        reduced = select_fallback_prereduce(expr, &pc);
                        if (reduced && !RAY_IS_ERR(reduced)) expr = reduced;
                    }
                    ray_t* col;
                    if (reduced && RAY_IS_ERR(reduced)) {
                        col = reduced;
                    } else {
                        col = select_fallback_passthrough_col(expr, tbl, nrows);
                        if (!col) {
                            /* An expression over earlier one-value outputs
                             * with no aggregate of its own reads them at
                             * row 0, as the row path reads a cell: they are
                             * bound as one-row columns, and over the whole
                             * column `(til m)` saw `[6]`, not 6 (#698). */
                            int at_row0 = one_value && !whole_verb &&
                                          !ray_is_atom(expr) &&
                                          !select_fallback_has_agg(expr);
                            col = at_row0 ? eval_expr_per_row(expr, tbl, 1)
                                : (whole_verb || one_value)
                                ? eval_expr_whole_column(expr, tbl)
                                : eval_expr_per_row(expr, tbl, nrows);
                            if (one_value) col = select_fallback_broadcast(col, bcast_len);
                        }
                        if (reduced) ray_release(reduced);
                    }
                    if (!col || RAY_IS_ERR(col)) {
                        ray_t* err = col ? col : ray_error("domain", "select: failed to evaluate output column expression");
                        ray_release(result);
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return err;
                    }
                    /* All output columns must agree in length — a length-changing
                     * projection (e.g. distinct) beside a full-length column would
                     * yield a ragged table.  Reject rather than emit one. */
                    int64_t col_len = ray_len(col);
                    if (out_len < 0) {
                        out_len = col_len;
                    } else if (col_len != out_len) {
                        ray_release(col);
                        ray_release(result);
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("length", "select: output column length %lld does not match %lld", (long long)col_len, (long long)out_len);
                    }
                    /* Later projections see this one: bind it in the table
                     * the remaining expressions are evaluated over, in place
                     * of a source column of the same name.  A column of
                     * another length (`distinct`) cannot be a row of that
                     * table and stays unbound.  In an all-aggregate select
                     * the one-row value is what a later projection reads
                     * (`{m: (max ts) y: (+ m 1)}`), and nothing scatters it. */
                    if (col_len == bcast_len) {
                        ray_t* bound = select_fallback_bind_alias(tbl, kid, col);
                        if (!bound || RAY_IS_ERR(bound)) {
                            ray_release(col);
                            ray_release(result);
                            if (nearest_handle_owned) ray_release(nearest_handle_owned);
                            if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                            ray_graph_free(g);
                            scratch_free(colops_hdr);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return bound ? bound : ray_error("oom", NULL);
                        }
                        tbl = bound;
                    }
                    result = ray_table_add_col(result, kid, col);
                    ray_release(col);
                    if (RAY_IS_ERR(result)) {
                        if (nearest_handle_owned) ray_release(nearest_handle_owned);
                        if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                        ray_graph_free(g); ray_release(tbl);
                        scratch_free(colops_hdr);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                    }
                }
                if (nearest_handle_owned) ray_release(nearest_handle_owned);
                if (nearest_query_owned)  ray_free_raw(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                result = apply_sort_take(result, dict_elems, dict_n,
                                         asc_id, desc_id, take_id, NULL);
                scratch_free(colops_hdr);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
            } else {
                root = ray_select_op(g, root, col_ops, nc);
                scratch_free(colops_hdr);
            }
        }
    }

    /* Sort: collect asc/desc columns in dict iteration order.
     * Only add to the DAG when there's no group-by — group-by changes the
     * output schema, so sort on output columns must happen post-execution.
     * Values are unevaluated — a SYM atom is a column name, a SYM vector
     * is multiple column names.  No ray_eval needed. */
    if (has_sort && !by_expr) {
        /* Exact-size scratch carve: bound = total names across the dict's
         * asc:/desc: entries (a -RAY_SYM atom counts 1, a RAY_SYM vector
         * counts its length).  The former fixed [16] arrays silently
         * capped multi-key sorts past the 16th key. */
        int64_t n_sort_max = 0;
        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            if (kid != asc_id && kid != desc_id) continue;
            ray_t* val = dict_elems[i + 1];
            if (val->type == -RAY_SYM) n_sort_max += 1;
            else if (ray_is_vec(val) && val->type == RAY_SYM) n_sort_max += ray_len(val);
        }
        if (n_sort_max < 1) n_sort_max = 1;
        ray_t* sortk_hdr = NULL;
        ray_op_t** sort_keys = (ray_op_t**)scratch_alloc(&sortk_hdr,
                (size_t)n_sort_max * (sizeof(ray_op_t*) + 1));
        if (!sort_keys) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL); }
        uint8_t* sort_descs = (uint8_t*)(sort_keys + n_sort_max);
        int64_t n_sort = 0;
        for (int64_t i = 0; i + 1 < dict_n; i += 2) {
            int64_t kid = dict_elems[i]->i64;
            uint8_t is_desc = 0;
            if (kid == asc_id) is_desc = 0;
            else if (kid == desc_id) is_desc = 1;
            else continue;
            ray_t* val = dict_elems[i + 1];
            if (val->type == -RAY_SYM) {
                /* Single column name */
                ray_t* s = ray_sym_str(val->i64);
                sort_keys[n_sort] = select_sort_key_op(g, root, val->i64, ray_str_ptr(s));
                sort_descs[n_sort] = is_desc;
                n_sort++;
            } else if (ray_is_vec(val) && val->type == RAY_SYM) {
                /* Multiple column names — cell-data via the vec's domain */
                for (int64_t c = 0; c < val->len; c++) {
                    ray_t* s = ray_sym_vec_cell(val, c);
                    sort_keys[n_sort] = select_sort_key_op(g, root, sym_cell_runtime_id(val, c), ray_str_ptr(s));
                    sort_descs[n_sort] = is_desc;
                    n_sort++;
                }
            } else {
                scratch_free(sortk_hdr);
                ray_graph_free(g); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select: asc/desc value must be a column name or symbol list, got %s", ray_type_name(val->type));
            }
        }
        if (n_sort > 0)
            root = ray_sort_op(g, root, sort_keys, sort_descs, NULL, n_sort);
        scratch_free(sortk_hdr);
    }

    /* Take: added to the DAG when there is no nearest (rerank absorbs the take
     * into its k parameter).
     *
     * Without a group-by the take is the DAG's HEAD/TAIL outright.  WITH a
     * group-by the result schema differs from the input, so the take is
     * normally left to the post-execution apply_sort_take — EXCEPT for a
     * positive integer atom take with no asc:/desc: clause: the group emits
     * groups in stable first-seen order, so "first N rows of the group result"
     * is exactly "first N groups".  Adding a HEAD above the GROUP node lets
     * exec.c's HEAD(GROUP) fusion forward N to exec_group as the group_limit
     * HINT, which the v2 radix engine uses to emit only N groups instead of
     * materializing all of them.  The hint is advisory: HEAD still trims and
     * apply_sort_take still runs at the end, so correctness never depends on
     * it.  A range take [start amount] with start >= 0 pushes start+amount the
     * same way (apply_sort_take then slices the range out of those groups).
     * NOT pushed with a group-by: negative (tail) takes and ranges counted
     * from the end, which need the full group set; and any asc:/desc: shape,
     * which reorders the groups first (that shape also owns the desc+take emit-filter
     * machinery — has_sort keeps the two disjoint).  Also NOT pushed when a
     * deferred key WHERE runs AFTER the group (post_group_where_expr): that
     * filter drops result rows, so taking the first N groups before it would
     * under-fill the answer.  Nor over a PARTED input (exec_group_parted
     * mis-applies the hint on empty partitions — see table_is_parted), nor
     * with a LIST-producing aggregate (top/bot): exec.c's HEAD/TAIL table trim
     * now retains LIST cells, but the group's LIST output has no reason to pay
     * a trim it can skip, and the take is a no-op there anyway. */
    ray_t* take_range = NULL;
    take_pre_t take_pre = {0};   /* decoded take: value, reused by apply_sort_take */
    bool group_take_push = (take_expr && by_expr && !nearest_expr && !has_sort
                            && !post_group_where_expr && !has_list_agg
                            && !group_keys_have_bool(by_expr, tbl)
                            && !table_is_parted(tbl));
    if (take_expr && !nearest_expr && (!by_expr || group_take_push)) {
        ray_t* tv = ray_eval(take_expr);
        if (!tv || RAY_IS_ERR(tv)) { ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return tv ? tv : ray_error("domain", "select: failed to evaluate `take:`"); }
        if (ray_is_atom(tv) && (tv->type == -RAY_I64 || tv->type == -RAY_I32)) {
            int64_t n_take = (tv->type == -RAY_I64) ? tv->i64 : tv->i32;
            /* INT64_MIN (0N) negated for a tail pushdown is signed-overflow UB;
             * reject it before it reaches ray_tail / the TAIL kernel. */
            if (n_take == INT64_MIN) {
                ray_release(tv);
                ray_graph_free(g); ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv);
                return ray_error("type", "select: take: count is null or out of range");
            }
            if (group_take_push) {
                take_pre.kind = TAKE_PRE_ATOM;
                take_pre.a = n_take;
            }
            ray_release(tv);
            if (group_take_push) {
                if (n_take > 0) root = ray_head(g, root, n_take);
                /* n_take <= 0 with a group-by: leave it to apply_sort_take. */
            } else if (n_take >= 0)
                root = ray_head(g, root, n_take);
            else
                root = ray_tail(g, root, -n_take);
        } else if (ray_is_vec(tv) && (tv->type == RAY_I64 || tv->type == RAY_I32) && tv->len == 2) {
            if (group_take_push) {
                /* range: apply_sort_take slices it after execution */
                const int64_t* rv = (const int64_t*)ray_data(tv);
                take_pre.kind = TAKE_PRE_RANGE;
                if (tv->type == RAY_I64) { take_pre.a = rv[0]; take_pre.b = rv[1]; }
                else { const int32_t* r32 = (const int32_t*)rv;
                       take_pre.a = r32[0]; take_pre.b = r32[1]; }
                ray_release(tv);
                /* [start amount] from the front reads only the first
                 * start+amount groups: push that bound like an atom take. */
                if (take_pre.a >= 0 && take_pre.b > 0 && take_pre.a <= INT64_MAX - take_pre.b)
                    root = ray_head(g, root, take_pre.a + take_pre.b);
            } else take_range = tv;  /* apply after DAG execution */
        } else {
            int8_t tv_t = tv->type;            /* capture BEFORE free */
            ray_release(tv);
            ray_graph_free(g); ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select: `take:` must be an integer atom or a 2-element integer range, got %s", ray_type_name(tv_t));
        }
    }

    /* Install the pre-computed top-count emit filter just before
     * ray_execute reads it (and the DAG built above which already
     * consumed pre_top_emit via the no_where_count_key_ok check).
     * No re-running of match_group_desc_count_take needed. */
    ray_group_emit_filter_t prev_self_emit = {0};
    bool self_emit_set = false;
    if (pre_top_emit_matched) {
        prev_self_emit = ray_group_emit_filter_get();
        pre_top_emit.target_depth = __VM ? __VM->eval_depth : 0;   /* our own group node */
        ray_group_emit_filter_set(pre_top_emit);
        self_emit_set = true;
    }

    /* Optimize and execute */
    root = ray_optimize(g, root);
    ray_t* result = ray_execute(g, root);
    if (n_hidden_sort > 0 && result && !RAY_IS_ERR(result)) {
        if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
        if (result && !RAY_IS_ERR(result) && result->type == RAY_TABLE) {
            /* the hidden keys were projected last: drop the trailing
             * columns (by position — an output may carry the same name) */
            int64_t rc = ray_table_ncols(result);
            int64_t keep_n = rc - n_hidden_sort;
            ray_t* kept = keep_n >= 0 ? ray_table_new(keep_n > 0 ? keep_n : 1) : NULL;
            for (int64_t c = 0; kept && !RAY_IS_ERR(kept) && c < keep_n; c++)
                kept = ray_table_add_col(kept, ray_table_col_name(result, c),
                                         ray_table_get_col_idx(result, c));
            if (kept && !RAY_IS_ERR(kept)) { ray_release(result); result = kept; }
            else if (kept) ray_release(kept);
        }
    }
    /* A computed key takes its column name from its op's ext sym, which is
     * not a name (a const node's slot holds the literal; an expression node's
     * ext resolves to whatever shares its id) — name it the way the
     * eval-level path names a computed key, whichever key path ran. */
    if (computed_single_key && result && !RAY_IS_ERR(result) && ray_is_lazy(result))
        result = ray_lazy_materialize(result);
    if (computed_single_key && result && !RAY_IS_ERR(result) && result->type == RAY_TABLE &&
        ray_table_ncols(result) > 0) {
        int64_t kname = derived_key_name(by_expr);
        int64_t rc = ray_table_ncols(result);
        for (int64_t c = 1; c < rc; c++)
            if (ray_table_col_name(result, c) == kname) { kname = ray_sym_intern("key", 3); break; }
        ray_table_set_col_name(result, 0, kname);
    }
    if (self_emit_set)
        ray_group_emit_filter_set(prev_self_emit);
    if (post_group_where_expr && result && !RAY_IS_ERR(result))
        result = filter_group_result(result, post_group_where_expr);

    ray_graph_free(g);
    /* The nearest-query buffer was only referenced by ext->rerank.query_vec
     * and is safe to free once the graph (and thus the op ext) is gone. */
    if (nearest_query_owned) ray_free_raw(nearest_query_owned);
    /* The HNSW handle was kept alive through ray_execute so the rerank
     * ext's idx pointer stayed valid.  Safe to release now that the
     * graph (and its ext nodes) has been freed. */
    if (nearest_handle_owned) ray_release(nearest_handle_owned);

    /* Post-process: range take [start count] applied after execution */
    if (take_range && result && !RAY_IS_ERR(result)) {
        ray_t* sliced = ray_take_fn(result, take_range);
        ray_release(result);
        ray_release(take_range);
        result = sliced;
    } else if (take_range) {
        ray_release(take_range);
    }

    /* Post-process: reorder GROUP BY BOOL results to match first-occurrence
     * order in the original table (exec.c radix sort puts false before true) */
    if (by_expr && result && !RAY_IS_ERR(result) && result->type == RAY_TABLE) {
        if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
        if (result && !RAY_IS_ERR(result) && result->type == RAY_TABLE) {
            ray_t* key_col = ray_table_get_col_idx(result, 0);
            if (key_col && key_col->type == RAY_BOOL && key_col->len >= 2) {
                /* Find first-occurrence order of bool values in original
                 * table.  Accept both scalar `-RAY_SYM` and single-element
                 * `RAY_SYM` vector forms. */
                int64_t by_sym = -1;
                if (by_expr->type == -RAY_SYM)
                    by_sym = by_expr->i64;
                else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1)
                    by_sym = ((int64_t*)ray_data(by_expr))[0];
                ray_t* orig_key = (by_sym >= 0) ? ray_table_get_col(tbl, by_sym) : NULL;
                if (orig_key && orig_key->type == RAY_BOOL && orig_key->len > 0) {
                    bool first_val = ((bool*)ray_data(orig_key))[0];
                    bool result_first = ((bool*)ray_data(key_col))[0];
                    if (first_val != result_first) {
                        /* Swap rows: reverse row order in all columns */
                        int64_t nrows_r = ray_table_nrows(result);
                        int64_t ncols_r = ray_table_ncols(result);
                        ray_t* reordered = ray_table_new((int32_t)ncols_r);
                        if (reordered && !RAY_IS_ERR(reordered)) {
                            int ok = 1;
                            for (int64_t c = 0; c < ncols_r && ok; c++) {
                                int64_t cn = ray_table_col_name(result, c);
                                ray_t* col = ray_table_get_col_idx(result, c);
                                int esz = ray_elem_size(col->type);
                                ray_t* new_col = ray_vec_new(col->type, nrows_r);
                                if (RAY_IS_ERR(new_col)) { ok = 0; break; }
                                /* row-reversal raw-copies cell ids from ONE
                                 * source column — keep its dictionary */
                                if (col->type == RAY_SYM)
                                    ray_sym_vec_adopt_domain(new_col, col);
                                new_col->len = nrows_r;
                                char* src = (char*)ray_data(col);
                                char* dst = (char*)ray_data(new_col);
                                bool has_nulls = ray_vec_may_have_nulls(col);
                                if (has_nulls) {
                                    for (int64_t r = 0; r < nrows_r; r++) {
                                        memcpy(dst + r * esz, src + (nrows_r - 1 - r) * esz, esz);
                                        if (ray_vec_is_null(col, nrows_r - 1 - r))
                                            ray_vec_set_null(new_col, r, true);
                                    }
                                } else {
                                    for (int64_t r = 0; r < nrows_r; r++)
                                        memcpy(dst + r * esz, src + (nrows_r - 1 - r) * esz, esz);
                                }
                                reordered = ray_table_add_col(reordered, cn, new_col);
                                ray_release(new_col);
                                if (RAY_IS_ERR(reordered)) { ok = 0; break; }
                            }
                            if (ok) {
                                ray_release(result);
                                result = reordered;
                            } else if (reordered && !RAY_IS_ERR(reordered)) {
                                ray_release(reordered);
                            }
                        }
                    }
                }
            }
        }
    }

    /* Drop the synthesized COUNT column (used only to get group
     * boundaries when n_aggs == 0 && n_nonaggs > 0).  Must happen
     * before the rename/sort_take steps so they don't see a phantom
     * column. */
    if (synth_count_col && by_expr && result && !RAY_IS_ERR(result)) {
        if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
        if (result && !RAY_IS_ERR(result) && result->type == RAY_TABLE) {
            int64_t nc = ray_table_ncols(result);
            if (nc >= 1) {
                ray_t* rebuilt = ray_table_new(nc - 1);
                if (rebuilt && !RAY_IS_ERR(rebuilt)) {
                    for (int64_t c = 0; c < nc - 1; c++) {
                        int64_t cn = ray_table_col_name(result, c);
                        ray_t* col = ray_table_get_col_idx(result, c);
                        rebuilt = ray_table_add_col(rebuilt, cn, col);
                    }
                    ray_release(result);
                    result = rebuilt;
                }
            }
        }
    }

    /* NOTE: tbl is released below AFTER the non-agg scatter, which
     * runs post-rename and post-sort_take so LIST columns do not
     * flow through the scalar-only apply_sort_take DAG. */

    /* Rename output columns if user specified names */
    if (result && !RAY_IS_ERR(result) && n_out > 0) {
        /* Materialize lazy results if needed */
        if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
    }
    if (result && !RAY_IS_ERR(result) && result->type == RAY_TABLE && n_out > 0) {
        ray_t* schema = ray_table_schema(result);
        if (schema && !RAY_IS_ERR(schema) && schema->type > 0 && schema->type < RAY_TYPE_COUNT) {
            int64_t ncols = schema->len;
            /* Count key columns in by clause */
            int n_key_cols = 0;
            if (by_expr) {
                if (ray_is_vec(by_expr) && by_expr->type == RAY_SYM) n_key_cols = (int)ray_len(by_expr);
                else n_key_cols = 1;
            }
            /* Collect user-defined output column names.
             * For group-by, the result layout is [keys, aggs..., nonaggs...].
             * Non-agg columns were added by the post-DAG scatter block
             * with correct names already — only agg columns need renaming,
             * in dict-iteration order of the agg entries. */
            if (by_expr) {
                /* Parted by-dict streaming: the deferred key columns carry the
                 * source-derived names their key ops emitted (a rename groups on
                 * the source sym, a computed key on the expr's derived sym).
                 * Rename the key positions (0..n_key_cols-1) to the by-dict
                 * ALIAS names.  by_expr is the SYM vec of aliases in key order,
                 * and the group result always lays keys out first, so position
                 * maps 1:1.  No-op on every non-deferred path. */
                if (parted_bydict_deferred &&
                    ray_is_vec(by_expr) && by_expr->type == RAY_SYM) {
                    const int64_t* aliases = (const int64_t*)ray_data(by_expr);
                    for (int64_t k = 0; k < n_key_cols && k < ncols; k++)
                        ray_table_set_col_name(result, k, aliases[k]);
                }
                /* Rename only the agg columns (positions after keys).
                 * Non-agg/fallback columns are named at scatter time. */
                for (int64_t j = 0; j < n_aggs_real && n_key_cols + j < ncols; j++)
                    ray_table_set_col_name(result, n_key_cols + j, agg_names[j]);
                /* The computed key's derived name yields to an output alias
                 * that spells the same. */
                if (computed_single_key && ncols > 1) {
                    int64_t kname = ray_table_col_name(result, 0);
                    for (int64_t c = 1; c < ncols; c++)
                        if (ray_table_col_name(result, c) == kname) {
                            ray_table_set_col_name(result, 0, ray_sym_intern("key", 3));
                            break;
                        }
                }
            } else {
                /* Projection-only: columns are in dict order.  Rename each
                 * output column directly — a projection may have any number
                 * of columns, so no fixed-capacity buffer is used here. */
                int j = 0;
                for (int64_t i = 0; i + 1 < dict_n; i += 2) {
                    int64_t kid = dict_elems[i]->i64;
                    /* Every clause keyword has to be skipped here, not just
                     * most of them: this walk is positional, so a keyword it
                     * fails to skip consumes an output slot and every name
                     * after it lands on the wrong column.  nearest: was
                     * missing, and it is counted out where the outputs are
                     * counted, so a query naming it before its outputs
                     * labelled them one position early. */
                    if (kid == from_id || kid == where_id || kid == by_id ||
                        kid == take_id || kid == asc_id || kid == desc_id ||
                        kid == nearest_id) continue;
                    if (n_key_cols + j < ncols)
                        ray_table_set_col_name(result, n_key_cols + j, kid);
                    j++;
                }
            }
        }
    }

    /* Arith-of-aggs post-pass: evaluate each rewritten wrapper once over
     * the n_groups-row result with the hidden agg slots bound as locals,
     * append the outputs under their user names, then drop the hidden
     * columns.  (The hidden slots sit right after the real agg columns:
     * they were compiled after every dict agg, in extraction order.) */
    if (n_compound > 0 && by_expr && result && !RAY_IS_ERR(result)) {
        if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
        if (result && !RAY_IS_ERR(result) && result->type == RAY_TABLE) {
            int nk2 = 1;
            if (ray_is_vec(by_expr) && by_expr->type == RAY_SYM)
                nk2 = (int)ray_len(by_expr);
            int64_t hbase = (int64_t)nk2 + n_aggs_real;
            int64_t rnc = ray_table_ncols(result);
            ray_t* cc_hdr = NULL;
            ray_t** comp_cols = (ray_t**)scratch_calloc(&cc_hdr,
                    (size_t)(n_compound > 0 ? n_compound : 1) * sizeof(ray_t*));
            if (!comp_cols) {
                for (int ci = 0; ci < n_compound; ci++)
                    { ray_release(compound_rw[ci]); compound_rw[ci] = NULL; }
                ray_release(result); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
            }
            ray_t* cerr = NULL;
            bool layout_ok = hbase + n_hidden_aggs <= rnc;
            if (layout_ok) {
                if (ray_env_push_query_scope() != RAY_OK) {
                    cerr = ray_error("oom", NULL);
                } else {
                    for (int hi = 0; hi < n_hidden_aggs; hi++)
                        ray_env_set_local(hidden_agg_names[hi],
                                          ray_table_get_col_idx(result,
                                                                hbase + hi));
                    int64_t n_groups = ray_table_nrows(result);
                    for (int ci = 0; ci < n_compound && !cerr; ci++) {
                        ray_t* v = ray_eval(compound_rw[ci]);
                        if (!v || RAY_IS_ERR(v)) {
                            cerr = v ? v : ray_error("domain",
                                "select by: failed to evaluate aggregate expression");
                        } else if (ray_is_lazy(v) &&
                                   (!(v = ray_lazy_materialize(v)) || RAY_IS_ERR(v))) {
                            cerr = v ? v : ray_error("domain",
                                "select by: failed to evaluate aggregate expression");
                        } else if (!(ray_is_vec(v) || v->type == RAY_LIST) ||
                                   ray_len(v) != n_groups) {
                            ray_t* nm = ray_sym_str(compound_names[ci]);
                            cerr = ray_error("domain",
                                "select by: output `%.*s` did not evaluate to one value per group",
                                nm ? (int)ray_str_len(nm) : 1, nm ? ray_str_ptr(nm) : "?");
                            ray_release(v);
                        } else comp_cols[ci] = v;
                    }
                    ray_env_pop_scope();
                }
            }
            for (int ci = 0; ci < n_compound; ci++)
                { ray_release(compound_rw[ci]); compound_rw[ci] = NULL; }
            if (cerr) {
                for (int ci = 0; ci < n_compound; ci++)
                    if (comp_cols[ci]) ray_release(comp_cols[ci]);
                scratch_free(cc_hdr);
                ray_release(result);
                ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cerr;
            }
            if (layout_ok) {
                ray_t* nt = ray_table_new(rnc - n_hidden_aggs + n_compound);
                for (int64_t c = 0; c < rnc && nt && !RAY_IS_ERR(nt); c++) {
                    if (c >= hbase && c < hbase + n_hidden_aggs) continue;
                    nt = ray_table_add_col(nt, ray_table_col_name(result, c),
                                           ray_table_get_col_idx(result, c));
                }
                for (int ci = 0; ci < n_compound && nt && !RAY_IS_ERR(nt); ci++)
                    nt = ray_table_add_col(nt, compound_names[ci],
                                           comp_cols[ci]);
                if (nt && !RAY_IS_ERR(nt)) {
                    ray_release(result);
                    result = nt;
                } else {
                    /* An output that did not evaluate to one value per
                     * group (a scalar, say) cannot be appended: report it
                     * rather than hand back the hidden columns. */
                    cerr = nt ? nt : ray_error("oom", NULL);
                }
            }
            for (int ci = 0; ci < n_compound; ci++)
                if (comp_cols[ci]) ray_release(comp_cols[ci]);
            scratch_free(cc_hdr);
            n_compound = 0;
            if (cerr) {
                ray_release(result);
                ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return cerr;
            }
        }
    }

    /* Post-process: scatter non-agg expressions into LIST columns.
     * Must run BEFORE apply_sort_take so the sort clause can
     * reference non-agg output columns (and so the take clause
     * slices the fully-populated result).  apply_sort_take handles
     * LIST columns in the result table (same path used by the
     * eval_group branch).
     *
     * Reads group keys from the DAG result and builds row→group_id
     * against the original tbl. */
    if (n_nonaggs > 0 && by_expr && result && !RAY_IS_ERR(result)) {
        if (ray_is_lazy(result)) result = ray_lazy_materialize(result);
        if (result && !RAY_IS_ERR(result) && result->type == RAY_TABLE) {
            int64_t n_groups = ray_table_nrows(result);

            /* R8 fast path: every non-agg is a literal atom expression
             * with no column refs.  Skip the entire row→gid mapping —
             * each non-agg becomes a typed broadcast vec the same width
             * as n_groups, no idx_buf or per-group slicing required.
             *
             * Q35 = `{one: 1, c: count(URL), by: URL desc: c take: 10}`
             * is the canonical case: with all-literal nonaggs we go
             * directly to apply_sort_take and the top-K fast path
             * downstream of it. */
            if (n_groups > 0) {
                /* Pre-check ALL nonaggs first so we don't half-apply on
                 * an unhandled atom type and then have to roll back. */
                int all_broadcastable = 1;
                for (int64_t ni = 0; ni < n_nonaggs && all_broadcastable; ni++) {
                    if (!can_atom_broadcast(nonagg_exprs[ni]))
                        all_broadcastable = 0;
                }
                if (all_broadcastable) {
                    for (int64_t ni = 0; ni < n_nonaggs; ni++) {
                        ray_t* col = atom_broadcast_vec(nonagg_exprs[ni], n_groups);
                        if (!col) {
                            /* can_atom_broadcast vetted these — anything
                             * after that is an OOM in atom_broadcast_vec. */
                            ray_release(result); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                        }
                        result = ray_table_add_col(result, nonagg_names[ni], col);
                        ray_release(col);
                        if (RAY_IS_ERR(result)) {
                            ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                        }
                    }
                    goto nonagg_done;
                }
            }

            /* Resolve key sym — gated to single scalar key above. */
            int64_t ks = -1;
            if (by_expr->type == -RAY_SYM && !(by_expr->attrs & ATTR_QUOTED))
                ks = by_expr->i64;
            else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1)
                ks = ((int64_t*)ray_data(by_expr))[0];

            if (ks < 0) {
                ray_release(result); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: could not resolve scalar group key symbol");
            }

            ray_t* orig_key = ray_table_get_col(tbl, ks);
            ray_t* grp_key  = ray_table_get_col(result, ks);
            int64_t nrows = orig_key ? ray_parted_nrows(orig_key) : 0;

            if (!orig_key || !grp_key) {
                ray_release(result); ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "select by: group key column not found");
            }

            if (n_groups > 0 && nrows > 0) {
                ray_t* scan_key = orig_key;
                int scan_key_owned = 0;
                if (RAY_IS_PARTED(scan_key->type) || scan_key->type == RAY_MAPCOMMON) {
                    scan_key = query_materialize_parted_col(scan_key);
                    if (!scan_key || RAY_IS_ERR(scan_key)) {
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return scan_key ? scan_key : ray_error("oom", NULL);
                    }
                    scan_key_owned = 1;
                }
                #define RELEASE_SCAN_KEY() do {                              \
                    if (scan_key_owned && scan_key) ray_release(scan_key);    \
                } while (0)

                int8_t okt = scan_key->type;
                int8_t gkt = grp_key->type;
                if (RAY_IS_PARTED(okt)) okt = (int8_t)RAY_PARTED_BASETYPE(okt);
                if (RAY_IS_PARTED(gkt)) gkt = (int8_t)RAY_PARTED_BASETYPE(gkt);

                /* Type-aware key element reader.  Normalizes any
                 * comparable scalar key into an int64_t so linear
                 * scans can use equality.  For floats we bitcast so
                 * NaN and -0/+0 match the DAG's hash-equality.
                 * <winreg.h> (via <windows.h>) owns the name on Windows. */
                #undef KEY_READ
                #define KEY_READ(dst, vec, base_type, idx) do {                \
                    const void* _d = ray_data(vec);                            \
                    switch (base_type) {                                       \
                    case RAY_BOOL:                                             \
                    case RAY_U8:   (dst) = ((const uint8_t* )_d)[idx]; break;  \
                    case RAY_I16:  (dst) = ((const int16_t* )_d)[idx]; break;  \
                    case RAY_I32:  (dst) = ((const int32_t* )_d)[idx]; break;  \
                    case RAY_I64:  (dst) = ((const int64_t* )_d)[idx]; break;  \
                    case RAY_F32: { uint32_t _u;                               \
                        memcpy(&_u, &((const float*)_d)[idx], 4);              \
                        (dst) = (int64_t)_u; break; }                          \
                    case RAY_F64: { int64_t _u;                                \
                        memcpy(&_u, &((const double*)_d)[idx], 8);             \
                        (dst) = _u; break; }                                   \
                    case RAY_DATE: case RAY_TIME:                              \
                        (dst) = ((const int32_t*)_d)[idx]; break;              \
                    case RAY_TIMESTAMP:                                        \
                        (dst) = ((const int64_t*)_d)[idx]; break;              \
                    /* SYM: raw index — scan_key vs grp_key, where grp_key  \
                     * is gathered FROM scan_key (same domain), never a     \
                     * foreign-domain id. */                                   \
                    case RAY_SYM:                                              \
                        (dst) = ray_read_sym(_d, (idx), (base_type),           \
                                             (vec)->attrs); break;             \
                    default: {                                                 \
                        /* Unsupported key type: signal via sentinel so the    \
                         * caller's type-mismatch guard catches it.  Should    \
                         * not actually reach here because okt == gkt is       \
                         * checked above and only known types pass. */         \
                        (dst) = 0; break;                                      \
                    }                                                          \
                    }                                                          \
                } while (0)

                /* Whitelist of key types supported by KEY_READ.  Any
                 * other type (LIST, STR, GUID, unknown) must error out —
                 * otherwise KEY_READ silently returns 0 and collapses
                 * all rows into a single (wrong) group.  LIST/STR/GUID
                 * are already routed through use_eval_group earlier;
                 * this is the last-line defense for future additions. */
                int key_supported =
                    (okt == RAY_BOOL || okt == RAY_U8   ||
                     okt == RAY_I16  || okt == RAY_I32  || okt == RAY_I64 ||
                     okt == RAY_F32  || okt == RAY_F64  ||
                     okt == RAY_DATE || okt == RAY_TIME || okt == RAY_TIMESTAMP ||
                     okt == RAY_SYM  || okt == RAY_STR);
                if (!key_supported) {
                    RELEASE_SCAN_KEY();
                    ray_release(result); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("nyi", "non-agg scatter: unsupported group key type");
                }

                /* The DAG group result key column must have a base
                 * type comparable to the input.  If types differ
                 * unexpectedly, fall back to error rather than mis-
                 * compare. */
                if (okt != gkt) {
                    RELEASE_SCAN_KEY();
                    ray_release(result); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("type", "group key type mismatch");
                }

                /* Allocations — any failure errors out rather than
                 * silently returning partial results. */
                ray_t* gk_hdr  = ray_alloc((size_t)n_groups * sizeof(int64_t));
                ray_t* rg_hdr  = ray_alloc((size_t)nrows    * sizeof(int64_t));
                ray_t* cnt_hdr = ray_alloc((size_t)n_groups * sizeof(int64_t));
                ray_t* off_hdr = ray_alloc((size_t)n_groups * sizeof(int64_t));
                ray_t* pos_hdr = ray_alloc((size_t)n_groups * sizeof(int64_t));
                if (!gk_hdr || !rg_hdr || !cnt_hdr || !off_hdr || !pos_hdr) {
                    if (gk_hdr)  ray_free(gk_hdr);
                    if (rg_hdr)  ray_free(rg_hdr);
                    if (cnt_hdr) ray_free(cnt_hdr);
                    if (off_hdr) ray_free(off_hdr);
                    if (pos_hdr) ray_free(pos_hdr);
                    RELEASE_SCAN_KEY();
                    ray_release(result); ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                }
                int64_t* gk      = (int64_t*)ray_data(gk_hdr);
                int64_t* row_gid = (int64_t*)ray_data(rg_hdr);
                int64_t* grp_cnt = (int64_t*)ray_data(cnt_hdr);
                int64_t* offsets = (int64_t*)ray_data(off_hdr);
                int64_t* pos     = (int64_t*)ray_data(pos_hdr);

                /* Copy group key values from the (possibly sliced) result.
                 * STR keys resolve via descriptors+pool below, not gk[]. */
                if (okt != RAY_STR)
                    for (int64_t gi = 0; gi < n_groups; gi++)
                        KEY_READ(gk[gi], grp_key, gkt, gi);

                /* Build row→group_id map.  Rows whose key isn't in the
                 * surviving group set get row_gid = -1 and are skipped.
                 *
                 * For high group cardinality (n_groups large), the naive
                 * O(nrows * n_groups) double loop dominated runtime —
                 * 5M * 730K ≈ 4T comparisons.  Build a value→gid hash
                 * instead so each row is one O(1) probe. */
                int rgid_did_mask = 0;
                if (okt == RAY_STR) {
                    /* Dict-code row_gid fast path: when the group key is a
                     * dict-encoded STR column, exec_group already grouped on its
                     * int32 codes and stashed the per-result-group codes.  Build
                     * row_gid by remapping each row's dict code to its result
                     * group index (a parallel int gather) instead of re-hashing
                     * 1.37M strings.  Falls through to the string probe when the
                     * key isn't dict-encoded or the stash doesn't match. */
                    if (ray_index_kind(scan_key) == RAY_IDX_DICT) {
                        ray_dict_cd_t cd = ray_dict_cd_get();
                        if (cd.valid && cd.key_sym == ks && cd.n_groups == n_groups
                            && cd.n_distinct > 0
                            && ray_index_payload(scan_key->index)->u.dict.codes) {
                            const int32_t* scan_codes = (const int32_t*)ray_data(
                                ray_index_payload(scan_key->index)->u.dict.codes);
                            ray_t* c2g_hdr = NULL;
                            int32_t* code_to_gid = (int32_t*)scratch_alloc(&c2g_hdr,
                                (size_t)cd.n_distinct * sizeof(int32_t));
                            if (code_to_gid && scan_codes) {
                                for (int64_t i = 0; i < cd.n_distinct; i++) code_to_gid[i] = -1;
                                for (int64_t gi = 0; gi < n_groups; gi++) {
                                    int32_t cc = cd.result_codes[gi];
                                    if (cc >= 0 && cc < cd.n_distinct) code_to_gid[cc] = (int32_t)gi;
                                }
                                ray_pool_t* dg_pool = ray_pool_get();
                                if (dg_pool && nrows >= 200000 && ray_pool_total_workers(dg_pool) >= 2) {
                                    dictgid_ctx_t dctx = { scan_codes, code_to_gid, cd.n_distinct, row_gid };
                                    ray_pool_dispatch(dg_pool, dictgid_fn, &dctx, nrows);
                                } else {
                                    for (int64_t r = 0; r < nrows; r++) {
                                        int32_t cc = scan_codes[r];
                                        row_gid[r] = (cc >= 0 && cc < cd.n_distinct) ? code_to_gid[cc] : -1;
                                    }
                                }
                                scratch_free(c2g_hdr);
                                ray_dict_cd_clear();
                                goto str_rgid_done;
                            }
                            if (c2g_hdr) scratch_free(c2g_hdr);
                        }
                    }
                    /* STR keys: hash the group key descriptors → gid, then
                     * probe each input row's descriptor (SSO-aware via the
                     * string pool; inline ≤12 B keys skip the pool).  grp_key
                     * and scan_key carry their own pools (grp_key shares the
                     * source pool, but resolve each independently to be safe). */
                    const ray_str_t* gdesc = (const ray_str_t*)ray_data(grp_key);
                    const char* gpool = grp_key->str_pool
                        ? (const char*)ray_data(grp_key->str_pool) : NULL;
                    const ray_str_t* sdesc = (const ray_str_t*)ray_data(scan_key);
                    const char* spool = scan_key->str_pool
                        ? (const char*)ray_data(scan_key->str_pool) : NULL;
                    uint64_t cap = (uint64_t)n_groups * 2; if (cap < 32) cap = 32;
                    uint64_t c = 1; while (c && c < cap) c <<= 1;
                    if (!c) {
                        ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                        ray_free(off_hdr); ray_free(pos_hdr);
                        RELEASE_SCAN_KEY();
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    cap = c;
                    uint64_t mask = cap - 1;
                    ray_t* slot_hdr = NULL;
                    int32_t* slot_gid_p1 = (int32_t*)scratch_calloc(&slot_hdr,
                        (size_t)cap * sizeof(int32_t));
                    if (!slot_gid_p1) {
                        if (slot_hdr) scratch_free(slot_hdr);
                        ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                        ray_free(off_hdr); ray_free(pos_hdr);
                        RELEASE_SCAN_KEY();
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        uint64_t h = ray_str_t_hash(&gdesc[gi], gpool);
                        uint64_t s = h & mask;
                        while (slot_gid_p1[s] != 0) s = (s + 1) & mask;
                        slot_gid_p1[s] = (int32_t)(gi + 1);
                    }
                    /* Probe each row to assign its gid.  Parallelise when the
                     * input is large enough to amortise dispatch — the slot
                     * table is read-only and row_gid writes are disjoint, so
                     * workers need no synchronisation (mirrors the int path). */
                    ray_pool_t* str_pool = ray_pool_get();
                    if (str_pool && nrows >= 200000 &&
                        ray_pool_total_workers(str_pool) >= 2) {
                        rgid_probe_str_ctx_t spctx = {
                            .slot_gid_p1 = slot_gid_p1,
                            .mask        = mask,
                            .gdesc       = gdesc,
                            .gpool       = gpool,
                            .sdesc       = sdesc,
                            .spool       = spool,
                            .row_gid     = row_gid,
                        };
                        ray_pool_dispatch(str_pool, rgid_probe_str_fn, &spctx, nrows);
                    } else {
                        for (int64_t r = 0; r < nrows; r++) {
                            uint64_t h = ray_str_t_hash(&sdesc[r], spool);
                            uint64_t s = h & mask;
                            int64_t found = -1;
                            for (;;) {
                                int32_t g_p1 = slot_gid_p1[s];
                                if (g_p1 == 0) break;
                                int32_t g = g_p1 - 1;
                                if (ray_str_t_eq(&gdesc[g], gpool, &sdesc[r], spool)) {
                                    found = g; break;
                                }
                                s = (s + 1) & mask;
                            }
                            row_gid[r] = found;
                        }
                    }
                    scratch_free(slot_hdr);
                    str_rgid_done: ;
                } else {
                    /* Capacity: 2 * n_groups rounded up to power of 2.
                     * Slot stores gid+1 (0 = empty) and the int64 key. */
                    uint64_t cap = (uint64_t)n_groups * 2;
                    if (cap < 32) cap = 32;
                    uint64_t c = 1;
                    while (c && c < cap) c <<= 1;
                    if (!c) {
                        ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                        ray_free(off_hdr); ray_free(pos_hdr);
                        RELEASE_SCAN_KEY();
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    cap = c;
                    uint64_t mask = cap - 1;
                    ray_t* gk_keys_hdr = NULL;
                    ray_t* gk_idx_hdr  = NULL;
                    int64_t* hk_keys = (int64_t*)scratch_alloc(&gk_keys_hdr,
                        (size_t)cap * sizeof(int64_t));
                    int32_t* hk_gid_p1 = (int32_t*)scratch_calloc(&gk_idx_hdr,
                        (size_t)cap * sizeof(int32_t));
                    if (!hk_keys || !hk_gid_p1) {
                        if (gk_keys_hdr) scratch_free(gk_keys_hdr);
                        if (gk_idx_hdr)  scratch_free(gk_idx_hdr);
                        ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                        ray_free(off_hdr); ray_free(pos_hdr);
                        RELEASE_SCAN_KEY();
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }

                    /* If n_groups exceeds the int32 sentinel range we'd
                     * lose distinct gids — fall back to the int64 store
                     * (rare: n_groups > ~2.1 B).  Otherwise i32+1 fits. */
                    int use_i64_gid = (n_groups >= ((int64_t)1 << 31) - 1);
                    ray_t* gk64_hdr = NULL;
                    int64_t* hk_gid64 = NULL;
                    if (use_i64_gid) {
                        hk_gid64 = (int64_t*)scratch_calloc(&gk64_hdr,
                            (size_t)cap * sizeof(int64_t));
                        if (!hk_gid64) {
                            scratch_free(gk_keys_hdr); scratch_free(gk_idx_hdr);
                            ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                            ray_free(off_hdr); ray_free(pos_hdr);
                            RELEASE_SCAN_KEY();
                            ray_release(result); ray_release(tbl);
                            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                        }
                    }

                    /* Insert (gk[gi] -> gi) into the hash. */
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        int64_t k = gk[gi];
                        uint64_t h = (uint64_t)k * 0x9E3779B97F4A7C15ULL;
                        h ^= h >> 33;
                        uint64_t s = h & mask;
                        for (;;) {
                            int64_t cur_p1 = use_i64_gid ? hk_gid64[s]
                                                         : (int64_t)hk_gid_p1[s];
                            if (cur_p1 == 0) {
                                if (use_i64_gid) hk_gid64[s] = gi + 1;
                                else hk_gid_p1[s] = (int32_t)(gi + 1);
                                hk_keys[s] = k;
                                break;
                            }
                            if (hk_keys[s] == k) break; /* dup gk — keep first */
                            s = (s + 1) & mask;
                        }
                    }

                    /* Probe each row to assign its gid.  Parallelise when
                     * the input is large enough to amortise dispatch
                     * overhead — the hash is read-only at this point so
                     * workers don't need to synchronise. */
                    ray_pool_t* pool = ray_pool_get();
                    if (pool && nrows >= 200000 && ray_pool_total_workers(pool) >= 2) {
                        rgid_probe_ctx_t pctx = {
                            .hk_keys       = hk_keys,
                            .hk_gid_p1     = use_i64_gid ? NULL : hk_gid_p1,
                            .hk_gid64      = use_i64_gid ? hk_gid64 : NULL,
                            .mask          = mask,
                            .orig_key_data = ray_data(scan_key),
                            .okt           = okt,
                            .okt_attrs     = scan_key->attrs,
                            .row_gid       = row_gid,
                            .selection     = saved_selection,
                            .sel_flg       = NULL,
                            .sel_offs      = NULL,
                            .sel_idx       = NULL,
                            .sel_n_segs    = 0,
                        };
                        if (saved_selection) {
                            ray_rowsel_t* sm = ray_rowsel_meta(saved_selection);
                            pctx.sel_flg    = ray_rowsel_flags(saved_selection);
                            pctx.sel_offs   = ray_rowsel_offsets(saved_selection);
                            pctx.sel_idx    = ray_rowsel_idx(saved_selection);
                            pctx.sel_n_segs = sm->n_segs;
                        }
                        ray_pool_dispatch(pool, rgid_probe_fn, &pctx, nrows);
                        rgid_did_mask = (saved_selection != NULL);
                    } else {
                        for (int64_t r = 0; r < nrows; r++) {
                            int64_t rv;
                            KEY_READ(rv, scan_key, okt, r);
                            uint64_t h = (uint64_t)rv * 0x9E3779B97F4A7C15ULL;
                            h ^= h >> 33;
                            uint64_t s = h & mask;
                            int64_t found = -1;
                            for (;;) {
                                int64_t cur_p1 = use_i64_gid ? hk_gid64[s]
                                                             : (int64_t)hk_gid_p1[s];
                                if (cur_p1 == 0) break;
                                if (hk_keys[s] == rv) { found = cur_p1 - 1; break; }
                                s = (s + 1) & mask;
                            }
                            row_gid[r] = found;
                        }
                    }

                    scratch_free(gk_keys_hdr);
                    scratch_free(gk_idx_hdr);
                    if (gk64_hdr) scratch_free(gk64_hdr);
                }
                #undef KEY_READ

                /* When path A was taken (no materialisation), the probe
                 * above looked up gids for every row in the original
                 * (unfiltered) table — including rows that the WHERE
                 * clause filtered out.  Mask those rows to -1 here so
                 * downstream count_distinct (and grp_cnt) only count
                 * the surviving rows.  Walks the morsel-segmented
                 * rowsel directly to avoid building a full bitmap.
                 *
                 * When the parallel rgid_probe path was taken with the
                 * selection threaded through (rgid_did_mask), this
                 * mask was already applied per-segment inside the probe
                 * worker — skip the redundant serial walk. */
                if (saved_selection && !rgid_did_mask) {
                    ray_rowsel_t*   sm   = ray_rowsel_meta(saved_selection);
                    const uint8_t*  flg  = ray_rowsel_flags(saved_selection);
                    const uint32_t* offs = ray_rowsel_offsets(saved_selection);
                    const uint16_t* lidx = ray_rowsel_idx(saved_selection);
                    for (uint32_t seg = 0; seg < sm->n_segs; seg++) {
                        int64_t s_lo = (int64_t)seg * RAY_MORSEL_ELEMS;
                        int64_t s_hi = s_lo + RAY_MORSEL_ELEMS;
                        if (s_hi > nrows) s_hi = nrows;
                        uint8_t f = flg[seg];
                        if (f == RAY_SEL_NONE) {
                            for (int64_t r = s_lo; r < s_hi; r++) row_gid[r] = -1;
                        } else if (f == RAY_SEL_ALL) {
                            /* every row in this segment passed — leave gid */
                        } else { /* RAY_SEL_MIX */
                            uint8_t in_seg[RAY_MORSEL_ELEMS / 8] = {0};
                            uint32_t off  = offs[seg];
                            uint32_t cnt  = offs[seg + 1] - off;
                            for (uint32_t i = 0; i < cnt; i++) {
                                uint16_t loc = lidx[off + i];
                                in_seg[loc >> 3] |= (uint8_t)(1u << (loc & 7));
                            }
                            for (int64_t r = s_lo; r < s_hi; r++) {
                                uint16_t loc = (uint16_t)(r - s_lo);
                                if (!(in_seg[loc >> 3] & (1u << (loc & 7))))
                                    row_gid[r] = -1;
                            }
                        }
                    }
                }

                /* Decide whether the per-group-slice bookkeeping
                 * (grp_cnt / offsets / pos / idx_buf) is needed.  It
                 * powers count_distinct_per_group_buf, the streaming
                 * aggr-unary path, nonagg_eval_per_group_buf, and the
                 * full-table-eval+gather path.  When ALL non-aggs are
                 * `count(distinct col_ref)` AND the n_groups gate
                 * routes them to the global-hash kernel, none of those
                 * consumers run — and building the slice index is dead
                 * weight (~15-20 ms on Q14).
                 *
                 * The global-hash path is taken when:
                 *   - the non-agg matches `match_count_distinct`,
                 *   - the inner expression is a column ref (SYM atom
                 *     with NAME attr), and
                 *   - n_groups > 50 000 (the per-group-slice cross-
                 *     over from the threshold dispatch above).
                 *
                 * If any non-agg falls outside that, we still need the
                 * index. */
                /* Decide whether we need to materialise the per-group
                 * idx_buf scatter.  Skipped only when EVERY non-agg
                 * is count(distinct col_ref) routed to the global-hash
                 * kernel (n_groups > 50 000) — that path walks row_gid
                 * directly and never reads the slice index.  Any other
                 * non-agg shape — including count(distinct) on small
                 * group counts — falls into count_distinct_per_group_buf
                 * which requires idx_buf+offsets+grp_cnt. */
                int needs_slice_idx = 0;
                for (int64_t ni = 0; ni < n_nonaggs && !needs_slice_idx; ni++) {
                    ray_t* cd_inner = match_count_distinct(nonagg_exprs[ni]);
                    int simple_cd_global = (cd_inner &&
                                            cd_inner->type == -RAY_SYM &&
                                            !(cd_inner->attrs & ATTR_QUOTED) &&
                                            n_groups > 50000);
                    if (simple_cd_global) {
                        ray_t* source = ray_table_get_col(tbl, cd_inner->i64);
                        int8_t type = source ? source->type : 0;
                        simple_cd_global = type == RAY_BOOL || type == RAY_U8 ||
                            type == RAY_I16 || type == RAY_I32 || type == RAY_I64 ||
                            type == RAY_F32 || type == RAY_F64 || type == RAY_DATE ||
                            type == RAY_TIME || type == RAY_TIMESTAMP || type == RAY_SYM;
                    }
                    if (!simple_cd_global) needs_slice_idx = 1;
                }

                int64_t* idx_buf = NULL;
                ray_t*   idx_hdr = NULL;
                if (needs_slice_idx) {
                    if (!slice_build(row_gid, nrows, saved_selection, n_groups,
                                     grp_cnt, offsets, pos, &idx_hdr)) {
                        ray_free(gk_hdr); ray_free(rg_hdr);
                        ray_free(cnt_hdr); ray_free(off_hdr);
                        ray_free(pos_hdr);
                        RELEASE_SCAN_KEY();
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("oom", NULL);
                    }
                    idx_buf = (int64_t*)ray_data(idx_hdr);
                }

                ray_t* scatter_err = NULL;
                for (int64_t ni = 0; ni < n_nonaggs && !scatter_err; ni++) {
                    /* Per-group count(distinct) — dispatch directly to
                     * exec_count_distinct on each group's slice using
                     * the same idx_buf+offsets+grp_cnt layout the
                     * streaming-AGG branch uses.
                     *
                     * High-cardinality grouping: try the single-pass
                     * global-hash kernel first.  Falls back to the
                     * per-group slice path on type miss / error. */
                    ray_t* cd_inner = match_count_distinct(nonagg_exprs[ni]);
                    if (cd_inner) {
                        ray_t* col = NULL;
                        /* Resolve the inner column for the global-hash
                         * fast path.  Direct column refs hit the path;
                         * computed expressions use the per-group fallback. */
                        ray_t* src_for_global = NULL;
                        int    src_owned = 0;
                        if (cd_inner->type == -RAY_SYM &&
                            !(cd_inner->attrs & ATTR_QUOTED)) {
                            src_for_global = ray_table_get_col(tbl, cd_inner->i64);
                        }
                        if (src_for_global && n_groups > 50000) {
                            if (RAY_IS_PARTED(src_for_global->type) ||
                                src_for_global->type == RAY_MAPCOMMON) {
                                ray_t* flat = query_materialize_parted_col(src_for_global);
                                if (!flat || RAY_IS_ERR(flat)) {
                                    col = flat ? flat : ray_error("oom", NULL);
                                    src_for_global = NULL;
                                } else {
                                    src_for_global = flat;
                                    src_owned = 1;
                                }
                            }
                        }
                        if (src_for_global) {
                            /* COUNT(DISTINCT) is exact per SQL/DSL
                             * semantics — the streaming HLL fast path
                             * here silently returned an approximate
                             * result and was removed.  Exact dedup
                             * follows below. */
                            /* Path selection: global-hash kernel scales
                             * with n_rows (per-row probe of one shared
                             * hash table); per-group-slice scales with
                             * n_groups (per-group setup + small dedup).
                             * Empirically the cross-over is around 50 K
                             * groups on the local hardware.  Partitioned
                             * high-cardinality columns are flattened above,
                             * so keep them on the single-pass kernel and
                             * avoid slicing through the partition layout
                             * again. */
                            if (!col) {
                                if (n_groups <= 50000) {
                                    col = count_distinct_per_group_buf(
                                        cd_inner, tbl, idx_buf, offsets, grp_cnt, n_groups);
                                } else {
                                    col = ray_count_distinct_per_group(
                                        src_for_global, row_gid, nrows, n_groups);
                                }
                            }
                            /* col == NULL → unsupported type, fall through. */
                        }
                        if (src_owned && src_for_global) ray_release(src_for_global);
                        if (!col) {
                            col = count_distinct_per_group_buf(
                                cd_inner, tbl, idx_buf, offsets, grp_cnt, n_groups);
                        }
                        if (RAY_IS_ERR(col)) { scatter_err = col; break; }
                        result = ray_table_add_col(result, nonagg_names[ni], col);
                        ray_release(col);
                        if (RAY_IS_ERR(result)) {
                            scatter_err = result; result = NULL; break;
                        }
                        continue;
                    }

                    /* Streaming-style fast path for `(aggr_fn col_or_expr)`
                     * where aggr_fn is RAY_FN_AGGR + RAY_UNARY (sum/avg/...,
                     * med/dev/var/stddev/...).  Bypasses the full-table eval
                     * + non-row-aligned fallback by slicing the source per
                     * group and calling the unary fn directly into a typed
                     * vec.  Equivalent perf-class to the streaming AGG path
                     * the eval-fallback uses for the same shapes. */
                    if (is_streaming_aggr_unary_call(nonagg_exprs[ni])) {
                        ray_t* col = NULL;
                        /* `(med col)` fast path — bucket-scatter values
                         * into a reused scratch and quickselect, skipping
                         * the per-group ray_at_fn + ray_med_fn scratch
                         * allocations.  NULL → unsupported input type
                         * (LIST/STR/etc); fall back to the generic
                         * aggr_unary_per_group_buf path below. */
                        if (is_med_call(nonagg_exprs[ni])) {
                            col = aggr_med_per_group_buf(nonagg_exprs[ni], tbl,
                                idx_buf, offsets, grp_cnt, n_groups);
                        }
                        if (!col) {
                            col = aggr_unary_per_group_buf(
                                nonagg_exprs[ni], tbl,
                                idx_buf, offsets, grp_cnt, n_groups);
                        }
                        if (RAY_IS_ERR(col)) { scatter_err = col; break; }
                        result = ray_table_add_col(result, nonagg_names[ni], col);
                        ray_release(col);
                        if (RAY_IS_ERR(result)) {
                            scatter_err = result; result = NULL; break;
                        }
                        continue;
                    }

                    /* Outer-agg or arith-of-aggs: must evaluate per group
                     * — a single full-table eval collapses every nested
                     * agg (max/min/sum/...) globally and broadcasts the
                     * scalar across all groups. */
                    if (is_agg_expr(nonagg_exprs[ni]) ||
                        expr_contains_agg(nonagg_exprs[ni])) {
                        ray_t* per_group = nonagg_eval_per_group_buf(
                            nonagg_exprs[ni], tbl, idx_buf, offsets, grp_cnt, n_groups);
                        if (RAY_IS_ERR(per_group)) {
                            scatter_err = per_group; break;
                        }
                        result = ray_table_add_col(result, nonagg_names[ni], per_group);
                        ray_release(per_group);
                        if (RAY_IS_ERR(result)) {
                            scatter_err = result; result = NULL; break;
                        }
                        continue;
                    }

                    if (ray_env_push_query_scope() != RAY_OK) {
                        scatter_err = ray_error("oom", NULL); break;
                    }
                    ray_t* _aqt = bind_all_columns(tbl);
                    ray_t* full_val = ray_eval(nonagg_exprs[ni]);
                    g_active_query_table = _aqt;
                    ray_env_pop_scope();
                    if (!full_val || RAY_IS_ERR(full_val)) {
                        scatter_err = full_val ? full_val : ray_error("domain", "select by: failed to evaluate non-aggregate column expression");
                        break;
                    }

                    ray_t* list_col = ray_alloc(n_groups * sizeof(ray_t*));
                    if (!list_col) {
                        ray_release(full_val);
                        scatter_err = ray_error("oom", NULL); break;
                    }
                    list_col->type = RAY_LIST;
                    /* Track filled length incrementally: ray_release of
                     * a RAY_LIST walks exactly v->len children, so
                     * keeping len in sync with the number of initialized
                     * slots lets error paths free without touching
                     * uninitialized memory — and avoids a memset. */
                    list_col->len = 0;
                    ray_t** list_out = (ray_t**)ray_data(list_col);

                    /* Decide per-group disposition of full_val:
                     *   - expression references a column → result must
                     *     be row-aligned; otherwise that's a bug and
                     *     we error out rather than silently broadcast.
                     *   - constant expression (no column refs) →
                     *     broadcast the value into every group cell. */
                    int refs_column = expr_refs_row_column(nonagg_exprs[ni], tbl);
                    int is_indexable =
                        ray_is_vec(full_val) || full_val->type == RAY_LIST;
                    int full_is_row_aligned =
                        is_indexable && full_val->len == nrows;

                    if (refs_column && !full_is_row_aligned) {
                        /* Non-streaming fallback: the expression didn't
                         * produce a row-aligned full-table result (e.g. a
                         * user lambda collapsed a vector to a scalar), so
                         * collect per-group and post-apply.  Cells can be
                         * any shape; homogeneous-scalar cells collapse to
                         * a typed vec. */
                        ray_release(full_val);
                        ray_release(list_col);  /* len=0, walks nothing */
                        ray_t* per_group = nonagg_eval_per_group_buf(
                            nonagg_exprs[ni], tbl, idx_buf, offsets, grp_cnt, n_groups);
                        if (RAY_IS_ERR(per_group)) {
                            scatter_err = per_group; break;
                        }
                        /* core produces typed vec or list as appropriate */
                        result = ray_table_add_col(result, nonagg_names[ni], per_group);
                        ray_release(per_group);
                        if (RAY_IS_ERR(result)) {
                            scatter_err = result; result = NULL; break;
                        }
                        continue;
                    }

                    /* R8 fallback: a non-literal expression that
                     * eval-collapses to an atom (constant within scope
                     * but not a parser-direct literal) takes the existing
                     * per-cell LIST broadcast.  The all-literal fast path
                     * at the top of the n_nonaggs block already handles
                     * the parser-literal case for Q35-shaped queries. */

                    int gather_ok = 1;
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        ray_t* cell;
                        if (full_is_row_aligned) {
                            cell = gather_by_idx(full_val,
                                &idx_buf[offsets[gi]], grp_cnt[gi]);
                            if (!cell || RAY_IS_ERR(cell)) {
                                gather_ok = 0;
                                break;
                            }
                        } else {
                            /* Constant (no column refs): broadcast */
                            ray_retain(full_val);
                            cell = full_val;
                        }
                        list_out[gi] = cell;
                        list_col->len = gi + 1;  /* commit slot */
                    }
                    ray_release(full_val);

                    if (!gather_ok) {
                        ray_release(list_col);  /* releases exactly len filled slots */
                        scatter_err = ray_error("oom", NULL); break;
                    }

                    result = ray_table_add_col(result, nonagg_names[ni], list_col);
                    ray_release(list_col);
                    if (RAY_IS_ERR(result)) {
                        scatter_err = result; result = NULL; break;
                    }
                }

                ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                ray_free(off_hdr); ray_free(pos_hdr);
                if (idx_hdr) ray_free(idx_hdr);
                RELEASE_SCAN_KEY();

                if (scatter_err) {
                    if (result) ray_release(result);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return scatter_err;
                }
                #undef RELEASE_SCAN_KEY
            } else {
                /* Empty grouped aggregates retain their result type;
                 * ordinary row expressions produce per-group LIST cells. */
                for (int64_t ni = 0; ni < n_nonaggs; ni++) {
                    ray_t* expr = nonagg_exprs[ni];
                    ray_t* empty_list = match_count_distinct(expr) ? ray_vec_new(RAY_I64, 0)
                        : is_streaming_aggr_unary_call(expr) ? aggr_unary_per_group_buf(expr, tbl, NULL, NULL, NULL, 0)
                        : can_atom_broadcast(expr) ? atom_broadcast_vec(expr, 0) : ray_list_new(0);
                    if (!empty_list || RAY_IS_ERR(empty_list)) {
                        ray_release(result); ray_release(tbl);
                        scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return empty_list ? empty_list : ray_error("oom", NULL);
                    }
                    result = ray_table_add_col(result, nonagg_names[ni], empty_list);
                    ray_release(empty_list);
                    if (RAY_IS_ERR(result)) { ray_release(tbl); scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result; }
                }
            }
        nonagg_done: ;  /* R8 fast-path target; nothing else to do here */
        }
    }

    if (n_dep_keys > 0 && result && !RAY_IS_ERR(result) &&
        result->type == RAY_TABLE) {
        if (ray_is_lazy(result))
            result = ray_lazy_materialize(result);
        if (result && RAY_IS_ERR(result)) {
            ray_release(tbl);
            scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
        }
        if (result && result->type == RAY_TABLE) {
            ray_t* base_col = ray_table_get_col(result, dep_key_base_sym);
            if (!base_col || !key_type_i64_projectable(base_col->type)) {
                ray_release(result);
                ray_release(tbl);
                scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return ray_error("domain", "dependent group key base missing");
            }
            int64_t n_groups = ray_table_nrows(result);
            for (int64_t dk = 0; dk < n_dep_keys; dk++) {
                ray_t* col = ray_vec_new(RAY_I64, n_groups);
                if (!col || RAY_IS_ERR(col)) {
                    ray_release(result);
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return col ? col : ray_error("oom", NULL);
                }
                col->len = n_groups;
                int64_t* out = (int64_t*)ray_data(col);
                int64_t bias = dep_key_biases[dk];
                for (int64_t i = 0; i < n_groups; i++)
                    out[i] = key_col_read_i64(base_col, i) + bias;
                result = ray_table_add_col(result, dep_key_names[dk], col);
                ray_release(col);
                if (RAY_IS_ERR(result)) {
                    ray_release(tbl);
                    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
                }
            }
        }
    }

    ray_release(tbl);

    /* Post-process: apply sort/take for group-by queries.  Runs
     * last so non-agg LIST columns are already in the result,
     * allowing sort clauses to reference non-agg output columns. */
    if (by_expr && (has_sort || take_expr))
        result = apply_sort_take(result, dict_elems, dict_n, asc_id, desc_id,
                                 take_id,
                                 take_pre.kind ? &take_pre : NULL);

    if (by_sym_vec_owned) ray_release(by_sym_vec_owned);
    if (saved_selection) ray_release(saved_selection);

    scratch_free(sel_slots_hdr); DICT_VIEW_CLOSE(dv); return result;
}

/* (xbar col bucket) — time/value bucketing: floor(col/bucket)*bucket */
/* Parallel inner loops for ray_xbar_fn fast path.  Dispatch one task per
 * morsel range so 5M-row temporal columns scale across the worker pool
 * (Q43's xbar was ~6ms serial, ~0.5ms with 28 workers). */
typedef struct {
    int8_t out_type;
    const void* in;
    void* out;
    int64_t b;
    int pow2;
} xbar_par_ctx_t;

static void xbar_par_fn(void* vctx, uint32_t worker_id,
                        int64_t start, int64_t end) {
    (void)worker_id;
    xbar_par_ctx_t* c = (xbar_par_ctx_t*)vctx;
    int64_t b = c->b;
    if (c->out_type == RAY_I64 || c->out_type == RAY_TIMESTAMP) {
        const int64_t* in = (const int64_t*)c->in;
        int64_t* o = (int64_t*)c->out;
        if (c->pow2) {
            int64_t mask = ~(b - 1);
            for (int64_t i = start; i < end; i++) o[i] = in[i] & mask;
        } else {
            for (int64_t i = start; i < end; i++) {
                int64_t a = in[i];
                int64_t q = a / b;
                if ((a ^ b) < 0 && q * b != a) q--;
                /* Unsigned wrap: only the null sentinel (INT64_MIN) rounds
                 * below INT64_MIN, and the caller rewrites it as null. */
                o[i] = (int64_t)((uint64_t)q * (uint64_t)b);
            }
        }
    } else if (c->out_type == RAY_I32 || c->out_type == RAY_DATE ||
               c->out_type == RAY_TIME) {
        const int32_t* in = (const int32_t*)c->in;
        int32_t* o = (int32_t*)c->out;
        int32_t b32 = (int32_t)b;
        if (c->pow2) {
            int32_t mask = (int32_t)~((uint32_t)b32 - 1);
            for (int64_t i = start; i < end; i++) o[i] = in[i] & mask;
        } else {
            for (int64_t i = start; i < end; i++) {
                int32_t a = in[i];
                int64_t q = (int64_t)a / b32;
                if ((a ^ b32) < 0 && q * b32 != a) q--;
                o[i] = (int32_t)(q * b32);
            }
        }
    } else { /* RAY_I16 */
        const int16_t* in = (const int16_t*)c->in;
        int16_t* o = (int16_t*)c->out;
        int16_t b16 = (int16_t)b;
        for (int64_t i = start; i < end; i++) {
            int16_t a = in[i];
            int16_t q = a / b16;
            if ((a ^ b16) < 0 && q * b16 != a) q--;
            o[i] = q * b16;
        }
    }
}

static bool xbar_bucket_fits_vec_type(int8_t out_type, int64_t b) {
    if (out_type == RAY_I32 || out_type == RAY_DATE || out_type == RAY_TIME)
        return b >= INT32_MIN && b <= INT32_MAX;
    if (out_type == RAY_I16)
        return b >= INT16_MIN && b <= INT16_MAX;
    return true;
}

ray_t* ray_xbar_fn(ray_t* col, ray_t* bucket) {
    /* Vectorised fast path for `(xbar VEC scalar_int)` on integer or
     * temporal columns.  The generic atomic_map_binary path was
     * allocating one ray_t* atom per row and calling ray_xbar_fn
     * recursively — at 5M rows this dominates (≥100 ms).  A direct
     * tight loop computes floor-div + multiply per element with no
     * allocations.  When the bucket is a power of two we lower the
     * divide further to mask + arithmetic.  Parallelised across the
     * worker pool for large columns.
     *
     * Short-circuited only when both bucket and col are well-typed;
     * everything else falls through to the recursive
     * atomic_map_binary path. */
    if (col && ray_is_vec(col) && bucket && ray_is_atom(bucket) &&
        (bucket->type == -RAY_I64 || bucket->type == -RAY_I32 ||
         bucket->type == -RAY_I16) &&
        (col->type == RAY_I64 || col->type == RAY_I32 ||
         col->type == RAY_I16 || col->type == RAY_TIMESTAMP ||
         col->type == RAY_DATE || col->type == RAY_TIME) &&
        !RAY_ATOM_IS_NULL(bucket)) {
        int64_t b = bucket->i64;
        if (b == 0) return ray_error("domain", "xbar: bucket size must be non-zero");
        if (!xbar_bucket_fits_vec_type(col->type, b))
            return atomic_map_binary(ray_xbar_fn, col, bucket);
        int64_t n = col->len;
        ray_t* out = ray_vec_new(col->type, n);
        if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
        out->len = n;

        int8_t out_type = col->type;
        int pow2 = 0;
        if (out_type == RAY_I64 || out_type == RAY_TIMESTAMP) {
            pow2 = (b > 0 && (b & (b - 1)) == 0);
        } else if (out_type == RAY_I32 || out_type == RAY_DATE || out_type == RAY_TIME) {
            int32_t b32 = (int32_t)b;
            pow2 = (b32 > 0 && ((uint32_t)b32 & ((uint32_t)b32 - 1)) == 0);
        }

        ray_pool_t* pool = ray_pool_get();
        if (pool && n >= 200000 && ray_pool_total_workers(pool) >= 2) {
            xbar_par_ctx_t ctx = {
                .out_type = out_type,
                .in       = ray_data(col),
                .out      = ray_data(out),
                .b        = b,
                .pow2     = pow2,
            };
            ray_pool_dispatch(pool, xbar_par_fn, &ctx, n);
        } else {
            xbar_par_ctx_t ctx = {
                .out_type = out_type,
                .in       = ray_data(col),
                .out      = ray_data(out),
                .b        = b,
                .pow2     = pow2,
            };
            xbar_par_fn(&ctx, 0, 0, n);
        }

        /* Propagate nulls if present.  Walk per-element via
         * ray_vec_is_null (sentinel-based). */
        if (ray_vec_may_have_nulls(col)) {
            for (int64_t i = 0; i < n; i++)
                if (ray_vec_is_null(col, i))
                    ray_vec_set_null(out, i, true);
        }
        return out;
    }

    /* Recursive unwrap for nested collections (list of vectors) */
    if (is_collection(col) || is_collection(bucket))
        return atomic_map_binary(ray_xbar_fn, col, bucket);
    /* Both are integer types (i64, i32, i16) → integer xbar */
    if (is_numeric(col) && is_numeric(bucket) && !is_float_op(col, bucket)) {
        int64_t a = as_i64(col), b = as_i64(bucket);
        if (b == 0 || RAY_ATOM_IS_NULL(col) || RAY_ATOM_IS_NULL(bucket))
            return ray_error("domain", "xbar: bucket size must be non-zero and operands non-null");
        int64_t q = a / b;
        if ((a ^ b) < 0 && q * b != a) q--;
        int64_t result = q * b;
        /* Result type follows the wider of the two operands */
        if (col->type == -RAY_I32 && bucket->type == -RAY_I32) return make_i32((int32_t)result);
        if (col->type == -RAY_I16 && bucket->type == -RAY_I16) return make_i16((int16_t)result);
        return make_i64(result);
    }
    /* Float path: either operand is f64 */
    if (is_numeric(col) && is_numeric(bucket)) {
        if (RAY_ATOM_IS_NULL(col) || RAY_ATOM_IS_NULL(bucket))
            return ray_error("domain", "xbar: operands must be non-null");
        double c = as_f64(col), b = as_f64(bucket);
        if (b == 0.0) return ray_error("domain", "xbar: bucket size must be non-zero");
        double fq = floor(c / b);
        return make_f64(fq * b);
    }
    /* Temporal xbar: col is temporal, bucket is integer or temporal (not float) */
    if (is_temporal(col) && (is_temporal(bucket) ||
        (is_numeric(bucket) && bucket->type != -RAY_F64))) {
        int64_t a = col->i64, b;
        if (is_temporal(bucket)) {
            b = bucket->i64;
            /* Cross-temporal conversion: TIME(ms) bucket on TIMESTAMP(ns) col */
            if (col->type == -RAY_TIMESTAMP && bucket->type == -RAY_TIME)
                b *= 1000000LL;
        } else {
            b = as_i64(bucket);
        }
        if (b == 0 || RAY_ATOM_IS_NULL(bucket)) return ray_error("domain", "xbar: bucket size must be non-zero and non-null");
        int64_t q = a / b;
        if ((a ^ b) < 0 && q * b != a) q--;
        int64_t result = q * b;
        if (col->type == -RAY_TIME) return ray_time(result);
        if (col->type == -RAY_DATE) return ray_date(result);
        return ray_timestamp(result);
    }
    return ray_error("type", "xbar: unsupported operand types, got %s and %s",
                     ray_type_name(col->type), ray_type_name(bucket->type));
}

/* ══════════════════════════════════════════
 * Update, Insert, Upsert
 * ══════════════════════════════════════════ */

/* Derive the storage type for a typeless (empty RAY_LIST) column from the
 * first value inserted into it — an empty () column adopts its type on the
 * first insert. Returns the RAY_* column type, or RAY_LIST when the payload
 * is itself nested (non-atom elements → a genuine list column). */
static int8_t typeless_col_type(ray_t* payload) {
    if (!payload) return RAY_I64;                 /* null row → default I64 */
    if (ray_is_atom(payload)) return -payload->type;
    if (ray_is_vec(payload)) return payload->type; /* typed vec → splice */
    if (payload->type == RAY_LIST) {
        int64_t m = ray_len(payload);
        if (m == 0) return RAY_LIST;               /* empty payload → stay typeless */
        ray_t** e = (ray_t**)ray_data(payload);
        if (e[0] && !ray_is_atom(e[0])) return RAY_LIST; /* nested cells */
        int8_t t = e[0] ? (int8_t)(-e[0]->type) : RAY_I64;
        if (t == RAY_I64) /* promote to F64 if any element is float */
            for (int64_t k = 0; k < m; k++)
                if (e[k] && e[k]->type == -RAY_F64) { t = RAY_F64; break; }
        return t;
    }
    return RAY_I64;
}

/* Helper: convert a Rayfall list of atoms into a typed column vector by
 * appending to an existing column (for insert/upsert). */
static ray_t* append_atom_to_col(ray_t* col_vec, ray_t* atom) {
    if (RAY_ATOM_IS_NULL(atom)) {
        int64_t idx = col_vec->len;
        uint8_t zero[16] = {0};
        col_vec = ray_vec_append(col_vec, zero);
        if (!RAY_IS_ERR(col_vec))
            ray_vec_set_null(col_vec, idx, true);
        return col_vec;
    }
    int8_t ct = col_vec->type;
    int8_t at = atom->type;
    /* Integer atoms accepted (with width narrowing) by the numeric columns. */
    bool is_int = (at == -RAY_I64 || at == -RAY_I32 ||
                   at == -RAY_I16 || at == -RAY_U8);
    switch (ct) {
    case RAY_BOOL: {
        if (at != -RAY_BOOL) return ray_error("type", "insert: value type does not match bool column, got %s", ray_type_name(at));
        uint8_t v = atom->b8;
        return ray_vec_append(col_vec, &v);
    }
    case RAY_U8: {
        if (!is_int) return ray_error("type", "insert: u8 column requires an integer value, got %s", ray_type_name(at));
        uint8_t v = (uint8_t)as_i64(atom);
        return ray_vec_append(col_vec, &v);
    }
    case RAY_I16: {
        if (!is_int) return ray_error("type", "insert: i16 column requires an integer value, got %s", ray_type_name(at));
        int16_t v = (int16_t)as_i64(atom);
        return ray_vec_append(col_vec, &v);
    }
    case RAY_I32: {
        if (!is_int) return ray_error("type", "insert: i32 column requires an integer value, got %s", ray_type_name(at));
        int32_t v = (int32_t)as_i64(atom);
        return ray_vec_append(col_vec, &v);
    }
    case RAY_I64: {
        if (!is_int) return ray_error("type", "insert: i64 column requires an integer value, got %s", ray_type_name(at));
        int64_t v = as_i64(atom);
        return ray_vec_append(col_vec, &v);
    }
    case RAY_F64: {
        if (at != -RAY_F64 && !is_int) return ray_error("type", "insert: f64 column requires a numeric value, got %s", ray_type_name(at));
        double v = (at == -RAY_F64) ? atom->f64 : (double)as_i64(atom);
        return ray_vec_append(col_vec, &v);
    }
    case RAY_DATE: {
        if (at != -RAY_DATE) return ray_error("type", "insert: date column requires a date value, got %s", ray_type_name(at));
        int32_t v = atom->i32;
        return ray_vec_append(col_vec, &v);
    }
    case RAY_TIME: {
        if (at != -RAY_TIME) return ray_error("type", "insert: time column requires a time value, got %s", ray_type_name(at));
        int32_t v = atom->i32;
        return ray_vec_append(col_vec, &v);
    }
    case RAY_TIMESTAMP: {
        if (at != -RAY_TIMESTAMP) return ray_error("type", "insert: timestamp column requires a timestamp value, got %s", ray_type_name(at));
        int64_t v = atom->i64;
        return ray_vec_append(col_vec, &v);
    }
    case RAY_GUID: {
        if (at != -RAY_GUID) return ray_error("type", "insert: guid column requires a guid value, got %s", ray_type_name(at));
        static const uint8_t zero_guid[16] = {0};
        const void* src = atom->obj ? ray_data(atom->obj) : zero_guid;
        return ray_vec_append(col_vec, src);
    }
    case RAY_SYM: {
        if (at != -RAY_SYM) return ray_error("type", "insert: sym column requires a sym value, got %s", ray_type_name(at));
        int64_t v = atom->i64;
        return ray_vec_append(col_vec, &v);
    }
    case RAY_STR: {
        if (at != -RAY_STR) return ray_error("type", "insert: str column requires a str value, got %s", ray_type_name(at));
        return ray_str_vec_append(col_vec, ray_str_ptr(atom), ray_str_len(atom));
    }
    default:
        return ray_error("type", "insert: unsupported column type %s", ray_type_name(ct));
    }
}

/* Build a fresh boxed LIST column.  A table payload supplies a LIST column
 * whose slots are rows and is therefore spliced; positional LIST/DICT rows
 * append their value as one boxed cell.  Every copied pointer gets its own
 * retain so source tables and returned snapshots remain independently owned. */
static ray_t* append_boxed_table_col(ray_t* orig_col, ray_t* payload,
                                     bool splice_payload) {
    if (!orig_col || RAY_IS_ERR(orig_col) || orig_col->type != RAY_LIST)
        return ray_error("type", "insert: boxed column source must be LIST");
    if (splice_payload &&
        (!payload || RAY_IS_ERR(payload) || payload->type != RAY_LIST))
        return ray_error("type", "insert: table payload for a LIST column must be LIST");

    int64_t tail = splice_payload ? payload->len : 1;
    if (tail < 0 || orig_col->len > INT64_MAX - tail)
        return ray_error("oom", NULL);
    ray_t* out = ray_list_new(orig_col->len + tail);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    ray_t** dst = (ray_t**)ray_data(out);
    ray_t** src = (ray_t**)ray_data(orig_col);
    for (int64_t r = 0; r < orig_col->len; r++) {
        dst[r] = src[r];
        if (dst[r]) ray_retain(dst[r]);
    }
    if (splice_payload) {
        ray_t** cells = (ray_t**)ray_data(payload);
        for (int64_t r = 0; r < tail; r++) {
            dst[orig_col->len + r] = cells[r];
            if (cells[r]) ray_retain(cells[r]);
        }
    } else {
        dst[orig_col->len] = payload;
        if (payload) ray_retain(payload);
    }
    out->len = orig_col->len + tail;
    return out;
}

/* (update {col: expr ... from: t [where: pred]})
 * Special form — receives unevaluated dict arg.
 * For rows matching where (or all if no where), evaluate column expressions
 * and replace those column values. Returns a new table. */
/* Forward declarations */

/* ---- update with WHERE, in place (#475 follow-up) -------------------
 * The WHERE arm below evaluates the predicate and every update
 * expression over the whole table and then rebuilds each updated column
 * cell by cell, so a keyed update into a named table costs O(rows
 * present) per call with a large constant, and an attached hash index
 * on the key column is never consulted.  When the binding is the sole
 * owner, this path resolves the rows first — through a fresh hash index
 * when the predicate is a plain equality on an indexed column, otherwise
 * through the same mask the legacy arm computes — gathers just those
 * rows into a sub-table, evaluates the update expressions there, and
 * scatters the results into the live columns in place.
 *
 * Only elementwise expressions take this path (see
 * update_expr_elementwise); anything else, and a predicate matching no
 * row, keeps the legacy arm.  Every result is type-checked before
 * anything is written,
 * so a mistyped or wrong-length value leaves the table untouched; only
 * an allocation failure inside a STR write can interrupt the scatter. */

static bool    upsert_col_ok(ray_t* col);      /* defined with the upsert path below */
static int64_t upsert_atom_int(ray_t* a);

/* The sub-table evaluation is only equivalent to the legacy whole-table
 * evaluation for elementwise expressions.  Accept a call only when its
 * head resolves to a builtin flagged RAY_FN_ATOMIC and every argument is
 * a column of `tbl`, a literal, a quoted symbol, or such a call.  A bare
 * symbol that is not a column (a free variable, possibly a table-length
 * vector), an order-dependent function (deltas, sums, ...), an
 * aggregate, a special form, or a lambda call keeps the legacy arm. */
static bool update_expr_elementwise(ray_t* e, ray_t* tbl) {
    if (!e || RAY_IS_ERR(e)) return false;
    if (e->type == -RAY_SYM) {
        if (e->attrs & ATTR_QUOTED) return true;
        return ray_table_get_col(tbl, e->i64) != NULL;
    }
    if (e->type < 0) return true;
    if (e->type != RAY_LIST) return false;
    int64_t n = ray_len(e);
    if (n < 2) return false;
    ray_t** el = (ray_t**)ray_data(e);
    ray_t* head = el[0];
    if (!head || head->type != -RAY_SYM || (head->attrs & ATTR_QUOTED)) return false;
    ray_t* fn = ray_env_get(head->i64);
    if (!fn || RAY_IS_ERR(fn)) return false;
    if (fn->type != RAY_UNARY && fn->type != RAY_BINARY && fn->type != RAY_VARY) return false;
    if (!(fn->attrs & RAY_FN_ATOMIC)) return false;
    for (int64_t i = 1; i < n; i++)
        if (!update_expr_elementwise(el[i], tbl)) return false;
    return true;
}

/* Rows matching `(== col atom)` through a fresh hash index on col.
 * Returns 1 with rows copied into *rows (n in *n_out), 0 when the
 * predicate is not of that shape or the index cannot answer. */
static int update_where_index_rows(ray_t* tbl, ray_t* where_expr,
                                   ray_t** rows_hdr, int64_t** rows, int64_t* n_out) {
    if (!where_expr || where_expr->type != RAY_LIST || ray_len(where_expr) != 3) return 0;
    ray_t** el = (ray_t**)ray_data(where_expr);
    if (!el[0] || el[0]->type != -RAY_SYM) return 0;
    if (el[0]->i64 != ray_sym_intern("==", 2)) return 0;
    ray_t* col = NULL; ray_t* val_node = NULL;
    for (int side = 1; side <= 2 && !col; side++) {
        ray_t* a = el[side];
        if (a && a->type == -RAY_SYM && ray_table_get_col(tbl, a->i64)) {
            col = ray_table_get_col(tbl, a->i64);
            val_node = el[3 - side];
        }
    }
    if (!col || !val_node || RAY_IS_ERR(val_node)) return 0;
    /* The other side must be a value: a literal, a quoted symbol, or a
     * variable — never a column name (that is a column-column compare). */
    if (val_node->type == -RAY_SYM && ray_table_get_col(tbl, val_node->i64)) return 0;
    if (!ray_is_atom(val_node)) return 0;
    if (col->attrs & RAY_ATTR_HAS_NULLS) return 0;
    if (ray_index_kind(col) != RAY_IDX_HASH) return 0;
    if (col->type == RAY_F64 || col->type == RAY_STR || col->type == RAY_GUID) return 0;
    ray_t* v = ray_eval(val_node);
    if (!v) return 0;
    if (RAY_IS_ERR(v)) { ray_error_free(v); return 0; }
    int ok = ray_is_atom(v) && !RAY_ATOM_IS_NULL(v) &&
             (col->type == RAY_SYM ? v->type == -RAY_SYM
                                   : (v->type == -col->type || v->type == -RAY_I64 ||
                                      v->type == -RAY_I32 || v->type == -RAY_I16 ||
                                      v->type == -RAY_U8));
    int64_t key = ok ? (col->type == RAY_SYM ? v->i64 : upsert_atom_int(v)) : 0;
    ray_release(v);
    if (!ok) return 0;
    const int64_t* ids = NULL; int64_t n = 0;
    int hit = ray_index_hash_group(col, key, &ids, &n);
    if (hit < 0) return 0;
    int64_t* out = (int64_t*)scratch_alloc(rows_hdr, (size_t)(n > 0 ? n : 1) * sizeof(int64_t));
    if (!out) return 0;
    if (n > 0) memcpy(out, ids, (size_t)n * sizeof(int64_t));
    *rows = out; *n_out = n;
    return 1;
}

/* Legacy-shaped mask evaluation (DAG, then eval with column bindings). */
static ray_t* update_where_mask_vec(ray_t* tbl, ray_t* where_expr) {
    ray_t* mask_vec = NULL;
    bool rejected = false;
    ray_graph_t* g = ray_graph_new(tbl);
    if (g) {
        ray_op_t* pred = compile_expr_dag(g, where_expr);
        if (pred) {
            pred = ray_optimize(g, pred);
            mask_vec = ray_execute(g, pred);
        } else if (g->compile_err) {
            /* rejected outright, not declined: no interpreter retry */
            mask_vec = graph_take_compile_err(g);
            rejected = true;
        }
        ray_graph_free(g);
    }
    if (!rejected && (!mask_vec || RAY_IS_ERR(mask_vec))) {
        int64_t ncols = ray_table_ncols(tbl);
        ray_env_push_query_scope();
        for (int64_t c = 0; c < ncols; c++)
            ray_env_set_query_local(ray_table_col_name(tbl, c), ray_table_get_col_idx(tbl, c));
        mask_vec = ray_eval(where_expr);
        ray_env_pop_scope();
    }
    if (!mask_vec) return ray_error("type", "update: `where:` predicate evaluation failed");
    if (RAY_IS_ERR(mask_vec)) return mask_vec;
    mask_vec = where_mask_spread(mask_vec, ray_table_nrows(tbl), "update");
    if (!mask_vec || RAY_IS_ERR(mask_vec)) return mask_vec ? mask_vec : ray_error("oom", NULL);
    if (mask_vec->type != RAY_BOOL || mask_vec->len != ray_table_nrows(tbl)) {
        int8_t mask_t = mask_vec->type;
        ray_release(mask_vec);
        return ray_error("type", "update: `where:` must produce a bool mask matching row count, got %s", ray_type_name(mask_t));
    }
    return mask_vec;
}

/* A scalar computed as a one-element vector rather than an atom, such as
 * `(+ (sum x) 0)` or an `if` whose parts are all scalar, becomes that atom
 * when the column has another length, so the atom broadcast below applies
 * to it.  Left as a vector it replaced the column with one row. */
static ray_t* update_scalar_vec(ray_t* v, int64_t nrows) {
    if (!v || RAY_IS_ERR(v) || v->type <= 0 || v->type == RAY_LIST ||
        !ray_is_vec(v) || v->len != 1 || nrows == 1)
        return v;
    int alloc = 0;
    ray_t* a = collection_elem(v, 0, &alloc);
    if (!a || RAY_IS_ERR(a)) return v;
    if (!alloc) ray_retain(a);
    ray_release(v);
    return a;
}

/* The cell an atom writes into a column of type ct (esz bytes wide).  GUID
 * keeps its 16 bytes in ->obj, an I64 atom into F64 is converted, and an
 * F32 atom holds its value widened in ->f64, so copying ->i64 wrote the
 * low half of a double: zeros for most values. */
static void update_atom_cell(const ray_t* a, int8_t ct, size_t esz, uint8_t elem[16]) {
    memset(elem, 0, 16);
    if (ct == RAY_GUID) {
        if (a->obj) memcpy(elem, ray_data(a->obj), 16);
    } else if (ct == RAY_F64 && a->type == -RAY_I64) {
        double p = (double)a->i64; memcpy(elem, &p, sizeof p);
    } else if (ct == RAY_F32 && (a->type == -RAY_F32 || a->type == -RAY_F64)) {
        float f = (float)a->f64; memcpy(elem, &f, sizeof f);
    } else {
        memcpy(elem, &a->i64, esz);
    }
}

/* One cell of an update-by result.  A STR cell goes through the string
 * pool: store_typed_elem has no STR arm and left the cell empty, so a
 * string per group came out as "".  Returns the column, or an error. */
/* Rank of a numeric column type for update by:'s promotion of a group
 * result of another type; 0 for a non-numeric type. */
static int update_by_num_rank(int8_t t) {
    switch (t) {
        case RAY_BOOL: return 1;
        case RAY_U8:   return 2;
        case RAY_I16:  return 3;
        case RAY_I32:  return 4;
        case RAY_I64:  return 5;
        case RAY_F32:  return 6;
        case RAY_F64:  return 7;
        default:       return 0;
    }
}

/* A copy of the numeric column `col` as type `to` (every row converted
 * through store_typed_elem, nulls carried).  Owned. */
static ray_t* update_by_promote(ray_t* col, int8_t to) {
    int64_t n = ray_len(col);
    ray_t* nc = ray_vec_new(to, n);
    if (!nc || RAY_IS_ERR(nc)) return nc ? nc : ray_error("oom", NULL);
    nc->len = n;
    for (int64_t r = 0; r < n; r++) {
        int alloc = 0;
        ray_t* cell = collection_elem(col, r, &alloc);
        if (!cell || RAY_IS_ERR(cell)) { ray_release(nc); return cell ? cell : ray_error("oom", NULL); }
        store_typed_elem(nc, r, cell);
        if (alloc) ray_release(cell);
    }
    return nc;
}

static ray_t* update_by_store(ray_t* col, int64_t row, ray_t* cell) {
    if (col->type == RAY_STR) {
        const char* sp = "";
        size_t sl = 0;
        if (cell->type == -RAY_STR && !RAY_ATOM_IS_NULL(cell)) {
            sp = ray_str_ptr(cell);
            sl = ray_str_len(cell);
        }
        return ray_str_vec_set(col, row, sp, sl);
    }
    store_typed_elem(col, row, cell);
    return col;
}

/* True iff `expr` reads a column of `tbl` anywhere in its subtree — unlike
 * expr_refs_row_column it looks inside aggregates too. */
static int expr_refs_any_column(ray_t* expr, ray_t* tbl) {
    if (!expr) return 0;
    if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
        if (ray_env_has_lexical_local(expr->i64)) return 0;
        if (ray_table_get_col(tbl, expr->i64)) return 1;
        if (ray_sym_is_dotted(expr->i64)) {
            const int64_t* segs;
            int nsegs = ray_sym_segs(expr->i64, &segs);
            if (nsegs >= 1 && ray_table_get_col(tbl, segs[0])) return 1;
        }
        return 0;
    }
    if (expr->type == RAY_LIST) {
        ray_t** elems = (ray_t**)ray_data(expr);
        int64_t n = ray_len(expr);
        for (int64_t i = 1; i < n; i++)
            if (expr_refs_any_column(elems[i], tbl)) return 1;
    }
    return 0;
}

/* True iff `expr` contains an aggregate over a column of `tbl`: the one
 * shape a per-row pass cannot evaluate, since the aggregate would see one
 * cell.  An aggregate over a global or a literal is the same value on
 * every row and is fine per row. */
static int expr_contains_agg_over_column(ray_t* expr, ray_t* tbl) {
    if (!expr || expr->type != RAY_LIST) return 0;
    if (is_agg_expr(expr)) return expr_refs_any_column(expr, tbl);
    ray_t** elems = (ray_t**)ray_data(expr);
    int64_t n = ray_len(expr);
    for (int64_t i = 0; i < n; i++)
        if (expr_contains_agg_over_column(elems[i], tbl)) return 1;
    return 0;
}

/* Whether a DAG error must stand rather than hand the expression to the
 * interpreter: only when an `if` refused its operands (g->if_refused).  The
 * interpreter's `if` tests one truth value, so retrying there wrote the
 * then-branch to every matched row.  Other DAG failures, such as an
 * operation the DAG does not take, keep the fallback. */
static bool update_dag_err_final(ray_t* out, bool if_refused) {
    return if_refused && out && RAY_IS_ERR(out);
}

/* Evaluate an update expression over `sub`: the DAG first, then the
 * interpreter.  The interpreter pass binds whole columns and evaluates once
 * — its vector builtins are elementwise, and aggregates and whole-column
 * verbs need the whole column — except for an `if` that reads a column in
 * row position (expr_refs_row_column): the interpreter's `if` tests ONE
 * truth value, so a whole-column pass wrote the then-branch to every row;
 * that goes per row — unless it also aggregates, which per row would see
 * one cell: that is an error (#692).  When both paths fail, the planner's error (a misspelt
 * column: `schema: column 'x' not found`) wins over the interpreter's less
 * specific one.  Owned; a one-element vector is its atom. */
static ray_t* update_eval_on(ray_t* sub, ray_t* expr) {
    ray_t* out = NULL;
    ray_t* cerr = NULL;
    bool if_refused = false;
    ray_graph_t* g = ray_graph_new(sub);
    if (g) {
        ray_op_t* op = compile_expr_dag(g, expr);
        if (op) {
            op = ray_optimize(g, op);
            out = ray_execute(g, op);
        } else {
            cerr = graph_take_compile_err(g);
        }
        if_refused = g->if_refused;
        ray_graph_free(g);
    }
    if (update_dag_err_final(out, if_refused)) {
        if (cerr) ray_error_free(cerr);
        return out;
    }
    if (!out || RAY_IS_ERR(out)) {
        /* Keep the planner's error — from compile, or from executing what
         * it compiled (a scan of a misspelt column: `schema: column 'x'
         * not found`) — to report if the interpreter fails as well. */
        if (out) { if (cerr) ray_error_free(cerr); cerr = out; out = NULL; }
        int64_t nrows = ray_table_nrows(sub);
        /* The interpreter's vector builtins are elementwise over whole
         * columns, and whole-column verbs (.idx.hash, distinct, ...) and
         * aggregates need the whole column; only `if` is not — it tests one
         * truth value — so an `if` over a column goes per row. */
        int per_row = expr_contains_call_named(expr, "if", 2) && expr_refs_row_column(expr, sub);
        if (per_row && expr_contains_agg_over_column(expr, sub)) {
            /* Per row, an aggregate over a column sees one cell:
             * `(if (> i (avg i)) ..)` would compare each row with itself.
             * Keep this an error until the fallback can reduce aggregates
             * first (#692).  (An aggregate over a global or a literal is the
             * same value on every row and goes through.) */
            if (cerr) ray_error_free(cerr);
            return ray_error("nyi", "update: an if over a column that also aggregates is not supported without the planner");
        }
        if (!per_row) {
            int64_t ncols = ray_table_ncols(sub);
            ray_env_push_query_scope();
            for (int64_t c = 0; c < ncols; c++)
                ray_env_set_query_local(ray_table_col_name(sub, c), ray_table_get_col_idx(sub, c));
            out = ray_eval(expr);
            ray_env_pop_scope();
        } else {
            out = eval_expr_per_row(expr, sub, nrows);
        }
        if ((!out || RAY_IS_ERR(out)) && cerr) {
            if (out) ray_error_free(out);
            return cerr;
        }
    }
    if (cerr) ray_error_free(cerr);   /* error objects bypass the refcount */
    if (!out) return ray_error("type", "update: failed to evaluate column update expression");
    if (RAY_IS_ERR(out)) return out;
    if (ray_is_lazy(out)) out = ray_lazy_materialize(out);
    return update_scalar_vec(out, ray_table_nrows(sub));
}

static bool update_numeric_promo(int8_t ct, int8_t et) {
    return (ct == RAY_I64 || ct == RAY_I32 || ct == RAY_F64) &&
           (et == RAY_I64 || et == RAY_I32 || et == RAY_F64);
}

/* Validate one evaluated update value against its column: NULL on ok,
 * the error otherwise.  Mirrors the legacy WHERE arm's acceptance. */
static ray_t* update_value_check(ray_t* col, ray_t* val, int64_t k) {
    int8_t ct = col->type;
    if (val->type < 0) {
        int ok = (val->type == -ct) ||
                 (ct == RAY_F64 && val->type == -RAY_I64) ||
                 (ct == RAY_LIST && val->type == -RAY_SYM);
        if (!ok) return ray_error("type", "update: cannot assign %s value to %s column",
                                  ray_type_name(val->type), ray_type_name(ct));
        return NULL;
    }
    if (!ray_is_vec(val) && val->type != RAY_LIST)
        return ray_error("type", "update: cannot assign %s expression to %s column",
                         ray_type_name(val->type), ray_type_name(ct));
    if (ray_len(val) != k)
        return ray_error("length", "update: expression yields %lld values for %lld matched rows",
                         (long long)ray_len(val), (long long)k);
    if (val->type == ct) return NULL;
    if (update_numeric_promo(ct, val->type)) return NULL;
    return ray_error("type", "update: expression type %s does not match %s column",
                     ray_type_name(val->type), ray_type_name(ct));
}

static double update_cell_f64(ray_t* v, int64_t i) {
    switch (v->type) {
    case RAY_F64: return ((const double*)ray_data(v))[i];
    case RAY_I32: return (double)((const int32_t*)ray_data(v))[i];
    default:      return (double)((const int64_t*)ray_data(v))[i];
    }
}

/* Scatter `val` (checked) into slots[c] at rows[0..k). */
static ray_t* update_scatter(ray_t** slots, int64_t c, const int64_t* rows, int64_t k, ray_t* val) {
    ray_t* col = slots[c];
    int8_t ct = col->type;
    if (ct == RAY_LIST) {
        ray_t** cells = (ray_t**)ray_data(col);
        for (int64_t i = 0; i < k; i++) {
            ray_t* nv = (val->type < 0) ? val : ((ray_t**)ray_data(val))[i];
            ray_t* old = cells[rows[i]];
            if (nv) ray_retain(nv);
            cells[rows[i]] = nv;
            if (old) ray_release(old);
        }
        return NULL;
    }
    ray_index_drop(&slots[c]);
    col = slots[c];
    if (RAY_IS_ERR(col)) return col;
    col->attrs &= (uint8_t)~RAY_ATTR_SORTED;

    if (ct == RAY_STR) {
        for (int64_t i = 0; i < k; i++) {
            const char* sp; size_t sl;
            if (val->type < 0) { sp = ray_str_ptr(val); sl = ray_str_len(val); }
            else               { sl = 0; sp = ray_str_vec_get(val, i, &sl); }
            ray_t* res = ray_str_vec_set(col, rows[i], sp ? sp : "", sp ? sl : 0);
            if (RAY_IS_ERR(res)) return res;
            slots[c] = col = res;
        }
        return NULL;
    }

    size_t esz = ray_sym_elem_size(ct, col->attrs);
    uint8_t* d = (uint8_t*)ray_data(col);
    if (val->type < 0) {
        /* Atom broadcast: one encoded cell, written k times. */
        uint8_t elem[16];
        bool is_null = RAY_ATOM_IS_NULL(val);
        update_atom_cell(val, ct, esz, elem);
        for (int64_t i = 0; i < k; i++) {
            if (is_null) ray_vec_set_null(col, rows[i], true);
            else {
                memcpy(d + (size_t)rows[i] * esz, elem, esz);
                if (ct == RAY_SYM && val->i64 == 0) col->attrs |= RAY_ATTR_HAS_NULLS;
            }
        }
        return NULL;
    }

    if (val->type != ct) {
        /* Numeric promotion among I64 / I32 / F64, null-aware. */
        void* p = ray_data(col);
        for (int64_t i = 0; i < k; i++) {
            int64_t r = rows[i];
            if (ray_vec_is_null(val, i)) { ray_vec_set_null(col, r, true); continue; }
            if (ct == RAY_F64) {
                ((double*)p)[r] = update_cell_f64(val, i);
            } else if (val->type == RAY_F64) {
                double x = ((const double*)ray_data(val))[i];
                if (ct == RAY_I64) ((int64_t*)p)[r] = ray_cast_f64_to_i64_null(x);
                else               ((int32_t*)p)[r] = ray_cast_f64_to_i32_null(x);
                if (ray_vec_is_null(col, r)) col->attrs |= RAY_ATTR_HAS_NULLS;
            } else if (ct == RAY_I64) {
                ((int64_t*)p)[r] = (int64_t)((const int32_t*)ray_data(val))[i];
            } else {
                ((int32_t*)p)[r] = (int32_t)((const int64_t*)ray_data(val))[i];
            }
        }
        return NULL;
    }

    if (ct == RAY_SYM) {
        int64_t* ids = (int64_t*)ray_data(col);
        for (int64_t i = 0; i < k; i++) {
            int64_t id = sym_cell_runtime_id(val, i);
            ids[rows[i]] = id;
            if (id == 0) col->attrs |= RAY_ATTR_HAS_NULLS;
        }
        return NULL;
    }
    const uint8_t* s = (const uint8_t*)ray_data(val);
    bool vn = ray_vec_may_have_nulls(val);
    for (int64_t i = 0; i < k; i++) {
        if (vn && ray_vec_is_null(val, i)) { ray_vec_set_null(col, rows[i], true); continue; }
        memcpy(d + (size_t)rows[i] * esz, s + (size_t)i * esz, esz);
    }
    return NULL;
}

static ray_t* update_where_inplace(ray_t* tbl, int64_t inplace_sym, ray_t* dict,
                                   ray_t* where_expr, bool* handled) {
    *handled = false;
    int64_t ncols = ray_table_ncols(tbl);
    if (inplace_sym < 0 || ncols <= 0) return NULL;
    if (tbl->rc != 2 || tbl->mmod != 0 || (tbl->attrs & (RAY_ATTR_SLICE | RAY_ATTR_ARENA))) return NULL;
    ray_t* live_cols = ((ray_t**)ray_data(tbl))[1];
    if (!live_cols || live_cols->rc != 1 || live_cols->len != ncols) return NULL;
    ray_t** slots = (ray_t**)ray_data(live_cols);
    for (int64_t c = 0; c < ncols; c++)
        if (slots[c]->rc != 1 || !upsert_col_ok(slots[c])) return NULL;

    DICT_VIEW_DECL(updf);
    DICT_VIEW_OPEN(dict, updf);
    if (DICT_VIEW_OVERFLOW(updf)) { DICT_VIEW_CLOSE(updf); *handled = true; return ray_error("oom", NULL); }
    int64_t from_id = ray_sym_intern("from", 4);
    int64_t where_id = ray_sym_intern("where", 5);
    int64_t by_id = ray_sym_intern("by", 2);

    /* One update per target column, first dict entry wins (legacy order). */
    ray_t* ucol_hdr = NULL; ray_t* uexpr_hdr = NULL; ray_t* uval_hdr = NULL;
    int64_t* ucol = (int64_t*)scratch_alloc(&ucol_hdr, (size_t)ncols * sizeof(int64_t));
    ray_t** uexpr = (ray_t**)scratch_alloc(&uexpr_hdr, (size_t)ncols * sizeof(ray_t*));
    ray_t** uval = (ray_t**)scratch_alloc(&uval_hdr, (size_t)ncols * sizeof(ray_t*));
    if (!ucol || !uexpr || !uval) {
        if (ucol_hdr) scratch_free(ucol_hdr);
        if (uexpr_hdr) scratch_free(uexpr_hdr);
        if (uval_hdr) scratch_free(uval_hdr);
        DICT_VIEW_CLOSE(updf); *handled = true; return ray_error("oom", NULL);
    }
    int64_t nu = 0;
    bool rowwise = true;
    for (int64_t c = 0; c < ncols; c++) {
        int64_t cn = ray_table_col_name(tbl, c);
        for (int64_t d = 0; d + 1 < updf_n; d += 2) {
            int64_t kid = updf[d]->i64;
            if (kid == from_id || kid == where_id || kid == by_id) continue;
            if (kid != cn) continue;
            ucol[nu] = c; uexpr[nu] = updf[d + 1]; uval[nu] = NULL; nu++;
            if (!update_expr_elementwise(updf[d + 1], tbl)) rowwise = false;
            break;
        }
    }
    /* A dict entry naming no column adds one; that is the general path's
     * job (this one writes existing columns in place and dropped it). */
    bool adds_col = false;
    for (int64_t d = 0; d + 1 < updf_n && !adds_col; d += 2) {
        int64_t kid = updf[d]->i64;
        if (kid == from_id || kid == where_id || kid == by_id) continue;
        bool found = false;
        for (int64_t i = 0; i < nu; i++)
            if (ray_table_col_name(tbl, ucol[i]) == kid) { found = true; break; }
        if (!found) adds_col = true;
    }
    if (!rowwise || adds_col) {
        scratch_free(ucol_hdr); scratch_free(uexpr_hdr); scratch_free(uval_hdr);
        DICT_VIEW_CLOSE(updf);
        return NULL;
    }
    *handled = true;

    /* Only a write into a key column can invalidate upsert's key map, and
     * that is the one mutation its recorded row count cannot see: a cell
     * rewrite leaves the count alone.  An update of any other column leaves
     * the mapping exactly as it was — dropping it there would send the next
     * batch back to rebuilding over every row for nothing.  ucol[0..nu) is
     * the complete set of columns this path writes. */
    {
        ray_index_t* kix = ray_table_ukey_get(tbl);
        if (kix) {
            bool hits_key = false;
            for (int64_t i = 0; i < nu && !hits_key; i++)
                for (int64_t kk = 0; kk < kix->u.ukey.nk; kk++)
                    if (kix->u.ukey.kci[kk] == (int16_t)ucol[i]) { hits_key = true; break; }
            if (hits_key) ray_table_ukey_drop(tbl);
        }
    }

    /* Matched rows: index probe, else the legacy mask. */
    ray_t* rows_hdr = NULL; int64_t* rows = NULL; int64_t k = 0;
    ray_t* err = NULL;
    if (!update_where_index_rows(tbl, where_expr, &rows_hdr, &rows, &k)) {
        ray_t* mask = update_where_mask_vec(tbl, where_expr);
        if (RAY_IS_ERR(mask)) err = mask;
        else {
            int64_t nrows = mask->len;
            const uint8_t* m = (const uint8_t*)ray_data(mask);
            int64_t cnt = 0;
            for (int64_t r = 0; r < nrows; r++) cnt += (m[r] != 0);
            rows = (int64_t*)scratch_alloc(&rows_hdr, (size_t)(cnt > 0 ? cnt : 1) * sizeof(int64_t));
            if (!rows) err = ray_error("oom", NULL);
            else for (int64_t r = 0; r < nrows; r++) if (m[r]) rows[k++] = r;
            ray_release(mask);
        }
    }

    /* No matched row: the legacy arm still evaluates every expression
     * over the table and reports its errors; evaluating over an empty
     * sub-table could raise where it would not, so leave it to them. */
    if (!err && k == 0) {
        if (rows_hdr) scratch_free(rows_hdr);
        scratch_free(ucol_hdr); scratch_free(uexpr_hdr); scratch_free(uval_hdr);
        DICT_VIEW_CLOSE(updf);
        *handled = false;
        return NULL;
    }

    /* Sub-table of the matched rows; expressions evaluate against it. */
    ray_t* idxv = NULL; ray_t* sub = NULL;
    if (!err && nu > 0) {
        idxv = ray_vec_new(RAY_I64, k);
        if (RAY_IS_ERR(idxv)) { err = idxv; idxv = NULL; }
        else {
            ray_t* r2 = ray_vec_append_raw(idxv, rows, k);
            if (RAY_IS_ERR(r2)) { err = r2; } else idxv = r2;
        }
        if (!err) {
            sub = ray_at_fn(tbl, idxv);
            if (!sub || RAY_IS_ERR(sub)) { err = sub ? sub : ray_error("oom", NULL); sub = NULL; }
        }
    }
    for (int64_t u = 0; u < nu && !err; u++) {
        ray_t* v = update_eval_on(sub, uexpr[u]);
        if (RAY_IS_ERR(v)) { err = v; break; }
        uval[u] = v;
        err = update_value_check(slots[ucol[u]], v, k);
    }
    for (int64_t u = 0; u < nu && !err; u++)
        err = update_scatter(slots, ucol[u], rows, k, uval[u]);

    for (int64_t u = 0; u < nu; u++) if (uval[u]) ray_release(uval[u]);
    if (sub) ray_release(sub);
    if (idxv) ray_release(idxv);
    if (rows_hdr) scratch_free(rows_hdr);
    scratch_free(ucol_hdr); scratch_free(uexpr_hdr); scratch_free(uval_hdr);
    DICT_VIEW_CLOSE(updf);
    if (err) return err;
    return ray_sym(inplace_sym);
}

ray_t* ray_update_fn(ray_t** args, int64_t n) {
    return ray_update(args, n);
}

ray_t* ray_update(ray_t** args, int64_t n) {
    if (n < 1) return ray_error("arity", "update: expects a query dict, got %lld args", (long long)n);
    ray_t* dict = args[0];
    if (!dict || dict->type != RAY_DICT)
        return ray_error("type", "update: query must be a dict, got %s", dict ? ray_type_name(dict->type) : "null");

    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr) return ray_error("domain", "update: missing `from:` clause");
    /* Detect in-place update: from: 't means quoted symbol */
    int64_t inplace_sym = -1;
    ray_t* tbl = ray_eval(from_expr);
    if (RAY_IS_ERR(tbl)) return tbl;
    if (tbl->type == -RAY_SYM) {
        /* from: 't — resolve symbol to table variable */
        inplace_sym = tbl->i64;
        ray_release(tbl);
        tbl = ray_env_get(inplace_sym);
        if (!tbl || RAY_IS_ERR(tbl)) return ray_error("domain", "update: `from:` symbol is unbound");
        ray_retain(tbl);
    }
    if (tbl->type != RAY_TABLE) { int8_t tbl_t = tbl->type; ray_release(tbl); return ray_error("type", "update: `from:` must be a table, got %s", ray_type_name(tbl_t)); }

    /* A parted table's data columns carry the RAY_PARTED_BASE wrapper type
     * (which `ray_type_name` prints as "?"), and its partition key is
     * RAY_MAPCOMMON.  The update machinery below reads the original column
     * through `ray_vec_new(ct, ...)` / `ray_data(col)` / the per-group gather
     * and type-check against the wrapper type, none of which understand the
     * parted/segmented shape — so `(update {col: … from: partedT})` failed
     * with `expression type I64 does not match ? column` (or the `by:` path
     * with `group: argument must be a vector`).  `select` solves this by
     * materialising parted columns on demand; replicate it here by flattening
     * the whole table once so every branch below sees wrapped-free vectors. */
    if (table_has_parted_columns(tbl)) {
        ray_t* flat_tbl = ray_table_new(ray_table_ncols(tbl));
        if (!flat_tbl || RAY_IS_ERR(flat_tbl)) { ray_release(tbl); return flat_tbl ? flat_tbl : ray_error("oom", NULL); }
        int64_t nc = ray_table_ncols(tbl);
        for (int64_t c = 0; c < nc; c++) {
            ray_t* col = ray_table_get_col_idx(tbl, c);
            ray_t* flat_col = query_materialize_parted_col(col);
            if (!flat_col || RAY_IS_ERR(flat_col)) {
                ray_release(flat_tbl); ray_release(tbl);
                return flat_col ? flat_col : ray_error("oom", NULL);
            }
            flat_tbl = ray_table_add_col(flat_tbl, ray_table_col_name(tbl, c), flat_col);
            ray_release(flat_col);
            if (!flat_tbl || RAY_IS_ERR(flat_tbl)) { ray_release(tbl); return flat_tbl ? flat_tbl : ray_error("oom", NULL); }
        }
        ray_release(tbl);
        tbl = flat_tbl;
    }

    ray_t* where_expr = dict_get(dict, "where");
    ray_t* by_expr = dict_get(dict, "by");

    /* update supports `by:` (grouped aggregate broadcast) and `where:` (masked
     * row assignment) only SEPARATELY.  The combination was previously a silent
     * wrong result: the `by:` branch below is gated on `!where_expr`, so when
     * both were present `by:` was silently dropped and the update applied as if
     * ungrouped (review 2.10).  Honoring both would require composing the
     * where-filter + group pipeline the way `select` does — a larger feature.
     * Until then, reject the combination LOUDLY rather than mislead. */
    if (by_expr && where_expr) {
        ray_release(tbl);
        return ray_error("nyi", "update does not support `by:` together with `where:`");
    }

    /* UPDATE WITH BY: group, compute aggregate, broadcast back */
    if (by_expr && !where_expr) {
        DICT_VIEW_DECL(updv);
        DICT_VIEW_OPEN(dict, updv);
        if (DICT_VIEW_OVERFLOW(updv)) {
            ray_release(tbl);
            DICT_VIEW_CLOSE(updv); return ray_error("oom", NULL);
        }
        int64_t dict_n = updv_n;
        ray_t** dict_elems = updv;
        int64_t from_id  = ray_sym_intern("from",  4);
        int64_t where_id = ray_sym_intern("where", 5);
        int64_t by_id    = ray_sym_intern("by",    2);

        /* Resolve group key column name.
         * by_expr is a name reference (not evaluated) — extract sym_id directly */
        int64_t by_col_name = -1;
        if (by_expr->type == -RAY_SYM) {
            by_col_name = by_expr->i64;
        }
        if (by_col_name < 0) { ray_release(tbl); DICT_VIEW_CLOSE(updv); return ray_error("type", "update by: group key must be a column name symbol"); }

        /* Find group column in table */
        ray_t* grp_col = ray_table_get_col(tbl, by_col_name);
        if (!grp_col) { ray_release(tbl); DICT_VIEW_CLOSE(updv); return ray_error("domain", "update by: group key column not found"); }
        int64_t nrows2 = ray_table_nrows(tbl);

        /* Use ray_group_indices_fn to get group indices: {key: [indices]}.
         * Flatten the resulting RAY_DICT into the legacy interleaved
         * [k0,v0,…] LIST shape this branch was written against. */
        ray_t* groups = NULL;
        {
            ray_t* gd = ray_group_indices_fn(grp_col);
            if (!gd || RAY_IS_ERR(gd)) { ray_release(tbl); DICT_VIEW_CLOSE(updv); return gd ? gd : ray_error("oom", NULL); }
            groups = groups_to_pair_list(gd);
            ray_release(gd);
            if (RAY_IS_ERR(groups)) { ray_release(tbl); DICT_VIEW_CLOSE(updv); return groups; }
        }

        int64_t ncols = ray_table_ncols(tbl);
        int64_t ngroups = groups->len / 2;
        ray_t** gdata = (ray_t**)ray_data(groups);
        if (!gdata) { ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return ray_error("oom", NULL); }

        int64_t n_updates = 0;
        for (int64_t d = 0; d + 1 < dict_n; d += 2) {
            int64_t kid = dict_elems[d]->i64;
            if (kid == from_id || kid == where_id || kid == by_id) continue;
            n_updates++;
        }
        size_t upd_slots = (size_t)(n_updates ? n_updates : 1);
        ray_t* upd_hdr = NULL;
        int64_t* upd_names = (int64_t*)scratch_calloc(&upd_hdr,
            upd_slots * sizeof(int64_t) +
            upd_slots * sizeof(ray_t*) +
            upd_slots * sizeof(uint8_t));
        if (!upd_names) { ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return ray_error("oom", NULL); }
        ray_t** upd_cols = (ray_t**)(upd_names + upd_slots);
        uint8_t* upd_used = (uint8_t*)(upd_cols + upd_slots);

        #define UPDATE_BY_CLEANUP_COLS() do {                                      \
            for (int64_t _ui = 0; _ui < n_updates; _ui++)                          \
                if (upd_cols[_ui]) ray_release(upd_cols[_ui]);                     \
            scratch_free(upd_hdr);                                                 \
        } while (0)

        /* For each aggregate expression, compute per group and broadcast */
        int64_t upd_i = 0;
        for (int64_t d = 0; d + 1 < dict_n; d += 2) {
            int64_t kid = dict_elems[d]->i64;
            if (kid == from_id || kid == where_id || kid == by_id) continue;
            ray_t* agg_expr = dict_elems[d + 1];

            /* We need to evaluate the aggregate per group.
             * Build the result column by evaluating the expression on each group's subset. */
            int8_t out_type = RAY_I64;
            ray_t* target_col = ray_table_get_col(tbl, kid);
            if (ngroups == 0 && target_col) out_type = target_col->type;
            ray_t* out_col = ray_vec_new(out_type, nrows2); /* resized to expression type on first group */
            if (RAY_IS_ERR(out_col)) { UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return out_col; }
            if (ngroups == 0) {
                out_col->len = nrows2;
                /* Match the per-group path's zero-fill so an unwritten buffer
                 * never carries allocator garbage (unreachable with rows today
                 * — a non-empty table always yields a group — but uniform). */
                memset(ray_data(out_col), 0,
                    (size_t)nrows2 * (size_t)ray_sym_elem_size(out_col->type, out_col->attrs));
            }
            int first_group = 1;

            for (int64_t gi = 0; gi < ngroups; gi++) {
                ray_t* idx_vec = gdata[gi * 2 + 1]; /* index vector for this group */
                int64_t gsize = ray_len(idx_vec);

                /* Build a sub-table for this group */
                ray_t* sub_tbl = ray_table_new((int32_t)ncols);
                if (RAY_IS_ERR(sub_tbl)) { ray_release(out_col); UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return sub_tbl; }
                for (int64_t c = 0; c < ncols; c++) {
                    int64_t cn = ray_table_col_name(tbl, c);
                    ray_t* full_col = ray_table_get_col_idx(tbl, c);
                    int8_t ct = full_col->type;
                    /* Create sub_col with the SOURCE column's width. For a SYM
                     * column that dictionary-index width is adaptive
                     * (W8/W16/W32/W64); the raw cell-id gather below must use it
                     * for BOTH src and dst, or a narrow-width column (e.g. a few
                     * distinct exchanges => W8) is read at an 8-byte stride and
                     * walks off the end of the buffer. */
                    ray_t* sub_col = (ct == RAY_SYM)
                        ? ray_sym_vec_new(full_col->attrs & RAY_SYM_W_MASK, gsize)
                        : ray_vec_new(ct, gsize);
                    if (RAY_IS_ERR(sub_col)) { ray_release(sub_tbl); ray_release(out_col); UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return sub_col; }
                    /* per-group gather raw-copies cell ids from ONE
                     * source column — keep its dictionary */
                    if (ct == RAY_SYM)
                        ray_sym_vec_adopt_domain(sub_col, full_col);
                    sub_col->len = gsize;
                    int esz = ray_sym_elem_size(ct, full_col->attrs);
                    char* src = (char*)ray_data(full_col);
                    char* dst = (char*)ray_data(sub_col);
                    int64_t* idxs = (int64_t*)ray_data(idx_vec);
                    bool copied_null = false;
                    for (int64_t r = 0; r < gsize; r++) {
                        memcpy(dst + r * esz, src + idxs[r] * esz, esz);
                        copied_null |= ray_vec_is_null(full_col, idxs[r]);
                    }
                    /* Raw payload gathers do not carry vector metadata.  The
                     * aggregate kernels use HAS_NULLS as their fast-path gate,
                     * so preserve it when any gathered row is null. */
                    if (copied_null)
                        sub_col->attrs |= RAY_ATTR_HAS_NULLS;
                    /* STR descriptors of pooled strings point into the
                     * source's pool: share it, or every string past the
                     * inline size reads as "" inside the group. */
                    if (ct == RAY_STR)
                        col_propagate_str_pool(sub_col, full_col);
                    sub_tbl = ray_table_add_col(sub_tbl, cn, sub_col);
                    ray_release(sub_col);
                    if (RAY_IS_ERR(sub_tbl)) { ray_release(out_col); UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return sub_tbl; }
                }

                /* Evaluate expression on the sub-table: DAG first, then the
                 * eval-level fallback with the group's columns bound (an
                 * aggregate the DAG declines, e.g. over temporal + - *,
                 * used to fail here with "failed to compile"). */
                ray_t* agg_result = update_eval_on(sub_tbl, agg_expr);
                ray_release(sub_tbl);

                if (RAY_IS_ERR(agg_result)) { ray_release(out_col); UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return agg_result; }
                /* A one-element result is the group's scalar, broadcast like
                 * an atom; it failed the length check on groups of 2+ rows. */
                agg_result = update_scalar_vec(agg_result, gsize);

                /* Determine output type from first group */
                if (first_group) {
                    if (ray_is_atom(agg_result)) out_type = -agg_result->type;
                    else if (ray_is_vec(agg_result)) out_type = agg_result->type;
                    ray_release(out_col);
                    out_col = ray_vec_new(out_type, nrows2);
                    if (RAY_IS_ERR(out_col)) { ray_release(agg_result); UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return out_col; }
                    out_col->len = nrows2;
                    memset(ray_data(out_col), 0,
                        (size_t)nrows2 * (size_t)ray_sym_elem_size(out_col->type, out_col->attrs));
                    first_group = 0;
                }

                /* Scatter the group's result back to its rows.  An atom
                 * broadcasts to every row of the group; a per-row vector
                 * (e.g. `update {v: (* v 2) by: k}`) scatters elementwise
                 * through idxs[r], symmetric with the atom branch.  Any other
                 * shape — a vector whose length is neither 1 nor the group
                 * size — has no row-aligned meaning, so decline loudly rather
                 * than leave the memset zeros in place (silent data loss). */
                /* A group result of another type than the column was cast
                 * into it silently (2.5 stored as 2, a TIMESTAMP as a TIME).
                 * Promote the column to the wider numeric type; reject any
                 * other mismatch loudly. */
                {
                    int8_t rt = ray_is_atom(agg_result) ? (int8_t)-agg_result->type : agg_result->type;
                    if (rt != out_type && rt > 0) {
                        int rr = update_by_num_rank(rt), orr = update_by_num_rank(out_type);
                        if (!rr || !orr) {
                            ray_release(agg_result); ray_release(out_col);
                            UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv);
                            return ray_error("type", "update by: group result type %s does not match %s", ray_type_name(rt), ray_type_name(out_type));
                        }
                        if (rr > orr) {
                            ray_t* promoted = update_by_promote(out_col, rt);
                            if (RAY_IS_ERR(promoted)) { ray_release(agg_result); ray_release(out_col); UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return promoted; }
                            ray_release(out_col);
                            out_col = promoted;
                            out_type = rt;
                        }
                    }
                }
                int64_t* idxs = (int64_t*)ray_data(idx_vec);
                ray_t* store_err = NULL;
                if (ray_is_atom(agg_result)) {
                    for (int64_t r = 0; r < gsize && !store_err; r++) {
                        ray_t* nc = update_by_store(out_col, idxs[r], agg_result);
                        if (RAY_IS_ERR(nc)) store_err = nc; else out_col = nc;
                    }
                } else if (ray_is_vec(agg_result) && ray_len(agg_result) == gsize) {
                    for (int64_t r = 0; r < gsize && !store_err; r++) {
                        int alloc = 0;
                        ray_t* cell = collection_elem(agg_result, r, &alloc);
                        ray_t* nc = update_by_store(out_col, idxs[r], cell);
                        if (RAY_IS_ERR(nc)) store_err = nc; else out_col = nc;
                        if (alloc) ray_release(cell);
                    }
                } else {
                    int64_t got = ray_is_vec(agg_result) ? ray_len(agg_result) : -1;
                    ray_release(agg_result); ray_release(out_col);
                    UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv);
                    return ray_error("length", "update by: expression result length %lld does not match group size %lld", (long long)got, (long long)gsize);
                }
                ray_release(agg_result);
                if (store_err) {
                    ray_release(out_col);
                    UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv);
                    return store_err;
                }
            }

            upd_names[upd_i] = kid;
            upd_cols[upd_i] = out_col;
            upd_i++;
        }

        /* Build result in schema order: replace existing targets in place, then
         * append genuinely new update columns in dict order. */
        ray_t* result = ray_table_new((int32_t)(ncols + n_updates));
        if (RAY_IS_ERR(result)) { UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return result; }
        for (int64_t c = 0; c < ncols; c++) {
            int64_t cn = ray_table_col_name(tbl, c);
            int64_t ui = -1;
            for (int64_t u = 0; u < n_updates; u++) {
                if (upd_names[u] == cn) { ui = u; break; }
            }
            if (ui >= 0) {
                result = ray_table_add_col(result, cn, upd_cols[ui]);
                ray_release(upd_cols[ui]);
                upd_cols[ui] = NULL;
                upd_used[ui] = 1;
            } else {
                ray_t* col = ray_table_get_col_idx(tbl, c);
                ray_retain(col);
                result = ray_table_add_col(result, cn, col);
                ray_release(col);
            }
            if (RAY_IS_ERR(result)) { UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return result; }
        }
        for (int64_t u = 0; u < n_updates; u++) {
            if (upd_used[u]) continue;
            result = ray_table_add_col(result, upd_names[u], upd_cols[u]);
            ray_release(upd_cols[u]);
            upd_cols[u] = NULL;
            if (RAY_IS_ERR(result)) { UPDATE_BY_CLEANUP_COLS(); ray_release(groups); ray_release(tbl); DICT_VIEW_CLOSE(updv); return result; }
        }
        UPDATE_BY_CLEANUP_COLS();
        #undef UPDATE_BY_CLEANUP_COLS

        ray_release(groups);
        /* Store in-place and return the symbol if amending by name. */
        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            ray_env_set(inplace_sym, result);
            ray_release(result);
            ray_release(tbl);
            DICT_VIEW_CLOSE(updv); return ray_sym(inplace_sym);
        }
        ray_release(tbl);
        DICT_VIEW_CLOSE(updv); return result;
    }

    /* Evaluate WHERE using the DAG to get a boolean mask */
    int64_t nrows = ray_table_nrows(tbl);
    uint8_t* mask = NULL;

    if (where_expr) {
        bool fast_handled = false;
        ray_t* fast = update_where_inplace(tbl, inplace_sym, dict, where_expr, &fast_handled);
        if (fast_handled) { ray_release(tbl); return fast; }
    }

    if (where_expr) {
        /* Try DAG compilation first, fall back to eval-level */
        ray_t* mask_vec = NULL;
        bool rejected = false;
        ray_graph_t* g = ray_graph_new(tbl);
        if (g) {
            ray_op_t* pred = compile_expr_dag(g, where_expr);
            if (pred) {
                pred = ray_optimize(g, pred);
                mask_vec = ray_execute(g, pred);
            } else if (g->compile_err) {
                /* rejected outright, not declined: no interpreter retry */
                mask_vec = graph_take_compile_err(g);
                rejected = true;
            }
            ray_graph_free(g);
        }
        /* Fallback: eval-level predicate evaluation */
        if (!rejected && (!mask_vec || RAY_IS_ERR(mask_vec))) {
            /* Bind column names to column vectors in env, then eval */
            int64_t ncols2 = ray_table_ncols(tbl);
            ray_env_push_query_scope();
            for (int64_t c = 0; c < ncols2; c++) {
                int64_t cn = ray_table_col_name(tbl, c);
                ray_t* col = ray_table_get_col_idx(tbl, c);
                ray_env_set_query_local(cn, col);
            }
            mask_vec = ray_eval(where_expr);
            ray_env_pop_scope();
        }
        if (!mask_vec || RAY_IS_ERR(mask_vec)) { ray_release(tbl); return mask_vec ? mask_vec : ray_error("type", "update: `where:` predicate evaluation failed"); }
        mask_vec = where_mask_spread(mask_vec, nrows, "update");
        if (!mask_vec || RAY_IS_ERR(mask_vec)) { ray_release(tbl); return mask_vec ? mask_vec : ray_error("oom", NULL); }
        if (mask_vec->type != RAY_BOOL || mask_vec->len != nrows) {
            int8_t mask_t = mask_vec->type;            /* capture BEFORE free */
            ray_release(mask_vec);
            ray_release(tbl);
            return ray_error("type", "update: `where:` must produce a bool mask matching row count, got %s", ray_type_name(mask_t));
        }
        mask = (uint8_t*)ray_data(mask_vec);
        /* Keep mask_vec alive until we're done */

        /* Build a new table with updated columns */
        int64_t ncols = ray_table_ncols(tbl);
        DICT_VIEW_DECL(updw);
        DICT_VIEW_OPEN(dict, updw);
        if (DICT_VIEW_OVERFLOW(updw)) {
            ray_release(mask_vec); ray_release(tbl);
            DICT_VIEW_CLOSE(updw); return ray_error("oom", NULL);
        }
        int64_t dict_n = updw_n;
        ray_t** dict_elems = updw;
        int64_t from_id = ray_sym_intern("from", 4);
        int64_t where_id = ray_sym_intern("where", 5);

        ray_t* result = ray_table_new(ncols);
        if (RAY_IS_ERR(result)) { ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return result; }

        for (int64_t c = 0; c < ncols; c++) {
            int64_t col_name = ray_table_col_name(tbl, c);
            ray_t* orig_col = ray_table_get_col_idx(tbl, c);

            /* Check if this column has an update expression */
            ray_t* update_expr = NULL;
            for (int64_t d = 0; d + 1 < dict_n; d += 2) {
                int64_t kid = dict_elems[d]->i64;
                if (kid == from_id || kid == where_id) continue;
                if (kid == col_name) { update_expr = dict_elems[d + 1]; break; }
            }

            if (!update_expr) {
                /* No update for this column — copy as-is */
                ray_retain(orig_col);
                result = ray_table_add_col(result, col_name, orig_col);
                ray_release(orig_col);
            } else {
                /* Evaluate the expression for each row and apply to matching rows */
                int8_t ct = orig_col->type;
                ray_t* new_col = ray_vec_new(ct, nrows);
                if (RAY_IS_ERR(new_col)) { ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return new_col; }

                /* DAG first, then the interpreter (update_eval_on). */
                ray_t* expr_vec = update_eval_on(tbl, update_expr);
                if (!expr_vec || RAY_IS_ERR(expr_vec)) { ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return expr_vec ? expr_vec : ray_error("type", "update: failed to evaluate column update expression"); }
                if (expr_vec->type >= 0 && ray_len(expr_vec) != nrows) {
                    int64_t got = ray_len(expr_vec);
                    ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl);
                    DICT_VIEW_CLOSE(updw); return ray_error("length", "update: column has %lld values for %lld rows", (long long)got, (long long)nrows);
                }

                /* WHERE update: expression result replaces ONLY masked rows.
                 * When type differs (e.g., I64 col, F64 expr from (* col 1.1)),
                 * keep original column type and cast expr results.
                 * Only numeric promotions are allowed — STR↔numeric is a type error. */
                int8_t expr_type = (expr_vec->type < 0) ? -expr_vec->type : expr_vec->type;
                if (expr_type != ct && expr_type > 0 && ray_is_vec(expr_vec)) {
                    /* Only allow numeric promotions (I64↔F64, I32↔F64) */
                    int is_numeric_promo = (ct == RAY_I64 || ct == RAY_I32 || ct == RAY_F64) &&
                                           (expr_type == RAY_I64 || expr_type == RAY_I32 || expr_type == RAY_F64);
                    if (!is_numeric_promo) {
                        ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl);
                        DICT_VIEW_CLOSE(updw); return ray_error("type", "update: cannot assign %s expression to %s column", ray_type_name(expr_type), ray_type_name(ct));
                    }
                    /* Copy original column values first */
                    int esz = ray_elem_size(ct);
                    memcpy(ray_data(new_col), ray_data(orig_col), (size_t)(nrows * esz));
                    new_col->len = nrows;
                    /* Overlay masked rows with type conversion */
                    for (int64_t r = 0; r < nrows; r++) {
                        if (!mask[r]) continue;
                        if (ct == RAY_I64 && expr_type == RAY_F64)
                            ((int64_t*)ray_data(new_col))[r] = ray_cast_f64_to_i64_null(((double*)ray_data(expr_vec))[r]);
                        else if (ct == RAY_I32 && expr_type == RAY_F64)
                            ((int32_t*)ray_data(new_col))[r] = ray_cast_f64_to_i32_null(((double*)ray_data(expr_vec))[r]);
                        else if (ct == RAY_F64 && expr_type == RAY_I64)
                            ((double*)ray_data(new_col))[r] = (double)((int64_t*)ray_data(expr_vec))[r];
                    }
                    /* Null propagation: the memcpy above only copies values,
                     * so re-flag null rows here — orig_col's nulls for the
                     * untouched rows, expr_vec's nulls for the masked rows.
                     * Also overwrite the destination payload with the
                     * dest-width sentinel: casting a NaN/INT_MIN sentinel
                     * across widths produces implementation-defined garbage
                     * that wouldn't match the typed null encoding. */
                    for (int64_t r = 0; r < nrows; r++) {
                        ray_t* src = mask[r] ? expr_vec : orig_col;
                        if (ray_vec_is_null(src, r)) {
                            ray_vec_set_null(new_col, r, true);
                            switch (ct) {
                                case RAY_F64:                              ((double*)ray_data(new_col))[r]  = NULL_F64; break;
                                case RAY_I64: case RAY_TIMESTAMP:          ((int64_t*)ray_data(new_col))[r] = NULL_I64; break;
                                case RAY_I32: case RAY_DATE: case RAY_TIME:((int32_t*)ray_data(new_col))[r] = NULL_I32; break;
                                case RAY_I16:                              ((int16_t*)ray_data(new_col))[r] = NULL_I16; break;
                                default: break;
                            }
                        }
                    }
                    ray_release(expr_vec);
                    result = ray_table_add_col(result, col_name, new_col);
                    ray_release(new_col);
                    if (RAY_IS_ERR(result)) { ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return result; }
                    continue;
                }

                /* Broadcast scalar atom to full column vector if needed */
                if (expr_vec->type < 0) {
                    /* Type check atom against column type BEFORE broadcast */
                    int ok = (expr_vec->type == -ct);
                    if (!ok && ct == RAY_F64 && expr_vec->type == -RAY_I64) ok = 1;
                    if (!ok && ct == RAY_LIST && expr_vec->type == -RAY_SYM) ok = 1;
                    if (!ok && ct == RAY_SYM && expr_vec->type == -RAY_SYM) ok = 1;
                    if (!ok) {
                        int8_t ev_t = expr_vec->type;            /* capture BEFORE free */
                        ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl);
                        DICT_VIEW_CLOSE(updw); return ray_error("type", "update: cannot assign %s value to %s column", ray_type_name(ev_t), ray_type_name(ct));
                    }
                    /* SYM atom to LIST column: build boxed list, merge with mask */
                    if (ct == RAY_LIST && expr_vec->type == -RAY_SYM) {
                        ray_free(new_col);
                        ray_t* new_list = ray_list_new((int32_t)nrows);
                        if (RAY_IS_ERR(new_list)) { ray_release(expr_vec); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return new_list; }
                        ray_t** orig_elems = (ray_t**)ray_data(orig_col);
                        for (int64_t r = 0; r < nrows; r++) {
                            ray_t* elem = mask[r] ? expr_vec : orig_elems[r];
                            ray_retain(elem);
                            new_list = ray_list_append(new_list, elem);
                            ray_release(elem);
                            if (RAY_IS_ERR(new_list)) { ray_release(expr_vec); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return new_list; }
                        }
                        ray_release(expr_vec);
                        result = ray_table_add_col(result, col_name, new_list);
                        ray_release(new_list);
                        if (RAY_IS_ERR(result)) { ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return result; }
                        continue;
                    }
                    ray_t* bcast = ray_vec_new(ct, nrows);
                    if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return bcast; }
                    if (ct == RAY_STR && expr_vec->type == -RAY_STR) {
                        const char* sp = ray_str_ptr(expr_vec);
                        size_t sl = ray_str_len(expr_vec);
                        for (int64_t r = 0; r < nrows; r++) {
                            bcast = ray_str_vec_append(bcast, sp, sl);
                            if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return bcast; }
                        }
                    } else {
                        /* elem is wide enough for every fixed-width type incl.
                         * GUID (16 B), whose payload lives in ->obj — copying
                         * ray_elem_size(ct) bytes from ->i64 would over-read an
                         * 8-byte buffer and write the wrong source for GUID. */
                        uint8_t elem[16];
                        update_atom_cell(expr_vec, ct, ray_elem_size(ct), elem);
                        for (int64_t r = 0; r < nrows; r++) {
                            bcast = ray_vec_append(bcast, elem);
                            if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return bcast; }
                        }
                    }
                    /* Preserve typed-null markers across broadcast.  Without
                     * this, (update {a: 0N from: t}) silently writes plain
                     * zeros into the I64 column — the value bits get copied
                     * but HAS_NULLS is not set, so (nil? a) reports false
                     * on what should be null cells. */
                    if (RAY_ATOM_IS_NULL(expr_vec)) {
                        for (int64_t r = 0; r < nrows; r++)
                            ray_vec_set_null(bcast, r, true);
                        /* Fill the correct-width sentinel into the payload. */
                        switch (ct) {
                            case RAY_F64: {
                                double* d = (double*)ray_data(bcast);
                                for (int64_t r = 0; r < nrows; r++) d[r] = NULL_F64;
                                break;
                            }
                            case RAY_I64: case RAY_TIMESTAMP: {
                                int64_t* d = (int64_t*)ray_data(bcast);
                                for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I64;
                                break;
                            }
                            case RAY_I32: case RAY_DATE: case RAY_TIME: {
                                int32_t* d = (int32_t*)ray_data(bcast);
                                for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I32;
                                break;
                            }
                            case RAY_I16: {
                                int16_t* d = (int16_t*)ray_data(bcast);
                                for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I16;
                                break;
                            }
                            default: break;
                        }
                    }
                    ray_release(expr_vec);
                    expr_vec = bcast;
                }

                /* Promote I64 vector to F64 if column is F64 */
                if (expr_vec->type == RAY_I64 && ct == RAY_F64) {
                    int64_t nr = ray_len(expr_vec);
                    ray_t* promoted = ray_vec_new(RAY_F64, nr);
                    if (RAY_IS_ERR(promoted)) { ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return promoted; }
                    int64_t* src_data = (int64_t*)ray_data(expr_vec);
                    for (int64_t r = 0; r < nr; r++) {
                        double v = (double)src_data[r];
                        promoted = ray_vec_append(promoted, &v);
                        if (RAY_IS_ERR(promoted)) { ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return promoted; }
                    }
                    /* Carry nulls across the I64→F64 promotion and overwrite
                     * the slot with NULL_F64 (NaN) so the payload encodes null. */
                    double* dst = (double*)ray_data(promoted);
                    for (int64_t r = 0; r < nr; r++) {
                        if (ray_vec_is_null(expr_vec, r)) {
                            ray_vec_set_null(promoted, r, true);
                            dst[r] = NULL_F64;
                        }
                    }
                    ray_release(expr_vec);
                    expr_vec = promoted;
                }

                /* Type check: expr_vec must match original column type */
                if (expr_vec->type != ct) {
                    int8_t ev_t = expr_vec->type;            /* capture BEFORE free */
                    ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl);
                    DICT_VIEW_CLOSE(updw); return ray_error("type", "update: expression type %s does not match %s column", ray_type_name(ev_t), ray_type_name(ct));
                }

                /* Merge: use expr_vec for matching rows, orig_col for non-matching.
                 * Null-bit propagation applies to STR/SYM as well — a null in
                 * either the orig column (unmasked rows) or the expr (masked
                 * rows) must travel into new_col's null state. */
                if (ct == RAY_STR) {
                    for (int64_t r = 0; r < nrows; r++) {
                        ray_t* src_vec = mask[r] ? expr_vec : orig_col;
                        size_t slen = 0;
                        const char* sp = ray_str_vec_get(src_vec, r, &slen);
                        new_col = ray_str_vec_append(new_col, sp ? sp : "", sp ? slen : 0);
                        if (RAY_IS_ERR(new_col)) { ray_release(expr_vec); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return new_col; }
                        if (ray_vec_is_null(src_vec, r))
                            ray_vec_set_null(new_col, new_col->len - 1, true);
                    }
                } else if (ct == RAY_SYM) {
                    /* The merged output MIXES cells of TWO source vecs
                     * (orig_col / expr_vec) — re-express each cell in the
                     * runtime domain (raw-copy fast path while both are
                     * runtime-domain). */
                    for (int64_t r = 0; r < nrows; r++) {
                        ray_t* src_vec = mask[r] ? expr_vec : orig_col;
                        int64_t sym_val = sym_cell_runtime_id(src_vec, r);
                        new_col = ray_vec_append(new_col, &sym_val);
                        if (RAY_IS_ERR(new_col)) { ray_release(expr_vec); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return new_col; }
                        if (ray_vec_is_null(src_vec, r))
                            ray_vec_set_null(new_col, new_col->len - 1, true);
                    }
                } else {
                    /* Source stride must match the column's true element
                     * width: orig_col/expr_vec are typed buffers, so an I32
                     * column strides 4 bytes, not 8.  A hardcoded 8 read
                     * past-end and mis-indexed the surviving (unmasked) rows. */
                    size_t elem_sz = ray_elem_size(ct);
                    uint8_t* orig_data = (uint8_t*)ray_data(orig_col);
                    uint8_t* expr_data = (uint8_t*)ray_data(expr_vec);
                    for (int64_t r = 0; r < nrows; r++) {
                        ray_t* src_vec = mask[r] ? expr_vec : orig_col;
                        uint8_t* base  = mask[r] ? expr_data : orig_data;
                        new_col = ray_vec_append(new_col, base + r * elem_sz);
                        if (RAY_IS_ERR(new_col)) { ray_release(expr_vec); ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return new_col; }
                        /* Propagate null bit from whichever side supplied
                         * the value.  Without this, masking in a typed-null
                         * broadcast would copy zero bytes into the slot but
                         * leave the destination's null state clear → silent
                         * loss of null marker. */
                        if (ray_vec_is_null(src_vec, r))
                            ray_vec_set_null(new_col, new_col->len - 1, true);
                    }
                }
                result = ray_table_add_col(result, col_name, new_col);
                ray_release(new_col);
                ray_release(expr_vec);
            }
            if (RAY_IS_ERR(result)) { ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return result; }
        }

        /* New columns (in the dict, not in the table): the expression's
         * value on the matched rows and null on the rest — false / 0x00 for
         * BOOL and U8, which have no null.  The loop above walks the table's
         * columns only, so these used to be dropped without a word. */
        for (int64_t d = 0; d + 1 < dict_n; d += 2) {
            int64_t kid = dict_elems[d]->i64;
            if (kid == from_id || kid == where_id) continue;
            bool exists = false;
            for (int64_t c = 0; c < ncols; c++)
                if (ray_table_col_name(tbl, c) == kid) { exists = true; break; }
            if (exists) continue;
            ray_t* v = update_eval_on(tbl, dict_elems[d + 1]);
            if (v && !RAY_IS_ERR(v) && ray_is_atom(v)) {
                ray_t* wide;
                if (v->type == -RAY_STR) {
                    wide = broadcast_scalar(v, nrows);
                } else {
                    ray_t* n_obj = make_i64(nrows);
                    wide = n_obj ? ray_take_fn(v, n_obj) : ray_error("oom", NULL);
                    if (n_obj) ray_release(n_obj);
                }
                ray_release(v);
                v = wide;
            }
            if (v && !RAY_IS_ERR(v) && !ray_is_vec(v)) {
                int8_t vt = v->type;
                ray_release(v);
                v = ray_error("type", "update: a new column under where: must be a typed vector, got %s",
                              ray_type_name(vt));
            } else if (v && !RAY_IS_ERR(v) && v->len != nrows) {
                int64_t got = v->len;
                ray_release(v);
                v = ray_error("length", "update: new column has %lld values for %lld rows",
                              (long long)got, (long long)nrows);
            }
            if (v && !RAY_IS_ERR(v)) v = ray_cow(v);
            if (!v || RAY_IS_ERR(v)) {
                ray_release(result); ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw);
                return v ? v : ray_error("oom", NULL);
            }
            ray_t* nul = ray_typed_null((int8_t)-v->type);
            for (int64_t r = 0; r < nrows && nul && !RAY_IS_ERR(nul); r++)
                if (!mask[r]) store_typed_elem(v, r, nul);
            if (nul && !RAY_IS_ERR(nul)) ray_release(nul);
            result = ray_table_add_col(result, kid, v);
            ray_release(v);
            if (RAY_IS_ERR(result)) { ray_release(mask_vec); ray_release(tbl); DICT_VIEW_CLOSE(updw); return result; }
        }

        ray_release(mask_vec);
        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            ray_env_set(inplace_sym, result);
            ray_release(result);
            ray_release(tbl);
            DICT_VIEW_CLOSE(updw); return ray_sym(inplace_sym);
        }
        ray_release(tbl);
        DICT_VIEW_CLOSE(updw); return result;
    }

    /* No WHERE — update all rows */
    int64_t ncols = ray_table_ncols(tbl);
    DICT_VIEW_DECL(upda);
    DICT_VIEW_OPEN(dict, upda);
    if (DICT_VIEW_OVERFLOW(upda)) {
        ray_release(tbl);
        DICT_VIEW_CLOSE(upda); return ray_error("oom", NULL);
    }
    int64_t dict_n = upda_n;
    ray_t** dict_elems = upda;
    int64_t from_id = ray_sym_intern("from", 4);

    ray_t* result = ray_table_new(ncols);
    if (RAY_IS_ERR(result)) { ray_release(tbl); DICT_VIEW_CLOSE(upda); return result; }

    for (int64_t c = 0; c < ncols; c++) {
        int64_t col_name = ray_table_col_name(tbl, c);
        ray_t* orig_col = ray_table_get_col_idx(tbl, c);

        ray_t* update_expr = NULL;
        for (int64_t d = 0; d + 1 < dict_n; d += 2) {
            int64_t kid = dict_elems[d]->i64;
            if (kid == from_id) continue;
            if (kid == col_name) { update_expr = dict_elems[d + 1]; break; }
        }

        if (!update_expr) {
            ray_retain(orig_col);
            result = ray_table_add_col(result, col_name, orig_col);
            ray_release(orig_col);
        } else {
            /* DAG first, then the interpreter (update_eval_on). */
            ray_t* expr_vec = update_eval_on(tbl, update_expr);
            if (!expr_vec || RAY_IS_ERR(expr_vec)) { ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return expr_vec ? expr_vec : ray_error("type", "update: failed to evaluate column update expression"); }
            if (expr_vec->type >= 0 && ray_len(expr_vec) != ray_table_nrows(tbl)) {
                /* A column of another length has no row-aligned meaning; it
                 * used to be added as-is and left the table ragged. */
                int64_t got = ray_len(expr_vec);
                ray_release(expr_vec); ray_release(result); ray_release(tbl);
                DICT_VIEW_CLOSE(upda); return ray_error("length", "update: column has %lld values for %lld rows", (long long)got, (long long)ray_table_nrows(tbl));
            }

            /* Broadcast scalar atom to full column vector if needed */
            if (expr_vec->type < 0) {
                int64_t nrows = ray_table_nrows(tbl);
                int8_t ct = orig_col->type;
                /* Type check atom against column type BEFORE broadcast */
                int ok = (expr_vec->type == -ct);
                if (!ok && ct == RAY_F64 && expr_vec->type == -RAY_I64) ok = 1;
                /* SYM atom → LIST column (LIST of SYM atoms) */
                if (!ok && ct == RAY_LIST && expr_vec->type == -RAY_SYM) ok = 1;
                if (!ok) {
                    int8_t ev_t = expr_vec->type;            /* capture BEFORE free */
                    ray_release(expr_vec); ray_release(result); ray_release(tbl);
                    DICT_VIEW_CLOSE(upda); return ray_error("type", "update: cannot assign %s value to %s column", ray_type_name(ev_t), ray_type_name(ct));
                }
                /* SYM atom to LIST column: broadcast as boxed list */
                if (ct == RAY_LIST && expr_vec->type == -RAY_SYM) {
                    ray_t* bcast = ray_list_new((int32_t)nrows);
                    if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                    for (int64_t r = 0; r < nrows; r++) {
                        ray_retain(expr_vec);
                        bcast = ray_list_append(bcast, expr_vec);
                        ray_release(expr_vec);
                        if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                    }
                    ray_release(expr_vec);
                    expr_vec = bcast;
                    goto no_where_add_col;
                }
                ray_t* bcast = ray_vec_new(ct, nrows);
                if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                if (ct == RAY_STR && expr_vec->type == -RAY_STR) {
                    const char* sp = ray_str_ptr(expr_vec);
                    size_t sl = ray_str_len(expr_vec);
                    for (int64_t r = 0; r < nrows; r++) {
                        bcast = ray_str_vec_append(bcast, sp, sl);
                        if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                    }
                } else {
                    /* Wide enough for every fixed-width type incl. GUID (16 B,
                     * payload in ->obj); ray_elem_size(ct) bytes from ->i64
                     * would over-read an 8-byte buffer for GUID. */
                    uint8_t elem[16];
                    update_atom_cell(expr_vec, ct, ray_elem_size(ct), elem);
                    for (int64_t r = 0; r < nrows; r++) {
                        bcast = ray_vec_append(bcast, elem);
                        if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                    }
                }
                /* Preserve typed-null markers across broadcast (mirrors the
                 * WHERE branch fix at the analogous site above). */
                if (RAY_ATOM_IS_NULL(expr_vec)) {
                    for (int64_t r = 0; r < nrows; r++)
                        ray_vec_set_null(bcast, r, true);
                    /* Fill the correct-width sentinel into the payload. */
                    switch (ct) {
                        case RAY_F64: {
                            double* d = (double*)ray_data(bcast);
                            for (int64_t r = 0; r < nrows; r++) d[r] = NULL_F64;
                            break;
                        }
                        case RAY_I64: case RAY_TIMESTAMP: {
                            int64_t* d = (int64_t*)ray_data(bcast);
                            for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I64;
                            break;
                        }
                        case RAY_I32: case RAY_DATE: case RAY_TIME: {
                            int32_t* d = (int32_t*)ray_data(bcast);
                            for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I32;
                            break;
                        }
                        case RAY_I16: {
                            int16_t* d = (int16_t*)ray_data(bcast);
                            for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I16;
                            break;
                        }
                        default: break;
                    }
                }
                ray_release(expr_vec);
                expr_vec = bcast;
            }

            /* Promote I64 vector to F64 if column is F64 */
            if (expr_vec->type == RAY_I64 && orig_col->type == RAY_F64) {
                int64_t nr = ray_len(expr_vec);
                ray_t* promoted = ray_vec_new(RAY_F64, nr);
                if (RAY_IS_ERR(promoted)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return promoted; }
                int64_t* src_data = (int64_t*)ray_data(expr_vec);
                for (int64_t r = 0; r < nr; r++) {
                    double v = (double)src_data[r];
                    promoted = ray_vec_append(promoted, &v);
                    if (RAY_IS_ERR(promoted)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return promoted; }
                }
                /* Carry nulls across the I64→F64 promotion and overwrite
                 * the slot with NULL_F64 (NaN) so the payload encodes null. */
                double* dst = (double*)ray_data(promoted);
                for (int64_t r = 0; r < nr; r++) {
                    if (ray_vec_is_null(expr_vec, r)) {
                        ray_vec_set_null(promoted, r, true);
                        dst[r] = NULL_F64;
                    }
                }
                ray_release(expr_vec);
                expr_vec = promoted;
            }

            /* No-WHERE update: allow type change for same-category types.
             * Atoms (type<0) will be broadcast later, check after broadcast.
             * For vectors, check now: only numeric promotions or same type.
             * Also allow SYM/LIST interop (columns may be stored as LIST). */
            if (expr_vec->type > 0 && expr_vec->type != orig_col->type) {
                int is_ok = 0;
                /* Numeric promotions */
                if ((orig_col->type == RAY_I64 || orig_col->type == RAY_I32 || orig_col->type == RAY_F64) &&
                    (expr_vec->type == RAY_I64 || expr_vec->type == RAY_I32 || expr_vec->type == RAY_F64))
                    is_ok = 1;
                /* SYM/LIST interop */
                if ((orig_col->type == RAY_SYM || orig_col->type == RAY_LIST) &&
                    (expr_vec->type == RAY_SYM || expr_vec->type == RAY_LIST))
                    is_ok = 1;
                if (!is_ok) {
                    int8_t ev_t = expr_vec->type, oc_t = orig_col->type;            /* capture BEFORE free */
                    ray_release(expr_vec); ray_release(result); ray_release(tbl);
                    DICT_VIEW_CLOSE(upda); return ray_error("type", "update: expression type %s does not match %s column", ray_type_name(ev_t), ray_type_name(oc_t));
                }
            }

no_where_add_col:
            result = ray_table_add_col(result, col_name, expr_vec);
            ray_release(expr_vec);
        }
        if (RAY_IS_ERR(result)) { ray_release(tbl); DICT_VIEW_CLOSE(upda); return result; }
    }

    /* Add NEW columns from dict (columns not already in the table) */
    for (int64_t d = 0; d + 1 < dict_n; d += 2) {
        int64_t kid = dict_elems[d]->i64;
        if (kid == from_id) continue;
        /* Check if this column already exists */
        int exists = 0;
        for (int64_t c = 0; c < ncols; c++) {
            if (ray_table_col_name(tbl, c) == kid) { exists = 1; break; }
        }
        if (exists) continue;

        /* New column: evaluate expression and add.  DAG first, then the
         * eval-level fallback with column bindings — the same sequence an
         * update of an EXISTING column takes (update_eval_on).  This site
         * used to fail outright when the DAG declined the expression
         * (temporal + - *), so `x: (+ ts 1)` worked as a select and as an
         * existing-column update but not as a new column. */
        ray_t* update_expr = dict_elems[d + 1];
        ray_t* expr_vec = update_eval_on(tbl, update_expr);
        if (!expr_vec || RAY_IS_ERR(expr_vec)) { ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return expr_vec ? expr_vec : ray_error("type", "update: failed to evaluate new column expression"); }
        if (expr_vec->type >= 0 && ray_len(expr_vec) != ray_table_nrows(tbl)) {
            /* A column of another length has no row-aligned meaning; it
             * used to be added as-is and left the table ragged (the where:
             * arm checks the same). */
            int64_t got = ray_len(expr_vec);
            ray_release(expr_vec); ray_release(result); ray_release(tbl);
            DICT_VIEW_CLOSE(upda); return ray_error("length", "update: new column has %lld values for %lld rows", (long long)got, (long long)ray_table_nrows(tbl));
        }

        /* Broadcast scalar to column */
        if (expr_vec->type < 0) {
            int64_t nrows = ray_table_nrows(tbl);
            int8_t ct = -expr_vec->type;
            ray_t* bcast = ray_vec_new(ct, nrows);
            if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
            if (ct == RAY_STR) {
                const char* sp = (expr_vec->type == -RAY_STR) ? ray_str_ptr(expr_vec) : "";
                size_t sl = (expr_vec->type == -RAY_STR) ? ray_str_len(expr_vec) : 0;
                for (int64_t r = 0; r < nrows; r++) {
                    bcast = ray_str_vec_append(bcast, sp, sl);
                    if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                }
            } else {
                /* elem holds any fixed-width payload incl. GUID's 16 B (in
                 * ->obj); copying from ->i64 would be wrong/over-read for GUID. */
                uint8_t elem[16];
                update_atom_cell(expr_vec, ct, ray_elem_size(ct), elem);
                for (int64_t r = 0; r < nrows; r++) {
                    bcast = ray_vec_append(bcast, elem);
                    if (RAY_IS_ERR(bcast)) { ray_release(expr_vec); ray_release(result); ray_release(tbl); DICT_VIEW_CLOSE(upda); return bcast; }
                }
            }
            /* Preserve typed-null markers across broadcast (mirrors the
             * existing-column branches above).  Without this,
             * (update {c: 0N from: t}) would silently materialise a
             * brand-new column of plain zeros. */
            if (RAY_ATOM_IS_NULL(expr_vec)) {
                for (int64_t r = 0; r < nrows; r++)
                    ray_vec_set_null(bcast, r, true);
                /* Fill the correct-width sentinel into the payload. */
                switch (ct) {
                    case RAY_F64: {
                        double* d = (double*)ray_data(bcast);
                        for (int64_t r = 0; r < nrows; r++) d[r] = NULL_F64;
                        break;
                    }
                    case RAY_I64: case RAY_TIMESTAMP: {
                        int64_t* d = (int64_t*)ray_data(bcast);
                        for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I64;
                        break;
                    }
                    case RAY_I32: case RAY_DATE: case RAY_TIME: {
                        int32_t* d = (int32_t*)ray_data(bcast);
                        for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I32;
                        break;
                    }
                    case RAY_I16: {
                        int16_t* d = (int16_t*)ray_data(bcast);
                        for (int64_t r = 0; r < nrows; r++) d[r] = NULL_I16;
                        break;
                    }
                    default: break;
                }
            }
            ray_release(expr_vec);
            expr_vec = bcast;
        }

        result = ray_table_add_col(result, kid, expr_vec);
        ray_release(expr_vec);
        if (RAY_IS_ERR(result)) { ray_release(tbl); DICT_VIEW_CLOSE(upda); return result; }
    }

    /* Store in-place and return the symbol if amending by name (from: 't). */
    if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
        ray_env_set(inplace_sym, result);
        ray_release(result);
        ray_release(tbl);
        DICT_VIEW_CLOSE(upda); return ray_sym(inplace_sym);
    }
    ray_release(tbl);
    DICT_VIEW_CLOSE(upda); return result;
}

/* A `.db.parted.get` table is a deliberately narrow shape: one leading
 * MAPCOMMON virtual column followed by one PARTED wrapper per physical
 * column.  Keep detection separate from validation so arity-2 insert and
 * upsert can reject parted values before any wrapper is mistaken for flat
 * vector storage. */
static bool table_has_parted_columns(ray_t* tbl) {
    if (!tbl || RAY_IS_ERR(tbl) || tbl->type != RAY_TABLE) return false;
    int64_t ncols = ray_table_ncols(tbl);
    for (int64_t c = 0; c < ncols; c++) {
        ray_t* col = ray_table_get_col_idx(tbl, c);
        if (col && (col->type == RAY_MAPCOMMON || RAY_IS_PARTED(col->type)))
            return true;
    }
    return false;
}

/* A fresh table with every parted / mapcommon column of `tbl` flattened
 * (query_materialize_parted_col); plain columns are shared.  Owned. */
static ray_t* query_materialize_parted_table(ray_t* tbl) {
    int64_t nc = ray_table_ncols(tbl);
    ray_t* flat_tbl = ray_table_new(nc);
    if (!flat_tbl || RAY_IS_ERR(flat_tbl)) return flat_tbl ? flat_tbl : ray_error("oom", NULL);
    for (int64_t c = 0; c < nc; c++) {
        ray_t* flat_col = query_materialize_parted_col(ray_table_get_col_idx(tbl, c));
        if (!flat_col || RAY_IS_ERR(flat_col)) {
            ray_release(flat_tbl);
            return flat_col ? flat_col : ray_error("oom", NULL);
        }
        flat_tbl = ray_table_add_col(flat_tbl, ray_table_col_name(tbl, c), flat_col);
        ray_release(flat_col);
        if (!flat_tbl || RAY_IS_ERR(flat_tbl)) return flat_tbl ? flat_tbl : ray_error("oom", NULL);
    }
    return flat_tbl;
}

typedef struct {
    ray_t* mapcommon;              /* borrowed */
    ray_t* key_values;             /* borrowed */
    ray_t* row_counts;             /* borrowed */
    int64_t ncols;
    int64_t ndata;
    int64_t nparts;
    int64_t total_rows;
    bool active_missing;
    int8_t key_type;
    struct ray_sym_domain_s* sym_dom; /* borrowed, shared by every data SYM */
    ray_t* sym_rep;                /* borrowed SYM segment for domain adoption */
} parted_insert_view_t;

static int byte_string_cmp(const char* a, size_t an,
                           const char* b, size_t bn) {
    size_t n = an < bn ? an : bn;
    int cmp = n ? memcmp(a, b, n) : 0;
    if (cmp < 0) return -1;
    if (cmp > 0) return 1;
    return (an > bn) - (an < bn);
}

/* Compare two MAPCOMMON cells under their logical key type. */
static int parted_key_cells_cmp(ray_t* keys, int64_t a, int64_t b,
                                bool* valid) {
    *valid = true;
    if (keys->type == RAY_DATE) {
        int32_t av = ((int32_t*)ray_data(keys))[a];
        int32_t bv = ((int32_t*)ray_data(keys))[b];
        if (av == NULL_I32 || bv == NULL_I32) { *valid = false; return 0; }
        return (av > bv) - (av < bv);
    }
    if (keys->type == RAY_I64) {
        int64_t av = ((int64_t*)ray_data(keys))[a];
        int64_t bv = ((int64_t*)ray_data(keys))[b];
        if (av == NULL_I64 || bv == NULL_I64) { *valid = false; return 0; }
        return (av > bv) - (av < bv);
    }
    if (keys->type == RAY_SYM) {
        ray_t* av = ray_sym_vec_cell(keys, a);
        ray_t* bv = ray_sym_vec_cell(keys, b);
        if (!av || !bv || ray_str_len(av) == 0 || ray_str_len(bv) == 0) {
            *valid = false;
            return 0;
        }
        return byte_string_cmp(ray_str_ptr(av), ray_str_len(av),
                               ray_str_ptr(bv), ray_str_len(bv));
    }
    *valid = false;
    return 0;
}

/* Compare an explicit runtime-domain key atom with one MAPCOMMON cell. */
static int parted_key_atom_cmp(ray_t* key, ray_t* keys, int64_t cell,
                               bool* valid) {
    *valid = true;
    if (keys->type == RAY_DATE) {
        int32_t cv = ((int32_t*)ray_data(keys))[cell];
        if (cv == NULL_I32 || RAY_ATOM_IS_NULL(key)) { *valid = false; return 0; }
        return (key->i32 > cv) - (key->i32 < cv);
    }
    if (keys->type == RAY_I64) {
        int64_t cv = ((int64_t*)ray_data(keys))[cell];
        if (cv == NULL_I64 || RAY_ATOM_IS_NULL(key)) { *valid = false; return 0; }
        return (key->i64 > cv) - (key->i64 < cv);
    }
    if (keys->type == RAY_SYM) {
        ray_t* ka = ray_sym_str(key->i64);
        ray_t* cv = ray_sym_vec_cell(keys, cell);
        if (!ka || !cv || ray_str_len(ka) == 0 || ray_str_len(cv) == 0) {
            *valid = false;
            return 0;
        }
        return byte_string_cmp(ray_str_ptr(ka), ray_str_len(ka),
                               ray_str_ptr(cv), ray_str_len(cv));
    }
    *valid = false;
    return 0;
}

static bool parted_segment_attrs_valid(int8_t base, uint8_t attrs) {
    uint8_t allowed = base == RAY_LIST
                    ? 0
                    : (RAY_ATTR_HAS_INDEX | RAY_ATTR_SORTED);
    if (base == RAY_SYM) allowed |= RAY_SYM_W_MASK;
    if (base == RAY_I32 || base == RAY_I64)
        allowed |= RAY_ATTR_HAS_LINK;
    /* SYM/STR nulls (the empty symbol / empty string) are in-band — the bit
     * only marks their presence, the payload is unchanged — and the store
     * carries it across a save, so every mapped partition holding one blank
     * cell arrives here with it set. */
    if (base == RAY_I16 || base == RAY_I32 || base == RAY_I64 ||
        base == RAY_F32 || base == RAY_F64 || base == RAY_DATE ||
        base == RAY_TIME || base == RAY_TIMESTAMP || base == RAY_GUID ||
        base == RAY_SYM || base == RAY_STR)
        allowed |= RAY_ATTR_HAS_NULLS;
    return (attrs & (uint8_t)~allowed) == 0;
}

/* Validate the complete canonical representation before looking at payload
 * values.  A NULL segment is tolerated only in the current active partition,
 * where a same-key non-empty append can materialise it.  Missing historical
 * segments are unqueryable by segmented execution and must never survive into
 * a newly returned live-tail snapshot. */
static ray_t* validate_parted_insert_target(ray_t* tbl,
                                            parted_insert_view_t* out) {
    memset(out, 0, sizeof(*out));
    if (tbl->len != 2 || tbl->attrs != 0)
        return ray_error("corrupt", "insert: parted target has non-canonical table metadata");
    ray_t** table_slots = (ray_t**)ray_data(tbl);
    ray_t* schema = table_slots[0];
    ray_t* columns = table_slots[1];
    if (!schema || RAY_IS_ERR(schema) || schema->type != RAY_I64 ||
        schema->attrs != 0 || schema->len < 0 ||
        !columns || RAY_IS_ERR(columns) || columns->type != RAY_LIST ||
        columns->attrs != 0 || columns->len != schema->len)
        return ray_error("corrupt", "insert: parted target has a malformed table container");
    int64_t ncols = ray_table_ncols(tbl);
    if (ncols < 2)
        return ray_error("type", "insert: parted target must have a MAPCOMMON column and at least one physical column");

    ray_t* mc = ray_table_get_col_idx(tbl, 0);
    if (!mc || mc->type != RAY_MAPCOMMON || mc->len != 2 ||
        mc->attrs > RAY_MC_I64)
        return ray_error("type", "insert: three-argument table form requires a canonical .db.parted.get table");

    ray_t** mp = (ray_t**)ray_data(mc);
    ray_t* keys = mp[0];
    ray_t* counts = mp[1];
    int8_t key_type = mc->attrs == RAY_MC_DATE ? RAY_DATE
                    : mc->attrs == RAY_MC_I64  ? RAY_I64
                    :                            RAY_SYM;
    uint8_t key_attrs = key_type == RAY_SYM ? RAY_SYM_W64 : 0;
    if (!keys || RAY_IS_ERR(keys) || keys->type != key_type ||
        keys->attrs != key_attrs ||
        !counts || RAY_IS_ERR(counts) || counts->type != RAY_I64 ||
        counts->attrs != 0 ||
        keys->len <= 0 || counts->len != keys->len)
        return ray_error("corrupt", "insert: malformed MAPCOMMON partition metadata");
    if (key_type == RAY_SYM &&
        ray_sym_vec_domain(keys) != ray_sym_runtime_domain())
        return ray_error("corrupt", "insert: MAPCOMMON symbol keys must use the runtime domain");

    /* Schema names must be unambiguous before exact named payload gathering. */
    for (int64_t c = 0; c < ncols; c++) {
        int64_t name = ray_table_col_name(tbl, c);
        if (name < 0)
            return ray_error("corrupt", "insert: parted target has an invalid column name at index %lld", (long long)c);
        for (int64_t d = c + 1; d < ncols; d++)
            if (ray_table_col_name(tbl, d) == name)
                return ray_error("corrupt", "insert: parted target has duplicate column names");
    }

    int64_t nparts = keys->len;
    int64_t* rc = (int64_t*)ray_data(counts);
    int64_t total = 0;
    for (int64_t p = 0; p < nparts; p++) {
        if (rc[p] < 0 || rc[p] == NULL_I64 || total > INT64_MAX - rc[p])
            return ray_error("corrupt", "insert: invalid MAPCOMMON row count at partition %lld", (long long)p);
        total += rc[p];
        if (p > 0) {
            bool valid = false;
            int cmp = parted_key_cells_cmp(keys, p - 1, p, &valid);
            if (!valid || cmp >= 0)
                return ray_error("corrupt", "insert: MAPCOMMON partition keys are not strictly increasing");
        } else if (key_type == RAY_DATE) {
            if (((int32_t*)ray_data(keys))[0] == NULL_I32)
                return ray_error("corrupt", "insert: MAPCOMMON contains a null partition key");
        } else if (key_type == RAY_I64) {
            if (((int64_t*)ray_data(keys))[0] == NULL_I64)
                return ray_error("corrupt", "insert: MAPCOMMON contains a null partition key");
        } else {
            ray_t* first = ray_sym_vec_cell(keys, 0);
            if (!first || ray_str_len(first) == 0)
                return ray_error("corrupt", "insert: MAPCOMMON contains an invalid partition key");
        }
    }

    struct ray_sym_domain_s* common_dom = NULL;
    ray_t* common_rep = NULL;
    bool active_missing = false;
    for (int64_t c = 1; c < ncols; c++) {
        ray_t* wrapper = ray_table_get_col_idx(tbl, c);
        if (!wrapper || !RAY_IS_PARTED(wrapper->type) ||
            wrapper->len != nparts || wrapper->attrs != 0)
            return ray_error("corrupt", "insert: physical column %lld is not a canonical PARTED wrapper", (long long)(c - 1));
        int8_t base = (int8_t)RAY_PARTED_BASETYPE(wrapper->type);
        if (base != RAY_LIST && (base < RAY_BOOL || base > RAY_STR))
            return ray_error("type", "insert: unsupported parted base type in physical column %lld", (long long)(c - 1));

        ray_t** segs = (ray_t**)ray_data(wrapper);
        /* ray_read_parted derives every wrapper from partition zero.  Requiring
         * that representative distinguishes this canonical shape from a table
         * assembled manually out of wrapper-like objects. */
        if (!segs[0])
            return ray_error("corrupt", "insert: physical column %lld has no first-partition segment", (long long)(c - 1));
        for (int64_t p = 0; p < nparts; p++) {
            ray_t* seg = segs[p];
            if (!seg) {
                if (p != nparts - 1)
                    return ray_error("corrupt", "insert: physical column %lld is missing historical partition %lld",
                                     (long long)(c - 1), (long long)p);
                active_missing = true;
                continue;
            }
            if (RAY_IS_ERR(seg) || seg->type != base ||
                !parted_segment_attrs_valid(base, seg->attrs) ||
                seg->len != rc[p])
                return ray_error("corrupt", "insert: segment metadata/length/type mismatch in physical column %lld partition %lld",
                                 (long long)(c - 1), (long long)p);
            if (seg->attrs & RAY_ATTR_HAS_INDEX) {
                ray_t* index = seg->index;
                if (!index || (uintptr_t)index <= 31 ||
                    RAY_IS_ERR(index) || index->type != RAY_INDEX)
                    return ray_error("corrupt", "insert: physical column %lld partition %lld has malformed index metadata",
                                     (long long)(c - 1), (long long)p);
            }
            if (base == RAY_SYM) {
                struct ray_sym_domain_s* dom = ray_sym_vec_domain(seg);
                if (dom == ray_sym_runtime_domain() || !ray_sym_domain_path(dom))
                    return ray_error("corrupt", "insert: parted SYM segments must use a FILE domain");
                if (!common_dom) { common_dom = dom; common_rep = seg; }
                else if (common_dom != dom)
                    return ray_error("corrupt", "insert: parted SYM segments do not share one FILE domain");
            }
        }
    }

    if (common_dom) {
        ray_t* empty = ray_sym_domain_str(common_dom, 0);
        if (ray_sym_domain_count(common_dom) <= 0 || !empty || ray_str_len(empty) != 0)
            return ray_error("corrupt", "insert: parted FILE symbol domain lacks the reserved empty symbol");
    }

    out->mapcommon = mc;
    out->key_values = keys;
    out->row_counts = counts;
    out->ncols = ncols;
    out->ndata = ncols - 1;
    out->nparts = nparts;
    out->total_rows = total;
    out->active_missing = active_missing;
    out->key_type = key_type;
    out->sym_dom = common_dom;
    out->sym_rep = common_rep;
    return NULL;
}

static ray_t* decide_parted_insert_partition(parted_insert_view_t* v,
                                             ray_t* key,
                                             bool* new_partition) {
    if (!key || RAY_IS_ERR(key) || RAY_IS_NULL(key) ||
        key->type != -v->key_type || RAY_ATOM_IS_NULL(key))
        return ray_error("type", "insert: partition key must be a non-null %s atom, got %s",
                         ray_type_name(-v->key_type),
                         key ? ray_type_name(key->type) : "null");
    bool valid = false;
    int cmp = parted_key_atom_cmp(key, v->key_values, v->nparts - 1, &valid);
    if (!valid)
        return ray_error("domain", "insert: partition key is not representable in the target key domain");
    if (cmp < 0)
        return ray_error("domain", "insert: partition key is historical/out of order; only the last or a later partition may grow");
    *new_partition = cmp > 0;
    return NULL;
}

static ray_t* parted_values_append(ray_t* vals, ray_t* value, bool owned) {
    if (!value || RAY_IS_ERR(value)) {
        if (owned && value && !RAY_IS_ERR(value)) ray_release(value);
        ray_release(vals);
        return value && RAY_IS_ERR(value) ? value
             : ray_error("type", "insert: physical payload contains a missing value");
    }
    ray_t* next = ray_list_append(vals, value);
    if (owned) ray_release(value);
    if (!next || RAY_IS_ERR(next)) {
        ray_release(vals);
        return next ? next : ray_error("oom", NULL);
    }
    return next;
}

/* Normalize LIST/TABLE/DICT payloads into physical target-column order.  The
 * returned list owns one ref per value; all named forms are exact and unique. */
static ray_t* normalize_parted_insert_rows(ray_t* tbl,
                                           parted_insert_view_t* v,
                                           ray_t* rows) {
    if (!rows || RAY_IS_ERR(rows) ||
        (rows->type != RAY_LIST && rows->type != RAY_TABLE &&
         rows->type != RAY_DICT))
        return ray_error("type", "insert: parted rows must be a list, table or dict, got %s",
                         rows ? ray_type_name(rows->type) : "null");

    ray_t* vals = ray_list_new(v->ndata);
    if (!vals || RAY_IS_ERR(vals)) return vals ? vals : ray_error("oom", NULL);

    if (rows->type == RAY_LIST) {
        if (rows->len != v->ndata) {
            ray_release(vals);
            return ray_error("domain", "insert: parted row list has %lld values, expected %lld physical columns",
                             (long long)rows->len, (long long)v->ndata);
        }
        ray_t** items = (ray_t**)ray_data(rows);
        for (int64_t c = 0; c < v->ndata; c++) {
            ray_t* wrapper = ray_table_get_col_idx(tbl, c + 1);
            if (RAY_PARTED_BASETYPE(wrapper->type) == RAY_LIST) {
                ray_t* one = ray_list_new(1);
                if (!one || RAY_IS_ERR(one)) {
                    ray_release(vals);
                    return one ? one : ray_error("oom", NULL);
                }
                one = ray_list_append(one, items[c]);
                if (!one || RAY_IS_ERR(one)) {
                    ray_release(vals);
                    return one ? one : ray_error("oom", NULL);
                }
                vals = parted_values_append(vals, one, true);
            } else {
                vals = parted_values_append(vals, items[c], false);
            }
            if (!vals || RAY_IS_ERR(vals)) return vals;
        }
        return vals;
    }

    if (rows->type == RAY_TABLE) {
        int64_t src_n = ray_table_ncols(rows);
        if (src_n != v->ndata) {
            ray_release(vals);
            return ray_error("domain", "insert: parted row table has %lld columns, expected exactly %lld physical columns",
                             (long long)src_n, (long long)v->ndata);
        }
        for (int64_t sc = 0; sc < src_n; sc++) {
            int64_t name = ray_table_col_name(rows, sc);
            int64_t src_matches = 0, target_matches = 0;
            for (int64_t i = 0; i < src_n; i++)
                if (ray_table_col_name(rows, i) == name) src_matches++;
            for (int64_t i = 1; i < v->ncols; i++)
                if (ray_table_col_name(tbl, i) == name) target_matches++;
            if (src_matches != 1 || target_matches != 1) {
                ray_release(vals);
                return ray_error("value", "insert: parted row table schema must exactly and uniquely match physical columns");
            }
        }
        for (int64_t c = 1; c < v->ncols; c++) {
            ray_t* col = ray_table_get_col(rows, ray_table_col_name(tbl, c));
            vals = parted_values_append(vals, col, false);
            if (!vals || RAY_IS_ERR(vals)) return vals;
        }
        return vals;
    }

    ray_t* keys = ray_dict_keys(rows);
    ray_t* dvals = ray_dict_vals(rows);
    if (!keys || keys->type != RAY_SYM || !dvals ||
        (dvals->type != RAY_LIST && !ray_is_vec(dvals)) ||
        keys->len != v->ndata || dvals->len != keys->len) {
        ray_release(vals);
        return ray_error("type", "insert: parted row dict must have one symbol key/value per physical column");
    }
    for (int64_t d = 0; d < keys->len; d++) {
        int64_t name = sym_cell_runtime_id(keys, d);
        int64_t dict_matches = 0, target_matches = 0;
        for (int64_t i = 0; i < keys->len; i++)
            if (sym_cell_runtime_id(keys, i) == name) dict_matches++;
        for (int64_t i = 1; i < v->ncols; i++)
            if (ray_table_col_name(tbl, i) == name) target_matches++;
        if (name < 0 || dict_matches != 1 || target_matches != 1) {
            ray_release(vals);
            return ray_error("value", "insert: parted row dict schema must exactly and uniquely match physical columns");
        }
    }
    for (int64_t c = 1; c < v->ncols; c++) {
        int64_t want = ray_table_col_name(tbl, c);
        int64_t found = -1;
        for (int64_t d = 0; d < keys->len; d++)
            if (sym_cell_runtime_id(keys, d) == want) { found = d; break; }
        if (found < 0) {
            ray_release(vals);
            return ray_error("value", "insert: parted row dict is missing a physical column");
        }
        if (dvals->type == RAY_LIST) {
            ray_t* item = ((ray_t**)ray_data(dvals))[found];
            ray_t* wrapper = ray_table_get_col_idx(tbl, c);
            if (RAY_PARTED_BASETYPE(wrapper->type) == RAY_LIST &&
                item && item->type == RAY_LIST) {
                ray_t* one = ray_list_new(1);
                if (!one || RAY_IS_ERR(one)) {
                    ray_release(vals);
                    return one ? one : ray_error("oom", NULL);
                }
                one = ray_list_append(one, item);
                if (!one || RAY_IS_ERR(one)) {
                    ray_release(vals);
                    return one ? one : ray_error("oom", NULL);
                }
                vals = parted_values_append(vals, one, true);
            } else {
                vals = parted_values_append(vals, item, false);
            }
        } else {
            int owned = 0;
            ray_t* item = collection_elem(dvals, found, &owned);
            vals = parted_values_append(vals, item, owned != 0);
        }
        if (!vals || RAY_IS_ERR(vals)) return vals;
    }
    return vals;
}

static bool parted_value_is_scalar(ray_t* v) {
    return v && !RAY_IS_ERR(v) &&
           (RAY_IS_NULL(v) || (v->type != RAY_LIST && !ray_is_vec(v)));
}

static bool parted_source_vec_compatible(int8_t dst, int8_t src) {
    return src == dst;
}

static ray_t* validate_parted_payload_atom(int8_t dst, ray_t* atom,
                                           int64_t physical_col) {
    if (!atom || RAY_IS_ERR(atom) ||
        (!RAY_IS_NULL(atom) && atom->type >= 0))
        return ray_error("type", "insert: physical column %lld payload cells must be atoms",
                         (long long)physical_col);
    if (RAY_IS_NULL(atom)) {
        if (dst == RAY_BOOL || dst == RAY_U8)
            return ray_error("type", "insert: physical column %lld (%s) has no null representation",
                             (long long)physical_col, ray_type_name(dst));
        return NULL;
    }
    if (atom->type != -dst)
        return ray_error("type", "insert: value type must exactly match physical column %lld (%s), got %s",
                         (long long)physical_col, ray_type_name(dst),
                         ray_type_name(atom->type));
    if (dst == RAY_GUID &&
        (!atom->obj || RAY_IS_ERR(atom->obj) ||
         atom->obj->type != RAY_U8 || atom->obj->len != 16))
        return ray_error("corrupt", "insert: GUID payload atom must own exactly 16 U8 bytes");
    if (RAY_ATOM_IS_NULL(atom)) {
        if (dst == RAY_BOOL || dst == RAY_U8)
            return ray_error("type", "insert: physical column %lld (%s) has no null representation",
                             (long long)physical_col, ray_type_name(dst));
        return NULL;
    }
    if (dst == RAY_SYM && !ray_sym_str(atom->i64))
        return ray_error("domain", "insert: SYM payload atom has an invalid runtime id");
    if ((dst == RAY_F32 && !isfinite((float)atom->f64)) ||
        (dst == RAY_F64 && !isfinite(atom->f64)))
        return ray_error("domain", "insert: non-finite float payload must use the typed-null representation");
    if (dst >= RAY_BOOL && dst <= RAY_STR) return NULL;
    return ray_error("type", "insert: unsupported physical column %lld (%s), got %s",
                     (long long)physical_col, ray_type_name(dst),
                     ray_type_name(atom->type));
}

/* Scalar physical columns broadcast. Every collection column establishes the
 * same batch length, including zero. */
static ray_t* validate_parted_payloads(ray_t* tbl,
                                       parted_insert_view_t* v,
                                       ray_t* vals,
                                       int64_t* out_batch) {
    ray_t** pv = (ray_t**)ray_data(vals);
    bool have_collection = false;
    int64_t batch = 1;
    for (int64_t c = 0; c < v->ndata; c++) {
        ray_t* payload = pv[c];
        if (parted_value_is_scalar(payload)) continue;
        if (!payload || RAY_IS_ERR(payload) ||
            (payload->type != RAY_LIST && !ray_is_vec(payload)))
            return ray_error("type", "insert: physical column %lld payload must be an atom or an exactly typed vector",
                             (long long)c);
        if (payload->len < 0)
            return ray_error("corrupt", "insert: physical column %lld payload has a negative vector length",
                             (long long)c);
        if (!have_collection) { batch = payload->len; have_collection = true; }
        else if (payload->len != batch)
            return ray_error("length", "insert: all non-scalar physical payload columns must have the same length");
    }

    for (int64_t c = 0; c < v->ndata; c++) {
        ray_t* wrapper = ray_table_get_col_idx(tbl, c + 1);
        int8_t dst = (int8_t)RAY_PARTED_BASETYPE(wrapper->type);
        ray_t* payload = pv[c];
        if (dst == RAY_LIST) {
            if (!parted_value_is_scalar(payload) && payload->type != RAY_LIST)
                return ray_error("type", "insert: physical column %lld LIST payload must contain boxed rows",
                                 (long long)c);
            continue;
        }
        if (!parted_value_is_scalar(payload) &&
            !parted_source_vec_compatible(dst, payload->type))
            return ray_error("type", "insert: physical column %lld vector type must exactly match %s",
                             (long long)c, ray_type_name(dst));

        /* Exact vector types need no per-cell boxing.  SYM still needs a
         * direct domain-resolvability scan (ray_sym_vec_cell deliberately
         * avoids building the FILE->runtime LUT), while floats reject
         * infinities but retain NaN as their typed-null representation. */
        if (!parted_value_is_scalar(payload)) {
            ray_t* owner = (payload->attrs & RAY_ATTR_SLICE)
                         ? payload->slice_parent : payload;
            if ((dst == RAY_BOOL || dst == RAY_U8) &&
                (!owner || ray_vec_may_have_nulls(owner)))
                return ray_error("type", "insert: physical column %lld (%s) has no null representation",
                                 (long long)c, ray_type_name(dst));
            if (dst == RAY_SYM) {
                for (int64_t r = 0; r < payload->len; r++)
                    if (!ray_sym_vec_cell(payload, r))
                        return ray_error("domain", "insert: physical column %lld SYM vector contains an invalid domain position",
                                         (long long)c);
            } else if (dst == RAY_F32) {
                const float* d = (const float*)ray_data(payload);
                for (int64_t r = 0; r < payload->len; r++)
                    if (!isfinite(d[r]) && !isnan(d[r]))
                        return ray_error("domain", "insert: physical column %lld F32 vector contains infinity",
                                         (long long)c);
            } else if (dst == RAY_F64) {
                const double* d = (const double*)ray_data(payload);
                for (int64_t r = 0; r < payload->len; r++)
                    if (!isfinite(d[r]) && !isnan(d[r]))
                        return ray_error("domain", "insert: physical column %lld F64 vector contains infinity",
                                         (long long)c);
            }
            continue;
        }

        ray_t* err = validate_parted_payload_atom(dst, payload, c);
        if (err) return err;
    }
    *out_batch = batch;
    return NULL;
}

/* Append a prevalidated non-SYM atom, preserving every concrete splayable
 * physical type (notably F32, which legacy flat insert does not handle). */
static ray_t* append_parted_atom(ray_t* vec, int8_t dst, ray_t* atom) {
    if (RAY_IS_NULL(atom) || RAY_ATOM_IS_NULL(atom)) {
        if (dst == RAY_STR)
            return ray_str_vec_append(vec, "", 0);
        uint8_t zero[16] = {0};
        ray_t* next = ray_vec_append(vec, zero);
        if (!next || RAY_IS_ERR(next)) return next;
        if (dst != RAY_BOOL && dst != RAY_U8 && dst != RAY_SYM)
            ray_vec_set_null(next, next->len - 1, true);
        return next;
    }

    switch (dst) {
    case RAY_BOOL: { uint8_t x = atom->b8; return ray_vec_append(vec, &x); }
    case RAY_U8:   { uint8_t x = atom->u8; return ray_vec_append(vec, &x); }
    case RAY_I16:  { int16_t x = atom->i16; return ray_vec_append(vec, &x); }
    case RAY_I32:  { int32_t x = atom->i32; return ray_vec_append(vec, &x); }
    case RAY_I64:  { int64_t x = atom->i64; return ray_vec_append(vec, &x); }
    case RAY_F32:  { float x = (float)atom->f64; return ray_vec_append(vec, &x); }
    case RAY_F64:  { double x = atom->f64; return ray_vec_append(vec, &x); }
    case RAY_DATE: { int32_t x = atom->i32; return ray_vec_append(vec, &x); }
    case RAY_TIME: { int32_t x = atom->i32; return ray_vec_append(vec, &x); }
    case RAY_TIMESTAMP: { int64_t x = atom->i64; return ray_vec_append(vec, &x); }
    case RAY_GUID: return ray_vec_append(vec, ray_data(atom->obj));
    case RAY_STR: return ray_str_vec_append(vec, ray_str_ptr(atom), ray_str_len(atom));
    default: return ray_error("type", "insert: unsupported parted append type %s", ray_type_name(dst));
    }
}

/* Concat copies sentinel payloads faithfully, but deliberately trusts the
 * source HAS_NULLS gate.  Finalize the private segment from its bytes so raw
 * API vectors cannot publish an unmarked null.  GUID uses an all-zero
 * sentinel and is outside par_finalize_nulls' numeric/temporal scope. */
static void finalize_parted_segment_nulls(ray_t* vec) {
    par_finalize_nulls(vec);
    if (vec->type != RAY_GUID) return;
    const uint8_t* d = (const uint8_t*)ray_data(vec);
    for (int64_t r = 0; r < vec->len; r++) {
        bool all_zero = true;
        for (int b = 0; b < 16; b++) {
            if (d[(size_t)r * 16u + (size_t)b] != 0) {
                all_zero = false;
                break;
            }
        }
        if (all_zero) {
            vec->attrs |= RAY_ATTR_HAS_NULLS;
            return;
        }
    }
}

static ray_t* build_parted_null_prefix(int8_t base, int64_t len) {
    ray_t* prefix = ray_vec_new(base, len);
    if (!prefix || RAY_IS_ERR(prefix))
        return prefix ? prefix : ray_error("oom", NULL);
    for (int64_t r = 0; r < len; r++) {
        ray_t* next = append_parted_atom(prefix, base, RAY_NULL_OBJ);
        if (!next || RAY_IS_ERR(next)) {
            ray_release(prefix);
            return next ? next : ray_error("oom", NULL);
        }
        prefix = next;
    }
    return prefix;
}

static ray_t* build_parted_nonsym_segment(int8_t base,
                                          ray_t* old_seg,
                                          int64_t old_len,
                                          ray_t* metadata_seg,
                                          ray_t* payload,
                                          int64_t batch) {
    if (!old_seg && old_len > 0 && (base == RAY_BOOL || base == RAY_U8))
        return ray_error("type", "insert: cannot backfill a missing active %s segment because the type has no null representation",
                         ray_type_name(base));

    ray_t* owned_prefix = NULL;
    ray_t* prefix = old_seg;
    if (!prefix && old_len > 0) {
        owned_prefix = build_parted_null_prefix(base, old_len);
        if (!owned_prefix || RAY_IS_ERR(owned_prefix))
            return owned_prefix ? owned_prefix : ray_error("oom", NULL);
        prefix = owned_prefix;
    }

    ray_t* result = NULL;
    if (parted_value_is_scalar(payload)) {
        ray_t* tail = ray_vec_new(base, batch);
        if (!tail || RAY_IS_ERR(tail))
            result = tail ? tail : ray_error("oom", NULL);
        for (int64_t r = 0; r < batch; r++) {
            if (!tail || RAY_IS_ERR(tail)) break;
            ray_t* next = append_parted_atom(tail, base, payload);
            if (!next || RAY_IS_ERR(next)) {
                ray_release(tail);
                tail = NULL;
                result = next ? next : ray_error("oom", NULL);
                break;
            }
            tail = next;
        }
        if (tail && !RAY_IS_ERR(tail)) {
            if (prefix) {
                /* Never append directly to an mmap (or any source) segment. */
                result = ray_vec_concat(prefix, tail);
                ray_release(tail);
            } else {
                result = tail;
            }
        }
    } else if (prefix) {
        /* Exact typed vector: one concat directly into the completed active
         * segment, with no per-cell boxing and no intermediate payload copy. */
        result = ray_vec_concat(prefix, payload);
    } else {
        /* A brand-new partition still needs to materialise slices/mmaps and
         * deep-copy STR storage rather than publish the caller's vector. */
        ray_t* empty = ray_vec_new(base, 0);
        if (!empty || RAY_IS_ERR(empty)) {
            result = empty ? empty : ray_error("oom", NULL);
        } else {
            result = ray_vec_concat(empty, payload);
            ray_release(empty);
        }
    }

    if (owned_prefix) ray_release(owned_prefix);
    if (!result || RAY_IS_ERR(result)) return result ? result : ray_error("oom", NULL);
    finalize_parted_segment_nulls(result);

    /* Link identity is semantic column metadata. The changed heap segment has
     * no index, but it keeps a representative I32/I64 link target. */
    if ((base == RAY_I32 || base == RAY_I64) && metadata_seg &&
        (metadata_seg->attrs & RAY_ATTR_HAS_LINK)) {
        result->attrs |= RAY_ATTR_HAS_LINK;
        result->link_target = metadata_seg->link_target;
    }
    return result;
}

static ray_t* build_parted_list_segment(ray_t* old_seg, int64_t old_len,
                                        ray_t* payload, int64_t batch) {
    if (old_len > INT64_MAX - batch) return ray_error("oom", NULL);
    ray_t* out = ray_list_new(old_len + batch);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    ray_t** dst = (ray_t**)ray_data(out);
    if (old_seg) {
        ray_t** src = (ray_t**)ray_data(old_seg);
        for (int64_t r = 0; r < old_len; r++) {
            dst[r] = src[r];
            if (dst[r]) ray_retain(dst[r]);
        }
    } else {
        memset(dst, 0, (size_t)old_len * sizeof(ray_t*));
    }

    if (payload->type == RAY_LIST) {
        ray_t** cells = (ray_t**)ray_data(payload);
        for (int64_t r = 0; r < batch; r++) {
            dst[old_len + r] = cells[r];
            if (cells[r]) ray_retain(cells[r]);
        }
    } else {
        for (int64_t r = 0; r < batch; r++) {
            dst[old_len + r] = payload;
            ray_retain(payload);
        }
    }
    out->len = old_len + batch;
    return out;
}

/* Allocate and copy the old prefix of a W64 FILE-domain SYM segment. Appended
 * positions are intentionally zero until the late domain-intern commit pass. */
static ray_t* build_parted_sym_skeleton(parted_insert_view_t* v,
                                        ray_t* old_seg,
                                        int64_t old_len,
                                        int64_t batch) {
    if (!v->sym_dom || !v->sym_rep)
        return ray_error("corrupt", "insert: no shared FILE domain representative for SYM column");
    if (old_len > INT64_MAX - batch) return ray_error("oom", NULL);
    int64_t total = old_len + batch;
    ray_t* out = ray_sym_vec_new(RAY_SYM_W64, total);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    ray_sym_vec_adopt_domain(out, v->sym_rep);
    if (total) memset(ray_data(out), 0, (size_t)total * sizeof(int64_t));
    if (old_seg) {
        int64_t domain_n = ray_sym_domain_count(v->sym_dom);
        for (int64_t r = 0; r < old_len; r++) {
            int64_t pos = ray_read_sym(ray_data(old_seg), r,
                                       old_seg->type, old_seg->attrs);
            if (pos < 0 || pos >= domain_n) {
                ray_release(out);
                return ray_error("corrupt", "insert: active SYM segment contains an out-of-domain position");
            }
            ((int64_t*)ray_data(out))[r] = pos;
        }
    }
    out->len = total;
    return out;
}

static ray_t* parted_wrapper_metadata(ray_t* wrapper, int64_t active) {
    ray_t** segs = (ray_t**)ray_data(wrapper);
    if (active < wrapper->len && segs[active]) return segs[active];
    int64_t start = active < wrapper->len ? active - 1 : wrapper->len - 1;
    for (int64_t p = start; p >= 0; p--)
        if (segs[p]) return segs[p];
    return NULL;
}

static ray_t* build_parted_mapcommon(parted_insert_view_t* v,
                                     ray_t* key,
                                     bool new_partition,
                                     int64_t batch) {
    if (new_partition && v->nparts == INT64_MAX)
        return ray_error("oom", NULL);
    int64_t new_n = v->nparts + (new_partition ? 1 : 0);
    ray_t* keys = ray_vec_new(v->key_type, new_n);
    ray_t* counts = ray_vec_new(RAY_I64, new_n);
    if (!keys || RAY_IS_ERR(keys) || !counts || RAY_IS_ERR(counts)) {
        ray_t* ret = keys && RAY_IS_ERR(keys) ? keys
                   : counts && RAY_IS_ERR(counts) ? counts : NULL;
        if (keys && keys != ret) {
            if (RAY_IS_ERR(keys)) ray_error_free(keys);
            else ray_release(keys);
        }
        if (counts && counts != ret) {
            if (RAY_IS_ERR(counts)) ray_error_free(counts);
            else ray_release(counts);
        }
        return ret ? ret : ray_error("oom", NULL);
    }

    for (int64_t p = 0; p < v->nparts; p++) {
        ray_t* next;
        if (v->key_type == RAY_DATE) {
            int32_t x = ((int32_t*)ray_data(v->key_values))[p];
            next = ray_vec_append(keys, &x);
        } else {
            int64_t x = v->key_type == RAY_SYM
                      ? ray_read_sym(ray_data(v->key_values), p,
                                     RAY_SYM, v->key_values->attrs)
                      : ((int64_t*)ray_data(v->key_values))[p];
            next = ray_vec_append(keys, &x);
        }
        if (!next || RAY_IS_ERR(next)) {
            ray_release(keys); ray_release(counts);
            return next ? next : ray_error("oom", NULL);
        }
        keys = next;

        int64_t rc = ((int64_t*)ray_data(v->row_counts))[p];
        if (!new_partition && p == v->nparts - 1) rc += batch;
        next = ray_vec_append(counts, &rc);
        if (!next || RAY_IS_ERR(next)) {
            ray_release(keys); ray_release(counts);
            return next ? next : ray_error("oom", NULL);
        }
        counts = next;
    }
    if (new_partition) {
        ray_t* next;
        if (v->key_type == RAY_DATE) {
            int32_t x = key->i32;
            next = ray_vec_append(keys, &x);
        } else {
            int64_t x = key->i64;
            next = ray_vec_append(keys, &x);
        }
        if (!next || RAY_IS_ERR(next)) {
            ray_release(keys); ray_release(counts);
            return next ? next : ray_error("oom", NULL);
        }
        keys = next;
        next = ray_vec_append(counts, &batch);
        if (!next || RAY_IS_ERR(next)) {
            ray_release(keys); ray_release(counts);
            return next ? next : ray_error("oom", NULL);
        }
        counts = next;
    }

    ray_t* mc = ray_alloc(2 * sizeof(ray_t*));
    if (!mc || RAY_IS_ERR(mc)) {
        ray_release(keys); ray_release(counts);
        return mc ? mc : ray_error("oom", NULL);
    }
    mc->type = RAY_MAPCOMMON;
    mc->len = 2;
    mc->attrs = v->mapcommon->attrs;
    memset(mc->aux, 0, 16);
    ((ray_t**)ray_data(mc))[0] = keys;   /* transfer ownership */
    ((ray_t**)ray_data(mc))[1] = counts;
    return mc;
}

/* Borrow the logical bytes of a prevalidated SYM payload cell without
 * allocating a runtime atom for typed vectors. */
static bool parted_sym_payload_bytes(ray_t* payload, int64_t row,
                                     const char** out_s, size_t* out_n) {
    ray_t* atom = NULL;
    if (parted_value_is_scalar(payload)) {
        atom = payload;
    } else if (payload->type == RAY_LIST) {
        atom = ((ray_t**)ray_data(payload))[row];
    } else if (payload->type == RAY_SYM) {
        ray_t* s = ray_sym_vec_cell(payload, row);
        if (!s) return false;
        *out_s = ray_str_ptr(s);
        *out_n = ray_str_len(s);
        return true;
    } else {
        return false;
    }
    if (RAY_IS_NULL(atom)) {
        *out_s = ""; *out_n = 0; return true;
    }
    if (!atom || atom->type != -RAY_SYM) return false;
    ray_t* s = ray_sym_str(atom->i64);
    if (!s) return false;
    *out_s = ray_str_ptr(s);
    *out_n = ray_str_len(s);
    return true;
}

/* Domain growth is append-only and currently has no rollback primitive. This
 * pass therefore runs only after the complete private result (including every
 * allocation and every non-SYM conversion) has been built. An OOM may leave
 * unused vocabulary entries in memory, but no row/table/env/disk state is
 * published. */
static ray_t* intern_parted_payload_symbols(ray_t* tbl,
                                            parted_insert_view_t* v,
                                            ray_t* vals,
                                            int64_t batch) {
    ray_t** pv = (ray_t**)ray_data(vals);
    for (int64_t c = 0; c < v->ndata; c++) {
        ray_t* wrapper = ray_table_get_col_idx(tbl, c + 1);
        if (RAY_PARTED_BASETYPE(wrapper->type) != RAY_SYM) continue;
        ray_t* payload = pv[c];
        int64_t n = parted_value_is_scalar(payload) ? (batch ? 1 : 0) : batch;
        for (int64_t r = 0; r < n; r++) {
            const char* s = NULL; size_t slen = 0;
            if (!parted_sym_payload_bytes(payload, r, &s, &slen))
                return ray_error("corrupt", "insert: could not resolve a SYM payload cell");
            if (ray_sym_domain_intern(v->sym_dom, s, slen) < 0)
                return ray_error("oom", "insert: could not extend the parted FILE symbol domain");
        }
    }
    return NULL;
}

static ray_t* fill_parted_payload_symbols(ray_t* tbl,
                                          parted_insert_view_t* v,
                                          ray_t* vals,
                                          ray_t* staged,
                                          int64_t old_len,
                                          int64_t batch) {
    ray_t** pv = (ray_t**)ray_data(vals);
    ray_t** sv = (ray_t**)ray_data(staged);
    for (int64_t c = 0; c < v->ndata; c++) {
        ray_t* wrapper = ray_table_get_col_idx(tbl, c + 1);
        if (RAY_PARTED_BASETYPE(wrapper->type) != RAY_SYM) continue;
        ray_t* payload = pv[c];
        ray_t* seg = sv[c];
        for (int64_t r = 0; r < batch; r++) {
            const char* s = NULL; size_t slen = 0;
            int64_t src_r = parted_value_is_scalar(payload) ? 0 : r;
            if (!parted_sym_payload_bytes(payload, src_r, &s, &slen))
                return ray_error("corrupt", "insert: could not resolve a staged SYM payload cell");
            int64_t pos = ray_sym_domain_find(v->sym_dom, s, slen);
            if (pos < 0)
                return ray_error("corrupt", "insert: staged SYM is absent after domain intern");
            ray_write_sym(ray_data(seg), old_len + r, (uint64_t)pos,
                          RAY_SYM, seg->attrs);
        }
    }
    return NULL;
}

static ray_t* insert_parted_rows(ray_t* tbl, ray_t* key, ray_t* rows) {
    parted_insert_view_t v;
    ray_t* err = validate_parted_insert_target(tbl, &v);
    if (err) return err;

    bool new_partition = false;
    err = decide_parted_insert_partition(&v, key, &new_partition);
    if (err) return err;
    if (new_partition && v.active_missing)
        return ray_error("domain", "insert: cannot advance the partition key while the current partition has missing physical segments");

    ray_t* vals = normalize_parted_insert_rows(tbl, &v, rows);
    if (!vals || RAY_IS_ERR(vals)) return vals ? vals : ray_error("oom", NULL);
    int64_t batch = 0;
    err = validate_parted_payloads(tbl, &v, vals, &batch);
    if (err) { ray_release(vals); return err; }

    /* Empty batch is a validated no-op and never creates an empty partition.
     * It cannot repair a missing active segment, so reject that shape instead
     * of returning the same unqueryable table. */
    if (batch == 0) {
        ray_release(vals);
        if (v.active_missing)
            return ray_error("domain", "insert: an empty batch cannot materialize missing active physical segments");
        ray_retain(tbl);
        return tbl;
    }

    if (v.total_rows > INT64_MAX - batch) {
        ray_release(vals);
        return ray_error("oom", NULL);
    }

    int64_t active = new_partition ? v.nparts : v.nparts - 1;
    int64_t old_len = new_partition ? 0
                    : ((int64_t*)ray_data(v.row_counts))[v.nparts - 1];
    if (old_len > INT64_MAX - batch) {
        ray_release(vals);
        return ray_error("oom", NULL);
    }
    if (new_partition &&
        (v.nparts == INT64_MAX ||
         (uint64_t)v.nparts + 1u > SIZE_MAX / sizeof(ray_t*))) {
        ray_release(vals);
        return ray_error("oom", NULL);
    }

    ray_t* staged = ray_list_new(v.ndata);
    if (!staged || RAY_IS_ERR(staged)) {
        ray_release(vals);
        return staged ? staged : ray_error("oom", NULL);
    }
    ray_t** pv = (ray_t**)ray_data(vals);
    for (int64_t c = 0; c < v.ndata; c++) {
        ray_t* wrapper = ray_table_get_col_idx(tbl, c + 1);
        ray_t** segs = (ray_t**)ray_data(wrapper);
        int8_t base = (int8_t)RAY_PARTED_BASETYPE(wrapper->type);
        ray_t* old_seg = new_partition ? NULL : segs[v.nparts - 1];
        ray_t* new_seg = base == RAY_LIST
            ? build_parted_list_segment(old_seg, old_len, pv[c], batch)
            : base == RAY_SYM
            ? build_parted_sym_skeleton(&v, old_seg, old_len, batch)
            : build_parted_nonsym_segment(base, old_seg, old_len,
                                           parted_wrapper_metadata(wrapper, active),
                                           pv[c], batch);
        if (!new_seg || RAY_IS_ERR(new_seg)) {
            ray_release(staged); ray_release(vals);
            return new_seg ? new_seg : ray_error("oom", NULL);
        }
        ray_t* next = ray_list_append(staged, new_seg);
        ray_release(new_seg);
        if (!next || RAY_IS_ERR(next)) {
            ray_release(staged); ray_release(vals);
            return next ? next : ray_error("oom", NULL);
        }
        staged = next;
    }

    ray_t* mc = build_parted_mapcommon(&v, key, new_partition, batch);
    if (!mc || RAY_IS_ERR(mc)) {
        ray_release(staged); ray_release(vals);
        return mc ? mc : ray_error("oom", NULL);
    }
    ray_t* result = ray_table_new(v.ncols);
    if (!result || RAY_IS_ERR(result)) {
        ray_release(mc); ray_release(staged); ray_release(vals);
        return result ? result : ray_error("oom", NULL);
    }
    result = ray_table_add_col(result, ray_table_col_name(tbl, 0), mc);
    ray_release(mc);
    if (!result || RAY_IS_ERR(result)) {
        ray_release(staged); ray_release(vals);
        return result ? result : ray_error("oom", NULL);
    }

    int64_t new_nparts = v.nparts + (new_partition ? 1 : 0);
    if ((uint64_t)new_nparts > SIZE_MAX / sizeof(ray_t*)) {
        ray_release(result); ray_release(staged); ray_release(vals);
        return ray_error("oom", NULL);
    }
    ray_t** staged_segs = (ray_t**)ray_data(staged);
    for (int64_t c = 0; c < v.ndata; c++) {
        ray_t* old_wrapper = ray_table_get_col_idx(tbl, c + 1);
        ray_t** old_segs = (ray_t**)ray_data(old_wrapper);
        ray_t* wrapper = ray_alloc((size_t)new_nparts * sizeof(ray_t*));
        if (!wrapper || RAY_IS_ERR(wrapper)) {
            ray_release(result); ray_release(staged); ray_release(vals);
            return wrapper ? wrapper : ray_error("oom", NULL);
        }
        wrapper->type = old_wrapper->type;
        wrapper->len = new_nparts;
        wrapper->attrs = 0;
        memset(wrapper->aux, 0, 16);
        ray_t** ws = (ray_t**)ray_data(wrapper);
        memset(ws, 0, (size_t)new_nparts * sizeof(ray_t*));
        for (int64_t p = 0; p < v.nparts; p++) {
            if (!new_partition && p == v.nparts - 1) continue;
            ws[p] = old_segs[p];
            if (ws[p]) ray_retain(ws[p]);
        }
        ws[active] = staged_segs[c];
        ray_retain(ws[active]);

        result = ray_table_add_col(result, ray_table_col_name(tbl, c + 1), wrapper);
        ray_release(wrapper);
        if (!result || RAY_IS_ERR(result)) {
            ray_release(staged); ray_release(vals);
            return result ? result : ray_error("oom", NULL);
        }
    }

    /* All allocations and all fallible non-SYM conversions are complete. */
    err = intern_parted_payload_symbols(tbl, &v, vals, batch);
    if (!err)
        err = fill_parted_payload_symbols(tbl, &v, vals, staged,
                                          old_len, batch);
    ray_release(staged);
    ray_release(vals);
    if (err) { ray_release(result); return err; }
    return result;
}

/* (insert table (list val1 val2 ...)) — append a row to a table
 * (insert parted partition-key rows) — grow the explicit live tail */
/* Defined with the rest of the upsert key-map helpers below; the in-place
 * insert arm needs it here. */
static void ukey_extend_after_append(ray_t* tbl, int64_t nrows0);

ray_t* ray_insert_fn(ray_t** args, int64_t n) {
    return ray_insert(args, n);
}

ray_t* ray_insert(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("arity", "insert: expects a target and a value, got %lld args", (long long)n);

    /* In-place vs functional is decided by the EVALUATED first argument:
     * a symbol value names a global to amend in place (returning that
     * symbol); a table/vec value is amended functionally (returning the
     * new value).  Evaluating first means it works however the symbol
     * arrives — literal 'sym, (quote sym), a variable, or a lambda param. */
    int64_t inplace_sym = -1;
    ray_t* tbl_raw = args[0];
    ray_t* tbl;

    /* Detect calling convention: already-evaluated args (from upsert) vs raw parse tree */
    int already_eval = (tbl_raw && tbl_raw->type == RAY_TABLE);

    if (already_eval) {
        tbl = tbl_raw;
        ray_retain(tbl);
    } else {
        tbl = ray_eval(tbl_raw);
        if (!tbl || RAY_IS_ERR(tbl)) return tbl ? tbl : ray_error("type", "insert: target evaluation failed");
        if (tbl->type == -RAY_SYM) {
            /* Symbol value → resolve the named global for in-place insert */
            inplace_sym = tbl->i64;
            ray_release(tbl);
            tbl = ray_env_get(inplace_sym);
            if (!tbl || RAY_IS_ERR(tbl)) return ray_error("domain", "insert: target symbol is unbound");
            ray_retain(tbl);
        }
    }

    /* ====================================================================
     * Vec/list dispatch — n==2 append, n==3 positional insert.
     * Tables with n==2 fall through to the legacy table-row append below.
     * ==================================================================== */
    if (tbl->type != RAY_TABLE) {
        if (already_eval) { int8_t tbl_t = tbl->type; ray_release(tbl); return ray_error("type", "insert: target must be a table, got %s", ray_type_name(tbl_t)); }
        if (tbl->attrs & RAY_ATTR_ARENA) { ray_release(tbl); return ray_error("type", "insert: cannot insert into an arena-backed value"); }

        /* Slice → materialise so cow can mutate. Lists never slice. */
        if (tbl->attrs & RAY_ATTR_SLICE) {
            if (tbl->type == RAY_LIST) { ray_release(tbl); return ray_error("type", "insert: cannot insert into a list slice"); }
            ray_t* empty = ray_vec_new(tbl->type, 0);
            if (!empty || RAY_IS_ERR(empty)) {
                ray_release(tbl);
                return empty ? empty : ray_error("oom", NULL);
            }
            ray_t* mat = ray_vec_concat(tbl, empty);
            ray_release(empty);
            ray_release(tbl);
            if (!mat || RAY_IS_ERR(mat)) return mat ? mat : ray_error("oom", NULL);
            tbl = mat;
        }

        bool is_target_list = (tbl->type == RAY_LIST);
        bool is_target_vec  = ray_is_vec(tbl);
        if (!is_target_list && !is_target_vec) {
            int8_t tbl_t = tbl->type;            /* capture BEFORE free */
            ray_release(tbl);
            return ray_error("type", "insert: target must be a table, list or vector, got %s", ray_type_name(tbl_t));
        }
        if (n != 2 && n != 3) {
            ray_release(tbl);
            return ray_error("domain", "insert: list/vector insert takes 2 or 3 args, got %lld", (long long)n);
        }

        ray_t* result = NULL;
        int8_t tt = tbl->type;

        if (n == 2) {
            /* APPEND */
            ray_t* val = ray_eval(args[1]);
            if (!val || RAY_IS_ERR(val)) {
                ray_release(tbl);
                return val ? val : ray_error("type", "insert: value evaluation failed");
            }
            if (is_target_list) {
                /* Always one slot — never splice on append. */
                tbl = ray_list_append(tbl, val);
                result = tbl;
            } else if (val->type == -tt) {
                /* Atom of matching type → element append */
                int64_t new_idx = tbl->len;
                if (tt == RAY_STR) {
                    tbl = ray_str_vec_append(tbl, ray_str_ptr(val), ray_str_len(val));
                } else if (tt == RAY_SYM) {
                    int64_t s = val->i64;
                    tbl = ray_vec_append(tbl, &s);
                } else if (tt == RAY_GUID) {
                    /* GUID atom's 16-byte payload lives in val->obj; typed-null
                     * atoms have obj==NULL — write zeros and let the post-call
                     * RAY_ATOM_IS_NULL check mark the slot. */
                    static const uint8_t zero_guid[16] = {0};
                    const void* src = val->obj ? ray_data(val->obj) : zero_guid;
                    tbl = ray_vec_append(tbl, src);
                } else {
                    tbl = ray_vec_append(tbl, &val->u8);
                }
                if (tbl && !RAY_IS_ERR(tbl) && RAY_ATOM_IS_NULL(val))
                    ray_vec_set_null(tbl, new_idx, true);
                result = tbl;
            } else if (val->type == tt) {
                /* Same-type vec → splice at end */
                result = ray_vec_concat(tbl, val);
                ray_release(tbl);
            } else {
                int8_t val_t = val->type;            /* capture BEFORE free */
                ray_release(tbl);
                ray_release(val);
                return ray_error("type", "insert: appended value type does not match %s vector, got %s",
                                 ray_type_name(tt), ray_type_name(val_t));
            }
            ray_release(val);
        } else {
            /* n == 3 — POSITIONAL */
            ray_t* idx_arg = ray_eval(args[1]);
            if (!idx_arg || RAY_IS_ERR(idx_arg)) {
                ray_release(tbl);
                return idx_arg ? idx_arg : ray_error("type", "insert: index evaluation failed");
            }
            ray_t* val = ray_eval(args[2]);
            if (!val || RAY_IS_ERR(val)) {
                ray_release(tbl);
                ray_release(idx_arg);
                return val ? val : ray_error("type", "insert: value evaluation failed");
            }

            if (is_target_list) {
                if (idx_arg->type == -RAY_I64) {
                    tbl = ray_list_insert_at(tbl, idx_arg->i64, val);
                    result = tbl;
                } else if (idx_arg->type == RAY_I64) {
                    if (val->type != RAY_LIST) {
                        int8_t val_t = val->type;            /* capture BEFORE free */
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", "insert: multi-index list insert requires a list of values, got %s", ray_type_name(val_t));
                    }
                    result = ray_list_insert_many(tbl, idx_arg, val);
                    ray_release(tbl);
                } else {
                    int8_t idx_t = idx_arg->type;            /* capture BEFORE free */
                    ray_release(tbl); ray_release(idx_arg); ray_release(val);
                    return ray_error("type", "insert: list index must be an i64 atom or vector, got %s", ray_type_name(idx_t));
                }
            } else {
                /* vec target */
                if (idx_arg->type == -RAY_I64) {
                    int64_t i = idx_arg->i64;
                    if (val->type == -tt) {
                        if (tt == RAY_STR) {
                            result = ray_str_vec_insert_at(tbl, i,
                                        ray_str_ptr(val), ray_str_len(val));
                            ray_release(tbl);
                        } else if (tt == RAY_SYM) {
                            int64_t s = val->i64;
                            tbl = ray_vec_insert_at(tbl, i, &s);
                            result = tbl;
                        } else if (tt == RAY_GUID) {
                            static const uint8_t zero_guid[16] = {0};
                            const void* src = val->obj ? ray_data(val->obj) : zero_guid;
                            tbl = ray_vec_insert_at(tbl, i, src);
                            result = tbl;
                        } else {
                            tbl = ray_vec_insert_at(tbl, i, &val->u8);
                            result = tbl;
                        }
                        if (result && !RAY_IS_ERR(result) && RAY_ATOM_IS_NULL(val))
                            ray_vec_set_null(result, i, true);
                    } else if (val->type == tt) {
                        result = ray_vec_insert_vec_at(tbl, i, val);
                        ray_release(tbl);
                    } else {
                        int8_t val_t = val->type;            /* capture BEFORE free */
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", "insert: value type does not match %s vector, got %s",
                                         ray_type_name(tt), ray_type_name(val_t));
                    }
                } else if (idx_arg->type == RAY_I64) {
                    if (tt == RAY_STR) {
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", "insert: multi-index insert not supported on str vectors");
                    }
                    if (val->type != tt && val->type != -tt) {
                        int8_t val_t = val->type;            /* capture BEFORE free */
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", "insert: value type does not match %s vector, got %s",
                                         ray_type_name(tt), ray_type_name(val_t));
                    }
                    result = ray_vec_insert_many(tbl, idx_arg, val);
                    ray_release(tbl);
                } else {
                    int8_t idx_t = idx_arg->type;            /* capture BEFORE free */
                    ray_release(tbl); ray_release(idx_arg); ray_release(val);
                    return ray_error("type", "insert: index must be an i64 atom or vector, got %s", ray_type_name(idx_t));
                }
            }
            ray_release(idx_arg);
            ray_release(val);
        }

        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            /* In-place amend stores the new value and returns the symbol. */
            ray_env_set(inplace_sym, result);
            ray_release(result);
            return ray_sym(inplace_sym);
        }
        return result;
    }

    /* Table arity-3 is reserved for explicit live-tail growth on the exact
     * representation returned by `.db.parted.get`. */
    if (n == 3) {
        if (!table_has_parted_columns(tbl)) {
            ray_release(tbl);
            return ray_error("type", "insert: three-argument table form requires a canonical .db.parted.get table");
        }
        if (inplace_sym >= 0 && ray_sym_is_reserved(inplace_sym) &&
            !ray_sym_is_ipc_hook(inplace_sym)) {
            ray_release(tbl);
            return ray_error("reserved", "insert: cannot rebind a reserved target");
        }
        ray_t* partition_key = already_eval
                             ? (ray_retain(args[1]), args[1])
                             : ray_eval(args[1]);
        if (!partition_key || RAY_IS_ERR(partition_key)) {
            ray_release(tbl);
            return partition_key ? partition_key
                                 : ray_error("type", "insert: partition key evaluation failed");
        }
        ray_t* rows = already_eval
                    ? (ray_retain(args[2]), args[2])
                    : ray_eval(args[2]);
        if (!rows || RAY_IS_ERR(rows)) {
            ray_release(tbl); ray_release(partition_key);
            return rows ? rows : ray_error("type", "insert: rows evaluation failed");
        }
        /* Preallocate the success value before domain growth/publication. */
        ray_t* success = NULL;
        if (inplace_sym >= 0) {
            success = ray_sym(inplace_sym);
            if (!success || RAY_IS_ERR(success)) {
                ray_release(rows);
                ray_release(partition_key);
                ray_release(tbl);
                return success ? success : ray_error("oom", NULL);
            }
        }
        ray_t* result = insert_parted_rows(tbl, partition_key, rows);
        ray_release(rows);
        ray_release(partition_key);
        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            /* Empty batches return the original table and require no rebind.
             * Otherwise publish only after the complete private result exists,
             * and surface dotted-path OOM/type failures instead of
             * falsely returning the target symbol as success. */
            ray_err_t bind_err = result == tbl ? RAY_OK
                               : ray_env_set(inplace_sym, result);
            ray_release(result);
            ray_release(tbl);
            if (bind_err != RAY_OK) {
                ray_release(success);
                return ray_error(ray_err_code_str(bind_err),
                                 "insert: could not rebind the parted target");
            }
            return success;
        }
        if (success) ray_release(success);
        ray_release(tbl);
        return result;
    }
    if (n != 2) {
        ray_release(tbl);
        return ray_error("arity", "insert: table insert takes 2 args, or 3 for a parted live tail, got %lld",
                         (long long)n);
    }
    if (table_has_parted_columns(tbl)) {
        ray_release(tbl);
        return ray_error("arity", "insert: parted table insert requires target, partition key and physical rows");
    }

    /* Evaluate the row argument (skip if already evaluated) */
    ray_t* row = already_eval ? (ray_retain(args[1]), args[1]) : ray_eval(args[1]);
    if (!row || RAY_IS_ERR(row)) { ray_release(tbl); return row ? row : ray_error("type", "insert: row evaluation failed"); }
    if (tbl->type != RAY_TABLE) { int8_t tbl_t = tbl->type; ray_release(tbl); ray_release(row); return ray_error("type", "insert: target must be a table, got %s", ray_type_name(tbl_t)); }

    int64_t ncols = ray_table_ncols(tbl);
    ray_t* row_orig = row; /* keep original eval result for cleanup */

    if (!is_list(row) && row->type != RAY_TABLE && row->type != RAY_DICT) { int8_t row_t = row->type; ray_release(tbl); ray_release(row); return ray_error("type", "insert: row must be a list, table or dict, got %s", ray_type_name(row_t)); }

    /* Table row: convert to list of column vectors */
    ray_t* tbl_row_list = NULL;
    if (row->type == RAY_TABLE) {
        int64_t src_ncols = ray_table_ncols(row);
        if (src_ncols != ncols) { ray_release(tbl); ray_release(row); return ray_error("domain", "insert: row table has %lld columns, target has %lld", (long long)src_ncols, (long long)ncols); }
        tbl_row_list = ray_alloc(ncols * sizeof(ray_t*));
        if (!tbl_row_list) { ray_release(tbl); ray_release(row_orig); return ray_error("oom", NULL); }
        tbl_row_list->type = RAY_LIST;
        tbl_row_list->len = ncols;
        ray_t** trl = (ray_t**)ray_data(tbl_row_list);
        for (int64_t c = 0; c < ncols; c++) {
            int64_t col_name = ray_table_col_name(tbl, c);
            ray_t* src_col = ray_table_get_col(row, col_name);
            if (!src_col) src_col = ray_table_get_col_idx(row, c);
            if (!src_col) {
                tbl_row_list->len = 0;
                ray_free(tbl_row_list);
                ray_release(tbl); ray_release(row_orig);
                return ray_error("domain", "insert: row table missing a target column");
            }
            trl[c] = src_col;
            ray_retain(src_col);
        }
        row = tbl_row_list;
    }

    /* Dict row: extract values in table column order */
    ray_t* dict_vals = NULL;
    if (row->type == RAY_DICT) {
        ray_t* dkeys = ray_dict_keys(row);
        ray_t* dvals = ray_dict_vals(row);
        if (!dkeys || dkeys->type != RAY_SYM || !dvals) {
            ray_release(tbl); ray_release(row_orig);
            return ray_error("type", "insert: dict row keys must be symbols");
        }
        int64_t dict_len = dkeys->len;

        dict_vals = ray_alloc(ncols * sizeof(ray_t*));
        if (!dict_vals) { ray_release(tbl); ray_release(row_orig); return ray_error("oom", NULL); }
        dict_vals->type = RAY_LIST;
        dict_vals->len = ncols;
        ray_t** dv = (ray_t**)ray_data(dict_vals);

        for (int64_t c = 0; c < ncols; c++) {
            int64_t col_name = ray_table_col_name(tbl, c);
            dv[c] = NULL;
            for (int64_t d = 0; d < dict_len; d++) {
                /* dict-key cell vs runtime column NAME id: translate the
                 * cell into the runtime domain before comparing (raw
                 * compare while the dict keys are runtime-domain). */
                int64_t dk = sym_cell_runtime_id(dkeys, d);
                if (dk != col_name) continue;
                if (dvals->type == RAY_LIST) {
                    dv[c] = ((ray_t**)ray_data(dvals))[d];
                    if (dv[c]) ray_retain(dv[c]);
                } else {
                    int alloc = 0;
                    dv[c] = collection_elem(dvals, d, &alloc);
                    if (!alloc && dv[c]) ray_retain(dv[c]);
                }
                break;
            }
        }
        /* Verify all dict keys exist as table columns */
        for (int64_t d = 0; d < dict_len; d++) {
            int64_t dk = sym_cell_runtime_id(dkeys, d);
            int found_in_tbl = 0;
            for (int64_t c = 0; c < ncols; c++) {
                if (ray_table_col_name(tbl, c) == dk) { found_in_tbl = 1; break; }
            }
            if (!found_in_tbl) {
                for (int64_t c = 0; c < ncols; c++) if (dv[c]) ray_release(dv[c]);
                dict_vals->len = 0;
                ray_free(dict_vals);
                ray_release(tbl); ray_release(row_orig);
                return ray_error("value", NULL);
            }
        }
        row = dict_vals;
    }

    if (ray_len(row) != ncols) {
        if (dict_vals) {
            for (int64_t c = 0; c < ncols; c++) ray_release(((ray_t**)ray_data(dict_vals))[c]);
            dict_vals->len = 0;
            ray_free(dict_vals);
        }
        ray_release(tbl); ray_release(row_orig);
        return ray_error("domain", "insert: row has %lld values, target has %lld columns", (long long)ray_len(row), (long long)ncols);
    }

    ray_t** row_elems = (ray_t**)ray_data(row);
    int64_t nrows = ray_table_nrows(tbl);

    /* ---- In-place append fast path (#455) -------------------------------
     * The rebuild below copies every existing row on every call, which
     * makes a named-table ingest O(rows present) per insert — quadratic
     * over a feed's lifetime.  When the binding is the sole owner of the
     * table and no column needs adoption or boxing, append the batch into
     * the existing columns instead: ray_vec_append grows in place (the
     * buddy allocator extends the block when the space is there), so the
     * cost is O(rows appended), amortized — the same profile as the
     * vector path.  Any gate miss falls through to the rebuild, which
     * keeps its copy semantics.
     *
     * On a mid-batch failure the appended lengths are rolled back, so the
     * table keeps its pre-insert value; unlike the rebuild, already-grown
     * capacity and dropped accelerator indexes are not restored. */
    bool inplace_ok = inplace_sym >= 0 && tbl->rc == 2 && tbl->mmod == 0 &&
                      !(tbl->attrs & (RAY_ATTR_SLICE | RAY_ATTR_ARENA));
    ray_t* live_cols = NULL;
    if (inplace_ok) {
        live_cols = ((ray_t**)ray_data(tbl))[1];
        if (!live_cols || live_cols->rc != 1 || live_cols->len != ncols)
            inplace_ok = false;
    }
    for (int64_t c = 0; inplace_ok && c < ncols; c++) {
        ray_t* col = ((ray_t**)ray_data(live_cols))[c];
        /* col->rc != 1: a shared column would be COW'd inside the append,
         * and ray_cow releases the slot's reference — on a mid-append OOM
         * the slot would dangle.  Sharing also means someone is looking at
         * the pre-insert value, which is what the rebuild is for. */
        if (!col || RAY_IS_ERR(col) || col->rc != 1 ||
            /* An EMPTY LIST column is typeless — the rebuild adopts a
             * concrete type from the first payload.  A non-empty LIST
             * column is genuinely boxed and appends like any other:
             * pointer cells, each retained by ray_list_append. */
            (col->type == RAY_LIST && ray_len(col) == 0) ||
            RAY_IS_PARTED(col->type) || col->type == RAY_MAPCOMMON ||
            col->mmod != 0 ||
            (col->attrs & (RAY_ATTR_SLICE | RAY_ATTR_ARENA))) {
            inplace_ok = false;
            break;
        }
        /* The rebuild normalizes SYM columns to W64 runtime-domain ids;
         * appending into a narrow or store-domain column would not. */
        if (col->type == RAY_SYM &&
            ((col->attrs & RAY_SYM_W_MASK) != RAY_SYM_W64 ||
             col->sym_domain != ray_sym_runtime_domain())) {
            inplace_ok = false;
            break;
        }
    }
    if (inplace_ok) {
        ray_t** slots = (ray_t**)ray_data(live_cols);
        ray_t* err = NULL;
        for (int64_t c = 0; c < ncols && !err; c++) {
            ray_t* col = slots[c];
            ray_t* pay = row_elems[c];
            int8_t ct = col->type;
            ray_t* res;
            if (ct == RAY_LIST) {
                /* Boxed column: a table payload supplies a LIST whose cells
                 * are rows and is spliced; any other row shape boxes its
                 * payload (or a NULL slot) as one cell — the same contract
                 * as append_boxed_table_col, minus the copy. */
                if (tbl_row_list) {
                    if (!pay || RAY_IS_ERR(pay) || pay->type != RAY_LIST) {
                        res = ray_error("type", "insert: table payload for a LIST column must be LIST");
                    } else {
                        ray_t** cells = (ray_t**)ray_data(pay);
                        int64_t m = pay->len;
                        res = col;
                        for (int64_t k = 0; k < m && !RAY_IS_ERR(res); k++) {
                            res = ray_list_append(res, cells[k]);
                            if (!RAY_IS_ERR(res)) { slots[c] = res; col = res; }
                        }
                    }
                } else {
                    res = ray_list_append(col, pay);
                }
            } else if (!pay) {
                ray_t* null_atom = ray_typed_null(-ct);
                res = append_atom_to_col(col, null_atom);
                ray_release(null_atom);
            } else if (ray_is_atom(pay)) {
                res = append_atom_to_col(col, pay);
            } else if (pay->type == ct && ct == RAY_STR) {
                bool pay_nulls = ray_vec_may_have_nulls(pay);
                res = col;
                int64_t m = ray_len(pay);
                for (int64_t k = 0; k < m && !RAY_IS_ERR(res); k++) {
                    int64_t at = res->len;
                    if (pay_nulls && ray_vec_is_null(pay, k)) {
                        res = ray_str_vec_append(res, "", 0);
                        if (!RAY_IS_ERR(res)) ray_vec_set_null(res, at, true);
                    } else {
                        size_t slen = 0;
                        const char* sp = ray_str_vec_get(pay, k, &slen);
                        res = ray_str_vec_append(res, sp ? sp : "", sp ? slen : 0);
                    }
                    if (!RAY_IS_ERR(res)) { slots[c] = res; col = res; }
                }
            } else if (pay->type == ct && ct == RAY_SYM &&
                       ((pay->attrs & RAY_SYM_W_MASK) != RAY_SYM_W64 ||
                        pay->sym_domain != ray_sym_runtime_domain())) {
                /* Narrow or store-domain SYM payload: translate per cell. */
                bool pay_nulls = ray_vec_may_have_nulls(pay);
                res = col;
                int64_t m = ray_len(pay);
                for (int64_t k = 0; k < m && !RAY_IS_ERR(res); k++) {
                    int64_t sym_val = sym_cell_runtime_id(pay, k);
                    int64_t at = res->len;
                    res = ray_vec_append(res, &sym_val);
                    if (!RAY_IS_ERR(res)) {
                        slots[c] = res;
                        col = res;
                        if (pay_nulls && ray_vec_is_null(pay, k))
                            ray_vec_set_null(res, at, true);
                    }
                }
            } else if (pay->type == ct) {
                /* Same-typed fixed-width vector (SYM already W64 runtime
                 * here, so ids are verbatim): one reserve, one memcpy.
                 * Raw cells carry their null sentinels; propagate the
                 * flag with them. */
                res = ray_vec_append_raw(col, ray_data(pay), ray_len(pay));
                if (!RAY_IS_ERR(res) && ray_vec_may_have_nulls(pay))
                    res->attrs |= RAY_ATTR_HAS_NULLS;
            } else if (ray_is_vec(pay) || pay->type == RAY_LIST) {
                /* Differently-typed vector or generic list: coerce cell by
                 * cell, exactly like the rebuild does. */
                res = col;
                int64_t m = ray_len(pay);
                for (int64_t k = 0; k < m && !RAY_IS_ERR(res); k++) {
                    int alloc = 0;
                    ray_t* e = collection_elem(pay, k, &alloc);
                    if (!e) {
                        ray_t* na = ray_typed_null(-ct);
                        res = append_atom_to_col(res, na);
                        ray_release(na);
                    } else {
                        res = append_atom_to_col(res, e);
                        if (alloc) ray_release(e);
                    }
                    if (!RAY_IS_ERR(res)) { slots[c] = res; col = res; }
                }
            } else {
                res = append_atom_to_col(col, pay);
            }
            if (RAY_IS_ERR(res)) err = res;
            else slots[c] = res;
        }
        if (err) {
            /* Roll the lengths back so the binding keeps its pre-insert
             * value.  The append helpers leave the passed vector alive on
             * failure, so every slot pointer is still valid.  Boxed cells
             * were retained on append — release them before truncating. */
            for (int64_t c = 0; c < ncols; c++) {
                ray_t* col = slots[c];
                if (col->type == RAY_LIST) {
                    ray_t** cells = (ray_t**)ray_data(col);
                    for (int64_t k = nrows; k < col->len; k++)
                        if (cells[k]) ray_release(cells[k]);
                }
                col->len = nrows;
            }
        } else {
            /* insert appends without consulting keys, and in ascending row
             * order, so the map's "duplicates answer lowest row first"
             * invariant survives entering the new rows as they landed. */
            ukey_extend_after_append(tbl, nrows);
        }
        if (dict_vals) {
            ray_t** dv = (ray_t**)ray_data(dict_vals);
            for (int64_t c = 0; c < ncols; c++) if (dv[c]) ray_release(dv[c]);
            dict_vals->len = 0;
            ray_free(dict_vals);
        }
        if (tbl_row_list) {
            ray_t** trl = (ray_t**)ray_data(tbl_row_list);
            for (int64_t c = 0; c < ncols; c++) if (trl[c]) ray_release(trl[c]);
            tbl_row_list->len = 0;
            ray_free(tbl_row_list);
        }
        ray_release(tbl);
        ray_release(row_orig);
        return err ? err : ray_sym(inplace_sym);
    }

    ray_t* result = ray_table_new(ncols);
    if (RAY_IS_ERR(result)) return result;

    for (int64_t c = 0; c < ncols; c++) {
        int64_t col_name = ray_table_col_name(tbl, c);
        ray_t* orig_col = ray_table_get_col_idx(tbl, c);
        int8_t ct = orig_col->type;

        /* Typeless empty column (an empty () list, adopt-on-first-insert).
         * The table has 0 rows here, so there is nothing to copy and the
         * derived type drives the new column's storage. */
        if (ct == RAY_LIST && ray_len(orig_col) == 0)
            ct = typeless_col_type(row_elems[c]);

        if (ct == RAY_LIST) {
            ray_t* new_col = append_boxed_table_col(
                orig_col, row_elems[c], tbl_row_list != NULL);
            if (!new_col || RAY_IS_ERR(new_col)) {
                ray_release(result);
                return new_col ? new_col : ray_error("oom", NULL);
            }
            result = ray_table_add_col(result, col_name, new_col);
            ray_release(new_col);
            if (!result || RAY_IS_ERR(result)) return result;
            continue;
        }

        ray_t* new_col = ray_vec_new(ct, nrows + 1);
        if (RAY_IS_ERR(new_col)) { ray_release(result); return new_col; }

        /* Copy existing data */
        bool src_has_nulls = ray_vec_may_have_nulls(orig_col);
        if (ct == RAY_STR) {
            for (int64_t r = 0; r < nrows; r++) {
                if (src_has_nulls && ray_vec_is_null(orig_col, r)) {
                    new_col = ray_str_vec_append(new_col, "", 0);
                    if (!RAY_IS_ERR(new_col))
                        ray_vec_set_null(new_col, new_col->len - 1, true);
                } else {
                    size_t slen = 0;
                    const char* sp = ray_str_vec_get(orig_col, r, &slen);
                    new_col = ray_str_vec_append(new_col, sp ? sp : "", sp ? slen : 0);
                }
                if (RAY_IS_ERR(new_col)) { ray_release(result); return new_col; }
            }
        } else if (ct == RAY_SYM) {
            /* The output MIXES orig_col cells with the appended row value
             * (a runtime-domain atom) — re-express each copied cell in
             * the runtime domain (raw-copy fast path pre-flip). */
            for (int64_t r = 0; r < nrows; r++) {
                int64_t sym_val = sym_cell_runtime_id(orig_col, r);
                new_col = ray_vec_append(new_col, &sym_val);
                if (RAY_IS_ERR(new_col)) { ray_release(result); return new_col; }
                if (src_has_nulls && ray_vec_is_null(orig_col, r))
                    ray_vec_set_null(new_col, new_col->len - 1, true);
            }
        } else {
            size_t elem_sz = ray_elem_size(ct);
            uint8_t* src = (uint8_t*)ray_data(orig_col);
            for (int64_t r = 0; r < nrows; r++) {
                new_col = ray_vec_append(new_col, src + r * elem_sz);
                if (RAY_IS_ERR(new_col)) { ray_release(result); return new_col; }
                if (src_has_nulls && ray_vec_is_null(orig_col, r))
                    ray_vec_set_null(new_col, new_col->len - 1, true);
            }
        }

        /* Append new row value(s) — atom for single row, vector for multi-row */
        if (!row_elems[c]) {
            /* NULL = null value for this column type */
            ray_t* null_atom = ray_typed_null(-ct);
            new_col = append_atom_to_col(new_col, null_atom);
            ray_release(null_atom);
        } else if (ray_is_atom(row_elems[c])) {
            new_col = append_atom_to_col(new_col, row_elems[c]);
        } else if (row_elems[c]->type == ct) {
            /* Same-typed vector → splice; preserves the column type. */
            ray_t* merged = ray_concat_fn(new_col, row_elems[c]);
            ray_release(new_col);
            new_col = merged;
        } else if (ray_is_vec(row_elems[c]) || row_elems[c]->type == RAY_LIST) {
            /* Generic list (or a differently-typed vector) is a multi-row
             * payload: append element-by-element so the column keeps its own
             * type instead of promoting to a generic LIST via concat. */
            ray_t* payload = row_elems[c];
            int64_t m = ray_len(payload);
            for (int64_t k = 0; k < m; k++) {
                int alloc = 0;
                ray_t* e = collection_elem(payload, k, &alloc);
                if (!e) {
                    ray_t* na = ray_typed_null(-ct);
                    new_col = append_atom_to_col(new_col, na);
                    ray_release(na);
                } else {
                    new_col = append_atom_to_col(new_col, e);
                    if (alloc) ray_release(e);
                }
                if (RAY_IS_ERR(new_col)) break;
            }
        } else {
            new_col = append_atom_to_col(new_col, row_elems[c]);
        }
        if (RAY_IS_ERR(new_col)) { ray_release(result); return new_col; }

        result = ray_table_add_col(result, col_name, new_col);
        ray_release(new_col);
        if (RAY_IS_ERR(result)) return result;
    }

    /* Cleanup dict_vals, tbl_row_list, and original row */
    if (dict_vals) {
        ray_t** dv = (ray_t**)ray_data(dict_vals);
        for (int64_t c = 0; c < ncols; c++) if (dv[c]) ray_release(dv[c]);
        dict_vals->len = 0; /* prevent ray_free from double-releasing children */
        ray_free(dict_vals);
    }
    if (tbl_row_list) {
        ray_t** trl = (ray_t**)ray_data(tbl_row_list);
        for (int64_t c = 0; c < ncols; c++) if (trl[c]) ray_release(trl[c]);
        tbl_row_list->len = 0;
        ray_free(tbl_row_list);
    }
    ray_release(tbl);
    ray_release(row_orig);

    /* In-place: store the new table in the env and return the symbol. */
    if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
        ray_env_set(inplace_sym, result);
        ray_release(result);
        return ray_sym(inplace_sym);
    }
    return result;
}

/* (upsert table key_col (list val1 val2 ...)) — update row if key matches, else insert.
 * Special form: first arg may be 'sym for in-place, other args are evaluated. */
/* ---- Upsert one-pass path (#475) ------------------------------------
 * The legacy arms below find the target row with a linear key scan and
 * rebuild the whole table to write one cell — per payload row, so a
 * named-table upsert costs O(rows present) per row and a batch costs
 * the same as the equivalent loop.  This path does one pass per call:
 * matched rows have their cells written in place, missing keys append
 * (amortized, as in insert's #455 path), and a batch resolves its keys
 * through one hash over the target built once per call.  A single-row
 * call consults a fresh hash index on the key column when one is
 * attached, and otherwise runs one typed scan.
 *
 * When the binding is the sole owner the live columns are mutated;
 * otherwise (a value target, or a shared table/column) the columns are
 * copied once and the same pass runs on the copy, which keeps the
 * value semantics of the rebuild at O(rows + payload) per call rather
 * than per row.  Any gate miss returns NULL with *handled = false and
 * the legacy arms run unchanged.
 *
 * Every cell is type-checked before anything is written, so a bad
 * payload leaves the table untouched.  Only an allocation failure can
 * interrupt the pass; appended rows are then rolled back, while cells
 * already overwritten in place keep their new values. */

static bool upsert_key_type_ok(int8_t t) {
    switch (t) {
    case RAY_BOOL: case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
    case RAY_DATE: case RAY_TIME: case RAY_TIMESTAMP:
    case RAY_F64: case RAY_SYM: case RAY_STR: case RAY_GUID:
        return true;
    default:
        return false;
    }
}

static bool upsert_col_ok(ray_t* col) {
    if (!col || RAY_IS_ERR(col)) return false;
    /* An EMPTY LIST column is typeless — the legacy insert arm adopts a
     * concrete type from the first payload; a non-empty LIST column is
     * genuinely boxed and is written like any other. */
    if (col->type == RAY_LIST && ray_len(col) == 0) return false;
    if (RAY_IS_PARTED(col->type) || col->type == RAY_MAPCOMMON) return false;
    if (col->mmod != 0) return false;
    if (col->attrs & (RAY_ATTR_SLICE | RAY_ATTR_ARENA)) return false;
    /* Cells are written as W64 runtime-domain ids (append_atom_to_col);
     * a narrow or store-domain column needs the legacy re-expression. */
    if (col->type == RAY_SYM &&
        ((col->attrs & RAY_SYM_W_MASK) != RAY_SYM_W64 ||
         col->sym_domain != ray_sym_runtime_domain()))
        return false;
    return true;
}

/* Integer-family cell widened to int64 through its storage width. */
static int64_t upsert_int_cell(const ray_t* col, int64_t r) {
    const void* d = ray_data((ray_t*)col);
    switch (col->type) {
    case RAY_BOOL: case RAY_U8: return (int64_t)((const uint8_t*)d)[r];
    case RAY_I16:               return (int64_t)((const int16_t*)d)[r];
    case RAY_I32: case RAY_DATE: case RAY_TIME:
                                return (int64_t)((const int32_t*)d)[r];
    default:                    return ((const int64_t*)d)[r];
    }
}

static int64_t upsert_atom_int(ray_t* a) {
    return a->type == -RAY_BOOL ? (int64_t)a->b8 : elem_as_i64(a);
}

static inline __attribute__((always_inline)) uint64_t upsert_hash_cell(ray_t* col, int64_t r) {
    switch (col->type) {
    case RAY_F64: return ray_hash_f64(((const double*)ray_data(col))[r]);
    case RAY_SYM: return ray_hash_i64(ray_read_sym(ray_data(col), r, col->type, col->attrs));
    case RAY_STR: {
        size_t n = 0;
        const char* s = ray_str_vec_get(col, r, &n);
        return ray_hash_bytes(s ? s : "", n);
    }
    case RAY_GUID: return ray_hash_bytes((const uint8_t*)ray_data(col) + r * 16, 16);
    default:       return ray_hash_i64(upsert_int_cell(col, r));
    }
}

static uint64_t upsert_hash_atom(int8_t ct, ray_t* a) {
    static const uint8_t zero_guid[16] = {0};
    switch (ct) {
    case RAY_F64: return ray_hash_f64(as_f64(a));
    case RAY_SYM: return ray_hash_i64(a->i64);
    case RAY_STR: {
        const char* s = ray_str_ptr(a);
        return ray_hash_bytes(s ? s : "", ray_str_len(a));
    }
    case RAY_GUID: return ray_hash_bytes(a->obj ? ray_data(a->obj) : zero_guid, 16);
    default:       return ray_hash_i64(upsert_atom_int(a));
    }
}

/* Cell-vs-atom equality with the legacy scan's semantics: F64 by value
 * (NaN never matches, so a null F64 key always appends), SYM by raw id,
 * STR by bytes, integers widened. */
static bool upsert_cell_eq_atom(ray_t* col, int64_t r, ray_t* a) {
    static const uint8_t zero_guid[16] = {0};
    switch (col->type) {
    case RAY_F64: return ((const double*)ray_data(col))[r] == as_f64(a);
    case RAY_SYM: return ray_read_sym(ray_data(col), r, col->type, col->attrs) == a->i64;
    case RAY_STR: {
        size_t n = 0;
        const char* s = ray_str_vec_get(col, r, &n);
        size_t m = ray_str_len(a);
        const char* p = ray_str_ptr(a);
        return n == m && (m == 0 || (s && p && memcmp(s, p, m) == 0));
    }
    case RAY_GUID:
        return memcmp((const uint8_t*)ray_data(col) + r * 16,
                      a->obj ? ray_data(a->obj) : zero_guid, 16) == 0;
    default: return upsert_int_cell(col, r) == upsert_atom_int(a);
    }
}

#define UPSERT_HASH_SEED 0x9E3779B97F4A7C15ULL

static uint64_t upsert_hash_row(ray_t** slots, const int64_t* kci, int64_t nk, int64_t r) {
    uint64_t h = UPSERT_HASH_SEED;
    for (int64_t k = 0; k < nk; k++)
        h = ray_hash_combine(h, upsert_hash_cell(slots[kci[k]], r));
    return h;
}

static uint64_t upsert_hash_atoms(ray_t** slots, const int64_t* kci, int64_t nk, ray_t** cells) {
    uint64_t h = UPSERT_HASH_SEED;
    for (int64_t k = 0; k < nk; k++)
        h = ray_hash_combine(h, upsert_hash_atom(slots[kci[k]]->type, cells[kci[k]]));
    return h;
}

static bool upsert_row_eq_atoms(ray_t** slots, const int64_t* kci, int64_t nk, int64_t r, ray_t** cells) {
    for (int64_t k = 0; k < nk; k++)
        if (!upsert_cell_eq_atom(slots[kci[k]], r, cells[kci[k]])) return false;
    return true;
}

/* Open-addressing map from key tuple to row: slots hold row+1, 0 is
 * empty.  Every target row is entered in row order, so a probe walking
 * from the hash slot meets duplicates lowest-row first — the same row
 * the legacy scan picks.  Rows appended during the pass are entered as
 * they land, so a key repeated inside one batch updates the row its
 * first occurrence appended. */
typedef struct {
    ray_t*   hdr;
    int64_t* slot;
    uint64_t mask;
} upsert_map_t;

/* Slot encoding.  An occupied slot holds row + 1, so it is always >= 1 and
 * the whole negative range is free for markers.  A deleted entry becomes a
 * tombstone rather than being cleared: a hole in the middle of a probe chain
 * would end the walk early and orphan every entry displaced past it. */
#define UKEY_SLOT_EMPTY ((int64_t)0)
#define UKEY_SLOT_TOMB  ((int64_t)-1)

/* An occupied slot carries the key's hash beside the row: the low 32 bits of
 * the hash in the high half, row + 1 in the low half.  The hash makes two
 * things cheap that used to read the table: a probe rejects a colliding slot
 * without touching its key cells, and growing the map re-places every entry
 * from the slots themselves (ukey_grow) instead of re-hashing every row.
 * Both markers stay reserved: row + 1 is never 0 and never all-ones. */
#define UKEY_PACK(h, row) ((int64_t)(((uint64_t)(uint32_t)(h) << 32) | (uint64_t)((row) + 1)))
#define UKEY_ROW(e)       ((int64_t)((uint64_t)(e) & 0xFFFFFFFFULL) - 1)
#define UKEY_H32(e)       ((uint32_t)((uint64_t)(e) >> 32))
/* Rows must fit the low half (the two markers take the ends), and the slot
 * index must come from the stored 32 bits, so the table stops at 2^32 slots. */
#define UKEY_MAX_ROWS     ((int64_t)0xFFFFFFFDLL)
#define UKEY_MAX_CAP      ((uint64_t)1 << 32)

static bool upsert_map_init(upsert_map_t* mp, int64_t entries) {
    uint64_t cap = 16;
    while (cap < (uint64_t)entries * 2) cap <<= 1;
    mp->slot = (int64_t*)scratch_alloc(&mp->hdr, cap * sizeof(int64_t));
    if (!mp->slot) return false;
    memset(mp->slot, 0, cap * sizeof(int64_t));
    mp->mask = cap - 1;
    return true;
}

/* Entries are written in ascending row order, and a tombstone is never
 * reused: taking one would put a later row ahead of an earlier one in its
 * chain, which changes which of two duplicate keys a keyed upsert updates. */
static void upsert_map_put(upsert_map_t* mp, uint64_t h, int64_t row) {
    uint64_t s = h & mp->mask;
    while (mp->slot[s] != UKEY_SLOT_EMPTY) s = (s + 1) & mp->mask;
    mp->slot[s] = UKEY_PACK(h, row);
}

/* Rows nrows0..n-1 were just appended in row order.  Entering them costs
 * what the append already cost, and it keeps the map describing the table:
 * left behind, it sends the next upsert back to rebuilding over every row,
 * which is the whole cost this map exists to avoid.  A map that cannot take
 * them is dropped rather than left naming a shorter table. */
static void ukey_extend_after_append(ray_t* tbl, int64_t nrows0) {
    ray_index_t* ix = ray_table_ukey_get(tbl);
    if (!ix) return;
    int64_t n = ray_table_nrows(tbl);
    if (ix->u.ukey.nrows != nrows0 || n < nrows0 ||
        (uint64_t)(n + ix->u.ukey.n_tomb) * 2 > ix->u.ukey.mask + 1) {
        ray_table_ukey_drop(tbl);
        return;
    }
    ray_t* cols = ((ray_t**)ray_data(tbl))[1];
    if (!cols || RAY_IS_ERR(cols)) { ray_table_ukey_drop(tbl); return; }
    ray_t** slots = (ray_t**)ray_data(cols);
    int64_t kci[RAY_UKEY_MAX_COLS];
    int64_t nk = ix->u.ukey.nk;
    for (int64_t k = 0; k < nk; k++) kci[k] = ix->u.ukey.kci[k];
    upsert_map_t map = { NULL, (int64_t*)ray_data(ix->u.ukey.slots), ix->u.ukey.mask };
    for (int64_t r = nrows0; r < n; r++)
        upsert_map_put(&map, upsert_hash_row(slots, kci, nk, r), r);
    ix->u.ukey.nrows = n;
}

/* Does this table-resident map still describe the table, and can it take
 * `m` more rows at the load factor it was sized for?  A row count that moved
 * without the map means something else wrote the table; a different key
 * means a different upsert. */
static bool ukey_describes(ray_index_t* ix, const int64_t* kci, int64_t nk,
                           int64_t nrows0) {
    if (ix->u.ukey.nrows != nrows0 || ix->u.ukey.nk != nk) return false;
    for (int64_t k = 0; k < nk; k++)
        if (ix->u.ukey.kci[k] != (int16_t)kci[k]) return false;
    return true;
}

/* Tombstones hold slots without being rows, so the load test counts them
 * alongside the live entries. */
static bool ukey_has_room(ray_index_t* ix, int64_t nrows0, int64_t m) {
    return (uint64_t)(nrows0 + m + ix->u.ukey.n_tomb) * 2 <= ix->u.ukey.mask + 1;
}

/* Grow a map that still describes the table but has run out of room, the way
 * sym.c's ht_grow_to grows the intern table: every slot already carries its
 * key's hash, so the bigger table is filled from the old slots — no key column
 * is read and no key is hashed again.  Tombstones vanish on the way, which is
 * what makes this worth doing after a delete too.  On failure the caller keeps
 * the old map and falls back to dropping it. */
static bool ukey_grow(ray_index_t* ix, int64_t entries) {
    uint64_t old_cap = ix->u.ukey.mask + 1;
    uint64_t cap = 16;
    while (cap < (uint64_t)entries * 2) {
        if (cap >= UKEY_MAX_CAP) return false;
        cap <<= 1;
    }
    if (cap < old_cap) cap = old_cap;
    if (cap > UKEY_MAX_CAP) return false;
    ray_t* fresh = ray_vec_new(RAY_I64, (int64_t)cap);
    if (!fresh || RAY_IS_ERR(fresh)) { if (fresh) ray_release(fresh); return false; }
    fresh->len = (int64_t)cap;
    int64_t* dst = (int64_t*)ray_data(fresh);
    memset(dst, 0, (size_t)cap * sizeof(int64_t));
    const int64_t* src = (const int64_t*)ray_data(ix->u.ukey.slots);
    uint64_t mask = cap - 1;
    /* Start at an empty slot and walk the old table cyclically, rather than
     * from slot 0: entries that share a probe chain must be re-placed in the
     * order they sit in it, or two rows carrying the same key swap places
     * whenever their chain wraps past the end of the array — and the next
     * keyed upsert then updates the later row instead of the lowest one.  A
     * chain never contains an empty slot, so starting at one enters every
     * chain at its beginning; the load factor of at most a half guarantees
     * such a slot exists. */
    uint64_t start = 0;
    while (start < old_cap && src[start] != UKEY_SLOT_EMPTY) start++;
    for (uint64_t i = 0; i < old_cap; i++) {
        int64_t e = src[(start + i) & (old_cap - 1)];
        if (e == UKEY_SLOT_EMPTY || e == UKEY_SLOT_TOMB) continue;
        uint64_t t = (uint64_t)UKEY_H32(e) & mask;
        while (dst[t] != UKEY_SLOT_EMPTY) t = (t + 1) & mask;
        dst[t] = e;
    }
    ray_release(ix->u.ukey.slots);
    ix->u.ukey.slots  = fresh;
    ix->u.ukey.mask   = mask;
    ix->u.ukey.n_tomb = 0;
    return true;
}

static int64_t upsert_map_find(upsert_map_t* mp, uint64_t h, ray_t** slots,
                               const int64_t* kci, int64_t nk, ray_t** cells) {
    uint64_t s = h & mp->mask;
    for (;;) {
        int64_t e = mp->slot[s];
        if (e == UKEY_SLOT_EMPTY) return -1;
        if (e != UKEY_SLOT_TOMB && UKEY_H32(e) == (uint32_t)h &&
            upsert_row_eq_atoms(slots, kci, nk, UKEY_ROW(e), cells)) return UKEY_ROW(e);
        s = (s + 1) & mp->mask;
    }
}

/* Single-row lookup: a fresh hash index on a lone key column answers in
 * O(1); otherwise one typed scan. */
static int64_t upsert_find_row(ray_t** slots, const int64_t* kci, int64_t nk,
                               ray_t** cells, int64_t nrows) {
    if (nk == 1) {
        ray_t* col = slots[kci[0]];
        ray_t* a = cells[kci[0]];
        int8_t t = col->type;
        if (t == RAY_SYM || (t != RAY_F64 && t != RAY_STR && t != RAY_GUID)) {
            int64_t key = (t == RAY_SYM) ? a->i64 : upsert_atom_int(a);
            int64_t hit = ray_index_find_row(col, key);
            if (hit >= -1) return hit;
            if (t == RAY_I64 || t == RAY_TIMESTAMP || t == RAY_SYM) {
                const int64_t* d = (const int64_t*)ray_data(col);
                for (int64_t r = 0; r < nrows; r++) if (d[r] == key) return r;
                return -1;
            }
            if (t == RAY_I32 || t == RAY_DATE || t == RAY_TIME) {
                const int32_t* d = (const int32_t*)ray_data(col);
                for (int64_t r = 0; r < nrows; r++) if ((int64_t)d[r] == key) return r;
                return -1;
            }
        }
    }
    for (int64_t r = 0; r < nrows; r++)
        if (upsert_row_eq_atoms(slots, kci, nk, r, cells)) return r;
    return -1;
}

/* Would append_atom_to_col accept `cell` for a column of type `ct`?
 * NULL on yes, the error it would raise otherwise. */
static ray_t* upsert_cell_fits(int8_t ct, ray_t* cell) {
    if (ct == RAY_LIST) return NULL;
    if (!ray_is_atom(cell))
        return ray_error("type", "upsert: %s column requires an atom value, got %s",
                         ray_type_name(ct), ray_type_name(cell->type));
    if (RAY_ATOM_IS_NULL(cell)) return NULL;
    int8_t at = cell->type;
    bool is_int = (at == -RAY_I64 || at == -RAY_I32 || at == -RAY_I16 || at == -RAY_U8);
    bool ok;
    switch (ct) {
    case RAY_BOOL:      ok = (at == -RAY_BOOL); break;
    case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
                        ok = is_int; break;
    case RAY_F64:       ok = (at == -RAY_F64 || is_int); break;
    case RAY_DATE:      ok = (at == -RAY_DATE); break;
    case RAY_TIME:      ok = (at == -RAY_TIME); break;
    case RAY_TIMESTAMP: ok = (at == -RAY_TIMESTAMP); break;
    case RAY_GUID:      ok = (at == -RAY_GUID); break;
    case RAY_SYM:       ok = (at == -RAY_SYM); break;
    case RAY_STR:       ok = (at == -RAY_STR); break;
    default:
        return ray_error("type", "upsert: unsupported column type %s", ray_type_name(ct));
    }
    if (ok) return NULL;
    return ray_error("type", "upsert: value type %s does not match %s column",
                     ray_type_name(at), ray_type_name(ct));
}

/* Overwrite one cell of slots[c] with `cell`.  Boxed columns swap the
 * pointer; STR columns write through ray_str_vec_set; fixed-width
 * columns append through append_atom_to_col (which owns the coercion
 * and null encoding) and move the new tail cell into place.  Any
 * attached index is dropped first: an overwritten cell would otherwise
 * leave a fresh-looking index answering with stale rows. */
static ray_t* upsert_write_cell(ray_t** slots, int64_t c, int64_t row, ray_t* cell) {
    ray_t* col = slots[c];
    if (col->type == RAY_LIST) {
        ray_t** cells = (ray_t**)ray_data(col);
        ray_t* old = cells[row];
        ray_retain(cell);
        cells[row] = cell;
        if (old) ray_release(old);
        return NULL;
    }
    ray_index_drop(&slots[c]);
    col = slots[c];
    if (RAY_IS_ERR(col)) return col;
    col->attrs &= (uint8_t)~RAY_ATTR_SORTED;
    ray_t* res;
    if (col->type == RAY_STR) {
        res = RAY_ATOM_IS_NULL(cell)
            ? ray_str_vec_set(col, row, "", 0)
            : ray_str_vec_set(col, row, ray_str_ptr(cell), ray_str_len(cell));
        if (RAY_IS_ERR(res)) return res;
        slots[c] = res;
        return NULL;
    }
    int64_t tail = col->len;
    res = append_atom_to_col(col, cell);
    if (RAY_IS_ERR(res)) return res;
    slots[c] = res;
    size_t esz = ray_sym_elem_size(res->type, res->attrs);
    uint8_t* d = (uint8_t*)ray_data(res);
    memcpy(d + (size_t)row * esz, d + (size_t)tail * esz, esz);
    res->len = tail;
    return NULL;
}

/* Append one row; a NULL cell lands as the column's null (a NULL slot
 * for a boxed column), mirroring the legacy insert arm. */
static ray_t* upsert_append_row(ray_t** slots, int64_t ncols, ray_t** cells) {
    for (int64_t c = 0; c < ncols; c++) {
        ray_t* col = slots[c];
        ray_t* cell = cells[c];
        ray_t* res;
        if (col->type == RAY_LIST) {
            res = ray_list_append(col, cell);
        } else if (!cell) {
            ray_t* na = ray_typed_null(-col->type);
            res = append_atom_to_col(col, na);
            ray_release(na);
        } else {
            res = append_atom_to_col(col, cell);
        }
        if (RAY_IS_ERR(res)) return res;
        slots[c] = res;
    }
    return NULL;
}

static void upsert_rollback_appends(ray_t** slots, int64_t ncols, int64_t nrows0) {
    for (int64_t c = 0; c < ncols; c++) {
        ray_t* col = slots[c];
        if (col->type == RAY_LIST) {
            ray_t** cells = (ray_t**)ray_data(col);
            for (int64_t k = nrows0; k < col->len; k++)
                if (cells[k]) ray_release(cells[k]);
        }
        col->len = nrows0;
    }
}

/* Can a multi-row payload be written column by column, without boxing a
 * cell?  Every target column needs a payload vector of its exact type and
 * width class: no coercion, no partial payload, no boxed or STR column,
 * and SYM ids already in the runtime domain the target stores. */
static bool upsert_vec_payload_ok(ray_t* tbl, ray_t** src_cols, int64_t ncols, int64_t m) {
    for (int64_t c = 0; c < ncols; c++) {
        ray_t* sc = src_cols[c];
        ray_t* tc = ray_table_get_col_idx(tbl, c);
        if (!sc || RAY_IS_ERR(sc) || sc->type != tc->type) return false;
        if (sc->attrs & RAY_ATTR_SLICE) return false;
        if (sc->len != m) return false;
        switch (tc->type) {
        case RAY_BOOL: case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
        case RAY_F64: case RAY_DATE: case RAY_TIME: case RAY_TIMESTAMP:
        case RAY_GUID:
            break;
        case RAY_SYM:
            if (ray_sym_vec_domain(sc) != ray_sym_runtime_domain()) return false;
            break;
        default:
            return false;
        }
    }
    return true;
}

/* Cell-vs-cell equality for two columns of one type, with the semantics of
 * upsert_cell_eq_atom: F64 by value, SYM by runtime id, integers widened. */
static inline __attribute__((always_inline)) bool
upsert_cell_eq_cell(ray_t* a, int64_t ra, ray_t* b, int64_t rb) {
    switch (a->type) {
    case RAY_F64:
        return ((const double*)ray_data(a))[ra] == ((const double*)ray_data(b))[rb];
    case RAY_SYM:
        return ray_read_sym(ray_data(a), ra, RAY_SYM, a->attrs) ==
               ray_read_sym(ray_data(b), rb, RAY_SYM, b->attrs);
    case RAY_GUID:
        return memcmp((const uint8_t*)ray_data(a) + ra * 16,
                      (const uint8_t*)ray_data(b) + rb * 16, 16) == 0;
    default:
        return upsert_int_cell(a, ra) == upsert_int_cell(b, rb);
    }
}

/* Map probe for payload row `r`.  An entry past nrows0 names a row this
 * batch appends but has not written yet, so its key is read from the
 * payload row that will land there (app[row - nrows0]). */
/* Per-row probe on the column-wise path.  Forced inline, with the hash and the
 * cell compare below it: once the resolve loop exists in two instances GCC's
 * inline budget leaves these as calls, and a call per row cost 10-20% on
 * tables that fit in cache. */
static inline __attribute__((always_inline)) int64_t
upsert_map_find_src(upsert_map_t* mp, uint64_t h, ray_t** slots,
                    const int64_t* kci, int64_t nk, ray_t** src,
                    int64_t r, int64_t nrows0, const int64_t* app) {
    uint64_t s = h & mp->mask;
    for (;;) {
        int64_t e = mp->slot[s];
        if (e == UKEY_SLOT_EMPTY) return -1;
        if (e != UKEY_SLOT_TOMB && UKEY_H32(e) == (uint32_t)h) {
            int64_t row = UKEY_ROW(e);
            bool eq = true;
            for (int64_t k = 0; k < nk && eq; k++) {
                ray_t* sc = src[kci[k]];
                eq = row < nrows0
                    ? upsert_cell_eq_cell(slots[kci[k]], row, sc, r)
                    : upsert_cell_eq_cell(sc, app[row - nrows0], sc, r);
            }
            if (eq) return row;
        }
        s = (s + 1) & mp->mask;
    }
}

/* Move `n` elements of `src` into `dst`: dst[di(i)] = src[si(i)], where a
 * NULL index array means the identity.  Null state travels in-band as the
 * sentinel; the HAS_NULLS gate is raised when a written value is null. */
/* Past this much memory a keyed upsert's probes and scattered writes miss the
 * per-core cache, and loading them ahead pays; below it the extra pass and
 * the prefetches are pure overhead.  Measured with a 512 KiB L2: a 640 KiB
 * probe set was 3-8% slower with them, an 890 KiB one 19% faster. */
#define UPSERT_PF_MIN_BYTES ((size_t)768 << 10)

static inline __attribute__((always_inline)) void
upsert_vec_move_impl(ray_t* dst, const int64_t* di, int64_t dbase,
                     ray_t* src, const int64_t* si, int64_t n, bool pf) {
    void* d = ray_data(dst);
    const void* s = ray_data(src);
    #define UPSERT_MOVE(T) do {                                              \
        for (int64_t i = 0; i < n; i++) {                                    \
            if (pf && i + 16 < n && di[i + 16] >= 0)                         \
                __builtin_prefetch(&((T*)d)[di[i + 16]], 1);                 \
            int64_t o = di ? di[i] : dbase + i;                              \
            if (o < 0) continue;                                             \
            ((T*)d)[o] = ((const T*)s)[si ? si[i] : i];                      \
        } } while (0)
    switch (dst->type) {
    case RAY_BOOL: case RAY_U8:  UPSERT_MOVE(uint8_t); break;
    case RAY_I16:                UPSERT_MOVE(int16_t); break;
    case RAY_I32: case RAY_DATE: case RAY_TIME: UPSERT_MOVE(int32_t); break;
    case RAY_I64: case RAY_TIMESTAMP: UPSERT_MOVE(int64_t); break;
    case RAY_F64:                UPSERT_MOVE(double); break;
    case RAY_GUID:
        for (int64_t i = 0; i < n; i++) {
            int64_t o = di ? di[i] : dbase + i;
            if (o < 0) continue;
            memcpy((uint8_t*)d + o * 16, (const uint8_t*)s + (si ? si[i] : i) * 16, 16);
        }
        break;
    case RAY_SYM: {
        bool any_null = false;
        for (int64_t i = 0; i < n; i++) {
            int64_t o = di ? di[i] : dbase + i;
            if (o < 0) continue;
            int64_t id = ray_read_sym(s, si ? si[i] : i, RAY_SYM, src->attrs);
            ((int64_t*)d)[o] = id;
            any_null |= (id == 0);
        }
        if (any_null) dst->attrs |= RAY_ATTR_HAS_NULLS;
        return;
    }
    default: return;
    }
    #undef UPSERT_MOVE
    if (!ray_vec_may_have_nulls(src)) return;
    for (int64_t i = 0; i < n; i++) {
        int64_t o = di ? di[i] : dbase + i;
        if (o >= 0 && ray_vec_is_null(src, si ? si[i] : i)) {
            dst->attrs |= RAY_ATTR_HAS_NULLS;
            return;
        }
    }
}

/* Each look-ahead loop exists twice, with its flag a compile-time constant,
 * and the choice is made once per batch or column.  A runtime flag tested
 * inside the loops cost 15-30% on tables that fit in cache, more than the
 * prefetching itself: the plain instances must compile to the loops that
 * never had it. */
static void upsert_vec_move(ray_t* dst, const int64_t* di, int64_t dbase,
                            ray_t* src, const int64_t* si, int64_t n) {
    upsert_vec_move_impl(dst, di, dbase, src, si, n, false);
}

static void upsert_vec_scatter_ahead(ray_t* dst, const int64_t* di, ray_t* src, int64_t n) {
    upsert_vec_move_impl(dst, di, 0, src, NULL, n, true);
}

/* Resolve every payload row to its target row, in payload order: dst[r] is
 * the matched row or -1 for a row this batch appends, app[] lists the payload
 * rows to append.  A new key enters the map at the row it will take, so a
 * repeat inside the batch finds it.
 *
 * On a table larger than the cache nearly all of a probe's time is two
 * misses — the slot, then the key cells of the row it names — and taken one
 * row at a time they never overlap.  The look-ahead instance hashes the batch
 * first (dst[r] holds row r's hash until this pass overwrites it) and loads
 * both a few rows ahead.  The separate hash pass pays on such a table even for
 * a batch shorter than the lookahead, where no prefetch fires. */
static inline __attribute__((always_inline)) void
upsert_resolve_impl(upsert_map_t* map, ray_t** slots, const int64_t* kci, int64_t nk,
                    ray_t** src, int64_t m, int64_t nrows0, int64_t* dst, int64_t* app,
                    int64_t* napp_out, int64_t* nhit_out, bool ahead) {
    enum { PF_SLOT = 16, PF_ROW = 8 };
    const char* kbase[RAY_UKEY_MAX_COLS];
    size_t kesz[RAY_UKEY_MAX_COLS];
    int64_t npf = 0;
    if (ahead) {
        npf = nk < RAY_UKEY_MAX_COLS ? nk : RAY_UKEY_MAX_COLS;
        for (int64_t k = 0; k < npf; k++) {
            ray_t* kc = slots[kci[k]];
            kbase[k] = (const char*)ray_data(kc);
            kesz[k] = ray_sym_elem_size(kc->type, kc->attrs);
        }
        for (int64_t r = 0; r < m; r++) {
            uint64_t h = UPSERT_HASH_SEED;
            for (int64_t k = 0; k < nk; k++)
                h = ray_hash_combine(h, upsert_hash_cell(src[kci[k]], r));
            dst[r] = (int64_t)h;
        }
    }

    int64_t napp = 0, nhit = 0;
    for (int64_t r = 0; r < m; r++) {
        uint64_t h;
        if (ahead) {
            if (r + PF_SLOT < m)
                __builtin_prefetch(&map->slot[(uint64_t)dst[r + PF_SLOT] & map->mask]);
            if (r + PF_ROW < m) {
                int64_t e = map->slot[(uint64_t)dst[r + PF_ROW] & map->mask];
                if (e != UKEY_SLOT_EMPTY && e != UKEY_SLOT_TOMB &&
                    UKEY_ROW(e) < nrows0)
                    for (int64_t k = 0; k < npf; k++)
                        __builtin_prefetch(kbase[k] + (size_t)UKEY_ROW(e) * kesz[k]);
            }
            h = (uint64_t)dst[r];
        } else {
            h = UPSERT_HASH_SEED;
            for (int64_t k = 0; k < nk; k++)
                h = ray_hash_combine(h, upsert_hash_cell(src[kci[k]], r));
        }
        int64_t hit = upsert_map_find_src(map, h, slots, kci, nk, src, r, nrows0, app);
        if (hit < 0) {
            upsert_map_put(map, h, nrows0 + napp);
            app[napp++] = r;
            dst[r] = -1;                    /* written by the append */
        } else {
            dst[r] = hit;
            nhit++;
        }
    }
    *napp_out = napp;
    *nhit_out = nhit;
}

/* Only the look-ahead instance is out of line; the plain one is expanded in
 * upsert_apply_vectors itself, where the loop lived before the look-ahead
 * existed — out of line it measured 4-9% slower on tables that fit in cache.
 * The look-ahead one must stay out of line: expanded into its only caller it
 * measured 12-26% slower on inserts into large tables. */
__attribute__((noinline))
static void upsert_resolve_ahead(upsert_map_t* map, ray_t** slots, const int64_t* kci, int64_t nk,
                                 ray_t** src, int64_t m, int64_t nrows0, int64_t* dst, int64_t* app,
                                 int64_t* napp, int64_t* nhit) {
    upsert_resolve_impl(map, slots, kci, nk, src, m, nrows0, dst, app, napp, nhit, true);
}

/* Column-wise body of upsert_apply for a payload upsert_vec_payload_ok
 * admitted.  Rows resolve in payload order exactly as the per-row loop
 * does — a new key enters the map at the row it will take, so a repeat
 * inside the batch finds it — then each column is written once: the new
 * rows appended in one go, and every matched cell scattered in payload
 * order, so a key repeated in the batch still ends at its last value.
 * Appends come first because a repeat may target a row just appended. */
static ray_t* upsert_apply_vectors(ray_t** slots, int64_t ncols,
                                   const int64_t* kci, int64_t nk,
                                   ray_t** src, int64_t m, int64_t nrows0,
                                   upsert_map_t* map, int64_t* nrows) {
    ray_t* dst_hdr = NULL; ray_t* app_hdr = NULL; ray_t* buf_hdr = NULL;
    int64_t* dst = (int64_t*)scratch_alloc(&dst_hdr, (size_t)m * sizeof(int64_t));
    int64_t* app = dst ? (int64_t*)scratch_alloc(&app_hdr, (size_t)m * sizeof(int64_t)) : NULL;
    ray_t* err = NULL;
    if (!dst || !app) { err = ray_error("oom", NULL); goto out; }

    /* The probed memory is the slots plus the key columns; past the per-core
     * cache the look-ahead instance pays, below it only costs. */
    size_t probe_bytes = (size_t)(map->mask + 1) * sizeof(int64_t);
    for (int64_t k = 0; k < nk && k < RAY_UKEY_MAX_COLS; k++) {
        ray_t* kc = slots[kci[k]];
        probe_bytes += (size_t)nrows0 * ray_sym_elem_size(kc->type, kc->attrs);
    }
    int64_t napp = 0, nhit = 0;
    if (probe_bytes >= UPSERT_PF_MIN_BYTES)
        upsert_resolve_ahead(map, slots, kci, nk, src, m, nrows0, dst, app, &napp, &nhit);
    else
        upsert_resolve_impl(map, slots, kci, nk, src, m, nrows0, dst, app, &napp, &nhit, false);

    if (napp > 0) {
        void* buf = scratch_alloc(&buf_hdr, (size_t)napp * 16);
        if (!buf) { err = ray_error("oom", NULL); goto out; }
        memset(buf, 0, (size_t)napp * 16);
        for (int64_t c = 0; c < ncols; c++) {
            /* Grow the column by napp zeroed cells in one step (and drop
             * its index), then gather the new rows straight into the tail. */
            int64_t tail = slots[c]->len;
            ray_t* res = ray_vec_append_raw(slots[c], buf, napp);
            if (RAY_IS_ERR(res)) { err = res; goto out; }
            slots[c] = res;
            upsert_vec_move(res, NULL, tail, src[c], app, napp);
        }
    }

    if (nhit > 0) {
        for (int64_t c = 0; c < ncols; c++) {
            bool is_key = false;
            for (int64_t k = 0; k < nk; k++) if (kci[k] == c) { is_key = true; break; }
            if (is_key) continue;           /* matched: already equal */
            ray_index_drop(&slots[c]);
            if (RAY_IS_ERR(slots[c])) { err = slots[c]; goto out; }
            slots[c]->attrs &= (uint8_t)~RAY_ATTR_SORTED;
            ray_t* col = slots[c];
            if ((size_t)col->len * ray_sym_elem_size(col->type, col->attrs) >= UPSERT_PF_MIN_BYTES)
                upsert_vec_scatter_ahead(col, dst, src[c], m);
            else
                upsert_vec_move(col, dst, 0, src[c], NULL, m);
        }
    }
    *nrows = nrows0 + napp;

out:
    if (buf_hdr) scratch_free(buf_hdr);
    if (app_hdr) scratch_free(app_hdr);
    if (dst_hdr) scratch_free(dst_hdr);
    return err;
}

/* One-pass upsert.  `kci[nk]` are the key column positions.  The
 * payload is either `src_cols` (ncols collections-or-NULL, each `m`
 * rows) or, when src_cols is NULL, `atoms` (ncols atoms-or-NULL, m==1).
 * `tbl` is borrowed.  Returns the result (the binding's symbol, or a
 * fresh table) or an error, and sets *handled; returns NULL with
 * *handled false when a gate misses and the legacy arms should run. */
static ray_t* upsert_apply(ray_t* tbl, int64_t inplace_sym,
                           const int64_t* kci, int64_t nk,
                           ray_t** src_cols, ray_t** atoms, int64_t m,
                           bool* handled) {
    *handled = false;
    int64_t ncols = ray_table_ncols(tbl);
    int64_t nrows0 = ray_table_nrows(tbl);
    if (ncols <= 0 || m <= 0) return NULL;

    for (int64_t c = 0; c < ncols; c++)
        if (!upsert_col_ok(ray_table_get_col_idx(tbl, c))) return NULL;
    for (int64_t k = 0; k < nk; k++) {
        ray_t* kc = ray_table_get_col_idx(tbl, kci[k]);
        if (!upsert_key_type_ok(kc->type)) return NULL;
    }

    /* Sole-owner check, as in insert's in-place path: the binding holds
     * one reference and the caller the other; the column list and every
     * column must be unshared so a write cannot be observed elsewhere. */
    bool inplace = inplace_sym >= 0 && tbl->rc == 2 && tbl->mmod == 0 &&
                   !(tbl->attrs & (RAY_ATTR_SLICE | RAY_ATTR_ARENA));
    ray_t* live_cols = NULL;
    if (inplace) {
        live_cols = ((ray_t**)ray_data(tbl))[1];
        if (!live_cols || live_cols->rc != 1 || live_cols->len != ncols)
            inplace = false;
    }
    for (int64_t c = 0; inplace && c < ncols; c++)
        if (((ray_t**)ray_data(live_cols))[c]->rc != 1) inplace = false;

    /* A batch of typed vectors matching the target is written column by
     * column; everything else is boxed into cells below. */
    bool vec_path = src_cols && m > 1 && upsert_vec_payload_ok(tbl, src_cols, ncols, m);

    /* Materialize the payload cells once: cells[r*ncols + c], with a
     * parallel ownership byte for atoms allocated out of typed vectors. */
    ray_t* cells_hdr = NULL; ray_t* owned_hdr = NULL;
    size_t ncell = vec_path ? 1 : (size_t)m * (size_t)ncols;
    ray_t** cells = (ray_t**)scratch_alloc(&cells_hdr, ncell * sizeof(ray_t*));
    uint8_t* owned = cells ? (uint8_t*)scratch_alloc(&owned_hdr, ncell) : NULL;
    if (!cells || !owned) {
        if (cells_hdr) scratch_free(cells_hdr);
        if (owned_hdr) scratch_free(owned_hdr);
        *handled = true;
        return ray_error("oom", NULL);
    }
    memset(cells, 0, ncell * sizeof(ray_t*));
    memset(owned, 0, ncell);

    ray_t* err = NULL;
    upsert_map_t map = { NULL, NULL, 0 };
    ray_index_t* ukey = NULL;   /* table-resident map, borrowed from tbl */
    ray_t* work = NULL;

    if (vec_path) {
        /* typed payload vectors are read in place */
    } else if (src_cols) {
        for (int64_t c = 0; c < ncols && !err; c++) {
            ray_t* sc = src_cols[c];
            if (!sc) continue;
            if (!is_collection(sc)) {
                err = ray_error("type", "upsert: multi-row value for column %lld must be a list or vector, got %s",
                                (long long)c, ray_type_name(sc->type));
                break;
            }
            if (ray_len(sc) != m) {
                err = ray_error("length", "upsert: column %lld has %lld values, key has %lld",
                                (long long)c, (long long)ray_len(sc), (long long)m);
                break;
            }
            for (int64_t r = 0; r < m; r++) {
                int alloc = 0;
                ray_t* e = collection_elem(sc, r, &alloc);
                if (e && RAY_IS_ERR(e)) { err = e; break; }
                cells[(size_t)r * ncols + c] = e;
                owned[(size_t)r * ncols + c] = (uint8_t)(alloc && e);
            }
        }
    } else {
        for (int64_t c = 0; c < ncols; c++) cells[c] = atoms[c];
    }

    /* Type-check every cell before writing anything.  An admitted vector
     * payload was checked once per column by upsert_vec_payload_ok. */
    for (int64_t r = 0; r < m && !err && !vec_path; r++) {
        ray_t** rc = cells + (size_t)r * ncols;
        for (int64_t k = 0; k < nk && !err; k++) {
            ray_t* kc = ray_table_get_col_idx(tbl, kci[k]);
            ray_t* a = rc[kci[k]];
            if (!a) {
                err = ray_error("domain", "upsert: key value missing");
            } else if (kc->type == RAY_STR && a->type != -RAY_STR) {
                err = ray_error("type", "upsert: key column is str but key value is %s", ray_type_name(a->type));
            } else if (kc->type == RAY_SYM && a->type != -RAY_SYM) {
                err = ray_error("type", "upsert: key column is sym but key value is %s", ray_type_name(a->type));
            }
        }
        for (int64_t c = 0; c < ncols && !err; c++)
            if (rc[c]) err = upsert_cell_fits(ray_table_get_col_idx(tbl, c)->type, rc[c]);
    }

    /* Work table: the live columns, or a private copy of them. */
    ray_t** slots = NULL;
    if (!err) {
        if (inplace) {
            slots = (ray_t**)ray_data(live_cols);
        } else {
            work = ray_table_new(ncols);
            if (RAY_IS_ERR(work)) { err = work; work = NULL; }
            for (int64_t c = 0; c < ncols && !err; c++) {
                ray_t* orig = ray_table_get_col_idx(tbl, c);
                ray_retain(orig);
                ray_t* cp = ray_cow(orig);
                if (!cp || RAY_IS_ERR(cp)) {
                    ray_release(orig);      /* ray_cow leaves the input alive on failure */
                    err = cp ? cp : ray_error("oom", NULL);
                    break;
                }
                work = ray_table_add_col(work, ray_table_col_name(tbl, c), cp);
                ray_release(cp);
                if (RAY_IS_ERR(work)) { err = work; work = NULL; }
            }
            if (!err) slots = (ray_t**)ray_data(((ray_t**)ray_data(work))[1]);
        }
    }

    /* The key->row map is what a bulk upsert actually costs.  Built over every
     * existing row on entry and freed on exit, it made a sequence of batches
     * quadratic in the table size: the profile of a growing table is almost
     * entirely hashing rows that were already there.  When the table is a
     * named binding mutated in place its object identity survives between
     * calls, so the map lives on the table and is carried across them; the
     * per-call cost becomes the batch, not the table.
     *
     * This covers a single-row upsert too, which otherwise scans the table
     * for its one key.  It matters less for the scan it saves than for the
     * map it keeps honest: a single row appended outside the map moves the
     * row count, and the next batch then finds the map stale and rebuilds
     * it over everything — one interleaved row per batch was enough to put
     * the whole quadratic back. */
    if (!err) {
        /* A key wider than the arm gets no map — but an already attached one
         * must go, not simply be skipped.  This call still writes every
         * non-key column of a matched row in place, and those may be the
         * attached map's key columns; with no row appended the row count does
         * not move either, so ukey_fits would accept it as fresh next time
         * and answer from stale rows. */
        if (inplace && nk > RAY_UKEY_MAX_COLS) ray_table_ukey_drop(tbl);
        if (inplace && nk <= RAY_UKEY_MAX_COLS) {
            ukey = ray_table_ukey_get(tbl);
            /* A map that no longer describes the table is stale and must go.
             * One that only ran out of room is grown from its own slots — the
             * rebuild it replaces re-reads and re-hashes every key column. */
            if (ukey && (!ukey_describes(ukey, kci, nk, nrows0) ||
                         (!ukey_has_room(ukey, nrows0, m) &&
                          !ukey_grow(ukey, nrows0 + m)))) {
                ray_table_ukey_drop(tbl);
                ukey = NULL;
            }
            /* Split on `ukey` first.  Folding the row-count test into the
             * same condition sent the `ukey == NULL && too many rows` case
             * into the else, which dereferences the map that is not there.
             * Declining the map for size must fall through to the scratch
             * one below, exactly as a failed build does. */
            if (!ukey) {
                if (nrows0 + m <= UKEY_MAX_ROWS) {
                    ray_t* idx = ray_index_build_ukey(kci, nk, nrows0 + m);
                    if (idx && !RAY_IS_ERR(idx)) {
                        if (ray_table_ukey_attach(tbl, idx)) ukey = ray_table_ukey_get(tbl);
                        else ray_release(idx);
                    }
                    /* A map we could not build or attach is not an error: fall
                     * through to the scratch one and answer at the old cost. */
                    if (ukey) {
                        map.slot = (int64_t*)ray_data(ukey->u.ukey.slots);
                        map.mask = ukey->u.ukey.mask;
                        for (int64_t r = 0; r < nrows0 && !err; r++)
                            upsert_map_put(&map, upsert_hash_row(slots, kci, nk, r), r);
                        ukey->u.ukey.nrows = nrows0;
                    }
                }
            } else {
                map.slot = (int64_t*)ray_data(ukey->u.ukey.slots);
                map.mask = ukey->u.ukey.mask;
            }
        }
        /* Without a table to hang it on, a map is only worth building for a
         * batch: for one row it costs the scan it would replace and is then
         * thrown away.  A single row keeps the scan. */
        if (!ukey && !err && m > 1) {
            if (!upsert_map_init(&map, nrows0 + m)) err = ray_error("oom", NULL);
            for (int64_t r = 0; r < nrows0 && !err; r++)
                upsert_map_put(&map, upsert_hash_row(slots, kci, nk, r), r);
        }
    }

    int64_t nrows = nrows0;
    if (vec_path && !err)
        err = upsert_apply_vectors(slots, ncols, kci, nk, src_cols, m, nrows0, &map, &nrows);
    for (int64_t r = 0; r < m && !err && !vec_path; r++) {
        ray_t** rc = cells + (size_t)r * ncols;
        uint64_t h = 0;
        int64_t hit;
        if (map.slot) {
            h = upsert_hash_atoms(slots, kci, nk, rc);
            hit = upsert_map_find(&map, h, slots, kci, nk, rc);
        } else {
            hit = upsert_find_row(slots, kci, nk, rc, nrows);
        }
        if (hit >= 0) {
            for (int64_t c = 0; c < ncols && !err; c++) {
                if (!rc[c]) continue;               /* partial payload: keep */
                bool is_key = false;
                for (int64_t k = 0; k < nk; k++) if (kci[k] == c) { is_key = true; break; }
                if (is_key) continue;               /* matched: already equal */
                err = upsert_write_cell(slots, c, hit, rc[c]);
            }
        } else {
            err = upsert_append_row(slots, ncols, rc);
            if (!err) {
                if (map.slot) upsert_map_put(&map, h, nrows);
                nrows++;
            }
        }
    }

    if (err && slots) upsert_rollback_appends(slots, ncols, nrows0);

    /* A rolled-back batch leaves the map naming rows that no longer exist and
     * a half-built one names nothing, so a failed call must not leave it on
     * the table.  A successful one records the row count it now describes. */
    if (ukey) {
        if (err) { ray_table_ukey_drop(tbl); ukey = NULL; }
        else ukey->u.ukey.nrows = nrows;
    }

    if (map.hdr) scratch_free(map.hdr);
    for (size_t i = 0; i < ncell; i++) if (owned[i] && cells[i]) ray_release(cells[i]);
    scratch_free(cells_hdr);
    scratch_free(owned_hdr);

    *handled = true;
    if (err) {
        if (work) ray_release(work);
        return err;
    }
    if (inplace) return ray_sym(inplace_sym);
    if (inplace_sym >= 0) {
        ray_env_set(inplace_sym, work);
        ray_release(work);
        return ray_sym(inplace_sym);
    }
    return work;
}

ray_t* ray_upsert_fn(ray_t** args, int64_t n) {
    return ray_upsert(args, n);
}

ray_t* ray_upsert(ray_t** args, int64_t n) {
    if (n < 3) return ray_error("arity", "upsert: expects a target, key and row, got %lld args", (long long)n);

    /* Detect calling convention: already-evaluated args (from recursive call) vs raw parse tree */
    int64_t inplace_sym = -1;
    ray_t* tbl_raw = args[0];
    int already_eval = (tbl_raw && tbl_raw->type == RAY_TABLE);
    ray_t* tbl;

    if (already_eval) {
        tbl = tbl_raw;
        ray_retain(tbl);
    } else {
        tbl = ray_eval(tbl_raw);
        if (!tbl || RAY_IS_ERR(tbl)) return tbl ? tbl : ray_error("type", "upsert: target evaluation failed");
        if (tbl->type == -RAY_SYM) {
            /* Symbol value → resolve the named global for in-place upsert */
            inplace_sym = tbl->i64;
            ray_release(tbl);
            tbl = ray_env_get(inplace_sym);
            if (!tbl || RAY_IS_ERR(tbl)) return ray_error("domain", "upsert: target symbol is unbound");
            ray_retain(tbl);
        }
    }

    /* PARTED wrappers are segmented pointer arrays, not flat row storage.
     * Reject before evaluating key/row expressions so the legacy match scan
     * can never interpret wrapper pointers as scalar cells. */
    if (tbl->type == RAY_TABLE && table_has_parted_columns(tbl)) {
        ray_release(tbl);
        return ray_error("nyi", "upsert: parted tables are not supported; use insert with an explicit partition key");
    }

    ray_t* key_sym = already_eval ? (ray_retain(args[1]), args[1]) : ray_eval(args[1]);
    if (!key_sym || RAY_IS_ERR(key_sym)) { ray_release(tbl); return key_sym ? key_sym : ray_error("type", "upsert: key evaluation failed"); }

    ray_t* row = already_eval ? (ray_retain(args[2]), args[2]) : ray_eval(args[2]);
    if (!row || RAY_IS_ERR(row)) { ray_release(tbl); ray_release(key_sym); return row ? row : ray_error("type", "upsert: row evaluation failed"); }

    if (tbl->type != RAY_TABLE) { int8_t tbl_t = tbl->type; ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("type", "upsert: target must be a table, got %s", ray_type_name(tbl_t)); }
    if (!is_list(row) && row->type != RAY_TABLE && row->type != RAY_DICT) { int8_t row_t = row->type; ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("type", "upsert: row must be a list, table or dict, got %s", ray_type_name(row_t)); }

    int64_t ncols = ray_table_ncols(tbl);

    /* Table row: iterate row-by-row for proper upsert semantics */
    if (row->type == RAY_TABLE) {
        int64_t src_nrows = ray_table_nrows(row);
        int64_t src_ncols = ray_table_ncols(row);

        /* Zero-row payload → upsert is a no-op regardless of payload
         * schema.  Skip all schema-strictness here: rejecting an empty
         * partial payload (e.g. for missing key columns) regresses the
         * pre-existing "empty input = do nothing" behavior.  No data
         * flows, so neither silent-drop nor null-key crashes are
         * possible below. */
        if (src_nrows == 0) {
            ray_release(key_sym); ray_release(row);
            return tbl;
        }

        /* Schema-strictness (table payload is a PARTIAL view — columns
         * in target but not in source are intentionally null-filled).
         * We only need to reject:
         *   (a) a source column whose name isn't in the target (extra
         *       → silent drop of user data);
         *   (b) a source column name that appears more than once in the
         *       source (ambiguous);
         *   (c) a source column name whose target column appears more
         *       than once in `tbl` (name-keyed gather can't tell which
         *       target slot the value belongs to → silent duplication).
         * Duplicate target columns whose names don't appear in `row`
         * are harmless — they get null-filled like any other missing
         * column. */
        for (int64_t sc = 0; sc < src_ncols; sc++) {
            int64_t scn = ray_table_col_name(row, sc);
            int64_t tbl_matches = 0, src_matches = 0;
            for (int64_t i = 0; i < ncols;     i++) if (ray_table_col_name(tbl, i) == scn) tbl_matches++;
            for (int64_t i = 0; i < src_ncols; i++) if (ray_table_col_name(row, i) == scn) src_matches++;
            if (tbl_matches != 1 || src_matches != 1) {
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("value", NULL);
            }
        }

        /* Partial updates may null-fill ordinary columns, but the key
         * column(s) MUST be present — otherwise the recursive upsert
         * reads a NULL from row_elems[key_col] and segfaults.  Resolve
         * key names from key_sym and require each to appear in row. */
        ray_t* keynames_hdr = NULL;
        int64_t* key_names = NULL;
        int64_t n_key = 0;
        if (key_sym->type == -RAY_SYM) {
            key_names = (int64_t*)scratch_alloc(&keynames_hdr, sizeof(int64_t));
            if (!key_names) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
            key_names[n_key++] = key_sym->i64;
        } else if (key_sym->type == -RAY_I64) {
            int64_t k = key_sym->i64;
            if (k <= 0 || k > ncols) {
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("domain", "upsert: key count must be 1..ncols, got %lld", (long long)k);
            }
            key_names = (int64_t*)scratch_alloc(&keynames_hdr, (size_t)k * sizeof(int64_t));
            if (!key_names) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
            for (int64_t i = 0; i < k; i++)
                key_names[n_key++] = ray_table_col_name(tbl, i);
        } else {
            int8_t ks_t = key_sym->type;            /* capture BEFORE free */
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", "upsert: key must be a symbol or integer, got %s", ray_type_name(ks_t));
        }
        for (int64_t k = 0; k < n_key; k++) {
            int found = 0;
            for (int64_t i = 0; i < src_ncols; i++)
                if (ray_table_col_name(row, i) == key_names[k]) { found = 1; break; }
            if (!found) {
                scratch_free(keynames_hdr);
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("value", NULL);
            }
        }
        /* Key column positions in the target.  Every key name is a
         * payload column, and each payload column matches exactly one
         * target column (checked above), so each resolves uniquely. */
        ray_t* tkci_hdr = NULL;
        int64_t* tkci = (int64_t*)scratch_alloc(&tkci_hdr, (size_t)n_key * sizeof(int64_t));
        if (!tkci) { scratch_free(keynames_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
        for (int64_t k = 0; k < n_key; k++) {
            tkci[k] = -1;
            for (int64_t c = 0; c < ncols; c++)
                if (ray_table_col_name(tbl, c) == key_names[k]) { tkci[k] = c; break; }
            if (tkci[k] < 0) {
                scratch_free(tkci_hdr); scratch_free(keynames_hdr);
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("domain", "upsert: key column not found in target table");
            }
        }
        scratch_free(keynames_hdr);

        /* Gather source columns in target order (now guaranteed 1-to-1). */
        ray_t* srcs_hdr = NULL;
        ray_t** src_cols = (ray_t**)scratch_alloc(&srcs_hdr, (size_t)ncols * sizeof(ray_t*));
        if (!src_cols) { scratch_free(tkci_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
        for (int64_t c = 0; c < ncols; c++) {
            int64_t cn = ray_table_col_name(tbl, c);
            src_cols[c] = ray_table_get_col(row, cn);
        }

        bool fast_handled = false;
        ray_t* fast = upsert_apply(tbl, inplace_sym, tkci, n_key, src_cols, NULL, src_nrows, &fast_handled);
        scratch_free(tkci_hdr);
        if (fast_handled) {
            scratch_free(srcs_hdr);
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return fast;
        }

        ray_t* cur_tbl = tbl;
        ray_retain(cur_tbl);
        for (int64_t r = 0; r < src_nrows; r++) {
            ray_t* single = ray_alloc(ncols * sizeof(ray_t*));
            if (!single) { scratch_free(srcs_hdr); ray_release(cur_tbl); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
            single->type = RAY_LIST;
            single->len = ncols;
            ray_t** sr = (ray_t**)ray_data(single);
            for (int64_t c = 0; c < ncols; c++) {
                int alloc = 0;
                sr[c] = src_cols[c] ? collection_elem(src_cols[c], r, &alloc) : NULL;
                if (!alloc && sr[c]) ray_retain(sr[c]);
            }
            ray_t* upsert_args[3] = { cur_tbl, key_sym, single };
            ray_t* new_tbl = ray_upsert_fn(upsert_args, 3);
            for (int64_t c = 0; c < ncols; c++) if (sr[c]) ray_release(sr[c]);
            single->len = 0;
            ray_free(single);
            ray_release(cur_tbl);
            if (RAY_IS_ERR(new_tbl)) { scratch_free(srcs_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return new_tbl; }
            cur_tbl = new_tbl;
        }
        scratch_free(srcs_hdr);
        ray_release(tbl);
        ray_release(key_sym);
        ray_release(row);
        if (inplace_sym >= 0 && cur_tbl && !RAY_IS_ERR(cur_tbl)) {
            ray_env_set(inplace_sym, cur_tbl);
            ray_release(cur_tbl);
            return ray_sym(inplace_sym);
        }
        return cur_tbl;
    }

    /* Dict row: extract values in column order to create a plain list */
    ray_t* dict_row_list = NULL;
    if (row->type == RAY_DICT) {
        ray_t* dkeys = ray_dict_keys(row);
        ray_t* dvals = ray_dict_vals(row);
        if (!dkeys || dkeys->type != RAY_SYM || !dvals) {
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", "upsert: dict row keys must be symbols");
        }
        int64_t n_pairs = dkeys->len;

        /* Schema-strictness: every column name must appear exactly once
         * on each side.  Mirrors the table-payload path. */
        if (n_pairs != ncols) {
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("value", NULL);
        }
        for (int64_t c = 0; c < ncols; c++) {
            int64_t cn = ray_table_col_name(tbl, c);
            int64_t tbl_matches = 0, dict_matches = 0;
            for (int64_t i = 0; i < ncols; i++)
                if (ray_table_col_name(tbl, i) == cn) tbl_matches++;
            for (int64_t d = 0; d < n_pairs; d++) {
                /* dict-key cell vs runtime column NAME id (see insert) */
                int64_t dk = sym_cell_runtime_id(dkeys, d);
                if (dk == cn) dict_matches++;
            }
            if (tbl_matches != 1 || dict_matches != 1) {
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("value", NULL);
            }
        }

        dict_row_list = ray_alloc(ncols * sizeof(ray_t*));
        if (!dict_row_list) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
        dict_row_list->type = RAY_LIST;
        dict_row_list->len = ncols;
        ray_t** drl = (ray_t**)ray_data(dict_row_list);
        for (int64_t c = 0; c < ncols; c++) {
            int64_t col_name = ray_table_col_name(tbl, c);
            drl[c] = NULL;
            for (int64_t d = 0; d < n_pairs; d++) {
                int64_t dk = sym_cell_runtime_id(dkeys, d);
                if (dk != col_name) continue;
                if (dvals->type == RAY_LIST) {
                    drl[c] = ((ray_t**)ray_data(dvals))[d];
                    if (drl[c]) ray_retain(drl[c]);
                } else {
                    int alloc = 0;
                    drl[c] = collection_elem(dvals, d, &alloc);
                    if (!alloc && drl[c]) ray_retain(drl[c]);
                }
                break;
            }
        }
        ray_release(row);
        row = dict_row_list;
    }

    if (ray_len(row) != ncols) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("domain", "upsert: row has %lld values, target has %lld columns", (long long)ray_len(row), (long long)ncols); }

    ray_t** row_elems = (ray_t**)ray_data(row);
    int64_t nrows = ray_table_nrows(tbl);

    /* Determine key columns — integer N means "first N columns are keys" */
    int64_t n_key_cols = 1;
    ray_t* kci_hdr = NULL;
    int64_t* key_col_indices = NULL;
    if (key_sym->type == -RAY_SYM) {
        key_col_indices = (int64_t*)scratch_alloc(&kci_hdr, sizeof(int64_t));
        if (!key_col_indices) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
        key_col_indices[0] = -1;
        for (int64_t c = 0; c < ncols; c++) {
            if (ray_table_col_name(tbl, c) == key_sym->i64) {
                key_col_indices[0] = c;
                break;
            }
        }
        if (key_col_indices[0] < 0) { scratch_free(kci_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("domain", "upsert: key column not found in target table"); }
    } else if (key_sym->type == -RAY_I64) {
        n_key_cols = key_sym->i64;
        if (n_key_cols <= 0 || n_key_cols > ncols) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("domain", "upsert: key count must be 1..ncols, got %lld", (long long)n_key_cols); }
        key_col_indices = (int64_t*)scratch_alloc(&kci_hdr, (size_t)n_key_cols * sizeof(int64_t));
        if (!key_col_indices) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
        for (int64_t k = 0; k < n_key_cols; k++) key_col_indices[k] = k;
    } else {
        int8_t ks_t = key_sym->type;            /* capture BEFORE free */
        ray_release(tbl); ray_release(key_sym); ray_release(row);
        return ray_error("type", "upsert: key must be a symbol or integer, got %s", ray_type_name(ks_t));
    }

    /* Multi-row upsert: if row values are vectors, iterate row-by-row */
    ray_t* key_elem = row_elems[key_col_indices[0]];
    if (!key_elem) { scratch_free(kci_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("domain", "upsert: key value missing"); }
    {
        bool multi = ray_is_vec(key_elem) || key_elem->type == RAY_LIST;
        bool fast_handled = false;
        ray_t* fast = multi
            ? upsert_apply(tbl, inplace_sym, key_col_indices, n_key_cols, row_elems, NULL, ray_len(key_elem), &fast_handled)
            : upsert_apply(tbl, inplace_sym, key_col_indices, n_key_cols, NULL, row_elems, 1, &fast_handled);
        if (fast_handled) {
            scratch_free(kci_hdr);
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return fast;
        }
    }
    if (ray_is_vec(key_elem) || key_elem->type == RAY_LIST) {
        int64_t new_nrows = ray_len(key_elem);
        ray_t* cur_tbl = tbl;
        ray_retain(cur_tbl);
        for (int64_t r = 0; r < new_nrows; r++) {
            /* Build single-row list from multi-row columns */
            ray_t* single_row = ray_alloc(ncols * sizeof(ray_t*));
            if (!single_row) { scratch_free(kci_hdr); ray_release(cur_tbl); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
            single_row->type = RAY_LIST;
            single_row->len = ncols;
            ray_t** sr = (ray_t**)ray_data(single_row);
            for (int64_t c = 0; c < ncols; c++) {
                int alloc = 0;
                sr[c] = collection_elem(row_elems[c], r, &alloc);
                if (!alloc && sr[c]) ray_retain(sr[c]);
            }
            /* Upsert single row into current table */
            ray_t* upsert_args[3] = { cur_tbl, key_sym, single_row };
            ray_t* new_tbl = ray_upsert_fn(upsert_args, 3);
            /* Clean up single_row */
            for (int64_t c = 0; c < ncols; c++) if (sr[c]) ray_release(sr[c]);
            single_row->len = 0;
            ray_free(single_row);
            ray_release(cur_tbl);
            if (RAY_IS_ERR(new_tbl)) { scratch_free(kci_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return new_tbl; }
            cur_tbl = new_tbl;
        }
        scratch_free(kci_hdr);
        ray_release(tbl);
        ray_release(key_sym);
        ray_release(row);
        if (inplace_sym >= 0 && cur_tbl && !RAY_IS_ERR(cur_tbl)) {
            ray_env_set(inplace_sym, cur_tbl);
            ray_release(cur_tbl);
            return ray_sym(inplace_sym);
        }
        return cur_tbl;
    }

    /* Type-check key columns before searching.  For SYM keys, resolve
     * the literal key atom (runtime-domain by design) into each key
     * column's OWN domain once: cells below compare as raw indices
     * against this per-column want-id.  Absent from the column's
     * domain → -1 → matches nothing → insert path (correct).  Exact
     * no-op while the column is runtime-domain (want == atom->i64). */
    ray_t* kw_hdr = NULL;
    int64_t* key_sym_want = (int64_t*)scratch_alloc(&kw_hdr, (size_t)n_key_cols * sizeof(int64_t));
    if (!key_sym_want) { scratch_free(kci_hdr); ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("oom", NULL); }
    for (int64_t k = 0; k < n_key_cols; k++) {
        int64_t kci = key_col_indices[k];
        ray_t* key_col = ray_table_get_col_idx(tbl, kci);
        ray_t* key_atom = row_elems[kci];
        int8_t kt = key_col->type;
        if (kt == RAY_STR && key_atom->type != -RAY_STR) {
            int8_t ka_t = key_atom->type;            /* capture BEFORE free */
            scratch_free(kw_hdr); scratch_free(kci_hdr);
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", "upsert: key column is str but key value is %s", ray_type_name(ka_t));
        }
        if (kt == RAY_SYM && key_atom->type != -RAY_SYM) {
            int8_t ka_t = key_atom->type;            /* capture BEFORE free */
            scratch_free(kw_hdr); scratch_free(kci_hdr);
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", "upsert: key column is sym but key value is %s", ray_type_name(ka_t));
        }
        key_sym_want[k] = -1;
        if (kt == RAY_SYM) {
            if (ray_sym_vec_domain(key_col) == ray_sym_runtime_domain()) {
                key_sym_want[k] = key_atom->i64;
            } else {
                ray_t* ks = ray_sym_str(key_atom->i64);
                key_sym_want[k] = ks
                    ? ray_sym_vec_lookup(key_col, ray_str_ptr(ks), ray_str_len(ks))
                    : -1;
            }
        }
    }

    /* Find the row to update by composite key match */
    int64_t match_row = -1;
    for (int64_t r = 0; r < nrows; r++) {
        int match = 1;
        for (int64_t k = 0; k < n_key_cols && match; k++) {
            int64_t kci = key_col_indices[k];
            ray_t* key_col = ray_table_get_col_idx(tbl, kci);
            ray_t* key_atom = row_elems[kci];
            int8_t kt = key_col->type;
            if (kt == RAY_F64) {
                double needle = (key_atom->type == -RAY_F64) ? key_atom->f64 : (double)key_atom->i64;
                if (((double*)ray_data(key_col))[r] != needle) match = 0;
            } else if (kt == RAY_SYM) {
                /* raw index compare against the domain-resolved want id */
                if (ray_read_sym(ray_data(key_col), r, key_col->type, key_col->attrs) != key_sym_want[k]) match = 0;
            } else if (kt == RAY_STR) {
                const char* ns = ray_str_ptr(key_atom);
                size_t nl = ray_str_len(key_atom);
                size_t rl = 0;
                const char* rs = ray_str_vec_get(key_col, r, &rl);
                if (rl != nl || (nl > 0 && (!rs || !ns || memcmp(rs, ns, nl) != 0))) match = 0;
            } else {
                int64_t needle = elem_as_i64(key_atom);
                int64_t existing = (kt == RAY_I64 || kt == RAY_TIMESTAMP) ?
                    ((int64_t*)ray_data(key_col))[r] :
                    (kt == RAY_I32 || kt == RAY_DATE || kt == RAY_TIME) ?
                    (int64_t)((int32_t*)ray_data(key_col))[r] :
                    (kt == RAY_BOOL) ? (int64_t)((uint8_t*)ray_data(key_col))[r] :
                    ((int64_t*)ray_data(key_col))[r];
                if (existing != needle) match = 0;
            }
        }
        if (match) { match_row = r; break; }
    }
    scratch_free(kw_hdr);
    scratch_free(kci_hdr);

    if (match_row < 0) {
        /* Key not found — insert: pass pre-evaluated args */
        ray_t* insert_args[2] = { tbl, row };
        ray_t* result = ray_insert_fn(insert_args, 2);
        ray_release(tbl);
        ray_release(key_sym);
        ray_release(row);
        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            ray_env_set(inplace_sym, result);
            ray_release(result);
            return ray_sym(inplace_sym);
        }
        return result;
    }

    /* Key found — update that row */
    ray_t* result = ray_table_new(ncols);
    if (RAY_IS_ERR(result)) { ray_release(tbl); ray_release(key_sym); ray_release(row); return result; }

    for (int64_t c = 0; c < ncols; c++) {
        int64_t col_name = ray_table_col_name(tbl, c);
        ray_t* orig_col = ray_table_get_col_idx(tbl, c);
        int8_t ct = orig_col->type;

        ray_t* new_col = ray_vec_new(ct, nrows);
        if (RAY_IS_ERR(new_col)) { ray_release(result); ray_release(tbl); ray_release(key_sym); ray_release(row); return new_col; }

        /* If row_elems[c] is NULL (missing column), keep original values */
        int has_new_val = (row_elems[c] != NULL);

        /* Copied rows must carry their null state across the rebuild —
         * mirror ray_insert_fn's copy loops (sentinel survives the raw
         * copy but RAY_ATTR_HAS_NULLS would otherwise be dropped). */
        bool src_has_nulls = ray_vec_may_have_nulls(orig_col);
        if (ct == RAY_STR) {
            for (int64_t r = 0; r < nrows; r++) {
                if (r == match_row && has_new_val) {
                    new_col = append_atom_to_col(new_col, row_elems[c]);
                } else if (src_has_nulls && ray_vec_is_null(orig_col, r)) {
                    new_col = ray_str_vec_append(new_col, "", 0);
                    if (!RAY_IS_ERR(new_col))
                        ray_vec_set_null(new_col, new_col->len - 1, true);
                } else {
                    size_t slen = 0;
                    const char* sp = ray_str_vec_get(orig_col, r, &slen);
                    new_col = ray_str_vec_append(new_col, sp ? sp : "", sp ? slen : 0);
                }
                if (RAY_IS_ERR(new_col)) { ray_release(result); ray_release(tbl); ray_release(key_sym); ray_release(row); return new_col; }
            }
        } else if (ct == RAY_SYM) {
            /* MIXES orig_col cells with the replacement row value (a
             * runtime-domain atom) — re-express copied cells in the
             * runtime domain (raw-copy fast path pre-flip). */
            for (int64_t r = 0; r < nrows; r++) {
                if (r == match_row && has_new_val) {
                    new_col = append_atom_to_col(new_col, row_elems[c]);
                } else {
                    int64_t sym_val = sym_cell_runtime_id(orig_col, r);
                    new_col = ray_vec_append(new_col, &sym_val);
                    if (!RAY_IS_ERR(new_col) && src_has_nulls && ray_vec_is_null(orig_col, r))
                        ray_vec_set_null(new_col, new_col->len - 1, true);
                }
                if (RAY_IS_ERR(new_col)) { ray_release(result); ray_release(tbl); ray_release(key_sym); ray_release(row); return new_col; }
            }
        } else {
            size_t elem_sz = ray_elem_size(ct);
            uint8_t* src = (uint8_t*)ray_data(orig_col);
            for (int64_t r = 0; r < nrows; r++) {
                if (r == match_row && has_new_val) {
                    new_col = append_atom_to_col(new_col, row_elems[c]);
                } else {
                    new_col = ray_vec_append(new_col, src + r * elem_sz);
                    if (!RAY_IS_ERR(new_col) && src_has_nulls && ray_vec_is_null(orig_col, r))
                        ray_vec_set_null(new_col, new_col->len - 1, true);
                }
                if (RAY_IS_ERR(new_col)) { ray_release(result); ray_release(tbl); ray_release(key_sym); ray_release(row); return new_col; }
            }
        }

        result = ray_table_add_col(result, col_name, new_col);
        ray_release(new_col);
        if (RAY_IS_ERR(result)) { ray_release(tbl); ray_release(key_sym); ray_release(row); return result; }
    }

    ray_release(tbl);
    ray_release(key_sym);
    ray_release(row);

    if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
        ray_env_set(inplace_sym, result);
        ray_release(result);
        return ray_sym(inplace_sym);
    }
    return result;
}

/* ══════════════════════════════════════════
 * Join operations
 * ══════════════════════════════════════════ */

/* Shared implementation for left-join (join_type=1) and inner-join (join_type=0).
 * (left-join t1 t2 [key ...]) / (inner-join t1 t2 [key ...]) */
static ray_t* join_impl(ray_t** args, int64_t n, uint8_t join_type) {
    if (n < 3) return ray_error("arity", "join: expects left table, right table and keys, got %lld args", (long long)n);

    ray_t* left_tbl  = args[0];
    ray_t* right_tbl = args[1];
    ray_t* keys      = args[2];

    /* Detect alternative calling convention: (join [keys] t1 t2) */
    if (left_tbl->type != RAY_TABLE && args[1]->type == RAY_TABLE && args[2]->type == RAY_TABLE) {
        keys      = args[0];
        left_tbl  = args[1];
        right_tbl = args[2];
    }

    if (left_tbl->type != RAY_TABLE || right_tbl->type != RAY_TABLE)
        return ray_error("type", "join: both operands must be tables, got %s and %s",
                         ray_type_name(left_tbl->type), ray_type_name(right_tbl->type));
    ray_t* _bxk = NULL;
    keys = unbox_vec_arg(keys, &_bxk);
    if (RAY_IS_ERR(keys)) return keys;
    if (!is_list(keys))
        { if (_bxk) ray_release(_bxk); return ray_error("type", "join: keys must be a symbol list, got %s", ray_type_name(keys->type)); }

    int64_t nk = ray_len(keys);
    if (nk == 0) { if (_bxk) ray_release(_bxk); return ray_error("domain", "join: requires at least 1 key, got %lld", (long long)nk); }
    ray_t** key_elems = (ray_t**)ray_data(keys);

    ray_graph_t* g = ray_graph_new(left_tbl);
    if (!g) { if (_bxk) ray_release(_bxk); return ray_error("oom", NULL); }

    ray_op_t* left_node  = ray_const_table(g, left_tbl);
    ray_op_t* right_node = ray_const_table(g, right_tbl);

    /* Exact-size carve, 2*nk wide (unbounded — no fixed key-count ceiling).
     * ray_join copies every id out of lk/rk before it returns, so the carve
     * is freed right after that call. */
    ray_t* keyops_hdr = NULL;
    ray_op_t** keyops = (ray_op_t**)scratch_alloc(&keyops_hdr, (size_t)nk * 2 * sizeof(ray_op_t*));
    if (!keyops) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("oom", NULL); }
    ray_op_t** lk = keyops;
    ray_op_t** rk = keyops + nk;
    for (int64_t i = 0; i < nk; i++) {
        if (key_elems[i]->type != -RAY_SYM) {
            int8_t ke_t = key_elems[i]->type;            /* capture BEFORE free */
            scratch_free(keyops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", "join: key must be a symbol name, got %s", ray_type_name(ke_t));
        }
        int64_t key_name = key_elems[i]->i64;
        ray_t* left_key_col = ray_table_get_col(left_tbl, key_name);
        ray_t* right_key_col = ray_table_get_col(right_tbl, key_name);
        if (!left_key_col || !right_key_col) {
            ray_t* key_name_str = ray_sym_str(key_name);
            const char* side = !left_key_col ? "left" : "right";
            scratch_free(keyops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            if (key_name_str) {
                ray_t* err = ray_error("domain", "join: key column '%.*s' not found in %s table",
                                       (int)ray_str_len(key_name_str), ray_str_ptr(key_name_str), side);
                return err;
            }
            return ray_error("domain", "join: key column not found in %s table", side);
        }
        int8_t left_key_type = left_key_col->type;
        int8_t right_key_type = right_key_col->type;
        if (RAY_IS_PARTED(left_key_type)) left_key_type = (int8_t)RAY_PARTED_BASETYPE(left_key_type);
        if (RAY_IS_PARTED(right_key_type)) right_key_type = (int8_t)RAY_PARTED_BASETYPE(right_key_type);
        if ((left_key_type == RAY_STR) != (right_key_type == RAY_STR)) {
            scratch_free(keyops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", "join: string key columns must have string type on both sides");
        }
        ray_t* name_str = ray_sym_str(key_elems[i]->i64);
        if (!name_str) { scratch_free(keyops_hdr); ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "join: unknown key symbol"); }
        lk[i] = ray_scan(g, ray_str_ptr(name_str));
        rk[i] = ray_scan(g, ray_str_ptr(name_str));
        if (!lk[i] || !rk[i]) { scratch_free(keyops_hdr); ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "join: key column not found"); }
    }

    if (_bxk) ray_release(_bxk);

    ray_op_t* jn = ray_join(g, left_node, lk, right_node, rk,
                           (uint32_t)nk, join_type);
    scratch_free(keyops_hdr);
    if (!jn) { ray_graph_free(g); return ray_error("oom", NULL); }

    jn = ray_optimize(g, jn);
    ray_t* result = ray_execute(g, jn);
    ray_graph_free(g);
    return result;
}

ray_t* ray_left_join_fn(ray_t** args, int64_t n)  { return join_impl(args, n, 1); }
ray_t* ray_inner_join_fn(ray_t** args, int64_t n) { return join_impl(args, n, 0); }
ray_t* ray_full_join_fn(ray_t** args, int64_t n)  { return join_impl(args, n, 2); }

/* (antijoin left right [keys])
 * Anti-semi-join: keep rows from left that have NO match in right on keys. */
static ray_t* antijoin_impl(ray_t** args, int64_t n) {
    if (n < 3) return ray_error("arity", "antijoin: expects left table, right table and keys, got %lld args", (long long)n);

    ray_t* left_tbl  = args[0];
    ray_t* right_tbl = args[1];
    ray_t* keys      = args[2];

    /* Detect alternative calling convention: (antijoin [keys] t1 t2) */
    if (left_tbl->type != RAY_TABLE && args[1]->type == RAY_TABLE && args[2]->type == RAY_TABLE) {
        keys      = args[0];
        left_tbl  = args[1];
        right_tbl = args[2];
    }

    if (left_tbl->type != RAY_TABLE || right_tbl->type != RAY_TABLE)
        return ray_error("type", "antijoin: both operands must be tables, got %s and %s",
                         ray_type_name(left_tbl->type), ray_type_name(right_tbl->type));
    ray_t* _bxk = NULL;
    keys = unbox_vec_arg(keys, &_bxk);
    if (RAY_IS_ERR(keys)) return keys;
    if (!is_list(keys))
        { if (_bxk) ray_release(_bxk); return ray_error("type", "antijoin: keys must be a symbol list, got %s", ray_type_name(keys->type)); }

    int64_t nk = ray_len(keys);
    if (nk == 0) { if (_bxk) ray_release(_bxk); return ray_error("domain", "antijoin: requires at least 1 key, got %lld", (long long)nk); }
    ray_t** key_elems = (ray_t**)ray_data(keys);

    ray_graph_t* g = ray_graph_new(left_tbl);
    if (!g) { if (_bxk) ray_release(_bxk); return ray_error("oom", NULL); }

    ray_op_t* left_node  = ray_const_table(g, left_tbl);
    ray_op_t* right_node = ray_const_table(g, right_tbl);

    /* Exact-size carve, 2*nk wide (unbounded — no fixed key-count ceiling).
     * ray_antijoin copies every id out of lk/rk before it returns, so the
     * carve is freed right after that call. */
    ray_t* keyops_hdr = NULL;
    ray_op_t** keyops = (ray_op_t**)scratch_alloc(&keyops_hdr, (size_t)nk * 2 * sizeof(ray_op_t*));
    if (!keyops) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("oom", NULL); }
    ray_op_t** lk = keyops;
    ray_op_t** rk = keyops + nk;
    for (int64_t i = 0; i < nk; i++) {
        if (key_elems[i]->type != -RAY_SYM) {
            int8_t ke_t = key_elems[i]->type;            /* capture BEFORE free */
            scratch_free(keyops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", "antijoin: key must be a symbol name, got %s", ray_type_name(ke_t));
        }
        int64_t key_name = key_elems[i]->i64;
        ray_t* left_key_col = ray_table_get_col(left_tbl, key_name);
        ray_t* right_key_col = ray_table_get_col(right_tbl, key_name);
        if (!left_key_col || !right_key_col) {
            ray_t* key_name_str = ray_sym_str(key_name);
            const char* side = !left_key_col ? "left" : "right";
            scratch_free(keyops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            if (key_name_str)
                return ray_error("domain", "antijoin: key column '%.*s' not found in %s table",
                                 (int)ray_str_len(key_name_str), ray_str_ptr(key_name_str), side);
            return ray_error("domain", "antijoin: key column not found in %s table", side);
        }
        int8_t left_key_type = left_key_col->type;
        int8_t right_key_type = right_key_col->type;
        if (RAY_IS_PARTED(left_key_type)) left_key_type = (int8_t)RAY_PARTED_BASETYPE(left_key_type);
        if (RAY_IS_PARTED(right_key_type)) right_key_type = (int8_t)RAY_PARTED_BASETYPE(right_key_type);
        if ((left_key_type == RAY_STR) != (right_key_type == RAY_STR)) {
            scratch_free(keyops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", "antijoin: string key columns must have string type on both sides");
        }
        ray_t* name_str = ray_sym_str(key_elems[i]->i64);
        if (!name_str) { scratch_free(keyops_hdr); ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "antijoin: unknown key symbol"); }
        lk[i] = ray_scan(g, ray_str_ptr(name_str));
        rk[i] = ray_scan(g, ray_str_ptr(name_str));
        if (!lk[i] || !rk[i]) { scratch_free(keyops_hdr); ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "antijoin: key column not found"); }
    }

    if (_bxk) ray_release(_bxk);

    ray_op_t* jn = ray_antijoin(g, left_node, lk, right_node, rk, (uint32_t)nk);
    scratch_free(keyops_hdr);
    if (!jn) { ray_graph_free(g); return ray_error("oom", NULL); }

    jn = ray_optimize(g, jn);
    ray_t* result = ray_execute(g, jn);
    ray_graph_free(g);
    return result;
}

ray_t* ray_anti_join_fn(ray_t** args, int64_t n) { return antijoin_impl(args, n); }

/* ------------------------------------------------------------------------ */
/* window-join parallel worker                                              */
/* ------------------------------------------------------------------------ */

typedef struct {
    int64_t cnt;
    int64_t sum_i;
    double  sum_f;
    int64_t sum_sq_i;
    double  sum_sq_f;
    int64_t extreme_i;
    double  extreme_f;
    int64_t prod_i;
    double  prod_f;
} wj_acc_t;

/* All variable-count arrays are pointers into exact-size scratch carves owned
 * by window_join_impl (eq family sized by n_eq, agg family by the agg-dict
 * length).  No fixed cap: the eq-key and aggregate counts are memory-limited
 * only. */
typedef struct {
    int64_t  left_nrows;
    int64_t  right_nrows;
    int64_t  n_eq;
    int64_t  n_agg;
    int      mode;          /* 0 = wj (prevailing-quote seeded), 1 = wj1 (strict) */

    /* Left-row metadata — pre-extracted to int64 so workers can read
     * without touching any ray_t objects (no locking, no allocation). */
    const int64_t*  lo_arr;
    const int64_t*  hi_arr;
    const int64_t** left_eq_arr;    /* [n_eq] */

    /* Right-side sort order and time column (sorted rank -> original idx) */
    const int64_t*  right_sort;
    const int64_t*  rt_time_i;

    /* Right equality columns (raw), kept for binary-search compares [n_eq] */
    const void**    eq_data;
    const int8_t*   eq_type;
    const uint8_t*  eq_attrs;

    /* Per-agg metadata and preloaded sorted source vectors [n_agg] */
    const uint8_t*  agg_raw;
    const uint16_t* agg_ops;
    const int8_t*   agg_result_types;
    const int*      agg_is_float;
    const int64_t** sorted_i;
    const double**  sorted_f;
    const uint8_t** sorted_nn;

    /* Per-agg result output — writers index by lr directly [n_agg] */
    void**          result_data;
    uint8_t**       result_null;  /* 1 byte per row: 1 = null */

    /* Per-worker scratch, indexed by worker_id (no per-row allocation):
     *   acc slice    = acc_base    + worker_id * n_agg   (wj_acc_t[n_agg])
     *   target slice = target_base + worker_id * n_eq    (int64_t[n_eq]) */
    wj_acc_t*       acc_base;
    int64_t*        target_base;
} wj_scan_ctx_t;

/* Compare the equality tuple of sorted-right row `ri` against `target_eq`.
 * Returns <0, 0, >0.  Used to bracket a left row's eq partition. */
static inline int wj_eq_cmp(const wj_scan_ctx_t* c, int64_t ri,
                            const int64_t* target_eq, int64_t n_eq) {
    for (int64_t e = 0; e < n_eq; e++) {
        int64_t rv = read_col_i64(c->eq_data[e], ri, c->eq_type[e], c->eq_attrs[e]);
        if (rv < target_eq[e]) return -1;
        if (rv > target_eq[e]) return 1;
    }
    return 0;
}

static void wj_scan_fn(void* ctx_, uint32_t worker_id, int64_t start, int64_t end) {
    wj_scan_ctx_t* c = (wj_scan_ctx_t*)ctx_;
    int64_t  n_eq   = c->n_eq;
    int64_t  n_agg  = c->n_agg;
    /* Per-worker scratch slices (disjoint by worker_id → no contention). */
    wj_acc_t* acc       = c->acc_base    + (size_t)worker_id * n_agg;
    int64_t*  target_eq = c->target_base + (size_t)worker_id * n_eq;
    int64_t  rn     = c->right_nrows;
    const int64_t* right_sort = c->right_sort;
    const int64_t* rt_time_i  = c->rt_time_i;

    for (int64_t lr = start; lr < end; lr++) {
        int64_t lo = c->lo_arr[lr];
        int64_t hi = c->hi_arr[lr];

        for (int64_t e = 0; e < n_eq; e++)
            target_eq[e] = c->left_eq_arr[e][lr];

        /* Locate the eq partition [ps, pe) in the sorted right table — the
         * contiguous run of ranks whose equality tuple == target_eq.  Compare
         * eq keys only (the sort is (eq..., time), so the run is contiguous). */
        int64_t ps = 0, ps_hi = rn;
        while (ps < ps_hi) {
            int64_t m = (ps + ps_hi) >> 1;
            if (wj_eq_cmp(c, right_sort[m], target_eq, n_eq) < 0) ps = m + 1;
            else ps_hi = m;
        }
        int64_t pe = ps, pe_hi = rn;
        while (pe < pe_hi) {
            int64_t m = (pe + pe_hi) >> 1;
            if (wj_eq_cmp(c, right_sort[m], target_eq, n_eq) <= 0) pe = m + 1;
            else pe_hi = m;
        }

        /* Upper bound (both modes): first rank in [ps, pe) with time > hi. */
        int64_t ub = ps, ub_hi = pe;
        while (ub < ub_hi) {
            int64_t m = (ub + ub_hi) >> 1;
            if (rt_time_i[right_sort[m]] <= hi) ub = m + 1; else ub_hi = m;
        }

        /* Lower bound differs by mode:
         *   wj1 (mode 1): first rank in [ps, pe) with time >= lo (strict window).
         *   wj  (mode 0): the prevailing quote — rightmost rank with time <= lo,
         *                 i.e. (first rank with time > lo) - 1, clamped to ps. */
        int64_t lb;
        if (c->mode == 1) {
            lb = ps; int64_t lbh = pe;
            while (lb < lbh) {
                int64_t m = (lb + lbh) >> 1;
                if (rt_time_i[right_sort[m]] < lo) lb = m + 1; else lbh = m;
            }
        } else {
            int64_t r = ps, rh = pe;
            while (r < rh) {
                int64_t m = (r + rh) >> 1;
                if (rt_time_i[right_sort[m]] <= lo) r = m + 1; else rh = m;
            }
            lb = (r > ps) ? r - 1 : ps;
        }
        if (lb > ub) lb = ub;   /* empty window / no partition → null result */

        memset(acc, 0, (size_t)n_agg * sizeof(wj_acc_t));
        for (int64_t a = 0; a < n_agg; a++) {
            if (c->agg_ops[a] == OP_PROD) { acc[a].prod_i = 1; acc[a].prod_f = 1.0; }
        }

        /* Per-agg tight scan (hoisted switch, sequential SIMD-friendly read) */
        for (int64_t a = 0; a < n_agg; a++) {
            if (c->agg_raw[a]) continue;
            wj_acc_t* A = &acc[a];
            uint16_t op = c->agg_ops[a];
            if (op == OP_COUNT) { A->cnt += (ub - lb); continue; }

            const uint8_t* nn = c->sorted_nn[a];
            if (c->agg_is_float[a]) {
                const double* ss = c->sorted_f[a];
                switch (op) {
                case OP_SUM: case OP_AVG: {
                    double sum = 0; int64_t cnt = 0;
                    if (nn) { for (int64_t k = lb; k < ub; k++) if (nn[k]) { sum += ss[k]; cnt++; } }
                    else    { for (int64_t k = lb; k < ub; k++) sum += ss[k]; cnt = ub - lb; }
                    A->sum_f = sum; A->cnt = cnt; break;
                }
                case OP_VAR: case OP_VAR_POP:
                case OP_STDDEV: case OP_STDDEV_POP: {
                    double sum = 0, sum2 = 0; int64_t cnt = 0;
                    if (nn) {
                        for (int64_t k = lb; k < ub; k++)
                            if (nn[k]) { double v = ss[k]; sum += v; sum2 += v * v; cnt++; }
                    } else {
                        for (int64_t k = lb; k < ub; k++) { double v = ss[k]; sum += v; sum2 += v * v; }
                        cnt = ub - lb;
                    }
                    A->sum_f = sum; A->sum_sq_f = sum2; A->cnt = cnt; break;
                }
                case OP_PROD: {
                    double p = 1.0; int64_t cnt = 0;
                    if (nn) { for (int64_t k = lb; k < ub; k++) if (nn[k]) { p *= ss[k]; cnt++; } }
                    else    { for (int64_t k = lb; k < ub; k++) p *= ss[k]; cnt = ub - lb; }
                    A->prod_f = p; A->cnt = cnt; break;
                }
                case OP_MIN: {
                    int64_t k = lb;
                    if (nn) {
                        double best = 0; int64_t cnt = 0;
                        for (; k < ub; k++) if (nn[k]) { best = ss[k]; cnt = 1; k++; break; }
                        for (; k < ub; k++) if (nn[k]) { double v = ss[k]; if (v < best) best = v; cnt++; }
                        A->extreme_f = best; A->cnt = cnt;
                    } else if (k < ub) {
                        double best = ss[k++];
                        for (; k < ub; k++) { double v = ss[k]; if (v < best) best = v; }
                        A->extreme_f = best; A->cnt = ub - lb;
                    }
                    break;
                }
                case OP_MAX: {
                    int64_t k = lb;
                    if (nn) {
                        double best = 0; int64_t cnt = 0;
                        for (; k < ub; k++) if (nn[k]) { best = ss[k]; cnt = 1; k++; break; }
                        for (; k < ub; k++) if (nn[k]) { double v = ss[k]; if (v > best) best = v; cnt++; }
                        A->extreme_f = best; A->cnt = cnt;
                    } else if (k < ub) {
                        double best = ss[k++];
                        for (; k < ub; k++) { double v = ss[k]; if (v > best) best = v; }
                        A->extreme_f = best; A->cnt = ub - lb;
                    }
                    break;
                }
                case OP_FIRST: {
                    if (nn) {
                        int64_t cnt = 0;
                        for (int64_t k = lb; k < ub; k++) if (nn[k]) {
                            if (cnt == 0) A->extreme_f = ss[k];
                            cnt++;
                        }
                        A->cnt = cnt;
                    } else if (lb < ub) {
                        A->extreme_f = ss[lb]; A->cnt = ub - lb;
                    }
                    break;
                }
                case OP_LAST: {
                    if (nn) {
                        int64_t cnt = 0, last_k = -1;
                        for (int64_t k = lb; k < ub; k++) if (nn[k]) { last_k = k; cnt++; }
                        if (last_k >= 0) A->extreme_f = ss[last_k];
                        A->cnt = cnt;
                    } else if (lb < ub) {
                        A->extreme_f = ss[ub - 1]; A->cnt = ub - lb;
                    }
                    break;
                }
                default: break;
                }
            } else {
                const int64_t* ss = c->sorted_i[a];
                switch (op) {
                case OP_SUM: case OP_AVG: {
                    int64_t sum = 0; int64_t cnt = 0;
                    if (nn) { for (int64_t k = lb; k < ub; k++) if (nn[k]) { sum += ss[k]; cnt++; } }
                    else    { for (int64_t k = lb; k < ub; k++) sum += ss[k]; cnt = ub - lb; }
                    A->sum_i = sum; A->cnt = cnt; break;
                }
                case OP_VAR: case OP_VAR_POP:
                case OP_STDDEV: case OP_STDDEV_POP: {
                    int64_t sum = 0, sum2 = 0; int64_t cnt = 0;
                    if (nn) {
                        for (int64_t k = lb; k < ub; k++)
                            if (nn[k]) { int64_t v = ss[k]; sum += v; sum2 += v * v; cnt++; }
                    } else {
                        for (int64_t k = lb; k < ub; k++) { int64_t v = ss[k]; sum += v; sum2 += v * v; }
                        cnt = ub - lb;
                    }
                    A->sum_i = sum; A->sum_sq_i = sum2; A->cnt = cnt; break;
                }
                case OP_PROD: {
                    int64_t p = 1; int64_t cnt = 0;
                    if (nn) { for (int64_t k = lb; k < ub; k++) if (nn[k]) { p *= ss[k]; cnt++; } }
                    else    { for (int64_t k = lb; k < ub; k++) p *= ss[k]; cnt = ub - lb; }
                    A->prod_i = p; A->cnt = cnt; break;
                }
                case OP_MIN: {
                    int64_t k = lb;
                    if (nn) {
                        int64_t best = 0, cnt = 0;
                        for (; k < ub; k++) if (nn[k]) { best = ss[k]; cnt = 1; k++; break; }
                        for (; k < ub; k++) if (nn[k]) { int64_t v = ss[k]; if (v < best) best = v; cnt++; }
                        A->extreme_i = best; A->cnt = cnt;
                    } else if (k < ub) {
                        int64_t best = ss[k++];
                        for (; k < ub; k++) { int64_t v = ss[k]; if (v < best) best = v; }
                        A->extreme_i = best; A->cnt = ub - lb;
                    }
                    break;
                }
                case OP_MAX: {
                    int64_t k = lb;
                    if (nn) {
                        int64_t best = 0, cnt = 0;
                        for (; k < ub; k++) if (nn[k]) { best = ss[k]; cnt = 1; k++; break; }
                        for (; k < ub; k++) if (nn[k]) { int64_t v = ss[k]; if (v > best) best = v; cnt++; }
                        A->extreme_i = best; A->cnt = cnt;
                    } else if (k < ub) {
                        int64_t best = ss[k++];
                        for (; k < ub; k++) { int64_t v = ss[k]; if (v > best) best = v; }
                        A->extreme_i = best; A->cnt = ub - lb;
                    }
                    break;
                }
                case OP_FIRST: {
                    if (nn) {
                        int64_t cnt = 0;
                        for (int64_t k = lb; k < ub; k++) if (nn[k]) {
                            if (cnt == 0) A->extreme_i = ss[k];
                            cnt++;
                        }
                        A->cnt = cnt;
                    } else if (lb < ub) {
                        A->extreme_i = ss[lb]; A->cnt = ub - lb;
                    }
                    break;
                }
                case OP_LAST: {
                    if (nn) {
                        int64_t cnt = 0, last_k = -1;
                        for (int64_t k = lb; k < ub; k++) if (nn[k]) { last_k = k; cnt++; }
                        if (last_k >= 0) A->extreme_i = ss[last_k];
                        A->cnt = cnt;
                    } else if (lb < ub) {
                        A->extreme_i = ss[ub - 1]; A->cnt = ub - lb;
                    }
                    break;
                }
                default: break;
                }
            }
        }

        /* Finalize → indexed write at slot lr */
        for (int64_t a = 0; a < n_agg; a++) {
            wj_acc_t* A = &acc[a];
            int8_t rty = c->agg_result_types[a];
            bool null_out = false;
            int64_t out_i = 0;
            double  out_f = 0.0;

            if (c->agg_raw[a]) {
                null_out = true;
            } else {
                switch (c->agg_ops[a]) {
                case OP_COUNT: out_i = A->cnt; break;
                case OP_SUM:
                    if (c->agg_is_float[a]) out_f = A->sum_f; else out_i = A->sum_i;
                    break;
                case OP_PROD:
                    if (A->cnt == 0) null_out = true;
                    else if (c->agg_is_float[a]) out_f = A->prod_f; else out_i = A->prod_i;
                    break;
                case OP_MIN: case OP_MAX: case OP_FIRST: case OP_LAST:
                    if (A->cnt == 0) null_out = true;
                    else if (c->agg_is_float[a]) out_f = A->extreme_f; else out_i = A->extreme_i;
                    break;
                case OP_AVG:
                    if (A->cnt == 0) null_out = true;
                    else out_f = c->agg_is_float[a]
                        ? A->sum_f / (double)A->cnt
                        : (double)A->sum_i / (double)A->cnt;
                    break;
                case OP_VAR: case OP_VAR_POP:
                case OP_STDDEV: case OP_STDDEV_POP: {
                    bool sample = (c->agg_ops[a] == OP_VAR || c->agg_ops[a] == OP_STDDEV);
                    bool insuf = sample ? (A->cnt <= 1) : (A->cnt <= 0);
                    if (insuf) { null_out = true; break; }
                    double mean, var_pop;
                    if (c->agg_is_float[a]) {
                        mean    = A->sum_f / (double)A->cnt;
                        var_pop = A->sum_sq_f / (double)A->cnt - mean * mean;
                    } else {
                        mean    = (double)A->sum_i / (double)A->cnt;
                        var_pop = (double)A->sum_sq_i / (double)A->cnt - mean * mean;
                    }
                    if (var_pop < 0) var_pop = 0;
                    if      (c->agg_ops[a] == OP_VAR_POP)    out_f = var_pop;
                    else if (c->agg_ops[a] == OP_VAR)        out_f = var_pop * A->cnt / (A->cnt - 1);
                    else if (c->agg_ops[a] == OP_STDDEV_POP) out_f = sqrt(var_pop);
                    else                                     out_f = sqrt(var_pop * A->cnt / (A->cnt - 1));
                    break;
                }
                default: null_out = true; break;
                }
            }

            /* Single-null float model: an F64 aggregate result that is
             * non-finite (avg/var division, sum overflow, sqrt(<0)) becomes
             * NULL_F64; reflect it in the null flag so HAS_NULLS is set and
             * the result column never holds a non-finite-non-0Nf value. */
            if (!null_out && rty == RAY_F64) {
                out_f = ray_f64_fin(out_f);
                if (out_f != out_f) null_out = true;
            }

            c->result_null[a][lr] = null_out ? 1 : 0;
            if (null_out) continue;

            void* rd = c->result_data[a];
            if (rty == RAY_F64)       ((double*)rd)[lr] = out_f;
            else if (rty == RAY_F32)  ((float*)rd)[lr]  = (float)out_f;
            else if (rty == RAY_I64 || rty == RAY_TIMESTAMP)
                ((int64_t*)rd)[lr] = out_i;
            else if (rty == RAY_I32 || rty == RAY_DATE || rty == RAY_TIME)
                ((int32_t*)rd)[lr] = (int32_t)out_i;
            else if (rty == RAY_I16)  ((int16_t*)rd)[lr] = (int16_t)out_i;
            else                       ((uint8_t*)rd)[lr] = (uint8_t)out_i;
        }
    }
}

/* Window join (Rayforce convention):
 *   (window-join  [eq-keys.. timeKey] intervals left right {agg})
 *   (window-join1 [eq-keys.. timeKey] intervals left right {agg})
 * `intervals` is a 2-element list (lo_vec hi_vec) of parallel vectors, one
 * entry per left row: the window for left row r is [lo_vec[r], hi_vec[r]].
 *
 * mode 0 (window-join / wj):  each window is seeded with the PREVAILING quote
 *   — the rightmost quote with time <= lo[r] within the eq partition — then
 *   extended through the last quote with time <= hi[r].
 * mode 1 (window-join1 / wj1): strict window — only quotes whose time falls in
 *   [lo[r], hi[r]].
 *
 * The legacy asof fall-through ((window-join L R [keys] time)) is mode-agnostic.
 */
static int8_t join_i64_eq_key_type(ray_t* col) {
    if (!col) return 0;
    int8_t t = col->type;
    if (RAY_IS_PARTED(t)) t = (int8_t)RAY_PARTED_BASETYPE(t);
    return t;
}

static bool join_i64_eq_key_supported(int8_t t) {
    switch (t) {
    case RAY_BOOL:
    case RAY_U8:
    case RAY_I16:
    case RAY_I32:
    case RAY_I64:
    case RAY_DATE:
    case RAY_TIME:
    case RAY_TIMESTAMP:
    case RAY_SYM:
        return true;
    default:
        return false;
    }
}

static ray_t* window_join_impl(ray_t** args, int64_t n, int mode) {
    if (n < 4) return ray_error("domain", "window-join: expects keys, intervals, left and right tables, got %lld args", (long long)n);

    /* Special form: evaluate first 4 args, keep agg dict (args[4]) unevaluated */
    ray_t* eargs[5];
    for (int i = 0; i < 4 && i < (int)n; i++) {
        eargs[i] = ray_eval(args[i]);
        if (!eargs[i] || RAY_IS_ERR(eargs[i])) {
            for (int j = 0; j < i; j++) ray_release(eargs[j]);
            return eargs[i] ? eargs[i] : ray_error("type", "window-join: argument evaluation failed");
        }
    }
    eargs[4] = (n >= 5) ? args[4] : NULL; /* agg dict stays unevaluated */

    /* Rayforce calling convention:
     * (window-join [eq+time keys] intervals left right {agg}) */
    if (n >= 5 && ray_is_vec(eargs[0]) && eargs[0]->type == RAY_SYM &&
        eargs[2]->type == RAY_TABLE && eargs[3]->type == RAY_TABLE) {
        /* Rayforce convention: implement at eval level.
         * See file-scope wj_scan_fn / wj_scan_ctx_t for the parallel worker. */
        ray_t* keys_vec = eargs[0];      /* [Sym Time] — equality + time keys */
        ray_t* intervals = eargs[1];     /* list of [lo hi] time windows */
        ray_t* left_tbl = eargs[2];      /* trades */
        ray_t* right_tbl = eargs[3];     /* quotes */
        ray_t* agg_dict = eargs[4];      /* unevaluated dict */

        int64_t nkeys = ray_len(keys_vec);
        if (nkeys < 2) return ray_error("domain", "window-join: keys must contain at least one equality key plus a time key, got %lld", (long long)nkeys);

        /* Last key is the time key, rest are equality keys.  keys_vec is
         * an EVALUATED user vec (any width, any domain) — read cells via
         * the domain-aware width-specialised helper and re-express them
         * as runtime name ids for the column lookups below. */
        int64_t time_key = sym_cell_runtime_id(keys_vec, nkeys - 1);
        int64_t n_eq = nkeys - 1;

        int64_t left_nrows = ray_table_nrows(left_tbl);
        int64_t right_nrows = ray_table_nrows(right_tbl);

        /* Get left time column */
        ray_t* left_time = ray_table_get_col(left_tbl, time_key);
        ray_t* right_time = ray_table_get_col(right_tbl, time_key);
        if (!left_time || !right_time) return ray_error("domain", "window-join: time key column not found in both tables");

        /* Get equality columns.  One calloc'd carve holds every n_eq-sized eq
         * buffer: five pointer arrays (left_eq/right_eq/eq_data/left_eq_hdr/
         * left_eq_arr) then two byte arrays (eq_type/eq_attrs).  Zero-init is
         * required — left_eq_hdr/left_eq_arr are released by a loop that
         * iterates unconditionally, and eq_data/eq_type/eq_attrs are aliased
         * into the scan ctx.  n_eq >= 1 (nkeys >= 2), so the carve is never
         * zero-sized. */
        ray_t* eq_hdr = NULL;
        ray_t** left_eq = (ray_t**)scratch_calloc(&eq_hdr,
            (size_t)n_eq * (5 * sizeof(void*) + 2 * sizeof(uint8_t)));
        if (!left_eq) { for (int i = 0; i < 4; i++) ray_release(eargs[i]); return ray_error("oom", NULL); }
        ray_t**      right_eq    = (ray_t**)(left_eq + n_eq);
        const void** eq_data     = (const void**)(right_eq + n_eq);
        ray_t**      left_eq_hdr = (ray_t**)(eq_data + n_eq);
        int64_t**    left_eq_arr = (int64_t**)(left_eq_hdr + n_eq);
        int8_t*      eq_type     = (int8_t*)(left_eq_arr + n_eq);
        uint8_t*     eq_attrs    = (uint8_t*)(eq_type + n_eq);
        for (int64_t e = 0; e < n_eq; e++) {
            int64_t eq_key = sym_cell_runtime_id(keys_vec, e);
            left_eq[e] = ray_table_get_col(left_tbl, eq_key);
            right_eq[e] = ray_table_get_col(right_tbl, eq_key);
            if (!left_eq[e] || !right_eq[e]) {
                scratch_free(eq_hdr);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                return ray_error("domain", "window-join: equality key column not found in both tables");
            }
            /* Equality keys in the window kernels are sorted/probed through
             * read_col_i64. Decline any type that reader cannot represent
             * instead of silently comparing the wrong bytes. */
            int8_t lct = join_i64_eq_key_type(left_eq[e]);
            int8_t rct = join_i64_eq_key_type(right_eq[e]);
            if (!join_i64_eq_key_supported(lct) ||
                !join_i64_eq_key_supported(rct)) {
                scratch_free(eq_hdr);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                return ray_error("nyi", "window-join: equality key column types %s and %s are not supported",
                                 ray_type_name(lct), ray_type_name(rct));
            }
        }

        /* Parse every (name, (op src)) pair from the agg dict.  The dict's
         * physical layout is [keys (SYM vec), vals (LIST)] — read keys[i]
         * via ray_read_sym and pair it with vals[i] from the LIST.
         *
         * The agg-dict length is the exact upper bound on the aggregate count;
         * carve every agg-sized buffer from one calloc'd block sized by it (or
         * leave the pointers NULL when there is no dict — n_agg stays 0 and
         * every a<n_agg loop is inert).  Order: thirteen 8-byte arrays, then
         * the int / uint16 / three byte arrays.  calloc: many of these
         * (agg_src_vecs, agg_result_vecs, sorted_*, sorted_*_hdr, null_stage*)
         * are read or freed at slots the producers may skip. */
        ray_t*  dkeys = NULL, *dvals = NULL;
        ray_t** lvals = NULL;
        int64_t adn = 0;
        if (agg_dict && agg_dict->type == RAY_DICT) {
            dkeys = ray_dict_keys(agg_dict);
            dvals = ray_dict_vals(agg_dict);
            if (dkeys && dkeys->type == RAY_SYM) adn = dkeys->len;
            lvals = (dvals && dvals->type == RAY_LIST) ? (ray_t**)ray_data(dvals) : NULL;
        }
        ray_t*    agg_hdr = NULL;
        int64_t*  agg_names = NULL;
        int64_t*  agg_src_ids = NULL;
        ray_t**   agg_src_vecs = NULL;
        ray_t**   agg_result_vecs = NULL;
        int64_t** sorted_i = NULL;
        double**  sorted_f = NULL;
        uint8_t** sorted_nn = NULL;
        ray_t**   sorted_i_hdr = NULL;
        ray_t**   sorted_f_hdr = NULL;
        ray_t**   sorted_nn_hdr = NULL;
        ray_t**   null_stage_hdr = NULL;
        uint8_t** null_stage = NULL;
        void**    result_data = NULL;
        int*      agg_is_float = NULL;
        uint16_t* agg_ops = NULL;
        int8_t*   agg_types = NULL;
        int8_t*   agg_result_types = NULL;
        uint8_t*  agg_raw = NULL;
        if (adn > 0) {
            agg_names = (int64_t*)scratch_calloc(&agg_hdr,
                (size_t)adn * (13 * sizeof(void*) + sizeof(int) + sizeof(uint16_t) + 3 * sizeof(uint8_t)));
            if (!agg_names) {
                scratch_free(eq_hdr);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                return ray_error("oom", NULL);
            }
            agg_src_ids      = (int64_t*)(agg_names + adn);
            agg_src_vecs     = (ray_t**)(agg_src_ids + adn);
            agg_result_vecs  = (ray_t**)(agg_src_vecs + adn);
            sorted_i         = (int64_t**)(agg_result_vecs + adn);
            sorted_f         = (double**)(sorted_i + adn);
            sorted_nn        = (uint8_t**)(sorted_f + adn);
            sorted_i_hdr     = (ray_t**)(sorted_nn + adn);
            sorted_f_hdr     = (ray_t**)(sorted_i_hdr + adn);
            sorted_nn_hdr    = (ray_t**)(sorted_f_hdr + adn);
            null_stage_hdr   = (ray_t**)(sorted_nn_hdr + adn);
            null_stage       = (uint8_t**)(null_stage_hdr + adn);
            result_data      = (void**)(null_stage + adn);
            agg_is_float     = (int*)(result_data + adn);
            agg_ops          = (uint16_t*)(agg_is_float + adn);
            agg_types        = (int8_t*)(agg_ops + adn);
            agg_result_types = (int8_t*)(agg_types + adn);
            agg_raw          = (uint8_t*)(agg_result_types + adn);
        }
        int64_t n_agg = 0;

        {
            for (int64_t di = 0; di < adn; di++) {
                /* dict-key cell becomes an output column NAME — runtime */
                int64_t kname_id = sym_cell_runtime_id(dkeys, di);
                ray_t* expr = lvals ? lvals[di] : NULL;
                if (!expr) continue;
                /* (op col) aggregation form */
                if (expr->type == RAY_LIST && expr->len >= 2) {
                    ray_t** ae = (ray_t**)ray_data(expr);
                    if (!(ae[0]->type == -RAY_SYM && !(ae[0]->attrs & ATTR_QUOTED))) continue;
                    if (!(ae[1]->type == -RAY_SYM && !(ae[1]->attrs & ATTR_QUOTED))) continue;
                    agg_names[n_agg]   = kname_id;
                    agg_ops[n_agg]     = resolve_agg_opcode(ae[0]->i64);
                    agg_src_ids[n_agg] = ae[1]->i64;
                    agg_raw[n_agg]     = 0;
                    n_agg++;
                    continue;
                }
                /* Bare column reference — legacy map-group form, emitted as null column */
                if (expr->type == -RAY_SYM && !(expr->attrs & ATTR_QUOTED)) {
                    agg_names[n_agg]   = kname_id;
                    agg_ops[n_agg]     = OP_MIN;
                    agg_src_ids[n_agg] = expr->i64;
                    agg_raw[n_agg]     = 1;
                    n_agg++;
                    continue;
                }
            }
        }

        /* Resolve sources, pick result types, allocate result vectors.
         * Raw bare-column form ({name: Col}) is a legacy placeholder — it
         * accepts any column type (numeric or not) and always produces a
         * nullable i64 column filled with nulls. All true aggregation ops
         * require a numeric source column. */
        for (int64_t a = 0; a < n_agg; a++) {
            if (agg_raw[a]) {
                agg_src_vecs[a]    = NULL;
                agg_types[a]       = RAY_I64;
                agg_is_float[a]    = 0;
                agg_result_types[a] = RAY_I64;
                agg_result_vecs[a] = ray_vec_new(RAY_I64, left_nrows);
                if (RAY_IS_ERR(agg_result_vecs[a])) {
                    ray_t* err = agg_result_vecs[a];
                    for (int64_t b = 0; b < a; b++) ray_release(agg_result_vecs[b]);
                    for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                    scratch_free(agg_hdr); scratch_free(eq_hdr);
                    return err;
                }
                continue;
            }
            if (agg_ops[a] == 0) {
                for (int64_t b = 0; b < a; b++) ray_release(agg_result_vecs[b]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return ray_error("domain", "window-join: unsupported aggregation op");
            }
            ray_t* src = ray_table_get_col(right_tbl, agg_src_ids[a]);
            if (!src) {
                for (int64_t b = 0; b < a; b++) ray_release(agg_result_vecs[b]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return ray_error("domain", "window-join: aggregation source column not found");
            }
            int8_t t = src->type;
            /* COUNT never reads source values — accept any column type. Every
             * other aggregation reads v_i/v_f and requires a numeric source. */
            if (agg_ops[a] != OP_COUNT) {
                switch (t) {
                case RAY_I64: case RAY_I32: case RAY_I16: case RAY_U8:
                case RAY_F64: case RAY_F32: case RAY_BOOL:
                case RAY_DATE: case RAY_TIME: case RAY_TIMESTAMP:
                    break;
                default:
                    for (int64_t b = 0; b < a; b++) ray_release(agg_result_vecs[b]);
                    for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                    scratch_free(agg_hdr); scratch_free(eq_hdr);
                    return ray_error("type", "window-join: aggregation source must be numeric, got %s", ray_type_name(t));
                }
            }
            agg_src_vecs[a]  = src;
            agg_types[a]     = t;
            agg_is_float[a]  = (t == RAY_F64 || t == RAY_F32);

            int8_t rt;
            switch (agg_ops[a]) {
            case OP_COUNT: rt = RAY_I64; break;
            case OP_AVG:
            case OP_VAR: case OP_VAR_POP:
            case OP_STDDEV: case OP_STDDEV_POP:
            case OP_MEDIAN:
            case OP_QUANTILE: rt = RAY_F64; break;
            case OP_SUM: case OP_PROD:
                rt = agg_is_float[a] ? RAY_F64 : RAY_I64; break;
            default: /* MIN/MAX/FIRST/LAST */ rt = t; break;
            }
            agg_result_types[a] = rt;
            agg_result_vecs[a] = ray_vec_new(rt, left_nrows);
            if (RAY_IS_ERR(agg_result_vecs[a])) {
                ray_t* err = agg_result_vecs[a];
                for (int64_t b = 0; b < a; b++) ray_release(agg_result_vecs[b]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return err;
            }
        }

        /* wj_acc_t is defined at file scope now (used by wj_scan_fn). */

        /* Sort right table by (eq_keys..., time) once so each left row only
         * scans the quote rows whose (eq,time) fall inside its window.
         * Per-row cost drops from O(right_nrows) to O(log right_nrows + window). */
        ray_t*   rs_hdr = NULL, *rt_hdr = NULL, *tmp_hdr = NULL;
        int64_t* right_sort = NULL;
        int64_t* rt_time_i  = NULL;
        int64_t* tmp_sort   = NULL;
        /* eq_data/eq_type/eq_attrs live in the eq carve (aliased into the scan
         * ctx); fill the right-side metadata for the sort compare here. */
        for (int64_t e = 0; e < n_eq; e++) {
            eq_data[e]  = ray_data(right_eq[e]);
            eq_type[e]  = right_eq[e]->type;
            eq_attrs[e] = right_eq[e]->attrs;
        }
        if (right_nrows > 0) {
            right_sort = (int64_t*)scratch_alloc(&rs_hdr,  (size_t)right_nrows * sizeof(int64_t));
            rt_time_i  = (int64_t*)scratch_alloc(&rt_hdr,  (size_t)right_nrows * sizeof(int64_t));
            tmp_sort   = (int64_t*)scratch_alloc(&tmp_hdr, (size_t)right_nrows * sizeof(int64_t));
            if (!right_sort || !rt_time_i || !tmp_sort) {
                if (rs_hdr)  scratch_free(rs_hdr);
                if (rt_hdr)  scratch_free(rt_hdr);
                if (tmp_hdr) scratch_free(tmp_hdr);
                for (int64_t a = 0; a < n_agg; a++) ray_release(agg_result_vecs[a]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return ray_error("oom", NULL);
            }
            /* Cache time column access so the sort compare avoids reloading them */
            int8_t  rt_type  = right_time->type;
            uint8_t rt_attrs = right_time->attrs;
            const void* rt_data = ray_data(right_time);
            for (int64_t rr = 0; rr < right_nrows; rr++) {
                right_sort[rr] = rr;
                rt_time_i[rr]  = read_col_i64(rt_data, rr, rt_type, rt_attrs);
            }
            /* Bottom-up merge sort on index array */
            for (int64_t width = 1; width < right_nrows; width *= 2) {
                for (int64_t lo = 0; lo < right_nrows; lo += 2 * width) {
                    int64_t mid = lo + width;
                    int64_t hi  = lo + 2 * width;
                    if (mid > right_nrows) mid = right_nrows;
                    if (hi  > right_nrows) hi  = right_nrows;
                    int64_t a = lo, b = mid, t = lo;
                    while (a < mid && b < hi) {
                        int64_t ai = right_sort[a], bi = right_sort[b];
                        int cmp = 0;
                        for (int64_t e = 0; e < n_eq && cmp == 0; e++) {
                            int64_t va = read_col_i64(eq_data[e], ai, eq_type[e], eq_attrs[e]);
                            int64_t vb = read_col_i64(eq_data[e], bi, eq_type[e], eq_attrs[e]);
                            if (va < vb) cmp = -1;
                            else if (va > vb) cmp = 1;
                        }
                        if (cmp == 0) {
                            if      (rt_time_i[ai] < rt_time_i[bi]) cmp = -1;
                            else if (rt_time_i[ai] > rt_time_i[bi]) cmp = 1;
                        }
                        tmp_sort[t++] = (cmp <= 0) ? right_sort[a++] : right_sort[b++];
                    }
                    while (a < mid) tmp_sort[t++] = right_sort[a++];
                    while (b < hi)  tmp_sort[t++] = right_sort[b++];
                    for (int64_t c = lo; c < hi; c++) right_sort[c] = tmp_sort[c];
                }
            }
            scratch_free(tmp_hdr);
            tmp_hdr  = NULL;
            tmp_sort = NULL;
        }

        /* Preload one sorted source column per aggregation.
         * After sorting right_sort, the hot loop wants *sequential* access
         * (SIMD + prefetch friendly) — not an indirect gather through
         * right_sort[k]. We materialize sorted_src_i[a][k] = value at
         * right_sort[k] once, then every left row's window scans are a
         * plain array walk.
         *
         * COUNT / raw form carry no source; nothing to preload. PROD and
         * ops on null-containing columns still go through the slow scan
         * (see below), so the preload is gated on the easy numeric cases.
         * sorted_i/f/nn and their _hdr arrays live in the (zero-initialized)
         * agg carve — slots skipped here stay NULL for WJ_CLEANUP_TEMP. */
        for (int64_t a = 0; a < n_agg; a++) {
            if (agg_raw[a] || agg_ops[a] == OP_COUNT) continue;
            ray_t* src = agg_src_vecs[a];
            if (!src || right_nrows == 0) continue;
            bool has_nulls = ray_vec_may_have_nulls(src);
            if (has_nulls) {
                sorted_nn[a] = (uint8_t*)scratch_alloc(&sorted_nn_hdr[a],
                                                        (size_t)right_nrows);
                if (!sorted_nn[a]) { goto wj_preload_oom; }
            }
            if (agg_is_float[a]) {
                sorted_f[a] = (double*)scratch_alloc(&sorted_f_hdr[a],
                                                      (size_t)right_nrows * sizeof(double));
                if (!sorted_f[a]) { goto wj_preload_oom; }
                int8_t t = agg_types[a];
                const void* sd = ray_data(src);
                for (int64_t k = 0; k < right_nrows; k++) {
                    int64_t rr = right_sort[k];
                    double v = (t == RAY_F32)
                        ? (double)((const float*)sd)[rr]
                        : ((const double*)sd)[rr];
                    sorted_f[a][k] = v;
                    if (has_nulls) sorted_nn[a][k] = ray_vec_is_null(src, rr) ? 0 : 1;
                }
            } else {
                sorted_i[a] = (int64_t*)scratch_alloc(&sorted_i_hdr[a],
                                                       (size_t)right_nrows * sizeof(int64_t));
                if (!sorted_i[a]) { goto wj_preload_oom; }
                const void* sd = ray_data(src);
                int8_t t = agg_types[a];
                uint8_t at = src->attrs;
                for (int64_t k = 0; k < right_nrows; k++) {
                    int64_t rr = right_sort[k];
                    sorted_i[a][k] = read_col_i64(sd, rr, t, at);
                    if (has_nulls) sorted_nn[a][k] = ray_vec_is_null(src, rr) ? 0 : 1;
                }
            }
        }

        #define WJ_CLEANUP_TEMP() do {                              \
            if (rs_hdr) scratch_free(rs_hdr);                       \
            if (rt_hdr) scratch_free(rt_hdr);                       \
            if (tmp_hdr) scratch_free(tmp_hdr);                     \
            for (int64_t _a = 0; _a < n_agg; _a++) {                \
                if (sorted_i_hdr[_a])  scratch_free(sorted_i_hdr[_a]);  \
                if (sorted_f_hdr[_a])  scratch_free(sorted_f_hdr[_a]);  \
                if (sorted_nn_hdr[_a]) scratch_free(sorted_nn_hdr[_a]); \
            }                                                       \
        } while (0)

        if (0) {
        wj_preload_oom:
            WJ_CLEANUP_TEMP();
            for (int64_t a = 0; a < n_agg; a++) ray_release(agg_result_vecs[a]);
            for (int i = 0; i < 4; i++) ray_release(eargs[i]);
            scratch_free(agg_hdr); scratch_free(eq_hdr);
            return ray_error("oom", NULL);
        }

        /* Pre-extract left-row metadata (interval endpoints + eq-key tuples)
         * into flat int64 arrays. This hoists all ray_t allocation and the
         * width-aware reads out of the hot loop so the parallel worker can
         * process rows without touching any ref-counted objects. */
        ray_t* lo_hdr = NULL, *hi_hdr = NULL;
        int64_t* lo_arr = (int64_t*)scratch_alloc(&lo_hdr, (size_t)left_nrows * sizeof(int64_t));
        int64_t* hi_arr = (int64_t*)scratch_alloc(&hi_hdr, (size_t)left_nrows * sizeof(int64_t));
        if ((!lo_arr || !hi_arr) && left_nrows > 0) {
            if (lo_hdr) scratch_free(lo_hdr);
            if (hi_hdr) scratch_free(hi_hdr);
            WJ_CLEANUP_TEMP();
            for (int64_t a = 0; a < n_agg; a++) ray_release(agg_result_vecs[a]);
            for (int i = 0; i < 4; i++) ray_release(eargs[i]);
            scratch_free(agg_hdr); scratch_free(eq_hdr);
            return ray_error("oom", NULL);
        }
        /* `intervals` is the v1 two-parallel-vector form: a 2-element list
         * (lo_vec hi_vec), each a vector with one entry per left row.  The
         * window for left row r is [lo_vec[r], hi_vec[r]]. */
        {
            int alloc_lo_v = 0, alloc_hi_v = 0;
            ray_t* lo_vec = (ray_len(intervals) >= 2)
                ? collection_elem(intervals, 0, &alloc_lo_v) : NULL;
            ray_t* hi_vec = (ray_len(intervals) >= 2)
                ? collection_elem(intervals, 1, &alloc_hi_v) : NULL;
            if (!lo_vec || !hi_vec || RAY_IS_ERR(lo_vec) || RAY_IS_ERR(hi_vec) ||
                !ray_is_vec(lo_vec) || !ray_is_vec(hi_vec) ||
                ray_len(lo_vec) != left_nrows || ray_len(hi_vec) != left_nrows) {
                if (alloc_lo_v && lo_vec) ray_release(lo_vec);
                if (alloc_hi_v && hi_vec) ray_release(hi_vec);
                if (lo_hdr) scratch_free(lo_hdr);
                if (hi_hdr) scratch_free(hi_hdr);
                WJ_CLEANUP_TEMP();
                for (int64_t a = 0; a < n_agg; a++) ray_release(agg_result_vecs[a]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return ray_error("domain", "window-join: intervals must be [lo hi] vectors matching left row count");
            }
            const void* lo_d = ray_data(lo_vec);
            const void* hi_d = ray_data(hi_vec);
            int8_t  lo_t = lo_vec->type,  hi_t = hi_vec->type;
            uint8_t lo_a = lo_vec->attrs, hi_a = hi_vec->attrs;
            for (int64_t lr = 0; lr < left_nrows; lr++) {
                lo_arr[lr] = read_col_i64(lo_d, lr, lo_t, lo_a);
                hi_arr[lr] = read_col_i64(hi_d, lr, hi_t, hi_a);
            }
            if (alloc_lo_v) ray_release(lo_vec);
            if (alloc_hi_v) ray_release(hi_vec);
        }

        /* left_eq_hdr/left_eq_arr live in the (zero-initialized) eq carve. */
        for (int64_t e = 0; e < n_eq; e++) {
            left_eq_arr[e] = (int64_t*)scratch_alloc(&left_eq_hdr[e],
                                                     (size_t)left_nrows * sizeof(int64_t));
            if (!left_eq_arr[e] && left_nrows > 0) {
                if (lo_hdr) scratch_free(lo_hdr);
                if (hi_hdr) scratch_free(hi_hdr);
                for (int64_t f = 0; f < e; f++)
                    if (left_eq_hdr[f]) scratch_free(left_eq_hdr[f]);
                WJ_CLEANUP_TEMP();
                for (int64_t a = 0; a < n_agg; a++) ray_release(agg_result_vecs[a]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return ray_error("oom", NULL);
            }
            const void* sd = ray_data(left_eq[e]);
            int8_t  t  = left_eq[e]->type;
            uint8_t at = left_eq[e]->attrs;
            for (int64_t lr = 0; lr < left_nrows; lr++)
                left_eq_arr[e][lr] = read_col_i64(sd, lr, t, at);
        }

        /* Pre-size each result vector and allocate a 1-byte-per-row null
         * staging array — writers index by lr without touching nulls. */
        /* null_stage_hdr/null_stage live in the (zero-initialized) agg carve. */
        for (int64_t a = 0; a < n_agg; a++) {
            agg_result_vecs[a]->len = left_nrows;
            null_stage[a] = (uint8_t*)scratch_alloc(&null_stage_hdr[a], (size_t)left_nrows);
            if (!null_stage[a] && left_nrows > 0) {
                if (lo_hdr) scratch_free(lo_hdr);
                if (hi_hdr) scratch_free(hi_hdr);
                for (int64_t f = 0; f < n_eq; f++) if (left_eq_hdr[f]) scratch_free(left_eq_hdr[f]);
                for (int64_t b = 0; b < a; b++) if (null_stage_hdr[b]) scratch_free(null_stage_hdr[b]);
                WJ_CLEANUP_TEMP();
                for (int64_t b = 0; b < n_agg; b++) ray_release(agg_result_vecs[b]);
                for (int i = 0; i < 4; i++) ray_release(eargs[i]);
                scratch_free(agg_hdr); scratch_free(eq_hdr);
                return ray_error("oom", NULL);
            }
            memset(null_stage[a], 0, (size_t)left_nrows);
        }

        /* Build the scan context and dispatch.  Per-worker scratch (acc +
         * target_eq) is carved once, sized by the worker count — no per-row
         * or per-morsel allocation.  Worker 0 (main thread) covers the serial
         * path too. */
        ray_pool_t* pool = ray_pool_get();
        int64_t nw = (pool && left_nrows >= 2048) ? ray_pool_total_workers(pool) : 1;
        ray_t* wscr_hdr = NULL;
        wj_acc_t* acc_base = (wj_acc_t*)scratch_alloc(&wscr_hdr,
            (size_t)nw * ((size_t)n_agg * sizeof(wj_acc_t) + (size_t)n_eq * sizeof(int64_t)));
        if (!acc_base) {
            if (lo_hdr) scratch_free(lo_hdr);
            if (hi_hdr) scratch_free(hi_hdr);
            for (int64_t f = 0; f < n_eq; f++) if (left_eq_hdr[f]) scratch_free(left_eq_hdr[f]);
            for (int64_t b = 0; b < n_agg; b++) if (null_stage_hdr[b]) scratch_free(null_stage_hdr[b]);
            WJ_CLEANUP_TEMP();
            for (int64_t b = 0; b < n_agg; b++) ray_release(agg_result_vecs[b]);
            for (int i = 0; i < 4; i++) ray_release(eargs[i]);
            scratch_free(agg_hdr); scratch_free(eq_hdr);
            return ray_error("oom", NULL);
        }
        int64_t* target_base = (int64_t*)(acc_base + (size_t)nw * n_agg);

        for (int64_t a = 0; a < n_agg; a++)
            result_data[a] = ray_data(agg_result_vecs[a]);

        wj_scan_ctx_t wctx;
        memset(&wctx, 0, sizeof(wctx));
        wctx.left_nrows  = left_nrows;
        wctx.right_nrows = right_nrows;
        wctx.n_eq        = n_eq;
        wctx.n_agg       = n_agg;
        wctx.mode        = mode;
        wctx.lo_arr      = lo_arr;
        wctx.hi_arr      = hi_arr;
        wctx.right_sort  = right_sort;
        wctx.rt_time_i   = rt_time_i;
        wctx.left_eq_arr = (const int64_t**)left_eq_arr;
        wctx.eq_data     = eq_data;
        wctx.eq_type     = eq_type;
        wctx.eq_attrs    = eq_attrs;
        wctx.agg_raw          = agg_raw;
        wctx.agg_ops          = agg_ops;
        wctx.agg_result_types = agg_result_types;
        wctx.agg_is_float     = agg_is_float;
        wctx.sorted_i    = (const int64_t**)sorted_i;
        wctx.sorted_f    = (const double**)sorted_f;
        wctx.sorted_nn   = (const uint8_t**)sorted_nn;
        wctx.result_data = result_data;
        wctx.result_null = null_stage;
        wctx.acc_base    = acc_base;
        wctx.target_base = target_base;

        if (pool && left_nrows >= 2048) {
            ray_pool_dispatch(pool, wj_scan_fn, &wctx, left_nrows);
        } else {
            wj_scan_fn(&wctx, 0, 0, left_nrows);
        }

        /* Apply staged null flags to each result vec's null bitmap sequentially. */
        for (int64_t a = 0; a < n_agg; a++) {
            ray_t* rv = agg_result_vecs[a];
            const uint8_t* stage = null_stage[a];
            for (int64_t lr = 0; lr < left_nrows; lr++)
                if (stage[lr]) ray_vec_set_null(rv, lr, true);
        }

        /* Free pre-extract scratch */
        scratch_free(wscr_hdr);
        if (lo_hdr) scratch_free(lo_hdr);
        if (hi_hdr) scratch_free(hi_hdr);
        for (int64_t e = 0; e < n_eq; e++)
            if (left_eq_hdr[e]) scratch_free(left_eq_hdr[e]);
        for (int64_t a = 0; a < n_agg; a++)
            if (null_stage_hdr[a]) scratch_free(null_stage_hdr[a]);


        WJ_CLEANUP_TEMP();
        #undef WJ_CLEANUP_TEMP

        /* Build result table: left columns + every aggregation column */
        int64_t ncols = ray_table_ncols(left_tbl);
        ray_t* result = ray_table_new(ncols + n_agg);
        for (int64_t c = 0; c < ncols; c++) {
            int64_t cn = ray_table_col_name(left_tbl, c);
            ray_t* cv = ray_table_get_col_idx(left_tbl, c);
            ray_retain(cv);
            result = ray_table_add_col(result, cn, cv);
            ray_release(cv);
        }
        for (int64_t a = 0; a < n_agg; a++) {
            result = ray_table_add_col(result, agg_names[a], agg_result_vecs[a]);
            ray_release(agg_result_vecs[a]);
        }
        for (int i = 0; i < 4; i++) ray_release(eargs[i]);
        /* agg_hdr/eq_hdr hold only the (now-consumed) pointer arrays; the
         * result table owns the vecs and left columns. */
        scratch_free(agg_hdr);
        scratch_free(eq_hdr);
        return result;
    }

    ray_t* left_tbl  = eargs[0];
    ray_t* right_tbl = eargs[1];
    ray_t* eq_keys   = eargs[2];
    ray_t* time_sym  = eargs[3];

    if (left_tbl->type != RAY_TABLE || right_tbl->type != RAY_TABLE)
        return ray_error("type", "window-join: both operands must be tables, got %s and %s",
                         ray_type_name(left_tbl->type), ray_type_name(right_tbl->type));
    if (time_sym->type != -RAY_SYM)
        return ray_error("type", "window-join: time key must be a symbol, got %s", ray_type_name(time_sym->type));

    int64_t n_eq = 0;
    ray_t** eq_elems = NULL;
    ray_t* _bxeq = NULL;
    eq_keys = unbox_vec_arg(eq_keys, &_bxeq);
    if (is_list(eq_keys)) {
        n_eq = ray_len(eq_keys);
        eq_elems = (ray_t**)ray_data(eq_keys);
    }

    ray_graph_t* g = ray_graph_new(left_tbl);
    if (!g) { if (_bxeq) ray_release(_bxeq); return ray_error("oom", NULL); }

    ray_op_t* left_node  = ray_const_table(g, left_tbl);
    ray_op_t* right_node = ray_const_table(g, right_tbl);

    ray_t* tname = ray_sym_str(time_sym->i64);
    if (!tname) { ray_graph_free(g); if (_bxeq) ray_release(_bxeq); return ray_error("domain", "window-join: unknown time key symbol"); }
    ray_op_t* time_op = ray_scan(g, ray_str_ptr(tname));
    if (!time_op) { ray_graph_free(g); if (_bxeq) ray_release(_bxeq); return ray_error("domain", "window-join: time key column not found"); }

    /* eq_ops: exact-size carve, n_eq wide (unbounded — no fixed key-count
     * ceiling).  ray_asof_join copies every id out of eq_ops before it
     * returns, so the carve is freed right after that call. */
    ray_t* eqops_hdr = NULL;
    ray_op_t** eq_ops = (ray_op_t**)scratch_alloc(&eqops_hdr,
            (size_t)(n_eq ? n_eq : 1) * sizeof(ray_op_t*));
    if (!eq_ops) { ray_graph_free(g); if (_bxeq) ray_release(_bxeq); return ray_error("oom", NULL); }
    for (int64_t i = 0; i < n_eq; i++) {
        if (eq_elems[i]->type != -RAY_SYM) {
            int8_t et = eq_elems[i]->type;  /* capture BEFORE free */
            scratch_free(eqops_hdr);
            ray_graph_free(g);
            if (_bxeq) ray_release(_bxeq);
            return ray_error("type", "window-join: equality key must be a symbol, got %s", ray_type_name(et));
        }
        ray_t* nm = ray_sym_str(eq_elems[i]->i64);
        if (!nm) { scratch_free(eqops_hdr); ray_graph_free(g); if (_bxeq) ray_release(_bxeq); return ray_error("domain", "window-join: unknown equality key symbol"); }
        eq_ops[i] = ray_scan(g, ray_str_ptr(nm));
        if (!eq_ops[i]) { scratch_free(eqops_hdr); ray_graph_free(g); if (_bxeq) ray_release(_bxeq); return ray_error("domain", "window-join: equality key column not found"); }
        ray_t* lcol = ray_table_get_col(left_tbl, eq_elems[i]->i64);
        ray_t* rcol = ray_table_get_col(right_tbl, eq_elems[i]->i64);
        int8_t lct = join_i64_eq_key_type(lcol);
        int8_t rct = join_i64_eq_key_type(rcol);
        if (!join_i64_eq_key_supported(lct) ||
            !join_i64_eq_key_supported(rct)) {
            scratch_free(eqops_hdr);
            ray_graph_free(g);
            if (_bxeq) ray_release(_bxeq);
            return ray_error("nyi", "window-join: equality key column types %s and %s are not supported",
                             ray_type_name(lct), ray_type_name(rct));
        }
    }

    if (_bxeq) ray_release(_bxeq);

    ray_op_t* jn = ray_asof_join(g, left_node, right_node,
                                time_op, eq_ops, (uint32_t)n_eq, 1);
    scratch_free(eqops_hdr);
    if (!jn) { ray_graph_free(g); return ray_error("oom", NULL); }

    jn = ray_optimize(g, jn);
    ray_t* result = ray_execute(g, jn);
    ray_graph_free(g);
    return result;
}

/* window-join (wj):  prevailing-quote-seeded window aggregation. */
ray_t* ray_window_join_fn(ray_t** args, int64_t n) {
    return window_join_impl(args, n, 0);
}

/* window-join1 (wj1): strict-window aggregation (no prevailing quote). */
ray_t* ray_window_join1_fn(ray_t** args, int64_t n) {
    return window_join_impl(args, n, 1);
}

/* ── streaming parted asof (SPIKE) ──────────────────────────────────────────
 * Monotonic count of per-partition (per-day) asof runs taken by the streaming
 * parted path below.  Bumped ONCE per partition processed — O(1) per partition,
 * never per-row, so it honours the "instrumentation never costs O(data)" rule.
 * Surfaced via ray_asof_perpart_runs() / (.sys.mem)'s "asof-perpart-runs". */
static _Atomic(int64_t) ray_asof_perpart_runs_ctr = 0;

int64_t ray_asof_perpart_runs(void) {
    return atomic_load_explicit(&ray_asof_perpart_runs_ctr,
                                memory_order_relaxed);
}

/* ── streaming parted ORDER BY ──────────────────────────────────────────────
 * Monotonic count of per-partition runs taken by either parted ORDER BY
 * streaming path (try_stream_parted_order_by concat, or
 * try_stream_parted_order_by_topk).  Bumped ONCE per partition processed — O(1) per partition, never per-row.  Surfaced via
 * ray_sort_perpart_runs() / (.sys.mem)'s "sort-perpart-runs". */
static _Atomic(int64_t) ray_sort_perpart_runs_ctr = 0;

int64_t ray_sort_perpart_runs(void) {
    return atomic_load_explicit(&ray_sort_perpart_runs_ctr,
                                memory_order_relaxed);
}

/* asof-join core: builds the disconnected asof DAG over two already-evaluated
 * flat table VALUES and runs the existing kernel.  keys_vec/left_tbl/right_tbl
 * are borrowed (caller owns).  This is the exact pre-spike behaviour; both the
 * flat fallback and the per-day streaming loop call it. */
static ray_t* ray_asof_join_core(ray_t* keys_vec, ray_t* left_tbl, ray_t* right_tbl) {
    if (left_tbl->type != RAY_TABLE || right_tbl->type != RAY_TABLE)
        return ray_error("type", "asof-join: both operands must be tables, got %s and %s",
                         ray_type_name(left_tbl->type), ray_type_name(right_tbl->type));

    /* Keys vector must be a SYM vector with at least 1 element: the time key;
     * any preceding keys are equality (partition) keys. */
    ray_t* _bxk = NULL;
    keys_vec = unbox_vec_arg(keys_vec, &_bxk);
    if (!is_list(keys_vec) || ray_len(keys_vec) < 1) {
        if (_bxk) ray_release(_bxk);
        return ray_error("domain", "asof-join: keys must be a non-empty symbol list");
    }
    ray_t** kelems = (ray_t**)ray_data(keys_vec);
    int64_t nkeys = ray_len(keys_vec);

    /* Last key is the time column */
    ray_t* time_sym = kelems[nkeys - 1];
    if (time_sym->type != -RAY_SYM) {
        int8_t ts_t = time_sym->type;            /* capture BEFORE free */
        if (_bxk) ray_release(_bxk);
        return ray_error("type", "asof-join: time key must be a symbol, got %s", ray_type_name(ts_t));
    }

    /* Remaining keys are equality keys */
    int64_t n_eq = nkeys - 1;
    ray_t** eq_syms = kelems; /* first n_eq elements */

    ray_graph_t* g = ray_graph_new(left_tbl);
    if (!g) { if (_bxk) ray_release(_bxk); return ray_error("oom", NULL); }

    ray_op_t* left_node  = ray_const_table(g, left_tbl);
    ray_op_t* right_node = ray_const_table(g, right_tbl);

    ray_t* tname = ray_sym_str(time_sym->i64);
    if (!tname) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "asof-join: unknown time key symbol"); }
    ray_op_t* time_op = ray_scan(g, ray_str_ptr(tname));
    if (!time_op) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "asof-join: time key column not found"); }

    /* eq_ops: exact-size carve, n_eq wide (unbounded — no fixed key-count
     * ceiling).  ray_asof_join copies every id out of eq_ops before it
     * returns, so the carve is freed right after that call. */
    ray_t* eqops_hdr = NULL;
    ray_op_t** eq_ops = (ray_op_t**)scratch_alloc(&eqops_hdr,
            (size_t)(n_eq ? n_eq : 1) * sizeof(ray_op_t*));
    if (!eq_ops) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("oom", NULL); }
    for (int64_t i = 0; i < n_eq; i++) {
        if (eq_syms[i]->type != -RAY_SYM) {
            int8_t es_t = eq_syms[i]->type;            /* capture BEFORE free */
            scratch_free(eqops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", "asof-join: equality key must be a symbol, got %s", ray_type_name(es_t));
        }
        ray_t* nm = ray_sym_str(eq_syms[i]->i64);
        if (!nm) { scratch_free(eqops_hdr); ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "asof-join: unknown equality key symbol"); }
        eq_ops[i] = ray_scan(g, ray_str_ptr(nm));
        if (!eq_ops[i]) { scratch_free(eqops_hdr); ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", "asof-join: equality key column not found"); }
        /* The asof kernel reads equality keys through read_col_i64
         * (asof_eq_lread). Decline types it cannot represent instead of
         * returning silently corrupted matches. */
        ray_t* lcol = ray_table_get_col(left_tbl, eq_syms[i]->i64);
        ray_t* rcol = ray_table_get_col(right_tbl, eq_syms[i]->i64);
        int8_t lct = join_i64_eq_key_type(lcol);
        int8_t rct = join_i64_eq_key_type(rcol);
        if (!join_i64_eq_key_supported(lct) ||
            !join_i64_eq_key_supported(rct)) {
            scratch_free(eqops_hdr);
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("nyi", "asof-join: equality key column types %s and %s are not supported",
                             ray_type_name(lct), ray_type_name(rct));
        }
    }

    if (_bxk) ray_release(_bxk);

    ray_op_t* jn = ray_asof_join(g, left_node, right_node,
                                time_op, eq_ops, (uint32_t)n_eq, 1);
    scratch_free(eqops_hdr);
    if (!jn) { ray_graph_free(g); return ray_error("oom", NULL); }

    jn = ray_optimize(g, jn);
    ray_t* result = ray_execute(g, jn);
    ray_graph_free(g);
    return result;
}

/* ── streaming parted asof: shape detection + per-partition eval ────────────
 *
 * The parted operands are received UNFLATTENED and the join runs per-day with
 * bounded memory.  A running CARRY table (last prevailing quote per equality-key
 * tuple, as of the end of each processed day) is prepended to each day's quote
 * segment, so a first-of-day trade — or a trade on a day its symbol is quiet —
 * still resolves to the most recent prior-day quote.  See asof_carry_recompute.
 */

/* Does this table carry at least one parted / MAPCOMMON column (i.e. is it a
 * parted table we can segment)?  All parted columns must share a segment
 * count; we read it from the first parted/MAPCOMMON column. */
static int64_t asof_parted_seg_count(ray_t* tbl) {
    if (!tbl || tbl->type != RAY_TABLE) return -1;
    int64_t ncols = ray_table_ncols(tbl);
    for (int64_t c = 0; c < ncols; c++) {
        ray_t* col = ray_table_get_col_idx(tbl, c);
        if (!col) continue;
        if (RAY_IS_PARTED(col->type)) return col->len;
        if (col->type == RAY_MAPCOMMON) {
            /* MAPCOMMON layout: [key_values, ...]; seg count = key_values len */
            ray_t** mc = (ray_t**)ray_data(col);
            return (col->len >= 1 && mc[0]) ? mc[0]->len : -1;
        }
    }
    return -1;  /* flat table — no segments */
}

/* If arg_ast is exactly `(select {from: X …})` and X evaluates to a parted
 * table, return that parted table (OWNED — caller releases) and set *out_dict
 * to the (borrowed) clause dict AST.  Otherwise return NULL (caller falls back
 * to flat evaluation).  A NULL / non-parted / non-select operand declines. */
static ray_t* asof_parted_select_operand(ray_t* arg_ast, ray_t** out_dict) {
    *out_dict = NULL;
    if (!arg_ast || arg_ast->type != RAY_LIST || ray_len(arg_ast) < 2) return NULL;
    ray_t** e = (ray_t**)ray_data(arg_ast);
    ray_t* head = e[0];
    if (!head || head->type != -RAY_SYM || (head->attrs & ATTR_QUOTED)) return NULL;
    if (head->i64 != ray_sym_intern("select", 6)) return NULL;
    ray_t* dict = e[1];
    if (!dict || dict->type != RAY_DICT) return NULL;
    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr) return NULL;
    ray_t* tbl = ray_eval(from_expr);
    if (!tbl || RAY_IS_ERR(tbl)) { if (tbl) ray_release(tbl); return NULL; }
    if (tbl->type != RAY_TABLE || asof_parted_seg_count(tbl) < 0) {
        ray_release(tbl);
        return NULL;
    }
    /* Per-day streaming concatenates each partition's select result.  That is
     * equivalent to flat (whole-table) evaluation for row-preserving clauses
     * (`from:`, `where:`) and per-row PROJECTION columns (`col: expr`) — those
     * all stream.  Only the WHOLE-ROWSET clauses must be rejected: `by:`
     * (group-by), `take:` (limit), `asc:`/`desc:` (sort) and `nearest:` (ANN
     * order) — run per partition and concatenated they SILENTLY diverge from
     * the flat result (e.g. `by:` yields one row per (DAY,key) instead of one
     * row per key overall).  Any of those declines to stream so the caller
     * falls back to the byte-identical flat eager path (correctness first).
     * Every OTHER key is a projection output column and is safe. */
    ray_t* keys = ray_dict_keys(dict);   /* borrowed */
    if (!keys || keys->type != RAY_SYM) { ray_release(tbl); return NULL; }
    for (int64_t i = 0; i < keys->len; i++) {
        ray_t* s = ray_sym_vec_cell(keys, i);
        if (!s) { ray_release(tbl); return NULL; }
        int64_t sl = ray_str_len(s);
        const char* sp = ray_str_ptr(s);
        bool blocked = (sl == 2 && memcmp(sp, "by",      2) == 0) ||
                       (sl == 4 && memcmp(sp, "take",    4) == 0) ||
                       (sl == 3 && memcmp(sp, "asc",     3) == 0) ||
                       (sl == 4 && memcmp(sp, "desc",    4) == 0) ||
                       (sl == 7 && memcmp(sp, "nearest", 7) == 0);
        if (blocked) { ray_release(tbl); return NULL; }
    }
    *out_dict = dict;
    return tbl;   /* owned */
}

/* Evaluate the operand's select over ONE partition: rebuild the clause dict
 * with from: bound to segment `seg` of parted_tbl (a self-evaluating flat
 * table value), then run ray_select.  Reuses ALL of select's projection /
 * where / by logic on a single day's flat sub-table.  Returns an owned table
 * (or RAY_IS_ERR). */
static ray_t* asof_eval_select_segment(ray_t* dict, ray_t* parted_tbl, int32_t seg) {
    ray_t* seg_tbl = build_segment_table(parted_tbl, seg);
    if (!seg_tbl || RAY_IS_ERR(seg_tbl)) return seg_tbl;

    ray_t* keys = ray_dict_keys(dict);   /* borrowed */
    ray_t* vals = ray_dict_vals(dict);   /* borrowed */
    if (!keys || keys->type != RAY_SYM || !vals || vals->type != RAY_LIST) {
        ray_release(seg_tbl);
        return ray_error("type", "asof stream: malformed select clause dict");
    }
    int64_t from_idx = -1, nk = keys->len;
    for (int64_t i = 0; i < nk; i++) {
        ray_t* s = ray_sym_vec_cell(keys, i);
        if (s && ray_str_len(s) == 4 && memcmp(ray_str_ptr(s), "from", 4) == 0) {
            from_idx = i; break;
        }
    }
    if (from_idx < 0) { ray_release(seg_tbl); return ray_error("domain", "asof stream: select missing from:"); }

    /* Clone vals with from: -> seg_tbl (self-evaluating). */
    ray_t* nvals = ray_list_new(vals->len);
    if (!nvals || RAY_IS_ERR(nvals)) { ray_release(seg_tbl); return nvals ? nvals : ray_error("oom", NULL); }
    nvals->len = vals->len;
    ray_t** src = (ray_t**)ray_data(vals);
    for (int64_t i = 0; i < vals->len; i++)
        nvals = ray_list_set(nvals, i, (i == from_idx) ? seg_tbl : src[i]);  /* set retains */
    ray_release(seg_tbl);   /* nvals now holds the ref */

    ray_retain(keys);
    ray_t* ndict = ray_dict_new(keys, nvals);   /* consumes both */
    if (!ndict || RAY_IS_ERR(ndict)) return ndict ? ndict : ray_error("oom", NULL);
    ray_t* r = ray_select_impl(&ndict, 1, true);
    ray_release(ndict);
    return r;
}

/* ── streaming parted ORDER BY: concat of pre-sorted partitions ─────────────
 *
 * Ascending lexicographic compare of row `ra` of key columns `ca[]` against
 * row `rb` of key columns `cb[]` (n_keys parallel columns, one row each side).
 * SYM keys compare by DOMAIN STRING (correct across segments even if the two
 * segments carry distinct sym domains); integer-family keys compare raw.
 * Direction-agnostic: returns <0 / 0 / >0 in ascending sense — the caller
 * applies the asc/desc interpretation.  Used only for the cross-partition
 * boundary compare (two scalar rows), never per-row. */
static int sort_stream_cmp_boundary(ray_t** ca, int64_t ra,
                                    ray_t** cb, int64_t rb, int64_t n_keys) {
    for (int64_t k = 0; k < n_keys; k++) {
        ray_t* a = ca[k];
        ray_t* b = cb[k];
        if (a->type == RAY_SYM) {
            int64_t ia = ray_read_sym(ray_data(a), ra, RAY_SYM, a->attrs);
            int64_t ib = ray_read_sym(ray_data(b), rb, RAY_SYM, b->attrs);
            ray_t* sa = ray_sym_domain_str(ray_sym_vec_domain(a), ia);
            ray_t* sb = ray_sym_domain_str(ray_sym_vec_domain(b), ib);
            uint32_t la = sa ? (uint32_t)ray_str_len(sa) : 0;
            uint32_t lb = sb ? (uint32_t)ray_str_len(sb) : 0;
            uint32_t ml = la < lb ? la : lb;
            int cmp = ml ? memcmp(ray_str_ptr(sa), ray_str_ptr(sb), ml) : 0;
            if (cmp == 0) cmp = (la > lb) - (la < lb);
            if (cmp) return cmp;
        } else {
            int64_t va = read_col_i64(ray_data(a), ra, a->type, a->attrs);
            int64_t vb = read_col_i64(ray_data(b), rb, b->type, b->attrs);
            if (va != vb) return va < vb ? -1 : 1;
        }
    }
    return 0;
}

/* Detect and execute the streaming parted ORDER BY shape.  Returns an owned
 * result (possibly RAY_IS_ERR) on the eligible path, or NULL to DECLINE to
 * the flat flatten+sort path.  Borrows `tbl` (the caller owns / releases it).
 *
 * Eligibility (ALL must hold; else decline):
 *   1. `tbl` is genuinely parted; asc:/desc: present; no by:/take:/nearest:.
 *   2. Every sort key is a bare source-column reference (no computed key) that
 *      resolves to a genuinely PARTED value column — a MAPCOMMON partition key
 *      or non-parted column declines (the flat path handles those).
 *   3. A SINGLE uniform direction across all keys (mixed asc+desc declines).
 *   4. Each partition is internally sorted on the keys in that direction
 *      (verified per-segment via ray_key_cols_sorted at O(partition), bailing
 *      early; null-bearing / float / STR keys are undecidable → decline).
 *   5. Consecutive non-empty partitions are globally ordered: last key of p
 *      <= first key of p+1 for asc (>= for desc).  Overlapping partition key
 *      ranges → decline (their concat would NOT be globally sorted).
 *
 * When all hold, the flat STABLE sort's result equals the ordered concat:
 * partitions are visited in segment order (== the flat flatten order) and
 * each is kept in its own (already-sorted, stable) row order, so equal keys —
 * including duplicates that straddle a partition boundary — land lower-
 * partition-then-lower-row, exactly as the flat stable sort places them. */
static ray_t* try_stream_parted_order_by(
    ray_t* dict, ray_t* tbl, ray_t** dict_elems, int64_t dict_n,
    int64_t from_id, int64_t where_id, int64_t by_id, int64_t take_id,
    int64_t asc_id, int64_t desc_id, int64_t nearest_id) {

    int64_t nseg = asof_parted_seg_count(tbl);
    if (nseg < 1) return NULL;                 /* not parted / no segments */

    /* Gate on clause shape: asc/desc present, no by/take/nearest, single
     * uniform direction, and only bare-column sort keys. */
    bool has_asc = false, has_desc = false;
    int64_t n_keys = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == by_id || kid == take_id || kid == nearest_id) return NULL;
        if (kid != asc_id && kid != desc_id) continue;
        if (kid == asc_id)  has_asc = true;
        if (kid == desc_id) has_desc = true;
        ray_t* v = dict_elems[i + 1];
        if (!v) return NULL;
        if (v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) n_keys += 1;
        else if (v->type == RAY_SYM) n_keys += ray_len(v);
        else return NULL;                      /* computed / literal sort key */
    }
    if (n_keys < 1) return NULL;               /* no sort key */
    if (has_asc && has_desc) return NULL;      /* mixed direction */
    uint8_t descending = has_desc ? 1 : 0;

    /* Every OUTPUT column must be a bare source-column ref (row-preserving) —
     * same invariant as try_stream_parted_order_by_topk's output-shape gate.
     * An aggregate/expression output collapses per segment (one row per
     * partition), which would concat into nseg wrong rows instead of the
     * flat path's single correctly-aggregated result, so it declines here. */
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id ||
            kid == nearest_id) continue;
        ray_t* v = dict_elems[i + 1];
        if (!v || v->type != -RAY_SYM || (v->attrs & ATTR_QUOTED)) return NULL;
        if (!ray_table_get_col(tbl, v->i64)) return NULL;
    }

    /* Carve: key syms + resolved parted columns + two per-segment key-vec
     * scratch rows (current segment, previous non-empty segment). */
    ray_t* hdr = NULL;
    int64_t* key_syms = (int64_t*)scratch_alloc(&hdr,
        (size_t)n_keys * (sizeof(int64_t) + 3 * sizeof(ray_t*)));
    if (!key_syms) return NULL;                /* decline (flat path retries) */
    ray_t** kcols   = (ray_t**)(key_syms + n_keys);   /* parted key columns */
    ray_t** segkeys = (ray_t**)(kcols + n_keys);      /* current seg key vecs */
    ray_t** prevkeys = (ray_t**)(segkeys + n_keys);   /* prev non-empty seg */

    int64_t ki = 0;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid != asc_id && kid != desc_id) continue;
        ray_t* v = dict_elems[i + 1];
        if (v->type == -RAY_SYM) key_syms[ki++] = v->i64;
        else for (int64_t c = 0; c < ray_len(v); c++)
            key_syms[ki++] = sym_cell_runtime_id(v, c);
    }

    /* Resolve every sort key to a genuinely parted value column of matching
     * segment count and a DECIDABLE base type (null-free integer-family / SYM
     * — float NaN ordering and STR/GUID belong to the real sort).  MAPCOMMON /
     * non-parted / missing / undecidable-typed → decline (the flat path, which
     * itself surfaces the pre-existing lazy-sort-over-parted `type` error,
     * handles those loudly).  The base-type gate here is essential: it declines
     * an undecidable key even when every segment is 0/1 rows (where the
     * per-segment sortedness check would trivially pass). */
    for (int64_t k = 0; k < n_keys; k++) {
        ray_t* col = ray_table_get_col(tbl, key_syms[k]);
        if (!col || !RAY_IS_PARTED(col->type) || col->len != nseg) {
            scratch_free(hdr); return NULL;
        }
        int8_t bt = (int8_t)RAY_PARTED_BASETYPE(col->type);
        if (bt != RAY_BOOL && bt != RAY_U8 && bt != RAY_I16 && bt != RAY_I32 &&
            bt != RAY_I64 && bt != RAY_DATE && bt != RAY_TIME &&
            bt != RAY_TIMESTAMP && bt != RAY_SYM) {
            scratch_free(hdr); return NULL;
        }
        kcols[k] = col;
    }

    /* Per-partition sortedness + cross-partition boundary ordering.  Empty
     * segments contribute no rows and no boundary; the boundary is checked
     * against the previous NON-EMPTY segment's last row. */
    int64_t prev_last = -1;   /* last row index of prev non-empty segment */
    bool have_prev = false;
    for (int64_t s = 0; s < nseg; s++) {
        int64_t seg_rows = -1;
        for (int64_t k = 0; k < n_keys; k++) {
            ray_t** segs = (ray_t**)ray_data(kcols[k]);
            ray_t* sv = segs[s];
            if (!sv) { scratch_free(hdr); return NULL; }
            segkeys[k] = sv;
            if (seg_rows < 0) seg_rows = sv->len;
            else if (sv->len != seg_rows) { scratch_free(hdr); return NULL; }
        }
        if (seg_rows == 0) continue;   /* empty partition */
        if (!ray_key_cols_sorted(segkeys, n_keys, descending, seg_rows)) {
            scratch_free(hdr); return NULL;      /* not internally sorted */
        }
        if (have_prev) {
            int cmp = sort_stream_cmp_boundary(prevkeys, prev_last,
                                               segkeys, 0, n_keys);
            /* asc: need last(prev) <= first(cur) → cmp <= 0.
             * desc: need last(prev) >= first(cur) → cmp >= 0. */
            if (descending ? (cmp < 0) : (cmp > 0)) {
                scratch_free(hdr); return NULL;  /* overlapping ranges */
            }
        }
        for (int64_t k = 0; k < n_keys; k++) prevkeys[k] = segkeys[k];
        prev_last = seg_rows - 1;
        have_prev = true;
    }

    /* Eligible.  Build the per-segment select dict once, dropping asc:/desc:
     * (the concat is already globally ordered — no per-partition sort). */
    ray_t* okeys = ray_dict_keys(dict);   /* borrowed SYM vec */
    ray_t* ovals = ray_dict_vals(dict);   /* borrowed LIST */
    if (!okeys || okeys->type != RAY_SYM || !ovals || ovals->type != RAY_LIST) {
        scratch_free(hdr); return NULL;
    }
    int64_t onk = okeys->len;
    int64_t keep = 0;
    for (int64_t i = 0; i < onk; i++) {
        int64_t kid = sym_cell_runtime_id(okeys, i);
        if (kid != asc_id && kid != desc_id) keep++;
    }
    ray_t* nkeys = ray_vec_new(RAY_SYM, keep ? keep : 1);
    ray_t* nvals = ray_list_new(keep ? keep : 1);
    if (!nkeys || RAY_IS_ERR(nkeys) || !nvals || RAY_IS_ERR(nvals)) {
        if (nkeys && !RAY_IS_ERR(nkeys)) ray_release(nkeys);
        if (nvals && !RAY_IS_ERR(nvals)) ray_release(nvals);
        scratch_free(hdr); return NULL;
    }
    nkeys->len = keep; nvals->len = keep;
    int64_t* nkd = (int64_t*)ray_data(nkeys);
    ray_t** ovd = (ray_t**)ray_data(ovals);
    int64_t w = 0;
    for (int64_t i = 0; i < onk; i++) {
        int64_t kid = sym_cell_runtime_id(okeys, i);
        if (kid == asc_id || kid == desc_id) continue;
        nkd[w] = kid;
        nvals = ray_list_set(nvals, w, ovd[i]);   /* retains ovd[i] */
        w++;
    }
    ray_t* sdict = ray_dict_new(nkeys, nvals);    /* consumes both */
    if (!sdict || RAY_IS_ERR(sdict)) { scratch_free(hdr); return sdict ? sdict : ray_error("oom", NULL); }

    /* Stream: per-partition select, ordered concat, one counter bump/segment. */
    ray_t* accum = NULL;
    for (int64_t s = 0; s < nseg; s++) {
        ray_t* seg_res = asof_eval_select_segment(sdict, tbl, (int32_t)s);
        atomic_fetch_add_explicit(&ray_sort_perpart_runs_ctr, 1,
                                  memory_order_relaxed);
        if (!seg_res || RAY_IS_ERR(seg_res)) {
            if (accum) ray_release(accum);
            ray_release(sdict); scratch_free(hdr);
            return seg_res ? seg_res : ray_error("oom", NULL);
        }
        ray_t* merged = ray_result_merge(accum, seg_res);
        ray_release(seg_res);
        if (accum) ray_release(accum);
        accum = merged;
        if (!accum || RAY_IS_ERR(accum)) {
            ray_release(sdict); scratch_free(hdr);
            return accum ? accum : ray_error("oom", NULL);
        }
    }
    ray_release(sdict);
    scratch_free(hdr);
    return accum;   /* nseg >= 1 guarantees a non-NULL result */
}

/* ── streaming parted ORDER BY + LIMIT: per-partition top-k ─────────────────
 *
 * `(select {COLS…} from: PARTED [where: W] asc/desc: KEY take: k)` — an
 * OUTPUT-bounded ORDER BY + LIMIT.  Instead of flattening the whole parted
 * table, sorting O(total) rows and slicing k, run top-k on EACH partition's
 * segment independently: the per-segment ray_select keeps the asc/desc/take
 * clauses, so each segment yields its own local top-k (≤k rows, already
 * sorted).  Concat the ≤P·k candidates, then run the SAME apply_sort_take ONCE
 * MORE over the candidate table → the global top-k.  Peak working memory ≈ one
 * segment + O(P·k) candidates; the output is O(k).
 *
 * Correctness (byte-identical to flatten→apply_sort_take→take):
 *   - The union of the per-partition top-k provably contains the global top-k:
 *     a global-top-k row has ≤k-1 rows ordered ahead of it globally, hence
 *     ≤k-1 within its own partition, so it survives that partition's local
 *     top-k (or its partition has <k rows and every row is kept).
 *   - Both stages route through apply_sort_take's STABLE (key, original-row)
 *     top-k kernels.  The candidate table is assembled in segment order and
 *     each segment's block preserves (key, local-row) order, so equal-key rows
 *     sit in global-flatten order in the candidate — the final stable merge
 *     therefore breaks ties by global original position, exactly as the flat
 *     stable sort does.
 *
 * Unlike the pre-sorted concat path this does NOT require partitions to be
 * internally sorted or their key ranges disjoint — per-partition top-k works
 * on unsorted segments — so it has BROADER eligibility.  Declines (NULL) to
 * the flat path on any ineligible shape.
 *
 * Eligibility (ALL must hold; else decline):
 *   1. `tbl` is genuinely parted (≥1 segment).
 *   2. asc:/desc: present; take: present; no by:/nearest:.
 *   3. take: evaluates to a POSITIVE integer atom k (>0).  Ranges / negatives /
 *      non-atoms are slices, not top-k, and decline.
 *   4. Every OUTPUT column is a bare (unquoted) source-column reference — a
 *      pure per-row gather, so per-partition evaluation is row-preserving.  An
 *      aggregate/expression output would collapse per segment (one row per
 *      partition) and diverge from flat, so it declines.
 *   5. Every sort key resolves to an output column (its alias), so the
 *      candidate table carries the sort column for the final merge. */
static ray_t* try_stream_parted_order_by_topk(
    ray_t* dict, ray_t* tbl, ray_t** dict_elems, int64_t dict_n,
    int64_t from_id, int64_t where_id, int64_t by_id, int64_t take_id,
    int64_t asc_id, int64_t desc_id, int64_t nearest_id) {

    int64_t nseg = asof_parted_seg_count(tbl);
    if (nseg < 1) return NULL;                 /* not parted / no segments */

    /* Clause-shape gate: asc/desc + take present, no by/nearest. */
    bool has_sort = false, has_take = false;
    ray_t* take_v = NULL;
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == by_id || kid == nearest_id) return NULL;
        if (kid == asc_id || kid == desc_id) has_sort = true;
        if (kid == take_id) { has_take = true; take_v = dict_elems[i + 1]; }
    }
    if (!has_sort || !has_take || !take_v) return NULL;

    /* take: must evaluate to a positive integer atom. */
    ray_t* kv = ray_eval(take_v);
    if (!kv || RAY_IS_ERR(kv) || !ray_is_atom(kv) ||
        (kv->type != -RAY_I64 && kv->type != -RAY_I32)) {
        if (kv) ray_release(kv);
        return NULL;
    }
    int64_t k = (kv->type == -RAY_I64) ? kv->i64 : kv->i32;
    ray_release(kv);
    /* k == 0 is a well-defined top-0 (empty); handle it in-stream rather than
     * declining, since the flat fallback cannot sort a parted table.  A
     * negative / range take is a slice, not a top-k — decline it. */
    if (k < 0) return NULL;

    /* Every output column must be a bare source-column ref (row-preserving). */
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid == from_id || kid == where_id || kid == by_id ||
            kid == take_id || kid == asc_id || kid == desc_id ||
            kid == nearest_id) continue;
        ray_t* v = dict_elems[i + 1];
        if (!v || v->type != -RAY_SYM || (v->attrs & ATTR_QUOTED)) return NULL;
        if (!ray_table_get_col(tbl, v->i64)) return NULL;
    }

    /* Every sort key must appear as an output alias (dict key of an output
     * column), so the candidate table carries the sort column. */
    for (int64_t i = 0; i + 1 < dict_n; i += 2) {
        int64_t kid = dict_elems[i]->i64;
        if (kid != asc_id && kid != desc_id) continue;
        ray_t* v = dict_elems[i + 1];
        if (!v) return NULL;
        int64_t nk = (v->type == -RAY_SYM && !(v->attrs & ATTR_QUOTED)) ? 1
                   : (v->type == RAY_SYM ? ray_len(v) : -1);
        if (nk < 0) return NULL;                /* computed / literal sort key */
        for (int64_t c = 0; c < nk; c++) {
            int64_t sk = (v->type == -RAY_SYM) ? v->i64
                                               : sym_cell_runtime_id(v, c);
            bool found = false;
            for (int64_t j = 0; j + 1 < dict_n && !found; j += 2) {
                int64_t okid = dict_elems[j]->i64;
                if (okid == from_id || okid == where_id || okid == by_id ||
                    okid == take_id || okid == asc_id || okid == desc_id ||
                    okid == nearest_id) continue;
                if (okid == sk) found = true;
            }
            if (!found) return NULL;
        }
    }

    /* Eligible.  Per-partition top-k via each segment's own ray_select (keeps
     * asc/desc/take → local top-k), concat into a candidate table, then one
     * final apply_sort_take over the candidates → the global top-k.  One
     * counter bump per partition. */
    ray_t* accum = NULL;
    for (int64_t s = 0; s < nseg; s++) {
        ray_t* seg_res = asof_eval_select_segment(dict, tbl, (int32_t)s);
        atomic_fetch_add_explicit(&ray_sort_perpart_runs_ctr, 1,
                                  memory_order_relaxed);
        if (!seg_res || RAY_IS_ERR(seg_res)) {
            if (accum) ray_release(accum);
            return seg_res ? seg_res : ray_error("oom", NULL);
        }
        ray_t* merged = ray_result_merge(accum, seg_res);
        ray_release(seg_res);
        if (accum) ray_release(accum);
        accum = merged;
        if (!accum || RAY_IS_ERR(accum))
            return accum ? accum : ray_error("oom", NULL);
    }

    /* Final merge: apply_sort_take reads asc/desc/take straight from dict_elems
     * and is the SAME stable kernel the flat path uses, so the k rows and their
     * order are byte-identical to flatten→sort→take.  It consumes `accum`. */
    return apply_sort_take(accum, dict_elems, dict_n, asc_id, desc_id, take_id, NULL);
}

/* If `tbl` is DATE-partitioned, return its (borrowed) per-partition RAY_DATE
 * key-values vector and set *out_n to the partition count; else return NULL.
 *
 * The partition key VALUES live in the MAPCOMMON column's mc[0] (one date per
 * partition), ascending by construction (collect_part_dirs sorts the on-disk
 * YYYY.MM.DD dirs, which sort chronologically).  We date-align ONLY when the
 * key is a DATE — an integer/sym partition scheme (mc[0] not RAY_DATE) returns
 * NULL so the caller falls back to the eager kernel (correctness first, no
 * date arithmetic on a non-date scheme). */
static ray_t* asof_parted_date_keys(ray_t* tbl, int64_t* out_n) {
    *out_n = 0;
    if (!tbl || tbl->type != RAY_TABLE) return NULL;
    int64_t ncols = ray_table_ncols(tbl);
    for (int64_t c = 0; c < ncols; c++) {
        ray_t* col = ray_table_get_col_idx(tbl, c);
        if (col && col->type == RAY_MAPCOMMON) {
            ray_t** mc = (ray_t**)ray_data(col);
            if (col->len >= 1 && mc[0] && mc[0]->type == RAY_DATE) {
                *out_n = mc[0]->len;
                return mc[0];   /* borrowed: owned by tbl's MAPCOMMON column */
            }
            return NULL;        /* MAPCOMMON but not DATE-keyed → no date align */
        }
    }
    return NULL;
}

/* Build a 0-row table matching the schema the right operand's select produces
 * per day (evaluated over segment 0), for a trades-only day: the asof then
 * null-fills every right column, exactly as a genuine no-match would.  Owned;
 * caller releases.  Built once and reused across all trades-only days. */
static ray_t* asof_empty_right(ray_t* dict, ray_t* parted_tbl) {
    ray_t* seg0 = asof_eval_select_segment(dict, parted_tbl, 0);
    if (!seg0 || RAY_IS_ERR(seg0)) return seg0;
    int64_t nc = ray_table_ncols(seg0);
    ray_t* out = ray_table_new(nc);
    if (!out || RAY_IS_ERR(out)) { ray_release(seg0); return out ? out : ray_error("oom", NULL); }
    for (int64_t c = 0; c < nc; c++) {
        ray_t* col = ray_table_get_col_idx(seg0, c);
        int64_t nm = ray_table_col_name(seg0, c);
        int8_t  ct = col ? col->type : RAY_I64;
        ray_t*  ev = ray_vec_new(ct, 0);
        if (!ev || RAY_IS_ERR(ev)) { ray_release(seg0); ray_release(out); return ev ? ev : ray_error("oom", NULL); }
        ev->len = 0;
        if (ct == RAY_SYM && col) ray_sym_vec_adopt_domain(ev, col);
        out = ray_table_add_col(out, nm, ev);
        ray_release(ev);
        if (!out || RAY_IS_ERR(out)) { ray_release(seg0); return out ? out : ray_error("oom", NULL); }
    }
    ray_release(seg0);
    return out;
}

/* O(ncols) positional column-NAME alignment check.  ray_result_merge /
 * ray_vec_concat pair columns by INDEX, so concatenating two tables is only
 * meaningful when they list the same column names in the same order.
 * asof_carry_recompute reorders the carry to the day segment's column order to
 * keep this invariant, and every day's segment is the same rdict select over
 * the same parted table (so its order is stable) — but both are implicit.  This
 * guard makes the invariant explicit and lets a merge site fail LOUD (clean
 * schema error) rather than silently concatenating a column under the wrong
 * name.  Returns true iff aligned; O(ncols), never O(rows). */
static bool asof_cols_aligned(ray_t* a, ray_t* b) {
    if (!a || !b || a->type != RAY_TABLE || b->type != RAY_TABLE) return false;
    int64_t na = ray_table_ncols(a), nb = ray_table_ncols(b);
    if (na != nb) return false;
    for (int64_t c = 0; c < na; c++)
        if (ray_table_col_name(a, c) != ray_table_col_name(b, c)) return false;
    return true;
}

/* ── cross-day carry: running last-prevailing-quote per equality-key tuple ───
 *
 * Recompute the carry table from `qtab` (= prior carry rows PREPENDED to the
 * day's quote segment).  The carry is the most-recent quote row per full
 * equality-key tuple, obtained with the engine's own group-by:
 *
 *   (select {c: (last c) for every non-eq column c  by: {eq keys}  from: qtab})
 *
 * Because qtab lists the (older) carry rows before the (newer) same-day quotes,
 * and quotes within a day are time-ascending, `last` per group picks the
 * most-recent quote — which naturally (a) keeps a gap key's carried row when it
 * did not quote today (it is still present in qtab), (b) refreshes keys that
 * quoted today, and (c) never invents a slot for a key that never quoted.
 *
 * `template` is the day's quote-segment table; the group-by output is reordered
 * to `template`'s exact column order so the NEXT day's ray_result_merge (which
 * concatenates columns positionally) stays schema-consistent.  `eq_names` are
 * the equality-key column name ids (keys_vec minus the trailing time key).
 * Returns an owned table (or RAY_IS_ERR); `qtab` is borrowed. */
static ray_t* asof_carry_recompute(ray_t* qtab, ray_t* template,
                                   const int64_t* eq_names, int64_t n_eq) {
    int64_t ncols = ray_table_ncols(template);

    /* Lone-time-key asof (n_eq == 0): the equality keys are OPTIONAL, so the
     * carry is a SINGLE global row = the last quote across the whole day.  A
     * present-but-0-length `by:` SYM vector does NOT mean "one global group" —
     * the select by-clause compiler special-cases len==1 and len>1 only, and a
     * 0-length by: degrades to an ungrouped PASSTHROUGH (every row survives),
     * which would let the carry grow without bound across days.  So we emit the
     * clause with NO by: entry at all — a whole-table `(select {c:(last c)}…)`
     * aggregate, which yields exactly one row.  With eq keys present we build the
     * by: SYM vec of the full equality-key tuple (name ids). */
    bool has_by = (n_eq > 0);
    ray_t* by_vec = NULL;
    if (has_by) {
        by_vec = ray_vec_new(RAY_SYM, n_eq);
        if (!by_vec || RAY_IS_ERR(by_vec)) return by_vec ? by_vec : ray_error("oom", NULL);
        by_vec->len = n_eq;
        for (int64_t i = 0; i < n_eq; i++) ((int64_t*)ray_data(by_vec))[i] = eq_names[i];
    }

    /* Clause dict: one `col: (last col)` projection per NON-eq column, plus
     * from: and (when eq keys exist) by:.  Slot count = non-eq cols + 1 (+1 for
     * by: when has_by). */
    int64_t n_proj = ncols - n_eq;
    if (n_proj < 0) n_proj = 0;
    int64_t nslot = n_proj + (has_by ? 2 : 1);
    ray_t* keys = ray_vec_new(RAY_SYM, nslot);
    if (!keys || RAY_IS_ERR(keys)) { if (by_vec) ray_release(by_vec); return keys ? keys : ray_error("oom", NULL); }
    keys->len = nslot;
    ray_t* vals = ray_list_new(nslot);
    if (!vals || RAY_IS_ERR(vals)) { if (by_vec) ray_release(by_vec); ray_release(keys); return vals ? vals : ray_error("oom", NULL); }
    vals->len = nslot;
    int64_t* kids = (int64_t*)ray_data(keys);

    int64_t last_sym = ray_sym_intern("last", 4);
    int64_t s = 0;
    ray_t* build_err = NULL;
    for (int64_t c = 0; c < ncols && !build_err; c++) {
        int64_t nm = ray_table_col_name(template, c);
        bool is_eq = false;
        for (int64_t k = 0; k < n_eq; k++) if (eq_names[k] == nm) { is_eq = true; break; }
        if (is_eq) continue;   /* eq keys enter the output via by: */
        if (s >= n_proj) { build_err = ray_error("schema", "asof carry: projection overflow"); break; }
        /* (last <col>) — head + column ref are both unquoted name syms. */
        ray_t* head = ray_sym(last_sym);
        ray_t* cref = ray_sym(nm);
        ray_t* agg  = ray_list_new(2);
        if (!head || RAY_IS_ERR(head) || !cref || RAY_IS_ERR(cref) || !agg || RAY_IS_ERR(agg)) {
            if (head && !RAY_IS_ERR(head)) ray_release(head);
            if (cref && !RAY_IS_ERR(cref)) ray_release(cref);
            build_err = (agg && RAY_IS_ERR(agg)) ? agg : ray_error("oom", NULL);
            if (agg && !RAY_IS_ERR(agg)) ray_release(agg);
            break;
        }
        agg->len = 2;
        agg = ray_list_set(agg, 0, head);   ray_release(head);
        agg = ray_list_set(agg, 1, cref);   ray_release(cref);
        kids[s] = nm;
        vals = ray_list_set(vals, s, agg);   ray_release(agg);
        s++;
    }
    if (!build_err) {
        if (has_by) {
            kids[s]  = ray_sym_intern("by", 2);
            vals = ray_list_set(vals, s, by_vec);   /* retains */
            s++;
        }
        kids[s]  = ray_sym_intern("from", 4);
        vals = ray_list_set(vals, s, qtab);      /* retains borrowed qtab */
    }
    if (by_vec) ray_release(by_vec);
    if (build_err) { ray_release(keys); ray_release(vals); return build_err; }

    ray_t* dict = ray_dict_new(keys, vals);      /* consumes keys + vals */
    if (!dict || RAY_IS_ERR(dict)) return dict ? dict : ray_error("oom", NULL);
    ray_t* raw = ray_select_impl(&dict, 1, true);
    ray_release(dict);
    if (!raw || RAY_IS_ERR(raw)) return raw ? raw : ray_error("type", NULL);

    /* Reorder group-by output (eq keys first, then aggs) back to `template`'s
     * column order so positional column concat stays valid next day. */
    ray_t* out = ray_table_new(ncols);
    if (!out || RAY_IS_ERR(out)) { ray_release(raw); return out ? out : ray_error("oom", NULL); }
    for (int64_t c = 0; c < ncols; c++) {
        int64_t nm = ray_table_col_name(template, c);
        ray_t* col = ray_table_get_col(raw, nm);
        if (!col) { ray_release(raw); ray_release(out); return ray_error("schema", "asof carry: column %lld missing from group-last", (long long)nm); }
        out = ray_table_add_col(out, nm, col);
        if (!out || RAY_IS_ERR(out)) { ray_release(raw); return out ? out : ray_error("oom", NULL); }
    }
    ray_release(raw);
    return out;
}

/* (asof-join [key1 key2 ... timeKey] leftTable rightTable)
 * Last key is the time/asof column, rest are equality keys.  The equality
 * keys are OPTIONAL: a lone time key (asof-join [timeKey] L R) performs an
 * un-partitioned asof over all rows.
 *
 * SPECIAL FORM: args arrive UNEVALUATED so a parted (select…from:PARTED) pair
 * can be detected and streamed per-partition (bounded memory).  Any other
 * shape (flat / non-parted / non-select operands, mismatched partitioning)
 * falls back to exactly the pre-spike behaviour: evaluate both operands and
 * run ray_asof_join_core over the flattened tables. */
ray_t* ray_asof_join_fn(ray_t** args, int64_t n) {
    if (n < 3) return ray_error("arity", "asof-join: expects keys, left table and right table, got %lld args", (long long)n);

    ray_t* keys_vec = ray_eval(args[0]);
    if (!keys_vec || RAY_IS_ERR(keys_vec))
        return keys_vec ? keys_vec : ray_error("type", "asof-join: key evaluation failed");

    /* Parted-shape probe: both operands must be (select {from:PARTED …}). */
    ray_t* ldict = NULL, *rdict = NULL;
    ray_t* lparted = asof_parted_select_operand(args[1], &ldict);
    ray_t* rparted = asof_parted_select_operand(args[2], &rdict);

    /* Date-aligned streaming requires BOTH operands DATE-partitioned.  The two
     * sides may carry DIFFERENT partition sets (a day with quotes but no
     * trades, or vice versa), so segments are paired by their partition-key
     * DATE VALUE, never by index.  A non-date (integer/sym) partition scheme on
     * either side declines to the eager fallback below. */
    int64_t l_ndates = 0, r_ndates = 0;
    ray_t* l_dates = lparted ? asof_parted_date_keys(lparted, &l_ndates) : NULL;
    ray_t* r_dates = rparted ? asof_parted_date_keys(rparted, &r_ndates) : NULL;

    if (lparted && rparted && l_dates && r_dates && l_ndates > 0 && r_ndates > 0) {
        /* Streaming per-day path over the ASCENDING UNION of both sides' dates.
         * ld/rd are the per-partition DATE values (ascending by construction).
         * A two-pointer merge visits each distinct date once; for each date d a
         * per-day prevailing-quote view `qtab` = carry ∪ day-d quotes drives:
         *   both sides have d  → asof(trade seg, qtab), then refresh carry
         *   trades-only (d only on left)  → asof(trade seg, carry) so cross-day /
         *                                    gap trades match the carried quote;
         *                                    trades with no carry entry no-match
         *   quotes-only (d only on right) → no trades emitted, but carry IS
         *                                    refreshed (its quotes matter later)
         * Peak memory is ~one day's trade+quote + the carry (+ growing result). */
        const int32_t* ld = (const int32_t*)ray_data(l_dates);
        const int32_t* rd = (const int32_t*)ray_data(r_dates);
        int64_t i = 0, j = 0;
        ray_t* accum   = NULL;
        ray_t* err     = NULL;
        ray_t* empty_r = NULL;   /* lazily-built 0-row right schema (reused) */
        ray_t* carry   = NULL;   /* running last-prevailing-quote per eq tuple */

        /* Equality-key column name ids = keys_vec minus the trailing time key.
         * The carry keys on the FULL eq tuple so (AAPL,NYSE) never matches an
         * (AAPL,NASDAQ) carried quote. */
        ray_t* eqn_kbx = NULL;
        ray_t* eqn_kv  = unbox_vec_arg(keys_vec, &eqn_kbx);
        int64_t  n_eq_c  = (is_list(eqn_kv) && ray_len(eqn_kv) >= 1) ? ray_len(eqn_kv) - 1 : 0;
        ray_t*   eqn_hdr = NULL;
        int64_t* eq_names = (int64_t*)scratch_alloc(&eqn_hdr,
                (size_t)(n_eq_c ? n_eq_c : 1) * sizeof(int64_t));
        if (!eq_names) {
            if (eqn_kbx) ray_release(eqn_kbx);
            ray_release(lparted); ray_release(rparted); ray_release(keys_vec);
            return ray_error("oom", NULL);
        }
        /* Trailing time-key column name: used to sort each day's carry view
         * time-ascending before the group-last (see below).  Captured up front
         * with the same sym validation ray_asof_join_core applies, since a
         * quotes-only first day sorts + recomputes carry BEFORE the core runs. */
        int64_t time_name = -1;
        {
            ray_t** ke = (ray_t**)ray_data(eqn_kv);
            for (int64_t k = 0; k < n_eq_c; k++) {
                /* A quotes-only first day reaches asof_carry_recompute (via
                 * eq_names) before ray_asof_join_core runs its own key
                 * validation.  Reject a non-symbol equality key up front, the
                 * same way ray_asof_join_core does, instead of reading a bogus
                 * union field. */
                if (ke[k]->type != -RAY_SYM) {
                    int8_t kt = ke[k]->type;
                    if (eqn_kbx) ray_release(eqn_kbx);
                    scratch_free(eqn_hdr);
                    ray_release(lparted); ray_release(rparted); ray_release(keys_vec);
                    return ray_error("type", "asof-join: equality key must be a symbol, got %s", ray_type_name(kt));
                }
                eq_names[k] = ke[k]->i64;
            }
            if (is_list(eqn_kv) && ray_len(eqn_kv) >= 1) {
                ray_t* tk = ke[ray_len(eqn_kv) - 1];
                if (tk->type != -RAY_SYM) {
                    int8_t tt = tk->type;
                    if (eqn_kbx) ray_release(eqn_kbx);
                    scratch_free(eqn_hdr);
                    ray_release(lparted); ray_release(rparted); ray_release(keys_vec);
                    return ray_error("type", "asof-join: time key must be a symbol, got %s", ray_type_name(tt));
                }
                time_name = tk->i64;
            }
        }
        if (eqn_kbx) ray_release(eqn_kbx);

        while (i < l_ndates || j < r_ndates) {
            int32_t d;
            int64_t lseg = -1, rseg = -1;
            if (i < l_ndates && (j >= r_ndates || ld[i] <= rd[j])) {
                d = ld[i];
                lseg = i;
                if (j < r_ndates && rd[j] == d) rseg = j;
            } else {
                d = rd[j];
                rseg = j;              /* quotes-only day: lseg stays -1 */
            }
            if (i < l_ndates && ld[i] == d) i++;
            if (j < r_ndates && rd[j] == d) j++;

            /* Count every union day processed (once, O(1) — never per-row). */
            atomic_fetch_add_explicit(&ray_asof_perpart_runs_ctr, 1, memory_order_relaxed);

            /* Today's quote segment (owned), or NULL when this day has none. */
            ray_t* day_r_seg = NULL;
            if (rseg >= 0) {
                day_r_seg = asof_eval_select_segment(rdict, rparted, (int32_t)rseg);
                if (!day_r_seg || RAY_IS_ERR(day_r_seg)) { err = day_r_seg ? day_r_seg : ray_error("type", NULL); break; }
            }

            /* qtab = prior carry rows PREPENDED to today's quotes.  Carry rows
             * come FIRST: they hold their ORIGINAL prior-day timestamps (older
             * than any day-d quote), so per-key time order stays ascending and
             * the asof kernel resolves a first-of-day trade to the carried quote
             * when no same-day quote precedes it.  A gap day (no quotes) reuses
             * the carry table directly as the prevailing-quote view. */
            ray_t* qtab = NULL;
            if (day_r_seg && carry) {
                /* Guard the positional-concat invariant before the merge: the
                 * carry MUST list the same columns in the same order as today's
                 * quote segment, else ray_result_merge would concat mismatched
                 * columns.  O(ncols); fail loud rather than corrupt silently. */
                if (!asof_cols_aligned(carry, day_r_seg)) {
                    err = ray_error("schema", "asof stream: carry/quote column order mismatch");
                    ray_release(day_r_seg); break;
                }
                qtab = ray_result_merge(carry, day_r_seg);
                /* ray_result_merge / ray_vec_concat propagate OOM as a BARE
                 * NULL, not a RAY_IS_ERR sentinel.  Both merge inputs are
                 * non-NULL here, so a NULL result is a real merge failure — it
                 * must NOT be confused with the legitimate "no carry and no
                 * quotes" case (which leaves qtab NULL via the branches below).
                 * Match the `!x || RAY_IS_ERR(x)` convention used everywhere
                 * else in this loop. */
                if (!qtab || RAY_IS_ERR(qtab)) { err = qtab ? qtab : ray_error("oom", NULL); qtab = NULL; ray_release(day_r_seg); break; }
            }
            else if (day_r_seg)    { qtab = day_r_seg; ray_retain(qtab); }
            else if (carry)        { qtab = carry;     ray_retain(qtab); }

            /* Sort the prevailing-quote view time-ascending.  The asof KERNEL
             * re-sorts its right input internally, so this does not change a
             * same-day match — but asof_carry_recompute derives the carry with a
             * `(last col) by: eq` group-by, whose `last` picks each group's LAST
             * ROW IN qtab ORDER.  That equals the latest-TIME quote only when
             * qtab is time-ascending per group; a day segment stored time-
             * UNSORTED would otherwise carry the wrong (last-stored) quote
             * forward.  Carry rows keep their older prior-day timestamps and stay
             * ahead of the same-day quotes.  sort_table_by_keys bails early and
             * returns the input retained when already ordered (the common case),
             * so a sorted day pays only an O(n) monotonicity scan. */
            if (qtab && time_name >= 0) {
                ray_t* sk = ray_sym(time_name);
                if (!sk || RAY_IS_ERR(sk)) { err = sk ? sk : ray_error("oom", NULL); ray_release(qtab); if (day_r_seg) ray_release(day_r_seg); break; }
                ray_t* qs = sort_table_by_keys(qtab, sk, 0);
                ray_release(sk);
                ray_release(qtab);
                qtab = qs;
                if (!qtab || RAY_IS_ERR(qtab)) { err = qtab ? qtab : ray_error("type", NULL); qtab = NULL; if (day_r_seg) ray_release(day_r_seg); break; }
            }

            /* Emit this day's trades (if any) against the prevailing-quote view. */
            if (lseg >= 0) {
                ray_t* day_l = asof_eval_select_segment(ldict, lparted, (int32_t)lseg);
                if (!day_l || RAY_IS_ERR(day_l)) { err = day_l ? day_l : ray_error("type", NULL); if (qtab) ray_release(qtab); if (day_r_seg) ray_release(day_r_seg); break; }

                ray_t* rt;
                if (qtab) { rt = qtab; ray_retain(rt); }
                else {
                    /* No quote seen on or before this day → empty right: every
                     * trade no-matches (null right cols), exactly as a genuine
                     * first-appearance would (no stale carry slot invented). */
                    if (!empty_r) {
                        empty_r = asof_empty_right(rdict, rparted);
                        if (!empty_r || RAY_IS_ERR(empty_r)) { err = empty_r ? empty_r : ray_error("type", NULL); empty_r = NULL; ray_release(day_l); if (day_r_seg) ray_release(day_r_seg); break; }
                    }
                    rt = empty_r; ray_retain(rt);
                }

                ray_t* part = ray_asof_join_core(keys_vec, day_l, rt);
                ray_release(day_l); ray_release(rt);
                if (!part || RAY_IS_ERR(part)) { err = part ? part : ray_error("type", NULL); if (qtab) ray_release(qtab); if (day_r_seg) ray_release(day_r_seg); break; }

                /* Same positional-concat guard for the day-to-day output
                 * accumulation: every day's asof output must share the running
                 * result's column order.  accum == NULL on the first emit
                 * (ray_result_merge then just adopts part), so only check when
                 * there is something to concat against. */
                if (accum && !asof_cols_aligned(accum, part)) {
                    err = ray_error("schema", "asof stream: per-day output column order mismatch");
                    ray_release(part); if (qtab) ray_release(qtab); if (day_r_seg) ray_release(day_r_seg); break;
                }
                ray_t* merged = ray_result_merge(accum, part);  /* returns owned */
                ray_release(part);
                if (accum) ray_release(accum);
                accum = merged;
                if (!accum || RAY_IS_ERR(accum)) { err = accum; accum = NULL; if (qtab) ray_release(qtab); if (day_r_seg) ray_release(day_r_seg); break; }
            }

            /* Refresh carry = group-last-by-eq(qtab) after a day that quoted.
             * qtab holds (carry ∪ today's quotes) so a key that did NOT quote
             * today keeps its carried row through the group-last. */
            if (rseg >= 0) {
                ray_t* nc = asof_carry_recompute(qtab, day_r_seg, eq_names, n_eq_c);
                if (!nc || RAY_IS_ERR(nc)) { err = nc ? nc : ray_error("type", NULL); if (qtab) ray_release(qtab); ray_release(day_r_seg); break; }
                if (carry) ray_release(carry);
                carry = nc;
            }

            if (qtab) ray_release(qtab);
            if (day_r_seg) ray_release(day_r_seg);
        }
        scratch_free(eqn_hdr);
        if (carry) ray_release(carry);
        if (empty_r) ray_release(empty_r);
        ray_release(lparted); ray_release(rparted); ray_release(keys_vec);
        if (err) { if (accum) ray_release(accum); return err; }
        return accum ? accum : ray_error("domain", "asof stream: no partitions produced");
    }

    /* Fallback: not the parted-select shape, or a non-date partition scheme —
     * evaluate operands and run the flat kernel (identical to pre-spike
     * behaviour, always correct). */
    if (lparted) ray_release(lparted);
    if (rparted) ray_release(rparted);

    ray_t* left = ray_eval(args[1]);
    if (!left || RAY_IS_ERR(left)) { ray_release(keys_vec); return left ? left : ray_error("type", "asof-join: left evaluation failed"); }
    ray_t* right = ray_eval(args[2]);
    if (!right || RAY_IS_ERR(right)) { ray_release(left); ray_release(keys_vec); return right ? right : ray_error("type", "asof-join: right evaluation failed"); }

    ray_t* r = ray_asof_join_core(keys_vec, left, right);
    ray_release(left); ray_release(right); ray_release(keys_vec);
    return r;
}


/* ══════════════════════════════════════════
 * delete — remove the rows a predicate matches
 * ══════════════════════════════════════════ */


/* Move the survivors down over the deleted rows.  Every column type stores a
 * fixed-width element — a RAY_STR cell is a 16-byte descriptor and the pool it
 * points into does not move with the row — so this is a memmove of the runs
 * between deletions, not a per-cell copy.  A boxed column releases the cells
 * it drops first.  Row order is preserved, so RAY_ATTR_SORTED stays true;
 * an attached accelerator index does not, and is dropped. */
static ray_t* delete_compact_cols(ray_t** slots, int64_t ncols, int64_t nrows,
                                  const int64_t* rows, int64_t k) {
    for (int64_t c = 0; c < ncols; c++) {
        if (slots[c]->attrs & RAY_ATTR_HAS_INDEX) {
            ray_t* drop_r = ray_index_drop(&slots[c]);
            if (RAY_IS_ERR(drop_r)) return drop_r;
        }
        ray_t* col = slots[c];
        size_t esz = (col->type == RAY_LIST) ? sizeof(ray_t*)
                                             : ray_sym_elem_size(col->type, col->attrs);
        uint8_t* d = (uint8_t*)ray_data(col);
        if (col->type == RAY_LIST) {
            ray_t** cells = (ray_t**)d;
            for (int64_t i = 0; i < k; i++)
                if (cells[rows[i]]) ray_release(cells[rows[i]]);
        }
        int64_t dst = rows[0];
        for (int64_t i = 0; i < k; i++) {
            int64_t src = rows[i] + 1;
            int64_t end = (i + 1 < k) ? rows[i + 1] : nrows;
            int64_t cnt = end - src;
            if (cnt > 0) {
                memmove(d + (size_t)dst * esz, d + (size_t)src * esz, (size_t)cnt * esz);
                dst += cnt;
            }
        }
        col->len = nrows - k;
    }
    return NULL;
}

/* Compaction moves a surviving row from r to r - (deletions before r) and
 * changes no key, so every entry keeps its bucket and only the row number it
 * stores has to shift: one pass over the slot array, no hashing and no key
 * reads.  Entries naming a deleted row become tombstones.
 *
 * Patching costs that one pass whatever k is, while rebuilding costs a hash
 * per survivor — so a large deletion is handed to the next upsert instead,
 * which also reclaims the tombstones a patch would leave behind. */
static void ukey_patch_after_delete(ray_t* tbl, const int64_t* rows, int64_t k,
                                    int64_t nrows0) {
    ray_index_t* ix = ray_table_ukey_get(tbl);
    if (!ix) return;
    int64_t left = nrows0 - k;
    if (ix->u.ukey.nrows != nrows0 || k * 2 >= nrows0 ||
        (uint64_t)(nrows0 + ix->u.ukey.n_tomb) * 2 > ix->u.ukey.mask + 1) {
        ray_table_ukey_drop(tbl);
        return;
    }
    /* Old row -> new row, or -1 for a deleted one, built in one pass.  Asking
     * a binary search per slot instead cost log(k) on every slot of the
     * capacity and dominated the patch; this is O(1) per slot. */
    ray_t* nr_hdr = NULL;
    int64_t* newrow = (int64_t*)scratch_alloc(&nr_hdr, (size_t)nrows0 * sizeof(int64_t));
    if (!newrow) { ray_table_ukey_drop(tbl); return; }
    for (int64_t r = 0, next = 0, ri = 0; r < nrows0; r++) {
        if (ri < k && rows[ri] == r) { newrow[r] = -1; ri++; }
        else newrow[r] = next++;
    }

    int64_t* slot = (int64_t*)ray_data(ix->u.ukey.slots);
    uint64_t cap = ix->u.ukey.mask + 1;
    int64_t tomb = 0;
    for (uint64_t s = 0; s < cap; s++) {
        int64_t e = slot[s];
        if (e == UKEY_SLOT_EMPTY || e == UKEY_SLOT_TOMB) continue;
        int64_t nr = newrow[UKEY_ROW(e)];
        if (nr < 0) { slot[s] = UKEY_SLOT_TOMB; tomb++; }
        else slot[s] = UKEY_PACK(UKEY_H32(e), nr);
    }
    scratch_free(nr_hdr);
    ix->u.ukey.n_tomb += tomb;
    ix->u.ukey.nrows = left;
}

ray_t* ray_delete_fn(ray_t** args, int64_t n) {
    return ray_delete(args, n);
}

/* (delete {from: t where: <pred>}) — remove the rows the predicate matches.
 *
 * `from: 't` (a quoted symbol) removes them from the named table in place,
 * keeping its upsert key map; `from: t` returns a new table without them.
 * Both forms evaluate the predicate once and compact the same matched-row
 * list with the same code, so they cannot disagree about which rows go.  The
 * functional form is deliberately NOT `select` over a negated predicate: that
 * would be a second expression to keep in step with this one. */
ray_t* ray_delete(ray_t** args, int64_t n) {
    if (n < 1) return ray_error("arity", "delete: expects a query dict, got %lld args", (long long)n);
    ray_t* dict = args[0];
    if (!dict || dict->type != RAY_DICT)
        return ray_error("type", "delete: query must be a dict, got %s", dict ? ray_type_name(dict->type) : "null");

    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr) return ray_error("domain", "delete: missing `from:` clause");
    ray_t* where_expr = dict_get(dict, "where");
    /* A delete with no predicate would empty the table, which is too easy to
     * reach by leaving a clause out.  Say so instead. */
    if (!where_expr) return ray_error("domain", "delete: missing `where:` clause");
    if (dict_get(dict, "by")) return ray_error("domain", "delete: `by:` is not supported");

    int64_t inplace_sym = -1;
    ray_t* tbl = ray_eval(from_expr);
    if (RAY_IS_ERR(tbl)) return tbl;
    if (tbl->type == -RAY_SYM) {
        inplace_sym = tbl->i64;
        ray_release(tbl);
        tbl = ray_env_get(inplace_sym);
        if (!tbl || RAY_IS_ERR(tbl)) return ray_error("domain", "delete: `from:` symbol is unbound");
        ray_retain(tbl);
    }
    if (tbl->type != RAY_TABLE) {
        int8_t t = tbl->type; ray_release(tbl);
        return ray_error("type", "delete: `from:` must be a table, got %s", ray_type_name(t));
    }

    /* A parted table's columns carry the RAY_PARTED_BASE wrapper and its
     * partition key is RAY_MAPCOMMON; neither describes a flat element the
     * compaction could move.  Flatten once, as ray_update does — the in-place
     * path declines a parted table anyway (upsert_col_ok rejects the
     * wrapper), so this only feeds the copying form. */
    if (table_has_parted_columns(tbl)) {
        ray_t* flat = ray_table_new(ray_table_ncols(tbl));
        if (!flat || RAY_IS_ERR(flat)) { ray_release(tbl); return flat ? flat : ray_error("oom", NULL); }
        int64_t nc = ray_table_ncols(tbl);
        for (int64_t c = 0; c < nc; c++) {
            ray_t* fc = query_materialize_parted_col(ray_table_get_col_idx(tbl, c));
            if (!fc || RAY_IS_ERR(fc)) { ray_release(flat); ray_release(tbl); return fc ? fc : ray_error("oom", NULL); }
            flat = ray_table_add_col(flat, ray_table_col_name(tbl, c), fc);
            ray_release(fc);
            if (!flat || RAY_IS_ERR(flat)) { ray_release(tbl); return flat ? flat : ray_error("oom", NULL); }
        }
        ray_release(tbl);
        tbl = flat;
    }

    int64_t ncols = ray_table_ncols(tbl);
    int64_t nrows0 = ray_table_nrows(tbl);
    if (ncols <= 0) { ray_release(tbl); return ray_error("domain", "delete: table has no columns"); }

    /* A slice column is a header-only view: ray_cow copies the header and
     * leaves ray_data resolving into the parent's storage, so compacting the
     * copy would move the PARENT's elements.  The in-place path declines one
     * through upsert_col_ok; the copying form has no such backstop, so refuse
     * rather than corrupt a buffer somebody else owns. */
    for (int64_t c = 0; c < ncols; c++)
        if (ray_table_get_col_idx(tbl, c)->attrs & RAY_ATTR_SLICE) {
            ray_release(tbl);
            return ray_error("domain", "delete: column %lld is a slice view", (long long)c);
        }

    /* Decide in-place BEFORE evaluating the predicate.  The predicate machinery
     * binds the table's columns into a query scope (and builds a graph over
     * the table), so reference counts are only quiet enough to read here. */
    bool inplace = inplace_sym >= 0 && tbl->rc == 2 && tbl->mmod == 0 &&
                   !(tbl->attrs & (RAY_ATTR_SLICE | RAY_ATTR_ARENA));
    ray_t* live_cols = NULL;
    if (inplace) {
        live_cols = ((ray_t**)ray_data(tbl))[1];
        if (!live_cols || live_cols->rc != 1 || live_cols->len != ncols) inplace = false;
    }
    for (int64_t c = 0; inplace && c < ncols; c++) {
        ray_t* col = ((ray_t**)ray_data(live_cols))[c];
        if (col->rc != 1 || !upsert_col_ok(col)) inplace = false;
    }

    /* Matched rows, ascending — the same two paths the in-place where-update
     * uses, so an indexed equality predicate is answered by the index here
     * too. */
    ray_t* rows_hdr = NULL; int64_t* rows = NULL; int64_t k = 0;
    ray_t* err = NULL;
    if (!update_where_index_rows(tbl, where_expr, &rows_hdr, &rows, &k)) {
        ray_t* mask = update_where_mask_vec(tbl, where_expr);
        if (RAY_IS_ERR(mask)) { ray_release(tbl); return mask; }
        int64_t mlen = mask->len;
        const uint8_t* m = (const uint8_t*)ray_data(mask);
        int64_t cnt = 0;
        for (int64_t r = 0; r < mlen; r++) cnt += (m[r] != 0);
        rows = (int64_t*)scratch_alloc(&rows_hdr, (size_t)(cnt > 0 ? cnt : 1) * sizeof(int64_t));
        if (!rows) err = ray_error("oom", NULL);
        else for (int64_t r = 0; r < mlen; r++) if (m[r]) rows[k++] = r;
        ray_release(mask);
        if (err) { ray_release(tbl); return err; }
    }

    /* Nothing matched: the in-place form leaves the binding and its map
     * untouched, the functional form answers with the table as it stands. */
    if (k == 0) {
        if (rows_hdr) scratch_free(rows_hdr);
        if (inplace_sym >= 0) { ray_release(tbl); return ray_sym(inplace_sym); }
        return tbl;
    }

    if (inplace) {
        ray_t** slots = (ray_t**)ray_data(live_cols);
        ray_t* cerr = delete_compact_cols(slots, ncols, nrows0, rows, k);
        if (cerr) { scratch_free(rows_hdr); ray_release(tbl); return cerr; }
        ukey_patch_after_delete(tbl, rows, k, nrows0);
        scratch_free(rows_hdr);
        ray_release(tbl);
        return ray_sym(inplace_sym);
    }

    /* Functional form (and the fallback when a gate misses): private copies of
     * the columns, then the same compaction.  A fresh table carries no key
     * map, which is correct — it describes rows this one does not have. */
    ray_t* work = ray_table_new(ncols);
    if (!work || RAY_IS_ERR(work)) {
        scratch_free(rows_hdr); ray_release(tbl);
        return work ? work : ray_error("oom", NULL);
    }
    for (int64_t c = 0; c < ncols && !err; c++) {
        ray_t* orig = ray_table_get_col_idx(tbl, c);
        ray_retain(orig);
        ray_t* cp = ray_cow(orig);
        if (!cp || RAY_IS_ERR(cp)) { ray_release(orig); err = cp ? cp : ray_error("oom", NULL); break; }
        work = ray_table_add_col(work, ray_table_col_name(tbl, c), cp);
        ray_release(cp);
        if (!work || RAY_IS_ERR(work)) { err = work ? work : ray_error("oom", NULL); work = NULL; }
    }
    if (!err) {
        ray_t** wslots = (ray_t**)ray_data(((ray_t**)ray_data(work))[1]);
        err = delete_compact_cols(wslots, ncols, nrows0, rows, k);
    }
    scratch_free(rows_hdr);
    ray_release(tbl);
    if (err) { if (work) ray_release(work); return err; }
    /* `from: 't` promises the binding is amended and the symbol comes back,
     * whether or not the in-place gate held — every sibling verb rebinds on
     * this path.  Returning the table instead left the binding with all its
     * rows and handed the caller a value it did not ask for. */
    if (inplace_sym >= 0) {
        ray_env_set(inplace_sym, work);
        ray_release(work);
        return ray_sym(inplace_sym);
    }
    return work;
}