raysense 0.6.0

Architectural X-ray for your codebase. Live, local, agent-ready.
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
/*
 *   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 "lang/eval.h"
#include "lang/env.h"
#include "ops/ops.h"
#include "ops/internal.h"
#include "ops/hash.h"
#include "ops/temporal.h"
#include "table/sym.h"
#include "table/dict.h"
#include "mem/sys.h"

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

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

/* Helper: look up a key in a select-clause dict (RAY_DICT).
 * Returns the value expression (unevaluated), or NULL if not found. */
static ray_t* dict_get(ray_t* dict, const char* key) {
    if (!dict || dict->type != RAY_DICT) return NULL;
    int64_t key_id = ray_sym_intern(key, strlen(key));
    return ray_dict_probe_sym_borrowed(dict, key_id);
}

/* 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.
 *
 * Caller passes stack-local buffers sized at DICT_VIEW_MAX.  If the dict
 * has more pairs than fits, sets `*out_n = -1` to flag overflow — every
 * call site checks this and returns a "domain" error rather than letting
 * the writes spill past the buffers.  The previous version of this helper
 * had no such guard and silently corrupted the stack on user-controlled
 * dicts with > 64 pairs.
 *
 * `key_atoms` must hold at least DICT_VIEW_MAX entries; `out_elems` at
 * least 2 * DICT_VIEW_MAX.  Keys are synthesized as -RAY_SYM atoms in
 * `key_atoms`; values are borrowed from the dict's vals list. */
#define DICT_VIEW_MAX 256
static void dict_pair_view(ray_t* d, ray_t* key_atoms, ray_t** out_elems, int64_t* out_n) {
    *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;
    if (n > DICT_VIEW_MAX) { *out_n = -1; return; }
    void* kbase = ray_data(keys);
    ray_t** vptrs = (ray_t**)ray_data(vals);
    for (int64_t i = 0; i < n; i++) {
        memset(&key_atoms[i], 0, sizeof(ray_t));
        key_atoms[i].type = -RAY_SYM;
        key_atoms[i].i64  = ray_read_sym(kbase, i, RAY_SYM, keys->attrs);
        out_elems[i*2]   = &key_atoms[i];
        out_elems[i*2+1] = vptrs[i];
    }
    *out_n = 2 * n;
}

#define DICT_VIEW_DECL(name)                            \
    ray_t   name##_keybuf[DICT_VIEW_MAX];               \
    ray_t*  name[DICT_VIEW_MAX * 2];                    \
    int64_t name##_n
#define DICT_VIEW_OPEN(d, name)                          \
    dict_pair_view((d), name##_keybuf, name, &name##_n)
/* Returns true if the open exceeded DICT_VIEW_MAX — caller should
 * `ray_release(tbl); return ray_error("domain", "clause too big");`. */
#define DICT_VIEW_OVERFLOW(name) ((name##_n) < 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", 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) {
                case RAY_SYM: k = ray_sym(ray_read_sym(base, i, RAY_SYM, keys->attrs)); 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;
}

/* 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 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;
    } 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;
    }
    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, "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;
    } else if (len == 4) {
        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;
    } else if (len == 5) {
        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, "strlen", 6) == 0) return ray_strlen;
    }
    /* NOTE: no DAG wiring for nil?/isnull yet.  The eval-level
     * builtin `nil?` (src/lang/eval.c:2029) is atom-only — it
     * returns false when applied to a column vec.  OP_ISNULL in
     * the DAG is per-element.  Wiring `nil?` here would diverge
     * from the eval fallback.  A proper pass should first add an
     * element-wise null-check builtin at eval level, then map it
     * here. */
    return NULL;
}

/* 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;
    if (len == 3 && memcmp(name, "var",   3) == 0) return OP_VAR;
    if (len == 4 && memcmp(name, "prod",  4) == 0) return OP_PROD;
    if (len == 4 && memcmp(name, "last",  4) == 0) return OP_LAST;
    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;
    return 0;
}

/* 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. */
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) {
    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;

    /* 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) {
        ray_op_t* sort_keys[16];
        uint8_t   sort_descs[16];
        uint8_t   n_sort = 0;
        for (int64_t i = 0; i + 1 < dict_n && n_sort < 16; 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 && n_sort < 16; c++) {
                    int64_t sid = ray_read_sym(ray_data(val), c, val->type, val->attrs);
                    ray_t* s = ray_sym_str(sid);
                    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);
    }

    /* 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", NULL);
        }
        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 {
            ray_release(tv); ray_graph_free(g); ray_release(result);
            return ray_error("domain", NULL);
        }
    }

    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_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;
}

/* --------------------------------------------------------------------------
 * 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 */
}

/* 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 */
static 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->nullmap 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 & RAY_ATTR_NAME)) {
        ray_op_t* bound = cexpr_env_lookup(g, expr->i64);
        if (bound) return bound;
        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 — let ray_scan produce a column-not-found
         * error at exec time, matching prior behavior. */
        return ray_scan(g, ray_str_ptr(s));
    }

    /* Symbol literal (no RAY_ATTR_NAME) → const atom node. */
    if (expr->type == -RAY_SYM)
        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 & RAY_ATTR_NAME))
        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 & RAY_ATTR_NAME))
        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 & RAY_ATTR_NAME) &&
            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++;
                        }
                        ray_op_t* result = compile_expr_dag(g, body);
                        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;

        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;
            col = &g->nodes[col_id];
            /* xbar(x, b) = x - (x % b)  (stays in integer domain) */
            ray_op_t* m = ray_mod(g, col, bucket);
            if (!m) return NULL;
            col = &g->nodes[col_id];
            return ray_sub(g, col, m);
        }

        /* (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;
            ray_op_t* t = compile_expr_dag(g, elems[2]);
            if (!t) return NULL;
            uint32_t t_id = t->id;
            ray_op_t* e = compile_expr_dag(g, elems[3]);
            if (!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) {
            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);
        }

        /* 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;
                    ray_op_t* c = compile_expr_dag(g, cpair[1]);
                    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;
                    ray_op_t* body = compile_expr_dag(g, cpair[1]);
                    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. */
        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;
                    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) {
            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];
                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]);
                return arg ? uctor(g, arg) : NULL;
            }
            /* 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;
                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_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);
                    default: return NULL;
                }
            }
        }
    }

    return NULL;
}

/* Walk an expression tree and bind any name-symbols that match table columns
 * into the current local scope. Recurses into list sub-expressions. */
static void expr_bind_table_names(ray_t* expr, ray_t* tbl) {
    if (!expr) return;
    if (expr->type == -RAY_SYM && (expr->attrs & RAY_ATTR_NAME)) {
        /* Plain column reference — bind the column into local scope. */
        ray_t* col = ray_table_get_col(tbl, expr->i64);
        if (col) { ray_env_set_local(expr->i64, col); return; }
        /* Dotted reference (e.g. `Timestamp.ss`) — the whole dotted
         * sym isn't a column name, but its HEAD segment might be.
         * Bind the head so ray_env_resolve's dotted walk can reach
         * it when ray_eval fires on this expression.  Non-column
         * heads (globals, locals) fall through to env_resolve's
         * normal scope-chain lookup. */
        if (ray_sym_is_dotted(expr->i64)) {
            const int64_t* segs;
            int nsegs = ray_sym_segs(expr->i64, &segs);
            if (nsegs >= 1) {
                ray_t* head_col = ray_table_get_col(tbl, segs[0]);
                if (head_col) ray_env_set_local(segs[0], head_col);
            }
        }
        return;
    }
    if (expr->type == RAY_LIST) {
        ray_t** elems = (ray_t**)ray_data(expr);
        int64_t n = ray_len(expr);
        for (int64_t i = 0; i < n; i++)
            expr_bind_table_names(elems[i], tbl);
    }
}

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 & RAY_ATTR_NAME)) {
        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 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;
}

/* 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 & RAY_ATTR_NAME)) {
        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;
}

/* 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) {
    ray_t* slice = ray_at_fn(col, idx_list);
    if (!slice || RAY_IS_ERR(slice)) {
        return slice ? slice : ray_error("oom", NULL);
    }
    ray_env_set_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) {
    int64_t col_syms[16];
    int n_cols = collect_col_refs(expr, tbl, col_syms, 16, 0);
    ray_t* cols[16];
    for (int i = 0; i < n_cols; i++)
        cols[i] = ray_table_get_col(tbl, col_syms[i]);

    if (ray_env_push_scope() != RAY_OK) return ray_error("oom", NULL);

    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 */

    for (int64_t gi = 0; gi < n_groups; gi++) {
        ray_t* idx_list = feeder(gi, fstate);
        if (!idx_list) {
            ray_env_pop_scope();
            if (result) ray_release(result);
            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) {
                ray_env_pop_scope();
                if (result) ray_release(result);
                return err;
            }
        }
        ray_t* cell = ray_eval(expr);
        if (!cell || RAY_IS_ERR(cell)) {
            ray_env_pop_scope();
            if (result) ray_release(result);
            return cell ? cell : ray_error("domain", NULL);
        }

        if (gi == 0) {
            int8_t t = cell->type;
            int collapsable = (t < 0 && t != -RAY_SYM && t != -RAY_STR && t != -RAY_GUID);
            if (collapsable) {
                int8_t vt = (int8_t)(-t);
                result = ray_vec_new(vt, n_groups);
                if (!result || RAY_IS_ERR(result)) {
                    ray_env_pop_scope(); ray_release(cell);
                    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) {
                    ray_env_pop_scope(); ray_release(cell);
                    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) {
            if (cell->type == typed_t && store_typed_elem(result, gi, cell) == 0) {
                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)) {
                    ray_env_pop_scope(); ray_release(cell);
                    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;
        }
    }

    ray_env_pop_scope();
    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;
}

/* 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. */
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", 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 & RAY_ATTR_NAME)) {
        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_scope() != RAY_OK) return ray_error("oom", NULL);
        expr_bind_table_names(col_expr, tbl);
        src = ray_eval(col_expr);
        ray_env_pop_scope();
        if (!src || RAY_IS_ERR(src)) return src ? src : ray_error("domain", NULL);
    }

    /* 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)) 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); ray_release(src);
    if (!agg_vec) {
        /* No groups produced a value (all empty?) — return an empty typed
         * vec sized n_groups; default to I64 for lack of a better guess. */
        agg_vec = ray_vec_new(RAY_I64, n_groups);
        if (agg_vec && !RAY_IS_ERR(agg_vec)) agg_vec->len = n_groups;
    }
    return agg_vec;
}

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

/* (select {from: t [where: pred] [by: key] [col: expr ...]})
 * Special form — receives unevaluated dict arg. */
ray_t* ray_select_fn(ray_t** args, int64_t n) {
    if (n < 1) return ray_error("domain", NULL);
    ray_t* dict = args[0];
    if (!dict || dict->type != RAY_DICT)
        return ray_error("type", NULL);

    /* Evaluate 'from:' to get the source table */
    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr) return ray_error("domain", NULL);
    ray_t* tbl = ray_eval(from_expr);
    if (RAY_IS_ERR(tbl)) return tbl;
    if (tbl->type != RAY_TABLE) { ray_release(tbl); return ray_error("type", NULL); }

    ray_t* where_expr = dict_get(dict, "where");
    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);
        return ray_error("domain", "select clause has too many keys");
    }
    int64_t dict_n = dv_n;
    ray_t** dict_elems = dv;
    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);

    /* 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);
            return ray_error("domain",
                "select: `nearest` cannot be combined with asc/desc");
        }
        if (by_expr) {
            ray_release(tbl);
            return ray_error("domain",
                "select: `nearest` cannot be combined with `by`");
        }
    }

    /* 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)
        return tbl;

    /* 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;
    DICT_VIEW_DECL(byv);
    if (by_expr && by_expr->type == RAY_DICT) {
        DICT_VIEW_OPEN(by_expr, byv);
        if (DICT_VIEW_OVERFLOW(byv)) {
            ray_release(tbl);
            return ray_error("domain", "by-dict has too many keys");
        }
        int64_t dlen = byv_n;
        int64_t nk = dlen / 2;
        if (nk == 0 || nk > 16) {
            ray_release(tbl);
            return ray_error("domain", "by-dict must have 1..16 keys");
        }
        ray_t** d_elems = byv;

        ray_env_push_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_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);
            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 = ray_eval(v);
            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_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);
            return fail_err;
        }
        by_expr = by_sym_vec_owned;
    }

    /* Build DAG */
    ray_graph_t* g = ray_graph_new(tbl);
    if (!g) {
        if (by_sym_vec_owned) ray_release(by_sym_vec_owned);
        ray_release(tbl); 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) */
    int64_t nonagg_names[16];
    ray_t*  nonagg_exprs[16];
    uint8_t n_nonaggs = 0;
    int synth_count_col = 0;  /* 1 if we synthesized OP_COUNT for group boundaries */

    /* Apply WHERE filter */
    if (where_expr) {
        ray_op_t* pred = compile_expr_dag(g, where_expr);
        if (!pred) {
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain",
                "WHERE predicate not supported by DAG compiler — "
                "most common causes: arity mismatch "
                "(e.g. `(in v)` instead of `(in col v)`), "
                "unknown function name, unsupported special form, "
                "or a sub-expression the compiler can't lower");
        }
        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);
            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);
            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);
                return tv ? tv : ray_error("domain", NULL);
            }
            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);
                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);
                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);
            return qvec ? qvec : ray_error("domain", NULL);
        }
        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);
            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);
            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_sys_alloc((size_t)dim * sizeof(float));
        if (!nearest_query_owned) {
            ray_release(qvec);
            ray_graph_free(g); ray_release(tbl);
            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_sys_free(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                return hobj ? hobj : ray_error("domain", NULL);
            }
            if (hobj->type != -RAY_I64 || !(hobj->attrs & RAY_ATTR_HNSW)) {
                ray_release(hobj); ray_sys_free(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                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_sys_free(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                return ray_error("type",
                    "nearest (ann): HNSW handle has been freed");
            }
            if (idx->dim != dim) {
                ray_release(hobj); ray_sys_free(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                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_sys_free(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    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_sys_free(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    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_sys_free(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                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_sys_free(nearest_query_owned);
                        ray_graph_free(g); ray_release(tbl);
                        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_sys_free(nearest_query_owned);
            ray_graph_free(g); ray_release(tbl);
            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_sys_free(nearest_query_owned);
            ray_graph_free(g); ray_release(tbl);
            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_sys_free(nearest_query_owned);
                ray_graph_free(g); ray_release(tbl);
                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_sys_alloc(
                    (size_t)src_ncols * sizeof(ray_op_t*));
                if (!col_ops) {
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    ray_sys_free(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    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_sys_free(col_ops);
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    ray_sys_free(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    return ray_error("oom", NULL);
                }
                root = ray_select(g, root, col_ops, (uint8_t)nc);
                ray_sys_free(col_ops);
                if (!root) {
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    ray_sys_free(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    return ray_error("oom", NULL);
                }
            }
        }
    }

    /* 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 & RAY_ATTR_NAME))
            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;
        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_agg_expr(dict_elems[i + 1])) { any_nonagg = 1; break; }
            }
        }

        /* Decide routing.  LIST/STR always fall to the eval-level
         * grouping because the DAG HT path can't pack them into
         * 8-byte key slots.  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 || kct == RAY_STR)
                    use_eval_group = 1;
                else if (kct == RAY_GUID && (any_nonagg || n_out == 0))
                    /* RAY_GUID routes to eval-level ray_group_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;
            }
        }
        /* 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 scatter block
         * only handles single scalar-key by-clauses — for multi-key
         * or computed-key groupings, fall back to eval-level so the
         * non-agg scatter has a well-defined row→group mapping. */
        if (!use_eval_group && any_nonagg) {
            /* Fast path requires a single scalar-named key column.
             * Multi-key and computed-key by-clauses with non-agg
             * expressions are not yet supported. */
            int single_scalar_key = 0;
            if (by_expr->type == -RAY_SYM && (by_expr->attrs & RAY_ATTR_NAME)) {
                single_scalar_key = 1;
            } else if (by_expr->type == RAY_SYM && ray_len(by_expr) == 1) {
                single_scalar_key = 1;
            }
            if (!single_scalar_key) {
                ray_graph_free(g); ray_release(tbl);
                return ray_error("nyi", "non-agg expression with multi-key or computed group key");
            }
        }
        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);
                ray_t* fres = ray_execute(g, root);
                ray_graph_free(g); g = NULL;
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); return fres ? fres : ray_error("domain", NULL); }
                if (ray_is_lazy(fres)) fres = ray_lazy_materialize(fres);
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); return fres ? fres : ray_error("domain", NULL); }
                eval_tbl = fres;
            } else {
                ray_graph_free(g); g = NULL;
            }
            /* eval_group path supports only simple scalar / [col] by-forms;
             * multi-key and computed keys shouldn't land here. */
            if (by_key_sym < 0) {
                if (eval_tbl != tbl) ray_release(eval_tbl);
                ray_release(tbl);
                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_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); 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); 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);
                            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); 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)) {
                            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 = (sc->attrs & RAY_ATTR_HAS_NULLS) != 0;
                            for (int64_t gi = 0; gi < ngroups; gi++) {
                                memcpy(db + (size_t)gi * esz,
                                       sb + (size_t)fi[gi] * esz, esz);
                                if (src_has_nulls && ray_vec_is_null(sc, fi[gi]))
                                    ray_vec_set_null(dst, gi, true);
                            }
                        }
                    } 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 = (sc->attrs & RAY_ATTR_HAS_NULLS) != 0;
                            for (int64_t gi = 0; gi < ngroups; gi++) {
                                memcpy(db + (size_t)gi * esz,
                                       sb + (size_t)fi[gi] * esz, esz);
                                if (src_has_nulls && ray_vec_is_null(sc, fi[gi]))
                                    ray_vec_set_null(dst, gi, true);
                            }
                        }
                    }

                    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);
                    return first_err;
                }
                return apply_sort_take(res, dict_elems, dict_n, asc_id, desc_id, take_id);
            }

            ray_t* groups_dict = ray_group_fn(key_col);
            if (RAY_IS_ERR(groups_dict)) { if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); 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); 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_vec_new(sc->type, 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);
                return empty;
            }

            /* Collect aggregation results */
            int n_agg_out = 0;
            int64_t agg_names[16];
            ray_t* agg_results[16];
            for (int64_t i = 0; i + 1 < dict_n && n_agg_out < 16; 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_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 & RAY_ATTR_NAME)) {
                        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);
                        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]); }
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); return src_col_val;
                        }
                    }

                    /* For each group, compute aggregation */
                    ray_t* agg_vec = NULL;
                    ray_t** grp_items = (ray_t**)ray_data(groups);
                    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)) 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);
                    }
                    ray_release(src_col_val);
                    agg_names[n_agg_out] = kid;
                    agg_results[n_agg_out] = agg_vec;
                    n_agg_out++;
                } else {
                    /* 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_scope() != RAY_OK) {
                        for (int ai = 0; ai < n_agg_out; ai++) { if (agg_results[ai]) ray_release(agg_results[ai]); }
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        return ray_error("oom", NULL);
                    }
                    expr_bind_table_names(val_expr_item, eval_tbl);
                    ray_t* full_val = ray_eval(val_expr_item);
                    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]); }
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); 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]); }
                        ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                        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]); }
                            ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                            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)) { ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl); 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 {
                    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]); }
                    ray_release(result); ray_release(groups); if (eval_tbl != tbl) ray_release(eval_tbl); ray_release(tbl);
                    return key_vec ? key_vec : ray_error("oom", NULL);
                }
                result = ray_table_add_col(result, by_key_sym, 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]);
            }

            /* 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); 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 = (sc->attrs & RAY_ATTR_HAS_NULLS) != 0;
                        for (int64_t gi = 0; gi < n_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(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);
            return apply_sort_take(result, dict_elems, dict_n, asc_id, desc_id, take_id);
        }

        /* Pre-scan: any non-aggregation expressions?  If so and there's a
         * WHERE, we must materialize the filtered table first so the
         * post-DAG scatter evaluates on filtered data (matching agg semantics). */
        int has_nonagg = 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_agg_expr(dict_elems[i + 1])) { has_nonagg = 1; break; }
        }

        /* The post-DAG scatter needs a flat single-segment table: it
         * reads key columns directly and runs ray_eval over the whole
         * input.  Detect parted tables up front — if the source is
         * parted and there's no WHERE to materialize it, return nyi. */
        int table_is_parted = 0;
        if (has_nonagg) {
            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; }
            }
            if (table_is_parted && !where_expr) {
                ray_graph_free(g); ray_release(tbl);
                return ray_error("nyi", "non-agg expression on parted table without WHERE");
            }
        }

        /* 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) {
            bool can_fuse = !has_nonagg && !table_is_parted;
            if (can_fuse) {
                root = ray_optimize(g, root);
                /* 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);
                    return fres ? fres : ray_error("domain", NULL);
                }
                /* 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);
            } 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); return fres ? fres : ray_error("domain", NULL); }
                if (ray_is_lazy(fres)) fres = ray_lazy_materialize(fres);
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); return fres ? fres : ray_error("domain", NULL); }
                ray_release(tbl);
                tbl = fres;
                g = ray_graph_new(tbl);
                if (!g) { ray_release(tbl); return ray_error("oom", NULL); }
                root = ray_const_table(g, tbl);
            }
        }

        /* Compile group key(s) */
        ray_op_t* key_ops[16];
        uint8_t n_keys = 0;

        if (by_expr->type == RAY_SYM) {
            /* Multiple keys as SYM vector: [col1 col2 ...] */
            int64_t nk = ray_len(by_expr);
            int64_t* sym_ids = (int64_t*)ray_data(by_expr);
            for (int64_t i = 0; i < nk && n_keys < 16; i++) {
                ray_t* name_str = ray_sym_str(sym_ids[i]);
                if (!name_str) { ray_graph_free(g); ray_release(tbl); return ray_error("domain", NULL); }
                key_ops[n_keys] = ray_scan(g, ray_str_ptr(name_str));
                if (!key_ops[n_keys]) { ray_graph_free(g); ray_release(tbl); return ray_error("domain", NULL); }
                n_keys++;
            }
        } else {
            /* Single key expression */
            key_ops[0] = compile_expr_dag(g, by_expr);
            if (!key_ops[0]) { ray_graph_free(g); ray_release(tbl); return ray_error("domain", NULL); }
            n_keys = 1;
        }

        /* Collect aggregation expressions from output columns.
         * Non-agg expressions are tracked separately for post-DAG scatter. */
        uint16_t agg_ops[16];
        ray_op_t* agg_ins[16];
        uint8_t 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 == 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_agg_expr(val_expr) && n_aggs < 16) {
                ray_t** agg_elems = (ray_t**)ray_data(val_expr);
                agg_ops[n_aggs] = resolve_agg_opcode(agg_elems[0]->i64);
                /* Compile the aggregation input (the column reference) */
                agg_ins[n_aggs] = compile_expr_dag(g, agg_elems[1]);
                if (!agg_ins[n_aggs]) { ray_graph_free(g); ray_release(tbl); return ray_error("domain", NULL); }
                n_aggs++;
            } else if (!is_agg_expr(val_expr) && n_nonaggs < 16) {
                nonagg_names[n_nonaggs] = kid;
                nonagg_exprs[n_nonaggs] = val_expr;
                n_nonaggs++;
            }
        }

        if (n_aggs > 0 || n_nonaggs > 0) {
            if (n_aggs > 0) {
                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); return fres ? fres : ray_error("domain", NULL); }
                if (ray_is_lazy(fres)) fres = ray_lazy_materialize(fres);
                if (!fres || RAY_IS_ERR(fres)) { ray_release(tbl); return fres ? fres : ray_error("domain", NULL); }
                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); return ray_error("oom", NULL); }
                n_keys = 0;
                if (by_expr->type == RAY_SYM) {
                    int64_t nk = ray_len(by_expr);
                    int64_t* sym_ids = (int64_t*)ray_data(by_expr);
                    for (int64_t i = 0; i < nk && n_keys < 16; i++) {
                        ray_t* ns = ray_sym_str(sym_ids[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); 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 & RAY_ATTR_NAME)
                && !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 = (sc->type == RAY_STR)
                                            ? ray_vec_new(RAY_STR, 0)
                                            : ray_vec_new(sc->type, 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 & RAY_ATTR_NAME)) {
                            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 & RAY_ATTR_NAME)) {
                                    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_scope();
                        for (int64_t c = 0; c < nc0; c++) {
                            ray_env_set_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);
                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_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_local(cn, cv);
                }
                ray_t* 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);
                    return computed_key ? computed_key : ray_error("domain", NULL);
                }
                ray_t* groups2_dict = ray_group_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);
                    return groups2_dict ? groups2_dict : ray_error("domain", NULL);
                }
                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);
                    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); 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);
                        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 & RAY_ATTR_NAME)) {
                    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 & RAY_ATTR_NAME)) {
                            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);
                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); 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.) or with +0.0 for F64 (ray_hash_f64 normalises
             * -0.0 to +0.0, and F64's null bit pattern on this platform
             * is the -0.0 pattern).  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);
                    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 = (grp_key_col->attrs & RAY_ATTR_HAS_NULLS) != 0;
                for (int64_t gi = 0; gi < n_groups; gi++) {
                    if (kt == RAY_F64)
                        memcpy(&gk_vals[gi], &((double*)ray_data(grp_key_col))[gi], 8);
                    else
                        gk_vals[gi] = read_col_i64(ray_data(grp_key_col), gi, kt, grp_key_col->attrs);
                    if (gk_has_nulls && ray_vec_is_null(grp_key_col, gi))
                        gk_null[gi] = 1;
                }
            }
            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); 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);
                    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 (F64 null has the -0.0 bit pattern, which
                 * ray_hash_f64 normalises to +0.0; integer-flavoured
                 * nulls are stored as value=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
                    && (orig_key_col->attrs & RAY_ATTR_HAS_NULLS) != 0;
                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) memcpy(&ov, &((double*)ray_data(orig_key_col))[r], 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); 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); 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); 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); 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); 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); 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); 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); 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); return result; }
            }

            if (fi_heap_hdr) ray_free(fi_heap_hdr);
            if (filtered_tbl != tbl) ray_release(filtered_tbl);
            ray_release(tbl);
            return apply_sort_take(result, dict_elems, dict_n, asc_id, desc_id, take_id);
        }
    } else if (n_out > 0) {
        /* Projection only (no group by) — select specific columns */
        ray_op_t* col_ops[16];
        uint8_t nc = 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 (nc < 16) {
                col_ops[nc] = compile_expr_dag(g, dict_elems[i + 1]);
                if (!col_ops[nc]) {
                    /* Nearest-path resources must be freed here too — the
                     * rerank handle/query buffers are held across the whole
                     * ray_select_fn body, not just inside the nearest block. */
                    if (nearest_handle_owned) ray_release(nearest_handle_owned);
                    if (nearest_query_owned)  ray_sys_free(nearest_query_owned);
                    ray_graph_free(g); ray_release(tbl);
                    return ray_error("domain", NULL);
                }
                nc++;
            }
        }
        root = ray_select(g, root, col_ops, nc);
    }

    /* 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) {
        ray_op_t* sort_keys[16];
        uint8_t   sort_descs[16];
        uint8_t   n_sort = 0;
        for (int64_t i = 0; i + 1 < dict_n && n_sort < 16; 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] = 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) {
                /* Multiple column names */
                for (int64_t c = 0; c < val->len && n_sort < 16; c++) {
                    int64_t sid = ray_read_sym(ray_data(val), c, val->type, val->attrs);
                    ray_t* s = ray_sym_str(sid);
                    sort_keys[n_sort] = ray_scan(g, ray_str_ptr(s));
                    sort_descs[n_sort] = is_desc;
                    n_sort++;
                }
            } else {
                ray_graph_free(g); ray_release(tbl);
                return ray_error("domain", NULL);
            }
        }
        if (n_sort > 0)
            root = ray_sort_op(g, root, sort_keys, sort_descs, NULL, n_sort);
    }

    /* Take: add to DAG only when no group-by and no nearest (rerank
     * absorbs the take into its k parameter). */
    ray_t* take_range = NULL;
    if (take_expr && !by_expr && !nearest_expr) {
        ray_t* tv = ray_eval(take_expr);
        if (!tv || RAY_IS_ERR(tv)) { ray_graph_free(g); ray_release(tbl); return tv ? tv : ray_error("domain", NULL); }
        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;
            ray_release(tv);
            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) {
            take_range = tv;  /* apply after DAG execution */
        } else {
            ray_release(tv);
            ray_graph_free(g); ray_release(tbl);
            return ray_error("domain", NULL);
        }
    }

    /* Optimize and execute */
    root = ray_optimize(g, root);
    ray_t* result = ray_execute(g, root);

    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_sys_free(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; }
                                new_col->len = nrows_r;
                                char* src = (char*)ray_data(col);
                                char* dst = (char*)ray_data(new_col);
                                bool has_nulls = (col->attrs & RAY_ATTR_HAS_NULLS) != 0;
                                for (int64_t r = 0; r < nrows_r; r++) {
                                    memcpy(dst + r * esz, src + (nrows_r - 1 - r) * esz, esz);
                                    if (has_nulls && ray_vec_is_null(col, nrows_r - 1 - r))
                                        ray_vec_set_null(new_col, r, true);
                                }
                                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. */
            int64_t agg_user_names[16];
            int64_t all_user_names[16];
            int n_agg_user = 0;
            int n_all_user = 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 (n_all_user < 16) all_user_names[n_all_user++] = kid;
                if (by_expr && !is_agg_expr(dict_elems[i + 1])) continue;
                if (n_agg_user < 16) agg_user_names[n_agg_user++] = kid;
            }
            if (by_expr) {
                /* Rename only the agg columns (positions after keys).
                 * Non-agg LIST columns were named at scatter time. */
                for (int j = 0; j < n_agg_user && n_key_cols + j < ncols; j++)
                    ray_table_set_col_name(result, n_key_cols + j, agg_user_names[j]);
            } else {
                /* Projection-only: columns are in dict order */
                for (int j = 0; j < n_all_user && n_key_cols + j < ncols; j++)
                    ray_table_set_col_name(result, n_key_cols + j, all_user_names[j]);
            }
        }
    }

    /* 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);

            /* Resolve key sym — gated to single scalar key above. */
            int64_t ks = -1;
            if (by_expr->type == -RAY_SYM && (by_expr->attrs & RAY_ATTR_NAME))
                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);
                return ray_error("domain", NULL);
            }

            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 ? orig_key->len : 0;

            if (!orig_key || !grp_key) {
                ray_release(result); ray_release(tbl);
                return ray_error("domain", NULL);
            }

            if (n_groups > 0 && nrows > 0) {
                int8_t okt = orig_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. */
                #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;              \
                    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);
                if (!key_supported) {
                    ray_release(result); ray_release(tbl);
                    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) {
                    ray_release(result); ray_release(tbl);
                    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);
                    ray_release(result); ray_release(tbl);
                    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 */
                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 (int64_t r = 0; r < nrows; r++) {
                    int64_t rv;
                    KEY_READ(rv, orig_key, okt, r);
                    row_gid[r] = -1;
                    for (int64_t gi = 0; gi < n_groups; gi++) {
                        if (rv == gk[gi]) { row_gid[r] = gi; break; }
                    }
                }
                #undef KEY_READ

                memset(grp_cnt, 0, (size_t)n_groups * sizeof(int64_t));
                for (int64_t r = 0; r < nrows; r++)
                    if (row_gid[r] >= 0) grp_cnt[row_gid[r]]++;

                int64_t total = 0;
                for (int64_t gi = 0; gi < n_groups; gi++) total += grp_cnt[gi];
                ray_t* idx_hdr = ray_alloc((size_t)total * sizeof(int64_t));
                if (!idx_hdr) {
                    ray_free(gk_hdr); ray_free(rg_hdr); ray_free(cnt_hdr);
                    ray_free(off_hdr); ray_free(pos_hdr);
                    ray_release(result); ray_release(tbl);
                    return ray_error("oom", NULL);
                }
                int64_t* idx_buf = (int64_t*)ray_data(idx_hdr);

                offsets[0] = 0;
                for (int64_t gi = 1; gi < n_groups; gi++)
                    offsets[gi] = offsets[gi - 1] + grp_cnt[gi - 1];

                memcpy(pos, offsets, (size_t)n_groups * sizeof(int64_t));
                for (int64_t r = 0; r < nrows; r++) {
                    int64_t gi = row_gid[r];
                    if (gi >= 0) idx_buf[pos[gi]++] = r;
                }

                ray_t* scatter_err = NULL;
                for (uint8_t ni = 0; ni < n_nonaggs && !scatter_err; ni++) {
                    /* 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_aggr_unary_call(nonagg_exprs[ni])) {
                        ray_t* 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;
                    }

                    if (ray_env_push_scope() != RAY_OK) {
                        scatter_err = ray_error("oom", NULL); break;
                    }
                    expr_bind_table_names(nonagg_exprs[ni], tbl);
                    ray_t* full_val = ray_eval(nonagg_exprs[ni]);
                    ray_env_pop_scope();
                    if (!full_val || RAY_IS_ERR(full_val)) {
                        scatter_err = full_val ? full_val : ray_error("domain", NULL);
                        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;
                    }

                    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); ray_free(idx_hdr);

                if (scatter_err) {
                    if (result) ray_release(result);
                    ray_release(tbl);
                    return scatter_err;
                }
            } else {
                /* Empty group set: add empty LIST columns so the
                 * output schema still includes the user-declared
                 * non-agg columns. */
                for (uint8_t ni = 0; ni < n_nonaggs; ni++) {
                    ray_t* empty_list = ray_list_new(0);
                    if (!empty_list || RAY_IS_ERR(empty_list)) {
                        ray_release(result); ray_release(tbl);
                        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); 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);

    if (by_sym_vec_owned) ray_release(by_sym_vec_owned);

    return result;
}

/* (xbar col bucket) — time/value bucketing: floor(col/bucket)*bucket */
ray_t* ray_xbar_fn(ray_t* col, ray_t* bucket) {
    /* 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", 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", NULL);
        double c = as_f64(col), b = as_f64(bucket);
        if (b == 0.0) return ray_error("domain", NULL);
        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", 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", NULL);
}

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

/* 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;
    if (ct == RAY_I64) {
        if (atom->type != -RAY_I64)
            return ray_error("type", NULL);
        int64_t v = atom->i64;
        return ray_vec_append(col_vec, &v);
    } else if (ct == RAY_SYM) {
        if (atom->type != -RAY_SYM)
            return ray_error("type", NULL);
        int64_t v = atom->i64;
        return ray_vec_append(col_vec, &v);
    } else if (ct == RAY_F64) {
        if (atom->type != -RAY_F64 && atom->type != -RAY_I64)
            return ray_error("type", NULL);
        double v = (atom->type == -RAY_F64) ? atom->f64 : (double)atom->i64;
        return ray_vec_append(col_vec, &v);
    } else if (ct == RAY_BOOL) {
        if (atom->type != -RAY_BOOL)
            return ray_error("type", NULL);
        uint8_t v = atom->b8;
        return ray_vec_append(col_vec, &v);
    } else if (ct == RAY_STR && atom->type == -RAY_STR) {
        const char *sptr = ray_str_ptr(atom);
        size_t slen = ray_str_len(atom);
        return ray_str_vec_append(col_vec, sptr, slen);
    }
    return ray_error("type", NULL);
}

/* (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 */

ray_t* ray_update_fn(ray_t** args, int64_t n) {
    if (n < 1) return ray_error("domain", NULL);
    ray_t* dict = args[0];
    if (!dict || dict->type != RAY_DICT)
        return ray_error("type", NULL);

    ray_t* from_expr = dict_get(dict, "from");
    if (!from_expr) return ray_error("domain", NULL);
    /* 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", NULL);
        ray_retain(tbl);
    }
    if (tbl->type != RAY_TABLE) { ray_release(tbl); return ray_error("type", NULL); }

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

    /* 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);
            return ray_error("domain", "update clause has too many keys");
        }
        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); return ray_error("type", NULL); }

        /* Find group column in table */
        ray_t* grp_col = ray_table_get_col(tbl, by_col_name);
        if (!grp_col) { ray_release(tbl); return ray_error("domain", NULL); }
        int64_t nrows2 = ray_table_nrows(tbl);

        /* Use ray_group_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_fn(grp_col);
            if (!gd || RAY_IS_ERR(gd)) { ray_release(tbl); return gd ? gd : ray_error("oom", NULL); }
            groups = groups_to_pair_list(gd);
            ray_release(gd);
            if (RAY_IS_ERR(groups)) { ray_release(tbl); return groups; }
        }

        /* Start with a copy of the original table */
        int64_t ncols = ray_table_ncols(tbl);
        ray_t* result = ray_table_new((int32_t)ncols);
        if (RAY_IS_ERR(result)) { ray_release(groups); ray_release(tbl); return result; }
        for (int64_t c = 0; c < ncols; c++) {
            int64_t cn = ray_table_col_name(tbl, c);
            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)) { ray_release(groups); ray_release(tbl); return result; }
        }

        /* For each aggregate expression, compute per group and broadcast */
        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];

            /* Evaluate the aggregate for each group and broadcast */
            ray_t* grp_items = (ray_t**)ray_data(groups) ? groups : NULL;
            if (!grp_items) { ray_release(result); ray_release(groups); ray_release(tbl); return ray_error("oom", NULL); }
            int64_t ngroups = groups->len / 2;
            ray_t** gdata = (ray_t**)ray_data(groups);

            /* We need to evaluate the aggregate per group.
             * Build the result column by evaluating the expression on each group's subset. */
            ray_t* out_col = ray_vec_new(RAY_I64, nrows2); /* will be resized to correct type */
            if (RAY_IS_ERR(out_col)) { ray_release(result); ray_release(groups); ray_release(tbl); return out_col; }

            int8_t out_type = RAY_I64;
            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); ray_release(result); ray_release(groups); ray_release(tbl); 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;
                    ray_t* sub_col = ray_vec_new(ct, gsize);
                    if (RAY_IS_ERR(sub_col)) { ray_release(sub_tbl); ray_release(out_col); ray_release(result); ray_release(groups); ray_release(tbl); return sub_col; }
                    sub_col->len = gsize;
                    int esz = ray_elem_size(ct);
                    char* src = (char*)ray_data(full_col);
                    char* dst = (char*)ray_data(sub_col);
                    int64_t* idxs = (int64_t*)ray_data(idx_vec);
                    for (int64_t r = 0; r < gsize; r++)
                        memcpy(dst + r * esz, src + idxs[r] * esz, esz);
                    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); ray_release(result); ray_release(groups); ray_release(tbl); return sub_tbl; }
                }

                /* Evaluate expression on sub-table via DAG */
                ray_graph_t* ug = ray_graph_new(sub_tbl);
                ray_op_t* expr_op = compile_expr_dag(ug, agg_expr);
                if (!expr_op) { ray_graph_free(ug); ray_release(sub_tbl); ray_release(out_col); ray_release(result); ray_release(groups); ray_release(tbl); return ray_error("domain", NULL); }
                expr_op = ray_optimize(ug, expr_op);
                ray_t* agg_result = ray_execute(ug, expr_op);
                ray_graph_free(ug);
                ray_release(sub_tbl);

                if (RAY_IS_ERR(agg_result)) { ray_release(out_col); ray_release(result); ray_release(groups); ray_release(tbl); return agg_result; }

                /* 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); ray_release(result); ray_release(groups); ray_release(tbl); return out_col; }
                    out_col->len = nrows2;
                    first_group = 0;
                }

                /* Broadcast aggregate value to all rows in this group */
                int64_t* idxs = (int64_t*)ray_data(idx_vec);
                if (ray_is_atom(agg_result)) {
                    for (int64_t r = 0; r < gsize; r++)
                        store_typed_elem(out_col, idxs[r], agg_result);
                }
                ray_release(agg_result);
            }

            /* Add the new column to the result table */
            result = ray_table_add_col(result, kid, out_col);
            ray_release(out_col);
            if (RAY_IS_ERR(result)) { ray_release(groups); ray_release(tbl); return result; }
        }

        ray_release(groups);
        /* Store in-place if needed */
        if (inplace_sym >= 0) {
            ray_env_set(inplace_sym, result);
        }
        ray_release(tbl);
        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) {
        /* Try DAG compilation first, fall back to eval-level */
        ray_t* mask_vec = NULL;
        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);
            }
            ray_graph_free(g);
        }
        /* Fallback: eval-level predicate evaluation */
        if (!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_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(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", NULL); }
        if (mask_vec->type != RAY_BOOL || mask_vec->len != nrows) {
            ray_release(mask_vec);
            ray_release(tbl);
            return ray_error("type", NULL);
        }
        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);
            return ray_error("domain", "update clause has too many keys");
        }
        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); 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); return new_col; }

                /* Evaluate expression via DAG, fallback to eval-level */
                ray_t* expr_vec = NULL;
                {
                    ray_graph_t* ug = ray_graph_new(tbl);
                    if (ug) {
                        ray_op_t* expr_op = compile_expr_dag(ug, update_expr);
                        if (expr_op) {
                            expr_op = ray_optimize(ug, expr_op);
                            expr_vec = ray_execute(ug, expr_op);
                        }
                        ray_graph_free(ug);
                    }
                }
                if (!expr_vec || RAY_IS_ERR(expr_vec)) {
                    /* Fallback: eval with column bindings */
                    int64_t ncols_e = ray_table_ncols(tbl);
                    ray_env_push_scope();
                    for (int64_t c2 = 0; c2 < ncols_e; c2++) {
                        int64_t cn = ray_table_col_name(tbl, c2);
                        ray_t* col2 = ray_table_get_col_idx(tbl, c2);
                        ray_env_set(cn, col2);
                    }
                    expr_vec = ray_eval(update_expr);
                    ray_env_pop_scope();
                }
                if (!expr_vec || RAY_IS_ERR(expr_vec)) { ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl); return expr_vec ? expr_vec : ray_error("type", NULL); }

                /* 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);
                        return ray_error("type", NULL);
                    }
                    /* 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] = (int64_t)((double*)ray_data(expr_vec))[r];
                        else if (ct == RAY_I32 && expr_type == RAY_F64)
                            ((int32_t*)ray_data(new_col))[r] = (int32_t)((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-bit propagation: memcpy above only copies values,
                     * not the nullmap.  Carry over orig_col's nulls for the
                     * untouched rows, and pull expr_vec's nulls in for the
                     * masked rows.  Without this, casting a null F64 expr
                     * back to an I64 column silently produces 0. */
                    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);
                    }
                    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); 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) {
                        ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl);
                        return ray_error("type", NULL);
                    }
                    /* 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); 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); 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); 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); 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); return bcast; }
                        }
                    } else {
                        size_t esz = (ct == RAY_BOOL) ? 1 : 8;
                        uint8_t elem[8] = {0};
                        if (ct == RAY_F64 && expr_vec->type == -RAY_I64) {
                            double promoted = (double)expr_vec->i64;
                            memcpy(elem, &promoted, 8);
                        } else {
                            memcpy(elem, &expr_vec->i64, esz);
                        }
                        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); 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 the null bitmap doesn't, 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);
                    }
                    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); 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); return promoted; }
                    }
                    /* Carry the nullmap across the I64→F64 promotion. */
                    for (int64_t r = 0; r < nr; r++)
                        if (ray_vec_is_null(expr_vec, r))
                            ray_vec_set_null(promoted, r, true);
                    ray_release(expr_vec);
                    expr_vec = promoted;
                }

                /* Type check: expr_vec must match original column type */
                if (expr_vec->type != ct) {
                    ray_release(expr_vec); ray_release(new_col); ray_release(result); ray_release(mask_vec); ray_release(tbl);
                    return ray_error("type", NULL);
                }

                /* 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 nullmap. */
                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); 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) {
                    for (int64_t r = 0; r < nrows; r++) {
                        ray_t* src_vec = mask[r] ? expr_vec : orig_col;
                        int64_t sym_val = ray_read_sym(ray_data(src_vec), r, src_vec->type, src_vec->attrs);
                        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); return new_col; }
                        if (ray_vec_is_null(src_vec, r))
                            ray_vec_set_null(new_col, new_col->len - 1, true);
                    }
                } else {
                    size_t elem_sz = (ct == RAY_BOOL) ? 1 : 8;
                    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); 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 nullmap 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); return result; }
        }

        ray_release(mask_vec);
        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            ray_env_set(inplace_sym, result);
        }
        ray_release(tbl);
        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);
        return ray_error("domain", "update clause has too many keys");
    }
    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); 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 {
            ray_t* expr_vec = NULL;
            {
                ray_graph_t* ug = ray_graph_new(tbl);
                if (ug) {
                    ray_op_t* expr_op = compile_expr_dag(ug, update_expr);
                    if (expr_op) {
                        expr_op = ray_optimize(ug, expr_op);
                        expr_vec = ray_execute(ug, expr_op);
                    }
                    ray_graph_free(ug);
                }
            }
            if (!expr_vec || RAY_IS_ERR(expr_vec)) {
                /* Fallback: eval with column bindings */
                int64_t ncols_f = ray_table_ncols(tbl);
                ray_env_push_scope();
                for (int64_t cf = 0; cf < ncols_f; cf++) {
                    int64_t cn = ray_table_col_name(tbl, cf);
                    ray_t* colf = ray_table_get_col_idx(tbl, cf);
                    ray_env_set(cn, colf);
                }
                expr_vec = ray_eval(update_expr);
                ray_env_pop_scope();
            }
            if (!expr_vec || RAY_IS_ERR(expr_vec)) { ray_release(result); ray_release(tbl); return expr_vec ? expr_vec : ray_error("type", NULL); }

            /* 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) {
                    ray_release(expr_vec); ray_release(result); ray_release(tbl);
                    return ray_error("type", NULL);
                }
                /* 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); 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); 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); 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); return bcast; }
                    }
                } else {
                    size_t esz = (ct == RAY_BOOL) ? 1 : 8;
                    uint8_t elem[8] = {0};
                    if (ct == RAY_F64 && expr_vec->type == -RAY_I64) {
                        double promoted = (double)expr_vec->i64;
                        memcpy(elem, &promoted, 8);
                    } else {
                        memcpy(elem, &expr_vec->i64, esz);
                    }
                    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); 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);
                }
                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); 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); return promoted; }
                }
                /* Carry the nullmap across the I64→F64 promotion. */
                for (int64_t r = 0; r < nr; r++)
                    if (ray_vec_is_null(expr_vec, r))
                        ray_vec_set_null(promoted, r, true);
                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) {
                    ray_release(expr_vec); ray_release(result); ray_release(tbl);
                    return ray_error("type", NULL);
                }
            }

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); 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 */
        ray_t* update_expr = dict_elems[d + 1];
        ray_graph_t* ug = ray_graph_new(tbl);
        ray_op_t* expr_op = compile_expr_dag(ug, update_expr);
        if (!expr_op) { ray_release(result); ray_release(tbl); ray_graph_free(ug); return ray_error("domain", NULL); }
        expr_op = ray_optimize(ug, expr_op);
        ray_t* expr_vec = ray_execute(ug, expr_op);
        ray_graph_free(ug);
        if (RAY_IS_ERR(expr_vec)) { ray_release(result); ray_release(tbl); return expr_vec; }

        /* 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); return bcast; }
            size_t esz = ray_elem_size(ct);
            uint8_t elem[8] = {0};
            memcpy(elem, &expr_vec->i64, esz > 8 ? 8 : esz);
            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); 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);
            }
            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); return result; }
    }

    /* Store in-place if from: 't */
    if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
        ray_env_set(inplace_sym, result);
    }
    ray_release(tbl);
    return result;
}

/* (insert table (list val1 val2 ...)) — append a row to a table */
ray_t* ray_insert_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", NULL);

    /* Special form: detect 'sym (quoted symbol for in-place insert) */
    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_raw && tbl_raw->type == -RAY_SYM && !(tbl_raw->attrs & RAY_ATTR_NAME)) {
        /* Quoted symbol 'sym (no ATTR_NAME) — in-place insert */
        inplace_sym = tbl_raw->i64;
        tbl = ray_env_get(inplace_sym);
        if (!tbl || RAY_IS_ERR(tbl)) return ray_error("domain", NULL);
        ray_retain(tbl);
    } else 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", NULL);
    }

    /* ====================================================================
     * 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) { ray_release(tbl); return ray_error("type", NULL); }
        if (tbl->attrs & RAY_ATTR_ARENA) { ray_release(tbl); return ray_error("type", NULL); }

        /* 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", NULL); }
            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) {
            ray_release(tbl);
            return ray_error("type", NULL);
        }
        if (n != 2 && n != 3) {
            ray_release(tbl);
            return ray_error("domain", NULL);
        }

        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", NULL);
            }
            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 {
                ray_release(tbl);
                ray_release(val);
                return ray_error("type", NULL);
            }
            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", NULL);
            }
            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", NULL);
            }

            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) {
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", NULL);
                    }
                    result = ray_list_insert_many(tbl, idx_arg, val);
                    ray_release(tbl);
                } else {
                    ray_release(tbl); ray_release(idx_arg); ray_release(val);
                    return ray_error("type", NULL);
                }
            } 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 {
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", NULL);
                    }
                } 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", NULL);
                    }
                    if (val->type != tt && val->type != -tt) {
                        ray_release(tbl); ray_release(idx_arg); ray_release(val);
                        return ray_error("type", NULL);
                    }
                    result = ray_vec_insert_many(tbl, idx_arg, val);
                    ray_release(tbl);
                } else {
                    ray_release(tbl); ray_release(idx_arg); ray_release(val);
                    return ray_error("type", NULL);
                }
            }
            ray_release(idx_arg);
            ray_release(val);
        }

        if (inplace_sym >= 0 && result && !RAY_IS_ERR(result)) {
            ray_env_set(inplace_sym, result);
            ray_retain(result);
        }
        return result;
    }

    /* Table target: arity-3 positional row insert is not implemented. */
    if (n != 2) { ray_release(tbl); return ray_error("nyi", NULL); }

    /* 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", NULL); }
    if (tbl->type != RAY_TABLE) { ray_release(tbl); ray_release(row); return ray_error("type", NULL); }

    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) { ray_release(tbl); ray_release(row); return ray_error("type", NULL); }

    /* 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", NULL); }
        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", NULL);
            }
            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", NULL);
        }
        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++) {
                int64_t dk = ray_read_sym(ray_data(dkeys), d, RAY_SYM, dkeys->attrs);
                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 = ray_read_sym(ray_data(dkeys), d, RAY_SYM, dkeys->attrs);
            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", NULL);
    }

    ray_t** row_elems = (ray_t**)ray_data(row);
    int64_t nrows = ray_table_nrows(tbl);

    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;

        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 = (orig_col->attrs & RAY_ATTR_HAS_NULLS) != 0;
        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) {
            for (int64_t r = 0; r < nrows; r++) {
                int64_t sym_val = ray_read_sym(ray_data(orig_col), r, orig_col->type, orig_col->attrs);
                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 = (ct == RAY_BOOL) ? 1 : 8;
            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 (ray_is_vec(row_elems[c]) || row_elems[c]->type == RAY_LIST) {
            ray_t* merged = ray_concat_fn(new_col, row_elems[c]);
            ray_release(new_col);
            new_col = merged;
        } 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: update the variable in the env */
    if (inplace_sym >= 0 && !RAY_IS_ERR(result)) {
        ray_env_set(inplace_sym, result);
        ray_retain(result);
        return result;
    }
    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. */
ray_t* ray_upsert_fn(ray_t** args, int64_t n) {
    if (n < 3) return ray_error("domain", NULL);

    /* 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_raw && tbl_raw->type == -RAY_SYM && !(tbl_raw->attrs & RAY_ATTR_NAME)) {
        inplace_sym = tbl_raw->i64;
        tbl = ray_env_get(inplace_sym);
        if (!tbl || RAY_IS_ERR(tbl)) return ray_error("domain", NULL);
        ray_retain(tbl);
    } else 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", NULL);
    }

    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", NULL); }

    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", NULL); }

    if (tbl->type != RAY_TABLE) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("type", NULL); }
    if (!is_list(row) && row->type != RAY_TABLE && row->type != RAY_DICT) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("type", NULL); }

    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. */
        int64_t key_names[16];
        int64_t n_key = 0;
        if (key_sym->type == -RAY_SYM) {
            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 || k > 16) {
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("domain", NULL);
            }
            for (int64_t i = 0; i < k; i++)
                key_names[n_key++] = ray_table_col_name(tbl, i);
        } else {
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", NULL);
        }
        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) {
                ray_release(tbl); ray_release(key_sym); ray_release(row);
                return ray_error("value", NULL);
            }
        }

        /* Gather source columns in target order (now guaranteed 1-to-1). */
        ray_t* src_cols[64];
        for (int64_t c = 0; c < ncols && c < 64; c++) {
            int64_t cn = ray_table_col_name(tbl, c);
            src_cols[c] = ray_table_get_col(row, cn);
        }
        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) { 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)) { ray_release(tbl); ray_release(key_sym); ray_release(row); return new_tbl; }
            cur_tbl = new_tbl;
        }
        ray_release(tbl);
        ray_release(key_sym);
        ray_release(row);
        if (inplace_sym >= 0 && !RAY_IS_ERR(cur_tbl)) {
            ray_env_set(inplace_sym, cur_tbl);
            ray_retain(cur_tbl);
        }
        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", NULL);
        }
        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++) {
                int64_t dk = ray_read_sym(ray_data(dkeys), d, RAY_SYM, dkeys->attrs);
                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 = ray_read_sym(ray_data(dkeys), d, RAY_SYM, dkeys->attrs);
                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", NULL); }

    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;
    int64_t key_col_indices[16];
    if (key_sym->type == -RAY_SYM) {
        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) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("domain", NULL); }
    } else if (key_sym->type == -RAY_I64) {
        n_key_cols = key_sym->i64;
        if (n_key_cols <= 0 || n_key_cols > ncols || n_key_cols > 16) { ray_release(tbl); ray_release(key_sym); ray_release(row); return ray_error("domain", NULL); }
        for (int64_t k = 0; k < n_key_cols; k++) key_col_indices[k] = k;
    } else {
        ray_release(tbl); ray_release(key_sym); ray_release(row);
        return ray_error("type", NULL);
    }

    /* Multi-row upsert: if row values are vectors, iterate row-by-row */
    ray_t* key_elem = row_elems[key_col_indices[0]];
    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) { 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)) { ray_release(tbl); ray_release(key_sym); ray_release(row); return new_tbl; }
            cur_tbl = new_tbl;
        }
        ray_release(tbl);
        ray_release(key_sym);
        ray_release(row);
        if (inplace_sym >= 0 && !RAY_IS_ERR(cur_tbl)) {
            ray_env_set(inplace_sym, cur_tbl);
            ray_retain(cur_tbl);
        }
        return cur_tbl;
    }

    /* Type-check key columns before searching */
    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) {
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", NULL);
        }
        if (kt == RAY_SYM && key_atom->type != -RAY_SYM) {
            ray_release(tbl); ray_release(key_sym); ray_release(row);
            return ray_error("type", NULL);
        }
    }

    /* 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) {
                if (ray_read_sym(ray_data(key_col), r, key_col->type, key_col->attrs) != key_atom->i64) 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; }
    }

    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 && !RAY_IS_ERR(result)) {
            ray_env_set(inplace_sym, result);
            ray_retain(result);
        }
        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);

        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 {
                    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) {
            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 = ray_read_sym(ray_data(orig_col), r, orig_col->type, orig_col->attrs);
                    new_col = ray_vec_append(new_col, &sym_val);
                }
                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 = (ct == RAY_BOOL) ? 1 : 8;
            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)) { 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 && !RAY_IS_ERR(result)) {
        ray_env_set(inplace_sym, result);
        ray_retain(result);
    }
    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("domain", NULL);

    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", NULL);
    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", NULL); }

    int64_t nk = ray_len(keys);
    if (nk == 0 || nk > 16) { if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
    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);

    ray_op_t* lk[16], *rk[16];
    for (int64_t i = 0; i < nk; i++) {
        if (key_elems[i]->type != -RAY_SYM) {
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", NULL);
        }
        ray_t* name_str = ray_sym_str(key_elems[i]->i64);
        if (!name_str) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
        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]) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
    }

    if (_bxk) ray_release(_bxk);

    ray_op_t* jn = ray_join(g, left_node, lk, right_node, rk,
                           (uint8_t)nk, join_type);
    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); }

/* (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("domain", NULL);

    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", NULL);
    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", NULL); }

    int64_t nk = ray_len(keys);
    if (nk == 0 || nk > 16) { if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
    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);

    ray_op_t* lk[16], *rk[16];
    for (int64_t i = 0; i < nk; i++) {
        if (key_elems[i]->type != -RAY_SYM) {
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", NULL);
        }
        ray_t* name_str = ray_sym_str(key_elems[i]->i64);
        if (!name_str) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
        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]) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
    }

    if (_bxk) ray_release(_bxk);

    ray_op_t* jn = ray_antijoin(g, left_node, lk, right_node, rk, (uint8_t)nk);
    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                                              */
/* ------------------------------------------------------------------------ */

#define WJ_MAX_AGG 16

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;

typedef struct {
    int64_t  left_nrows;
    int64_t  right_nrows;
    int64_t  n_eq;
    int64_t  n_agg;

    /* 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[WJ_MAX_AGG];

    /* 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 */
    const void*     eq_data[WJ_MAX_AGG];
    int8_t          eq_type[WJ_MAX_AGG];
    uint8_t         eq_attrs[WJ_MAX_AGG];

    /* Per-agg metadata and preloaded sorted source vectors */
    uint8_t         agg_raw[WJ_MAX_AGG];
    uint16_t        agg_ops[WJ_MAX_AGG];
    int8_t          agg_result_types[WJ_MAX_AGG];
    int             agg_is_float[WJ_MAX_AGG];
    const int64_t*  sorted_i[WJ_MAX_AGG];
    const double*   sorted_f[WJ_MAX_AGG];
    const uint8_t*  sorted_nn[WJ_MAX_AGG];

    /* Per-agg result output — writers index by lr directly */
    void*           result_data[WJ_MAX_AGG];
    uint8_t*        result_null[WJ_MAX_AGG];  /* 1 byte per row: 1 = null */
} wj_scan_ctx_t;

static void wj_scan_fn(void* ctx_, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    wj_scan_ctx_t* c = (wj_scan_ctx_t*)ctx_;
    wj_acc_t acc[WJ_MAX_AGG];
    int64_t  n_eq   = c->n_eq;
    int64_t  n_agg  = c->n_agg;
    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];

        int64_t target_eq[WJ_MAX_AGG];
        for (int64_t e = 0; e < n_eq; e++)
            target_eq[e] = c->left_eq_arr[e][lr];

        /* lower_bound: first rank with (eq, time) >= (target_eq, lo) */
        int64_t lb = 0, lb_hi = rn;
        while (lb < lb_hi) {
            int64_t m = (lb + lb_hi) >> 1;
            int64_t ri = right_sort[m];
            int cmp = 0;
            for (int64_t e = 0; e < n_eq && cmp == 0; 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]) cmp = -1;
                else if (rv > target_eq[e]) cmp = 1;
            }
            if (cmp == 0 && rt_time_i[ri] < lo) cmp = -1;
            if (cmp < 0) lb = m + 1; else lb_hi = m;
        }
        int64_t ub = lb, ub_hi = rn;
        while (ub < ub_hi) {
            int64_t m = (ub + ub_hi) >> 1;
            int64_t ri = right_sort[m];
            int cmp = 0;
            for (int64_t e = 0; e < n_eq && cmp == 0; 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]) cmp = -1;
                else if (rv > target_eq[e]) cmp = 1;
            }
            if (cmp == 0 && rt_time_i[ri] <= hi) cmp = -1;
            if (cmp < 0) ub = m + 1; else ub_hi = m;
        }

        memset(acc, 0, sizeof(acc));
        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;
                }
            }

            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 t1 t2 [eq-keys] time-col)
 * ASOF join: for each left row, find closest right row with time <= left.time
 * within the same equality partition. */
ray_t* ray_window_join_fn(ray_t** args, int64_t n) {
    if (n < 4) return ray_error("domain", NULL);

    /* 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", NULL);
        }
    }
    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", NULL);
        int64_t* key_ids = (int64_t*)ray_data(keys_vec);

        /* Last key is the time key, rest are equality keys */
        int64_t time_key = key_ids[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", NULL);

        /* Get equality columns */
        ray_t* left_eq[16], *right_eq[16];
        for (int64_t e = 0; e < n_eq && e < 16; e++) {
            left_eq[e] = ray_table_get_col(left_tbl, key_ids[e]);
            right_eq[e] = ray_table_get_col(right_tbl, key_ids[e]);
            if (!left_eq[e] || !right_eq[e]) return ray_error("domain", NULL);
        }

        /* 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.
         * WJ_MAX_AGG is defined at file scope (for wj_scan_ctx_t). */
        int64_t  agg_names[WJ_MAX_AGG];
        uint16_t agg_ops[WJ_MAX_AGG];
        int64_t  agg_src_ids[WJ_MAX_AGG];
        ray_t*   agg_src_vecs[WJ_MAX_AGG] = {0};
        int8_t   agg_types[WJ_MAX_AGG];
        int      agg_is_float[WJ_MAX_AGG];
        ray_t*   agg_result_vecs[WJ_MAX_AGG] = {0};
        int      agg_raw[WJ_MAX_AGG] = {0};  /* {name: Col} bare-column form — legacy placeholder */
        int64_t  n_agg = 0;

        if (agg_dict && agg_dict->type == RAY_DICT) {
            ray_t* dkeys = ray_dict_keys(agg_dict);
            ray_t* dvals = ray_dict_vals(agg_dict);
            int64_t adn = (dkeys && dkeys->type == RAY_SYM) ? dkeys->len : 0;
            ray_t** lvals = (dvals && dvals->type == RAY_LIST) ? (ray_t**)ray_data(dvals) : NULL;
            for (int64_t di = 0; di < adn && n_agg < WJ_MAX_AGG; di++) {
                int64_t kname_id = ray_read_sym(ray_data(dkeys), di, RAY_SYM, dkeys->attrs);
                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 & RAY_ATTR_NAME))) continue;
                    if (!(ae[1]->type == -RAY_SYM && (ae[1]->attrs & RAY_ATTR_NAME))) 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 & RAY_ATTR_NAME)) {
                    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. */
        int8_t agg_result_types[WJ_MAX_AGG];
        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]);
                    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]);
                return ray_error("domain", NULL);
            }
            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]);
                return ray_error("domain", NULL);
            }
            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]);
                    return ray_error("type", NULL);
                }
            }
            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: 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]);
                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;
        const void* eq_data[16];
        int8_t      eq_type[16];
        uint8_t     eq_attrs[16];
        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]);
                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. */
        int64_t* sorted_i[WJ_MAX_AGG]  = {0};
        double*  sorted_f[WJ_MAX_AGG]  = {0};
        uint8_t* sorted_nn[WJ_MAX_AGG] = {0};  /* 0 = null, 1 = value present */
        ray_t*   sorted_i_hdr[WJ_MAX_AGG] = {0};
        ray_t*   sorted_f_hdr[WJ_MAX_AGG] = {0};
        ray_t*   sorted_nn_hdr[WJ_MAX_AGG] = {0};
        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 = (src->attrs & RAY_ATTR_HAS_NULLS) != 0;
            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]);
            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]);
            return ray_error("oom", NULL);
        }
        for (int64_t lr = 0; lr < left_nrows; lr++) {
            int alloc_iv = 0;
            ray_t* iv = collection_elem(intervals, lr, &alloc_iv);
            if (!iv || RAY_IS_ERR(iv) || ray_len(iv) < 2) {
                if (alloc_iv && iv) ray_release(iv);
                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]);
                return ray_error("domain", NULL);
            }
            int alloc_lo = 0, alloc_hi = 0;
            ray_t* lo_atom = collection_elem(iv, 0, &alloc_lo);
            ray_t* hi_atom = collection_elem(iv, 1, &alloc_hi);
            lo_arr[lr] = as_i64(lo_atom);
            hi_arr[lr] = as_i64(hi_atom);
            if (alloc_lo) ray_release(lo_atom);
            if (alloc_hi) ray_release(hi_atom);
            if (alloc_iv) ray_release(iv);
        }

        ray_t*   left_eq_hdr[WJ_MAX_AGG] = {0};
        int64_t* left_eq_arr[WJ_MAX_AGG] = {0};
        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]);
                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 the nullmap. */
        ray_t*   null_stage_hdr[WJ_MAX_AGG] = {0};
        uint8_t* null_stage[WJ_MAX_AGG]     = {0};
        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]);
                return ray_error("oom", NULL);
            }
            memset(null_stage[a], 0, (size_t)left_nrows);
        }

        /* Build the scan context and dispatch. */
        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.lo_arr      = lo_arr;
        wctx.hi_arr      = hi_arr;
        wctx.right_sort  = right_sort;
        wctx.rt_time_i   = rt_time_i;
        for (int64_t e = 0; e < n_eq; e++) {
            wctx.left_eq_arr[e] = left_eq_arr[e];
            wctx.eq_data[e]     = eq_data[e];
            wctx.eq_type[e]     = eq_type[e];
            wctx.eq_attrs[e]    = eq_attrs[e];
        }
        for (int64_t a = 0; a < n_agg; a++) {
            wctx.agg_raw[a]          = (uint8_t)agg_raw[a];
            wctx.agg_ops[a]          = agg_ops[a];
            wctx.agg_result_types[a] = agg_result_types[a];
            wctx.agg_is_float[a]     = agg_is_float[a];
            wctx.sorted_i[a]         = sorted_i[a];
            wctx.sorted_f[a]         = sorted_f[a];
            wctx.sorted_nn[a]        = sorted_nn[a];
            wctx.result_data[a]      = ray_data(agg_result_vecs[a]);
            wctx.result_null[a]      = null_stage[a];
        }

        ray_pool_t* pool = ray_pool_get();
        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 */
        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]);
        return result;
        #undef WJ_MAX_AGG
    }

    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", NULL);
    if (time_sym->type != -RAY_SYM)
        return ray_error("type", NULL);

    uint8_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 = (uint8_t)ray_len(eq_keys);
        eq_elems = (ray_t**)ray_data(eq_keys);
    }

    ray_graph_t* g = ray_graph_new(left_tbl);
    if (!g) 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); return ray_error("domain", NULL); }
    ray_op_t* time_op = ray_scan(g, ray_str_ptr(tname));
    if (!time_op) { ray_graph_free(g); return ray_error("domain", NULL); }

    ray_op_t* eq_ops[16];
    for (uint8_t i = 0; i < n_eq; i++) {
        if (eq_elems[i]->type != -RAY_SYM) {
            ray_graph_free(g);
            return ray_error("type", NULL);
        }
        ray_t* nm = ray_sym_str(eq_elems[i]->i64);
        if (!nm) { ray_graph_free(g); return ray_error("domain", NULL); }
        eq_ops[i] = ray_scan(g, ray_str_ptr(nm));
        if (!eq_ops[i]) { ray_graph_free(g); return ray_error("domain", NULL); }
    }

    if (_bxeq) ray_release(_bxeq);

    ray_op_t* jn = ray_asof_join(g, left_node, right_node,
                                time_op, eq_ops, n_eq, 1);
    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;
}

/* (asof-join [key1 key2 ... timeKey] leftTable rightTable)
 * Last key is the time/asof column, rest are equality keys. */
ray_t* ray_asof_join_fn(ray_t** args, int64_t n) {
    if (n < 3) return ray_error("arity", NULL);
    ray_t* keys_vec   = args[0];
    ray_t* left_tbl   = args[1];
    ray_t* right_tbl  = args[2];

    if (left_tbl->type != RAY_TABLE || right_tbl->type != RAY_TABLE)
        return ray_error("type", NULL);

    /* Keys vector must be a SYM vector with at least 2 elements (eq + time) */
    ray_t* _bxk = NULL;
    keys_vec = unbox_vec_arg(keys_vec, &_bxk);
    if (!is_list(keys_vec) || ray_len(keys_vec) < 2) {
        if (_bxk) ray_release(_bxk);
        return ray_error("domain", NULL);
    }
    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) {
        if (_bxk) ray_release(_bxk);
        return ray_error("type", NULL);
    }

    /* Remaining keys are equality keys */
    uint8_t n_eq = (uint8_t)(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", NULL); }
    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", NULL); }

    ray_op_t* eq_ops[16];
    for (uint8_t i = 0; i < n_eq; i++) {
        if (eq_syms[i]->type != -RAY_SYM) {
            ray_graph_free(g); if (_bxk) ray_release(_bxk);
            return ray_error("type", NULL);
        }
        ray_t* nm = ray_sym_str(eq_syms[i]->i64);
        if (!nm) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
        eq_ops[i] = ray_scan(g, ray_str_ptr(nm));
        if (!eq_ops[i]) { ray_graph_free(g); if (_bxk) ray_release(_bxk); return ray_error("domain", NULL); }
    }

    if (_bxk) ray_release(_bxk);

    ray_op_t* jn = ray_asof_join(g, left_node, right_node,
                                time_op, eq_ops, n_eq, 1);
    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;
}