rayforce-sys 1.2.0

Raw FFI bindings to the RayforceDB v2 core (librayforce)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
/*
 *   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.
 */

/*  Collection / higher-order builtins — extracted from eval.c  */

#include "lang/internal.h"
#include "core/types.h"
#include "core/pool.h"
#include "ops/hash.h"
#include "ops/internal.h"   /* col_propagate_str_pool */
#include "ops/idxop.h"
#include "ops/ops.h"
#include <stdatomic.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/* ══════════════════════════════════════════
 * Open-addressing hash set used by distinct/union/except/sect/in
 * to replace O(n×m) linear scans with O(n+m) hash lookups.
 *
 * Slots store the row index of the first occurrence in a single vec,
 * or HS_EMPTY.  Hashing and equality are dispatched through a small
 * vtable keyed by collection type so the same set can probe a typed
 * vec or a boxed RAY_LIST without per-call boxing on the typed path.
 * Nulls aggregate into a separate null-bucket flag, mirroring atom_eq
 * (all nulls compare equal regardless of typed-null vs RAY_NULL_OBJ).
 *
 * Load factor capped at 0.5; growth doubles capacity.
 * ══════════════════════════════════════════ */

#define HS_EMPTY ((int64_t)-1)

typedef struct hashset_t {
    int64_t* slots;        /* cap entries; -1 = empty, else row index */
    ray_t*   block;        /* backing alloc for slots */
    int64_t  cap;          /* power of 2 */
    int64_t  mask;         /* cap - 1 */
    int64_t  count;        /* live entries (excl. null bucket) */
    int      null_seen;    /* 1 if any null has been recorded */
    int64_t  null_idx;     /* row index of first null encountered */
    /* Cached typed-vec data pointers to avoid re-derefs in hot loops. */
    ray_t*   src;          /* source vec or list */
    int8_t   src_type;     /* ray_t.type */
    bool     src_has_nulls;
    void*    src_data;     /* pointer to typed data (or RAY_LIST elements) */
    /* Int-family cells hash through f64 (hs_hash_row).  Off until a probe
     * crosses numeric classes — see hs_needs_num_f64. */
    bool     num_f64;
    int8_t   probe_type;   /* last probe type vetted by hashset_adopt_probe */
} hashset_t;

/* Numeric class of a typed-vec type, matching is_numeric/atom_eq: every
 * int width compares to every float width through f64.  Temporal types are
 * not numeric there (a DATE never equals an I32), so they stay class 0. */
enum { HS_NUM_NONE = 0, HS_NUM_INT = 1, HS_NUM_FLT = 2 };
static inline int hs_num_class(int8_t t) {
    switch (t) {
        case RAY_BOOL: case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
            return HS_NUM_INT;
        case RAY_F32: case RAY_F64:
            return HS_NUM_FLT;
        default:
            return HS_NUM_NONE;
    }
}

/* Cell i of a numeric typed vec as f64 — as_f64 on the boxed atom. */
static inline double hs_cell_f64(int8_t t, const void* data, int64_t i) {
    switch (t) {
        case RAY_BOOL: return (double)((const bool*)data)[i];
        case RAY_U8:   return (double)((const uint8_t*)data)[i];
        case RAY_I16:  return (double)((const int16_t*)data)[i];
        case RAY_I32:  return (double)((const int32_t*)data)[i];
        case RAY_I64:  return (double)((const int64_t*)data)[i];
        case RAY_F32:  return (double)((const float*)data)[i];
        default:       return ((const double*)data)[i];
    }
}

/* Int cells hash with ray_hash_i64 and float cells (typed, or numeric atoms
 * in a RAY_LIST) with ray_hash_f64, so 3 and 3.0 land in different buckets
 * and hs_eq_rows never sees them (#645).  An int side meeting a float or
 * list side must hash its ints through f64 as well. */
static inline bool hs_needs_num_f64(int8_t a, int8_t b) {
    int ca = hs_num_class(a), cb = hs_num_class(b);
    if (ca == HS_NUM_INT) return cb == HS_NUM_FLT || b == RAY_LIST;
    if (cb == HS_NUM_INT) return ca == HS_NUM_FLT || a == RAY_LIST;
    return false;
}

/* Hash a single row at index i in src.  Mirrors atom_eq's coercion
 * rules: numeric types normalize through f64 so an I64 atom and an
 * F64 atom holding the same value collide.  A typed int vec hashes
 * through i64 unless num_f64 is set — the set is being probed from the
 * other numeric class (hs_needs_num_f64). */
static uint64_t hs_hash_row(ray_t* src, int64_t i, int8_t t, void* data,
                            bool num_f64) {
    if (num_f64 && hs_num_class(t) == HS_NUM_INT)
        return ray_hash_f64(hs_cell_f64(t, data, i));
    switch (t) {
        case RAY_I64:       return ray_hash_i64(((const int64_t*)data)[i]);
        case RAY_I32:       return ray_hash_i64((int64_t)((const int32_t*)data)[i]);
        case RAY_I16:       return ray_hash_i64((int64_t)((const int16_t*)data)[i]);
        case RAY_U8:        return ray_hash_i64((int64_t)((const uint8_t*)data)[i]);
        case RAY_BOOL:      return ray_hash_i64((int64_t)((const bool*)data)[i]);
        case RAY_F64:       return ray_hash_f64(((const double*)data)[i]);
        case RAY_F32:       return ray_hash_f64((double)((const float*)data)[i]);
        case RAY_DATE:      return ray_hash_i64((int64_t)((const int32_t*)data)[i]);
        case RAY_TIME:      return ray_hash_i64((int64_t)((const int32_t*)data)[i]);
        case RAY_TIMESTAMP: return ray_hash_i64(((const int64_t*)data)[i]);
        case RAY_SYM: {
            /* Hashes the raw cell id, i.e. the BUILD side's id space.
             * Cross-domain probes never reach this hash — they are
             * translated into the build side's domain up front in
             * hashset_find_xrow (sym-domain Phase 2). */
            uint64_t s = ray_read_sym(data, i, src->type, src->attrs);
            return ray_hash_i64((int64_t)s);
        }
        case RAY_GUID:
            return ray_hash_bytes((const uint8_t*)data + i * 16, 16);
        case RAY_STR: {
            size_t l = 0;
            const char* p = ray_str_vec_get(src, i, &l);
            return p ? ray_hash_bytes(p, l) : 0;
        }
        case RAY_LIST: {
            ray_t** elems = (ray_t**)data;
            ray_t* e = elems[i];
            if (!e || RAY_ATOM_IS_NULL(e)) return 0;
            /* Numeric coercion: hash as f64 so distinct numeric types
             * holding the same value collide (atom_eq does the same). */
            if (is_numeric(e)) return ray_hash_f64(as_f64(e));
            switch (e->type) {
                case -RAY_SYM:       return ray_hash_i64(e->i64);
                case -RAY_DATE:
                case -RAY_TIME:      return ray_hash_i64((int64_t)e->i32);
                case -RAY_TIMESTAMP: return ray_hash_i64(e->i64);
                case -RAY_GUID: {
                    const uint8_t* g = e->obj
                        ? (const uint8_t*)ray_data(e->obj)
                        : (const uint8_t*)ray_data((ray_t*)e);
                    return ray_hash_bytes(g, 16);
                }
                case -RAY_STR:
                    return ray_hash_bytes(ray_str_ptr(e), ray_str_len(e));
                default:
                    /* Unknown / unsupported atom kind: hash by type tag. */
                    return ray_hash_i64((int64_t)e->type);
            }
        }
        default:
            return ray_hash_i64(i);
    }
}

/* Compare two rows for equality, dispatched the same way as the hash. */
static int hs_eq_rows(ray_t* a_src, int64_t ai, int8_t at, void* a_data,
                      ray_t* b_src, int64_t bi, int8_t bt, void* b_data) {
    if (at == bt && at != RAY_LIST) {
        switch (at) {
            case RAY_I64:       return ((const int64_t*)a_data)[ai] == ((const int64_t*)b_data)[bi];
            case RAY_I32:       return ((const int32_t*)a_data)[ai] == ((const int32_t*)b_data)[bi];
            case RAY_I16:       return ((const int16_t*)a_data)[ai] == ((const int16_t*)b_data)[bi];
            case RAY_U8:        return ((const uint8_t*)a_data)[ai] == ((const uint8_t*)b_data)[bi];
            case RAY_BOOL:      return ((const bool*)a_data)[ai] == ((const bool*)b_data)[bi];
            case RAY_F64:       return ((const double*)a_data)[ai] == ((const double*)b_data)[bi];
            case RAY_F32:       return ((const float*)a_data)[ai] == ((const float*)b_data)[bi];
            case RAY_DATE:
            case RAY_TIME:      return ((const int32_t*)a_data)[ai] == ((const int32_t*)b_data)[bi];
            case RAY_TIMESTAMP: return ((const int64_t*)a_data)[ai] == ((const int64_t*)b_data)[bi];
            case RAY_SYM: {
                /* Raw index equality is only meaningful within ONE id
                 * space — gate on domain identity (sym-domain Phase 2);
                 * cross-domain rows fall through to the boxed compare
                 * below (collection_elem re-expresses through each
                 * cell's domain).  Pre-flip both sides are the runtime
                 * singleton: byte-identical fast path. */
                if (ray_sym_vec_domain(a_src) != ray_sym_vec_domain(b_src))
                    break;
                uint64_t sa = ray_read_sym(a_data, ai, a_src->type, a_src->attrs);
                uint64_t sb = ray_read_sym(b_data, bi, b_src->type, b_src->attrs);
                return sa == sb;
            }
            case RAY_GUID:
                return memcmp((const uint8_t*)a_data + ai * 16,
                              (const uint8_t*)b_data + bi * 16, 16) == 0;
            case RAY_STR: {
                size_t al = 0, bl = 0;
                const char* ap = ray_str_vec_get(a_src, ai, &al);
                const char* bp = ray_str_vec_get(b_src, bi, &bl);
                if (!ap) ap = "";
                if (!bp) bp = "";
                return al == bl && memcmp(ap, bp, al) == 0;
            }
        }
    }
    /* Mixed numeric typed vecs: atom_eq compares any two numerics
     * through f64 — do the same without boxing a pair per probe. */
    if (hs_num_class(at) != HS_NUM_NONE && hs_num_class(bt) != HS_NUM_NONE)
        return hs_cell_f64(at, a_data, ai) == hs_cell_f64(bt, b_data, bi);
    /* Fall back to atom_eq via boxed values.  Used for cross-type
     * comparisons (e.g. except over typed I64 vs F64 vec) and the
     * RAY_LIST path.  collection_elem allocates a temporary atom for
     * typed vecs; the cost is paid only on collisions / mixed types. */
    int alloc_a = 0, alloc_b = 0;
    ray_t* a = collection_elem(a_src, ai, &alloc_a);
    ray_t* b = collection_elem(b_src, bi, &alloc_b);
    int eq = (a && b) ? atom_eq(a, b) : 0;
    if (alloc_a && a) ray_release(a);
    if (alloc_b && b) ray_release(b);
    return eq;
}

/* True if the row at index i in src is null. */
static inline int hs_row_is_null(ray_t* src, int64_t i, void* data) {
    if (src->type == RAY_LIST) {
        ray_t* e = ((ray_t**)data)[i];
        return !e || RAY_ATOM_IS_NULL(e);
    }
    return ray_vec_may_have_nulls(src) && ray_vec_is_null(src, i);
}

static bool hashset_init(hashset_t* hs, ray_t* src, int64_t hint) {
    int64_t cap = 16;
    /* Cap target: 2× hint to keep load factor under 0.5. */
    while (cap < (hint > 0 ? hint * 2 : 16)) cap *= 2;
    hs->block = ray_alloc((size_t)cap * sizeof(int64_t));
    if (!hs->block || RAY_IS_ERR(hs->block)) { hs->block = NULL; return false; }
    hs->slots = (int64_t*)ray_data(hs->block);
    for (int64_t i = 0; i < cap; i++) hs->slots[i] = HS_EMPTY;
    hs->cap = cap;
    hs->mask = cap - 1;
    hs->count = 0;
    hs->null_seen = 0;
    hs->null_idx = HS_EMPTY;
    hs->src = src;
    hs->src_type = src ? src->type : 0;
    hs->src_has_nulls = src ? ray_vec_may_have_nulls(src) : false;
    hs->src_data = src ? ray_data(src) : NULL;
    hs->num_f64 = false;
    hs->probe_type = hs->src_type;
    return true;
}

static void hashset_destroy(hashset_t* hs) {
    if (hs->block) { ray_release(hs->block); hs->block = NULL; }
    hs->slots = NULL;
}

/* Re-slot every stored row into a fresh table of new_cap, hashing with
 * the set's current num_f64 mode. */
static bool hashset_rehash(hashset_t* hs, int64_t new_cap) {
    int64_t old_cap = hs->cap;
    int64_t* old_slots = hs->slots;
    ray_t* nb = ray_alloc((size_t)new_cap * sizeof(int64_t));
    if (!nb || RAY_IS_ERR(nb)) return false;
    int64_t* ns = (int64_t*)ray_data(nb);
    for (int64_t i = 0; i < new_cap; i++) ns[i] = HS_EMPTY;
    int64_t mask = new_cap - 1;
    for (int64_t i = 0; i < old_cap; i++) {
        int64_t ridx = old_slots[i];
        if (ridx == HS_EMPTY) continue;
        uint64_t h = hs_hash_row(hs->src, ridx, hs->src_type, hs->src_data,
                                 hs->num_f64);
        int64_t s = (int64_t)(h & (uint64_t)mask);
        while (ns[s] != HS_EMPTY) s = (s + 1) & mask;
        ns[s] = ridx;
    }
    ray_release(hs->block);
    hs->block = nb;
    hs->slots = ns;
    hs->cap = new_cap;
    hs->mask = mask;
    return true;
}

static bool hashset_grow(hashset_t* hs) {
    int64_t new_cap = hs->cap * 2;
    if (new_cap < hs->cap) return false;
    return hashset_rehash(hs, new_cap);
}

/* Probe the set for the row (probe_src, probe_i).  Returns the stored
 * row index from the build-side vec on hit, HS_EMPTY on miss. */
/* A probe of a type the set has not seen yet.  From the other numeric
 * class, rehash once so int cells hash through f64 — later probes and
 * inserts keep the mode, and same-type callers never get here.  Returns
 * false on OOM: the slots keep their old layout, the type stays unvetted
 * so the next probe retries, and the caller answers by hashset_scan. */
static __attribute__((noinline, cold)) bool
hashset_adopt_probe(hashset_t* hs, int8_t probe_type) {
    if (!hs->num_f64 && hs_needs_num_f64(hs->src_type, probe_type)) {
        hs->num_f64 = true;
        /* Only int cells change hash in this mode: a float- or list-built
         * set already sits in the f64 layout, so it needs no rehash. */
        if (hs_num_class(hs->src_type) == HS_NUM_INT &&
            !hashset_rehash(hs, hs->cap)) {
            hs->num_f64 = false;
            return false;
        }
    }
    hs->probe_type = probe_type;
    return true;
}

/* Hash-free probe for when the table could not be rehashed: compare the
 * row against every stored one. */
static __attribute__((noinline, cold)) int64_t
hashset_scan(hashset_t* hs, ray_t* probe_src, int64_t probe_i,
             int8_t probe_type, void* probe_data) {
    for (int64_t k = 0; k < hs->cap; k++) {
        int64_t stored = hs->slots[k];
        if (stored != HS_EMPTY &&
            hs_eq_rows(probe_src, probe_i, probe_type, probe_data,
                       hs->src, stored, hs->src_type, hs->src_data))
            return stored;
    }
    return HS_EMPTY;
}

static int64_t hashset_find_xrow(hashset_t* hs, ray_t* probe_src, int64_t probe_i,
                                  int8_t probe_type, void* probe_data) {
    if (hs_row_is_null(probe_src, probe_i, probe_data))
        return hs->null_seen ? hs->null_idx : HS_EMPTY;
    /* sym-domain Phase 2: SYM-vec vs SYM-vec probes across DIFFERENT
     * domains cannot compare (or hash) raw ids.  Re-express the probe
     * cell in the build side's domain once, then run the standard raw
     * probe.  Absent from the build domain ⇒ no build row can hold
     * that symbol ⇒ miss.  Same-domain probes (the only pre-flip case)
     * skip this entirely: byte-identical fast path. */
    if (probe_type == RAY_SYM && hs->src_type == RAY_SYM &&
        probe_src->type == RAY_SYM && hs->src && hs->src->type == RAY_SYM) {
        struct ray_sym_domain_s* bdom = ray_sym_vec_domain(hs->src);
        struct ray_sym_domain_s* pdom = ray_sym_vec_domain(probe_src);
        if (bdom != pdom) {
            int64_t pid = ray_read_sym(probe_data, probe_i, probe_type,
                                       probe_src->attrs);
            ray_t* s = ray_sym_domain_str(pdom, pid);
            int64_t xid = s ? ray_sym_domain_find(bdom, ray_str_ptr(s),
                                                  ray_str_len(s)) : -1;
            if (xid < 0) return HS_EMPTY;
            uint64_t hx = ray_hash_i64(xid);
            int64_t sx = (int64_t)(hx & (uint64_t)hs->mask);
            while (hs->slots[sx] != HS_EMPTY) {
                int64_t stored = hs->slots[sx];
                if (ray_read_sym(hs->src_data, stored, hs->src_type,
                                 hs->src->attrs) == xid)
                    return stored;
                sx = (sx + 1) & hs->mask;
            }
            return HS_EMPTY;
        }
    }
    if (probe_type != hs->probe_type &&
        !hashset_adopt_probe(hs, probe_type))
        return hashset_scan(hs, probe_src, probe_i, probe_type, probe_data);
    uint64_t h = hs_hash_row(probe_src, probe_i, probe_type, probe_data,
                             hs->num_f64);
    int64_t s = (int64_t)(h & (uint64_t)hs->mask);
    while (hs->slots[s] != HS_EMPTY) {
        int64_t stored = hs->slots[s];
        if (hs_eq_rows(probe_src, probe_i, probe_type, probe_data,
                       hs->src,    stored,   hs->src_type, hs->src_data))
            return stored;
        s = (s + 1) & hs->mask;
    }
    return HS_EMPTY;
}

/* qsort comparator state for distinct_sort_indices: thread-local so
 * the standard qsort entry point can pull it without a context arg.
 * Single-threaded VM-eval is the only caller, so TLS is fine. */
static _Thread_local ray_t* g_dsort_src;
static _Thread_local int8_t g_dsort_type;
static _Thread_local const void* g_dsort_data;

static int distinct_sort_cmp(const void* a, const void* b) {
    int64_t ia = *(const int64_t*)a;
    int64_t ib = *(const int64_t*)b;
    switch (g_dsort_type) {
        case RAY_I64: case RAY_TIMESTAMP: {
            int64_t va = ((const int64_t*)g_dsort_data)[ia];
            int64_t vb = ((const int64_t*)g_dsort_data)[ib];
            return (va > vb) - (va < vb);
        }
        case RAY_I32: case RAY_DATE: case RAY_TIME: {
            int32_t va = ((const int32_t*)g_dsort_data)[ia];
            int32_t vb = ((const int32_t*)g_dsort_data)[ib];
            return (va > vb) - (va < vb);
        }
        case RAY_I16: {
            int16_t va = ((const int16_t*)g_dsort_data)[ia];
            int16_t vb = ((const int16_t*)g_dsort_data)[ib];
            return (va > vb) - (va < vb);
        }
        case RAY_U8: case RAY_BOOL: {
            uint8_t va = ((const uint8_t*)g_dsort_data)[ia];
            uint8_t vb = ((const uint8_t*)g_dsort_data)[ib];
            return (va > vb) - (va < vb);
        }
        case RAY_F64: {
            double va = ((const double*)g_dsort_data)[ia];
            double vb = ((const double*)g_dsort_data)[ib];
            return (va > vb) - (va < vb);
        }
        case RAY_F32: {
            float va = ((const float*)g_dsort_data)[ia];
            float vb = ((const float*)g_dsort_data)[ib];
            return (va > vb) - (va < vb);
        }
        default: {
            /* Fall back to boxed compare for less-common element kinds. */
            int alloc_a = 0, alloc_b = 0;
            ray_t* a_e = collection_elem(g_dsort_src, ia, &alloc_a);
            ray_t* b_e = collection_elem(g_dsort_src, ib, &alloc_b);
            double va = a_e ? as_f64(a_e) : 0.0;
            double vb = b_e ? as_f64(b_e) : 0.0;
            if (alloc_a && a_e) ray_release(a_e);
            if (alloc_b && b_e) ray_release(b_e);
            return (va > vb) - (va < vb);
        }
    }
}

/* Sort `count` indices by their numeric value in `src`.  Preserves
 * the existing `distinct` semantic of returning numeric output sorted. */
static void distinct_sort_indices(ray_t* src, int64_t* idx, int64_t count) {
    g_dsort_src = src;
    g_dsort_type = src->type;
    g_dsort_data = ray_data(src);
    qsort(idx, (size_t)count, sizeof(int64_t), distinct_sort_cmp);
}

/* Insert row i (from the set's build-side src) if absent.
 * Returns true if newly inserted, false if duplicate.  On grow OOM
 * the set silently keeps the previous capacity (caller proceeds). */
static bool hashset_insert(hashset_t* hs, int64_t i) {
    if (hs_row_is_null(hs->src, i, hs->src_data)) {
        if (hs->null_seen) return false;
        hs->null_seen = 1;
        hs->null_idx = i;
        return true;
    }
    if (hs->count * 2 >= hs->cap) {
        if (!hashset_grow(hs)) { /* fall through, may degrade */ }
    }
    uint64_t h = hs_hash_row(hs->src, i, hs->src_type, hs->src_data,
                             hs->num_f64);
    int64_t s = (int64_t)(h & (uint64_t)hs->mask);
    while (hs->slots[s] != HS_EMPTY) {
        int64_t stored = hs->slots[s];
        if (hs_eq_rows(hs->src, i,      hs->src_type, hs->src_data,
                       hs->src, stored, hs->src_type, hs->src_data))
            return false;
        s = (s + 1) & hs->mask;
    }
    hs->slots[s] = i;
    hs->count++;
    return true;
}

/* Vector-needle find with the NEEDLES hashed and the haystack scanned once:
 * out[i] = first row of `hay` equal to needle i, NULL_I64 on a miss.  The
 * alternative (hash the haystack, probe the needles) costs a hash build over
 * the big side per call; this costs one over the small side plus a scan that
 * stops as soon as every distinct needle has been seen.  `first` is indexed
 * by the hashset's representative row (the first needle inserted with that
 * key), so duplicate needles share one answer and a null needle matches the
 * haystack's first null exactly as the haystack-hashed path does.
 * Returns false only on OOM (caller falls back). */
static bool find_hash_needles(ray_t* hay, ray_t* nd, int64_t* out, bool* any_null) {
    int64_t m = nd->len, n = hay->len;
    hashset_t hs;
    if (!hashset_init(&hs, nd, m)) return false;
    int64_t* first = (int64_t*)ray_alloc_raw((size_t)(m > 0 ? m : 1) * sizeof(int64_t));
    if (!first) { hashset_destroy(&hs); return false; }
    int64_t distinct = 0;
    for (int64_t j = 0; j < m; j++) {
        first[j] = -1;
        if (hashset_insert(&hs, j)) distinct++;
    }
    int8_t ht = hay->type; void* hd = ray_data(hay);
    int64_t found = 0;
    for (int64_t r = 0; r < n && found < distinct; r++) {
        int64_t rep = hashset_find_xrow(&hs, hay, r, ht, hd);
        if (rep != HS_EMPTY && first[rep] < 0) { first[rep] = r; found++; }
    }
    int8_t nt = nd->type; void* ndd = ray_data(nd);
    for (int64_t i = 0; i < m; i++) {
        int64_t rep = hashset_find_xrow(&hs, nd, i, nt, ndd);
        int64_t pos = (rep == HS_EMPTY) ? -1 : first[rep];
        if (pos < 0) { out[i] = NULL_I64; *any_null = true; }
        else out[i] = pos;
    }
    ray_free_raw(first);
    hashset_destroy(&hs);
    return true;
}

/* Hash the smaller side only when the scan side probes cheaply: SYM rows
 * from a DIFFERENT domain than the needles would need a per-row string
 * translation (hashset_find_xrow's cross-domain arm) — hashing the
 * haystack and translating the few needles is the right orientation there. */
static bool find_needle_side_ok(ray_t* hay, ray_t* nd) {
    if (nd->len >= hay->len) return false;
    if (hay->type == RAY_SYM && nd->type == RAY_SYM &&
        ray_sym_vec_domain(hay) != ray_sym_vec_domain(nd)) return false;
    return true;
}

/* ══════════════════════════════════════════
 * Higher-order functions
 * ══════════════════════════════════════════ */

/* (map fn val vec) — apply binary fn(val, elem) to each element of vec.
 * Also supports (map fn vec) for unary mapping. */
ray_t* ray_map_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", "map: requires at least 2 args (fn and vec), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* fn = args[0];
    ray_t* _bx = NULL;

    if (n == 2) {
        /* Unary map: (map fn vec) */
        ray_t* vec = unbox_vec_arg(args[1], &_bx);
        if (RAY_IS_ERR(vec)) return vec;
        if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "map: vec arg must be a list, got %s", ray_type_name(vec->type)); }
        int64_t len = ray_len(vec);
        ray_t* result = ray_alloc(len * sizeof(ray_t*));
        if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
        result->type = RAY_LIST;
        result->len = len;
        ray_t** out = (ray_t**)ray_data(result);
        ray_t** elems = (ray_t**)ray_data(vec);
        for (int64_t i = 0; i < len; i++) {
            out[i] = call_fn1(fn, elems[i]);
            /* Materialise a lazy result (e.g. the lambda returned
             * (count (distinct x)) — distinct/count of a vec is lazy).  A LIST
             * of unforced lazy nodes breaks every downstream consumer that
             * doesn't force per element (sum, ==, +). */
            if (ray_is_lazy(out[i])) out[i] = ray_lazy_materialize(out[i]);
            if (RAY_IS_ERR(out[i])) {
                for (int64_t j = 0; j < i; j++) ray_release(out[j]);
                result->len = 0; ray_release(result); if (_bx) ray_release(_bx);
                return out[i];
            }
        }
        if (_bx) ray_release(_bx);
        return result;
    }

    /* Binary map: (map fn val vec) — apply fn(val, elem) */
    ray_t* val = args[1];
    ray_t* vec = unbox_vec_arg(args[2], &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    /* If vec is scalar, just call fn(val, vec) once */
    if (!is_list(vec)) {
        if (_bx) ray_release(_bx);
        return call_fn2(fn, val, args[2]);
    }
    int64_t len = ray_len(vec);
    ray_t* result = ray_alloc(len * sizeof(ray_t*));
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = len;
    ray_t** out = (ray_t**)ray_data(result);
    ray_t** elems = (ray_t**)ray_data(vec);
    for (int64_t i = 0; i < len; i++) {
        out[i] = call_fn2(fn, val, elems[i]);
        if (ray_is_lazy(out[i])) out[i] = ray_lazy_materialize(out[i]);
        if (RAY_IS_ERR(out[i])) {
            for (int64_t j = 0; j < i; j++) ray_release(out[j]);
            result->len = 0; ray_release(result); if (_bx) ray_release(_bx);
            return out[i];
        }
    }
    if (_bx) ray_release(_bx);
    return result;
}

/* (pmap fn val vec) — sequential alias for map, kept as a distinct builtin so
 * call sites can signal intent.  Evaluation is NOT parallelised: the applied
 * function runs on the calling thread's VM (the VM is thread-local and eval is
 * single-threaded), so results and semantics are identical to map. */
ray_t* ray_pmap_fn(ray_t** args, int64_t n) {
    return ray_map_fn(args, n);
}

/* (fold fn vec) or (fold fn init vec) — reduce with binary fn */
ray_t* ray_fold_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", "fold: requires at least 2 args (fn and vec), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* fn = args[0];
    ray_t* vec;
    ray_t* acc;
    ray_t* _bx = NULL;
    if (n == 2) {
        /* (fold fn vec) — use first element as initial value */
        vec = unbox_vec_arg(args[1], &_bx);
        if (RAY_IS_ERR(vec)) return vec;
        if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "fold: vec arg must be a list, got %s", ray_type_name(vec->type)); }
        int64_t len = ray_len(vec);
        if (len == 0) { if (_bx) ray_release(_bx); return ray_error("domain", "fold: empty list has no initial value, got %lld", (long long)len); }
        ray_t** elems = (ray_t**)ray_data(vec);
        ray_retain(elems[0]);
        acc = elems[0];
        for (int64_t i = 1; i < len; i++) {
            ray_t* next = call_fn2(fn, acc, elems[i]);
            ray_release(acc);
            if (RAY_IS_ERR(next)) { if (_bx) ray_release(_bx); return next; }
            acc = next;
        }
        if (_bx) ray_release(_bx);
        return acc;
    }

    /* (fold fn init vec) */
    ray_retain(args[1]);
    acc = args[1];
    vec = unbox_vec_arg(args[2], &_bx);
    if (RAY_IS_ERR(vec)) { ray_release(acc); return vec; }
    if (!is_list(vec)) { ray_release(acc); if (_bx) ray_release(_bx); return ray_error("type", "fold: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    ray_t** elems = (ray_t**)ray_data(vec);
    for (int64_t i = 0; i < len; i++) {
        ray_t* next = call_fn2(fn, acc, elems[i]);
        ray_release(acc);
        if (RAY_IS_ERR(next)) { if (_bx) ray_release(_bx); return next; }
        acc = next;
    }
    if (_bx) ray_release(_bx);
    return acc;
}

/* Typed prefix-scan kernels for the native arithmetic operators.  The
 * generic scan below unboxes the vector into a LIST and drives the full
 * call machinery per element — and, worse, RETURNS a LIST of boxed
 * atoms, so every downstream vector op on the running sums degrades to
 * the boxed paths too (the q28 20-row moving average spent ~13ms on
 * 45k rows this way).  For (scan + vec) / (scan * vec) over numeric
 * typed vectors, run one tight per-width loop and return a TYPED
 * vector.  Per-step semantics mirror ray_add_fn / ray_mul_fn exactly:
 * floats — plain IEEE ops, NaN (the F64 null) propagates and sticks;
 * ints — same-width wrap-truncate arithmetic (make_typed_int), null
 * sentinel propagation is sticky, and a wrap that lands on the
 * sentinel reads as null downstream exactly like the boxed path. */
static ray_t* scan_typed_arith(ray_t* fn, ray_t* vec) {
    if (fn->type != RAY_BINARY || !vec || !ray_is_vec(vec) || vec->len <= 0)
        return NULL;
    ray_binary_fn bf = (ray_binary_fn)(uintptr_t)fn->i64;
    bool is_add = (bf == ray_add_fn);
    if (!is_add && bf != ray_mul_fn) return NULL;
    int8_t t = vec->type;
    if (t != RAY_F64 && t != RAY_I64 && t != RAY_I32 && t != RAY_I16)
        return NULL;
    int64_t len = vec->len;
    ray_t* out = ray_vec_new(t, len);
    if (!out || RAY_IS_ERR(out)) { if (out) ray_release(out); return NULL; }
    out->len = len;
    /* Sentinel-bearing partials downstream: mirror the DAG convention —
     * advertise nulls when the source does. */
    out->attrs |= (vec->attrs & RAY_ATTR_HAS_NULLS);
    if (t == RAY_F64) {
        const double* restrict v = (const double*)ray_data(vec);
        double* restrict o = (double*)ray_data(out);
        double acc = v[0];
        o[0] = acc;
        if (is_add) for (int64_t i = 1; i < len; i++) { acc += v[i]; o[i] = acc; }
        else        for (int64_t i = 1; i < len; i++) { acc *= v[i]; o[i] = acc; }
        return out;
    }
    #define SCAN_INT_LOOP(CT, SENT)                                           \
        do {                                                                  \
            const CT* restrict v = (const CT*)ray_data(vec);                  \
            CT* restrict o = (CT*)ray_data(out);                              \
            CT acc = v[0];                                                    \
            o[0] = acc;                                                       \
            for (int64_t i = 1; i < len; i++) {                               \
                CT x = v[i];                                                  \
                if (acc == (SENT) || x == (SENT)) acc = (SENT);               \
                else acc = is_add                                             \
                    ? (CT)((int64_t)acc + (int64_t)x)                         \
                    : (CT)((int64_t)acc * (int64_t)x);                        \
                o[i] = acc;                                                   \
            }                                                                 \
        } while (0)
    if (t == RAY_I64)      SCAN_INT_LOOP(int64_t, NULL_I64);
    else if (t == RAY_I32) SCAN_INT_LOOP(int32_t, NULL_I32);
    else                   SCAN_INT_LOOP(int16_t, NULL_I16);
    #undef SCAN_INT_LOOP
    return out;
}

/* (prior fn vec) — pairwise combinator over adjacent elements:
 * out[i] = (fn v[i] v[i-1]) for i >= 1, out[0] = (fn v[0] v[0]) — the
 * self-pair start matches the concat/take shift idiom it replaces
 * (prev[0] = v[0]), so a comparison's first slot compares equal → false.
 * The six comparison operators over null-free
 * numeric/temporal typed vectors (plus ==//= over SYM, where adjacent
 * same-vector ids compare directly) run one fused pass returning a BOOL
 * vector — no shifted copy, no boxing.  Everything else falls to the
 * generic per-pair application returning a LIST, mirroring scan. */
static ray_t* prior_typed_cmp(ray_t* fn, ray_t* vec) {
    if (fn->type != RAY_BINARY || !vec || !ray_is_vec(vec) || vec->len <= 0)
        return NULL;
    if (ray_vec_may_have_nulls(vec)) return NULL;
    ray_binary_fn bf = (ray_binary_fn)(uintptr_t)fn->i64;
    int op;                                   /* 0:< 1:> 2:<= 3:>= 4:== 5:!= */
    if      (bf == ray_lt_fn)  op = 0;
    else if (bf == ray_gt_fn)  op = 1;
    else if (bf == ray_lte_fn) op = 2;
    else if (bf == ray_gte_fn) op = 3;
    else if (bf == ray_eq_fn)  op = 4;
    else if (bf == ray_neq_fn) op = 5;
    else return NULL;
    int8_t t = vec->type;
    bool is_sym = (t == RAY_SYM);
    if (is_sym && op != 4 && op != 5) return NULL;  /* SYM ordering → generic */
    if (!is_sym) switch (t) {
        case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
        case RAY_DATE: case RAY_TIME: case RAY_TIMESTAMP:
        case RAY_F64: case RAY_F32: break;
        default: return NULL;
    }
    int64_t n = vec->len;
    ray_t* out = ray_vec_new(RAY_BOOL, n);
    if (!out || RAY_IS_ERR(out)) { if (out) ray_release(out); return NULL; }
    out->len = n;
    uint8_t* restrict o = (uint8_t*)ray_data(out);
    #define PRIOR_CMP_LOOP(READ)                                          \
        do {                                                              \
            int64_t prev = READ(0);                                       \
            o[0] = (uint8_t)((op == 2) | (op == 3) | (op == 4));          \
            for (int64_t i = 1; i < n; i++) {                             \
                int64_t cur = READ(i);                                    \
                uint8_t r;                                                \
                switch (op) {                                             \
                    case 0: r = cur <  prev; break;                       \
                    case 1: r = cur >  prev; break;                       \
                    case 2: r = cur <= prev; break;                       \
                    case 3: r = cur >= prev; break;                       \
                    case 4: r = cur == prev; break;                       \
                    default: r = cur != prev; break;                      \
                }                                                         \
                o[i] = r;                                                 \
                prev = cur;                                               \
            }                                                             \
        } while (0)
    if (is_sym) {
        const void* d = ray_data(vec);
        uint8_t at = vec->attrs;
        #define RD_SYM(i) ((int64_t)ray_read_sym(d, (i), RAY_SYM, at))
        PRIOR_CMP_LOOP(RD_SYM);
        #undef RD_SYM
    } else if (t == RAY_F64 || t == RAY_F32) {
        /* Float compares must stay in the float domain (NaN, fractions). */
        const void* d = ray_data(vec);
        double prev = (t == RAY_F64) ? ((const double*)d)[0]
                                     : (double)((const float*)d)[0];
        o[0] = (uint8_t)((op == 2) | (op == 3) | (op == 4));
        /* NaN self-compare: <=/>=/== on a NaN first element is false —
         * overwrite the constant start for that case. */
        if (prev != prev) o[0] = (uint8_t)(op == 5);
        for (int64_t i = 1; i < n; i++) {
            double cur = (t == RAY_F64) ? ((const double*)d)[i]
                                        : (double)((const float*)d)[i];
            uint8_t r;
            switch (op) {
                case 0: r = cur <  prev; break;
                case 1: r = cur >  prev; break;
                case 2: r = cur <= prev; break;
                case 3: r = cur >= prev; break;
                case 4: r = cur == prev; break;
                default: r = cur != prev; break;
            }
            o[i] = r;
            prev = cur;
        }
    } else {
        const void* d = ray_data(vec);
        uint8_t at = vec->attrs;
        #define RD_I(i) read_col_i64(d, (i), t, at)
        PRIOR_CMP_LOOP(RD_I);
        #undef RD_I
    }
    #undef PRIOR_CMP_LOOP
    return out;
}

ray_t* ray_prior_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", "prior: requires fn and vec args, got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);
    ray_t* fn = args[0];
    {
        ray_t* fast = prior_typed_cmp(fn, args[1]);
        if (fast) return fast;
    }
    ray_t* _bx = NULL;
    ray_t* vec = unbox_vec_arg(args[1], &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "prior: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    ray_t* result = ray_alloc(len > 0 ? len * (int64_t)sizeof(ray_t*) : 0);
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = len;
    if (len == 0) { if (_bx) ray_release(_bx); return result; }
    ray_t** out = (ray_t**)ray_data(result);
    ray_t** elems = (ray_t**)ray_data(vec);
    for (int64_t i = 0; i < len; i++) {
        out[i] = call_fn2(fn, elems[i], elems[i > 0 ? i - 1 : 0]);
        if (RAY_IS_ERR(out[i])) {
            for (int64_t j = 0; j < i; j++) ray_release(out[j]);
            result->len = 0; ray_release(result); if (_bx) ray_release(_bx);
            return out[i];
        }
    }
    if (_bx) ray_release(_bx);
    return result;
}

/* (scan fn vec) — running fold, returns vector of partial results */
ray_t* ray_scan_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", "scan: requires at least 2 args (fn and vec), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* fn = args[0];
    {
        ray_t* fast = scan_typed_arith(fn, args[1]);
        if (fast) return fast;
    }
    ray_t* _bx = NULL;
    ray_t* vec = unbox_vec_arg(args[1], &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "scan: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    if (len == 0) {
        if (_bx) ray_release(_bx);
        ray_t* result = ray_alloc(0);
        if (!result) return ray_error("oom", NULL);
        result->type = RAY_LIST;
        result->len = 0;
        return result;
    }

    ray_t* result = ray_alloc(len * sizeof(ray_t*));
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = len;
    ray_t** out = (ray_t**)ray_data(result);
    ray_t** elems = (ray_t**)ray_data(vec);

    ray_retain(elems[0]);
    out[0] = elems[0];
    for (int64_t i = 1; i < len; i++) {
        out[i] = call_fn2(fn, out[i - 1], elems[i]);
        if (RAY_IS_ERR(out[i])) {
            for (int64_t j = 0; j < i; j++) ray_release(out[j]);
            result->len = 0; ray_release(result); if (_bx) ray_release(_bx);
            return out[i];
        }
    }
    if (_bx) ray_release(_bx);
    return result;
}

/* (filter vec mask) — filter vector by boolean mask */
ray_t* ray_filter_fn(ray_t* vec, ray_t* mask) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    if (ray_is_lazy(mask)) mask = ray_lazy_materialize(mask);

    /* Table filter: apply mask to each column */
    if (vec->type == RAY_TABLE && ray_is_vec(mask) && mask->type == RAY_BOOL) {
        int64_t ncols = ray_table_ncols(vec);
        int64_t nrows = ray_table_nrows(vec);
        if (nrows != mask->len) return ray_error("length", "filter: mask length must equal table row count %lld, got %lld", (long long)nrows, (long long)mask->len);
        ray_t* result = ray_table_new(ncols);
        if (RAY_IS_ERR(result)) return result;
        for (int64_t c = 0; c < ncols; c++) {
            int64_t cn = ray_table_col_name(vec, c);
            ray_t* src_col = ray_table_get_col_idx(vec, c);
            ray_t* filtered = ray_filter_fn(src_col, mask);
            if (RAY_IS_ERR(filtered)) { ray_release(result); return filtered; }
            result = ray_table_add_col(result, cn, filtered);
            ray_release(filtered);
            if (RAY_IS_ERR(result)) return result;
        }
        return result;
    }

    /* String filter: STR atom + bool mask → filter characters */
    if (ray_is_atom(vec) && (-vec->type) == RAY_STR && ray_is_vec(mask) && mask->type == RAY_BOOL) {
        const char* sp = ray_str_ptr(vec);
        size_t slen = ray_str_len(vec);
        int64_t mlen = mask->len;
        if ((int64_t)slen != mlen) return ray_error("length", "filter: mask length must equal string length %lld, got %lld", (long long)slen, (long long)mlen);
        bool* mb = (bool*)ray_data(mask);
        int64_t count = 0;
        for (int64_t i = 0; i < mlen; i++) if (mb[i]) count++;
        char buf[8192];
        if ((size_t)count > sizeof(buf)) return ray_error("limit", NULL);
        int64_t j = 0;
        for (int64_t i = 0; i < mlen; i++) {
            if (mb[i]) buf[j++] = sp[i];
        }
        return ray_str(buf, (size_t)count);
    }

    /* Fast path: typed vector + typed bool mask */
    if (ray_is_vec(vec) && ray_is_vec(mask) && mask->type == RAY_BOOL) {
        int64_t len = vec->len;
        int64_t mlen = mask->len;
        if (len != mlen) return ray_error("length", "filter: mask length must equal vec length %lld, got %lld", (long long)len, (long long)mlen);
        bool* mb = (bool*)ray_data(mask);

        /* Count true values */
        int64_t count = 0;
        for (int64_t i = 0; i < len; i++) if (mb[i]) count++;

        int8_t vtype = vec->type;
        int esz = ray_elem_size(vtype);
        ray_t* result = ray_vec_new(vtype, count);
        if (RAY_IS_ERR(result)) return result;
        result->len = count;
        char* src = (char*)ray_data(vec);
        char* dst = (char*)ray_data(result);
        int64_t j = 0;
        for (int64_t i = 0; i < len; i++) {
            if (mb[i]) {
                memcpy(dst + j * esz, src + i * esz, esz);
                if (ray_vec_is_null(vec, i))
                    ray_vec_set_null(result, j, true);
                j++;
            }
        }
        return result;
    }

    /* Fallback: boxed list path */
    ray_t *_bx1 = NULL, *_bx2 = NULL;
    vec = unbox_vec_arg(vec, &_bx1);
    if (RAY_IS_ERR(vec)) return vec;
    mask = unbox_vec_arg(mask, &_bx2);
    if (RAY_IS_ERR(mask)) { if (_bx1) ray_release(_bx1); return mask; }
    if (!is_list(vec) || !is_list(mask)) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("type", "filter: vec and mask must be lists, got %s and %s", ray_type_name(vec->type), ray_type_name(mask->type)); }
    int64_t len = ray_len(vec);
    int64_t mlen = ray_len(mask);
    if (len != mlen) return ray_error("length", "filter: mask length must equal vec length %lld, got %lld", (long long)len, (long long)mlen);

    ray_t** velems = (ray_t**)ray_data(vec);
    ray_t** melems = (ray_t**)ray_data(mask);

    /* Validate mask is all booleans */
    for (int64_t i = 0; i < len; i++) {
        if (melems[i]->type != -RAY_BOOL) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("type", "filter: mask elements must be bool, got %s", ray_type_name(melems[i]->type)); }
    }
    /* Count true values */
    int64_t count = 0;
    for (int64_t i = 0; i < len; i++) {
        if (melems[i]->b8) count++;
    }

    ray_t* result = ray_alloc(count * sizeof(ray_t*));
    if (!result) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = count;
    ray_t** out = (ray_t**)ray_data(result);
    int64_t j = 0;
    for (int64_t i = 0; i < len; i++) {
        if (melems[i]->b8) {
            ray_retain(velems[i]);
            out[j++] = velems[i];
        }
    }
    if (_bx1) ray_release(_bx1);
    if (_bx2) ray_release(_bx2);
    return result;
}

/* (apply fn vec1 vec2) — zip-apply fn element-wise over two vectors */
ray_t* ray_apply_fn(ray_t** args, int64_t n) {
    if (n < 3) return ray_error("domain", "apply: requires at least 3 args (fn and two vecs), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* fn = args[0];

    /* If both args are scalars, just call fn(a, b) once */
    if (ray_is_atom(args[1]) && ray_is_atom(args[2]))
        return call_fn2(fn, args[1], args[2]);

    ray_t *_bx1 = NULL, *_bx2 = NULL;
    ray_t* vec1 = unbox_vec_arg(args[1], &_bx1);
    if (RAY_IS_ERR(vec1)) return vec1;
    ray_t* vec2 = unbox_vec_arg(args[2], &_bx2);
    if (RAY_IS_ERR(vec2)) { if (_bx1) ray_release(_bx1); return vec2; }
    if (!is_list(vec1) || !is_list(vec2)) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("type", "apply: both vec args must be lists, got %s and %s", ray_type_name(vec1->type), ray_type_name(vec2->type)); }
    int64_t len1 = ray_len(vec1);
    int64_t len2 = ray_len(vec2);
    int64_t len = len1 < len2 ? len1 : len2;

    ray_t* result = ray_alloc(len * sizeof(ray_t*));
    if (!result) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = len;
    ray_t** out = (ray_t**)ray_data(result);
    ray_t** e1 = (ray_t**)ray_data(vec1);
    ray_t** e2 = (ray_t**)ray_data(vec2);

    for (int64_t i = 0; i < len; i++) {
        out[i] = call_fn2(fn, e1[i], e2[i]);
        if (RAY_IS_ERR(out[i])) {
            for (int64_t j = 0; j < i; j++) ray_release(out[j]);
            result->len = 0; ray_release(result); if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2);
            return out[i];
        }
    }
    if (_bx1) ray_release(_bx1);
    if (_bx2) ray_release(_bx2);
    return result;
}

/* ══════════════════════════════════════════
 * Collection operations
 * ══════════════════════════════════════════ */

/* Helper: compare two atoms for equality (value-based) */
int atom_eq(ray_t* a, ray_t* b) {
    int a_null = RAY_ATOM_IS_NULL(a);
    int b_null = RAY_ATOM_IS_NULL(b);
    if (a_null && b_null) return 1;
    if (a_null || b_null) return 0;
    if (a->type != b->type) {
        if (is_numeric(a) && is_numeric(b))
            return as_f64(a) == as_f64(b);
        return 0;
    }
    switch (a->type) {
    case -RAY_I64:  return a->i64 == b->i64;
    case -RAY_I32:  return a->i32 == b->i32;
    case -RAY_I16:  return a->i16 == b->i16;
    case -RAY_U8:   return a->u8 == b->u8;
    case -RAY_F64:  return a->f64 == b->f64;
    case -RAY_F32:  return (float)a->f64 == (float)b->f64;
    case -RAY_BOOL: return a->b8 == b->b8;
    case -RAY_SYM:  return a->i64 == b->i64;
    case -RAY_DATE: case -RAY_TIME:
        return a->i32 == b->i32;
    case -RAY_TIMESTAMP:
        return a->i64 == b->i64;
    case -RAY_GUID: {
        const uint8_t* ga = a->obj ? (const uint8_t*)ray_data(a->obj) : (const uint8_t*)ray_data((ray_t*)a);
        const uint8_t* gb = b->obj ? (const uint8_t*)ray_data(b->obj) : (const uint8_t*)ray_data((ray_t*)b);
        return memcmp(ga, gb, 16) == 0;
    }
    case -RAY_STR:
        return ray_str_len(a) == ray_str_len(b) &&
               memcmp(ray_str_ptr(a), ray_str_ptr(b), ray_str_len(a)) == 0;
    case RAY_LIST: {
        /* Structural compare: lists are equal iff same length AND every
         * pair of elements is atom_eq.  Without this, two structurally-
         * identical lists with different element pointers compared via
         * the default branch's memcmp on ray_t** — i.e. pointer
         * identity, never structurally equal — which broke (group LIST)
         * (every row its own bucket) and dict/distinct fallbacks. */
        if (a->len != b->len) return 0;
        ray_t** ea = (ray_t**)ray_data(a);
        ray_t** eb = (ray_t**)ray_data(b);
        for (int64_t i = 0; i < a->len; i++) {
            if (!ea[i] || !eb[i]) {
                if (ea[i] != eb[i]) return 0;
                continue;
            }
            if (!atom_eq(ea[i], eb[i])) return 0;
        }
        return 1;
    }
    default:
        /* Vector equality: same type and length, element-wise comparison */
        if (a->type > 0 && a->type == b->type && a->len == b->len) {
            int esz = ray_elem_size(a->type);
            return memcmp(ray_data(a), ray_data(b), (size_t)(a->len * esz)) == 0;
        }
        return 0;
    }
}

/* Forward declaration */
ray_t* list_to_typed_vec(ray_t* list, int8_t orig_vec_type);

/* Eager vector dedup — called by the DAG executor's OP_DISTINCT case.
 * Factored out so the executor doesn't go through ray_distinct_fn, which
 * is now a lazy producer for vectors and would re-wrap into a chain. */
ray_t* distinct_vec_eager(ray_t* x) {
    int64_t len = ray_len(x);
    if (len == 0) { ray_retain(x); return x; }

    /* Fast path: when a fresh, null-free sort index is attached, walk the
     * permutation once to collect the first-in-run row ids.  The walk
     * produces ids in value-ascending order (matching the hashset path's
     * distinct_sort_indices contract) in O(n) with no hash table.
     * Guard on ray_index_has to avoid a consult bump when no index exists
     * (mirrors the find fast-path's !has_nulls && ray_index_has gate). */
    if (x->type != RAY_SYM && x->type != RAY_GUID && x->type != RAY_STR
            && ray_index_has(x)) {
        ray_idx_consults[IDX_SITE_DISTINCT]++;
        int64_t count = 0;
        ray_t* ids = ray_index_distinct_ids(x, &count);
        if (ids) {
            ray_idx_hits[IDX_SITE_DISTINCT]++;
            ray_t* result = gather_by_idx(x, (int64_t*)ray_data(ids), count);
            ray_release(ids);
            return result;
        }
    }

    int64_t idx_stack[256];
    int64_t* idx = (len <= 256) ? idx_stack : (int64_t*)ray_alloc_raw((size_t)len * sizeof(int64_t));
    if (!idx) return ray_error("oom", NULL);

    /* SYM presence-bitmap dedup: a SYM code is a position in [0, domain_count),
     * so for a small domain a domain_count-byte "seen" array collects the
     * first-occurrence row ids in O(n) with NO hashing — identical first-
     * appearance order to the hashset path below.  Gate dc<=len so the array
     * never costs more than the data it replaces; larger domains fall through. */
    if (x->type == RAY_SYM) {
        int64_t dc = ray_sym_domain_count(ray_sym_vec_domain(x));
        if (dc > 0 && dc <= 65536 && dc <= len) {
            uint8_t* seen = (uint8_t*)ray_calloc_raw((size_t)dc);
            if (!seen) { if (idx != idx_stack) ray_free_raw(idx); return ray_error("oom", NULL); }
            const void* data = ray_data(x);
            int8_t ty = x->type; uint16_t at = x->attrs;
            int64_t count = 0;
            for (int64_t i = 0; i < len; i++) {
                int64_t code = ray_read_sym(data, i, ty, at);
                if (!seen[code]) { seen[code] = 1; idx[count++] = i; }
            }
            ray_free_raw(seen);
            ray_t* result = gather_by_idx(x, idx, count);
            if (idx != idx_stack) ray_free_raw(idx);
            return result;
        }
    }

    /* Dict-encoded STR: dedup on the int32 dict-code vector (codes in
     * [0, n_distinct)) via a presence array — first-occurrence order, no
     * string hashing.  Mirrors the SYM presence path; gated nd<=len so the
     * seen[] array never costs more than the data it replaces. */
    if (x->type == RAY_STR && ray_index_kind(x) == RAY_IDX_DICT) {
        ray_index_t* dix = ray_index_payload(x->index);
        int64_t nd = dix->u.dict.n_distinct;
        ray_t* codes_v = dix->u.dict.codes;
        if (nd > 0 && nd <= len && codes_v && !RAY_IS_ERR(codes_v)
            && codes_v->len == len) {
            const int32_t* codes = (const int32_t*)ray_data(codes_v);
            uint8_t* seen = (uint8_t*)ray_calloc_raw((size_t)nd);
            if (!seen) { if (idx != idx_stack) ray_free_raw(idx); return ray_error("oom", NULL); }
            int64_t count = 0;
            for (int64_t i = 0; i < len; i++) {
                int32_t c = codes[i];
                if (c >= 0 && c < nd && !seen[c]) { seen[c] = 1; idx[count++] = i; }
            }
            ray_free_raw(seen);
            ray_t* result = gather_by_idx(x, idx, count);
            if (idx != idx_stack) ray_free_raw(idx);
            return result;
        }
    }

    /* Parallel radix kernel for large fixed-width columns — same
     * partition scheme as exec_count_distinct, producing the same row-id
     * set the hashset pass below would (one first-occurrence id per
     * value).  NULL means the kernel declined (type, size, or pool) and
     * the hashset handles it.  Either way the ids flow into the shared
     * sort + gather tail, so result semantics are identical. */
    int64_t count = 0;
    ray_t* rids = distinct_radix_first_ids(x);
    if (rids && RAY_IS_ERR(rids)) {
        if (idx != idx_stack) ray_free_raw(idx);
        return rids;
    }
    if (rids) {
        count = ray_len(rids);
        memcpy(idx, ray_data(rids), (size_t)count * sizeof(int64_t));
        ray_release(rids);
    } else {
        hashset_t hs;
        if (!hashset_init(&hs, x, len)) {
            if (idx != idx_stack) ray_free_raw(idx);
            return ray_error("oom", NULL);
        }
        for (int64_t i = 0; i < len; i++) {
            if (hashset_insert(&hs, i)) idx[count++] = i;
        }
        hashset_destroy(&hs);
    }

    /* Sort unique indices by value for numeric/temporal types — preserves
     * pre-existing distinct semantics.  qsort-based; was O(count^2). */
    if (x->type != RAY_SYM && x->type != RAY_GUID && x->type != RAY_STR && count > 1) {
        distinct_sort_indices(x, idx, count);
    }

    ray_t* result = gather_by_idx(x, idx, count);
    if (idx != idx_stack) ray_free_raw(idx);
    return result;
}

static ray_t* parted_to_flat_vec(ray_t* x) {
    if (!x || !RAY_IS_PARTED(x->type)) return ray_error("type", NULL);
    int8_t base = (int8_t)RAY_PARTED_BASETYPE(x->type);
    ray_t** segs = (ray_t**)ray_data(x);
    int64_t total = 0;
    for (int64_t s = 0; s < x->len; s++)
        if (segs[s]) total += segs[s]->len;

    /* RAY_LIST: share (retain) elements — ray_vec_new rejects type 0 and
     * the per-cell copy below has no notion of nested elements. */
    if (base == RAY_LIST)
        return parted_flatten_list(segs, x->len, total);

    ray_t* out = ray_vec_new(base, base == RAY_STR ? 0 : total);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    if (base != RAY_STR) out->len = total;

    int64_t pos = 0;
    for (int64_t s = 0; s < x->len; s++) {
        ray_t* seg = segs[s];
        if (!seg) continue;
        for (int64_t i = 0; i < seg->len; i++) {
            if (base == RAY_STR) {
                size_t slen = 0;
                const char* sp = ray_str_vec_get(seg, i, &slen);
                out = ray_str_vec_append(out, sp ? sp : "", sp ? slen : 0);
                if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
                if (ray_vec_is_null(seg, i)) ray_vec_set_null(out, pos, true);
            } else {
                int allocated = 0;
                ray_t* elem = collection_elem(seg, i, &allocated);
                if (!elem || RAY_IS_ERR(elem) || store_typed_elem(out, pos, elem) != 0) {
                    if (allocated && elem && !RAY_IS_ERR(elem)) ray_release(elem);
                    ray_release(out);
                    return elem && RAY_IS_ERR(elem) ? elem : ray_error("type", NULL);
                }
                if (allocated) ray_release(elem);
            }
            pos++;
        }
    }
    return out;
}

/* (distinct x) — remove duplicates. Dispatches on type:
 *   table → deduplicate rows (via DAG GROUP with zero aggs)
 *   vector → remove duplicate elements, preserving first occurrence
 *   string → unique chars, sorted */
ray_t* ray_distinct_fn(ray_t* x) {
    /* Extend an existing lazy chain — must come before the lazy-materialise
     * guard so the chain is preserved rather than collapsed. */
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_DISTINCT);

    /* Table distinct: dispatch to table-specific implementation */
    if (x->type == RAY_TABLE)
        return ray_table_distinct_fn(x);

    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = distinct_vec_eager(flat);
        ray_release(flat);
        return out;
    }

    /* String distinct: unique chars, sorted */
    if (ray_is_atom(x) && (-x->type) == RAY_STR) {
        const char* sp = ray_str_ptr(x);
        size_t slen = ray_str_len(x);
        if (slen == 0) { ray_retain(x); return x; }
        char uniq[256];
        int nu = 0;
        for (size_t i = 0; i < slen; i++) {
            int dup = 0;
            for (int j = 0; j < nu; j++) { if (uniq[j] == sp[i]) { dup = 1; break; } }
            if (!dup && nu < 256) uniq[nu++] = sp[i];
        }
        /* Sort */
        for (int i = 0; i < nu - 1; i++)
            for (int j = i + 1; j < nu; j++)
                if ((unsigned char)uniq[i] > (unsigned char)uniq[j]) { char t = uniq[i]; uniq[i] = uniq[j]; uniq[j] = t; }
        return ray_str(uniq, (size_t)nu);
    }

    /* Typed vector path: start a fresh lazy chain.  The DAG executor will
     * call distinct_vec_eager() when the chain is materialised. */
    if (ray_is_vec(x)) {
        ray_graph_t* g = ray_graph_new(NULL);
        if (!g) return ray_error("oom", NULL);
        ray_op_t* in = ray_graph_input_vec(g, x);
        ray_op_t* op = ray_distinct_op(g, in);
        return ray_lazy_wrap(g, op);
    }

    ray_t* _bx = NULL;
    x = unbox_vec_arg(x, &_bx);
    if (RAY_IS_ERR(x)) return x;
    if (!is_list(x)) { if (_bx) ray_release(_bx); return ray_error("type", "distinct: argument must be a list, got %s", ray_type_name(x->type)); }
    int64_t len = ray_len(x);
    if (len == 0) { if (_bx) ray_release(_bx); ray_retain(x); return x; }
    ray_t** elems = (ray_t**)ray_data(x);

    ray_t* result = ray_alloc(len * sizeof(ray_t*));
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    ray_t** out = (ray_t**)ray_data(result);
    int64_t count = 0;

    for (int64_t i = 0; i < len; i++) {
        int dup = 0;
        for (int64_t j = 0; j < count; j++) {
            if (atom_eq(out[j], elems[i])) { dup = 1; break; }
        }
        if (!dup) {
            ray_retain(elems[i]);
            out[count++] = elems[i];
        }
    }
    result->len = count;
    /* Sort: atoms before vectors (scalars have negative type) */
    for (int64_t i = 0; i < count - 1; i++) {
        for (int64_t j = i + 1; j < count; j++) {
            int ai = ray_is_atom(out[i]);
            int aj = ray_is_atom(out[j]);
            if (!ai && aj) {
                ray_t* tmp = out[i]; out[i] = out[j]; out[j] = tmp;
            }
        }
    }
    if (_bx) ray_release(_bx);
    return result;
}

/* (in val vec) — check membership */
ray_t* ray_in_fn(ray_t* val, ray_t* vec) {
    if (ray_is_lazy(val)) val = ray_lazy_materialize(val);
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    /* Parted columns store segment pointers where vector paths expect flat
     * cells.  Flatten both operands before membership; distinct uses the same
     * helper, while except/sect still reject parted input. */
    ray_t* flat_val = NULL;
    ray_t* flat_vec = NULL;
    if (val && RAY_IS_PARTED(val->type)) {
        flat_val = parted_to_flat_vec(val);
        if (!flat_val || RAY_IS_ERR(flat_val)) return flat_val ? flat_val : ray_error("oom", NULL);
        val = flat_val;
    }
    if (vec && RAY_IS_PARTED(vec->type)) {
        flat_vec = parted_to_flat_vec(vec);
        if (!flat_vec || RAY_IS_ERR(flat_vec)) {
            if (flat_val) ray_release(flat_val);
            return flat_vec ? flat_vec : ray_error("oom", NULL);
        }
        vec = flat_vec;
    }
    if (flat_val || flat_vec) {
        ray_t* r = ray_in_fn(val, vec);
        if (flat_val) ray_release(flat_val);
        if (flat_vec) ray_release(flat_vec);
        return r;
    }
    /* STR in STR: for each char of val, check membership in vec string */
    if (ray_is_atom(val) && (-val->type) == RAY_STR && ray_is_atom(vec) && (-vec->type) == RAY_STR) {
        const char* vp = ray_str_ptr(val);
        size_t vlen = ray_str_len(val);
        const char* sp = ray_str_ptr(vec);
        size_t slen = ray_str_len(vec);
        ray_t* result = ray_vec_new(RAY_BOOL, (int64_t)vlen);
        if (RAY_IS_ERR(result)) return result;
        result->len = (int64_t)vlen;
        bool* out = (bool*)ray_data(result);
        for (size_t i = 0; i < vlen; i++) {
            out[i] = false;
            for (size_t j = 0; j < slen; j++) {
                if (vp[i] == sp[j]) { out[i] = true; break; }
            }
        }
        return result;
    }
    /* Scalar in scalar: equality check */
    if (ray_is_atom(val) && ray_is_atom(vec))
        return make_bool(atom_eq(val, vec) ? 1 : 0);
    /* STR in LIST: for each char of val, check membership in list elements */
    if (ray_is_atom(val) && (-val->type) == RAY_STR && (vec->type == RAY_LIST)) {
        const char* vp = ray_str_ptr(val);
        size_t vlen = ray_str_len(val);
        ray_t* result = ray_vec_new(RAY_BOOL, (int64_t)vlen);
        if (RAY_IS_ERR(result)) return result;
        result->len = (int64_t)vlen;
        bool* out_b = (bool*)ray_data(result);
        ray_t** list_elems = (ray_t**)ray_data(vec);
        int64_t list_len = ray_len(vec);
        for (size_t i = 0; i < vlen; i++) {
            out_b[i] = false;
            ray_t* ch = ray_str(&vp[i], 1);
            for (int64_t j = 0; j < list_len; j++) {
                if (atom_eq(ch, list_elems[j])) { out_b[i] = true; break; }
            }
            ray_release(ch);
        }
        return result;
    }
    /* Vector val: map in over each element */
    if (is_collection(val) && !ray_is_atom(val)) {
        int64_t vlen = ray_len(val);
        if (vlen == 0) {
            /* Empty collection → return empty list */
            return ray_list_new(0);
        }
        /* Hash-based fast path: both sides are typed vecs of compatible
         * shape and the result is a row-aligned bool vec.  Build a
         * hashset over `vec` once, probe per element of `val`.  Was
         * O(len(val)×len(vec)); now O(len(val)+len(vec)). */
        if (ray_is_vec(val) && ray_is_vec(vec)) {
            /* Attached hash index on the set: one probe per needle, nothing
             * built per call.  Null needles miss (the index is consulted on
             * null-free sets only), matching the hashset path below. */
            if (ray_index_has(vec)) {
                ray_idx_consults[IDX_SITE_IN]++;
                int64_t* pos = (int64_t*)ray_calloc_raw((size_t)vlen * sizeof(int64_t));
                if (!pos) return ray_error("oom", NULL);
                bool miss = false;
                if (ray_index_find_vec(vec, val, pos, &miss)) {
                    ray_idx_hits[IDX_SITE_IN]++;
                    ray_t* result = ray_vec_new(RAY_BOOL, vlen);
                    if (RAY_IS_ERR(result)) { ray_free_raw(pos); return result; }
                    result->len = vlen;
                    bool* out = (bool*)ray_data(result);
                    for (int64_t i = 0; i < vlen; i++) out[i] = pos[i] != NULL_I64;
                    ray_free_raw(pos);
                    return result;
                }
                ray_free_raw(pos);
            }
            /* Typed kernel first: verdict-LUT for SYM, SIMD small-set for
             * ints, pool-parallel — the same engine the fused WHERE path
             * uses.  Gated off null-bearing operands: this hashset path
             * matches null ∈ {…null…} as TRUE (null equals null), while
             * the WHERE kernel uses null-matches-nothing semantics — the
             * two must not be conflated.  NULL result = unsupported shape
             * (STR etc.): fall through to the hashset probe below. */
            /* Admission check, so it uses the EXACT ray_vec_has_nulls rather
             * than the conservative row-kernel gate.  ray_vec_may_have_nulls
             * returns true unconditionally for SYM and STR — their null is a
             * payload value, not an attribute bit — so gating this on it made
             * the branch unreachable for every text column, including the SYM
             * verdict-LUT the kernel carries specifically for them.  Every
             * `in` over a symbol column fell through to the generic per-row
             * hashset probe: ~4.8 ms against ~40 us for the equivalent `==`
             * on a 351k-row column (#593).  vec.h says as much where it
             * defines the two — "paths requiring null-free data use has_nulls
             * below".  The exact check costs one pass and is not on a per-row
             * path.
             *
             * Only BOTH sides null-bearing must fall through.  The kernel's
             * null-matches-nothing and this path's null-equals-null disagree
             * on exactly one question — does a null row match a null set
             * element — and that needs a null on each side.  A null row
             * against a null-free set, or a null set element against a
             * null-free column, is false under both.  Requiring both sides
             * null-free sent any nullable column to the hashset: one null
             * row cost ~25x.  The set is tested first: it is usually the
             * small side, and a null-free set skips the column scan. */
            if (!(ray_vec_has_nulls(vec) && ray_vec_has_nulls(val))) {
                ray_t* fast = ray_in_vec_exec(val, vec, false);
                if (fast) return fast;
            }
            ray_t* result = ray_vec_new(RAY_BOOL, vlen);
            if (RAY_IS_ERR(result)) return result;
            result->len = vlen;
            bool* out = (bool*)ray_data(result);
            /* Fewer needles than set rows: hash the needles and scan the
             * set once instead of hashing the whole set per call. */
            if (find_needle_side_ok(vec, val)) {
                int64_t* pos = (int64_t*)ray_calloc_raw((size_t)vlen * sizeof(int64_t));
                bool miss = false;
                if (pos && find_hash_needles(vec, val, pos, &miss)) {
                    for (int64_t i = 0; i < vlen; i++) out[i] = pos[i] != NULL_I64;
                    ray_free_raw(pos);
                    return result;
                }
                if (pos) ray_free_raw(pos);
            }
            hashset_t hs;
            if (!hashset_init(&hs, vec, vec->len)) {
                ray_release(result);
                return ray_error("oom", NULL);
            }
            for (int64_t j = 0; j < vec->len; j++) hashset_insert(&hs, j);
            int8_t val_type = val->type;
            void*  val_data = ray_data(val);
            for (int64_t i = 0; i < vlen; i++) {
                out[i] = hashset_find_xrow(&hs, val, i, val_type, val_data) != HS_EMPTY;
            }
            hashset_destroy(&hs);
            return result;
        }
        /* Probe first element to check if result is scalar or vector */
        int alloc0 = 0;
        ray_t* e0 = collection_elem(val, 0, &alloc0);
        if (RAY_IS_ERR(e0)) return e0;
        ray_t* r0 = ray_in_fn(e0, vec);
        if (alloc0) ray_release(e0);
        if (RAY_IS_ERR(r0)) return r0;
        if (ray_is_atom(r0) && r0->type == -RAY_BOOL) {
            /* All results are scalar bools — use typed bool vector */
            ray_t* result = ray_vec_new(RAY_BOOL, vlen);
            if (RAY_IS_ERR(result)) { ray_release(r0); return result; }
            result->len = vlen;
            bool* out = (bool*)ray_data(result);
            out[0] = r0->b8;
            ray_release(r0);
            for (int64_t i = 1; i < vlen; i++) {
                int alloc = 0;
                ray_t* elem = collection_elem(val, i, &alloc);
                if (RAY_IS_ERR(elem)) { ray_release(result); return elem; }
                ray_t* r = ray_in_fn(elem, vec);
                if (alloc) ray_release(elem);
                if (RAY_IS_ERR(r)) { ray_release(result); return r; }
                out[i] = r->b8;
                ray_release(r);
            }
            return result;
        } else {
            /* Results are non-scalar — collect as list */
            ray_t* result = ray_list_new(vlen);
            if (RAY_IS_ERR(result)) { ray_release(r0); return result; }
            result = ray_list_append(result, r0);
            ray_release(r0);
            if (RAY_IS_ERR(result)) return result;
            for (int64_t i = 1; i < vlen; i++) {
                int alloc = 0;
                ray_t* elem = collection_elem(val, i, &alloc);
                if (RAY_IS_ERR(elem)) { ray_release(result); return elem; }
                ray_t* r = ray_in_fn(elem, vec);
                if (alloc) ray_release(elem);
                if (RAY_IS_ERR(r)) { ray_release(result); return r; }
                result = ray_list_append(result, r);
                ray_release(r);
                if (RAY_IS_ERR(result)) return result;
            }
            return result;
        }
    }
    /* Typed vector: search without boxing */
    if (ray_is_vec(vec) && ray_is_atom(val)) {
        int64_t len = vec->len;
        bool has_nulls = ray_vec_may_have_nulls(vec);
        bool val_null = RAY_ATOM_IS_NULL(val);
        if (has_nulls) {
            for (int64_t i = 0; i < len; i++) {
                if (ray_vec_is_null(vec, i)) {
                    if (val_null) return make_bool(1);
                    continue;
                }
                int alloc = 0;
                ray_t* elem = collection_elem(vec, i, &alloc);
                int eq = atom_eq(val, elem);
                if (alloc) ray_release(elem);
                if (eq) return make_bool(1);
            }
        } else {
            if (val_null) return make_bool(0); /* no nulls in vec, null val → no match */
            for (int64_t i = 0; i < len; i++) {
                int alloc = 0;
                ray_t* elem = collection_elem(vec, i, &alloc);
                int eq = atom_eq(val, elem);
                if (alloc) ray_release(elem);
                if (eq) return make_bool(1);
            }
        }
        return make_bool(0);
    }
    ray_t* _bx = NULL;
    vec = unbox_vec_arg(vec, &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "in: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    ray_t** elems = (ray_t**)ray_data(vec);
    for (int64_t i = 0; i < len; i++) {
        if (atom_eq(val, elems[i])) { if (_bx) ray_release(_bx); return make_bool(1); }
    }
    if (_bx) ray_release(_bx);
    return make_bool(0);
}

/* Helper: convert a boxed list result back to a typed vector if the original was typed */
ray_t* list_to_typed_vec(ray_t* list, int8_t orig_vec_type) {
    if (!list || RAY_IS_ERR(list) || list->type != RAY_LIST) return list;
    int64_t count = list->len;
    /* For SYM and STR types, only convert when empty (to get [] instead of ()) */
    if (orig_vec_type == RAY_SYM || orig_vec_type == RAY_STR) {
        if (count == 0) {
            ray_release(list);
            return ray_vec_new(orig_vec_type, 0);
        }
        return list; /* Keep as boxed list for non-empty SYM/STR */
    }
    ray_t* vec = ray_vec_new(orig_vec_type, count);
    if (RAY_IS_ERR(vec)) return vec;
    vec->len = count;
    ray_t** elems = (ray_t**)ray_data(list);
    for (int64_t i = 0; i < count; i++)
        store_typed_elem(vec, i, elems[i]);
    /* Release the list (ray_release_owned_refs handles child elements) */
    ray_release(list);
    return vec;
}

/* (vec_elem_in helper removed — replaced by hashset-based lookups in
 * except/union/sect/in/distinct.) */

/* (except vec1 vec2) — elements in vec1 not in vec2 */
ray_t* ray_except_fn(ray_t* vec1, ray_t* vec2) {
    if (ray_is_lazy(vec1)) vec1 = ray_lazy_materialize(vec1);
    if (ray_is_lazy(vec2)) vec2 = ray_lazy_materialize(vec2);

    /* Typed vector path: hash-based.  Was O(len1×len2); now O(len1+len2). */
    if (ray_is_vec(vec1) && (ray_is_vec(vec2) || ray_is_atom(vec2))) {
        int64_t len1 = vec1->len;
        int64_t idx_stack[256];
        int64_t* idx = (len1 <= 256) ? idx_stack : (int64_t*)ray_alloc_raw((size_t)len1 * sizeof(int64_t));
        if (!idx) return ray_error("oom", NULL);
        int64_t count = 0;
        if (ray_is_atom(vec2)) {
            /* Scalar: filter out matching elements (single compare per row). */
            for (int64_t i = 0; i < len1; i++) {
                int alloc = 0;
                ray_t* elem = collection_elem(vec1, i, &alloc);
                int eq = atom_eq(elem, vec2);
                if (alloc) ray_release(elem);
                if (!eq) idx[count++] = i;
            }
        } else {
            hashset_t hs;
            if (!hashset_init(&hs, vec2, vec2->len)) {
                if (idx != idx_stack) ray_free_raw(idx);
                return ray_error("oom", NULL);
            }
            for (int64_t j = 0; j < vec2->len; j++) hashset_insert(&hs, j);
            int8_t v1_type = vec1->type;
            void*  v1_data = ray_data(vec1);
            for (int64_t i = 0; i < len1; i++) {
                if (hashset_find_xrow(&hs, vec1, i, v1_type, v1_data) == HS_EMPTY)
                    idx[count++] = i;
            }
            hashset_destroy(&hs);
        }
        ray_t* result = gather_by_idx(vec1, idx, count);
        if (idx != idx_stack) ray_free_raw(idx);
        return result;
    }

    /* Boxed list fallback */
    int8_t orig_type = ray_is_vec(vec1) ? vec1->type : -1;
    ray_t *_bx1 = NULL, *_bx2 = NULL;
    vec1 = unbox_vec_arg(vec1, &_bx1);
    if (RAY_IS_ERR(vec1)) return vec1;
    vec2 = unbox_vec_arg(vec2, &_bx2);
    if (RAY_IS_ERR(vec2)) { if (_bx1) ray_release(_bx1); return vec2; }
    if (!is_list(vec1)) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("type", "except: first arg must be a list, got %s", ray_type_name(vec1->type)); }
    int64_t len1 = ray_len(vec1);
    ray_t** e1 = (ray_t**)ray_data(vec1);

    ray_t* result = ray_alloc(len1 * sizeof(ray_t*));
    if (!result) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    ray_t** out = (ray_t**)ray_data(result);
    int64_t count = 0;

    if (ray_is_atom(vec2)) {
        for (int64_t i = 0; i < len1; i++) {
            if (!atom_eq(e1[i], vec2)) { ray_retain(e1[i]); out[count++] = e1[i]; }
        }
    } else {
        int64_t len2 = ray_len(vec2);
        ray_t** e2 = (ray_t**)ray_data(vec2);
        for (int64_t i = 0; i < len1; i++) {
            int found = 0;
            for (int64_t j = 0; j < len2; j++) {
                if (atom_eq(e1[i], e2[j])) { found = 1; break; }
            }
            if (!found) { ray_retain(e1[i]); out[count++] = e1[i]; }
        }
    }
    result->len = count;
    if (_bx1) ray_release(_bx1);
    if (_bx2) ray_release(_bx2);
    if (orig_type >= 0 && count == 0) { ray_release(result); return ray_vec_new(orig_type, 0); }
    return result;
}

/* (union vec1 vec2) — elements in vec1 + elements in vec2 not already in vec1 */
ray_t* ray_union_fn(ray_t* vec1, ray_t* vec2) {
    if (ray_is_lazy(vec1)) vec1 = ray_lazy_materialize(vec1);
    if (ray_is_lazy(vec2)) vec2 = ray_lazy_materialize(vec2);

    /* Typed vector path: hash-based.  Was O(len1×len2); now O(len1+len2). */
    if (ray_is_vec(vec1) && ray_is_vec(vec2)) {
        int64_t len2 = vec2->len;
        int64_t idx_stack[256];
        int64_t* idx = (len2 <= 256) ? idx_stack : (int64_t*)ray_alloc_raw((size_t)len2 * sizeof(int64_t));
        if (!idx) return ray_error("oom", NULL);
        hashset_t hs;
        if (!hashset_init(&hs, vec1, vec1->len)) {
            if (idx != idx_stack) ray_free_raw(idx);
            return ray_error("oom", NULL);
        }
        for (int64_t j = 0; j < vec1->len; j++) hashset_insert(&hs, j);
        int8_t v2_type = vec2->type;
        void*  v2_data = ray_data(vec2);
        int64_t extra = 0;
        for (int64_t i = 0; i < len2; i++) {
            if (hashset_find_xrow(&hs, vec2, i, v2_type, v2_data) == HS_EMPTY)
                idx[extra++] = i;
        }
        hashset_destroy(&hs);
        ray_t* part2 = gather_by_idx(vec2, idx, extra);
        if (idx != idx_stack) ray_free_raw(idx);
        if (RAY_IS_ERR(part2)) return part2;
        ray_t* result = ray_concat_fn(vec1, part2);
        ray_release(part2);
        return result;
    }

    /* Boxed list fallback */
    ray_t *_bx1 = NULL, *_bx2 = NULL;
    vec1 = unbox_vec_arg(vec1, &_bx1);
    if (RAY_IS_ERR(vec1)) return vec1;
    vec2 = unbox_vec_arg(vec2, &_bx2);
    if (RAY_IS_ERR(vec2)) { if (_bx1) ray_release(_bx1); return vec2; }
    if (!is_list(vec1) || !is_list(vec2)) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("type", "union: both args must be lists, got %s and %s", ray_type_name(vec1->type), ray_type_name(vec2->type)); }
    int64_t len1 = ray_len(vec1), len2 = ray_len(vec2);
    ray_t** e1 = (ray_t**)ray_data(vec1);
    ray_t** e2 = (ray_t**)ray_data(vec2);

    ray_t* result = ray_alloc((len1 + len2) * sizeof(ray_t*));
    if (!result) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    ray_t** out = (ray_t**)ray_data(result);
    int64_t count = 0;
    for (int64_t i = 0; i < len1; i++) { ray_retain(e1[i]); out[count++] = e1[i]; }
    for (int64_t i = 0; i < len2; i++) {
        int found = 0;
        for (int64_t j = 0; j < count; j++)
            if (atom_eq(out[j], e2[i])) { found = 1; break; }
        if (!found) { ray_retain(e2[i]); out[count++] = e2[i]; }
    }
    result->len = count;
    if (_bx1) ray_release(_bx1);
    if (_bx2) ray_release(_bx2);
    return result;
}

/* (sect vec1 vec2) — intersection: elements in both */
ray_t* ray_sect_fn(ray_t* vec1, ray_t* vec2) {
    if (ray_is_lazy(vec1)) vec1 = ray_lazy_materialize(vec1);
    if (ray_is_lazy(vec2)) vec2 = ray_lazy_materialize(vec2);

    /* Typed vector path: hash-based.  Was O(len1×len2); now O(len1+len2). */
    if (ray_is_vec(vec1) && ray_is_vec(vec2)) {
        int64_t len1 = vec1->len;
        int64_t idx_stack[256];
        int64_t* idx = (len1 <= 256) ? idx_stack : (int64_t*)ray_alloc_raw((size_t)len1 * sizeof(int64_t));
        if (!idx) return ray_error("oom", NULL);
        hashset_t hs;
        if (!hashset_init(&hs, vec2, vec2->len)) {
            if (idx != idx_stack) ray_free_raw(idx);
            return ray_error("oom", NULL);
        }
        for (int64_t j = 0; j < vec2->len; j++) hashset_insert(&hs, j);
        int8_t v1_type = vec1->type;
        void*  v1_data = ray_data(vec1);
        int64_t count = 0;
        for (int64_t i = 0; i < len1; i++) {
            if (hashset_find_xrow(&hs, vec1, i, v1_type, v1_data) != HS_EMPTY)
                idx[count++] = i;
        }
        hashset_destroy(&hs);
        ray_t* result = gather_by_idx(vec1, idx, count);
        if (idx != idx_stack) ray_free_raw(idx);
        return result;
    }

    /* Boxed list fallback */
    ray_t *_bx1 = NULL, *_bx2 = NULL;
    vec1 = unbox_vec_arg(vec1, &_bx1);
    if (RAY_IS_ERR(vec1)) return vec1;
    vec2 = unbox_vec_arg(vec2, &_bx2);
    if (RAY_IS_ERR(vec2)) { if (_bx1) ray_release(_bx1); return vec2; }
    if (!is_list(vec1) || !is_list(vec2)) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("type", "sect: both args must be lists, got %s and %s", ray_type_name(vec1->type), ray_type_name(vec2->type)); }
    int64_t len1 = ray_len(vec1);
    ray_t** e1 = (ray_t**)ray_data(vec1);
    ray_t** e2 = (ray_t**)ray_data(vec2);
    int64_t len2 = ray_len(vec2);

    ray_t* result = ray_alloc(len1 * sizeof(ray_t*));
    if (!result) { if (_bx1) ray_release(_bx1); if (_bx2) ray_release(_bx2); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    ray_t** out = (ray_t**)ray_data(result);
    int64_t count = 0;
    for (int64_t i = 0; i < len1; i++) {
        for (int64_t j = 0; j < len2; j++) {
            if (atom_eq(e1[i], e2[j])) { ray_retain(e1[i]); out[count++] = e1[i]; break; }
        }
    }
    result->len = count;
    if (_bx1) ray_release(_bx1);
    if (_bx2) ray_release(_bx2);
    return result;
}

/* (take vec n) — first n elements (positive) or last |n| elements (negative) */
ray_t* ray_take_fn(ray_t* vec, ray_t* n_obj) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    /* N must be an integer (or 2-elem i64 vector for range-take).  Reject
     * floats up front: as_i64(f64) reads the bit pattern and would cause
     * e.g. (take 1.0 2.0) to attempt a 4.6-quintillion-element allocation
     * and surface as "oom" — misleading for what is really a type error. */
    if (ray_is_atom(n_obj) && (n_obj->type == -RAY_F64 || n_obj->type == -RAY_F32))
        return ray_error("type", "take: count must be an integer, got %s", ray_type_name(n_obj->type));
    /* Range take: (take collection [start amount]) — slice from start for amount elements */
    if (ray_is_vec(n_obj) && n_obj->type == RAY_I64 && ray_len(n_obj) == 2) {
        int64_t* idx = (int64_t*)ray_data(n_obj);
        int64_t start = idx[0];
        int64_t amount = idx[1];
        if (amount < 0) return ray_error("length", "take: range amount must be non-negative, got %lld", (long long)amount);

        /* Table range take */
        if (vec->type == RAY_TABLE) {
            int64_t ncols = ray_table_ncols(vec);
            ray_t* result = ray_table_new(ncols);
            if (RAY_IS_ERR(result)) return result;
            for (int64_t i = 0; i < ncols; i++) {
                ray_t* col = ray_table_get_col_idx(vec, i);
                int64_t name_id = ray_table_col_name(vec, i);
                ray_t* taken = ray_take_fn(col, n_obj);
                if (RAY_IS_ERR(taken)) { ray_release(result); return taken; }
                result = ray_table_add_col(result, name_id, taken);
                ray_release(taken);
                if (RAY_IS_ERR(result)) return result;
            }
            return result;
        }

        /* String range take */
        if (ray_is_atom(vec) && (-vec->type) == RAY_STR) {
            const char* s = ray_str_ptr(vec);
            int64_t slen = (int64_t)ray_str_len(vec);
            if (start < 0) start = slen + start;
            if (start < 0) start = 0;
            if (start >= slen) return ray_str("", 0);
            /* amount can be up to INT64_MAX, so `start + amount` overflows;
             * take min(amount, slen - start) instead (0 <= start < slen). */
            int64_t end = (amount < slen - start) ? start + amount : slen;
            return ray_str(s + start, (size_t)(end - start));
        }

        /* Typed vector range take */
        if (ray_is_vec(vec)) {
            int64_t len = ray_len(vec);
            if (start < 0) start = len + start;
            if (start < 0) start = 0;
            if (start >= len) {
                ray_t* empty = ray_vec_new(vec->type, 0);
                /* Empty STR result still needs the source's pool reference
                 * so downstream ops have a consistent owner. */
                if (vec->type == RAY_STR && !RAY_IS_ERR(empty))
                    col_propagate_str_pool(empty, vec);
                return empty;
            }
            /* Overflow-safe clamp: amount may be up to INT64_MAX, so avoid
             * computing start+amount unless it is known to fit (0 <= start < len). */
            int64_t end = (amount < len - start) ? start + amount : len;
            int64_t count = end - start;
            int8_t vtype = vec->type;
            int esz = ray_sym_elem_size(vtype, vec->attrs);
            ray_t* result = (vtype == RAY_SYM)
                          ? ray_sym_vec_new(vec->attrs & RAY_SYM_W_MASK, count)
                          : ray_vec_new(vtype, count);
            if (RAY_IS_ERR(result)) return result;
            result->len = count;
            memcpy(ray_data(result), (char*)ray_data(vec) + start * esz, (size_t)(count * esz));
            /* SYM ids copied verbatim — resolve over the source domain. */
            if (vtype == RAY_SYM) ray_sym_vec_adopt_domain(result, vec);
            /* RAY_STR: the copied ray_str_t records still reference the
             * source's str_pool by pool_off — propagate the pool ray_t
             * (with retain) so the result owns a valid backing store. */
            if (vtype == RAY_STR) col_propagate_str_pool(result, vec);
            /* Propagate null bitmap — check parent's flag for slices */
            bool has_nulls = ray_vec_may_have_nulls(vec);
            if (has_nulls) {
                for (int64_t i = 0; i < count; i++)
                    if (ray_vec_is_null(vec, start + i))
                        ray_vec_set_null(result, i, true);
            }
            return result;
        }

        /* Dict range take — slice both keys and vals in parallel. */
        if (vec->type == RAY_DICT) {
            ray_t* keys = ray_dict_keys(vec);
            ray_t* vals = ray_dict_vals(vec);
            int64_t len = keys ? keys->len : 0;
            if (start < 0) start = len + start;
            if (start < 0) start = 0;
            /* Overflow-safe clamp (amount up to INT64_MAX); keep the end<start
             * guard because the dict branch does not early-return start>=len. */
            int64_t end = (amount < len - start) ? start + amount : len;
            if (end < start) end = start;
            int64_t count = end - start;

            ray_t* nk = ray_vec_slice(keys, start, count);
            if (!nk || RAY_IS_ERR(nk)) return nk ? nk : ray_error("oom", NULL);

            ray_t* nv;
            if (vals && vals->type == RAY_LIST) {
                nv = ray_alloc(count * sizeof(ray_t*));
                if (!nv) { ray_release(nk); return ray_error("oom", NULL); }
                nv->type = RAY_LIST;
                nv->len  = count;
                ray_t** vsrc = (ray_t**)ray_data(vals);
                ray_t** vdst = (ray_t**)ray_data(nv);
                for (int64_t i = 0; i < count; i++) {
                    vdst[i] = vsrc[start + i];
                    if (vdst[i]) ray_retain(vdst[i]);
                }
            } else {
                nv = ray_vec_slice(vals, start, count);
                if (!nv || RAY_IS_ERR(nv)) { ray_release(nk); return nv ? nv : ray_error("oom", NULL); }
            }
            return ray_dict_new(nk, nv);
        }

        /* Boxed list range take */
        if (vec->type == RAY_LIST) {
            int64_t len = ray_len(vec);
            if (start < 0) start = len + start;
            if (start < 0) start = 0;
            if (start >= len) {
                ray_t* result = ray_alloc(0);
                result->type = RAY_LIST;
                result->len = 0;
                return result;
            }
            int64_t end = (amount < len - start) ? start + amount : len;
            int64_t count = end - start;
            ray_t** elems = (ray_t**)ray_data(vec);
            ray_t* result = ray_alloc(count * sizeof(ray_t*));
            if (!result) return ray_error("oom", NULL);
            result->type = RAY_LIST;
            result->len = count;
            ray_t** out = (ray_t**)ray_data(result);
            for (int64_t i = 0; i < count; i++) {
                ray_retain(elems[start + i]);
                out[i] = elems[start + i];
            }
            return result;
        }

        return ray_error("type", "take: range take unsupported for %s", ray_type_name(vec->type));
    }
    /* Every scalar-count branch below turns the count into a magnitude with
     * `n < 0 ? -n : n`, and `-INT64_MIN` is signed-overflow UB.  INT64_MIN is
     * also the i64 null sentinel (0N), so reject it once here as a type error
     * — symmetric with drop and the window verbs — rather than letting a null
     * count through.  Float counts are already rejected above, so is_numeric
     * here only admits integer atoms (and implies ray_is_atom). */
    if (is_numeric(n_obj) && as_i64(n_obj) == INT64_MIN)
        return ray_error("type", "take: count is null or out of range");
    /* Char take: (take 'a' n) → string of n copies of char */
    if (ray_is_atom(vec) && vec->type == -RAY_STR && ray_str_len(vec) == 1 && ray_is_atom(n_obj) && is_numeric(n_obj)) {
        int64_t n = as_i64(n_obj);
        int64_t count = n < 0 ? -n : n;
        char buf[8192];
        if (count > (int64_t)sizeof(buf)) return ray_error("limit", NULL);
        for (int64_t i = 0; i < count; i++) buf[i] = vec->sdata[0];
        return ray_str(buf, (size_t)count);
    }
    /* Scalar take: (take value n) → repeat value n times */
    if (ray_is_atom(vec) && (-vec->type) != RAY_STR && ray_is_atom(n_obj) && is_numeric(n_obj)) {
        int64_t n = as_i64(n_obj);
        int64_t count = n < 0 ? -n : n;
        int8_t vtype = -(vec->type);
        ray_t* result = ray_vec_new(vtype, count);
        if (RAY_IS_ERR(result)) return result;
        result->len = count;
        for (int64_t i = 0; i < count; i++)
            store_typed_elem(result, i, vec);
        return result;
    }
    /* String take: (take "hello" 3) → "hel", with wrapping extension */
    if (ray_is_atom(vec) && (-vec->type) == RAY_STR && ray_is_atom(n_obj) && is_numeric(n_obj)) {
        const char* s = ray_str_ptr(vec);
        int64_t slen = (int64_t)ray_str_len(vec);
        int64_t n = as_i64(n_obj);
        int64_t abs_n = n < 0 ? -n : n;
        char buf[8192];
        if (abs_n > (int64_t)sizeof(buf)) return ray_error("limit", NULL);
        if (slen == 0) {
            return ray_str("", 0);
        }
        if (n >= 0) {
            for (int64_t i = 0; i < abs_n; i++) buf[i] = s[i % slen];
        } else {
            for (int64_t i = 0; i < abs_n; i++) {
                int64_t si = slen - (abs_n - i) % slen;
                if (si == slen) si = 0;
                buf[i] = s[si];
            }
        }
        return ray_str(buf, (size_t)abs_n);
    }
    /* Table take: apply take to each column */
    if (vec->type == RAY_TABLE && is_numeric(n_obj)) {
        int64_t ncols = ray_table_ncols(vec);
        ray_t* result = ray_table_new(ncols);
        if (RAY_IS_ERR(result)) return result;
        for (int64_t i = 0; i < ncols; i++) {
            ray_t* col = ray_table_get_col_idx(vec, i);
            int64_t name_id = ray_table_col_name(vec, i);
            ray_t* taken = ray_take_fn(col, n_obj);
            if (RAY_IS_ERR(taken)) { ray_release(result); return taken; }
            result = ray_table_add_col(result, name_id, taken);
            ray_release(taken);
            if (RAY_IS_ERR(result)) return result;
        }
        return result;
    }
    /* Dict take: apply take to keys and vals in parallel.  Wrapping for
     * |n| > pair count works the same as for typed vectors. */
    if (vec->type == RAY_DICT && is_numeric(n_obj)) {
        ray_t* keys = ray_dict_keys(vec);
        ray_t* vals = ray_dict_vals(vec);
        if (!keys) return ray_error("type", "take: dict has no keys vector");
        ray_t* nk = ray_take_fn(keys, n_obj);
        if (RAY_IS_ERR(nk)) return nk;
        ray_t* nv = vals ? ray_take_fn(vals, n_obj) : ray_list_new(0);
        if (!nv || RAY_IS_ERR(nv)) { ray_release(nk); return nv ? nv : ray_error("oom", NULL); }
        return ray_dict_new(nk, nv);
    }
    /* Typed vector take with extension */
    if (ray_is_vec(vec) && is_numeric(n_obj)) {
        int64_t len = ray_len(vec);
        int64_t n = as_i64(n_obj);
        int64_t abs_n = n < 0 ? -n : n;
        int8_t vtype = vec->type;
        int esz = ray_sym_elem_size(vtype, vec->attrs);
        ray_t* result = (vtype == RAY_SYM)
                      ? ray_sym_vec_new(vec->attrs & RAY_SYM_W_MASK, abs_n)
                      : ray_vec_new(vtype, abs_n);
        if (RAY_IS_ERR(result)) return result;
        result->len = abs_n;
        char* src = (char*)ray_data(vec);
        char* dst = (char*)ray_data(result);
        if (len == 0) {
            memset(dst, 0, (size_t)(abs_n * esz));
        } else if (n >= 0 && abs_n > 0) {
            /* Doubling tile-copy: O(log(abs_n/len)) memcpys instead of
             * abs_n calls of esz bytes each.  Invariant: after every
             * memcpy `copied` is a multiple of `len`, so dst[0..copied)
             * holds a perfect tile and we can keep doubling from dst[0].
             * The final partial copy is < copied so it stays within the
             * already-tiled prefix. */
            int64_t to_copy = abs_n < len ? abs_n : len;
            memcpy(dst, src, (size_t)(to_copy * esz));
            int64_t copied = to_copy;
            while (copied + copied <= abs_n) {
                memcpy(dst + copied * esz, dst, (size_t)(copied * esz));
                copied *= 2;
            }
            int64_t remaining = abs_n - copied;
            if (remaining > 0)
                memcpy(dst + copied * esz, dst, (size_t)(remaining * esz));
        } else if (n < 0) {
            /* Negative: take from end with wrap */
            for (int64_t i = 0; i < abs_n; i++) {
                int64_t si = len - (abs_n - i) % len;
                if (si == len) si = 0;
                memcpy(dst + i * esz, src + si * esz, esz);
            }
        }
        /* RAY_STR: the copied ray_str_t records still reference the
         * source's str_pool by pool_off — propagate the pool ray_t
         * (with retain) so the result owns a valid backing store.
         * Without this, a subsequent op (e.g. asc) would see
         * result->str_pool == NULL while the elements still point
         * past the SSO threshold, tripping the assertion in
         * ray_str_t_ptr / strsort_repack_window / strkey_cmp. */
        if (vtype == RAY_STR) col_propagate_str_pool(result, vec);
        /* SYM ids copied verbatim — resolve over the source domain. */
        if (vtype == RAY_SYM) ray_sym_vec_adopt_domain(result, vec);
        /* Propagate null bitmap — check parent's flag for slices */
        bool has_nulls = len > 0 && ray_vec_may_have_nulls(vec);
        if (has_nulls) {
            if (n >= 0) {
                for (int64_t i = 0; i < abs_n; i++)
                    if (ray_vec_is_null(vec, i % len))
                        ray_vec_set_null(result, i, true);
            } else {
                for (int64_t i = 0; i < abs_n; i++) {
                    int64_t si = len - (abs_n - i) % len;
                    if (si == len) si = 0;
                    if (ray_vec_is_null(vec, si))
                        ray_vec_set_null(result, i, true);
                }
            }
        }
        return result;
    }
    ray_t* _bx = NULL;
    vec = unbox_vec_arg(vec, &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    if (!is_list(vec) || !is_numeric(n_obj))
        { if (_bx) ray_release(_bx); return ray_error("type", "take: requires a list and a numeric count, got %s and %s", ray_type_name(vec->type), ray_type_name(n_obj->type)); }
    int64_t len = ray_len(vec);
    int64_t n = as_i64(n_obj);
    ray_t** elems = (ray_t**)ray_data(vec);

    int64_t abs_n = n < 0 ? -n : n;
    int64_t elem_count = abs_n;
    ray_t* result = ray_alloc(elem_count * sizeof(ray_t*));
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = elem_count;
    ray_t** out = (ray_t**)ray_data(result);
    if (len == 0) {
        result->len = 0;
    } else if (n >= 0) {
        for (int64_t i = 0; i < elem_count; i++) {
            ray_retain(elems[i % len]);
            out[i] = elems[i % len];
        }
    } else {
        for (int64_t i = 0; i < elem_count; i++) {
            int64_t si = len - (elem_count - i) % len;
            if (si == len) si = 0;
            ray_retain(elems[si]);
            out[i] = elems[si];
        }
    }
    if (_bx) ray_release(_bx);
    return result;
}

static bool collection_count(ray_t* x, int64_t* out) {
    if (!x || RAY_IS_ERR(x)) return false;
    if (ray_is_lazy(x)) {
        ray_t* mat = ray_lazy_materialize(x);
        if (!mat || RAY_IS_ERR(mat)) return false;
        bool ok = collection_count(mat, out);
        ray_release(mat);
        return ok;
    }
    if (ray_is_atom(x) && x->type == -RAY_STR) {
        *out = (int64_t)ray_str_len(x);
        return true;
    }
    if (ray_is_vec(x) || x->type == RAY_LIST) {
        *out = x->len;
        return true;
    }
    if (x->type == RAY_TABLE) {
        *out = ray_table_nrows(x);
        return true;
    }
    if (x->type == RAY_DICT) {
        ray_t* keys = ray_dict_keys(x);
        *out = keys ? keys->len : 0;
        return true;
    }
    return false;
}

static ray_t* range_vec(int64_t start, int64_t amount) {
    ray_t* r = ray_vec_new(RAY_I64, 2);
    if (!r || RAY_IS_ERR(r)) return r ? r : ray_error("oom", NULL);
    r->len = 2;
    int64_t* d = (int64_t*)ray_data(r);
    d[0] = start;
    d[1] = amount;
    return r;
}

static ray_t* collection_slice(ray_t* x, int64_t start, int64_t amount) {
    ray_t* r = range_vec(start, amount);
    if (!r || RAY_IS_ERR(r)) return r ? r : ray_error("oom", NULL);
    ray_t* out = ray_take_fn(x, r);
    ray_release(r);
    return out;
}

ray_t* ray_drop_fn(ray_t* vec, ray_t* n_obj) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    if (!n_obj || !ray_is_atom(n_obj) ||
        (n_obj->type != -RAY_I64 && n_obj->type != -RAY_I32 &&
         n_obj->type != -RAY_I16 && n_obj->type != -RAY_U8))
        return ray_error("type", "drop: count must be an integer atom");

    int64_t len = 0;
    if (!collection_count(vec, &len))
        return ray_error("type", "drop: expected a collection, got %s", vec ? ray_type_name(vec->type) : "null");

    int64_t n = as_i64(n_obj);
    /* INT64_MIN is the i64 null sentinel (0N) and `-INT64_MIN` is signed-
     * overflow UB; reject it as a type error, symmetric with take, rather
     * than silently dropping the whole collection. */
    if (n == INT64_MIN)
        return ray_error("type", "drop: count is null or out of range");
    int64_t start = 0;
    int64_t amount = len;
    if (n >= 0) {
        start = n < len ? n : len;
        amount = len - start;
    } else {
        int64_t cut = -n;   /* n != INT64_MIN here, so negation is safe */
        amount = cut < len ? len - cut : 0;
    }
    return collection_slice(vec, start, amount);
}

ray_t* ray_rotate_fn(ray_t* vec, ray_t* n_obj) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    if (!n_obj || !ray_is_atom(n_obj) ||
        (n_obj->type != -RAY_I64 && n_obj->type != -RAY_I32 &&
         n_obj->type != -RAY_I16 && n_obj->type != -RAY_U8))
        return ray_error("type", "rotate: count must be an integer atom");

    int64_t len = 0;
    if (!collection_count(vec, &len))
        return ray_error("type", "rotate: expected a collection, got %s", vec ? ray_type_name(vec->type) : "null");
    if (len == 0) return collection_slice(vec, 0, 0);

    int64_t n = as_i64(n_obj) % len;
    if (n < 0) n += len;
    if (n == 0) {
        ray_retain(vec);
        return vec;
    }

    ray_t* a = collection_slice(vec, n, len - n);
    if (!a || RAY_IS_ERR(a)) return a ? a : ray_error("oom", NULL);
    ray_t* b = collection_slice(vec, 0, n);
    if (!b || RAY_IS_ERR(b)) { ray_release(a); return b ? b : ray_error("oom", NULL); }
    ray_t* out = ray_concat_fn(a, b);
    ray_release(a);
    ray_release(b);
    return out;
}

ray_t* ray_cut_fn(ray_t* vec, ray_t* idxs) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    int64_t len = 0;
    if (!collection_count(vec, &len))
        return ray_error("type", "cut: expected a collection, got %s", vec ? ray_type_name(vec->type) : "null");
    if (!is_collection(idxs) || idxs->type == RAY_STR || idxs->type == RAY_SYM)
        return ray_error("type", "cut: indices must be an integer vector or list");

    int64_t nidx = idxs->len;
    ray_t* out = ray_list_new(nidx + 1);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    int64_t prev = 0;

    for (int64_t i = 0; i < nidx; i++) {
        if ((i & 65535) == 0 && ray_interrupted()) {
            ray_release(out);
            return ray_error("cancel", "interrupted");
        }
        int alloc = 0;
        ray_t* ie = collection_elem(idxs, i, &alloc);
        if (!ie || RAY_IS_ERR(ie)) { ray_release(out); return ie ? ie : ray_error("type", NULL); }
        if (!ray_is_atom(ie) ||
            (ie->type != -RAY_I64 && ie->type != -RAY_I32 &&
             ie->type != -RAY_I16 && ie->type != -RAY_U8)) {
            if (alloc) ray_release(ie);
            ray_release(out);
            return ray_error("type", "cut: indices must be integers");
        }
        int64_t idx = as_i64(ie);
        if (alloc) ray_release(ie);
        if (idx < 0 || idx > len || idx < prev) {
            ray_release(out);
            return ray_error("domain", "cut: indices must be sorted and within collection length");
        }
        if (idx > prev) {
            ray_t* seg = collection_slice(vec, prev, idx - prev);
            if (!seg || RAY_IS_ERR(seg)) { ray_release(out); return seg ? seg : ray_error("oom", NULL); }
            out = ray_list_append(out, seg);
            ray_release(seg);
            if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
        }
        prev = idx;
    }

    ray_t* tail = collection_slice(vec, prev, len - prev);
    if (!tail || RAY_IS_ERR(tail)) { ray_release(out); return tail ? tail : ray_error("oom", NULL); }
    out = ray_list_append(out, tail);
    ray_release(tail);
    return out;
}

static bool cross_len(ray_t* x, int64_t* out) {
    if (ray_is_atom(x)) { *out = 1; return true; }
    if (is_collection(x) && x->type != RAY_TABLE && x->type != RAY_DICT) {
        *out = x->len;
        return true;
    }
    return false;
}

static ray_t* cross_elem(ray_t* x, int64_t i, int* allocated) {
    if (ray_is_atom(x)) {
        *allocated = 0;
        return x;
    }
    return collection_elem(x, i, allocated);
}

ray_t* ray_cross_fn(ray_t* a, ray_t* b) {
    if (ray_is_lazy(a)) a = ray_lazy_materialize(a);
    if (ray_is_lazy(b)) b = ray_lazy_materialize(b);

    int64_t na = 0, nb = 0;
    if (!cross_len(a, &na) || !cross_len(b, &nb))
        return ray_error("type", "cross: expected atoms or vectors/lists");
    if (na > 0 && nb > INT64_MAX / na)
        return ray_error("length", "cross: result too large");
    int64_t total = na * nb;

    ray_t* out = ray_list_new(total);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    for (int64_t i = 0; i < na; i++) {
        if ((i & 1023) == 0 && ray_interrupted()) {
            ray_release(out);
            return ray_error("cancel", "interrupted");
        }
        int alloc_a = 0;
        ray_t* ae = cross_elem(a, i, &alloc_a);
        if (!ae || RAY_IS_ERR(ae)) { ray_release(out); return ae ? ae : ray_error("type", NULL); }
        for (int64_t j = 0; j < nb; j++) {
            int alloc_b = 0;
            ray_t* be = cross_elem(b, j, &alloc_b);
            if (!be || RAY_IS_ERR(be)) {
                if (alloc_a) ray_release(ae);
                ray_release(out);
                return be ? be : ray_error("type", NULL);
            }
            ray_t* pair = ray_list_new(2);
            if (!pair || RAY_IS_ERR(pair)) {
                if (alloc_a) ray_release(ae);
                if (alloc_b) ray_release(be);
                ray_release(out);
                return pair ? pair : ray_error("oom", NULL);
            }
            pair = ray_list_append(pair, ae);
            if (!pair || RAY_IS_ERR(pair)) {
                if (alloc_a) ray_release(ae);
                if (alloc_b) ray_release(be);
                ray_release(out);
                return pair ? pair : ray_error("oom", NULL);
            }
            pair = ray_list_append(pair, be);
            if (alloc_b) ray_release(be);
            if (!pair || RAY_IS_ERR(pair)) {
                if (alloc_a) ray_release(ae);
                ray_release(out);
                return pair ? pair : ray_error("oom", NULL);
            }
            out = ray_list_append(out, pair);
            ray_release(pair);
            if (!out || RAY_IS_ERR(out)) {
                if (alloc_a) ray_release(ae);
                return out ? out : ray_error("oom", NULL);
            }
        }
        if (alloc_a) ray_release(ae);
    }
    return out;
}

/* (at vec idx) or (at table 'col) — index into vector or table */
ray_t* ray_at_fn(ray_t* vec, ray_t* idx) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    /* Table column access by symbol key — return the typed vector directly.
     * A column loaded from a splayed/parted table (.db.parted.get) is still in
     * segmented (RAY_IS_PARTED) form; the query path flattens it lazily, but a
     * direct `(at table 'col)` must materialize it so downstream `(at col i)` /
     * count / formatting see a dense vector instead of failing with `type`. */
    if (vec->type == RAY_TABLE && idx->type == -RAY_SYM) {
        ray_t* col = ray_table_get_col(vec, idx->i64);
        if (!col) return ray_error("domain", "at: unknown table column");
        if (RAY_IS_PARTED(col->type)) return parted_to_flat_vec(col);
        if (col->type == RAY_MAPCOMMON) return materialize_mapcommon(col);
        ray_retain(col);
        return col;
    }

    /* Table row access by integer index: (at table 0) → {col1: val1, col2: val2} */
    if (vec->type == RAY_TABLE && ray_is_atom(idx) &&
        (idx->type == -RAY_I64 || idx->type == -RAY_I32 ||
         idx->type == -RAY_I16 || idx->type == -RAY_U8)) {
        int64_t row = as_i64(idx);
        int64_t nrows = ray_table_nrows(vec);
        if (row < 0 || row >= nrows) return ray_error("domain", "at: row index %lld out of range for table of %lld rows", (long long)row, (long long)nrows);
        int64_t ncols = ray_table_ncols(vec);
        /* Build a dict: keys SYM vec + vals LIST */
        ray_t* keys = ray_sym_vec_new(RAY_SYM_W64, ncols);
        if (RAY_IS_ERR(keys)) return keys;
        ray_t* vals = ray_list_new(ncols);
        if (RAY_IS_ERR(vals)) { ray_release(keys); return vals; }
        for (int64_t c = 0; c < ncols; c++) {
            int64_t key_id = ray_table_col_name(vec, c);
            keys = ray_vec_append(keys, &key_id);
            if (RAY_IS_ERR(keys)) { ray_release(vals); return keys; }
            ray_t* col = ray_table_get_col_idx(vec, c);
            int alloc = 0;
            ray_t* val = collection_elem(col, row, &alloc);
            if (RAY_IS_ERR(val)) { ray_release(keys); ray_release(vals); return val; }
            vals = ray_list_append(vals, val);
            if (alloc) ray_release(val);
            if (RAY_IS_ERR(vals)) { ray_release(keys); return vals; }
        }
        return ray_dict_new(keys, vals);
    }

    /* Table row selection by index vector: apply the row ids to each
     * column and return a table.  Keep this before the generic collection
     * fallback; otherwise a table indexed by millions of row ids becomes
     * a LIST of row dictionaries. */
    if (vec->type == RAY_TABLE && idx->type == RAY_I64) {
        int64_t nrows = ray_table_nrows(vec);
        int64_t nidx = ray_len(idx);
        int64_t* ids = (int64_t*)ray_data(idx);
        for (int64_t i = 0; i < nidx; i++) {
            if (ids[i] < 0 || ids[i] >= nrows)
                return ray_error("domain", "at: row index %lld out of range for table of %lld rows", (long long)ids[i], (long long)nrows);
        }

        int64_t ncols = ray_table_ncols(vec);
        ray_t* result = ray_table_new(ncols);
        if (!result || RAY_IS_ERR(result)) return result ? result : ray_error("oom", NULL);
        for (int64_t c = 0; c < ncols; c++) {
            ray_t* col = ray_table_get_col_idx(vec, c);
            int64_t name = ray_table_col_name(vec, c);
            if (!col) continue;
            ray_t* gathered = gather_by_idx(col, ids, nidx);
            if (!gathered || RAY_IS_ERR(gathered)) {
                ray_release(result);
                return gathered ? gathered : ray_error("oom", NULL);
            }
            result = ray_table_add_col(result, name, gathered);
            ray_release(gathered);
            if (!result || RAY_IS_ERR(result))
                return result ? result : ray_error("oom", NULL);
        }
        return result;
    }

    /* Dict key access: (at dict key) → value or 0Nl if missing */
    if (vec->type == RAY_DICT) {
        /* Vectorized lookup keeps LIST available as a composite scalar key
         * (xkey uses lists for multi-column keys), while every typed vector
         * is interpreted as a batch of independent keys. */
        if (is_collection(idx) && idx->type != RAY_LIST) {
            int64_t n = ray_len(idx);
            ray_t* out = ray_list_new(n);
            if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
            /* Typed keys probed by a typed vector of the same type: resolve
             * every position in one `find` (attached hash index, else one
             * hash build) instead of a scan of the keys per probe.  The
             * per-key path below stays for mixed shapes; a sliced key vector
             * probed with nulls also stays there — the scalar lookup reads a
             * slice's nulls through its parent, which the hash does not. */
            ray_t* dkeys = ray_dict_keys(vec);
            ray_t* dvals = ray_dict_vals(vec);
            if (dkeys && dvals && ray_is_vec(dkeys) && ray_is_vec(idx) &&
                idx->type == dkeys->type &&
                !((dkeys->attrs & RAY_ATTR_SLICE) && (idx->attrs & RAY_ATTR_HAS_NULLS))) {
                ray_t* pos = ray_find_fn(dkeys, idx);
                if (!pos || RAY_IS_ERR(pos)) { ray_release(out); return pos ? pos : ray_error("oom", NULL); }
                const int64_t* pd = (const int64_t*)ray_data(pos);
                int64_t nvals = ray_len(dvals);
                for (int64_t i = 0; i < n; i++) {
                    ray_t* value;
                    if (pd[i] == NULL_I64 || pd[i] < 0 || pd[i] >= nvals) {
                        value = ray_typed_null(-RAY_I64);   /* 0Nl for missing key */
                    } else {
                        int alloc = 0;
                        value = collection_elem(dvals, pd[i], &alloc);
                        if (value && !RAY_IS_ERR(value) && !alloc) ray_retain(value);
                    }
                    if (!value || RAY_IS_ERR(value)) {
                        ray_release(pos); ray_release(out);
                        return value ? value : ray_error("oom", NULL);
                    }
                    out = ray_list_append(out, value);
                    ray_release(value);
                    if (!out || RAY_IS_ERR(out)) { ray_release(pos); return out ? out : ray_error("oom", NULL); }
                }
                ray_release(pos);
                return out;
            }
            for (int64_t i = 0; i < n; i++) {
                int allocated = 0;
                ray_t* key = collection_elem(idx, i, &allocated);
                if (!key || RAY_IS_ERR(key)) {
                    ray_release(out);
                    return key ? key : ray_error("oom", NULL);
                }
                ray_t* value = ray_at_fn(vec, key);
                if (allocated) ray_release(key);
                if (!value || RAY_IS_ERR(value)) {
                    ray_release(out);
                    return value ? value : ray_error("oom", NULL);
                }
                out = ray_list_append(out, value);
                ray_release(value);
                if (!out || RAY_IS_ERR(out))
                    return out ? out : ray_error("oom", NULL);
            }
            return out;
        }
        ray_t* v = ray_dict_get(vec, idx);
        if (v) return v;
        return ray_typed_null(-RAY_I64); /* 0Nl for missing key */
    }

    /* String indexing: (at "hello" 1) → 'e', (at "hello" [0 4]) → "ho" */
    if (ray_is_atom(vec) && (-vec->type) == RAY_STR) {
        const char* s = ray_str_ptr(vec);
        size_t slen = ray_str_len(vec);
        if (is_collection(idx)) {
            /* Multiple indices → build string from chars */
            int64_t idxlen = ray_len(idx);
            char buf[8192];
            if ((size_t)idxlen > sizeof(buf)) return ray_error("limit", NULL);
            for (int64_t j = 0; j < idxlen; j++) {
                int alloc = 0;
                ray_t* ie = collection_elem(idx, j, &alloc);
                int64_t k = as_i64(ie);
                if (alloc) ray_release(ie);
                if (k < 0 || (size_t)k >= slen) return ray_error("domain", "at: string index %lld out of range for length %lld", (long long)k, (long long)slen);
                buf[j] = s[k];
            }
            return ray_str(buf, (size_t)idxlen);
        }
        int64_t i = as_i64(idx);
        if (i < 0 || (size_t)i >= slen) return ray_error("domain", "at: string index %lld out of range for length %lld", (long long)i, (long long)slen);
        /* Return 1-char string atom */
        return ray_str(&s[i], 1);
    }

    /* Typed vector index: keep the result typed when every row id is valid.
     * The generic path below remains responsible for out-of-range/null row ids,
     * because it preserves the scalar `at` rule of producing typed null atoms. */
    if (ray_is_vec(vec) && idx->type == RAY_I64 && ray_is_vec(idx)) {
        int64_t idxlen = ray_len(idx);
        int64_t vlen = ray_len(vec);
        int64_t* ids = (int64_t*)ray_data(idx);
        bool idx_has_nulls = ray_vec_may_have_nulls(idx);
        bool valid = true;
        for (int64_t j = 0; j < idxlen; j++) {
            if ((idx_has_nulls && ray_vec_is_null(idx, j)) ||
                ids[j] < 0 || ids[j] >= vlen) {
                valid = false;
                break;
            }
        }
        if (valid) return gather_by_idx(vec, ids, idxlen);
    }

    /* Vector index: (at vec [i j k]) → vector of values */
    if (is_collection(idx) && idx->type != -RAY_SYM) {
        int64_t idxlen = ray_len(idx);
        ray_t* result = ray_alloc(idxlen * sizeof(ray_t*));
        if (!result) return ray_error("oom", NULL);
        result->type = RAY_LIST;
        result->len = idxlen;
        ray_t** out = (ray_t**)ray_data(result);
        for (int64_t j = 0; j < idxlen; j++) {
            int alloc = 0;
            ray_t* idx_elem = collection_elem(idx, j, &alloc);
            if (RAY_IS_ERR(idx_elem)) {
                /* Only out[0..j) are initialised; shrink len so ray_release
                 * frees exactly those and never reads the garbage tail
                 * (out[j..idxlen) is uninitialised). */
                result->len = j;
                ray_release(result);
                return idx_elem;
            }
            ray_t* sub_idx = idx_elem;
            ray_t* val = ray_at_fn(vec, sub_idx);
            if (alloc) ray_release(idx_elem);
            if (RAY_IS_ERR(val)) {
                result->len = j;
                ray_release(result);
                return val;
            }
            out[j] = val;
        }
        return result;
    }

    if (idx->type != -RAY_I64 && idx->type != -RAY_I32 &&
        idx->type != -RAY_I16 && idx->type != -RAY_U8)
        return ray_error("type", "at: index must be an integer, got %s", ray_type_name(idx->type));
    int64_t i = as_i64(idx);

    /* Typed vector: extract element directly */
    if (ray_is_vec(vec)) {
        int64_t len = ray_len(vec);
        if (i < 0 || i >= len) return ray_typed_null(-vec->type); /* out of bounds → typed null */
        int alloc = 0;
        ray_t* elem = collection_elem(vec, i, &alloc);
        /* collection_elem always allocates for typed vecs, so elem is owned */
        return elem;
    }

    if (!is_list(vec)) return ray_error("type", "at: cannot index %s", ray_type_name(vec->type));
    int64_t len = ray_len(vec);
    if (i < 0 || i >= len) return ray_typed_null(-RAY_I64); /* out of bounds → 0Nl */
    ray_t* elem = ((ray_t**)ray_data(vec))[i];
    ray_retain(elem);
    return elem;
}

/* (fill replacement values) — replace null cells in values.  Registered as
 * atomic, so scalar replacement broadcasts and vector replacements zip. */
ray_t* ray_fill_fn(ray_t* replacement, ray_t* value) {
    ray_t* out = RAY_ATOM_IS_NULL(value) ? replacement : value;
    ray_retain(out);
    return out;
}

/* (find vec val) — index of first occurrence, or -1 */
ray_t* ray_find_fn(ray_t* vec, ray_t* val) {
    if (ray_is_lazy(vec)) vec = ray_lazy_materialize(vec);
    if (ray_is_lazy(val)) val = ray_lazy_materialize(val);
    /* String find: (find "hello" 'l') → index of char in string */
    if (ray_is_atom(vec) && (-vec->type) == RAY_STR && ray_is_atom(val) && val->type == -RAY_STR && ray_str_len(val) == 1) {
        const char* s = ray_str_ptr(vec);
        size_t slen = ray_str_len(vec);
        char c = ray_str_ptr(val)[0];
        for (size_t i = 0; i < slen; i++) {
            if (s[i] == c) return make_i64((int64_t)i);
        }
        return ray_typed_null(-RAY_I64);
    }
    /* Vector val: (find vec [v1 v2]) → dense I64 [idx1 idx2], O(n+m) via hashset. */
    if (is_collection(val) && !ray_is_atom(val)) {
        int64_t vlen = ray_len(val);
        ray_t* result = ray_vec_new(RAY_I64, vlen);
        if (RAY_IS_ERR(result)) return result;
        result->len = vlen;
        int64_t* out = (int64_t*)ray_data(result);
        bool any_null = false;

        /* Both sides typed vecs — O(n+m).  In order of preference:
         *   1. an attached hash index on the haystack: one probe per needle,
         *      nothing built per call;
         *   2. fewer needles than rows: hash the needles, scan the haystack
         *      once (find_hash_needles);
         *   3. hash the haystack, probe the needles (mirrors ray_in_fn). */
        if (ray_is_vec(val) && ray_is_vec(vec)) {
            bool done = false;
            if (ray_index_has(vec)) {
                ray_idx_consults[IDX_SITE_FIND]++;
                if (ray_index_find_vec(vec, val, out, &any_null)) {
                    ray_idx_hits[IDX_SITE_FIND]++;
                    done = true;
                }
            }
            if (!done && find_needle_side_ok(vec, val))
                done = find_hash_needles(vec, val, out, &any_null);
            if (!done) {
                hashset_t hs;
                if (!hashset_init(&hs, vec, vec->len)) { ray_release(result); return ray_error("oom", NULL); }
                for (int64_t j = 0; j < vec->len; j++) hashset_insert(&hs, j);
                int8_t vt = val->type; void* vd = ray_data(val);
                for (int64_t i = 0; i < vlen; i++) {
                    int64_t row = hashset_find_xrow(&hs, val, i, vt, vd);
                    if (row == HS_EMPTY) { out[i] = NULL_I64; any_null = true; }
                    else out[i] = row;
                }
                hashset_destroy(&hs);
            }
        } else {
            /* Fallback (LIST/mixed): per-element scalar find into the dense buffer. */
            for (int64_t j = 0; j < vlen; j++) {
                int alloc = 0;
                ray_t* ve = collection_elem(val, j, &alloc);
                ray_t* one = ray_find_fn(vec, ve);   /* scalar → i64 atom or 0Nl */
                if (alloc) ray_release(ve);
                if (RAY_IS_ERR(one)) { ray_release(result); return one; }
                if (RAY_ATOM_IS_NULL(one)) { out[j] = NULL_I64; any_null = true; }
                else out[j] = one->i64;
                ray_release(one);
            }
        }
        if (any_null) result->attrs |= RAY_ATTR_HAS_NULLS;
        return result;
    }
    /* Typed vector: search without boxing */
    if (ray_is_vec(vec)) {
        int64_t len = vec->len;
        bool has_nulls = ray_vec_may_have_nulls(vec);
        bool val_null = RAY_ATOM_IS_NULL(val);

        /* Hash-index fast path: an integer-family, symbol or string needle
         * against an indexed column of the same family (ray_index_find_atom
         * owns the type matrix).  Float and cross-family needles fall
         * through to the scan (the scan owns promotion semantics; we do
         * not replicate them here). */
        if (!val_null && ray_is_atom(val) && ray_index_has(vec) &&
            (!has_nulls || vec->type == RAY_SYM)) {
            ray_idx_consults[IDX_SITE_FIND]++;
            int64_t row = ray_index_find_atom(vec, val);
            if (row >= -1) {   /* -2 means not eligible — fall through */
                ray_idx_hits[IDX_SITE_FIND]++;
                if (row < 0)
                    return ray_typed_null(-RAY_I64);
                return make_i64(row);
            }
        }

        if (has_nulls) {
            for (int64_t i = 0; i < len; i++) {
                if (ray_vec_is_null(vec, i)) {
                    if (val_null) return make_i64(i);
                    continue;
                }
                if (val_null) continue;
                int alloc = 0;
                ray_t* elem = collection_elem(vec, i, &alloc);
                int eq = atom_eq(elem, val);
                if (alloc) ray_release(elem);
                if (eq) return make_i64(i);
            }
        } else {
            if (!val_null) {
                for (int64_t i = 0; i < len; i++) {
                    int alloc = 0;
                    ray_t* elem = collection_elem(vec, i, &alloc);
                    int eq = atom_eq(elem, val);
                    if (alloc) ray_release(elem);
                    if (eq) return make_i64(i);
                }
            }
        }
        return ray_typed_null(-RAY_I64);
    }
    ray_t* _bx = NULL;
    vec = unbox_vec_arg(vec, &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "find: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    ray_t** elems = (ray_t**)ray_data(vec);
    for (int64_t i = 0; i < len; i++) {
        if (atom_eq(elems[i], val)) { if (_bx) ray_release(_bx); return make_i64(i); }
    }
    if (_bx) ray_release(_bx);
    return ray_typed_null(-RAY_I64); /* 0Nl = not found */
}

/* (til n) — generate integer sequence [0, 1, ..., n-1] */
static void til_fill(void* ctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    int64_t* out = (int64_t*)ctx;
    for (int64_t i = start; i < end; i++)
        out[i] = i;
}

ray_t* ray_til_fn(ray_t* x) {
    if (!ray_is_atom(x) || x->type != -RAY_I64) return ray_error("type", "til: argument must be an i64 atom, got %s", ray_type_name(x->type));
    int64_t n = x->i64;
    if (n < 0) return ray_error("domain", "til: count must be non-negative, got %lld", (long long)n);
    if (n == 0) return ray_vec_new(RAY_I64, 0);

    ray_t* vec = ray_vec_new(RAY_I64, n);
    if (!vec || RAY_IS_ERR(vec)) return vec;
    vec->len = n;
    int64_t* out = (int64_t*)ray_data(vec);
    ray_pool_dispatch(ray_pool_get(), til_fill, out, n);
    return vec;
}

/* Shared eager kernel — called by the OP_REVERSE executor. */
ray_t* reverse_vec_eager(ray_t* x) {
    int64_t len = x->len;
    if (len <= 1) { ray_retain(x); return x; }
    int8_t vtype = x->type;
    if (vtype == RAY_STR) {
        ray_t* result = ray_vec_new(RAY_STR, len);
        if (RAY_IS_ERR(result)) return result;
        bool has_nulls = ray_vec_may_have_nulls(x);
        if (has_nulls) {
            for (int64_t i = 0; i < len; i++) {
                if (ray_vec_is_null(x, len - 1 - i)) {
                    result = ray_str_vec_append(result, "", 0);
                    if (!RAY_IS_ERR(result))
                        ray_vec_set_null(result, result->len - 1, true);
                } else {
                    size_t slen;
                    const char* sp = ray_str_vec_get(x, len - 1 - i, &slen);
                    result = ray_str_vec_append(result, sp ? sp : "", sp ? slen : 0);
                }
                if (RAY_IS_ERR(result)) return result;
            }
        } else {
            for (int64_t i = 0; i < len; i++) {
                size_t slen;
                const char* sp = ray_str_vec_get(x, len - 1 - i, &slen);
                result = ray_str_vec_append(result, sp ? sp : "", sp ? slen : 0);
                if (RAY_IS_ERR(result)) return result;
            }
        }
        return result;
    }
    ray_t* result = (vtype == RAY_SYM)
        ? ray_sym_vec_new(x->attrs & RAY_SYM_W_MASK, len)
        : ray_vec_new(vtype, len);
    if (!result || RAY_IS_ERR(result)) return result ? result : ray_error("oom", NULL);
    result->len = len;
    int esz = ray_elem_size(vtype);
    if (vtype == RAY_SYM) esz = ray_sym_elem_size(vtype, x->attrs);
    char* src = (char*)ray_data(x);
    char* dst = (char*)ray_data(result);
    bool has_nulls = ray_vec_may_have_nulls(x);
    if (has_nulls) {
        for (int64_t i = 0; i < len; i++) {
            memcpy(dst + i * esz, src + (len - 1 - i) * esz, esz);
            if (ray_vec_is_null(x, len - 1 - i))
                ray_vec_set_null(result, i, true);
        }
    } else {
        for (int64_t i = 0; i < len; i++)
            memcpy(dst + i * esz, src + (len - 1 - i) * esz, esz);
    }
    /* SYM ids copied verbatim — resolve over the source domain. */
    if (vtype == RAY_SYM) ray_sym_vec_adopt_domain(result, x);
    return result;
}

typedef struct {
    ray_t*  src;
    ray_t*  dst;
    ray_pool_t* pool;
    int64_t src_off;
    int64_t dst_off;
    int     esz;
} ts_shift_ctx_t;

static inline bool ts_morsel_checkpoint(ray_pool_t* pool, int64_t local_idx) {
    return (local_idx % RAY_MORSEL_ELEMS) == 0 && pool_cancelled(pool);
}

static void ts_shift_copy_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_shift_ctx_t* c = (ts_shift_ctx_t*)vctx;
    const char* src = (const char*)ray_data(c->src) + (c->src_off + start) * c->esz;
    char* dst = (char*)ray_data(c->dst) + (c->dst_off + start) * c->esz;
    size_t esz = (size_t)c->esz;
    for (int64_t i = start; i < end; i++, src += esz, dst += esz) {
        if (ts_morsel_checkpoint(c->pool, i - start)) return;
        memcpy(dst, src, esz);
    }
}

static ray_t* ts_alloc_like(ray_t* x) {
    if (x->type == RAY_STR)
        return ray_error("type", "time-series shift: string vectors are not supported yet");
    ray_t* out = (x->type == RAY_SYM)
               ? ray_sym_vec_new(x->attrs & RAY_SYM_W_MASK, x->len)
               : ray_vec_new(x->type, x->len);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = x->len;
    if (x->type == RAY_SYM) ray_sym_vec_adopt_domain(out, x);
    return out;
}

static void ts_set_boundary(ray_t* out, int64_t idx) {
    if (!out || idx < 0 || idx >= out->len) return;
    switch (out->type) {
        case RAY_BOOL:
        case RAY_U8:
            ((uint8_t*)ray_data(out))[idx] = 0;
            return;
        case RAY_SYM:
            ray_write_sym(ray_data(out), idx, 0, out->type, out->attrs);
            return;
        default:
            ray_vec_set_null(out, idx, true);
            return;
    }
}

static ray_t* shift_vec_eager(ray_t* x, bool lead) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "time-series shift expects a vector input");
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = shift_vec_eager(flat, lead);
        ray_release(flat);
        return out;
    }

    int64_t n = x->len;
    ray_t* out = ts_alloc_like(x);
    if (!out || RAY_IS_ERR(out)) return out;
    if (n == 0) return out;
    if (n == 1) {
        ts_set_boundary(out, 0);
        return out;
    }

    int esz = (x->type == RAY_SYM)
            ? ray_sym_elem_size(x->type, x->attrs)
            : ray_elem_size(x->type);
    ray_pool_t* pool = ray_pool_get();
    ts_shift_ctx_t ctx = {
        .src = x,
        .dst = out,
        .pool = pool,
        .src_off = lead ? 1 : 0,
        .dst_off = lead ? 0 : 1,
        .esz = esz
    };
    ray_pool_dispatch(pool, ts_shift_copy_fn, &ctx, n - 1);
    if (pool_cancelled(pool)) { ray_release(out); return ray_error("cancel", "interrupted"); }
    ts_set_boundary(out, lead ? n - 1 : 0);
    if (x->attrs & RAY_ATTR_HAS_NULLS) out->attrs |= RAY_ATTR_HAS_NULLS;
    return out;
}

ray_t* lag_vec_eager(ray_t* x) {
    return shift_vec_eager(x, false);
}

ray_t* lead_vec_eager(ray_t* x) {
    return shift_vec_eager(x, true);
}

static bool ts_delta_type(int8_t t) {
    switch (t) {
        case RAY_BOOL: case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
        case RAY_F32: case RAY_F64:
        case RAY_DATE: case RAY_TIME: case RAY_TIMESTAMP:
            return true;
        default:
            return false;
    }
}

static bool ts_ratio_type(int8_t t) {
    switch (t) {
        case RAY_BOOL: case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
        case RAY_F32: case RAY_F64:
            return true;
        default:
            return false;
    }
}

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
    bool   has_nulls;
} ts_delta_ctx_t;

static void ts_deltas_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_delta_ctx_t* c = (ts_delta_ctx_t*)vctx;
    ray_t* src = c->src;
    int8_t t = src->type;
    const void* d = ray_data(src);

    if (c->dst->type == RAY_F64) {
        double* out = (double*)ray_data(c->dst);
        for (int64_t r = start + 1; r < end + 1; r++) {
            if (ts_morsel_checkpoint(c->pool, r - (start + 1))) return;
            if (c->has_nulls && (ray_vec_is_null(src, r) || ray_vec_is_null(src, r - 1))) {
                out[r] = NULL_F64;
                continue;
            }
            double cur = (t == RAY_F64) ? ((const double*)d)[r]
                       : (t == RAY_F32) ? (double)((const float*)d)[r]
                       : (double)read_col_i64(d, r, t, src->attrs);
            double prev = (t == RAY_F64) ? ((const double*)d)[r - 1]
                        : (t == RAY_F32) ? (double)((const float*)d)[r - 1]
                        : (double)read_col_i64(d, r - 1, t, src->attrs);
            out[r] = ray_f64_fin(cur - prev);
        }
    } else {
        int64_t* out = (int64_t*)ray_data(c->dst);
        for (int64_t r = start + 1; r < end + 1; r++) {
            if (ts_morsel_checkpoint(c->pool, r - (start + 1))) return;
            if (c->has_nulls && (ray_vec_is_null(src, r) || ray_vec_is_null(src, r - 1))) {
                out[r] = NULL_I64;
                continue;
            }
            int64_t cur = read_col_i64(d, r, t, src->attrs);
            int64_t prev = read_col_i64(d, r - 1, t, src->attrs);
            out[r] = (int64_t)((uint64_t)cur - (uint64_t)prev);
        }
    }
}

ray_t* deltas_vec_eager(ray_t* x) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "deltas expects a vector input");
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = deltas_vec_eager(flat);
        ray_release(flat);
        return out;
    }
    if (!ts_delta_type(x->type))
        return ray_error("type", "deltas expects a numeric or temporal vector, got %s", ray_type_name(x->type));

    int64_t n = x->len;
    int8_t out_type = (x->type == RAY_F64 || x->type == RAY_F32) ? RAY_F64 : RAY_I64;
    ray_t* out = ray_vec_new(out_type, n);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = n;
    if (n == 0) return out;
    out->attrs |= RAY_ATTR_HAS_NULLS;
    if (out_type == RAY_F64) ((double*)ray_data(out))[0] = NULL_F64;
    else                     ((int64_t*)ray_data(out))[0] = NULL_I64;
    if (n == 1) return out;

    ray_pool_t* pool = ray_pool_get();
    ts_delta_ctx_t ctx = {
        .src = x,
        .dst = out,
        .pool = pool,
        .has_nulls = ray_vec_may_have_nulls(x)
    };
    ray_pool_dispatch(pool, ts_deltas_fn, &ctx, n - 1);
    if (pool_cancelled(pool)) { ray_release(out); return ray_error("cancel", "interrupted"); }
    return out;
}

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
    bool   has_nulls;
} ts_ratio_ctx_t;

static void ts_ratios_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_ratio_ctx_t* c = (ts_ratio_ctx_t*)vctx;
    ray_t* src = c->src;
    int8_t t = src->type;
    const void* d = ray_data(src);
    double* out = (double*)ray_data(c->dst);

    for (int64_t r = start + 1; r < end + 1; r++) {
        if (ts_morsel_checkpoint(c->pool, r - (start + 1))) return;
        if (c->has_nulls && (ray_vec_is_null(src, r) || ray_vec_is_null(src, r - 1))) {
            out[r] = NULL_F64;
            continue;
        }
        double cur = (t == RAY_F64) ? ((const double*)d)[r]
                   : (t == RAY_F32) ? (double)((const float*)d)[r]
                   : (double)read_col_i64(d, r, t, src->attrs);
        double prev = (t == RAY_F64) ? ((const double*)d)[r - 1]
                    : (t == RAY_F32) ? (double)((const float*)d)[r - 1]
                    : (double)read_col_i64(d, r - 1, t, src->attrs);
        out[r] = (prev != 0.0) ? ray_f64_fin(cur / prev) : NULL_F64;
    }
}

ray_t* ratios_vec_eager(ray_t* x) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "ratios expects a vector input");
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = ratios_vec_eager(flat);
        ray_release(flat);
        return out;
    }
    if (!ts_ratio_type(x->type))
        return ray_error("type", "ratios expects a numeric vector, got %s", ray_type_name(x->type));

    int64_t n = x->len;
    ray_t* out = ray_vec_new(RAY_F64, n);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = n;
    if (n == 0) return out;
    out->attrs |= RAY_ATTR_HAS_NULLS;
    ((double*)ray_data(out))[0] = NULL_F64;
    if (n == 1) return out;

    ray_pool_t* pool = ray_pool_get();
    ts_ratio_ctx_t ctx = {
        .src = x,
        .dst = out,
        .pool = pool,
        .has_nulls = ray_vec_may_have_nulls(x)
    };
    ray_pool_dispatch(pool, ts_ratios_fn, &ctx, n - 1);
    if (pool_cancelled(pool)) { ray_release(out); return ray_error("cancel", "interrupted"); }
    return out;
}

typedef struct {
    int64_t first_nonnull; /* offset inside block, -1 when the block is all null */
    uint8_t has_last;
    uint8_t last[16];
} ts_fill_summary_t;

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
    ts_fill_summary_t* summary;
    ts_fill_summary_t* carry;
    int64_t nblocks;
    int esz;
} ts_fill_ctx_t;

static void ts_fills_pass1_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_fill_ctx_t* c = (ts_fill_ctx_t*)vctx;
    const char* src = (const char*)ray_data(c->src);
    char* dst = (char*)ray_data(c->dst);
    size_t esz = (size_t)c->esz;

    for (int64_t b = start; b < end; b++) {
        int64_t lo = b * RAY_MORSEL_ELEMS;
        int64_t hi = lo + RAY_MORSEL_ELEMS;
        if (hi > c->src->len) hi = c->src->len;

        ts_fill_summary_t s = { .first_nonnull = -1, .has_last = 0 };
        uint8_t last[16] = {0};
        bool have = false;

        for (int64_t r = lo; r < hi; r++) {
            if (ts_morsel_checkpoint(c->pool, r - lo)) return;
            char* out_cell = dst + (size_t)r * esz;
            const char* in_cell = src + (size_t)r * esz;
            if (ray_vec_is_null(c->src, r)) {
                if (have) memcpy(out_cell, last, esz);
                else      ray_vec_set_null(c->dst, r, true);
                continue;
            }
            memcpy(out_cell, in_cell, esz);
            memcpy(last, in_cell, esz);
            have = true;
            if (s.first_nonnull < 0) s.first_nonnull = r - lo;
        }

        if (have) {
            s.has_last = 1;
            memcpy(s.last, last, esz);
        }
        c->summary[b] = s;
    }
}

static void ts_fills_pass2_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_fill_ctx_t* c = (ts_fill_ctx_t*)vctx;
    char* dst = (char*)ray_data(c->dst);
    size_t esz = (size_t)c->esz;

    for (int64_t b = start; b < end; b++) {
        ts_fill_summary_t carry = c->carry[b];
        if (!carry.has_last) continue;

        int64_t lo = b * RAY_MORSEL_ELEMS;
        int64_t hi = lo + RAY_MORSEL_ELEMS;
        if (hi > c->dst->len) hi = c->dst->len;
        int64_t first = c->summary[b].first_nonnull;
        int64_t stop = first >= 0 ? lo + first : hi;

        for (int64_t r = lo; r < stop; r++) {
            if (ts_morsel_checkpoint(c->pool, r - lo)) return;
            memcpy(dst + (size_t)r * esz, carry.last, esz);
        }
    }
}

ray_t* fills_vec_eager(ray_t* x) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "fills expects a vector input");
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = fills_vec_eager(flat);
        ray_release(flat);
        return out;
    }
    if (x->type == RAY_SYM || x->type == RAY_STR ||
        x->type == RAY_BOOL || x->type == RAY_U8 ||
        (!ray_vec_may_have_nulls(x) && !(x->attrs & RAY_ATTR_SLICE))) {
        ray_retain(x);
        return x;
    }

    int64_t n = x->len;
    ray_t* out = ts_alloc_like(x);
    if (!out || RAY_IS_ERR(out)) return out;
    if (n == 0) return out;

    int64_t nblocks = (n + RAY_MORSEL_ELEMS - 1) / RAY_MORSEL_ELEMS;
    ray_t* summary_hdr = NULL;
    ray_t* carry_hdr = NULL;
    ts_fill_summary_t* summary = (ts_fill_summary_t*)scratch_calloc(&summary_hdr, (size_t)nblocks * sizeof(ts_fill_summary_t));
    ts_fill_summary_t* carry = (ts_fill_summary_t*)scratch_calloc(&carry_hdr, (size_t)nblocks * sizeof(ts_fill_summary_t));
    if (!summary || !carry) {
        if (summary_hdr) scratch_free(summary_hdr);
        if (carry_hdr) scratch_free(carry_hdr);
        ray_release(out);
        return ray_error("oom", NULL);
    }

    int esz = (x->type == RAY_SYM) ? ray_sym_elem_size(x->type, x->attrs) : ray_elem_size(x->type);
    ray_pool_t* pool = ray_pool_get();
    ts_fill_ctx_t ctx = {
        .src = x, .dst = out, .pool = pool,
        .summary = summary, .carry = carry,
        .nblocks = nblocks, .esz = esz
    };
    ray_pool_dispatch(pool, ts_fills_pass1_fn, &ctx, nblocks);
    if (pool_cancelled(pool)) {
        scratch_free(summary_hdr); scratch_free(carry_hdr); ray_release(out);
        return ray_error("cancel", "interrupted");
    }

    ts_fill_summary_t csum = { .first_nonnull = -1, .has_last = 0 };
    for (int64_t b = 0; b < nblocks; b++) {
        if ((b & 1023) == 0 && pool_cancelled(pool)) {
            scratch_free(summary_hdr); scratch_free(carry_hdr); ray_release(out);
            return ray_error("cancel", "interrupted");
        }
        carry[b] = csum;
        if (summary[b].has_last) csum = summary[b];
    }

    ray_pool_dispatch(pool, ts_fills_pass2_fn, &ctx, nblocks);
    if (pool_cancelled(pool)) {
        scratch_free(summary_hdr); scratch_free(carry_hdr); ray_release(out);
        return ray_error("cancel", "interrupted");
    }
    scratch_free(summary_hdr);
    scratch_free(carry_hdr);
    return out;
}

typedef struct {
    uint8_t seen;
    int64_t count;
    uint64_t acc_i;
    double acc_f;
    int64_t min_i;
    int64_t max_i;
    double min_f;
    double max_f;
} ts_run_state_t;

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
    ts_run_state_t* summary;
    ts_run_state_t* carry;
    uint16_t opcode;
    bool is_float;
    bool has_nulls;
    int64_t nblocks;
} ts_run_ctx_t;

static bool ts_numeric_type(int8_t t) {
    switch (t) {
        case RAY_BOOL: case RAY_U8: case RAY_I16: case RAY_I32: case RAY_I64:
        case RAY_F32: case RAY_F64:
            return true;
        default:
            return false;
    }
}

static bool ts_running_type(uint16_t opcode, int8_t t) {
    if (opcode == OP_MINS || opcode == OP_MAXS) {
        return ts_numeric_type(t) || t == RAY_DATE || t == RAY_TIME || t == RAY_TIMESTAMP;
    }
    return ts_numeric_type(t);
}

static int8_t ts_running_out_type(uint16_t opcode, int8_t t) {
    switch (opcode) {
        case OP_AVGS:
            return RAY_F64;
        case OP_SUMS:
        case OP_PRDS:
            return (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : RAY_I64;
        case OP_MINS:
        case OP_MAXS:
            return (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : t;
        default:
            return RAY_I64;
    }
}

static const char* ts_running_name(uint16_t opcode) {
    switch (opcode) {
        case OP_SUMS: return "sums";
        case OP_AVGS: return "avgs";
        case OP_MINS: return "mins";
        case OP_MAXS: return "maxs";
        case OP_PRDS: return "prds";
        default:      return "running";
    }
}

static void ts_run_init(ts_run_state_t* s, uint16_t opcode) {
    memset(s, 0, sizeof(*s));
    if (opcode == OP_PRDS) {
        s->acc_i = 1;
        s->acc_f = 1.0;
    }
}

static double ts_read_as_f64(ray_t* src, int64_t row) {
    const void* d = ray_data(src);
    if (src->type == RAY_F64) return ((const double*)d)[row];
    if (src->type == RAY_F32) return (double)((const float*)d)[row];
    return (double)read_col_i64(d, row, src->type, src->attrs);
}

static int64_t ts_read_as_i64(ray_t* src, int64_t row) {
    return read_col_i64(ray_data(src), row, src->type, src->attrs);
}

static void ts_run_update(ts_run_state_t* s, ray_t* src, int64_t row,
                          uint16_t opcode, bool is_float) {
    if (is_float) {
        double v = ts_read_as_f64(src, row);
        switch (opcode) {
            case OP_SUMS: s->acc_f += v; break;
            case OP_PRDS: s->acc_f *= v; break;
            case OP_AVGS: s->acc_f += v; s->count++; s->seen = 1; break;
            case OP_MINS:
                if (!s->seen || v < s->min_f) s->min_f = v;
                s->seen = 1;
                break;
            case OP_MAXS:
                if (!s->seen || v > s->max_f) s->max_f = v;
                s->seen = 1;
                break;
        }
        return;
    }

    int64_t v = ts_read_as_i64(src, row);
    switch (opcode) {
        case OP_SUMS: s->acc_i += (uint64_t)v; break;
        case OP_PRDS: s->acc_i *= (uint64_t)v; break;
        case OP_AVGS: s->acc_f += (double)v; s->count++; s->seen = 1; break;
        case OP_MINS:
            if (!s->seen || v < s->min_i) s->min_i = v;
            s->seen = 1;
            break;
        case OP_MAXS:
            if (!s->seen || v > s->max_i) s->max_i = v;
            s->seen = 1;
            break;
    }
}

static void ts_run_combine(ts_run_state_t* dst, const ts_run_state_t* src,
                           uint16_t opcode, bool is_float) {
    switch (opcode) {
        case OP_SUMS:
            if (is_float) dst->acc_f += src->acc_f;
            else          dst->acc_i += src->acc_i;
            return;
        case OP_PRDS:
            if (is_float) dst->acc_f *= src->acc_f;
            else          dst->acc_i *= src->acc_i;
            return;
        case OP_AVGS:
            dst->acc_f += src->acc_f;
            dst->count += src->count;
            if (dst->count > 0) dst->seen = 1;
            return;
        case OP_MINS:
            if (!src->seen) return;
            if (is_float) {
                if (!dst->seen || src->min_f < dst->min_f) dst->min_f = src->min_f;
            } else {
                if (!dst->seen || src->min_i < dst->min_i) dst->min_i = src->min_i;
            }
            dst->seen = 1;
            return;
        case OP_MAXS:
            if (!src->seen) return;
            if (is_float) {
                if (!dst->seen || src->max_f > dst->max_f) dst->max_f = src->max_f;
            } else {
                if (!dst->seen || src->max_i > dst->max_i) dst->max_i = src->max_i;
            }
            dst->seen = 1;
            return;
    }
}

static void ts_run_write(ray_t* out, int64_t row, const ts_run_state_t* s,
                         uint16_t opcode, bool is_float) {
    void* d = ray_data(out);
    switch (opcode) {
        case OP_SUMS:
            if (out->type == RAY_F64) ((double*)d)[row] = ray_f64_fin(s->acc_f);
            else ((int64_t*)d)[row] = (int64_t)s->acc_i;
            return;
        case OP_PRDS:
            if (out->type == RAY_F64) ((double*)d)[row] = ray_f64_fin(s->acc_f);
            else ((int64_t*)d)[row] = (int64_t)s->acc_i;
            return;
        case OP_AVGS:
            if (s->count <= 0) ray_vec_set_null(out, row, true);
            else ((double*)d)[row] = ray_f64_fin(s->acc_f / (double)s->count);
            return;
        case OP_MINS:
            if (!s->seen) { ray_vec_set_null(out, row, true); return; }
            if (is_float) ((double*)d)[row] = ray_f64_fin(s->min_f);
            else write_col_i64(d, row, s->min_i, out->type, out->attrs);
            return;
        case OP_MAXS:
            if (!s->seen) { ray_vec_set_null(out, row, true); return; }
            if (is_float) ((double*)d)[row] = ray_f64_fin(s->max_f);
            else write_col_i64(d, row, s->max_i, out->type, out->attrs);
            return;
    }
}

static void ts_running_summary_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_run_ctx_t* c = (ts_run_ctx_t*)vctx;
    for (int64_t b = start; b < end; b++) {
        int64_t lo = b * RAY_MORSEL_ELEMS;
        int64_t hi = lo + RAY_MORSEL_ELEMS;
        if (hi > c->src->len) hi = c->src->len;

        ts_run_state_t s;
        ts_run_init(&s, c->opcode);
        for (int64_t r = lo; r < hi; r++) {
            if (ts_morsel_checkpoint(c->pool, r - lo)) return;
            if (c->has_nulls && ray_vec_is_null(c->src, r)) continue;
            ts_run_update(&s, c->src, r, c->opcode, c->is_float);
        }
        c->summary[b] = s;
    }
}

static void ts_running_emit_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_run_ctx_t* c = (ts_run_ctx_t*)vctx;
    for (int64_t b = start; b < end; b++) {
        int64_t lo = b * RAY_MORSEL_ELEMS;
        int64_t hi = lo + RAY_MORSEL_ELEMS;
        if (hi > c->src->len) hi = c->src->len;

        ts_run_state_t s = c->carry[b];
        for (int64_t r = lo; r < hi; r++) {
            if (ts_morsel_checkpoint(c->pool, r - lo)) return;
            if (!(c->has_nulls && ray_vec_is_null(c->src, r)))
                ts_run_update(&s, c->src, r, c->opcode, c->is_float);
            ts_run_write(c->dst, r, &s, c->opcode, c->is_float);
        }
    }
}

ray_t* running_vec_eager(ray_t* x, uint16_t opcode) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "%s expects a vector input", ts_running_name(opcode));
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = running_vec_eager(flat, opcode);
        ray_release(flat);
        return out;
    }
    if (!ts_running_type(opcode, x->type))
        return ray_error("type", "%s expects a numeric vector%s, got %s",
                         ts_running_name(opcode),
                         (opcode == OP_MINS || opcode == OP_MAXS) ? " or temporal vector" : "",
                         ray_type_name(x->type));

    int64_t n = x->len;
    int8_t out_type = ts_running_out_type(opcode, x->type);
    ray_t* out = ray_vec_new(out_type, n);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = n;
    if (n == 0) return out;

    int64_t nblocks = (n + RAY_MORSEL_ELEMS - 1) / RAY_MORSEL_ELEMS;
    ray_t* summary_hdr = NULL;
    ray_t* carry_hdr = NULL;
    ts_run_state_t* summary = (ts_run_state_t*)scratch_calloc(&summary_hdr, (size_t)nblocks * sizeof(ts_run_state_t));
    ts_run_state_t* carry = (ts_run_state_t*)scratch_calloc(&carry_hdr, (size_t)nblocks * sizeof(ts_run_state_t));
    if (!summary || !carry) {
        if (summary_hdr) scratch_free(summary_hdr);
        if (carry_hdr) scratch_free(carry_hdr);
        ray_release(out);
        return ray_error("oom", NULL);
    }

    ray_pool_t* pool = ray_pool_get();
    ts_run_ctx_t ctx = {
        .src = x, .dst = out, .pool = pool,
        .summary = summary, .carry = carry,
        .opcode = opcode,
        .is_float = (x->type == RAY_F64 || x->type == RAY_F32),
        .has_nulls = ray_vec_may_have_nulls(x),
        .nblocks = nblocks
    };

    ray_pool_dispatch(pool, ts_running_summary_fn, &ctx, nblocks);
    if (pool_cancelled(pool)) {
        scratch_free(summary_hdr); scratch_free(carry_hdr); ray_release(out);
        return ray_error("cancel", "interrupted");
    }

    ts_run_state_t csum;
    ts_run_init(&csum, opcode);
    for (int64_t b = 0; b < nblocks; b++) {
        if ((b & 1023) == 0 && pool_cancelled(pool)) {
            scratch_free(summary_hdr); scratch_free(carry_hdr); ray_release(out);
            return ray_error("cancel", "interrupted");
        }
        carry[b] = csum;
        ts_run_combine(&csum, &summary[b], opcode, ctx.is_float);
    }

    ray_pool_dispatch(pool, ts_running_emit_fn, &ctx, nblocks);
    if (pool_cancelled(pool)) {
        scratch_free(summary_hdr); scratch_free(carry_hdr); ray_release(out);
        return ray_error("cancel", "interrupted");
    }
    scratch_free(summary_hdr);
    scratch_free(carry_hdr);
    return out;
}

static bool ts_cell_equal(ray_t* x, int64_t a, int64_t b) {
    bool an = ray_vec_is_null(x, a);
    bool bn = ray_vec_is_null(x, b);
    if (an || bn) return an == bn;

    const void* d = ray_data(x);
    switch (x->type) {
        case RAY_BOOL:
        case RAY_U8:
            return ((const uint8_t*)d)[a] == ((const uint8_t*)d)[b];
        case RAY_I16:
            return ((const int16_t*)d)[a] == ((const int16_t*)d)[b];
        case RAY_I32:
        case RAY_DATE:
        case RAY_TIME:
            return ((const int32_t*)d)[a] == ((const int32_t*)d)[b];
        case RAY_I64:
        case RAY_TIMESTAMP:
            return ((const int64_t*)d)[a] == ((const int64_t*)d)[b];
        case RAY_F32:
            return ((const float*)d)[a] == ((const float*)d)[b];
        case RAY_F64:
            return ((const double*)d)[a] == ((const double*)d)[b];
        case RAY_SYM:
            return read_col_i64(d, a, x->type, x->attrs) == read_col_i64(d, b, x->type, x->attrs);
        case RAY_GUID:
            return memcmp((const uint8_t*)d + a * 16, (const uint8_t*)d + b * 16, 16) == 0;
        case RAY_STR: {
            size_t al = 0, bl = 0;
            const char* ap = ray_str_vec_get(x, a, &al);
            const char* bp = ray_str_vec_get(x, b, &bl);
            return al == bl && (!al || (ap && bp && memcmp(ap, bp, al) == 0));
        }
        default:
            return false;
    }
}

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
} ts_differ_ctx_t;

static void ts_differ_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_differ_ctx_t* c = (ts_differ_ctx_t*)vctx;
    uint8_t* out = (uint8_t*)ray_data(c->dst);
    for (int64_t r = start + 1; r < end + 1; r++) {
        if (ts_morsel_checkpoint(c->pool, r - (start + 1))) return;
        out[r] = ts_cell_equal(c->src, r, r - 1) ? 0 : 1;
    }
}

ray_t* differ_vec_eager(ray_t* x) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "differ expects a vector input");
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = differ_vec_eager(flat);
        ray_release(flat);
        return out;
    }

    int64_t n = x->len;
    ray_t* out = ray_vec_new(RAY_BOOL, n);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = n;
    if (n == 0) return out;
    ((uint8_t*)ray_data(out))[0] = 1;
    if (n == 1) return out;

    ray_pool_t* pool = ray_pool_get();
    ts_differ_ctx_t ctx = { .src = x, .dst = out, .pool = pool };
    ray_pool_dispatch(pool, ts_differ_fn, &ctx, n - 1);
    if (pool_cancelled(pool)) { ray_release(out); return ray_error("cancel", "interrupted"); }
    return out;
}

typedef struct {
    uint64_t sum_i;
    double   sum_f;
    double   sum_sq_f;
    int64_t  count;
} ts_mprefix_state_t;

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
    uint64_t* prefix_i;
    double* prefix_f;
    double* prefix_sq_f;
    int64_t* prefix_count;
    ts_mprefix_state_t* summary;
    ts_mprefix_state_t* carry;
    uint16_t opcode;
    bool has_nulls;
    int64_t window;
} ts_mprefix_ctx_t;

static const char* ts_moving_name(uint16_t opcode) {
    switch (opcode) {
        case OP_MSUM:   return "msum";
        case OP_MAVG:   return "mavg";
        case OP_MMIN:   return "mmin";
        case OP_MMAX:   return "mmax";
        case OP_MCOUNT: return "mcount";
        case OP_MVAR:   return "mvar";
        case OP_MDEV:   return "mdev";
        default:        return "moving";
    }
}

static bool ts_moving_type(uint16_t opcode, int8_t t) {
    switch (opcode) {
        case OP_MCOUNT:
            return true;
        case OP_MMIN:
        case OP_MMAX:
            return ts_numeric_type(t) || t == RAY_DATE || t == RAY_TIME || t == RAY_TIMESTAMP;
        case OP_MVAR:
        case OP_MDEV:
            return ts_numeric_type(t);
        default:
            return ts_numeric_type(t);
    }
}

static int8_t ts_moving_out_type(uint16_t opcode, int8_t t) {
    switch (opcode) {
        case OP_MCOUNT:
            return RAY_I64;
        case OP_MAVG:
        case OP_MVAR:
        case OP_MDEV:
            return RAY_F64;
        case OP_MSUM:
            return (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : RAY_I64;
        case OP_MMIN:
        case OP_MMAX:
            return (t == RAY_F64 || t == RAY_F32) ? RAY_F64 : t;
        default:
            return RAY_I64;
    }
}

static bool ts_size_overflows(int64_t n, size_t esz) {
    return n < 0 || (uint64_t)n > ((uint64_t)SIZE_MAX / (uint64_t)esz);
}

static void ts_mprefix_scan_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_mprefix_ctx_t* c = (ts_mprefix_ctx_t*)vctx;
    for (int64_t b = start; b < end; b++) {
        int64_t lo = b * RAY_MORSEL_ELEMS;
        int64_t hi = lo + RAY_MORSEL_ELEMS;
        if (hi > c->src->len) hi = c->src->len;

        uint64_t sum_i = 0;
        double sum_f = 0.0;
        double sum_sq_f = 0.0;
        int64_t count = 0;
        for (int64_t r = lo; r < hi; r++) {
            if (ts_morsel_checkpoint(c->pool, r - lo)) return;
            bool nn = !(c->has_nulls && ray_vec_is_null(c->src, r));
            if (nn) {
                if (c->prefix_i) sum_i += (uint64_t)ts_read_as_i64(c->src, r);
                if (c->prefix_f || c->prefix_sq_f) {
                    double v = ts_read_as_f64(c->src, r);
                    if (c->prefix_f) sum_f += v;
                    if (c->prefix_sq_f) sum_sq_f += v * v;
                }
                count++;
            }
            if (c->prefix_i)      c->prefix_i[r + 1] = sum_i;
            if (c->prefix_f)      c->prefix_f[r + 1] = sum_f;
            if (c->prefix_sq_f)   c->prefix_sq_f[r + 1] = sum_sq_f;
            if (c->prefix_count)  c->prefix_count[r + 1] = count;
        }
        c->summary[b] = (ts_mprefix_state_t){
            .sum_i = sum_i, .sum_f = sum_f, .sum_sq_f = sum_sq_f, .count = count
        };
    }
}

static void ts_mprefix_adjust_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_mprefix_ctx_t* c = (ts_mprefix_ctx_t*)vctx;
    for (int64_t b = start; b < end; b++) {
        int64_t lo = b * RAY_MORSEL_ELEMS;
        int64_t hi = lo + RAY_MORSEL_ELEMS;
        if (hi > c->src->len) hi = c->src->len;
        ts_mprefix_state_t carry = c->carry[b];

        for (int64_t r = lo; r < hi; r++) {
            if (ts_morsel_checkpoint(c->pool, r - lo)) return;
            if (c->prefix_i)      c->prefix_i[r + 1] += carry.sum_i;
            if (c->prefix_f)      c->prefix_f[r + 1] += carry.sum_f;
            if (c->prefix_sq_f)   c->prefix_sq_f[r + 1] += carry.sum_sq_f;
            if (c->prefix_count)  c->prefix_count[r + 1] += carry.count;
        }
    }
}

static void ts_mprefix_emit_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_mprefix_ctx_t* c = (ts_mprefix_ctx_t*)vctx;
    void* out = ray_data(c->dst);
    for (int64_t r = start; r < end; r++) {
        if (ts_morsel_checkpoint(c->pool, r - start)) return;
        int64_t lo = (r >= c->window - 1) ? (r - c->window + 1) : 0;
        int64_t hi = r + 1;
        switch (c->opcode) {
            case OP_MCOUNT:
                ((int64_t*)out)[r] = c->prefix_count[hi] - c->prefix_count[lo];
                break;
            case OP_MSUM:
                if (c->prefix_f)
                    ((double*)out)[r] = ray_f64_fin(c->prefix_f[hi] - c->prefix_f[lo]);
                else
                    ((int64_t*)out)[r] = (int64_t)(c->prefix_i[hi] - c->prefix_i[lo]);
                break;
            case OP_MAVG: {
                int64_t cnt = c->prefix_count[hi] - c->prefix_count[lo];
                if (cnt <= 0) {
                    ray_vec_set_null(c->dst, r, true);
                } else {
                    ((double*)out)[r] = ray_f64_fin((c->prefix_f[hi] - c->prefix_f[lo]) / (double)cnt);
                }
                break;
            }
            case OP_MVAR:
            case OP_MDEV: {
                int64_t cnt = c->prefix_count[hi] - c->prefix_count[lo];
                if (cnt <= 0) {
                    ray_vec_set_null(c->dst, r, true);
                } else {
                    double sum = c->prefix_f[hi] - c->prefix_f[lo];
                    double sum_sq = c->prefix_sq_f[hi] - c->prefix_sq_f[lo];
                    double mean = sum / (double)cnt;
                    double var = sum_sq / (double)cnt - mean * mean;
                    if (var < 0.0) var = 0.0;
                    ((double*)out)[r] = ray_f64_fin(c->opcode == OP_MDEV ? sqrt(var) : var);
                }
                break;
            }
        }
    }
}

static ray_t* moving_prefix_vec_eager(ray_t* x, uint16_t opcode, int64_t window, int8_t out_type) {
    int64_t n = x->len;
    ray_t* out = ray_vec_new(out_type, n);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = n;
    if (n == 0) return out;

    int64_t nblocks = (n + RAY_MORSEL_ELEMS - 1) / RAY_MORSEL_ELEMS;
    int64_t entries = n + 1;
    ray_t *pi_hdr = NULL, *pf_hdr = NULL, *psq_hdr = NULL, *pc_hdr = NULL, *sum_hdr = NULL, *carry_hdr = NULL;
    uint64_t* prefix_i = NULL;
    double* prefix_f = NULL;
    double* prefix_sq_f = NULL;
    int64_t* prefix_count = NULL;

    bool need_i = (opcode == OP_MSUM && out_type == RAY_I64);
    bool need_sq = (opcode == OP_MVAR || opcode == OP_MDEV);
    bool need_f = (opcode == OP_MAVG || need_sq || (opcode == OP_MSUM && out_type == RAY_F64));
    bool need_count = (opcode == OP_MAVG || opcode == OP_MCOUNT || need_sq);

    if (need_i) {
        if (ts_size_overflows(entries, sizeof(uint64_t))) { ray_release(out); return ray_error("oom", NULL); }
        prefix_i = (uint64_t*)scratch_calloc(&pi_hdr, (size_t)entries * sizeof(uint64_t));
    }
    if (need_f) {
        if (ts_size_overflows(entries, sizeof(double))) {
            if (pi_hdr) scratch_free(pi_hdr);
            ray_release(out);
            return ray_error("oom", NULL);
        }
        prefix_f = (double*)scratch_calloc(&pf_hdr, (size_t)entries * sizeof(double));
    }
    if (need_sq) {
        if (ts_size_overflows(entries, sizeof(double))) {
            if (pi_hdr) scratch_free(pi_hdr);
            if (pf_hdr) scratch_free(pf_hdr);
            ray_release(out);
            return ray_error("oom", NULL);
        }
        prefix_sq_f = (double*)scratch_calloc(&psq_hdr, (size_t)entries * sizeof(double));
    }
    if (need_count) {
        if (ts_size_overflows(entries, sizeof(int64_t))) {
            if (pi_hdr) scratch_free(pi_hdr);
            if (pf_hdr) scratch_free(pf_hdr);
            if (psq_hdr) scratch_free(psq_hdr);
            ray_release(out);
            return ray_error("oom", NULL);
        }
        prefix_count = (int64_t*)scratch_calloc(&pc_hdr, (size_t)entries * sizeof(int64_t));
    }
    ts_mprefix_state_t* summary = (ts_mprefix_state_t*)scratch_calloc(&sum_hdr, (size_t)nblocks * sizeof(ts_mprefix_state_t));
    ts_mprefix_state_t* carry = (ts_mprefix_state_t*)scratch_calloc(&carry_hdr, (size_t)nblocks * sizeof(ts_mprefix_state_t));
    if ((need_i && !prefix_i) || (need_f && !prefix_f) || (need_sq && !prefix_sq_f) || (need_count && !prefix_count) || !summary || !carry) {
        if (pi_hdr) scratch_free(pi_hdr);
        if (pf_hdr) scratch_free(pf_hdr);
        if (psq_hdr) scratch_free(psq_hdr);
        if (pc_hdr) scratch_free(pc_hdr);
        if (sum_hdr) scratch_free(sum_hdr);
        if (carry_hdr) scratch_free(carry_hdr);
        ray_release(out);
        return ray_error("oom", NULL);
    }

    ray_pool_t* pool = ray_pool_get();
    ts_mprefix_ctx_t ctx = {
        .src = x, .dst = out, .pool = pool,
        .prefix_i = prefix_i, .prefix_f = prefix_f, .prefix_sq_f = prefix_sq_f,
        .prefix_count = prefix_count,
        .summary = summary, .carry = carry,
        .opcode = opcode,
        .has_nulls = ray_vec_may_have_nulls(x),
        .window = window
    };

    ray_pool_dispatch(pool, ts_mprefix_scan_fn, &ctx, nblocks);
    if (pool_cancelled(pool)) goto cancelled;

    ts_mprefix_state_t run = {0};
    for (int64_t b = 0; b < nblocks; b++) {
        if ((b & 1023) == 0 && pool_cancelled(pool)) goto cancelled;
        carry[b] = run;
        run.sum_i += summary[b].sum_i;
        run.sum_f += summary[b].sum_f;
        run.sum_sq_f += summary[b].sum_sq_f;
        run.count += summary[b].count;
    }

    ray_pool_dispatch(pool, ts_mprefix_adjust_fn, &ctx, nblocks);
    if (pool_cancelled(pool)) goto cancelled;
    ray_pool_dispatch(pool, ts_mprefix_emit_fn, &ctx, n);
    if (pool_cancelled(pool)) goto cancelled;

    if (pi_hdr) scratch_free(pi_hdr);
    if (pf_hdr) scratch_free(pf_hdr);
    if (psq_hdr) scratch_free(psq_hdr);
    if (pc_hdr) scratch_free(pc_hdr);
    scratch_free(sum_hdr);
    scratch_free(carry_hdr);
    return out;

cancelled:
    if (pi_hdr) scratch_free(pi_hdr);
    if (pf_hdr) scratch_free(pf_hdr);
    if (psq_hdr) scratch_free(psq_hdr);
    if (pc_hdr) scratch_free(pc_hdr);
    scratch_free(sum_hdr);
    scratch_free(carry_hdr);
    ray_release(out);
    return ray_error("cancel", "interrupted");
}

typedef struct {
    ray_t* src;
    ray_t* dst;
    ray_pool_t* pool;
    uint16_t opcode;
    bool is_float;
    bool has_nulls;
    int64_t window;
    int64_t ntasks;
    _Atomic(int) oom;
} ts_mminmax_ctx_t;

static inline int64_t ts_task_lo(int64_t n, int64_t task, int64_t ntasks) {
    int64_t q = n / ntasks;
    int64_t r = n % ntasks;
    return q * task + (r * task) / ntasks;
}

static bool ts_mminmax_better(ts_mminmax_ctx_t* c, int64_t a, int64_t b) {
    if (c->is_float) {
        double av = ts_read_as_f64(c->src, a);
        double bv = ts_read_as_f64(c->src, b);
        return c->opcode == OP_MMIN ? av <= bv : av >= bv;
    }
    int64_t av = ts_read_as_i64(c->src, a);
    int64_t bv = ts_read_as_i64(c->src, b);
    return c->opcode == OP_MMIN ? av <= bv : av >= bv;
}

static void ts_mminmax_write(ts_mminmax_ctx_t* c, int64_t out_row, int64_t src_row) {
    void* out = ray_data(c->dst);
    if (c->is_float) ((double*)out)[out_row] = ray_f64_fin(ts_read_as_f64(c->src, src_row));
    else write_col_i64(out, out_row, ts_read_as_i64(c->src, src_row), c->dst->type, c->dst->attrs);
}

static void ts_mminmax_task_fn(void* vctx, uint32_t worker_id, int64_t start, int64_t end) {
    (void)worker_id;
    ts_mminmax_ctx_t* c = (ts_mminmax_ctx_t*)vctx;
    if (atomic_load_explicit(&c->oom, memory_order_relaxed)) return;

    int64_t n = c->src->len;
    for (int64_t task = start; task < end; task++) {
        int64_t lo = ts_task_lo(n, task, c->ntasks);
        int64_t hi = ts_task_lo(n, task + 1, c->ntasks);
        if (lo >= hi) continue;
        int64_t scan_lo = (lo >= c->window - 1) ? (lo - c->window + 1) : 0;
        int64_t scan_len = hi - scan_lo;
        int64_t cap_i64 = c->window < scan_len ? c->window : scan_len;
        if (cap_i64 < 1) cap_i64 = 1;
        if (ts_size_overflows(cap_i64, sizeof(int64_t))) {
            atomic_store_explicit(&c->oom, 1, memory_order_relaxed);
            return;
        }

        ray_t* dq_hdr = NULL;
        int64_t* dq = (int64_t*)scratch_alloc(&dq_hdr, (size_t)cap_i64 * sizeof(int64_t));
        if (!dq) {
            atomic_store_explicit(&c->oom, 1, memory_order_relaxed);
            return;
        }

        int64_t head = 0;
        int64_t len = 0;
        for (int64_t r = scan_lo; r < hi; r++) {
            if (ts_morsel_checkpoint(c->pool, r - scan_lo)) { scratch_free(dq_hdr); return; }
            int64_t expire = (r >= c->window - 1) ? (r - c->window + 1) : 0;
            while (len > 0 && dq[head] < expire) {
                head = (head + 1) % cap_i64;
                len--;
            }

            bool nn = !(c->has_nulls && ray_vec_is_null(c->src, r));
            if (nn) {
                while (len > 0) {
                    int64_t back_pos = (head + len - 1) % cap_i64;
                    if (!ts_mminmax_better(c, r, dq[back_pos])) break;
                    len--;
                }
                dq[(head + len) % cap_i64] = r;
                len++;
            }

            if (r >= lo) {
                if (len == 0) ray_vec_set_null(c->dst, r, true);
                else          ts_mminmax_write(c, r, dq[head]);
            }
        }
        scratch_free(dq_hdr);
    }
}

static ray_t* moving_minmax_vec_eager(ray_t* x, uint16_t opcode, int64_t window, int8_t out_type) {
    int64_t n = x->len;
    ray_t* out = ray_vec_new(out_type, n);
    if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
    out->len = n;
    if (n == 0) return out;

    ray_pool_t* pool = ray_pool_get();
    uint32_t workers = pool ? pool->n_workers + 1 : 1;
    int64_t overlap_tasks = (window > 0) ? (n + window - 1) / window : 1;
    if (overlap_tasks < 1) overlap_tasks = 1;
    uint32_t ntasks = workers;
    if ((int64_t)ntasks > overlap_tasks) ntasks = (uint32_t)overlap_tasks;
    if (ntasks < 1) ntasks = 1;
    if (n < RAY_PARALLEL_THRESHOLD) ntasks = 1;

    ts_mminmax_ctx_t ctx = {
        .src = x, .dst = out, .pool = pool,
        .opcode = opcode,
        .is_float = (x->type == RAY_F64 || x->type == RAY_F32),
        .has_nulls = ray_vec_may_have_nulls(x),
        .window = window,
        .ntasks = (int64_t)ntasks
    };
    atomic_store_explicit(&ctx.oom, 0, memory_order_relaxed);

    if (ntasks == 1) ts_mminmax_task_fn(&ctx, 0, 0, 1);
    else             ray_pool_dispatch_n(pool, ts_mminmax_task_fn, &ctx, ntasks);

    if (atomic_load_explicit(&ctx.oom, memory_order_relaxed)) {
        ray_release(out);
        return ray_error("oom", NULL);
    }
    if (pool_cancelled(pool)) {
        ray_release(out);
        return ray_error("cancel", "interrupted");
    }
    return out;
}

ray_t* moving_vec_eager(ray_t* x, uint16_t opcode, int64_t window) {
    if (!x || RAY_IS_ERR(x)) return x;
    if (!ray_is_vec(x)) return ray_error("type", "%s expects a vector input", ts_moving_name(opcode));
    if (window < 1) return ray_error("range", "%s window must be >= 1", ts_moving_name(opcode));
    if (RAY_IS_PARTED(x->type)) {
        ray_t* flat = parted_to_flat_vec(x);
        if (!flat || RAY_IS_ERR(flat)) return flat ? flat : ray_error("oom", NULL);
        ray_t* out = moving_vec_eager(flat, opcode, window);
        ray_release(flat);
        return out;
    }
    if (!ts_moving_type(opcode, x->type)) {
        return ray_error("type", "%s expects %s, got %s",
                         ts_moving_name(opcode),
                         (opcode == OP_MMIN || opcode == OP_MMAX) ? "a numeric or temporal vector" :
                         (opcode == OP_MCOUNT) ? "a vector" : "a numeric vector",
                         ray_type_name(x->type));
    }

    int8_t out_type = ts_moving_out_type(opcode, x->type);
    if (opcode == OP_MMIN || opcode == OP_MMAX)
        return moving_minmax_vec_eager(x, opcode, window, out_type);
    return moving_prefix_vec_eager(x, opcode, window, out_type);
}

static ray_t* ts_start_lazy_unary(ray_t* x, ray_op_t* (*ctor)(ray_graph_t*, ray_op_t*)) {
    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* in = ray_graph_input_vec(g, x);
    ray_op_t* op = ctor(g, in);
    return ray_lazy_wrap(g, op);
}

static ray_t* ts_start_lazy_moving(ray_t* x, uint16_t opcode, int64_t window) {
    ray_graph_t* g = ray_graph_new(NULL);
    if (!g) return ray_error("oom", NULL);
    ray_op_t* in = ray_graph_input_vec(g, x);
    ray_op_t* op = NULL;
    switch (opcode) {
        case OP_MSUM:   op = ray_msum_op(g, in, window); break;
        case OP_MAVG:   op = ray_mavg_op(g, in, window); break;
        case OP_MMIN:   op = ray_mmin_op(g, in, window); break;
        case OP_MMAX:   op = ray_mmax_op(g, in, window); break;
        case OP_MCOUNT: op = ray_mcount_op(g, in, window); break;
        case OP_MVAR:   op = ray_mvar_op(g, in, window); break;
        case OP_MDEV:   op = ray_mdev_op(g, in, window); break;
    }
    if (!op) { ray_graph_free(g); return ray_error("oom", NULL); }
    return ray_lazy_wrap(g, op);
}

static bool ts_window_arg(ray_t* n, int64_t* out) {
    if (!n || !ray_is_atom(n) || RAY_ATOM_IS_NULL(n)) return false;
    switch (n->type) {
        case -RAY_I64:  *out = n->i64; return true;
        case -RAY_I32:  *out = (int64_t)n->i32; return true;
        case -RAY_I16:  *out = (int64_t)n->i16; return true;
        case -RAY_U8:
        case -RAY_BOOL: *out = (int64_t)n->u8; return true;
        default: return false;
    }
}

static ray_t* ts_moving_fn(ray_t* n, ray_t* x, uint16_t opcode) {
    int64_t window = 0;
    if (!ts_window_arg(n, &window))
        return ray_error("type", "%s window must be an integer atom", ts_moving_name(opcode));
    if (window < 1)
        return ray_error("range", "%s window must be >= 1", ts_moving_name(opcode));
    if (ray_is_lazy(x)) return ray_lazy_append_param(x, opcode, window);
    if (ray_is_vec(x)) return ts_start_lazy_moving(x, opcode, window);
    return ray_error("type", "%s expects a vector input, got %s", ts_moving_name(opcode), ray_type_name(x ? x->type : 0));
}

ray_t* ray_lag_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_LAG);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_lag_op);
    return ray_error("type", "lag expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_lead_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_LEAD);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_lead_op);
    return ray_error("type", "lead expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_deltas_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_DELTAS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_deltas_op);
    return ray_error("type", "deltas expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_ratios_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_RATIOS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_ratios_op);
    return ray_error("type", "ratios expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_fills_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_FILLS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_fills_op);
    if (x && x->type == RAY_TABLE) {
        int64_t ncols = ray_table_ncols(x);
        ray_t* out = ray_table_new(ncols);
        if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);

        for (int64_t c = 0; c < ncols; c++) {
            ray_t* col = ray_table_get_col_idx(x, c);
            ray_t* filled = fills_vec_eager(col);
            if (!filled || RAY_IS_ERR(filled)) {
                ray_release(out);
                return filled ? filled : ray_error("oom", NULL);
            }

            out = ray_table_add_col(out, ray_table_col_name(x, c), filled);
            ray_release(filled);
            if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL);
        }
        return out;
    }
    return ray_error("type", "fills expects a vector or table input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_sums_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_SUMS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_sums_op);
    return ray_error("type", "sums expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_avgs_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_AVGS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_avgs_op);
    return ray_error("type", "avgs expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_mins_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_MINS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_mins_op);
    return ray_error("type", "mins expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_maxs_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_MAXS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_maxs_op);
    return ray_error("type", "maxs expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_prds_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_PRDS);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_prds_op);
    return ray_error("type", "prds expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_differ_fn(ray_t* x) {
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_DIFFER);
    if (ray_is_vec(x)) return ts_start_lazy_unary(x, ray_differ_op);
    return ray_error("type", "differ expects a vector input, got %s", ray_type_name(x ? x->type : 0));
}

ray_t* ray_msum_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MSUM);
}

ray_t* ray_mavg_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MAVG);
}

ray_t* ray_mmin_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MMIN);
}

ray_t* ray_mmax_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MMAX);
}

ray_t* ray_mcount_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MCOUNT);
}

ray_t* ray_mvar_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MVAR);
}

ray_t* ray_mdev_fn(ray_t* n, ray_t* x) {
    return ts_moving_fn(n, x, OP_MDEV);
}

/* (reverse vec) — reverse a vector */
ray_t* ray_reverse_fn(ray_t* x) {
    if (!x || RAY_IS_ERR(x)) return x;

    /* Extend an existing lazy chain. */
    if (ray_is_lazy(x)) return ray_lazy_append(x, OP_REVERSE);

    if (ray_is_atom(x)) { ray_retain(x); return x; }

    /* Typed vector path: start a fresh lazy chain.  The DAG executor will
     * call reverse_vec_eager() when the chain is materialised. */
    if (ray_is_vec(x)) {
        ray_graph_t* g = ray_graph_new(NULL);
        if (!g) return ray_error("oom", NULL);
        ray_op_t* in = ray_graph_input_vec(g, x);
        ray_op_t* op = ray_reverse_op(g, in);
        return ray_lazy_wrap(g, op);
    }

    /* Boxed list path — eager (DAG cannot yet express list reversal) */
    ray_t* _bx = NULL;
    x = unbox_vec_arg(x, &_bx);
    if (RAY_IS_ERR(x)) return x;
    if (!is_list(x)) { if (_bx) ray_release(_bx); return ray_error("type", "reverse: argument must be a list, got %s", ray_type_name(x->type)); }
    int64_t len = ray_len(x);
    ray_t** elems = (ray_t**)ray_data(x);

    ray_t* result = ray_alloc(len * sizeof(ray_t*));
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = len;
    ray_t** out = (ray_t**)ray_data(result);
    for (int64_t i = 0; i < len; i++) {
        ray_retain(elems[len - 1 - i]);
        out[i] = elems[len - 1 - i];
    }
    if (_bx) ray_release(_bx);
    return result;
}

/* ══════════════════════════════════════════
 * Binary search
 * ══════════════════════════════════════════ */

/* (rand n max) → vector of n random i64 in [0, max) */
ray_t* ray_rand_fn(ray_t* a, ray_t* b) {
    if (!ray_is_atom(a) || !ray_is_atom(b)) return ray_error("type", "rand: count and max must be atoms, got %s and %s", ray_type_name(a->type), ray_type_name(b->type));
    int64_t n, mx;
    if (a->type == -RAY_I64) n = a->i64;
    else if (a->type == -RAY_I32) n = a->i32;
    else return ray_error("type", "rand: count must be an integer, got %s", ray_type_name(a->type));
    if (b->type == -RAY_I64) mx = b->i64;
    else if (b->type == -RAY_I32) mx = b->i32;
    else return ray_error("type", "rand: max must be an integer, got %s", ray_type_name(b->type));
    if (n < 0) return ray_error("domain", "rand: count must be non-negative, got %lld", (long long)n);
    if (mx <= 0) return ray_error("domain", "rand: max must be positive, got %lld", (long long)mx);
    if (n == 0) return ray_vec_new(RAY_I64, 0);
    ray_t* vec = ray_vec_new(RAY_I64, n);
    if (RAY_IS_ERR(vec)) return vec;
    int64_t* d = (int64_t*)ray_data(vec);
    for (int64_t i = 0; i < n; i++) d[i] = (int64_t)(rand() % mx);
    vec->len = n;
    return vec;
}

/* (bin sorted-vec val) → rightmost index where sorted[i] <= val, -1 if none */
ray_t* ray_bin_fn(ray_t* sorted, ray_t* val) {
    if (!ray_is_vec(sorted) || sorted->type != RAY_I64)
        return ray_error("type", "bin: sorted arg must be an i64 vector, got %s", ray_type_name(sorted->type));
    int64_t* d = (int64_t*)ray_data(sorted);
    int64_t n = sorted->len;

    if (ray_is_atom(val) && (val->type == -RAY_I64 || val->type == -RAY_I32)) {
        int64_t v = val->i64;
        int64_t lo = 0, hi = n - 1, result = -1;
        while (lo <= hi) {
            int64_t mid = lo + (hi - lo) / 2;
            if (d[mid] <= v) { result = mid; lo = mid + 1; }
            else hi = mid - 1;
        }
        return ray_i64(result);
    }
    if (ray_is_vec(val) && val->type == RAY_I64) {
        int64_t* vals = (int64_t*)ray_data(val);
        int64_t vn = val->len;
        ray_t* rvec = ray_vec_new(RAY_I64, vn);
        if (RAY_IS_ERR(rvec)) return rvec;
        int64_t* out = (int64_t*)ray_data(rvec);
        for (int64_t i = 0; i < vn; i++) {
            int64_t v = vals[i];
            int64_t lo = 0, hi = n - 1, r = -1;
            while (lo <= hi) {
                int64_t mid = lo + (hi - lo) / 2;
                if (d[mid] <= v) { r = mid; lo = mid + 1; }
                else hi = mid - 1;
            }
            out[i] = r;
        }
        rvec->len = vn;
        return rvec;
    }
    return ray_error("type", "bin: val must be an i64 atom or vector, got %s", ray_type_name(val->type));
}

/* (binr sorted-vec val) → leftmost index where sorted[i] >= val */
ray_t* ray_binr_fn(ray_t* sorted, ray_t* val) {
    if (!ray_is_vec(sorted) || sorted->type != RAY_I64)
        return ray_error("type", "binr: sorted arg must be an i64 vector, got %s", ray_type_name(sorted->type));
    int64_t* d = (int64_t*)ray_data(sorted);
    int64_t n = sorted->len;

    if (ray_is_atom(val) && (val->type == -RAY_I64 || val->type == -RAY_I32)) {
        int64_t v = val->i64;
        int64_t lo = 0, hi = n - 1, result = n;
        while (lo <= hi) {
            int64_t mid = lo + (hi - lo) / 2;
            if (d[mid] >= v) { result = mid; hi = mid - 1; }
            else lo = mid + 1;
        }
        return ray_i64(result >= n ? n - 1 : result);
    }
    if (ray_is_vec(val) && val->type == RAY_I64) {
        int64_t* vals = (int64_t*)ray_data(val);
        int64_t vn = val->len;
        ray_t* rvec = ray_vec_new(RAY_I64, vn);
        if (RAY_IS_ERR(rvec)) return rvec;
        int64_t* out = (int64_t*)ray_data(rvec);
        for (int64_t i = 0; i < vn; i++) {
            int64_t v = vals[i];
            int64_t lo = 0, hi = n - 1, r = n;
            while (lo <= hi) {
                int64_t mid = lo + (hi - lo) / 2;
                if (d[mid] >= v) { r = mid; hi = mid - 1; }
                else lo = mid + 1;
            }
            out[i] = r >= n ? n - 1 : r;
        }
        rvec->len = vn;
        return rvec;
    }
    return ray_error("type", "binr: val must be an i64 atom or vector, got %s", ray_type_name(val->type));
}

/* ══════════════════════════════════════════
 * Map variants
 * ══════════════════════════════════════════ */

/* (map-left fn fixed vec) → apply fn(fixed, elem) for each elem in vec */
/* Helper for map-left/map-right: iterate over vec calling fn with two args */
static ray_t* map_iterate(ray_t* fn, ray_t* fixed, ray_t* vec, int fixed_is_left) {
    /* If both are scalars, just call once */
    if (!ray_is_vec(vec) && vec->type != RAY_LIST) {
        if (fixed_is_left)
            return call_fn2(fn, fixed, vec);
        else
            return call_fn2(fn, vec, fixed);
    }

    int64_t vn = vec->len;
    ray_t* stack_results[4096];
    ray_t** results = stack_results;
    if (vn > 4096) {
        results = (ray_t**)ray_calloc_raw((size_t)vn * sizeof(ray_t*));
        if (!results) return ray_error("oom", NULL);
    }

    for (int64_t i = 0; i < vn; i++) {
        int alloc = 0;
        ray_t* elem = collection_elem(vec, i, &alloc);
        if (fixed_is_left)
            results[i] = call_fn2(fn, fixed, elem);
        else
            results[i] = call_fn2(fn, elem, fixed);
        if (alloc) ray_release(elem);
        if (RAY_IS_ERR(results[i])) {
            ray_t* err = results[i];
            for (int64_t j = 0; j < i; j++) ray_release(results[j]);
            if (results != stack_results) ray_free_raw(results);
            return err;
        }
    }
    ray_t* out = ray_enlist_fn(results, vn);
    for (int64_t i = 0; i < vn; i++) ray_release(results[i]);
    if (results != stack_results) ray_free_raw(results);
    return out;
}

/* (map-left fn left right): iterate the LEFT operand, pairing each element with
 * the whole RIGHT → fn(left_i, right).
 * (map-right fn left right): iterate the RIGHT operand, pairing the whole LEFT
 * with each element → fn(left, right_i).
 *
 * The iterated side is fixed by the operator, not auto-detected. When that side
 * is an atom there is nothing to iterate, so fn is applied once to (left,right);
 * for an atomic fn that single call is exactly its broadcast over the other
 * operand, so map-left/map-right agree with plain atomic application there.
 * Every call goes through call_fn2, which routes atomic builtins through the
 * broadcasting engine (so a held vector conforms element-wise) and hands
 * lambdas their arguments whole. */
ray_t* ray_map_left_fn(ray_t** args, int64_t n) {
    if (n != 3) return ray_error("domain", "map-left: requires exactly 3 args (fn, left, right), got %lld", (long long)n);
    /* iterate left (vec), hold right (fixed) → fn(left_i, right) */
    return map_iterate(args[0], args[2], args[1], 0);
}

ray_t* ray_map_right_fn(ray_t** args, int64_t n) {
    if (n != 3) return ray_error("domain", "map-right: requires exactly 3 args (fn, left, right), got %lld", (long long)n);
    /* iterate right (vec), hold left (fixed) → fn(left, right_i) */
    return map_iterate(args[0], args[1], args[2], 1);
}

/* ══════════════════════════════════════════
 * Fold/scan variants
 * ══════════════════════════════════════════ */

/* (fold-left fn init coll) — left fold with explicit initial value */
ray_t* ray_fold_left_fn(ray_t** args, int64_t n) {
    /* Same as (fold fn init coll) — fold already goes left-to-right */
    return ray_fold_fn(args, n);
}

/* (fold-while pred f init coll) — a fold that stops when the running
 * result says stop.
 *
 * Before each step the accumulator is offered to `pred`; a falsy answer
 * ends the fold and yields the accumulator as it stands.  The test comes
 * BEFORE the first element, so a predicate that is false at the start
 * returns `init` untouched and touches nothing.
 *
 * Deliberately unlike ray_fold_fn in one respect: that routes its
 * collection through unbox_vec_arg -> to_boxed_list, boxing every element
 * up front.  For a primitive whose whole purpose is to stop early, paying
 * for the tail it never reaches is exactly the cost being removed here
 * (issue 588), so elements are pulled one at a time via collection_elem —
 * the same way map_iterate walks its input.  Stopping at element three of
 * a million costs three boxed atoms, not a million. */
ray_t* ray_fold_while_fn(ray_t** args, int64_t n) {
    if (n != 4) return ray_error("domain", "fold-while: requires exactly 4 args (pred, fn, init, coll), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* pred = args[0];
    ray_t* fn   = args[1];
    ray_t* coll = args[3];
    if (!is_collection(coll))
        return ray_error("type", "fold-while: coll arg must be a collection, got %s", ray_type_name(coll->type));

    ray_retain(args[2]);
    ray_t* acc = args[2];
    int64_t len = ray_len(coll);
    for (int64_t i = 0; i < len; i++) {
        ray_t* keep = call_fn1(pred, acc);
        if (ray_is_lazy(keep)) keep = ray_lazy_materialize(keep);
        if (RAY_IS_ERR(keep)) { ray_release(acc); return keep; }
        int go = is_truthy(keep);
        ray_release(keep);
        if (!go) return acc;

        int alloc = 0;
        ray_t* elem = collection_elem(coll, i, &alloc);
        ray_t* next = call_fn2(fn, acc, elem);
        if (alloc) ray_release(elem);
        ray_release(acc);
        if (RAY_IS_ERR(next)) return next;
        acc = next;
    }
    return acc;
}

/* (fold-right fn init coll) — right fold */
ray_t* ray_fold_right_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", "fold-right: requires at least 2 args (fn and vec), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* fn = args[0];
    ray_t* _bx = NULL;
    ray_t* acc;
    ray_t* vec;

    if (n == 2) {
        /* (fold-right fn vec) — use last element as initial value */
        vec = unbox_vec_arg(args[1], &_bx);
        if (RAY_IS_ERR(vec)) return vec;
        if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "fold-right: vec arg must be a list, got %s", ray_type_name(vec->type)); }
        int64_t len = ray_len(vec);
        if (len == 0) { if (_bx) ray_release(_bx); return ray_error("domain", "fold-right: empty list has no initial value, got %lld", (long long)len); }
        ray_t** elems = (ray_t**)ray_data(vec);
        ray_retain(elems[len - 1]);
        acc = elems[len - 1];
        for (int64_t i = len - 2; i >= 0; i--) {
            ray_t* next = call_fn2(fn, elems[i], acc);
            ray_release(acc);
            if (RAY_IS_ERR(next)) { if (_bx) ray_release(_bx); return next; }
            acc = next;
        }
        if (_bx) ray_release(_bx);
        return acc;
    }

    /* (fold-right fn init coll) */
    ray_retain(args[1]);
    acc = args[1];
    vec = unbox_vec_arg(args[2], &_bx);
    if (RAY_IS_ERR(vec)) { ray_release(acc); return vec; }
    if (!is_list(vec)) { ray_release(acc); if (_bx) ray_release(_bx); return ray_error("type", "fold-right: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    ray_t** elems = (ray_t**)ray_data(vec);
    for (int64_t i = len - 1; i >= 0; i--) {
        ray_t* next = call_fn2(fn, elems[i], acc);
        ray_release(acc);
        if (RAY_IS_ERR(next)) { if (_bx) ray_release(_bx); return next; }
        acc = next;
    }
    if (_bx) ray_release(_bx);
    return acc;
}

/* (scan-left fn vec) — running left fold (same as scan) */
ray_t* ray_scan_left_fn(ray_t** args, int64_t n) {
    return ray_scan_fn(args, n);
}

/* (scan-right fn vec) — running right fold, returns vector of partial results */
ray_t* ray_scan_right_fn(ray_t** args, int64_t n) {
    if (n < 2) return ray_error("domain", "scan-right: requires at least 2 args (fn and vec), got %lld", (long long)n);
    for (int64_t i = 0; i < n; i++)
        if (ray_is_lazy(args[i])) args[i] = ray_lazy_materialize(args[i]);

    ray_t* fn = args[0];
    ray_t* _bx = NULL;
    ray_t* vec = unbox_vec_arg(args[1], &_bx);
    if (RAY_IS_ERR(vec)) return vec;
    if (!is_list(vec)) { if (_bx) ray_release(_bx); return ray_error("type", "scan-right: vec arg must be a list, got %s", ray_type_name(vec->type)); }
    int64_t len = ray_len(vec);
    if (len == 0) {
        if (_bx) ray_release(_bx);
        ray_t* result = ray_alloc(0);
        if (!result) return ray_error("oom", NULL);
        result->type = RAY_LIST;
        result->len = 0;
        return result;
    }

    ray_t* result = ray_alloc(len * sizeof(ray_t*));
    if (!result) { if (_bx) ray_release(_bx); return ray_error("oom", NULL); }
    result->type = RAY_LIST;
    result->len = len;
    ray_t** out = (ray_t**)ray_data(result);
    ray_t** elems = (ray_t**)ray_data(vec);

    ray_retain(elems[len - 1]);
    out[len - 1] = elems[len - 1];
    for (int64_t i = len - 2; i >= 0; i--) {
        out[i] = call_fn2(fn, elems[i], out[i + 1]);
        if (RAY_IS_ERR(out[i])) {
            for (int64_t j = i + 1; j < len; j++) ray_release(out[j]);
            result->len = 0; ray_release(result); if (_bx) ray_release(_bx);
            return out[i];
        }
    }
    if (_bx) ray_release(_bx);
    return result;
}