rudb-exec 0.6.0

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

use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, OnceLock};

use rudb_catalog::Table;
use rudb_common::stage::{self, Stage};
use rudb_common::{Error, Field, LogicalType, Result, Session, Value};
use rudb_csv::{Part, Reader as CsvReader, Split};
use rudb_encoding::sequence::Sequence;
use rudb_functions::{
    FILE_ROW_NUMBER, Given, TableFunction, csv_given, open_csv, open_parquet, series_length,
};
use rudb_graph::Rids;
use rudb_kernels::{Stepping, cast, moment_steps};
use rudb_metrics::Counters;
use rudb_native::{Reader as NativeReader, RunProjectionPart, RunProjectionScan};
use rudb_parquet::{Bound, Op, Reader, Test, skips};
use rudb_pipeline::{Compaction, Gauge, Morsel, Progress, Source, narrow};
use rudb_plan::{ConjunctionOp, Expr, ExprRef, Node, NodeRef, Plan, Slice};
use rudb_seam::{Context, SeamId, Settings};
use rudb_storage::Probe;
use rudb_vector::{Chunk, Data, Selection, VECTOR_SIZE, Vector};

use crate::cutoff::Cutoff;
use crate::expr::{evaluate_all, evaluate_all_in_time_zone};
use crate::prepared::{Prepared, Scratch};
use crate::register::compaction;
use crate::schema::Schema;
use crate::sideways::{Keys, Sideways};
use crate::stream::{later_passes, marking_pays};
use crate::table::{Across, hash};

/// One morsel per position, handed to whoever asks first.
///
/// The three sources that already have their chunks, or can read one by number, hand out a morsel
/// per chunk, and this is the whole of the sharing that needs: a counter, one fetch and add per
/// morsel, and no lock held while anything is read. It is what makes the difference between two
/// threads scanning a table and two threads waiting for each other.
#[derive(Debug)]
pub(crate) struct Handout {
    next: AtomicU64,
    total: u64,
}

impl Handout {
    /// A handout over `total` positions.
    pub(crate) fn new(total: usize) -> Self {
        Self { next: AtomicU64::new(0), total: u64::try_from(total).unwrap_or(u64::MAX) }
    }

    /// How many positions there are in all, which is how many morsels this will ever hand out.
    pub(crate) fn total(&self) -> usize {
        usize::try_from(self.total).unwrap_or(usize::MAX)
    }

    /// The next position, as a morsel covering it, or `None` when they are all taken.
    pub(crate) fn take(&self) -> Option<Morsel> {
        let at = self.next.fetch_add(1, Ordering::Relaxed);
        (at < self.total).then(|| Morsel::new(at, at, at + 1))
    }

    /// The next position, as a number rather than as a morsel covering it.
    ///
    /// For a source whose morsel covers something other than the position it counted, which is a
    /// scan handing out stripes: it counts stripes here and looks up the parts they hold.
    pub(crate) fn number(&self) -> Option<u64> {
        let at = self.next.fetch_add(1, Ordering::Relaxed);
        (at < self.total).then_some(at)
    }
}

/// Where a morsel has got to, as an index into whatever the source counts.
pub(crate) fn position(morsel: &Morsel) -> usize {
    usize::try_from(morsel.cursor()).unwrap_or(usize::MAX)
}

/// Rows for a bounded grouped source derived while a query runs.
#[derive(Debug)]
pub(crate) struct Frequencies {
    chunks: Vec<Chunk>,
    handout: Handout,
}

impl Frequencies {
    /// Builds exact aggregate rows supplied by a certified native derived-group synopsis.
    pub(crate) fn records(schema: Schema, records: Vec<Vec<Value>>) -> Result<Self> {
        let types = schema.types();
        let mut chunks = Vec::with_capacity(records.len().div_ceil(VECTOR_SIZE));
        for records in records.chunks(VECTOR_SIZE) {
            let mut columns = vec![Vec::with_capacity(records.len()); types.len()];
            for record in records {
                if record.len() != types.len() {
                    return Err(Error::internal("a stored aggregate row has the wrong width"));
                }
                for (values, value) in columns.iter_mut().zip(record) {
                    values.push(value.clone());
                }
            }
            let vectors = columns
                .into_iter()
                .zip(&types)
                .map(|(values, ty)| Vector::from_values(ty.clone(), &values))
                .collect::<Result<Vec<_>>>()?;
            chunks.push(Chunk::with_rows(vectors, records.len())?);
        }
        let handout = Handout::new(chunks.len());
        Ok(Self { chunks, handout })
    }

    /// Builds already evaluated multi-column grouped counts.
    pub(crate) fn grouped(schema: Schema, entries: Vec<(Vec<Value>, u64)>) -> Result<Self> {
        let output_types = schema.types();
        let Some((&LogicalType::BigInt, group_types)) = output_types.split_last() else {
            return Err(Error::internal("a grouped frequency source count is not BIGINT"));
        };
        let mut chunks = Vec::with_capacity(entries.len().div_ceil(VECTOR_SIZE));
        for entries in entries.chunks(VECTOR_SIZE) {
            let mut columns = vec![Vec::with_capacity(entries.len()); group_types.len()];
            let mut counts = Vec::with_capacity(entries.len());
            for (keys, count) in entries {
                if keys.len() != group_types.len() {
                    return Err(Error::internal("a grouped frequency key has the wrong width"));
                }
                for (column, key) in columns.iter_mut().zip(keys) {
                    column.push(key.clone());
                }
                counts.push(
                    i64::try_from(*count)
                        .map_err(|_| Error::internal("a stored frequency exceeds BIGINT"))?,
                );
            }
            let mut output = columns
                .into_iter()
                .zip(group_types)
                .map(|(values, ty)| Vector::from_values(ty.clone(), &values))
                .collect::<Result<Vec<_>>>()?;
            output.push(Vector::flat(LogicalType::BigInt, Data::Int64(counts.into()))?);
            chunks.push(Chunk::with_rows(output, entries.len())?);
        }
        let handout = Handout::new(chunks.len());
        Ok(Self { chunks, handout })
    }
}

impl Source for Frequencies {
    fn morsel(&self) -> Option<Morsel> {
        self.handout.take()
    }

    fn morsels(&self, _threads: usize, _weight: usize) -> Option<usize> {
        Some(self.handout.total())
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        let at = position(morsel);
        *out = self
            .chunks
            .get(at)
            .ok_or_else(|| Error::internal("a frequency morsel is out of range"))?
            .clone();
        morsel.advance(1);
        Ok(Progress::Done)
    }
}

/// A grouped distinct aggregate evaluated from a current row-preserving covering projection.
///
/// The source is chosen from bound columns, and the projection is scanned when the pipeline runs.
/// It stores original covered codes, not group counts. The ordinary operators above this source
/// still apply projection, ordering, and limits.
#[derive(Debug)]
pub(crate) struct ProjectionDistinct<'a> {
    reader: &'a NativeReader,
    order: usize,
    covered: usize,
    schema: Schema,
    handout: Handout,
    partitions: AtomicUsize,
    scanner: Mutex<Option<Arc<RunProjectionScan<'a>>>>,
    output: Mutex<ProjectionOutput>,
}

#[derive(Debug, Default)]
struct ProjectionOutput {
    parts: Vec<Option<RunProjectionPart>>,
    finished: usize,
    chunks: Option<Vec<Chunk>>,
    next: usize,
}

impl<'a> ProjectionDistinct<'a> {
    pub(crate) fn new(
        reader: &'a NativeReader,
        order: usize,
        covered: usize,
        schema: Schema,
    ) -> Self {
        Self {
            reader,
            order,
            covered,
            schema,
            handout: Handout::new(8),
            partitions: AtomicUsize::new(1),
            scanner: Mutex::new(None),
            output: Mutex::new(ProjectionOutput::default()),
        }
    }

    fn chunks(&self, rows: Vec<(i32, u64)>) -> Result<Vec<Chunk>> {
        let group_type = self.schema.types()[0].clone();
        let entries = rows
            .into_iter()
            .map(|(group, count)| {
                let value = match group_type {
                    LogicalType::TinyInt => Value::TinyInt(
                        i8::try_from(group)
                            .map_err(|_| Error::internal("a projection group exceeds TINYINT"))?,
                    ),
                    LogicalType::SmallInt => Value::SmallInt(
                        i16::try_from(group)
                            .map_err(|_| Error::internal("a projection group exceeds SMALLINT"))?,
                    ),
                    LogicalType::Integer => Value::Integer(group),
                    _ => return Err(Error::internal("a projection group has an unsupported type")),
                };
                Ok((vec![value], count))
            })
            .collect::<Result<Vec<_>>>()?;
        Ok(Frequencies::grouped(self.schema.clone(), entries)?.chunks)
    }
}

impl Source for ProjectionDistinct<'_> {
    fn morsel(&self) -> Option<Morsel> {
        self.handout.take().filter(|morsel| {
            usize::try_from(morsel.index()).unwrap_or(usize::MAX)
                < self.partitions.load(Ordering::Relaxed)
        })
    }

    fn morsels(&self, threads: usize, _weight: usize) -> Option<usize> {
        // Smaller projections stay on one worker. Larger ones divide whole pages among the
        // workers already leased by the engine, without a private thread pool.
        let rows = self.reader.table().rows();
        // One projection page cannot hold 524,288 covered codes, even at one byte per code.
        // This keeps the requested partition count at or below the stored page count.
        let partitions = threads.clamp(1, 8).min(rows.div_ceil(1 << 19).max(1));
        self.partitions.store(partitions, Ordering::Relaxed);
        Some(partitions)
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        let mut held =
            self.output.lock().map_err(|_| Error::internal("projection output lock poisoned"))?;
        if held.chunks.is_none() && self.partitions.load(Ordering::Relaxed) == 1 {
            let rows = self
                .reader
                .grouped_distinct_run_projection_with_workers(
                    self.order,
                    self.covered,
                    usize::MAX,
                    1,
                )?
                .ok_or_else(|| Error::internal("a selected covering projection disappeared"))?;
            held.chunks = Some(self.chunks(rows)?);
        }
        if held.chunks.is_none() && held.parts.get(position(morsel)).is_none_or(Option::is_none) {
            drop(held);
            let scanner = {
                let mut slot = self
                    .scanner
                    .lock()
                    .map_err(|_| Error::internal("projection scanner lock poisoned"))?;
                if slot.is_none() {
                    *slot = Some(Arc::new(
                        self.reader.run_projection_scan(self.order, self.covered)?.ok_or_else(
                            || Error::internal("a selected covering projection disappeared"),
                        )?,
                    ));
                }
                Arc::clone(slot.as_ref().expect("projection scanner initialized"))
            };
            let partitions = self.partitions.load(Ordering::Relaxed).min(scanner.pages());
            let part = position(morsel);
            if part >= partitions {
                *out = Chunk::empty(&self.schema.types());
                morsel.advance(1);
                return Ok(Progress::Done);
            }
            let scanned = scanner.partition(part, partitions)?;
            held = self
                .output
                .lock()
                .map_err(|_| Error::internal("projection output lock poisoned"))?;
            if held.parts.is_empty() {
                held.parts.resize_with(partitions, || None);
            }
            held.parts[part] = Some(scanned);
            held.finished += 1;
            if held.finished < partitions {
                *out = Chunk::empty(&self.schema.types());
                morsel.advance(1);
                return Ok(Progress::Done);
            }
            let parts = held
                .parts
                .iter_mut()
                .map(|part| part.take().expect("all parts finished"))
                .collect::<Vec<_>>();
            let rows = scanner.finish(parts, usize::MAX)?;
            held.chunks = Some(self.chunks(rows)?);
        }
        let chunks = held.chunks.as_ref().expect("projection output was initialized");
        let total = chunks.len();
        let next = chunks.get(held.next).cloned();
        if let Some(chunk) = next {
            *out = chunk;
            held.next += 1;
            if held.next < total {
                return Ok(Progress::More);
            }
        } else {
            *out = Chunk::empty(&self.schema.types());
        }
        morsel.advance(1);
        Ok(Progress::Done)
    }
}

/// A whole-table aggregate answered from what the file already wrote down.
///
/// One row, worked out before the pipeline starts, so there is nothing to scan and nothing to
/// combine. What can be answered this way is decided in the builder, and this only carries the
/// answer, because the interesting part is which questions a file can settle and not how a single
/// row is handed out.
#[derive(Debug)]
pub(crate) struct Summary {
    row: Chunk,
    handout: Handout,
}

impl Summary {
    /// The one row, with a value per aggregate in the order the aggregates were written.
    pub(crate) fn new(schema: &Schema, values: &[Value]) -> Result<Self> {
        let types = schema.types();
        if types.len() != values.len() {
            return Err(Error::internal("a summary has a different number of values and columns"));
        }
        let columns = types
            .iter()
            .zip(values)
            .map(|(ty, value)| Vector::from_values(ty.clone(), std::slice::from_ref(value)))
            .collect::<Result<Vec<_>>>()?;
        Ok(Self { row: Chunk::with_rows(columns, 1)?, handout: Handout::new(1) })
    }
}

impl Source for Summary {
    fn morsel(&self) -> Option<Morsel> {
        self.handout.take()
    }

    fn morsels(&self, _threads: usize, _weight: usize) -> Option<usize> {
        Some(self.handout.total())
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        *out = self.row.clone();
        morsel.advance(1);
        Ok(Progress::Done)
    }
}

fn poisoned<T>(_: T) -> Error {
    Error::internal("a thread panicked while reading a file")
}

/// A base table scan.
///
/// `columns` is the position in the stored table of each column the plan asked for, worked out once
/// when the operator is built. The plan's projection is a list of fields and the table's columns are
/// a list of fields, and they are the same list today only because the binder projects every column
/// in order. Resolving by name rather than assuming that is what keeps this operator correct after
/// projection pushdown makes the plan's list a subset, which is the M1 change section 9.2 describes
/// as the difference between 20 GB and 200 MB on ClickBench.
///
/// A morsel here is one stored chunk, because that is the unit the table hands back and reading
/// half of one costs the same as reading all of it. Over a native file with enough stripes to go
/// round it is a whole stripe instead, which is sixty four chunks, for the reason [`Scan::stripes`]
/// gives.
///
/// `probes` is what the filter above this scan already knows, in the same shape [`FileScan`] takes
/// it, and it is answered against the table's zone maps a chunk at a time. That is a finer unit than
/// the Parquet path gets: a row group on the files this engine is measured against is a hundred
/// thousand rows and a chunk is two thousand and forty eight, and on a selective filter over a
/// clustered column that is most of the difference between the two paths.
///
/// `sideways` is the other source of the same kind of test, and it is one a plan cannot carry: the
/// range of the key a join above this scan is about to look every one of these rows up by, which is
/// not known until that join's other side has finished. See [`crate::sideways`].
///
/// `cutoff` is a third, and it is the only one that keeps changing while the scan runs: how good a
/// row has to be to still be wanted by the top N above. See [`crate::cutoff`].
#[derive(Debug)]
pub(crate) struct Scan<'a> {
    table: &'a Table,
    columns: Vec<Option<usize>>,
    offsets: Vec<i64>,
    probes: Vec<Probe>,
    /// The runtime filter of the join this scan drives, empty for a scan that drives no join.
    sideways: Option<Arc<Sideways<'a>>>,
    /// The runtime filters of joins further up that reached this scan through the joins between,
    /// each with its own count of whether it is paying. Only their ranges, bitmaps and filters are
    /// read. The exact rows a join can hand down are positions counted from this join's own side,
    /// and those stay with `sideways`.
    also: Vec<(Arc<Sideways<'a>>, Paying)>,
    /// The cutoff of the top N above this scan, empty for a scan with no top N that could use one.
    cutoff: Option<Arc<Cutoff>>,
    /// Which table index this scan's columns bind against, which is how the runtime filter knows
    /// whether it is about one of them.
    index: u32,
    /// `probes` and whatever the runtime filter turned out to hold, worked out the first time the
    /// scan is asked anything.
    ///
    /// Once rather than per chunk, and lazily rather than at construction, because the one moment
    /// both are known is after the pipeline this one depends on has finished and before this one has
    /// started. That is [`Source::morsels`], and every read is after it.
    testing: OnceLock<Vec<Probe>>,
    schema: Schema,
    chunks: Handout,
    /// The parts of each stripe, empty when the rows are not native.
    ///
    /// A part is a quarter of a megabyte of a column and a stripe holds sixty four of them under
    /// one page, so a worker that takes a part takes a sixty fourth of a read. Handing parts out
    /// one at a time puts every worker in the same stripe at the same moment, and the reader can
    /// only let one of them read the page: the other fifteen read their own part out of the same
    /// bytes, and the winner reads those bytes again as part of the page. On the ClickBench file a
    /// cold column comes off the disk at 550 MB/s that way, on a disk that does 4 GB/s with one
    /// reader and 7 GB/s with eight, because a quarter of the bytes move twice and the rest move in
    /// reads too small to keep the queue full.
    ///
    /// A morsel that is a whole stripe fixes it at the source. Each worker owns the page it reads,
    /// nobody loses a race, and sixteen workers put sixteen quarter megabyte reads in flight.
    stripes: Vec<Range<usize>>,
    /// The filter this scan applies to the rows it read, when the builder gave it one.
    pushed: Option<Pushed>,
    /// How many chunks the zone maps proved the filter keeps whole, so the comparison never ran.
    waved: AtomicUsize,
    /// The runs of parts a morsel covers, used instead of `chunks` once it is there.
    ///
    /// Empty until [`Source::morsels`] fills it, which is the one moment the scan knows how many
    /// workers it will face and so the one moment it can decide how finely to cut.
    spread: OnceLock<Spread>,
    skipped: AtomicUsize,
    /// The projected string columns the pushed filter does not read, by their place in the
    /// projection, which are read after the filters have run and only for the rows they kept. See
    /// [`Self::read_deferring`].
    deferred: Vec<usize>,
    /// Every projected column the pushed filter does not read, by its place in the projection,
    /// which are read that way instead once a join's bitmap or filter is measured keeping few rows.
    /// See [`Paying::tight`].
    deferrable: Vec<usize>,
    /// What the filters kept of the parts read that way, which stops the deferring once they are
    /// measured keeping most rows, since then the string columns are read nearly whole anyway and
    /// the second read is a cost with nothing to show for it.
    deferring: Paying,
    /// The projected columns only the pushed filter reads, by their place in the projection.
    ///
    /// Empty unless the operator the filter came from is an aggregate or a projection, since those
    /// are the only ones that name every column they read and hide the scan's columns from
    /// everything above them. A column here is left out when a filter narrows a chunk and comes
    /// back as a null constant. On TPC-H q01 that is the ship date, which the filter reads to keep
    /// 98 percent of lineitem and nothing reads again, and narrowing unpacked it for every row.
    unread: Vec<usize>,
    /// Whether the runtime filter has been earning the hash it costs.
    paying: Paying,
    /// What the pushed filter keeps, which decides whether a Bloom filter runs ahead of it.
    passed: Paying,
    /// The operator row this scan reports its part counts to, when it is being measured.
    ///
    /// The scan has counted its own skips since the walk was written and nobody outside could see
    /// them. They are the number that says whether the physical order of the table is doing any
    /// work, so they go on the operator row.
    counters: Option<Arc<Counters>>,
}

/// How many rows go through the runtime filter before it has to justify itself.
const WARMUP: usize = 1 << 16;

/// How few rows in how many a join's filter has to keep before a scan reads its other columns only
/// at the rows it kept, whatever their type. See [`Scan::read_deferring`].
const TIGHT: usize = 16;

/// Whether the runtime filter is worth the hash it costs, counted as the scan goes.
///
/// A filter that turns most rows away pays for itself many times over: the row it drops is a row the
/// join above does not look up, and a lookup into a build side too large for the cache is several
/// times what the filter charges. A filter that turns none away is a hash and a cache line per row
/// spent on nothing. Which of the two a join has is not something either side knows before the rows
/// go past, so the scan applies the filter, counts what it kept, and stops applying it once enough
/// rows have gone by to say it is not paying.
///
/// Giving up is final. Once the scan stops counting the numbers stop moving, so a filter that failed
/// its warmup is never asked again, and the rest of the table goes past at the speed it would have
/// had if the join had never armed anything.
#[derive(Debug, Default)]
struct Paying {
    seen: AtomicUsize,
    kept: AtomicUsize,
}

impl Paying {
    /// Whether the filter has earned another chunk.
    fn worth(&self) -> bool {
        let seen = self.seen.load(Ordering::Relaxed);
        // Under the warmup there is nothing to go on, and a filter is given the benefit of it.
        seen < WARMUP
            || self.kept.load(Ordering::Relaxed).saturating_mul(4) < seen.saturating_mul(3)
    }

    /// Whether the filter has been measured keeping more than half of what it saw.
    fn loose(&self) -> bool {
        let seen = self.seen.load(Ordering::Relaxed);
        seen >= WARMUP && self.kept.load(Ordering::Relaxed).saturating_mul(2) > seen
    }

    /// Whether the filter has been measured keeping under one row in [`TIGHT`].
    fn tight(&self) -> bool {
        let seen = self.seen.load(Ordering::Relaxed);
        seen >= WARMUP && self.kept.load(Ordering::Relaxed).saturating_mul(TIGHT) < seen
    }

    /// Records what one chunk put through the filter and what came out.
    fn saw(&self, rows: usize, kept: usize) {
        self.seen.fetch_add(rows, Ordering::Relaxed);
        self.kept.fetch_add(kept, Ordering::Relaxed);
    }
}

/// Moves tests from the projection's numbering onto the table's.
///
/// A test names a column of the projection and a zone names a column of the table, so the move
/// happens once here rather than at every chunk. A test on a column that is somehow not projected is
/// dropped, which costs a chunk that gets read.
/// Which of a scan's columns the operators over the filter at `filter` read, by place.
///
/// The walk goes up from the filter until it reaches an aggregate or a projection, noting every
/// column each operator on the way reads. Those two produce columns of their own, so nothing above
/// them sees the scan's columns. Filters, inner and outer joins, cross products, sorts and plain
/// limits only pass the columns they were given on, so what they read is all they read. Anything
/// else ends the walk with nothing, and so does the top of the plan: a set operation or a
/// `DISTINCT` compares whole rows, a dependent join lets the other side read this one, and the
/// root hands every column it has to the caller, none of which shows up as an expression.
fn read_above(plan: &Plan, filter: NodeRef, schema: &Schema) -> Option<Vec<bool>> {
    let mut parents: HashMap<NodeRef, Vec<NodeRef>> = HashMap::new();
    let mut seen = HashSet::new();
    let mut stack = vec![plan.root()];
    while let Some(at) = stack.pop() {
        if !seen.insert(at) {
            continue;
        }
        for child in plan.node(at).children().into_iter().flatten() {
            parents.entry(child).or_default().push(at);
            stack.push(child);
        }
    }
    let mut above = vec![false; schema.types().len()];
    let mut read = |expr: ExprRef| {
        crate::join::columns(plan, expr, &mut |binding| {
            if let Some(place) = schema.position_of(binding) {
                above[place] = true;
            }
        });
    };
    let mut here = filter;
    loop {
        let [parent] = parents.get(&here)?.as_slice() else { return None };
        match *plan.node(*parent) {
            Node::Aggregate { groups, aggregates, .. } => {
                for slice in [groups, aggregates] {
                    plan.expr_list(slice).iter().for_each(|&expr| read(expr));
                }
                break;
            }
            Node::Project { exprs, .. } => {
                plan.expr_list(exprs).iter().for_each(|&expr| read(expr));
                break;
            }
            Node::Filter { predicate, .. } => read(predicate),
            Node::Join { conditions, .. } => {
                plan.expr_list(conditions).iter().for_each(|&expr| read(expr));
            }
            Node::CrossProduct { .. } => {}
            Node::Sort { keys, .. } | Node::TopN { keys, .. } => {
                plan.sort_key_list(keys).iter().for_each(|key| read(key.expr));
            }
            Node::Limit { count, offset, .. }
                if count.read().is_none() && offset.read().is_none() => {}
            _ => return None,
        }
        here = *parent;
    }
    Some(above)
}

fn onto(columns: &[Option<usize>], tests: Vec<(usize, Op, Bound)>) -> Vec<Probe> {
    tests
        .into_iter()
        .filter_map(|(at, op, value)| {
            Some(Probe { column: columns.get(at)?.as_ref().copied()?, op, value })
        })
        .collect()
}

/// A filter a builder is handing to a scan to apply, rather than one it means to run above it.
///
/// This is what DuckDB calls the table filters of a scan, and the reason to want it is the middle of
/// the three answers a zone map can give. Pruning uses one end of it: the chunk holds nothing the
/// filter wants, so it is never read. The other end is the chunk that holds nothing the filter would
/// throw away, and only an operator that has both the zone and the predicate in front of it can act
/// on that. A filter above the scan has the predicate and not the zone, so it compares every row of
/// every chunk whatever the statistics already proved.
///
/// It is offered rather than pushed, because only a filter sitting directly on a stored table can
/// go. See [`rudb_opt::bounds::into_scan`]. Whether the predicate reads as tests is a separate
/// question and decides only whether the middle answer above is available: a predicate with an `OR`
/// in it still moves down, it just gets compared on every chunk the pruning left alive, which is
/// what it would have done above the scan anyway.
#[derive(Debug)]
pub(crate) struct Pushdown {
    /// The filter node this came from, which is still in the plan and is what the compaction gain
    /// function counts the passes above.
    pub(crate) node: NodeRef,
    pub(crate) predicate: ExprRef,
    /// The conjuncts, read as tests, in the numbering of what the scan produces.
    pub(crate) tests: Vec<(usize, Op, Bound)>,
    /// Whether those tests are the whole predicate, which is what makes the zone shortcut sound.
    pub(crate) whole: bool,
    /// The tests of each operand of a top level `AND`, see [`rudb_opt::bounds::Moved::conjuncts`].
    pub(crate) conjuncts: Vec<Option<Vec<(usize, Op, Bound)>>>,
    /// Whether the scan feeds an aggregate that reads a marked chunk, so that a chunk the filter
    /// keeps nearly all of is marked rather than cut. See [`Chunk::marked`].
    pub(crate) marks: bool,
}

/// Everything the builder hands a scan that narrows what it reads.
///
/// Four things rather than one because they arrive from four places and are used at four moments.
/// The pruning tests come from the predicate and are asked of a zone before a chunk is read. The
/// pushed filter is that same predicate again, kept whole, and is applied to the chunks that were
/// read. The sideways filter comes from a join that has already built its side and is asked while the
/// scan is running. The cutoff comes from the top N above and is asked again for every part, because
/// it tightens as the query runs. They are gathered into one argument because a scan takes all four
/// and a constructor of ten parameters is a constructor nobody reads.
#[derive(Debug, Default)]
pub(crate) struct Filters<'a> {
    /// Tests a zone map can rule a chunk out with, which is as many of the conjuncts as could be
    /// read. One that is missing costs a chunk that gets read and never costs a row.
    pub(crate) pruning: Vec<(usize, Op, Bound)>,
    /// The whole predicate, for the scan to apply, or `None` when a filter above it is applying it.
    pub(crate) pushed: Option<Pushdown>,
    /// What a join built and handed back down after the plan was already running.
    pub(crate) sideways: Option<Arc<Sideways<'a>>>,
    /// The same from joins further up, which reached this scan through the joins in between.
    pub(crate) also: Vec<Arc<Sideways<'a>>>,
    /// How good a row has to be to still interest the top N above, which moves while the scan runs.
    pub(crate) cutoff: Option<Arc<Cutoff>>,
}

/// How many readers can hold working space for a pushed filter without ever waiting on each other.
///
/// Sixty four, the way the radix partitions are sixty four. It is more threads than any machine the
/// engine runs a single pipeline on, and few enough slots that a scan can make them all up front.
/// A machine with more threads than this wraps and two of its readers share a slot, which is still
/// correct and is slow in exactly the way one shared free list was.
const SLOTS: usize = 64;

/// The next reader number to hand out, counted across the process rather than per query.
static READERS: AtomicUsize = AtomicUsize::new(0);

thread_local! {
    /// This thread's reader number, assigned the first time it asks for one and kept for its life.
    ///
    /// A thread identifier that turned into a small index would do instead and `ThreadId` is not
    /// one. Pool threads are made once and run every query, so this is assigned once per thread for
    /// the life of the process and read out of a `Cell` after that.
    static READER: Cell<usize> = const { Cell::new(usize::MAX) };
}

/// Drops the rows of a chunk that the exact rows from a join above do not hold, for a scan with no
/// filter to fold them into.
///
/// `reduced` is the set and the table position of the chunk's first row. The rows are the part's
/// own at this point, so the row numbers a scan makes up are still in step with them.
///
/// # Errors
///
/// Whatever narrowing the chunk to the rows that survived raises.
/// A part whose exact rows keep one row in this many or fewer is read at those rows alone. See
/// [`Source::read_reduced`].
const SPARSE_READ: usize = 8;

fn reduce(reduced: Option<(&Rids, u64)>, chunk: &mut Chunk) -> Result<()> {
    let Some((rows, first)) = reduced else { return Ok(()) };
    let len = chunk.len();
    let kept = Selection::from_indices(rows.offsets_in(first, len));
    if kept.len() == len {
        return Ok(());
    }
    let whole = std::mem::replace(chunk, Chunk::empty(&[]));
    *chunk = whole.select(&kept)?;
    Ok(())
}

/// Which slot the calling thread takes its working space out of.
fn reader() -> usize {
    READER.with(|reader| {
        let mut at = reader.get();
        if at == usize::MAX {
            at = READERS.fetch_add(1, Ordering::Relaxed);
            reader.set(at);
        }
        at % SLOTS
    })
}

/// The filter a scan applies itself, once it has been prepared against the scan's own columns.
#[derive(Debug)]
struct Pushed {
    predicate: Prepared,
    /// A necessary single-column LIKE that can run before the other projected column is read.
    late: Option<Late>,
    /// The whole predicate when it is a `LIKE` a compressed text page can answer without its
    /// strings being read. See [`Scan::read_stored`].
    stored: Option<Stored>,
    compaction: &'static dyn Compaction,
    passes: u32,
    /// Whether a chunk this keeps most of is marked rather than cut. See [`Pushdown::marks`].
    marks: bool,
    /// The same conjuncts as probes, or `None` when they are not the whole predicate or one of them
    /// could not be moved onto the table's numbering.
    ///
    /// `None` rather than a shorter list, because a list with one conjunct missing from it would
    /// prove the rows pass a filter that is not the one being applied, and [`Zone::certain`] over an
    /// empty list is vacuously true. Wrong by omission is the one failure mode worth a type here. It
    /// costs the comparison on every chunk, which is what the engine did before this existed.
    ///
    /// [`Zone::certain`]: rudb_storage::Zone::certain
    probes: Option<Vec<Probe>>,
    /// For each operand of the predicate's top level `AND`, the probes that are the whole of it, or
    /// `None` for one they are not. `None` altogether when no operand has any, or when the prepared
    /// predicate does not number its operands the way the plan does.
    ///
    /// The per operand half of `probes`. A part that cannot prove the whole predicate can still
    /// prove some of it, and the operands it proves are left out of the comparison for that part.
    settles: Option<Vec<Option<Vec<Probe>>>>,
    /// Working space kept per reader rather than in one free list they all queue for.
    ///
    /// A [`Source`] has no per instance state to keep this in, which a [`Stream`] does, so the scan
    /// keeps the slots itself and each reader goes to the one its thread was given. It was a single
    /// free list behind a single lock to begin with, and that is a lock taken twice per chunk by
    /// every thread in the pipeline, on every chunk the comparison runs on. Uncontended that is tens
    /// of nanoseconds against a comparison over a thousand rows and it does not matter. Contended it
    /// is the query: moving the ClickBench filters into the scan put nineteen of them onto this path
    /// and three came out slower on ten threads and faster on one, which is the shape of a lock and
    /// not the shape of extra work.
    ///
    /// The lock stays because a slot can still be shared, on a machine with more threads than
    /// [`SLOTS`]. The point is not that there is no lock, it is that taking it never waits.
    ///
    /// Building the working space per chunk instead would be simpler than either, and it is two
    /// allocations of a vector per step of the predicate on a call a scan makes a hundred thousand
    /// times.
    ///
    /// [`Stream`]: rudb_pipeline::Stream
    spare: Vec<Mutex<Option<Working>>>,
}

/// One reader's share of what a pushed filter mutates.
#[derive(Debug)]
struct Working {
    scratch: Scratch,
    late_scratch: Option<Scratch>,
    gauge: Gauge,
}

/// The first predicate and column of a two-column selective scan.
#[derive(Debug)]
struct Late {
    input: usize,
    predicate: Prepared,
    seen: AtomicUsize,
    kept: AtomicUsize,
}

/// Stop paying for the first-column probe when it has not removed enough rows.
const LATE_WARMUP: usize = 1 << 14;

impl Late {
    fn worth(&self) -> bool {
        let seen = self.seen.load(Ordering::Relaxed);
        seen < LATE_WARMUP || self.kept.load(Ordering::Relaxed).saturating_mul(4) < seen
    }

    fn saw(&self, rows: usize, kept: usize) {
        self.seen.fetch_add(rows, Ordering::Relaxed);
        self.kept.fetch_add(kept, Ordering::Relaxed);
    }
}

/// A filter that is one `LIKE '%a%b%'` on a string column, answered by the pages rather than by the
/// strings.
#[derive(Debug)]
struct Stored {
    /// The column, in the scan's numbering.
    input: usize,
    sequence: Sequence,
    negated: bool,
}

/// The column, the pieces and whether it is a `NOT LIKE`, when `predicate` is a `LIKE` whose pattern
/// is some pieces between `%` and nothing else.
///
/// Those are the patterns a string holds exactly when it holds the pieces in order, which is what a
/// page can answer on its codes. A `_` is one character and not one byte, and a pattern anchored at
/// either end asks where the pieces are and not only whether, so neither comes here.
fn stored_like(plan: &Plan, schema: &Schema, predicate: ExprRef) -> Option<Stored> {
    let Expr::Function { name, args } = *plan.expr(predicate) else { return None };
    let negated = match plan.string(name) {
        "~~" => false,
        "!~~" => true,
        _ => return None,
    };
    let [column, constant] = plan.expr_list(args) else { return None };
    let Expr::Column(binding) = *plan.expr(*column) else { return None };
    let Expr::Constant(value) = *plan.expr(*constant) else { return None };
    let Value::Varchar(spelling) = plan.value(value) else { return None };
    let inner = spelling.strip_prefix('%')?.strip_suffix('%')?;
    if inner.contains('_') {
        return None;
    }
    let input = schema.position_of(binding)?;
    if schema.types().get(input) != Some(&LogicalType::Varchar) {
        return None;
    }
    let pieces: Vec<&[u8]> = inner.split('%').map(str::as_bytes).collect();
    Some(Stored { input, sequence: Sequence::new(&pieces)?, negated })
}

/// A LIKE conjunct followed by a simple comparison on another column.
///
/// Only a necessary conjunct of an AND is allowed here. The full predicate still runs after the
/// sparse read, so this choice cannot accept a row the ordinary scan would reject.
fn late_like(plan: &Plan, schema: &Schema, predicate: ExprRef) -> Option<(usize, ExprRef)> {
    if schema.width() != 2 {
        return None;
    }
    let Expr::Conjunction { op: ConjunctionOp::And, children } = *plan.expr(predicate) else {
        return None;
    };
    let [left, right] = plan.expr_list(children) else { return None };
    for (candidate, other) in [(*left, *right), (*right, *left)] {
        let Expr::Function { name, args } = *plan.expr(candidate) else { continue };
        if plan.string(name) != "~~" {
            continue;
        }
        let [column, constant] = plan.expr_list(args) else { continue };
        let Expr::Column(binding) = *plan.expr(*column) else { continue };
        if !matches!(*plan.expr(*constant), Expr::Constant(_)) {
            continue;
        }
        let Some(input) = schema.position_of(binding) else { continue };
        let Expr::Compare { left, right, .. } = *plan.expr(other) else { continue };
        let compared = match (plan.expr(left), plan.expr(right)) {
            (Expr::Column(binding), Expr::Constant(_))
            | (Expr::Constant(_), Expr::Column(binding)) => *binding,
            _ => continue,
        };
        if schema.position_of(compared) == Some(1 - input) {
            return Some((input, candidate));
        }
    }
    None
}

impl Pushed {
    /// Prepares the offered predicate against what the scan produces.
    ///
    /// The same three things [`crate::stream::Filter`] builds, because this is that operator moved
    /// rather than a second one written: the predicate prepared over the input's types, the
    /// compaction the session asked for, and how many times the rows it keeps get read again.
    ///
    /// # Errors
    ///
    /// If the predicate does not resolve against the scan's schema, or if the session has pinned the
    /// compaction seam to something that cannot run over these columns. Both are what the filter
    /// above the scan would have raised, at the same moment, for the same reasons.
    fn new(
        plan: &Plan,
        schema: &Schema,
        columns: &[Option<usize>],
        pushdown: Pushdown,
        seams: &Settings,
        session: &Session,
    ) -> Result<Self> {
        let types = schema.types();
        let context = Context::new(SeamId::ChunkCompaction, seams).with_types(&types);
        let compaction = compaction().choose(&context)?.strategy();
        let whole = pushdown.whole;
        let wanted = pushdown.tests.len();
        let probes = onto(columns, pushdown.tests);
        let settles: Vec<Option<Vec<Probe>>> = pushdown
            .conjuncts
            .into_iter()
            .map(|tests| {
                let tests = tests?;
                let wanted = tests.len();
                Some(onto(columns, tests)).filter(|probes| probes.len() == wanted)
            })
            .collect();
        let predicate = Prepared::one(plan, pushdown.predicate, schema)?.in_session(session);
        let settles = (predicate.conjuncts() == Some(settles.len())
            && settles.iter().any(Option::is_some))
        .then_some(settles);
        let late = if columns.len() == 2 && columns.iter().all(Option::is_some) {
            late_like(plan, schema, pushdown.predicate)
                .map(|(input, expr)| {
                    Ok::<_, Error>(Late {
                        input,
                        predicate: Prepared::one(plan, expr, schema)?.in_session(session),
                        seen: AtomicUsize::new(0),
                        kept: AtomicUsize::new(0),
                    })
                })
                .transpose()?
        } else {
            None
        };
        let stored = stored_like(plan, schema, pushdown.predicate);
        Ok(Self {
            predicate,
            late,
            stored,
            compaction,
            passes: later_passes(plan, pushdown.node),
            marks: pushdown.marks,
            probes: (whole && probes.len() == wanted).then_some(probes),
            settles,
            spare: (0..SLOTS).map(|_| Mutex::new(None)).collect(),
        })
    }

    /// One reader's working space, out of its own slot or newly made.
    fn take(&self, slot: usize) -> Working {
        let waiting = self.spare[slot].lock().ok().and_then(|mut spare| spare.take());
        waiting.unwrap_or_else(|| Working {
            scratch: self.predicate.scratch(),
            late_scratch: self.late.as_ref().map(|late| late.predicate.scratch()),
            gauge: Gauge::new(self.passes),
        })
    }

    /// The working space back into the slot it came out of.
    ///
    /// A lock this cannot take is a lock somebody panicked holding, and the answer to that is to drop
    /// the working space rather than to fail the scan: the next reader builds one and the query
    /// finishes. The gain function loses the counts this thread had gathered, which costs a
    /// compaction decision made on less evidence and costs no rows.
    fn give(&self, slot: usize, working: Working) {
        if let Ok(mut spare) = self.spare[slot].lock() {
            *spare = Some(working);
        }
    }
}

/// How a native scan cuts its parts into morsels.
///
/// A run is a range of part numbers and a morsel covers one run. The ranges are built out of the
/// parts the zone maps leave alive, so a run holds work rather than holding whatever happened to sit
/// next to it, and the handout beside them is what hands one run to each worker that asks.
///
/// `order` is the order the runs go out in, as positions in `runs`, and empty for table order. A
/// morsel is still numbered by its run's place in `runs`, so the rows a top N ranks by where they
/// arrived arrive at the same places whatever order the runs are read in.
#[derive(Debug)]
struct Spread {
    runs: Vec<Range<usize>>,
    order: Vec<usize>,
    handout: Handout,
}

impl<'a> Scan<'a> {
    /// A scan of `table` producing the plan's projected columns.
    ///
    /// # Errors
    ///
    /// If the plan asks for a column the table does not have, which means the catalog changed under
    /// a plan that was bound against it.
    pub(crate) fn new(
        plan: &Plan,
        table: &'a Table,
        index: u32,
        projection: Slice,
        filters: Filters<'a>,
        seams: &Settings,
        session: &Session,
    ) -> Result<Self> {
        let Filters { pruning, pushed: pushdown, sideways, also, cutoff } = filters;
        let fields = plan.field_list(projection).to_vec();
        let mut columns = Vec::with_capacity(fields.len());
        for field in &fields {
            if field.name == FILE_ROW_NUMBER {
                columns.push(None);
                continue;
            }
            let position = table.column_index(&field.name).ok_or_else(|| {
                Error::catalog(format!(
                    "Table \"{}\" does not have a column named \"{}\"",
                    table.name().table,
                    field.name
                ))
            })?;
            columns.push(Some(position));
        }
        let probes = onto(&columns, pruning);
        let schema = Schema::numbered(fields, index);
        let chunks = Handout::new(table.rows().chunk_count());
        let mut next = 0_i64;
        let mut offsets = Vec::with_capacity(table.rows().chunk_count());
        for at in 0..table.rows().chunk_count() {
            offsets.push(next);
            next =
                next.saturating_add(i64::try_from(table.rows().chunk_len(at)?).unwrap_or(i64::MAX));
        }
        let stripes = table.rows().stripe_parts();
        let mut read = vec![false; columns.len()];
        if let Some(pushdown) = &pushdown {
            crate::join::columns(plan, pushdown.predicate, &mut |binding| {
                if let Some(at) = schema.position_of(binding) {
                    read[at] = true;
                }
            });
        }
        let types = schema.types();
        let deferrable: Vec<usize> =
            (0..columns.len()).filter(|&at| columns[at].is_some() && !read[at]).collect();
        let deferred =
            deferrable.iter().copied().filter(|&at| types[at] == LogicalType::Varchar).collect();
        let unread = pushdown
            .as_ref()
            .and_then(|pushdown| read_above(plan, pushdown.node, &schema))
            .map(|above| {
                (0..columns.len())
                    .filter(|&at| columns[at].is_some() && read[at] && !above[at])
                    .collect()
            })
            .unwrap_or_default();
        let pushed = pushdown
            .map(|pushdown| Pushed::new(plan, &schema, &columns, pushdown, seams, session))
            .transpose()?;
        Ok(Self {
            table,
            columns,
            offsets,
            probes,
            sideways,
            also: also.into_iter().map(|sideways| (sideways, Paying::default())).collect(),
            cutoff,
            index,
            testing: OnceLock::new(),
            schema,
            chunks,
            stripes,
            pushed,
            waved: AtomicUsize::new(0),
            spread: OnceLock::new(),
            skipped: AtomicUsize::new(0),
            counters: None,
            deferred,
            deferrable,
            deferring: Paying::default(),
            unread,
            paying: Paying::default(),
            passed: Paying::default(),
        })
    }

    /// Applies the filter the builder handed over, to the chunk numbered `at`.
    ///
    /// The three way decision, and the one this whole arrangement exists for. A chunk whose zone
    /// proves every row passes is handed on as it was read, with no comparison, no selection and no
    /// narrowing. A chunk the zone cannot decide is compared, which is what a filter above the scan
    /// would have done to every chunk of the table. The third answer, that the chunk holds nothing at
    /// all, was settled before this: [`Source::read`] walks past it and never reads it.
    ///
    /// On a predicate that keeps most of a clustered column this is most of the chunks. The bounds
    /// note in `rudb-common` has the case: `l_shipdate <= '1998-09-02'` keeps ninety eight percent of
    /// TPC-H lineitem, so nearly every chunk of it is one where the comparison was going to keep
    /// every row and the only thing it produced was the knowledge that it had.
    ///
    /// The exact rows a join above handed down go into the same narrowing, see [`Source::reduce`].
    ///
    /// # Errors
    ///
    /// Whatever evaluating the predicate or narrowing the chunk reports.
    fn apply(&self, at: usize, chunk: &mut Chunk, mark: bool) -> Result<()> {
        let reduced = self.reduced(at).filter(|(rows, _)| !rows.is_full());
        self.apply_reduced(at, reduced, chunk, mark)
    }

    /// [`Self::apply`] with the exact rows given rather than looked up, and `None` for a chunk
    /// they were already applied to when it was read. See [`Self::read_reduced`].
    fn apply_reduced(
        &self,
        at: usize,
        reduced: Option<(&Rids, u64)>,
        chunk: &mut Chunk,
        mark: bool,
    ) -> Result<()> {
        let Some(pushed) = self.pushed.as_ref() else { return reduce(reduced, chunk) };
        let whole =
            pushed.probes.as_deref().is_some_and(|probes| self.table.rows().certain(at, probes));
        if whole {
            self.waved.fetch_add(1, Ordering::Relaxed);
            return reduce(reduced, chunk);
        }
        // Taken and given back rather than built here. An empty free list means every other reader
        // is holding one, which is a reader that has not had a turn yet rather than an error.
        let slot = reader();
        let mut working = pushed.take(slot);
        // Charged to its own stage, because otherwise a filter the scan took into itself is time
        // the document calls reading.
        let filtering = stage::Timing::start(Stage::Filter);
        let kept = match &pushed.settles {
            Some(settles) => {
                let rows = self.table.rows();
                let settled: Vec<bool> = settles
                    .iter()
                    .map(|probes| probes.as_deref().is_some_and(|probes| rows.certain(at, probes)))
                    .collect();
                pushed.predicate.evaluate_settled(chunk, &mut working.scratch, &settled)
            }
            None => pushed.predicate.evaluate_filter(chunk, &mut working.scratch),
        };
        filtering.stop(0);
        let mut kept = kept?;
        self.passed.saw(chunk.len(), kept.len());
        // After the filter and on its answer rather than on the chunk, so that the filter's kernels
        // read the columns flat as they came off the disk and the chunk is narrowed once. Narrowing
        // it for the reduction first left the filter a selected chunk, and the comparison on a
        // selected column was the slow path on Q3 at SF1, seven hundred times a query.
        if let Some((rows, first)) = reduced {
            let held: Vec<u32> = kept
                .iter()
                .filter(|&row| rows.contains(first + row as u64))
                .filter_map(|row| u32::try_from(row).ok())
                .collect();
            kept = Selection::from_indices(held);
        }
        // Marked rather than cut only where nothing after this reads the chunk before the aggregate
        // does. A join's filter below narrows the chunk again, and a reduction has already moved
        // the rows, so either one gets the chunk cut as before.
        let alone = reduced.is_none() && self.sideways.is_none() && self.also.is_empty();
        if kept.len() != chunk.len() {
            if mark && alone && pushed.marks && marking_pays(kept.len(), chunk.len()) {
                let whole = std::mem::replace(chunk, Chunk::empty(&[]));
                *chunk = whole.marked(kept);
            } else {
                self.narrow_read_columns(pushed.compaction, chunk, &kept, &mut working.gauge)?;
            }
        }
        pushed.give(slot, working);
        Ok(())
    }

    /// Narrows a chunk to the rows a filter kept, leaving out the columns nothing reads afterwards.
    ///
    /// A column only the pushed filter read is taken out before narrowing and comes back as a null
    /// constant of the kept length, unless a runtime filter from a join reads it next. Narrowing a
    /// packed column unpacks it at every kept row, and a string column wrapped in the selection is
    /// copied out string by string by the first join that gathers it, which is the work this saves.
    fn narrow_read_columns(
        &self,
        how: &dyn Compaction,
        chunk: &mut Chunk,
        kept: &Selection,
        gauge: &mut Gauge,
    ) -> Result<()> {
        let mut unread = self.unread.clone();
        let joins = self.sideways.iter().chain(self.also.iter().map(|(sideways, _)| sideways));
        for sideways in joins {
            let domain = sideways
                .domain(self.index)
                .or_else(|| sideways.spare(self.index))
                .map(|(key, _)| key);
            let sifting = sideways.sifting(self.index).map(|(key, _)| key);
            unread.retain(|&at| domain != Some(at) && sifting != Some(at));
        }
        if unread.is_empty() {
            return narrow(how, chunk, kept, gauge);
        }
        let rows = chunk.len();
        let types = chunk.types();
        let mut columns: Vec<Option<Vector>> = std::mem::replace(chunk, Chunk::empty(&[]))
            .into_columns()
            .into_iter()
            .map(Some)
            .collect();
        let live: Vec<Vector> = columns
            .iter_mut()
            .enumerate()
            .filter(|(at, _)| !unread.contains(at))
            .filter_map(|(_, column)| column.take())
            .collect();
        let mut part = Chunk::with_rows(live, rows)?;
        narrow(how, &mut part, kept, gauge)?;
        let len = part.len();
        let mut live = part.into_columns().into_iter();
        let mut out = Vec::with_capacity(columns.len());
        for (at, ty) in types.into_iter().enumerate() {
            if unread.contains(&at) {
                out.push(Vector::constant(ty, Value::Null, len));
            } else {
                out.push(
                    live.next().ok_or_else(|| Error::internal("a narrowed column went missing"))?,
                );
            }
        }
        *chunk = Chunk::with_rows(out, len)?;
        Ok(())
    }

    /// Reads only the rows of part `at` the exact rows from a join above hold, when they are few.
    ///
    /// The exact rows name the part's survivors before a byte of it is read, so a part in which few
    /// survive is decoded at those rows and no others. Reading the part whole and cutting it
    /// afterwards decodes every row of every column to keep a handful. A reduction through a
    /// relationship whose parent side is small is the case: on TPC-H q17 the 204 parts a filter
    /// keeps have 6,088 lines among the six million of `lineitem`, which is two or three a part.
    ///
    /// Past one survivor in [`SPARSE_READ`] rows the part is read whole, because decoding at a
    /// position costs more than decoding in a run and a part that keeps many rows is closer to the
    /// run.
    fn read_reduced(&self, at: usize, out: &mut Chunk) -> Result<bool> {
        let Some((rows, first)) = self.reduced(at).filter(|(rows, _)| !rows.is_full()) else {
            return Ok(false);
        };
        let len = self.table.rows().chunk_len(at)?;
        let positions = rows.offsets_in(first, len);
        if positions.len().saturating_mul(SPARSE_READ) > len {
            return Ok(false);
        }
        let projected: Vec<usize> = self.columns.iter().flatten().copied().collect();
        let read = self.table.rows().read_rows(at, &projected, &positions)?;
        let kept = positions.len();
        let mut held = Vec::with_capacity(self.columns.len());
        let mut real = 0;
        for column in &self.columns {
            if column.is_some() {
                held.push(read.column(real)?.clone());
                real += 1;
            } else {
                let numbers = positions
                    .iter()
                    .map(|&row| Value::BigInt(self.offsets[at] + i64::from(row)))
                    .collect::<Vec<_>>();
                held.push(Vector::from_values(LogicalType::BigInt, &numbers)?);
            }
        }
        *out = Chunk::with_rows(held, kept)?;
        self.apply_reduced(at, None, out, false)?;
        self.sift(out)?;
        Ok(true)
    }

    /// Runs the filter this scan took off the operator above it and the joins' runtime filters over
    /// one chunk it has just read, the exact bitmaps first where it can.
    ///
    /// A bitmap costs a subtraction and a bit test a row and is exact, while the pushed filter
    /// evaluates a predicate and then narrows every column, which unpacks the packed ones. In q03 at
    /// SF1 the date filter keeps half of lineitem and the join to orders keeps about one row in a
    /// hundred of those, so narrowing on the date first unpacked fifty times more rows than the join
    /// ever read. A Bloom filter costs a hash a row, so it goes ahead of the pushed filter only once
    /// that filter has been measured keeping more than half the rows. In q04 the date filter keeps
    /// about two thirds of lineitem and hashing first saved a third of the scan, while in q10 it keeps
    /// a quarter and hashing the whole chunk cost more than the narrowing it saved.
    ///
    /// The graph reduction inside [`Self::apply`] names rows by their place in the part, so a part it
    /// narrows keeps the old order, which lets the reduction see the rows where they were read.
    ///
    /// `mark` is whether the caller hands the chunk straight on, which is what lets the pushed
    /// filter mark the rows it keeps rather than cut them. A caller that reads the rows afterwards
    /// passes false.
    fn narrow_read(&self, at: usize, out: &mut Chunk, mark: bool) -> Result<()> {
        let placed = self.reduced(at).is_some_and(|(rows, _)| !rows.is_full());
        if placed {
            self.apply(at, out, false)?;
            return self.sift(out);
        }
        self.sift_exact(out)?;
        if out.is_empty() {
            return Ok(());
        }
        if self.passed.loose() {
            self.sift_hashed(out)?;
            if out.is_empty() {
                return Ok(());
            }
            return self.apply(at, out, mark);
        }
        self.apply(at, out, mark)?;
        self.sift_hashed(out)
    }

    /// Reads a part whose filter is a `LIKE` only that filter reads, asking the column's pages which
    /// rows pass rather than reading the strings.
    ///
    /// A compressed text page answers on its codes, see [`rudb_encoding::sequence`], and the column
    /// comes back as nulls the way a column only the filter reads always does after narrowing. In
    /// TPC-H q13 the orders scan decompressed every comment, laid it out and checked it was text to
    /// search it for two words and then threw it away. A part the pages cannot answer, a join that
    /// reads the column, or a reduction that names rows by where they were read, goes the usual way.
    fn read_stored(&self, at: usize, out: &mut Chunk) -> Result<bool> {
        let Some(pushed) = &self.pushed else { return Ok(false) };
        let Some(stored) = &pushed.stored else { return Ok(false) };
        if !self.unread.contains(&stored.input) || self.reduced(at).is_some() {
            return Ok(false);
        }
        let joins = self.sideways.iter().chain(self.also.iter().map(|(sideways, _)| sideways));
        for sideways in joins {
            let domain = sideways
                .domain(self.index)
                .or_else(|| sideways.spare(self.index))
                .map(|(key, _)| key);
            let sifting = sideways.sifting(self.index).map(|(key, _)| key);
            if domain == Some(stored.input) || sifting == Some(stored.input) {
                return Ok(false);
            }
        }
        let Some(column) = self.columns[stored.input] else { return Ok(false) };
        let rows = self.table.rows();
        // A LIKE answered on the pages is string work, and it is the whole of the filter here.
        let searching = stage::Timing::start(Stage::Strings);
        let holding = rows.rows_holding(at, column, &stored.sequence, stored.negated);
        searching.stop(0);
        let Some(kept) = holding? else {
            return Ok(false);
        };
        let len = rows.chunk_len(at)?;
        let others: Vec<usize> = (0..self.columns.len())
            .filter(|&place| place != stored.input)
            .filter_map(|place| self.columns[place])
            .collect();
        let read = if others.is_empty() { None } else { Some(rows.read(at, &others)?) };
        let types = self.schema.types();
        let mut held = Vec::with_capacity(self.columns.len());
        let mut real = 0;
        for (place, column) in self.columns.iter().enumerate() {
            if place == stored.input {
                held.push(Vector::constant(types[place].clone(), Value::Null, len));
            } else if column.is_some() {
                let read =
                    read.as_ref().ok_or_else(|| Error::internal("a read column is missing"))?;
                held.push(read.column(real)?.clone());
                real += 1;
            } else {
                held.push(Vector::sequence(self.offsets[at], 1, len));
            }
        }
        *out = Chunk::with_rows(held, len)?;
        self.passed.saw(len, kept.len());
        if kept.len() != len {
            let kept = Selection::from_indices(kept);
            let alone = self.sideways.is_none() && self.also.is_empty();
            if alone && pushed.marks && marking_pays(kept.len(), len) {
                let whole = std::mem::replace(out, Chunk::empty(&[]));
                *out = whole.marked(kept);
            } else {
                let slot = reader();
                let mut working = pushed.take(slot);
                narrow(pushed.compaction, out, &kept, &mut working.gauge)?;
                pushed.give(slot, working);
            }
        }
        self.sift_exact(out)?;
        if !out.is_empty() {
            self.sift_hashed(out)?;
        }
        Ok(true)
    }

    /// Reads the first LIKE column before the other projected column when it can reject most rows.
    ///
    /// The full filter runs on the survivors after the sparse read. A dense result falls back to
    /// the ordinary two-column read, and a scan with a sideways filter keeps its original row
    /// positions rather than entering this path.
    fn read_late(&self, at: usize, out: &mut Chunk) -> Result<bool> {
        let Some(pushed) = &self.pushed else { return Ok(false) };
        let Some(late) = &pushed.late else { return Ok(false) };
        if self.sideways.is_some() || !self.also.is_empty() || !late.worth() {
            return Ok(false);
        }
        let Some(primary) = self.columns[late.input] else { return Ok(false) };
        let Some(secondary) = self.columns[1 - late.input] else { return Ok(false) };
        let read = self.table.rows().read(at, &[primary])?;
        let len = read.len();
        let mut columns = self
            .schema
            .types()
            .into_iter()
            .map(|ty| Vector::constant(ty, Value::Null, len))
            .collect::<Vec<_>>();
        columns[late.input] = read.column(0)?.clone();
        let first = Chunk::with_rows(columns, len)?;
        let slot = reader();
        let mut working = pushed.take(slot);
        let filtering = stage::Timing::start(Stage::Filter);
        let selected = late.predicate.evaluate_filter(
            &first,
            working
                .late_scratch
                .as_mut()
                .ok_or_else(|| Error::internal("a late filter has no scratch"))?,
        );
        filtering.stop(0);
        let selected = selected?;
        pushed.give(slot, working);
        late.saw(len, selected.len());
        if selected.len().saturating_mul(4) > len {
            return Ok(false);
        }
        if selected.is_empty() {
            *out = Chunk::empty(&self.schema.types());
            return Ok(true);
        }
        let fetched = self.table.rows().read_selected(at, &[secondary], selected.indices())?;
        let first = read.column(0)?.gather(selected.indices())?;
        let second = fetched.column(0)?.clone();
        let columns = if late.input == 0 { vec![first, second] } else { vec![second, first] };
        *out = Chunk::with_rows(columns, selected.len())?;
        self.apply(at, out, false)?;
        Ok(true)
    }

    /// Reads the string columns nothing filters on only for the rows the filters keep.
    ///
    /// The other columns are read first, with a null in place of each string column the pushed
    /// filter and the joins' filters do not read and a row number on the end, and the filters run
    /// over that as they would over the whole part. The row numbers that come through are the rows
    /// kept, and the string columns are then read at those rows alone, which for a compressed page
    /// decompresses nothing else. In TPC-H q10 the join to orders keeps a quarter of the customer
    /// scan, and decompressing the name, address, phone and comment of the other three quarters
    /// was a fifth of the query.
    ///
    /// Only for a scan that has a filter of some kind, since without one every row is kept, and only
    /// while the filters are measured dropping a quarter of the rows or more. In q01 the date filter
    /// keeps nearly all of lineitem and reading the two flags a second time cost 7 percent. A graph
    /// reduction names rows by where they were read and a late LIKE has its own path, so those do
    /// not come here.
    ///
    /// Once a join's bitmap or filter is measured keeping under one row in [`TIGHT`], every column
    /// neither it nor the pushed filter reads is put off the same way, integers included, since an
    /// integer page unpacks at the rows asked for alone. In TPC-H q17 the bitmap over the parts of
    /// one brand and container keeps about one `lineitem` row in a thousand, and decoding the
    /// quantity and the price of the other nine hundred and ninety nine was a third of the query.
    /// See `spec/perf/47-columns-at-the-kept-rows.md`.
    fn read_deferring(&self, at: usize, out: &mut Chunk) -> Result<bool> {
        if !self.deferring.worth() || self.reduced(at).is_some() {
            return Ok(false);
        }
        let own = self.sideways.iter().map(|sideways| (sideways, &self.paying));
        let joins = own.chain(self.also.iter().map(|(sideways, paying)| (sideways, paying)));
        let mut keys = Vec::new();
        let mut tight = false;
        for (sideways, paying) in joins {
            let before = keys.len();
            keys.extend(
                sideways
                    .domain(self.index)
                    .or_else(|| sideways.spare(self.index))
                    .map(|(key, _)| key),
            );
            keys.extend(sideways.sifting(self.index).map(|(key, _)| key));
            tight |= keys.len() > before && paying.tight();
        }
        let wide = if tight { &self.deferrable } else { &self.deferred };
        if wide.is_empty() || (self.pushed.is_none() && keys.is_empty()) {
            return Ok(false);
        }
        let deferred: Vec<usize> = wide.iter().copied().filter(|at| !keys.contains(at)).collect();
        let first: Vec<usize> = (0..self.columns.len())
            .filter(|at| !deferred.contains(at))
            .filter_map(|at| self.columns[at])
            .collect();
        if deferred.is_empty() || first.is_empty() {
            return Ok(false);
        }
        let read = self.table.rows().read(at, &first)?;
        let len = read.len();
        let types = self.schema.types();
        let mut held = Vec::with_capacity(self.columns.len() + 1);
        let mut real = 0;
        for (place, column) in self.columns.iter().enumerate() {
            if deferred.contains(&place) {
                held.push(Vector::constant(types[place].clone(), Value::Null, len));
            } else if column.is_some() {
                held.push(read.column(real)?.clone());
                real += 1;
            } else {
                held.push(Vector::sequence(self.offsets[at], 1, len));
            }
        }
        held.push(Vector::sequence(0, 1, len));
        *out = Chunk::with_rows(held, len)?;
        self.narrow_read(at, out, false)?;
        let kept = out.len();
        self.deferring.saw(len, kept);
        let mut columns = std::mem::replace(out, Chunk::empty(&[])).into_columns();
        let numbers =
            columns.pop().ok_or_else(|| Error::internal("the row numbers went missing"))?;
        if kept > 0 {
            // A part every row of which was kept is read whole, which is the plain read with nothing
            // to gather afterwards.
            let wanted: Vec<usize> = deferred.iter().filter_map(|&at| self.columns[at]).collect();
            let fetched = if kept == len {
                self.table.rows().read(at, &wanted)?
            } else {
                let mut block = Vec::new();
                if !numbers.signed_block(&mut block) || block.len() < kept {
                    block = (0..kept)
                        .map(|row| numbers.signed_at(row).and_then(|at| i64::try_from(at).ok()))
                        .collect::<Option<Vec<i64>>>()
                        .ok_or_else(|| Error::internal("a row number is not a row"))?;
                }
                let positions = block[..kept]
                    .iter()
                    .map(|&number| u32::try_from(number))
                    .collect::<std::result::Result<Vec<u32>, _>>()
                    .map_err(|_| Error::internal("a row number is not a row"))?;
                self.table.rows().read_rows(at, &wanted, &positions)?
            };
            for (from, &place) in deferred.iter().enumerate() {
                columns[place] = fetched.column(from)?.clone();
            }
        }
        *out = Chunk::with_rows(columns, kept)?;
        Ok(true)
    }

    /// Drops the rows of one chunk that a join above this scan cannot hold a match for.
    ///
    /// The range above rules out whole chunks and this rules out rows inside the ones that are left,
    /// which is the tier that still says something about a column whose values are spread over their
    /// whole domain. One hash of one column and one cache line touched per row, against a filter
    /// small enough to stay in the cache while a fact table goes past it.
    ///
    /// The column is read out of the chunk this scan has just produced rather than out of the table,
    /// so the position is the projection's and the rows are whatever the chunk holds, including the
    /// row numbers a scan makes up. Nothing to do at all for a scan with no join above it, which is
    /// two loads and a branch per chunk, and nothing to do either once [`Paying`] has decided the
    /// filter is not turning enough rows away to be worth hashing for.
    ///
    /// # Errors
    ///
    /// Whatever narrowing the chunk to the rows that survived raises.
    ///
    /// With more than one join above, every bitmap goes before any filter, because a bitmap costs a
    /// bit a row and a filter costs a hash and a cache line, and a row a bitmap has dropped is a row
    /// no filter has to hash.
    fn sift(&self, chunk: &mut Chunk) -> Result<()> {
        self.sift_exact(chunk)?;
        self.sift_hashed(chunk)
    }

    /// The bitmaps half of [`Self::sift`], which is cheap enough to go in front of the pushed
    /// filter. See [`Self::narrow_read`].
    fn sift_exact(&self, chunk: &mut Chunk) -> Result<()> {
        let handoffs = || {
            let own = self.sideways.iter().map(|sideways| (sideways, &self.paying));
            own.chain(self.also.iter().map(|(sideways, paying)| (sideways, paying)))
        };
        // The bitmap a join over a relationship with no link in the file leaves, or one over integer
        // keys that sit close together. It is exact and costs a subtraction and a bit a row, which
        // is cheap but not free, so it answers to the same count as the filter and a bitmap that
        // keeps nearly every row stops being asked. It takes the place of the filter rather than
        // going in front of it, see `Found::domain`.
        for (sideways, paying) in handoffs() {
            // A join above that is not this scan's own placed its answer as exact rows this scan
            // does not read, so it is tested by the bitmap kept beside them.
            let own = self.sideways.as_ref().is_some_and(|own| Arc::ptr_eq(own, sideways));
            let domain = sideways
                .domain(self.index)
                .or_else(|| if own { None } else { sideways.spare(self.index) });
            let Some((at, domain)) = domain else { continue };
            if !paying.worth() {
                continue;
            }
            let Ok(column) = chunk.column(at) else { continue };
            let rows = chunk.len();
            let kept = domain.keep(column, rows, &mut Vec::new());
            paying.saw(rows, kept.len());
            if kept.len() < rows {
                let whole = std::mem::replace(chunk, Chunk::empty(&[]));
                *chunk = whole.select(&Selection::from_indices(kept))?;
            }
        }
        Ok(())
    }

    /// The filters half of [`Self::sift`], a hash a row, run after the bitmaps so that a row a
    /// bitmap has dropped is a row no filter has to hash.
    fn sift_hashed(&self, chunk: &mut Chunk) -> Result<()> {
        let handoffs = || {
            let own = self.sideways.iter().map(|sideways| (sideways, &self.paying));
            own.chain(self.also.iter().map(|(sideways, paying)| (sideways, paying)))
        };
        for (sideways, paying) in handoffs() {
            if chunk.is_empty() {
                return Ok(());
            }
            if sideways.domain(self.index).is_some() {
                continue;
            }
            let Some((at, filter)) = sideways.sifting(self.index) else { continue };
            if !paying.worth() {
                continue;
            }
            let Ok(column) = chunk.column(at) else { continue };
            let rows = chunk.len();
            let mut hashes = Vec::new();
            hash(std::slice::from_ref(column), rows, &mut hashes, Across::TwoInputs);
            // The whole chunk asked at once rather than a row at a time inside the selection,
            // because the filter is larger than the cache and a row of it is a trip to memory the
            // core can only overlap with the next row's if nothing in between branches on the
            // answer.
            let mut held = Vec::new();
            filter.holds_run(&hashes, &mut held);
            let kept = Selection::from_predicate(rows, |row| held[row]);
            paying.saw(rows, kept.len());
            if kept.len() < rows {
                let whole = std::mem::replace(chunk, Chunk::empty(&[]));
                *chunk = whole.select(&kept)?;
            }
        }
        Ok(())
    }

    /// Every test this scan has, which is the plan's and whatever a join above it worked out.
    ///
    /// Settled on the first call and the same afterwards, because a filter that answered one thing
    /// while the morsels were cut and another while they were read would be a scan whose work was
    /// divided by one set of rows and done over a different one.
    fn testing(&self) -> &[Probe] {
        self.testing.get_or_init(|| {
            let mut probes = self.probes.clone();
            if let Some(sideways) = self.sideways.as_ref() {
                // Here because this is the one moment every instance of the scan passes through
                // after the build side has finished and before a row is read.
                if let (Some(counters), Some(reduced)) =
                    (&self.counters, sideways.reduction(self.index))
                {
                    counters.reducing(reduced);
                }
                probes.extend(onto(&self.columns, sideways.tests(self.index)));
            }
            for (sideways, _) in &self.also {
                probes.extend(onto(&self.columns, sideways.tests(self.index)));
            }
            probes
        })
    }

    /// How good a row has to be for the top N above to still want it, as a test on one column.
    ///
    /// Empty for a scan with no top N above it, and empty until some instance of that top N has held
    /// a full set of candidates. It is one test at most, because only the first sort key says
    /// anything about a whole part. See [`crate::cutoff`].
    fn cutoff(&self) -> Vec<Probe> {
        let Some(cutoff) = self.cutoff.as_ref() else { return Vec::new() };
        let Some(test) = cutoff.probe(self.index) else { return Vec::new() };
        onto(&self.columns, vec![test])
    }

    /// The table column a top N above this scan orders by first, and whether it runs descending.
    fn ordered(&self) -> Option<(usize, bool)> {
        let (column, op) = self.cutoff.as_ref()?.ordered(self.index)?;
        let column = (*self.columns.get(column)?)?;
        Some((column, op == Op::GreaterOrEqual))
    }

    /// Whether part `at` holds nothing anything above this scan could want.
    ///
    /// The two sets of tests are asked separately rather than joined into one, so that a scan with no
    /// top N above it does what it always did and a scan with one pays an allocation per part it
    /// hands back and nothing per part it walks past.
    fn ruled(&self, at: usize, probes: &[Probe], cutoff: &[Probe]) -> bool {
        let rows = self.table.rows();
        (!probes.is_empty() && rows.skips(at, probes))
            || (!cutoff.is_empty() && rows.skips(at, cutoff))
            || self.reduced_away(at)
            || self.keyed_away(at)
    }

    /// The key lists the joins above handed down, each with the table column it is about.
    fn keyed(&self) -> impl Iterator<Item = (usize, &Keys)> {
        let joins = self.sideways.iter().chain(self.also.iter().map(|(sideways, _)| sideways));
        joins.filter_map(|sideways| {
            let (at, keys) = sideways.keys(self.index)?;
            Some((self.columns.get(at).copied().flatten()?, keys))
        })
    }

    /// Whether a join above holds no key inside part `at`, so the part is never read. See [`Keys`].
    fn keyed_away(&self, at: usize) -> bool {
        self.keyed().any(|(column, keys)| {
            self.table.rows().ruled_by(at, column, &|range| keys.misses(range))
        })
    }

    /// The exact rows a join above handed down, and where part `at` starts, when there are some.
    ///
    /// The position is the row's place in the table, which is what a link calls a child `rid`, so
    /// the set can be asked about a part without reading anything.
    fn reduced(&self, at: usize) -> Option<(&Rids, u64)> {
        let rows = self.sideways.as_ref()?.rows(self.index)?;
        let first = u64::try_from(*self.offsets.get(at)?).ok()?;
        Some((rows, first))
    }

    /// Whether the exact rows hold nothing inside part `at`, so the part is never read.
    ///
    /// spec/graph/05-execution.md section 5.5. On a child stored in the order of its parent, which
    /// is `lineitem` against `orders`, a selective filter on the parent leaves most parts of the
    /// child with no member at all, and this is where they go.
    fn reduced_away(&self, at: usize) -> bool {
        let Some((rows, first)) = self.reduced(at) else { return false };
        let Some(len) =
            self.table.rows().chunk_len(at).ok().and_then(|len| u64::try_from(len).ok())
        else {
            return false;
        };
        len > 0 && !rows.any_between(first, first + len - 1)
    }

    /// The parts the statistics leave alive, by stripe, and how many rows they hold between them.
    ///
    /// This is the pruning the scan used to do while reading, asked before any worker is started, and
    /// it buys two things the read time version could not give. The instance count can be taken from
    /// the rows that are really coming rather than from the size of the table. And the work can be
    /// divided by where it is, which matters because a selective predicate on a file written in key
    /// order leaves its surviving parts next to each other: on ClickBench 39 the filter keeps about
    /// ninety of nine hundred and seventy four parts and they sit inside two stripes, so a morsel per
    /// stripe left fourteen of sixteen workers with nothing to do and the query was faster on one
    /// thread than on the whole machine.
    ///
    /// It asks in two passes because the two kinds of statistic cost very different amounts. The
    /// stripe bounds are in the directory and already in memory, so the first pass is sixteen
    /// comparisons and no reading at all. The sieves are a page per stripe off the disk, and that is
    /// the pass this skips whenever the bounds have already spread the work over enough stripes to
    /// keep every worker busy, because there the sieves would buy a better instance count at the
    /// price of doing serially what the workers were about to do at the same time. ClickBench 19 is
    /// the query that pays it: an equality on an identifier, where no stripe bound rules anything out
    /// and the sieves rule out all but nine parts, and doing them here made it half as fast.
    fn living(&self, threads: usize, weight: usize) -> (Vec<Live>, usize) {
        let (mut live, rows) = self.bounded();
        let working = live.iter().filter(|stripe| !stripe.parts.is_empty()).count();
        let probes = self.testing();
        // A key list is asked whatever the spread, because it is what turns a scan of every part
        // into a scan of a handful, and the instance count should be taken from the handful.
        let keyed = self.keyed().next().is_some();
        let sifting = !probes.is_empty() && worth_sifting(working, threads, rows, weight);
        if !keyed && !sifting {
            return (live, rows);
        }
        let mut sifted = 0;
        for stripe in &mut live {
            stripe.parts.retain(|&at| {
                !(sifting && self.table.rows().skips(at, probes)) && !self.keyed_away(at)
            });
            stripe.rows =
                stripe.parts.iter().map(|&at| self.table.rows().chunk_len(at).unwrap_or(0)).sum();
            sifted += stripe.rows;
        }
        (live, sifted)
    }

    /// The first of those two passes, the one that reads nothing.
    ///
    /// A stripe nothing was pruned from takes its row count off the directory rather than by adding
    /// up its parts, which is the same answer without the walk. It is worth not doing rather than
    /// worth doing: this runs on the one thread every worker is waiting for, and a table in memory
    /// puts a hundred and twenty chunks in a row group, so on twenty million rows the walk was
    /// 19,532 lookups to arrive at 163 numbers the groups were already holding. It does not show up
    /// in a query time, and the reason to say so is that the same walk on a native file is sixty
    /// four parts to a stripe and nobody would have thought to look.
    fn bounded(&self) -> (Vec<Live>, usize) {
        let probes = self.testing();
        let mut rows = 0;
        let mut live = Vec::with_capacity(self.stripes.len());
        for (stripe, parts) in self.stripes.iter().enumerate() {
            let keyed_away = || {
                self.keyed().any(|(column, keys)| {
                    self.table.rows().stripe_ruled_by(stripe, column, &|range| keys.misses(range))
                })
            };
            if (!probes.is_empty() && self.table.rows().stripe_skips(stripe, probes))
                || keyed_away()
            {
                live.push(Live::default());
                continue;
            }
            let held = self.table.rows().stripe_rows(stripe);
            rows += held;
            live.push(Live { parts: parts.clone().collect(), rows: held });
        }
        (live, rows)
    }

    /// What this scan produces.
    pub(crate) fn schema(&self) -> &Schema {
        &self.schema
    }

    /// Connects this scan's part counts to the operator row that owns it.
    pub(crate) fn watched(mut self, counters: Arc<Counters>) -> Self {
        self.counters = Some(counters);
        self
    }

    /// The parts no morsel covers, which are the ones ruled out before any worker started.
    ///
    /// Counted here and not in [`Source::read`] because a part outside every run is never visited,
    /// so the walk has no chance to see it. Between this and the walk, every part of the table is
    /// counted exactly once, which is the invariant the test at the bottom of this file checks.
    fn unreached(&self, runs: &[Range<usize>]) {
        let Some(counters) = self.counters.as_ref() else { return };
        let covered: usize = runs.iter().map(|run| run.end.saturating_sub(run.start)).sum();
        for _ in 0..self.table.rows().chunk_count().saturating_sub(covered) {
            counters.part_pruned();
        }
    }
}

/// What one stripe has left in it after the statistics have been asked.
///
/// The two numbers travel together because they are answered together and because keeping them apart
/// is how the row count ends up being worked out twice: once to size the instances and once to cut
/// the runs. Empty parts and no rows is a stripe that was ruled out, which is a case the callers
/// test for rather than drop, since a run never crosses a stripe and the positions have to line up.
#[derive(Debug, Default)]
struct Live {
    /// The parts still worth reading, in order.
    parts: Vec<usize>,
    /// How many rows those parts hold.
    rows: usize,
}

/// Whether reading the sieves before the workers start is worth what it costs.
///
/// `working` is how many stripes the bounds left with anything in them and `rows` is what those hold.
/// The sieves would say which parts of those stripes really matter, which gives a truer instance
/// count and lets the work be cut where it is, and they cost a page read and a decode per stripe on
/// the one thread that is asking. That trade only pays when the bounds have left the work piled into
/// fewer stripes than there are workers to want it, because that is the case where handing out a
/// stripe apiece leaves most of the machine idle. When the work is already spread the workers find it
/// themselves, at the same time, each reading the sieves of the stripe it is in.
fn worth_sifting(working: usize, threads: usize, rows: usize, weight: usize) -> bool {
    working < threads.min(instances_for(rows, weight))
}

/// The live parts cut into the runs a morsel covers, by the rows they hold rather than by the stripe.
///
/// The unit used to be the stripe, because sixty four parts under one page is the read the disk wants
/// and a worker that owns the whole stripe owns the page, and a stripe was cut further only when there
/// were not enough stripes with work in them to go round. What that missed is that two stripes with
/// work in them are not two equal pieces of work. The statistics leave hundreds of live parts in one
/// and three in the next, both of them count as a stripe with work in it, and a morsel apiece hands
/// one worker a hundred times what it hands another. On ClickBench 39 that showed up as the slowest of
/// five instances taking 2.2 milliseconds against a mean of 0.96, which is more than half the query
/// spent waiting for one thread.
///
/// So the question is how many rows a stripe holds and not whether it holds any. A stripe holding no
/// more than one worker's share stays one run and reads its own page, which is every stripe of a table
/// nothing was pruned from, so the ownership the old rule was protecting is untouched in the case it
/// was written for. A stripe holding more than a share is cut, and it is cut into half shares rather
/// than into shares, because by then its page is being shared whatever happens and the only thing left
/// to play for is letting a worker that drew a slow piece be overtaken instead of waited for.
///
/// Dividing by rows is not the same as dividing by work, and when the statistics have piled the live
/// parts into fewer stripes than there are workers it is not even close. Equal rows go to every worker
/// and then a filter above the scan keeps six percent of them, and if the survivors sit together, which
/// on a file in key order they do, whichever worker holds them does all of the aggregate work above the
/// scan while the rest finish and stop. No division by rows can see that, because the rows are already
/// equal. What can survive it is having more pieces than workers, so that the worker holding the dense
/// piece is overtaken rather than waited for, and the piled case is exactly the case where cutting
/// finer is free: the stripes are already being shared, so the page ownership that argues for a whole
/// stripe has already been given up. A quarter of a share there, a whole share when there are as many
/// working stripes as workers.
///
/// Runs never cross a stripe either way. One that did would own two pages, which is the thing all of
/// this is avoiding.
fn runs_of(live: &[Live], instances: usize, rows: impl Fn(usize) -> usize) -> Vec<Range<usize>> {
    let total: usize = live.iter().map(|stripe| stripe.rows).sum();
    let working = live.iter().filter(|stripe| !stripe.parts.is_empty()).count();
    let share = total.div_ceil(instances.max(1)).max(1);
    // The biggest run allowed, and the size of the pieces an oversized stripe is cut into. They are
    // the same number in the piled case because there is nothing left to protect there.
    let (whole, piece) = if working >= instances {
        (share, share.div_ceil(2).max(1))
    } else {
        let piece = share.div_ceil(4).max(1);
        (piece, piece)
    };
    let mut runs: Vec<Range<usize>> = Vec::with_capacity(instances.saturating_mul(2));
    for stripe in live {
        let parts = &stripe.parts;
        let Some((&first, &last)) = parts.first().zip(parts.last()) else { continue };
        if stripe.rows <= whole {
            runs.push(first..last + 1);
            continue;
        }
        let mut taken = 0;
        let mut open = false;
        for &at in parts {
            let size = rows(at);
            // Closed before the part that would take it over rather than after, so a run is at most a
            // piece. A part bigger than a piece on its own is its own run, because a part is the
            // smallest thing a morsel can point at.
            if open && taken + size <= piece {
                if let Some(run) = runs.last_mut() {
                    run.end = at + 1;
                }
                taken += size;
            } else {
                runs.push(at..at + 1);
                taken = size;
                open = true;
            }
        }
    }
    runs
}

/// How many parts a run covers at most when a top N above the scan wants the best of them first.
const ORDERED_RUN: usize = 8;

/// The runs with the pieces of at most [`ORDERED_RUN`] parts a top N wants most cut out of them, and
/// the order to hand them out in, for a scan under a top N whose first key is one of its columns.
///
/// The cutoff a top N tells the scan is only as good as the rows it has seen, and a scan that walks
/// the file in the order it was written shows it rows in that order. ClickBench 26 orders by
/// EventTime a file written in CounterID order, where every counter starts at the first day, so the
/// earliest times are spread over the whole file and the scan read 380 of 2830 parts before the
/// cutoff ruled out the rest. ClickHouse reads in the order of the key when it can for the same
/// reason. Here the parts are not moved, since the file order is what the pages are cut by, but the
/// pieces whose stored range reaches furthest the way the ordering wants, the smallest low for an
/// ascending key and the largest high for a descending one, go out first. After those the cutoff is
/// close to the answer's last key and most of what is left is ruled out by its stored range alone.
///
/// Only the best `front` pieces go first. What is left of each run between them stays one run and
/// goes out after them in table order, so a query the cutoff helps little, such as one whose filter
/// keeps few of the rows in the pieces that looked best, still reads most of the file in long runs.
/// A piece with no stored range is never put first, and pieces that tie keep their table order.
fn best_first(
    runs: Vec<Range<usize>>,
    front: usize,
    descending: bool,
    range: impl Fn(usize) -> Option<rudb_storage::Range>,
) -> (Vec<Range<usize>>, Vec<usize>) {
    let mut pieces = Vec::with_capacity(runs.len());
    for run in runs.iter().cloned() {
        let mut start = run.start;
        while start < run.end {
            let end = (start + ORDERED_RUN).min(run.end);
            pieces.push(start..end);
            start = end;
        }
    }
    let reach: Vec<Option<Bound>> = pieces
        .iter()
        .map(|piece| {
            piece
                .clone()
                .filter_map(|at| {
                    let range = range(at)?;
                    if descending { range.high } else { range.low }
                })
                .reduce(
                    |one, other| if descending { one.larger(other) } else { one.smaller(other) },
                )
        })
        .collect();
    let better = if descending { std::cmp::Ordering::Greater } else { std::cmp::Ordering::Less };
    let mut first = vec![false; pieces.len()];
    let mut chosen = Vec::with_capacity(front);
    // A pass per piece picked rather than a sort, because two bounds need not compare and a sort
    // handed an order that is not total may panic. `front` is a few times the workers.
    for _ in 0..front.min(pieces.len()) {
        let mut best: Option<usize> = None;
        for (at, bound) in reach.iter().enumerate() {
            let Some(bound) = bound else { continue };
            if first[at] {
                continue;
            }
            let wins = best
                .and_then(|best| reach[best].as_ref())
                .is_none_or(|held| bound.order(held) == Some(better));
            if wins {
                best = Some(at);
            }
        }
        let Some(best) = best else { break };
        first[best] = true;
        chosen.push(best);
    }
    if chosen.is_empty() {
        return (runs, Vec::new());
    }
    // Put back together in table order, a chosen piece on its own and the pieces between two chosen
    // ones as one run, remembering where each chosen piece landed.
    let mut out = Vec::with_capacity(runs.len() + 2 * chosen.len());
    let mut landed = vec![0; pieces.len()];
    let mut early = Vec::with_capacity(runs.len() + 2 * chosen.len());
    let mut next = 0;
    for run in &runs {
        let mut rest: Option<Range<usize>> = None;
        while let Some(piece) = pieces.get(next).filter(|piece| piece.start < run.end) {
            if first[next] {
                if let Some(rest) = rest.take() {
                    out.push(rest);
                    early.push(false);
                }
                landed[next] = out.len();
                out.push(piece.clone());
                early.push(true);
            } else {
                match rest.as_mut() {
                    Some(rest) => rest.end = piece.end,
                    None => rest = Some(piece.clone()),
                }
            }
            next += 1;
        }
        if let Some(rest) = rest {
            out.push(rest);
            early.push(false);
        }
    }
    let mut order: Vec<usize> = chosen.iter().map(|&piece| landed[piece]).collect();
    order.extend((0..out.len()).filter(|&at| !early[at]));
    (out, order)
}

impl Source for Scan<'_> {
    fn morsel(&self) -> Option<Morsel> {
        let Some(spread) = self.spread.get() else {
            return self.chunks.take();
        };
        let taken = usize::try_from(spread.handout.number()?).unwrap_or(usize::MAX);
        let index = spread.order.get(taken).copied().unwrap_or(taken);
        let run = spread.runs.get(index)?;
        let start = u64::try_from(run.start).unwrap_or(u64::MAX);
        let end = u64::try_from(run.end).unwrap_or(u64::MAX);
        Some(Morsel::new(u64::try_from(index).unwrap_or(u64::MAX), start, end))
    }

    fn morsels(&self, threads: usize, weight: usize) -> Option<usize> {
        let chunks = self.chunks.total();
        // A table that says nothing about how its chunks are grouped has nothing to divide by, so
        // its chunks go out one at a time to whoever asks, which is what every table did before
        // there were row groups. The test is on the grouping and not on the format, because an in
        // memory table has row groups now and they are the same thing to everything below.
        if self.stripes.is_empty() {
            return Some(chunks);
        }
        let (live, rows) = self.living(threads, weight);
        // An instance is not free, so a scan asks for as many as the rows behind it can pay for
        // rather than for every worker the machine has. What one costs is a thread, and what it
        // buys is a share of the work, and `instances_for` is where those two are weighed. The
        // rows it is handed are the ones the zone maps leave rather than the ones the table holds,
        // because a query that reads a fifteenth of a file is a query the size of a fifteenth of a
        // file and asking for a worker per stripe of the whole of it is asking for fifteen threads
        // that start, find their stripe ruled out and stop.
        let instances = chunks.min(threads).min(instances_for(rows, weight));
        // A stripe per worker only divides the work when there are at least as many stripes as
        // workers. Below that it would leave workers with nothing, and the duplicate reads it
        // avoids are cheaper than the half of the machine it would cost, so a small table keeps
        // handing out parts. The reader is told what is coming before any of it starts, because a
        // page cache smaller than the number of workers in it evicts pages that are still in use.
        //
        // One worker takes stripes too, which page ownership on its own would not ask for, because
        // a morsel covering a run of parts is what lets [`Self::read`] walk past the parts the zone
        // maps rule out. A morsel covering one part cannot walk anywhere: it is drained the moment
        // that part is ruled out, so the scan has to hand the empty chunk up and be called again.
        if instances > 0 && self.stripes.len() >= instances {
            // Twice the workers rather than exactly the workers. The cache drops the page that has
            // been there longest, which with a slot per worker is always the page of whoever
            // entered their stripe first, which is the worker still in it. Room for the stripe
            // each worker has moved on to as well as the one it is in is what stops a straggler
            // reading its own page again, and it costs a page per worker per column.
            self.table.rows().keep_stripes(instances.saturating_mul(2));
            let runs = runs_of(&live, instances, |at| self.table.rows().chunk_len(at).unwrap_or(0));
            self.unreached(&runs);
            let (runs, order) = match self.ordered() {
                Some((column, descending)) => best_first(runs, instances * 2, descending, |at| {
                    self.table.rows().range_of(at, column)
                }),
                None => (runs, Vec::new()),
            };
            let _ = self.spread.set(Spread { handout: Handout::new(runs.len()), runs, order });
        }
        Some(instances)
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        // A part the zone maps have ruled out is never read, so its columns are never copied and
        // its rows are never handed to the filter above. It is also never handed up as an empty
        // chunk, which is what this loop is for: returning one costs a call into every operator in
        // the pipeline to carry nothing, and a selective query is mostly ruled out parts. On the
        // million row ClickBench file a point lookup rules out all but four of 974 parts and used
        // to push the other 970 through the filter and the projection one at a time, which was 1.7
        // of the 1.8 milliseconds it took to answer with no rows.
        //
        // Walking rather than returning is safe because a part that skips has nothing the caller
        // could want, so the only thing the old return said that this does not is how far the
        // morsel had got, and nothing outside asks that between one part and the next.
        let probes = self.testing();
        // Asked again for every part this hands back, rather than settled once the way `testing` is,
        // because the top N above fills it while this runs and it only ever tightens. Once per part
        // rather than once per call, since a call that walks a long run of ruled out parts is a call
        // during which the other workers are still reading and improving it. See [`crate::cutoff`].
        let cutoff = self.cutoff();
        let at = loop {
            let at = position(morsel);
            if at >= self.table.rows().chunk_count() || morsel.is_drained() {
                *out = Chunk::empty(&self.schema.types());
                return Ok(Progress::Done);
            }
            morsel.advance(1);
            if !self.ruled(at, probes, &cutoff) {
                break at;
            }
            self.skipped.fetch_add(1, Ordering::Relaxed);
            if let Some(counters) = &self.counters {
                counters.part_pruned();
            }
        };
        if let Some(counters) = &self.counters {
            counters.part_read();
        }
        if self.read_stored(at, out)? || self.read_late(at, out)? || self.read_deferring(at, out)? {
            return Ok(more(morsel));
        }
        if self.read_reduced(at, out)? {
            return Ok(more(morsel));
        }
        let projected: Vec<usize> = self.columns.iter().flatten().copied().collect();
        let read = self.table.rows().read(at, &projected)?;
        if self.columns.iter().all(Option::is_some) {
            *out = read;
            self.narrow_read(at, out, true)?;
            return Ok(more(morsel));
        }
        let mut held = Vec::with_capacity(self.columns.len());
        let mut real = 0;
        for column in &self.columns {
            if column.is_some() {
                held.push(read.column(real)?.clone());
                real += 1;
            } else {
                held.push(Vector::sequence(self.offsets[at], 1, read.len()));
            }
        }
        *out = Chunk::with_rows(held, read.len())?;
        self.narrow_read(at, out, true)?;
        Ok(more(morsel))
    }

    /// A pass over the rows for every step of the filter this scan took off the operator above it.
    ///
    /// Counted exactly the way [`crate::stream::Filter`] counts it, because it is that operator's
    /// work and the pipeline has to arrive at the same number whichever side of the scan boundary
    /// it is being done on. Zero for a scan with nothing pushed into it, which is a scan that only
    /// reads, and reading is the 1 the pipeline already counts.
    fn weight(&self) -> usize {
        self.pushed.as_ref().map_or(0, |pushed| pushed.predicate.passes())
    }
}

/// Whether a morsel the scan has just taken a part out of has another one in it.
fn more(morsel: &Morsel) -> Progress {
    if morsel.is_drained() { Progress::Done } else { Progress::More }
}

/// How many instances of a scan over a table cut into row groups are worth running.
///
/// An instance costs a worker, and a worker costs a lock, a push and a notify now that the pool
/// parks its threads rather than starting one per pipeline per run. It used to cost a thread
/// creation, about sixteen microseconds, and a sixteen way scan spent a quarter of a millisecond
/// before it read a row. What is left is the share of the work the instance takes, which is the
/// tension this function sits in: dividing a small table further buys less than the coordination
/// costs, whatever the coordination is.
///
/// Two slopes, and the larger wins. The first grows to eight and is what a small table uses, where
/// there are not many rows to divide and dividing them further buys less than the threads cost. The
/// second has no ceiling of its own and takes over past about half a million rows, where there is
/// enough work behind each instance that another one pays for itself. What actually stops it is the
/// pool, since [`Pipeline::degree`](rudb_pipeline::Pipeline::degree) clamps this to the threads the
/// database was given.
///
/// The numbers are measured rather than reasoned. Over the whole of ClickBench on the 999,975 row
/// `hits` sample, warm, with the cap forced to a fixed value: four is 261.3 ms, eight is 198.9,
/// sixteen is 188.1 and thirty two is 192.5. On the same suite over a 100,000 row sample: two is
/// 42.2 ms, four is 31.5, eight is 31.4 and sixteen is 35.9. So a million rows wants sixteen, a
/// hundred thousand wants four to eight, and both of those are what these two slopes give.
///
/// The old shape of this capped small tables at four and only grew past six hundred thousand rows,
/// on a measurement that said sixteen instances made the million row suite 16.6 percent slower.
/// That is no longer true of this engine and it is worth saying why rather than quietly changing
/// the constant: the per instance cost that measurement was paying has come down, so the cap moved
/// with it.
///
/// That comment used to end by saying these should be measured again once the thread stopped being
/// created per pipeline per run, because that was the last fixed cost in here and removing it would
/// take both slopes up. The thread is gone and the measurement was done, and the answer was no. On
/// the million row sample, halving both divisors is 196.3 ms against 199.8, and quartering them is
/// 196.5. On the hundred thousand row sample, halving them is 35.3 ms against 35.5 and quartering
/// them is 38.2. So dividing further is worth about a percent at a million rows and costs eight
/// percent at a hundred thousand, and the numbers stay where they are. What that says is that the
/// scan is no longer what limits how much of this machine a query uses, and the next thing to look
/// at is the operators above it.
///
/// The operators above it are what `weight` is. Both slopes were in rows, and a row is not what an
/// instance is worth, it is what the work behind the instance was being counted in. The two stop
/// agreeing the moment two queries spend different amounts on a row. ClickBench 39, 40 and 42 are
/// where that showed: each of them prunes to about twenty four thousand rows and each of them ran
/// the whole query on one thread of thirty two, because twenty four thousand is under the first
/// divisor. Twenty four thousand rows is small. Twenty four thousand rows of 39's aggregate, which
/// spends a hundred and eleven nanoseconds on each one grouping five columns with two wide strings
/// among them, is a millisecond of work on one core with thirty one idle.
///
/// So the small slope counts work and the large one still counts rows. That split is the whole of
/// the rule and it is deliberate. The small slope is the one that says a query is too small to
/// divide, and whether it is depends on what the query does with what it reads. The large slope is
/// the one that says how far a big query is worth dividing, and the measurements above say that is
/// sixteen at a million rows and that thirty two is worse, which is a fact about this machine and
/// not about the query, so nothing the operators say should move it.
///
/// Ordinary work is a weight of 1 and gives back exactly the rule that was measured. See
/// [`Stream::weight`](rudb_pipeline::Stream::weight) for what an operator is answering.
fn instances_for(rows: usize, weight: usize) -> usize {
    let small = rows.saturating_mul(weight.max(1)).div_ceil(25_000).min(8);
    let large = rows.div_ceil(62_500);
    small.max(large).max(1)
}

/// One row and no columns.
///
/// What `SELECT 1` sits on. It produces a chunk of width zero and length one exactly once, which is
/// the case `Chunk`'s stored row count exists for.
#[derive(Debug)]
pub(crate) struct Dummy {
    schema: Schema,
    one: Handout,
}

impl Dummy {
    pub(crate) fn new() -> Self {
        Self { schema: Schema::empty(), one: Handout::new(1) }
    }

    /// What this produces, which is no columns at all.
    pub(crate) fn schema(&self) -> &Schema {
        &self.schema
    }
}

impl Source for Dummy {
    fn morsel(&self) -> Option<Morsel> {
        self.one.take()
    }

    fn morsels(&self, _threads: usize, _weight: usize) -> Option<usize> {
        Some(self.one.total())
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        *out = Chunk::with_rows(Vec::new(), 1)?;
        morsel.advance(1);
        Ok(Progress::Done)
    }
}

/// Literal rows.
///
/// The expressions are evaluated once when the operator is built, over a one row chunk with no
/// columns, because a `VALUES` row in a bound plan is constants and folded arithmetic and cannot
/// refer to anything. Evaluating them lazily would buy nothing and would make an error in a literal
/// arrive on the first `next` rather than where the query says it is.
#[derive(Debug)]
pub(crate) struct Values {
    schema: Schema,
    chunks: Vec<Chunk>,
    handout: Handout,
}

impl Values {
    /// The rows of a [`Node::Values`](rudb_plan::Node::Values), already evaluated.
    ///
    /// # Errors
    ///
    /// If a row is not as wide as the column list, or anything the expressions report.
    /// The rows evaluated with the semantics of the query session.
    pub(crate) fn new(
        plan: &Plan,
        index: u32,
        columns: Slice,
        rows: Slice,
        session: &Session,
    ) -> Result<Self> {
        let time_zone = session.session_time_zone();
        let fields = plan.field_list(columns).to_vec();
        let schema = Schema::numbered(fields, index);
        let types = schema.types();
        let source = Schema::empty();
        let one = Chunk::with_rows(Vec::new(), 1)?;
        let mut down: Vec<Vec<Value>> = vec![Vec::new(); types.len()];
        for row in plan.row_list(rows) {
            let exprs: Vec<ExprRef> = plan.expr_list(*row).to_vec();
            if exprs.len() != types.len() {
                return Err(Error::internal(format!(
                    "a VALUES row of {} expressions in a {} column list",
                    exprs.len(),
                    types.len()
                )));
            }
            let evaluated = evaluate_all_in_time_zone(plan, &exprs, &source, &one, time_zone)?;
            for (position, vector) in evaluated.iter().enumerate() {
                down[position].push(vector.value_at(0));
            }
        }
        let total = down.first().map_or(0, Vec::len);
        let mut chunks = Vec::new();
        let mut start = 0;
        while start < total {
            let end = (start + VECTOR_SIZE).min(total);
            let mut built = Vec::with_capacity(types.len());
            for (position, ty) in types.iter().enumerate() {
                built.push(Vector::from_values(ty.clone(), &down[position][start..end])?);
            }
            chunks.push(Chunk::with_rows(built, end - start)?);
            start = end;
        }
        let handout = Handout::new(chunks.len());
        Ok(Self { schema, chunks, handout })
    }

    /// What these rows are.
    pub(crate) fn schema(&self) -> &Schema {
        &self.schema
    }
}

/// A table function that produces a run of integers.
///
/// The arguments are evaluated once when the operator is built, the same way a `VALUES` row is and
/// for the same reason: they are constants by the time they are here, since a table function that
/// can see a row is `LATERAL` and does not bind to this node.
///
/// The values are produced a chunk at a time rather than all at once. `range(100000000)` is a
/// hundred million rows and a corpus that writes it means it, so materializing the whole run into
/// a `Vec` before the first chunk comes out would be eight hundred megabytes for a query whose
/// answer is one number.
/// A morsel here is a run of positions in the sequence, several chunks long, because the rows are
/// worked out rather than read and handing out a morsel per chunk would be more counter traffic than
/// arithmetic. Sixteen chunks is small enough that a hundred million rows is still six thousand
/// units for a scheduler to balance and large enough that the handout is not the cost.
#[derive(Debug)]
pub(crate) struct Series {
    schema: Schema,
    /// The first value, which is the value at position zero.
    start: i64,
    step: i64,
    /// How many values there are, which `series_length` worked out once.
    rows: u64,
    /// BIGINT, or the moment type of a series of moments.
    ty: LogicalType,
    /// The moments of a series whose step has months in it, which cannot be worked out from a
    /// position and so are walked once and kept.
    listed: Option<Arc<[i64]>>,
    morsels: AtomicU64,
}

/// How many positions one morsel of a series covers.
const RUN: u64 = 16 * VECTOR_SIZE as u64;

impl Series {
    /// The rows of a [`Node::TableFunction`](rudb_plan::Node::TableFunction).
    ///
    /// A null in any argument gives no rows at all, which is DuckDB's answer and is not the same
    /// as an error. The three defaults are the three that make a one argument call mean what
    /// everybody writes it to mean, which is zero up to the number.
    ///
    /// # Errors
    ///
    /// Whatever evaluating an argument reports, and a step of zero.
    pub(crate) fn new(plan: &Plan, index: u32, function: &str, args: Slice) -> Result<Self> {
        let Some(function) = TableFunction::lookup(function) else {
            return Err(Error::internal(format!("a plan with a table function called {function}")));
        };
        let exprs: Vec<ExprRef> = plan.expr_list(args).to_vec();
        let source = Schema::empty();
        let one = Chunk::with_rows(Vec::new(), 1)?;
        let evaluated = evaluate_all(plan, &exprs, &source, &one)?;
        if let [start, stop, step] = evaluated.as_slice()
            && *step.logical_type() == LogicalType::Interval
        {
            let ty = start.logical_type().clone();
            let schema = Schema::numbered(vec![Field::new(function.name(), ty.clone())], index);
            let empty = Self { ty, ..Self::empty(schema.clone()) };
            let values = (start.value_at(0), stop.value_at(0), step.value_at(0));
            let Some(stepping) = moments(function, &values.0, &values.1, &values.2)? else {
                return Ok(empty);
            };
            let rows = u64::try_from(stepping.len()).unwrap_or(u64::MAX);
            return Ok(match stepping {
                Stepping::Even { start, step, .. } => Self { start, step, rows, ..empty },
                Stepping::Listed(stamps) => Self { rows, listed: Some(stamps.into()), ..empty },
            });
        }
        let fields = vec![Field::new(function.name(), LogicalType::BigInt)];
        let schema = Schema::numbered(fields, index);
        let mut given = Vec::with_capacity(evaluated.len());
        for vector in &evaluated {
            match vector.value_at(0) {
                Value::Null => return Ok(Self::empty(schema)),
                Value::BigInt(n) => given.push(n),
                other => {
                    return Err(Error::internal(format!(
                        "a table function argument bound as BIGINT arrived as {other}"
                    )));
                }
            }
        }
        let (start, stop, step) = match given.as_slice() {
            [stop] => (0, *stop, 1),
            [start, stop] => (*start, *stop, 1),
            [start, stop, step] => (*start, *stop, *step),
            _ => {
                return Err(Error::internal(format!(
                    "{}() bound with {} arguments",
                    function.name(),
                    given.len()
                )));
            }
        };
        let rows = u64::try_from(series_length(function, start, stop, step)?).unwrap_or(u64::MAX);
        Ok(Self { start, step, rows, ..Self::empty(schema) })
    }

    fn empty(schema: Schema) -> Self {
        Self {
            schema,
            start: 0,
            step: 1,
            rows: 0,
            ty: LogicalType::BigInt,
            listed: None,
            morsels: AtomicU64::new(0),
        }
    }

    /// What this produces, which is one column named after the function.
    pub(crate) fn schema(&self) -> &Schema {
        &self.schema
    }

    /// The value at a position in the sequence.
    ///
    /// Worked out rather than carried, because a thread that is handed the tenth morsel has not
    /// counted its way to it and never will. Inside a chunk the step is still added a row at a time,
    /// which is what keeps the answers the same as the loop that used to be here.
    fn value_at(&self, position: u64) -> i64 {
        let steps = i64::try_from(position).unwrap_or(i64::MAX);
        self.start.saturating_add(self.step.saturating_mul(steps))
    }
}

impl Source for Series {
    fn morsel(&self) -> Option<Morsel> {
        let index = self.morsels.fetch_add(1, Ordering::Relaxed);
        let start = index.saturating_mul(RUN);
        (start < self.rows)
            .then(|| Morsel::new(index, start, self.rows.min(start.saturating_add(RUN))))
    }

    fn morsels(&self, _threads: usize, _weight: usize) -> Option<usize> {
        Some(usize::try_from(self.rows.div_ceil(RUN)).unwrap_or(usize::MAX))
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        let count = usize::try_from(morsel.remaining()).unwrap_or(usize::MAX).min(VECTOR_SIZE);
        if count == 0 {
            *out = Chunk::empty(std::slice::from_ref(&self.ty));
            return Ok(Progress::Done);
        }
        // The loop is over `i64` rather than over `Value`, and the vector is built out of the run
        // it fills rather than out of a list of tagged values that would have to be read back one
        // at a time to find the run again. `range()` is the source every microbenchmark in
        // `rudb-bench` reads from, so a chunk of it costing a `Value` a row would be measuring the
        // generator instead of what is downstream of it.
        let counted = if let Some(listed) = &self.listed {
            let from = usize::try_from(morsel.cursor()).unwrap_or(usize::MAX);
            let Some(run) = listed.get(from..from.saturating_add(count)) else {
                return Err(Error::internal("a morsel past the end of a series of moments"));
            };
            run.to_vec()
        } else {
            let mut at = self.value_at(morsel.cursor());
            let mut counted = Vec::with_capacity(count);
            for _ in 0..count {
                counted.push(at);
                at = at.saturating_add(self.step);
            }
            counted
        };
        morsel.advance(u64::try_from(count).unwrap_or(u64::MAX));
        let vector = Vector::flat(self.ty.clone(), Data::Int64(counted.into()))?;
        *out = Chunk::with_rows(vec![vector], count)?;
        Ok(if morsel.is_drained() { Progress::Done } else { Progress::More })
    }
}

/// The moments a `range` or `generate_series` call over dates or timestamps gives, or `None` when an
/// argument is null, which is no rows at all.
///
/// The bounds are checked here and not in the kernel because the table form refuses them in its
/// own words, as binder errors, and refuses a zero interval where the list form answers empty.
///
/// # Errors
///
/// An infinite bound, a zero interval, one with mixed signs, and a series past 2^32 moments.
pub(crate) fn moments(
    function: TableFunction,
    start: &Value,
    stop: &Value,
    step: &Value,
) -> Result<Option<Stepping>> {
    let moment = |value: &Value| match value {
        Value::Timestamp(stamp) | Value::TimestampTz(stamp) => Ok(Some(*stamp)),
        Value::Null => Ok(None),
        other => Err(Error::internal(format!("a range of moments from a {other}"))),
    };
    let (Some(start), Some(stop), Value::Interval { months, days, micros }) =
        (moment(start)?, moment(stop)?, step)
    else {
        return Ok(None);
    };
    if [start, stop].iter().any(|&stamp| stamp == i64::MAX || stamp == -i64::MAX) {
        return Err(Error::binder("RANGE with infinite bounds is not supported"));
    }
    let forward = *months > 0 || *days > 0 || *micros > 0;
    let backward = *months < 0 || *days < 0 || *micros < 0;
    if !forward && !backward {
        return Err(Error::binder("interval cannot be 0!"));
    }
    if forward && backward {
        return Err(Error::binder(
            "RANGE with composite interval that has mixed signs is not supported",
        ));
    }
    moment_steps(function.inclusive(), start, stop, (*months, *days, *micros)).map(Some)
}

/// A scan of one or more files, Parquet or CSV.
///
/// The files are named by the arguments, which the binder already expanded: a pattern was walked
/// there and a name that is not a pattern was checked there, so what arrives is a list of names that
/// existed when the statement was bound. They are opened here rather than being carried from the
/// binder, because binding and running are separated by however long a prepared statement lives and
/// a plan that held open descriptors would hold them for all of that.
///
/// One at a time, in the order the list gives, which is the order the rows come out in. Opening all
/// of them up front would mean a directory of ten thousand files costing ten thousand descriptors
/// before the first row, and closing each one at its end is what makes a scan of a whole directory
/// cost one.
///
/// The plan's column list is resolved against each file's by name, which is the same thing [`Scan`]
/// does against a catalog table and for the same reason. Today the binder projects every column in
/// order, so the mapping is the identity, and the moment projection pushdown makes the plan's list a
/// subset the reader reads a subset. That is the difference `spec/engine/05-scan.md` section 5.6
/// describes between reading two columns of ClickBench and reading a hundred and five.
///
/// The first file decides the types and every file after it is cast to them, which is DuckDB's rule
/// and was measured: a second file holding `'5'` where the first holds an `INTEGER` reads as 5, and
/// one holding `'txt'` is a conversion error naming the file it came from.
///
/// That is the Parquet rule and CSV does not follow it. A Parquet file states its schema, so there
/// is a first file's word to take, and a CSV file states nothing, so the binder sniffed all of them
/// and combined the answers. What arrives here is that combined answer, and each CSV file is told it
/// as it is opened rather than being allowed to use its own sample, which is what keeps a file that
/// happens to hold nothing but whole numbers from handing up BIGINT into a stream that is DOUBLE.
///
/// A morsel is one row group of one Parquet file, or one range of bytes of one CSV file.
///
/// The row group is what the format stores and what a reader can be positioned at without having
/// read what came before it, so it is the smallest unit two threads can take without one of them
/// waiting on the other. A CSV file cannot be positioned, because nothing in it says where a row
/// begins until every byte before it has been parsed, so its ranges guess where their first row
/// starts and check the guess against where the range before ended. [`rudb_csv::split`] is where
/// that is done. A CSV file is one morsel when it is short, when the query has one thread, and when
/// the query wants `file_row_number`, since a range does not know how many rows came before it.
///
/// The files are cut into morsels one file at a time rather than all at once. Cutting a file means
/// reading its footer, and a directory of ten thousand files would be ten thousand footers read
/// before the first row came out, which is what the paragraph above about descriptors is about and
/// is the same answer.
///
/// Each morsel carries its own reader, and for Parquet those readers share one open file and one
/// parsed footer through [`Reader::split`]. So the scan holds no lock across a read, which is the
/// whole point: the version of this before #486 had one morsel, one reader and a mutex around it,
/// and a second thread asking for work got none.
///
/// `sideways` is what a join above this scan worked out about the key it is going to look these
/// rows up by, which is the same handoff [`Scan`] takes and for the same reason. It arrives here
/// rather than in the plan because it is not known until the join's other side has finished. Only
/// the filter tier is read: the range tier would have to reach the row group bounds, and those are
/// asked before the build side has run.
#[derive(Debug)]
pub(crate) struct FileScan<'a> {
    function: TableFunction,
    paths: Vec<String>,
    given: Given,
    wanted: Vec<Field>,
    /// Whether the last column the scan produces is the row's ordinal inside its own file.
    ///
    /// `file_row_number=True`, which is a column no file holds and the scan counts. It is last
    /// because the binder puts it last, and it is a flag rather than a position because everything
    /// else here indexes [`Self::wanted`] and that list is the file's columns only.
    numbered: bool,
    schema: Schema,
    /// The comparisons a row group's bounds can be checked against before it is handed out.
    ///
    /// Written in terms of this scan's own output positions, because that is what the filter above
    /// it is written in terms of and what the builder can read without knowing which file is open.
    /// [`Self::advance`] turns them into the file's column numbers, once per file, since two files
    /// of one glob are allowed to hold the same columns in a different order.
    ///
    /// Empty when there is no filter above the scan, when the filter has no conjunct a bound can
    /// answer, or when the source is a CSV, and empty means every row group is handed out, which is
    /// what every scan did before this existed.
    tests: Vec<(usize, Op, Bound)>,
    /// How far through the file list the cutting has got, and the file it is in the middle of.
    cutting: Mutex<Cutting>,
    /// What each morsel handed out covers, by [`Morsel::index`].
    ///
    /// The outer lock is held for a lookup and a clone of one handle, and the read that follows
    /// holds the inner one, so two threads reading two row groups never wait for each other. The
    /// entries stay after their morsel is drained, because they are three words and a dropped
    /// reader once the rows are out and because a driver is allowed to ask again.
    open: Mutex<HashMap<u64, Arc<Mutex<Piece>>>>,
    counters: Option<Arc<Counters>>,
    /// The runtime filter of the join this scan drives, empty for a scan that drives no join.
    sideways: Option<Arc<Sideways<'a>>>,
    /// Which table index this scan's columns bind against, which is how the runtime filter knows
    /// whether it is about one of them.
    index: u32,
    /// Whether the runtime filter has been earning the hash it costs.
    paying: Paying,
}

/// How many rows a scan aims to put in one morsel.
///
/// A morsel is the unit of work a thread takes, so it decides two things at once: how many threads
/// can be busy at all, and how evenly the last round of work divides among them. A row group is the
/// obvious unit and it is the wrong size for both. DuckDB writes a hundred and twenty two thousand
/// rows into one, so the million row ClickBench file has nine, and nine pieces of work is nine busy
/// threads on a machine with thirty two and one straggler deciding when everybody is finished.
///
/// Thirty two thousand is measured rather than picked. The same suite over copies of that file
/// written with different row group sizes runs in 898 ms on nine groups, 749 on thirty one and 768
/// on sixty one, all at eight threads, and at thirty two threads the nine group file is slower than
/// it was at eight while the thirty one group file is faster again at 676.
///
/// Whether a row group is cut at all is [`morsel_rows`], which asks the file how long its pages are
/// first, because a morsel cannot start inside a page for free.
const MORSEL_ROWS: usize = 32_768;

/// The rows a run of whole row groups is gathered up to, or zero when groups are being cut instead
/// or the sink asked for nothing.
///
/// A file written with small row groups is as common as one written with large ones: the ClickBench
/// ten million row sample is 1,203 groups of about eight thousand rows, and anything written by a
/// streaming job or a Spark stage with many small tasks looks the same. Handed out one group at a
/// time, every morsel pays a split of the reader, a registration and a hand over at the sink, and a
/// load into a native file pays far more, because that sink never lets a stripe span two morsels.
/// It writes eight thousand row stripes one at a time behind the writer's lock, and loading the
/// sample took 73.6 s against 22.2 s for the same rows in 81 groups, with 1,203 writes waiting a
/// summed 961 s. So a sink that pays for a boundary asks through [`Source::gather`] and the groups
/// are gathered until the next would take the run past what it asked for, and never so far that
/// there are fewer morsels than threads. A group that large or larger is handed out alone.
///
/// A query over the file asks for nothing and is cut as it always was. Gathering the same sample
/// for queries made the ClickBench set 6 to 14% slower on 32 threads, measured and not explained
/// yet, though fewer and larger morsels leaving threads idle at the end of a scan is the suspect.
fn gather_rows(reader: &FileReader, cut: usize, threads: usize, asked: usize) -> usize {
    let FileReader::Parquet(parquet) = reader else { return 0 };
    if cut != 0 || asked == 0 {
        return 0;
    }
    let total = parquet
        .metadata()
        .row_groups
        .iter()
        .map(|group| usize::try_from(group.rows).unwrap_or(usize::MAX))
        .fold(0_usize, usize::saturating_add);
    gather_target(total, threads, asked)
}

/// The arithmetic of [`gather_rows`]: a share of the file for every thread, and no more than the
/// sink asked for.
fn gather_target(total: usize, threads: usize, asked: usize) -> usize {
    (total / threads.max(1)).min(asked)
}

/// How many row groups the run starting with a group of `first` rows takes, `later` being the rows
/// of the groups after it that may join.
///
/// Always at least the first. The next joins only if the run stays within `gather` rows with it, so
/// a run never grows past the target by more than its first group.
fn gathered(first: usize, later: impl Iterator<Item = usize>, gather: usize) -> usize {
    let mut total = first;
    let mut taken = 1;
    for rows in later {
        match total.checked_add(rows) {
            Some(next) if next <= gather => {
                total = next;
                taken += 1;
            }
            _ => break,
        }
    }
    taken
}

/// How many times what a morsel wastes it has to read before the cutting is worth doing.
///
/// A morsel that starts inside a page pays for that page decoded twice, once by the morsel that ends
/// in it and once by the morsel that starts in it. The columns of a row group do not break their
/// pages in the same places, so a boundary costs one page of every column read, and holding a
/// morsel's first pages against the bytes it goes on to read is what says whether that is a rounding
/// error or the whole read.
///
/// Four is the smallest number that still refuses the file DuckDB writes, where the first page of a
/// chunk is the whole chunk and the waste is not a quarter but everything. A page a quarter of a
/// morsel long is measured to be worth cutting anyway: the ClickBench suite over a copy of the file
/// written with 64 KB pages runs in 770 ms at thirty two threads against 909 uncut.
///
/// Asking in bytes rather than in rows matters, and asking in rows is what this did first and got
/// wrong. A column with four distinct values packs a whole row group into one page of two bit
/// dictionary indices, so the longest page of a file measured in rows is always the cheapest column
/// in it, and a file of small pages was refused because of the one column that cost nothing to read.
const FINE: u64 = 4;

/// How many rows one morsel of a row group covers, or zero to hand out whole row groups.
///
/// Two things have to be true before a row group is worth cutting, and on the files this engine is
/// measured against neither of them is.
///
/// There have to be threads with nothing to do. A file of nine row groups already keeps eight
/// threads busy, so cutting it costs the cutting and buys nothing, and the cut is made exactly fine
/// enough to give every thread a piece rather than as fine as it will go.
///
/// The pages have to be small. A morsel steps over whole pages for free but decodes the page its
/// first row sits in, so a page as long as a row group means a group cut four ways is a group
/// decoded four times. DuckDB writes exactly that, one page per column chunk, and cutting its files
/// anyway is a measured disaster: the suite at one thread went from 2.9 seconds to 5.9 with the scan
/// reading, decompressing and decoding four times the bytes for the same answers.
fn morsel_rows(reader: &FileReader, groups: usize, threads: usize) -> usize {
    let FileReader::Parquet(parquet) = reader else { return 0 };
    if groups == 0 || threads <= groups {
        return 0;
    }
    let page = parquet.page_bytes().unwrap_or(u64::MAX);
    cut_rows(page, parquet.chunk_bytes(), group_rows(parquet, 0), groups, threads)
}

/// The arithmetic of [`morsel_rows`], with the file already asked its questions.
///
/// `page` is what a morsel reads before it reads a row it wants, `whole` what reading the group it
/// is part of costs, and `rows` how many rows that group holds, all over the projected columns.
fn cut_rows(page: u64, whole: u64, rows: usize, groups: usize, threads: usize) -> usize {
    if groups == 0 || threads <= groups || rows == 0 || page == 0 {
        return 0;
    }
    let cut = rows.div_ceil(threads.div_ceil(groups)).max(MORSEL_ROWS);
    let taking = u64::try_from(cut.min(rows)).unwrap_or(u64::MAX);
    let each = whole / u64::try_from(rows).unwrap_or(u64::MAX) * taking;
    if page.saturating_mul(FINE) > each { 0 } else { cut }
}

/// The rows of the next morsel of a row group of `rows` rows, `part` of which are handed out.
///
/// Even pieces rather than full ones and a remainder, because the remainder is the piece everybody
/// else waits for. A group of a hundred and twenty three thousand rows is four morsels of thirty one
/// thousand rather than three of thirty two thousand and one of twenty five.
///
/// An empty range means the group is done, and a group of no rows is done straight away, which is
/// what stops a file with an empty row group in it from being cut forever.
fn next_piece(rows: usize, part: usize, target: usize) -> Range<usize> {
    let each = rows.div_ceil(parts(rows, target));
    let upto = part.saturating_add(each).min(rows);
    part.min(upto)..upto
}

/// How many morsels a row group of `rows` rows is cut into, which is one when `target` is zero.
fn parts(rows: usize, target: usize) -> usize {
    if target == 0 {
        return 1;
    }
    rows.div_ceil(target).max(1)
}

/// How many rows the row group at `at` holds, or none if it is not a group this file has.
fn group_rows(reader: &Reader, at: usize) -> usize {
    reader
        .metadata()
        .row_groups
        .get(at)
        .map_or(0, |group| usize::try_from(group.rows).unwrap_or(usize::MAX))
}

/// Decides how finely the file being cut is worth cutting, and how many morsels that comes to.
///
/// Called when a file is opened and again when the scheduler says how many threads it has, which
/// happens in that order on the first file and the other way round on the rest. Both are cheap: the
/// page size is read once per file and the rest is arithmetic over the footer.
fn aim(cutting: &mut Cutting) {
    let Some(reader) = cutting.reader.as_ref() else { return };
    cutting.cut = morsel_rows(reader, cutting.groups, cutting.threads);
    cutting.gather = gather_rows(reader, cutting.cut, cutting.threads, cutting.asked);
    cutting.pieces = pieces(reader, cutting.cut, cutting.gather);
}

/// How many morsels a whole file comes to, which is what [`Source::morsels`] answers with.
fn pieces(reader: &FileReader, target: usize, gather: usize) -> usize {
    let FileReader::Parquet(reader) = reader else { return 1 };
    let rows: Vec<usize> = reader
        .metadata()
        .row_groups
        .iter()
        .map(|group| usize::try_from(group.rows).unwrap_or(usize::MAX))
        .collect();
    morsels_of(&rows, target, gather)
}

/// How many morsels groups of these sizes come to, cut to `target` rows or gathered up to `gather`.
fn morsels_of(rows: &[usize], target: usize, gather: usize) -> usize {
    if gather == 0 {
        return rows.iter().map(|&group| parts(group, target)).sum::<usize>().max(1);
    }
    let mut at = 0;
    let mut morsels = 0;
    while let Some(&first) = rows.get(at) {
        at += gathered(first, rows[at + 1..].iter().copied(), gather);
        morsels += 1;
    }
    morsels.max(1)
}

/// Where the cutting has got to.
///
/// One file's worth of morsels is cut at a time, and the reader the Parquet splits come off is kept
/// for as long as that file has row groups left to hand out.
#[derive(Debug)]
struct Cutting {
    /// How many files have been opened, which is the next one to open.
    at: usize,
    /// The reader the file being cut is read through, `None` between files.
    reader: Option<FileReader>,
    /// The next row group of that file to hand out, and one past its last.
    group: usize,
    groups: usize,
    /// How many rows of that row group have been handed out already.
    ///
    /// A row group is cut into several morsels when it is large enough to be worth cutting, so the
    /// cutting sits inside a group as well as between them. Zero whenever the next morsel starts a
    /// group, which is every morsel of a file whose groups are small.
    part: usize,
    /// How many rows one morsel of a row group of this file covers, or zero for whole row groups.
    ///
    /// Worked out when the file is opened and again when the scheduler says how many threads it has,
    /// because whether cutting a group pays depends on the page size the writer chose and on there
    /// being a thread with nothing else to do.
    cut: usize,
    /// How many rows a run of small whole row groups is gathered up to, or zero when groups are
    /// being cut rather than gathered. See [`gather_rows`].
    gather: usize,
    /// How many rows the sink asked a morsel to hold, or zero when it asked for nothing, which is
    /// every sink but a load. Kept across files, since it is asked once and every file is aimed.
    asked: usize,
    /// How many threads the scheduler said it would lend, which is one until it says otherwise.
    ///
    /// [`Source::morsels`] is asked before any instance starts and is given the ceiling, so this is
    /// set by the time the cutting matters. One until then, which means no cutting, because a scan
    /// nobody told is a scan that should not be inventing work.
    threads: usize,
    /// How many morsels the whole of the file being cut comes to.
    ///
    /// Worked out once when the file is opened, because [`Source::morsels`] is asked before any of
    /// them is handed out and it is asked to decide how many instances of a pipeline to build.
    pieces: usize,
    /// [`FileScan::tests`] against the column numbers of the file being cut.
    skipping: Vec<Test>,
    /// How many row groups the bounds have ruled out so far, over every file of the scan.
    ///
    /// Nothing downstream needs this. It is here because a pruning that silently stops working
    /// costs time and nothing else, so the tests read it to prove groups are actually being
    /// skipped rather than read and filtered.
    skipped: usize,
    /// The ordinal in that file of the first row of the next morsel, which is what
    /// `file_row_number` counts from and what keeps that column right whatever order the morsels
    /// are read in.
    row: i64,
    /// How many morsels have been handed out, which is the next one's index.
    given: u64,
    /// The CSV file being cut, once it has been cut into ranges, and the next range to hand out.
    ///
    /// The reader it was opened with is inside it, so [`Cutting::reader`] is `None` while it is set.
    split: Option<(Arc<Split>, usize)>,
}

/// What one morsel covers, and the reader open on it.
#[derive(Debug)]
struct Piece {
    /// Which of the scan's paths, so that a column that will not cast names the file it came from.
    file: usize,
    /// The reader, taken away once it has no more chunks in it.
    reader: Option<FileReader>,
    /// What went wrong cutting this morsel, if anything.
    ///
    /// [`Source::morsel`] hands back an `Option` and has nowhere to put an error, and a file that
    /// cannot be opened half way through a scan has to be reported rather than read past. So the
    /// handout queues a morsel that covers nothing and carries the error, and the read reports it.
    failure: Option<Error>,
    /// The ordinal in the file of the next row this morsel produces.
    row: i64,
}

impl<'a> FileScan<'a> {
    /// The rows of a `read_parquet` or `read_csv` call, over every file it names.
    ///
    /// # Errors
    ///
    /// If the first file is gone or unreadable since it was bound, or if it no longer has a column
    /// the plan asked for, which is what a file replaced between binding and running looks like.
    ///
    /// All but the last of these are the plan and the fields of one node of it, and bundling them
    /// into a struct on the way in would only spell the same node a second way.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        plan: &Plan,
        index: u32,
        function: TableFunction,
        args: Slice,
        options: Slice,
        settings: Slice,
        columns: Slice,
        tests: Vec<(usize, Op, Bound)>,
        sideways: Option<Arc<Sideways<'a>>>,
    ) -> Result<Self> {
        let paths = file_arguments(plan, args, function)?;
        let given = csv_options(plan, options, settings)?;
        let produced = plan.field_list(columns).to_vec();
        // The binder puts the counted column last and nothing between here and there reorders a
        // scan's columns, so the flag is whether the last one is it. Pruning can drop it, in which
        // case there is nothing to count, and pruning can drop everything else, in which case the
        // file is opened for its row count and no column of it is read.
        let numbered = produced.last().is_some_and(|field| field.name == FILE_ROW_NUMBER);
        let wanted =
            if numbered { produced[..produced.len() - 1].to_vec() } else { produced.clone() };
        let scan = Self {
            function,
            paths,
            given,
            wanted,
            numbered,
            schema: Schema::numbered(produced, index),
            tests,
            cutting: Mutex::new(Cutting {
                at: 0,
                reader: None,
                group: 0,
                groups: 0,
                part: 0,
                cut: 0,
                gather: 0,
                asked: 0,
                threads: 1,
                pieces: 1,
                skipping: Vec::new(),
                skipped: 0,
                row: 0,
                given: 0,
                split: None,
            }),
            open: Mutex::new(HashMap::new()),
            counters: None,
            sideways,
            index,
            paying: Paying::default(),
        };
        // The first file is opened now rather than on the first read, so that a file that has gone
        // missing since binding is reported where a caller is still asking a question about this
        // scan rather than in the middle of a result.
        {
            let mut cutting = scan.cutting.lock().map_err(poisoned)?;
            scan.advance(&mut cutting)?;
        }
        Ok(scan)
    }

    /// Drops the rows of one chunk that a join above this scan cannot hold a match for.
    ///
    /// The same test [`Scan::sift`] makes and the same argument for it, applied where the rows come
    /// out of a file rather than out of a stored table. One hash of one column and one cache line
    /// touched per row, against a filter holding the keys the build side turned out to have.
    ///
    /// Nothing at all for a scan with no join above it, which is two loads and a branch per chunk,
    /// and nothing either once [`Paying`] has decided the filter is not turning enough rows away to
    /// be worth hashing for.
    ///
    /// # Errors
    ///
    /// Whatever narrowing the chunk to the rows that survived raises.
    fn sift(&self, chunk: &mut Chunk) -> Result<()> {
        let Some(sideways) = self.sideways.as_ref() else { return Ok(()) };
        if !self.paying.worth() {
            return Ok(());
        }
        // Keys close together come as a bitmap in place of the filter, see `Found::domain`.
        if let Some((at, domain)) = sideways.domain(self.index) {
            let Ok(column) = chunk.column(at) else { return Ok(()) };
            let rows = chunk.len();
            let kept = domain.keep(column, rows, &mut Vec::new());
            self.paying.saw(rows, kept.len());
            if kept.len() < rows {
                let whole = std::mem::replace(chunk, Chunk::empty(&[]));
                *chunk = whole.select(&Selection::from_indices(kept))?;
            }
            return Ok(());
        }
        let Some((at, filter)) = sideways.sifting(self.index) else { return Ok(()) };
        let Ok(column) = chunk.column(at) else { return Ok(()) };
        let rows = chunk.len();
        let mut hashes = Vec::new();
        hash(std::slice::from_ref(column), rows, &mut hashes, Across::TwoInputs);
        // The whole chunk asked at once rather than a row at a time inside the selection, because
        // the filter is larger than the cache and a row of it is a trip to memory the core can only
        // overlap with the next row's if nothing in between branches on the answer.
        let mut held = Vec::new();
        filter.holds_run(&hashes, &mut held);
        let kept = Selection::from_predicate(rows, |row| held[row]);
        self.paying.saw(rows, kept.len());
        if kept.len() == rows {
            return Ok(());
        }
        let whole = std::mem::replace(chunk, Chunk::empty(&[]));
        *chunk = whole.select(&kept)?;
        Ok(())
    }

    /// Connects this source's file counters to the operator row that owns it.
    pub(crate) fn watched(mut self, counters: Arc<Counters>) -> Self {
        self.counters = Some(counters);
        self
    }

    /// What this scan produces, in the types the first file settled on.
    pub(crate) fn schema(&self) -> &Schema {
        &self.schema
    }

    /// Opens the next file and projects it, or leaves the reader empty at the end of the list.
    fn advance(&self, cutting: &mut Cutting) -> Result<()> {
        cutting.reader = None;
        cutting.split = None;
        cutting.row = 0;
        cutting.group = 0;
        cutting.groups = 0;
        cutting.part = 0;
        cutting.cut = 0;
        cutting.gather = 0;
        cutting.pieces = 1;
        cutting.skipping = Vec::new();
        let Some(path) = self.paths.get(cutting.at) else { return Ok(()) };
        let mut reader = FileReader::open(self.function, path, self.given.clone())?;
        let first = if cutting.at == 0 { None } else { self.paths.first().map(String::as_str) };
        let held = positions(self.function, &self.wanted, &reader.fields(), path, first)?;
        reader.project(&held)?;
        reader.settle(&self.wanted)?;
        cutting.groups = reader.row_groups();
        cutting.skipping = self
            .tests
            .iter()
            .filter_map(|(at, op, value)| {
                Some(Test { column: *held.get(*at)?, op: *op, value: value.clone() })
            })
            .collect();
        cutting.reader = Some(reader);
        cutting.at += 1;
        aim(cutting);
        self.divide(cutting);
        Ok(())
    }

    /// Cuts the CSV file being read into ranges, when it is long enough and there are threads to
    /// read them on.
    ///
    /// Not when the query wants `file_row_number`, since a range has no way to know how many rows
    /// came before it short of reading them, and the number is the one thing the column is for.
    fn divide(&self, cutting: &mut Cutting) {
        if cutting.threads <= 1 || self.numbered {
            return;
        }
        let size = rudb_csv::split::size();
        match cutting.reader.take() {
            Some(FileReader::Csv(reader)) if reader.ranges(size) > 1 => {
                let ranges = reader.ranges(size);
                let split = Split::new(reader, ranges);
                cutting.pieces = split.ranges();
                cutting.split = Some((Arc::new(split), 0));
            }
            other => cutting.reader = other,
        }
    }

    /// The next morsel's worth of the file being cut, or `None` when that file has none left.
    ///
    /// A Parquet file gives one per row group and takes a split of the reader it was opened with. A
    /// CSV file that was cut into ranges gives one per range, and one that was not gives one, which
    /// takes the reader itself.
    fn cut(&self, cutting: &mut Cutting) -> Result<Option<Piece>> {
        let file = cutting.at.saturating_sub(1);
        if let Some((split, next)) = cutting.split.as_mut() {
            if *next == split.ranges() {
                return Ok(None);
            }
            let part = split.part(*next);
            *next += 1;
            return Ok(Some(Piece {
                file,
                reader: Some(FileReader::Part(part)),
                failure: None,
                row: 0,
            }));
        }
        if let Some(FileReader::Parquet(reader)) = cutting.reader.as_ref() {
            // Row groups the filter above this scan has already ruled out are stepped over here
            // rather than handed out and thrown away downstream, which is the whole point: the data
            // pages of a skipped group are never read, never decompressed and never decoded. The row
            // counter still moves, because a later group's rows keep the numbers the file gives them.
            let metadata = reader.metadata();
            while cutting.group < cutting.groups {
                let Some(group) = metadata.row_groups.get(cutting.group) else { break };
                if !skips(&cutting.skipping, group, &metadata.schema) {
                    break;
                }
                cutting.group += 1;
                cutting.row = cutting.row.saturating_add(group.rows);
                cutting.skipped = cutting.skipped.saturating_add(1);
                if let Some(counters) = &self.counters {
                    counters.part_pruned();
                }
            }
        }
        let piece = match cutting.reader.as_ref() {
            Some(FileReader::Parquet(reader)) if cutting.group < cutting.groups => {
                let at = cutting.group;
                let rows = group_rows(reader, at);
                // A run of small groups goes out as one morsel. It stops at a group the filter
                // rules out, which the loop above steps over when the next morsel is cut.
                let taken = if cutting.part == 0 && cutting.gather > rows {
                    let metadata = reader.metadata();
                    let later = metadata.row_groups.get(at + 1..cutting.groups).unwrap_or(&[]);
                    let later = later
                        .iter()
                        .take_while(|group| !skips(&cutting.skipping, group, &metadata.schema))
                        .map(|group| usize::try_from(group.rows).unwrap_or(usize::MAX));
                    gathered(rows, later, cutting.gather)
                } else {
                    1
                };
                if taken > 1 {
                    let covered = (at..at + taken)
                        .map(|group| group_rows(reader, group))
                        .fold(0_usize, usize::saturating_add);
                    if let Some(counters) = &self.counters {
                        for _ in 0..taken {
                            counters.part_read();
                        }
                    }
                    let split = reader.split(at..at + taken)?;
                    cutting.group = at + taken;
                    let row = cutting.row;
                    cutting.row =
                        cutting.row.saturating_add(i64::try_from(covered).unwrap_or(i64::MAX));
                    return Ok(Some(Piece {
                        file,
                        reader: Some(FileReader::Parquet(split)),
                        failure: None,
                        row,
                    }));
                }
                let piece = next_piece(rows, cutting.part, cutting.cut);
                // Once per row group and not once per piece, so that the two counts on the
                // operator row are both in row groups and a reader can add them up.
                if piece.start == 0
                    && let Some(counters) = &self.counters
                {
                    counters.part_read();
                }
                let split = reader.split_rows(at, piece.clone())?;
                if piece.end >= rows {
                    cutting.group += 1;
                    cutting.part = 0;
                } else {
                    cutting.part = piece.end;
                }
                let row = cutting.row;
                cutting.row = cutting
                    .row
                    .saturating_add(i64::try_from(piece.end - piece.start).unwrap_or(i64::MAX));
                Piece { file, reader: Some(FileReader::Parquet(split)), failure: None, row }
            }
            Some(FileReader::Csv(_)) => {
                Piece { file, reader: cutting.reader.take(), failure: None, row: 0 }
            }
            // Either the row groups of the file being cut have all been handed out, or there is no
            // file being cut at all, and both mean the same thing to the caller.
            _ => return Ok(None),
        };
        Ok(Some(piece))
    }

    /// Registers a morsel's worth of work and hands back the morsel that covers it.
    fn hand(&self, cutting: &mut Cutting, piece: Piece) -> Option<Morsel> {
        let index = cutting.given;
        cutting.given += 1;
        let covers = u64::from(piece.failure.is_none());
        self.open.lock().ok()?.insert(index, Arc::new(Mutex::new(piece)));
        Some(Morsel::new(index, 0, covers))
    }

    /// What the morsel of this index covers.
    fn piece(&self, index: u64) -> Result<Arc<Mutex<Piece>>> {
        let open = self.open.lock().map_err(poisoned)?;
        open.get(&index)
            .map(Arc::clone)
            .ok_or_else(|| Error::internal(format!("a file scan was read at morsel {index}")))
    }

    /// The chunk with the row number column on the end of it.
    ///
    /// Built rather than read, because no file holds it. The values are a run, and the reason this
    /// is a loop over a range rather than a sequence vector is that the scan's consumer is free to
    /// slice or gather the chunk and a flat column survives both without a case.
    fn number(&self, chunk: Chunk, piece: &mut Piece) -> Result<Chunk> {
        let rows = chunk.len();
        let mut columns = Vec::with_capacity(chunk.width() + 1);
        for at in 0..chunk.width() {
            columns.push(chunk.column(at)?.clone());
        }
        let first = piece.row;
        piece.row = piece.row.saturating_add(i64::try_from(rows).unwrap_or(i64::MAX));
        let mut data = Vec::with_capacity(rows);
        for at in 0..rows {
            data.push(first.saturating_add(i64::try_from(at).unwrap_or(i64::MAX)));
        }
        columns.push(Vector::flat(LogicalType::BigInt, Data::Int64(data.into()))?);
        Chunk::with_rows(columns, rows)
    }

    /// The chunk with every column in the type the first file gave it.
    ///
    /// Almost always nothing, because almost always every file has the same schema, and the check is
    /// a type comparison per column per chunk rather than per row.
    ///
    /// `file` is which of the scan's paths this chunk came out of, and the only thing it is for is
    /// naming that file if a column will not cast.
    fn conform(&self, chunk: Chunk, file: usize) -> Result<Chunk> {
        let rows = chunk.len();
        let settled = self.wanted.iter().enumerate().all(|(at, field)| {
            chunk.column(at).is_ok_and(|column| column.logical_type() == &field.ty)
        });
        if settled {
            return Ok(chunk);
        }
        let mut columns = Vec::with_capacity(self.wanted.len());
        for (at, field) in self.wanted.iter().enumerate() {
            let column = chunk.column(at)?;
            if column.logical_type() == &field.ty {
                columns.push(column.clone());
                continue;
            }
            columns.push(cast(column, &field.ty, false).map_err(|error| {
                let path = self.paths.get(file).map_or("", String::as_str);
                Error::conversion(format!(
                    "Error while reading file \"{path}\": failed to cast column \"{}\" from type \
                     {} to {}: {}",
                    field.name,
                    column.logical_type(),
                    field.ty,
                    error.message()
                ))
            })?);
        }
        Chunk::with_rows(columns, rows)
    }
}

impl Source for FileScan<'_> {
    fn morsel(&self) -> Option<Morsel> {
        let mut cutting = self.cutting.lock().ok()?;
        loop {
            match self.cut(&mut cutting) {
                Ok(Some(piece)) => return self.hand(&mut cutting, piece),
                Ok(None) => {}
                Err(error) => {
                    let file = cutting.at.saturating_sub(1);
                    let piece = Piece { file, reader: None, failure: Some(error), row: 0 };
                    return self.hand(&mut cutting, piece);
                }
            }
            if cutting.at >= self.paths.len() {
                return None;
            }
            if let Err(error) = self.advance(&mut cutting) {
                let file = cutting.at.saturating_sub(1);
                let piece = Piece { file, reader: None, failure: Some(error), row: 0 };
                return self.hand(&mut cutting, piece);
            }
        }
    }

    /// The first file's morsels, taken as what every file in the list looks like.
    ///
    /// The first file is already open, because opening it is how a scan reports a file that has
    /// gone missing since it was bound, so what its row groups come to is there to be read without
    /// opening anything. The rest are assumed to match, which is right for a directory written by
    /// one writer and is the case worth being right about. A CSV file is cut into ranges here, now
    /// that the number of threads is known, so a list of CSV files is as many morsels as the first
    /// file has ranges for every file.
    fn morsels(&self, threads: usize, _weight: usize) -> Option<usize> {
        let mut cutting = self.cutting.lock().ok()?;
        cutting.threads = threads;
        aim(&mut cutting);
        self.divide(&mut cutting);
        Some(cutting.pieces.max(1).saturating_mul(self.paths.len().max(1)))
    }

    /// Remembered for [`aim`], which [`Source::morsels`] calls next.
    fn gather(&self, rows: usize) {
        if let Ok(mut cutting) = self.cutting.lock() {
            cutting.asked = rows;
        }
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        let piece = self.piece(morsel.index())?;
        let mut piece = piece.lock().map_err(poisoned)?;
        if let Some(error) = piece.failure.take() {
            return Err(error);
        }
        loop {
            let file = piece.file;
            let Some(reader) = piece.reader.as_mut() else {
                morsel.advance(1);
                *out = Chunk::empty(&self.schema.types());
                return Ok(Progress::Done);
            };
            let before = reader.bytes_read();
            let next = reader.next_chunk()?;
            if let Some(counters) = &self.counters {
                counters.read(reader.bytes_read().saturating_sub(before));
            }
            let Some(chunk) = next else {
                // A file that is empty gives no chunk rather than an empty one, so this is not the
                // place that skips it. Taking the reader away and going round again is.
                piece.reader = None;
                continue;
            };
            let mut chunk = self.conform(chunk, file)?;
            if self.numbered {
                chunk = self.number(chunk, &mut piece)?;
            }
            // After the row numbers rather than before them, because the number a row carries is
            // its ordinal in the file and dropping rows first would renumber the ones that are
            // left.
            self.sift(&mut chunk)?;
            *out = chunk;
            return Ok(Progress::More);
        }
    }
}

/// One open file, whichever of the two readers it needed.
///
/// An enum rather than a trait because there are two of them and they are both in this workspace.
/// What is behind the two is not alike at all, which is the reason the enum is here rather than the
/// readers being made to look the same: a Parquet file states its schema and stores each column
/// apart, so reading two of a hundred and five is reading two stretches of the file, while a CSV
/// file states nothing and interleaves everything, so every byte is parsed whatever the projection
/// is and the projection only saves the conversion and the copy. The three calls they do share are
/// exactly the three the scan above needs.
///
/// A range of a CSV file cut up by [`FileScan::divide`] is a third kind, which only a morsel holds.
/// It was projected and settled as the whole file before it was cut, so it is only ever asked for
/// chunks.
#[derive(Debug)]
enum FileReader {
    Parquet(Reader),
    Csv(CsvReader),
    Part(Part),
}

impl FileReader {
    /// Opens `path` with the reader `function` names.
    fn open(function: TableFunction, path: &str, given: Given) -> Result<Self> {
        match function {
            TableFunction::ReadCsv => Ok(Self::Csv(open_csv(path, given)?)),
            _ => Ok(Self::Parquet(open_parquet(path)?)),
        }
    }

    /// The columns the file holds, in the order it holds them.
    fn fields(&self) -> Vec<Field> {
        match self {
            Self::Parquet(reader) => reader.fields(),
            Self::Csv(reader) => reader.fields(),
            Self::Part(_) => Vec::new(),
        }
    }

    /// Reads only these columns, by position in the file, in this order.
    fn project(&mut self, columns: &[usize]) -> Result<()> {
        match self {
            Self::Parquet(reader) => reader.project(columns),
            Self::Csv(reader) => reader.project(columns),
            Self::Part(_) => Err(cut_already()),
        }
    }

    /// Tells the file the types the whole read settled on, where that is a thing to say.
    ///
    /// It is one thing for Parquet and everything for CSV. A Parquet file states its types and the
    /// first file's are the read's, so a later file that disagrees is read as what it holds and cast
    /// by [`FileScan::conform`]. The exception is a byte array column the plan wants as text, which
    /// is `binary_as_string` arriving as the answer it produced rather than as a flag of its own,
    /// and which is a rename rather than a conversion. A CSV file has no types of its own, only the
    /// ones a sample of it suggested, and the read's came from combining the samples of every file,
    /// so this replaces the suggestion before a row is parsed rather than converting twice.
    fn settle(&mut self, wanted: &[Field]) -> Result<()> {
        match self {
            Self::Parquet(reader) => {
                let text: Vec<bool> =
                    wanted.iter().map(|field| field.ty == LogicalType::Varchar).collect();
                reader.as_string(&text);
                Ok(())
            }
            Self::Csv(reader) => {
                let types: Vec<LogicalType> = wanted.iter().map(|field| field.ty.clone()).collect();
                reader.retype(&types)
            }
            Self::Part(_) => Err(cut_already()),
        }
    }

    /// The next chunk, or `None` at the end of the file.
    fn next_chunk(&mut self) -> Result<Option<Chunk>> {
        match self {
            Self::Parquet(reader) => reader.next_chunk(),
            Self::Csv(reader) => reader.next_chunk(),
            Self::Part(part) => part.next_chunk(),
        }
    }

    /// How many row groups the file has, which is how many morsels it is worth.
    ///
    /// Zero for CSV, which has none, and whose morsels are its ranges.
    fn row_groups(&self) -> usize {
        match self {
            Self::Parquet(reader) => reader.metadata().row_groups.len(),
            Self::Csv(_) | Self::Part(_) => 0,
        }
    }

    /// Bytes of the file read so far, which for Parquet is the compressed column bytes.
    fn bytes_read(&self) -> u64 {
        match self {
            Self::Parquet(reader) => reader.bytes_read(),
            Self::Csv(reader) => reader.bytes_read(),
            Self::Part(part) => part.bytes_read(),
        }
    }
}

/// What asking a range of a CSV file to be something other than read comes to, which is a scan
/// that cut a file before it finished opening it.
fn cut_already() -> Error {
    Error::internal("a range of a CSV file was reshaped after the file was cut")
}

/// What the call's named parameters said about how the CSV files are written.
///
/// The binder worked this out to sniff the files with and wrote the names and the values into the
/// plan, and this works it out again from them to read the files with. Both go through
/// [`csv_given`], so a file is read the way it was sniffed and the columns a query was planned
/// against are the columns it reads. A `read_parquet` call has none of these and gets the default,
/// which says nothing and is never asked.
fn csv_options(plan: &Plan, options: Slice, settings: Slice) -> Result<Given> {
    if options.len == 0 {
        return Ok(Given::default());
    }
    let exprs: Vec<ExprRef> = plan.expr_list(settings).to_vec();
    let source = Schema::empty();
    let one = Chunk::with_rows(Vec::new(), 1)?;
    let evaluated = evaluate_all(plan, &exprs, &source, &one)?;
    let names: Vec<&str> = plan.name_list(options).iter().map(|name| plan.string(*name)).collect();
    let written: Vec<(&str, Value)> =
        names.into_iter().zip(evaluated.iter().map(|vector| vector.value_at(0))).collect();
    csv_given(&written)
}

/// The file names a file reading table function was called with.
///
/// The binder already refused anything that is not a constant string and already expanded whatever
/// patterns there were, so a failure here is a plan that was built wrong rather than a statement
/// somebody wrote wrong, and it says so.
pub(crate) fn file_arguments(
    plan: &Plan,
    args: Slice,
    function: TableFunction,
) -> Result<Vec<String>> {
    let exprs: Vec<ExprRef> = plan.expr_list(args).to_vec();
    let source = Schema::empty();
    let one = Chunk::with_rows(Vec::new(), 1)?;
    let evaluated = evaluate_all(plan, &exprs, &source, &one)?;
    let mut paths = Vec::with_capacity(evaluated.len());
    for vector in &evaluated {
        match vector.value_at(0) {
            Value::Varchar(path) => paths.push(path),
            other => {
                return Err(Error::internal(format!(
                    "{}() bound with {other:?} rather than constant file names",
                    function.name()
                )));
            }
        }
    }
    Ok(paths)
}

/// Where in `held` each of `wanted` is, by name.
///
/// `first` is the file the schema came from, and is `None` when `path` is that file. The two say
/// different things about a missing column and DuckDB writes both: the first file is the one the
/// plan was bound against, so a column missing from it means the file was replaced since, while a
/// column missing from a later one means the files in the set do not agree with each other.
///
/// The disagreement is worded by whichever reader found it, because the two readers in DuckDB are
/// two pieces of code that each wrote their own sentence and a compatibility test that compares
/// output compares all of it. For CSV this is only reachable when a file changed between binding and
/// running, since the binder sniffed every file and would have said the same thing first.
pub(crate) fn positions(
    function: TableFunction,
    wanted: &[Field],
    held: &[Field],
    path: &str,
    first: Option<&str>,
) -> Result<Vec<usize>> {
    let mut positions = Vec::with_capacity(wanted.len());
    for field in wanted {
        let at = held.iter().position(|column| column.name == field.name).ok_or_else(|| {
            let Some(first) = first else {
                return Error::io(format!(
                    "File \"{path}\" does not have a column named \"{}\"",
                    field.name
                ));
            };
            if matches!(function, TableFunction::ReadCsv) {
                return rudb_csv::mismatch(first, path, &field.name);
            }
            let candidates: Vec<&str> = held.iter().map(|column| column.name.as_str()).collect();
            Error::invalid_input(format!(
                "Failed to read file \"{path}\": schema mismatch in glob: column \"{}\" was read \
                 from the original file \"{first}\", but could not be found in file \
                 \"{path}\".\nCandidate names: {}\nIf you are trying to read files with different \
                 schemas, try setting union_by_name=True",
                field.name,
                candidates.join(", ")
            ))
        })?;
        positions.push(at);
    }
    Ok(positions)
}

impl Source for Values {
    fn morsel(&self) -> Option<Morsel> {
        self.handout.take()
    }

    fn morsels(&self, _threads: usize, _weight: usize) -> Option<usize> {
        Some(self.handout.total())
    }

    fn read(&self, morsel: &mut Morsel, out: &mut Chunk) -> Result<Progress> {
        *out = match self.chunks.get(position(morsel)) {
            Some(chunk) => chunk.clone(),
            None => Chunk::empty(&self.schema.types()),
        };
        morsel.advance(1);
        Ok(Progress::Done)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use rudb_catalog::{QualifiedName, Table};
    use rudb_common::{Field, LogicalType, Value};
    use rudb_functions::TableFunction;
    use rudb_metrics::Counters;
    use rudb_pipeline::{Progress, Source};
    use rudb_plan::{ColumnBinding, Node, Plan};
    use rudb_storage::Blocked;
    use rudb_vector::{Chunk, Data, Vector};

    use super::{
        Across, Bound, Cutoff, FileScan, Filters, Handout, Live, OnceLock, Op, Paying, Probe,
        Pushdown, RUN, Scan, Schema, Series, Session, Settings, Sideways, VECTOR_SIZE, WARMUP,
        best_first, cut_rows, gather_target, gathered, hash, instances_for, morsels_of, next_piece,
        parts, runs_of, worth_sifting,
    };
    use crate::sideways::Found;

    /// Every morsel a row group of `rows` rows is cut into, by asking for them the way `cut` does.
    fn cutting(rows: usize, target: usize) -> Vec<std::ops::Range<usize>> {
        let mut out = Vec::new();
        let mut part = 0;
        loop {
            let piece = next_piece(rows, part, target);
            if piece.is_empty() {
                return out;
            }
            part = piece.end;
            out.push(piece);
            assert!(out.len() <= rows + 1, "cutting {rows} rows into {target} did not terminate");
        }
    }

    /// A series without going through a plan, which is what `Series::new` is for.
    fn series(start: i64, step: i64, rows: u64) -> Series {
        Series { start, step, rows, ..Series::empty(Schema::empty()) }
    }

    /// Every value the series produces, and how many morsels it took to produce them.
    fn drained(series: &Series) -> (Vec<i64>, usize) {
        let mut values = Vec::new();
        let mut morsels = 0;
        while let Some(mut morsel) = series.morsel() {
            morsels += 1;
            loop {
                let mut chunk = Chunk::empty(&[LogicalType::BigInt]);
                let progress = series.read(&mut morsel, &mut chunk).expect("a series reads");
                for row in 0..chunk.len() {
                    match chunk.value_at(row, 0) {
                        Value::BigInt(value) => values.push(value),
                        other => panic!("a series produced {other}"),
                    }
                }
                if progress == Progress::Done {
                    break;
                }
            }
        }
        (values, morsels)
    }

    /// A morsel holds more than a chunk, so reading one is several calls, and the last of them is
    /// the one that says the morsel is done.
    #[test]
    fn a_morsel_of_a_series_is_read_a_chunk_at_a_time() {
        let rows = VECTOR_SIZE as u64 * 2 + 5;
        let (values, morsels) = drained(&series(0, 1, rows));

        assert_eq!(morsels, 1);
        assert_eq!(values.len(), rows as usize);
        assert_eq!(values[0], 0);
        assert_eq!(values[values.len() - 1], rows as i64 - 1);
    }

    /// The value at a position is worked out from the position rather than counted up to, because
    /// the thread that gets the second morsel never saw the first one.
    #[test]
    fn a_series_longer_than_a_morsel_carries_on_where_the_last_one_stopped() {
        let rows = RUN + 3;
        let (values, morsels) = drained(&series(10, 3, rows));

        assert_eq!(morsels, 2);
        assert_eq!(values.len(), rows as usize);
        assert_eq!(values[0], 10);
        assert_eq!(values[RUN as usize], 10 + 3 * RUN as i64);
        assert_eq!(values[values.len() - 1], 10 + 3 * (rows as i64 - 1));
    }

    /// Nothing to produce is no morsels at all, rather than one morsel that produces nothing, which
    /// is what a null argument to `range()` means.
    #[test]
    fn a_series_of_nothing_hands_out_no_work() {
        let (values, morsels) = drained(&series(0, 1, 0));

        assert!(values.is_empty());
        assert_eq!(morsels, 0);
    }

    /// The whole of what makes a source shareable: a position goes to one caller and the next
    /// caller gets the next one, so two threads scanning a table read different chunks of it.
    #[test]
    fn a_handout_gives_each_position_to_one_caller_and_then_stops() {
        let handout = Handout::new(3);

        let taken: Vec<u64> = (0..3).map(|_| handout.take().expect("a position").start()).collect();

        assert_eq!(taken, [0, 1, 2]);
        assert!(handout.take().is_none());
        assert!(handout.take().is_none());
    }

    /// A native file of `parts` parts of `rows` rows each, and the catalog table over it.
    ///
    /// The file is written rather than faked because the thing under test is what the scan does
    /// with the stripes the format puts the parts in, and only the format knows where those are.
    fn native(label: &str, parts: usize, rows: usize) -> (Table, std::path::PathBuf) {
        let stamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("time advances")
            .as_nanos();
        let path = std::env::temp_dir()
            .join(format!("rudb-exec-{label}-{}-{stamp}.rdb", std::process::id()));
        let mut writer = rudb_native::Writer::create(
            &path,
            "items",
            vec![Field::required("id", LogicalType::Integer)],
        )
        .expect("a new file");
        for part in 0..parts {
            let values: Vec<Value> =
                (0..rows).map(|row| Value::Integer((part * rows + row) as i32)).collect();
            let chunk = Chunk::new(vec![
                Vector::from_values(LogicalType::Integer, &values).expect("integers"),
            ])
            .expect("matching rows");
            writer.append(&chunk).expect("one part");
        }
        writer.finish().expect("commit");
        let reader = rudb_native::Reader::open(&path).expect("reopen from disk");
        let table = Table::native(QualifiedName::new("memory", "main", "items"), reader)
            .expect("a native table");
        (table, path)
    }

    /// A scan of a table, built the way the builder builds one.
    fn scan_of(table: &Table) -> Scan<'_> {
        let plan = Plan::parse("Get memory.main.items AS items #0 [id::INTEGER]")
            .expect("the plan text round trips");
        let Node::Get { index, columns, .. } = *plan.node(plan.root()) else {
            panic!("the plan is a get");
        };
        Scan::new(
            &plan,
            table,
            index,
            columns,
            Filters::default(),
            &Settings::default(),
            &Session::default(),
        )
        .expect("the column is there")
    }

    /// Every morsel the scan hands out, as the parts it covers and the rows that came out of it.
    fn taken(scan: &Scan<'_>) -> Vec<(std::ops::Range<u64>, usize)> {
        let mut out = Vec::new();
        while let Some(mut morsel) = scan.morsel() {
            let mut rows = 0;
            loop {
                let mut chunk = Chunk::empty(&[]);
                let progress = scan.read(&mut morsel, &mut chunk).expect("a part reads");
                rows += chunk.len();
                if progress == Progress::Done {
                    break;
                }
            }
            out.push((morsel.start()..morsel.end(), rows));
        }
        out
    }

    /// A whole stripe per morsel, once there are at least as many stripes as workers.
    ///
    /// This is what stops the workers racing for a page. Handing out parts puts every worker in the
    /// same stripe at once, one of them reads the page and the rest read their own part out of the
    /// same bytes, so a quarter of what a cold column moves is moved twice and the rest moves in
    /// reads too small to keep the disk busy. A morsel that is a whole stripe gives the page to one
    /// worker and there is nothing left to race for.
    #[test]
    fn a_native_scan_with_a_stripe_for_every_worker_hands_out_stripes() {
        let (table, path) = native("stripes", 128, 200);
        let scan = scan_of(&table);
        let stripes = table.rows().stripe_parts();
        assert_eq!(stripes.len(), 2, "two full stripes of sixty four parts");

        assert_eq!(scan.morsels(2, 1), Some(2), "one instance per stripe");
        let taken = taken(&scan);

        assert_eq!(taken.len(), 2, "one morsel per stripe");
        assert_eq!(taken[0].0, 0..64);
        assert_eq!(taken[1].0, 64..128);
        assert_eq!(taken.iter().map(|(_, rows)| rows).sum::<usize>(), 128 * 200);
        std::fs::remove_file(path).expect("remove scratch file");
    }

    /// A table with fewer stripes than workers keeps handing out parts.
    ///
    /// A stripe per worker would leave every worker but one with nothing at all, and the duplicate
    /// reads it saves are worth less than the rest of the machine.
    #[test]
    fn a_native_scan_with_one_stripe_hands_out_parts() {
        let (table, path) = native("one-stripe", 64, 400);
        let scan = scan_of(&table);
        assert_eq!(table.rows().stripe_parts().len(), 1, "one stripe and more workers than that");

        assert_eq!(scan.morsels(4, 1), Some(2), "the rows behind it pay for two instances");
        let taken = taken(&scan);

        assert_eq!(taken.len(), 64, "one morsel per part");
        assert_eq!(taken[0].0, 0..1);
        assert_eq!(taken.iter().map(|(_, rows)| rows).sum::<usize>(), 64 * 400);
        std::fs::remove_file(path).expect("remove scratch file");
    }

    /// A scan of a native table with bounds tests on its only column, the way a filter compiles one.
    fn pruned_scan(table: &Table, tests: Vec<(usize, Op, Bound)>) -> Scan<'_> {
        let plan = Plan::parse("Get memory.main.items AS items #0 [id::INTEGER]")
            .expect("the plan text round trips");
        let Node::Get { index, columns, .. } = *plan.node(plan.root()) else {
            panic!("the plan is a get");
        };
        Scan::new(
            &plan,
            table,
            index,
            columns,
            Filters { pruning: tests, ..Filters::default() },
            &Settings::default(),
            &Session::default(),
        )
        .expect("the column is there")
    }

    /// A predicate that leaves one stripe of two still divides the work between two workers.
    ///
    /// Two things are being checked and the second is the one that matters. The instance count comes
    /// from the rows the zone maps leave rather than from the rows the table holds, so a scan that
    /// will read half a file asks for what half a file pays for. And the stripe that has work in it
    /// is cut up, because a morsel per stripe would have given one worker everything and the other
    /// nothing, which is how ClickBench 39 came to be faster on one thread than on sixteen.
    #[test]
    fn a_native_scan_divides_the_stripe_that_survives_pruning() {
        let (table, path) = native("pruned", 128, 400);
        assert_eq!(table.rows().stripe_parts().len(), 2, "two full stripes of sixty four parts");
        let scan = pruned_scan(&table, vec![(0, Op::GreaterOrEqual, Bound::Int(25_600))]);

        assert_eq!(scan.morsels(2, 1), Some(2), "the surviving half pays for two instances");
        let taken = taken(&scan);

        assert!(taken.len() > 2, "the one stripe with work is cut up, not handed out whole");
        assert!(
            taken.iter().all(|(run, _)| run.start >= 64),
            "no morsel covers the ruled out stripe"
        );
        assert_eq!(taken.iter().map(|(_, rows)| rows).sum::<usize>(), 64 * 400);
        std::fs::remove_file(path).expect("remove scratch file");
    }

    /// The rule that decides whether to read the sieves before starting anybody.
    ///
    /// The case in the middle is ClickBench 19, an equality on an identifier over the whole file. No
    /// stripe bound rules anything out there, so all sixteen stripes come out of the first pass with
    /// work in them and there is nothing left for a better instance count to fix. Reading the sieves
    /// anyway would find that only nine parts matter, on one thread, while sixteen workers that were
    /// about to find the same thing at the same time waited for it, and that cost the query a third
    /// of its time.
    #[test]
    fn the_sieves_are_read_early_only_when_the_bounds_have_left_the_work_in_a_heap() {
        assert!(worth_sifting(2, 32, 1_000_000, 1), "two stripes of work and a machine to fill");
        assert!(
            !worth_sifting(16, 32, 1_000_000, 1),
            "sixteen is as many instances as the rows buy"
        );
        assert!(!worth_sifting(1, 1, 1_000_000, 1), "one worker has nowhere to spread it anyway");
        assert!(!worth_sifting(1, 32, 1_000, 1), "a thousand rows pay for one worker either way");
        assert!(worth_sifting(1, 32, 1_000, 64), "a thousand rows of heavy work pay for more");
    }

    /// The live parts of each stripe, with the rows they hold worked out the way `bounded` does.
    fn living(stripes: &[&[usize]], rows: impl Fn(usize) -> usize) -> Vec<Live> {
        stripes
            .iter()
            .map(|parts| Live {
                parts: parts.to_vec(),
                rows: parts.iter().map(|&at| rows(at)).sum(),
            })
            .collect()
    }

    /// The cutting rule on its own. A stripe holding no more than a share stays one morsel while
    /// there are as many stripes with work in them as there are workers, and once there are fewer the
    /// stripes are shared anyway and the cut gets finer.
    #[test]
    fn a_stripe_holding_no_more_than_a_share_stays_one_run() {
        let live = living(&[&[0, 1, 2, 3], &[4, 5, 6, 7], &[8, 9, 10, 11]], |_| 1);

        assert_eq!(runs_of(&live, 1, |_| 1), [0..4, 4..8, 8..12], "one worker, one run a stripe");
        assert_eq!(runs_of(&live, 3, |_| 1), [0..4, 4..8, 8..12], "a stripe is a share exactly");
        let four = runs_of(&live, 4, |_| 1);
        assert_eq!(four.len(), 12, "three stripes for four workers, so a part apiece");
        assert_eq!(four.first(), Some(&(0..1)));
        assert_eq!(four.last(), Some(&(11..12)));
    }

    /// The reason the cut is by rows. Three stripes with work in them are not three pieces of work
    /// when the statistics left one of them holding almost all of it, and the old rule handed that
    /// one to a single worker while the other two finished at once.
    #[test]
    fn the_stripe_holding_the_rows_is_the_one_that_gets_cut() {
        let rows = |at: usize| if (1..9).contains(&at) { 1_000 } else { 10 };
        let live = living(&[&[0], &[1, 2, 3, 4, 5, 6, 7, 8], &[9]], rows);

        let runs = runs_of(&live, 2, rows);

        assert_eq!(runs, [0..1, 1..3, 3..5, 5..7, 7..9, 9..10], "the big stripe in four, not one");
        let held: Vec<usize> = runs.iter().map(|run| run.clone().map(rows).sum()).collect();
        assert_eq!(held, [10, 2_000, 2_000, 2_000, 2_000, 10], "and the four are the same size");
    }

    /// And the other half of it. One stripe holding all the work is cut into runs of its live parts,
    /// and the parts the zone maps ruled out are not in any of them except where they sit between
    /// two that survived, which a morsel walks past without reading.
    #[test]
    fn a_stripe_that_holds_all_the_work_is_cut_up() {
        let live = living(&[&[1, 3, 5, 7], &[]], |_| 1);

        let runs = runs_of(&live, 2, |_| 1);

        assert_eq!(runs, [1..2, 3..4, 5..6, 7..8], "a run apiece, two workers, four to go round");
        assert!(runs.iter().all(|run| run.start >= 1 && run.end <= 8));
    }

    /// Under a top N the pieces of eight parts that reach furthest the way the ordering runs are cut
    /// out of the runs and go out first, what is left of a run between them stays one run, and every
    /// part is still in exactly one run. Part `at` holds `at % 13` to `at % 13 + 5`, and parts 30 to
    /// 39 have no stored range. The pieces are 0..8, 8..16, 16..20, 20..28, 28..36, 36..44 and 44..45.
    #[test]
    fn the_pieces_a_top_n_wants_most_go_out_first() {
        let range = |at: usize| {
            let low = i128::try_from(at % 13).expect("small");
            (!(30..40).contains(&at)).then(|| rudb_storage::Range {
                low: Some(Bound::Int(low)),
                high: Some(Bound::Int(low + 5)),
                ..rudb_storage::Range::default()
            })
        };
        let runs = vec![0..20, 20..45];

        // Lows reached: 0, 0 (part 13), 3 (16..20 has 16 to 19), 0 (part 26), 2 (28 and 29), 1
        // (part 40), 5 (part 44). The two that reach 0 first go first, in table order between them.
        let (out, order) = best_first(runs.clone(), 2, false, range);
        assert_eq!(out, [0..8, 8..16, 16..20, 20..45]);
        assert_eq!(order, [0, 1, 2, 3]);
        let (out, order) = best_first(runs.clone(), 3, false, range);
        assert_eq!(out, [0..8, 8..16, 16..20, 20..28, 28..45]);
        assert_eq!(order, [0, 1, 3, 2, 4], "the third best is the first to reach 0 after 13");
        let (out, order) = best_first(runs.clone(), 4, false, range);
        assert_eq!(out, [0..8, 8..16, 16..20, 20..28, 28..36, 36..44, 44..45]);
        assert_eq!(order, [0, 1, 3, 5, 2, 4, 6]);
        let (out, order) = best_first(runs.clone(), 1, true, range);
        assert_eq!(out, [0..8, 8..16, 16..20, 20..45]);
        assert_eq!(order, [1, 0, 2, 3], "part 12 reaches 17 and part 25 only ties it");
        let (out, order) = best_first(runs.clone(), 99, false, |_| None);
        assert_eq!((out, order), (runs, Vec::new()), "nothing stored leaves the runs alone");
        let (out, _) = best_first(vec![0..20, 20..45], 3, false, range);
        let seen: Vec<usize> = out.iter().flat_map(Clone::clone).collect();
        assert_eq!(seen, (0..45).collect::<Vec<_>>());
    }

    /// A scan of the `rudb-parquet` fixture, which is 4096 rows in two row groups.
    ///
    /// Built from a plan written as text, the way every other operator test in this crate builds
    /// one, because the fields a table function node carries are arena slices and writing them out
    /// by hand would be a test of the arena builders.
    fn fixture() -> FileScan<'static> {
        pruned(Vec::new())
    }

    /// The same scan with bounds tests on it, which is what a filter above the scan compiles to.
    ///
    /// Told it has two threads, as the scheduler tells every scan before it takes a morsel, so the
    /// two groups are a share each and are handed out apart rather than gathered.
    fn pruned(tests: Vec<(usize, Op, Bound)>) -> FileScan<'static> {
        let scan = untold(tests);
        assert_eq!(scan.morsels(2, 0), Some(2), "a group for each of the two threads");
        scan
    }

    /// The scan as it is before the scheduler says anything, which is one thread.
    fn untold(tests: Vec<(usize, Op, Bound)>) -> FileScan<'static> {
        let path = format!("{}/../rudb-parquet/testdata/mixed.parquet", env!("CARGO_MANIFEST_DIR"));
        let path = path.replace('\\', "\\\\").replace('\'', "''");
        let text = format!(
            "TableFunction read_parquet args=['{path}'::VARCHAR] #0 [a::INTEGER, b::BIGINT]"
        );
        let plan = Plan::parse(&text).expect("the plan text round trips");
        let Node::TableFunction { index, args, options, settings, columns, .. } =
            *plan.node(plan.root())
        else {
            panic!("the plan is a table function");
        };
        FileScan::new(
            &plan,
            index,
            TableFunction::ReadParquet,
            args,
            options,
            settings,
            columns,
            tests,
            None,
        )
        .expect("the fixture is there")
    }

    /// Every morsel the scan hands out, drained.
    fn morsels(scan: &FileScan<'_>) -> Vec<usize> {
        let mut rows = Vec::new();
        while let Some(mut morsel) = scan.morsel() {
            let mut chunk = Chunk::empty(&[]);
            let mut read = 0;
            while let Progress::More = scan.read(&mut morsel, &mut chunk).expect("decodes") {
                read += chunk.len();
            }
            rows.push(read);
        }
        rows
    }

    /// The property the scheduler needs. A file of two row groups is two units of work, not one,
    /// and the two between them hold every row of the file.
    #[test]
    fn a_parquet_file_is_one_morsel_per_row_group() {
        let scan = fixture();
        let rows = morsels(&scan);
        assert_eq!(rows, [2048, 2048], "two row groups of 2048");
    }

    /// The point of the bounds tests. The integer column of the fixture runs 0 to 96, so a filter
    /// asking for rows above a thousand cannot be satisfied by either row group, and neither group
    /// is handed out at all. No morsel means no page was read, which is the whole saving.
    #[test]
    fn a_row_group_whose_bounds_rule_out_the_filter_is_never_handed_out() {
        let scan = pruned(vec![(0, Op::Greater, Bound::Int(1_000))]);

        let rows = morsels(&scan);

        assert_eq!(rows, Vec::<usize>::new(), "both row groups are ruled out");
        let cutting = scan.cutting.lock().expect("the lock holds");
        assert_eq!(cutting.skipped, 2, "and both were skipped rather than read");
    }

    /// The other half of it. A bound that overlaps the column leaves the scan exactly as it was,
    /// because a row group the statistics cannot rule out has to be read and filtered as usual.
    #[test]
    fn a_row_group_whose_bounds_overlap_the_filter_is_handed_out_as_usual() {
        let scan = pruned(vec![(0, Op::Greater, Bound::Int(50))]);

        let rows = morsels(&scan);

        assert_eq!(rows, [2048, 2048], "nothing is ruled out");
        let cutting = scan.cutting.lock().expect("the lock holds");
        assert_eq!(cutting.skipped, 0);
    }

    /// A test naming a column the scan does not produce is dropped rather than misread as column
    /// zero, which would skip row groups holding rows the query wants.
    #[test]
    fn a_test_against_a_position_the_scan_does_not_produce_rules_nothing_out() {
        let scan = pruned(vec![(7, Op::Greater, Bound::Int(1_000))]);

        assert_eq!(morsels(&scan), [2048, 2048]);
    }

    /// Morsels are taken until they run out, and asking after that hands back nothing rather than
    /// starting again, which is what makes a driver loop safe to write as a `while let`.
    #[test]
    fn a_scan_that_has_handed_out_every_morsel_hands_out_no_more() {
        let scan = fixture();
        let _ = morsels(&scan);
        assert!(scan.morsel().is_none());
        assert!(scan.morsel().is_none());
    }

    /// Each morsel carries its own reader, so taking them all before reading any of them reads the
    /// same rows as taking and reading them one at a time. That is the difference between a scan
    /// two threads can share and a scan they queue behind.
    #[test]
    fn every_morsel_can_be_taken_before_any_of_them_is_read() {
        let scan = fixture();
        let mut taken = Vec::new();
        while let Some(morsel) = scan.morsel() {
            taken.push(morsel);
        }
        assert_eq!(taken.len(), 2);
        let mut rows = Vec::new();
        for morsel in &mut taken {
            let mut chunk = Chunk::empty(&[]);
            let mut read = 0;
            while let Progress::More = scan.read(morsel, &mut chunk).expect("decodes") {
                read += chunk.len();
            }
            rows.push(read);
        }
        assert_eq!(rows, [2048, 2048]);
    }

    /// A table of one `INTEGER` column holding `0..rows`, which puts a different range in every
    /// chunk and so makes the zone maps worth having.
    fn counted(rows: usize) -> Table {
        let mut table = Table::new(
            QualifiedName::new("memory", "main", "t"),
            vec![Field::new("n", LogicalType::Integer)],
        )
        .expect("one column");
        let values: Vec<Vec<Value>> = (0..rows).map(|n| vec![Value::Integer(n as i32)]).collect();
        table.append_rows(&values).expect("integers");
        table
    }

    /// A scan of that table with `tests` on its only column, built without going through a plan.
    fn scanning(table: &Table, tests: Vec<(usize, Op, Bound)>) -> Scan<'_> {
        let fields = vec![Field::new("n", LogicalType::Integer)];
        let probes =
            tests.into_iter().map(|(column, op, value)| Probe { column, op, value }).collect();
        Scan {
            table,
            columns: vec![Some(0)],
            offsets: (0..table.rows().chunk_count())
                .map(|at| i64::try_from(at * VECTOR_SIZE).expect("a small table"))
                .collect(),
            probes,
            also: Vec::new(),
            sideways: None,
            cutoff: None,
            index: 0,
            testing: OnceLock::new(),
            schema: Schema::numbered(fields, 0),
            chunks: Handout::new(table.rows().chunk_count()),
            stripes: Vec::new(),
            pushed: None,
            waved: AtomicUsize::new(0),
            spread: OnceLock::new(),
            skipped: AtomicUsize::new(0),
            counters: None,
            deferred: Vec::new(),
            deferrable: Vec::new(),
            deferring: Paying::default(),
            unread: Vec::new(),
            paying: Paying::default(),
            passed: Paying::default(),
        }
    }

    /// A scan of that table under a top N that has filled its candidates and reached `bound`.
    ///
    /// No tests of its own, so the only thing that can rule a part out here is the cutoff.
    fn beaten(table: &Table, op: Op, bound: Option<Bound>) -> Scan<'_> {
        let cutoff = Cutoff::new();
        cutoff.about(ColumnBinding::new(0, 0), op);
        if let Some(bound) = bound {
            cutoff.reached(bound);
        }
        let mut scan = scanning(table, Vec::new());
        scan.cutoff = Some(cutoff);
        scan
    }

    /// A scan of `counted` applying `predicate` itself, the way the builder hands one over.
    ///
    /// The plan is written out and parsed rather than assembled, so the predicate this applies is
    /// the one a query would really have produced, casts and all.
    fn applying<'a>(table: &'a Table, predicate: &str) -> (Plan, Scan<'a>) {
        let plan =
            Plan::parse(&format!("Filter {predicate}\n  Get memory.main.t AS t #0 [n::INTEGER]"))
                .expect("the plan text round trips");
        let Node::Filter { input, predicate } = *plan.node(plan.root()) else {
            panic!("the plan is a filter");
        };
        let Node::Get { index, columns, .. } = *plan.node(input) else {
            panic!("under a get");
        };
        let moved =
            rudb_opt::bounds::into_scan(&plan, plan.root()).expect("a filter over a stored table");
        let pruning = rudb_opt::bounds::of(&plan, input, predicate);
        let pushdown = Pushdown {
            node: plan.root(),
            predicate,
            tests: moved.tests,
            whole: moved.whole,
            conjuncts: moved.conjuncts,
            marks: false,
        };
        let filters = Filters { pruning, pushed: Some(pushdown), ..Filters::default() };
        let scan = Scan::new(
            &plan,
            table,
            index,
            columns,
            filters,
            &Settings::default(),
            &Session::default(),
        )
        .expect("the column is there");
        (plan, scan)
    }

    /// A necessary LIKE can read one column first without changing the full AND answer.
    #[test]
    fn a_sparse_like_fetches_the_second_column_after_selection() {
        let mut table = Table::new(
            QualifiedName::new("memory", "main", "t"),
            vec![
                Field::new("URL", LogicalType::Varchar),
                Field::new("SearchPhrase", LogicalType::Varchar),
            ],
        )
        .expect("two columns");
        let rows = (0..VECTOR_SIZE * 3)
            .map(|row| {
                let matching =
                    row == 3 || row == 5 || (VECTOR_SIZE..VECTOR_SIZE + 400).contains(&row);
                let phrase = if row == 5 || row == VECTOR_SIZE + 10 { "" } else { "phrase" };
                vec![
                    Value::Varchar(if matching { "google.test" } else { "example.test" }.into()),
                    Value::Varchar(phrase.into()),
                ]
            })
            .collect::<Vec<_>>();
        table.append_rows(&rows).expect("rows");
        let plan = Plan::parse(
            "Filter (\"~~\"(#0.0::VARCHAR, '%google%'::VARCHAR)::BOOLEAN AND (#0.1::VARCHAR <> ''::VARCHAR)::BOOLEAN)::BOOLEAN\n  Get memory.main.t AS t #0 [URL::VARCHAR, SearchPhrase::VARCHAR]",
        )
        .expect("the plan text round trips");
        let Node::Filter { input, predicate } = *plan.node(plan.root()) else {
            panic!("the plan is a filter");
        };
        let Node::Get { index, columns, .. } = *plan.node(input) else { panic!("under a get") };
        let moved =
            rudb_opt::bounds::into_scan(&plan, plan.root()).expect("a filter over a stored table");
        let pushdown = Pushdown {
            node: plan.root(),
            predicate,
            tests: moved.tests,
            whole: moved.whole,
            conjuncts: moved.conjuncts,
            marks: false,
        };
        let filters = Filters { pushed: Some(pushdown), ..Filters::default() };
        let scan = Scan::new(
            &plan,
            &table,
            index,
            columns,
            filters,
            &Settings::default(),
            &Session::default(),
        )
        .expect("two projected columns");
        assert!(scan.pushed.as_ref().and_then(|pushed| pushed.late.as_ref()).is_some());
        let mut found = Vec::new();
        while let Some(mut morsel) = scan.morsel() {
            loop {
                let mut chunk = Chunk::empty(&[]);
                let progress = scan.read(&mut morsel, &mut chunk).expect("a part reads");
                found.extend(
                    (0..chunk.len()).map(|row| (chunk.value_at(row, 0), chunk.value_at(row, 1))),
                );
                if progress == Progress::Done {
                    break;
                }
            }
        }
        let expected = rows
            .iter()
            .filter(|row| {
                matches!(&row[0], Value::Varchar(url) if url.contains("google"))
                    && matches!(&row[1], Value::Varchar(phrase) if !phrase.is_empty())
            })
            .map(|row| (row[0].clone(), row[1].clone()))
            .collect::<Vec<_>>();
        assert_eq!(found, expected, "sparse, dense, and empty parts keep the same rows");
    }

    /// The middle of the three answers. Every row of the table is at or above zero, so every chunk
    /// is one the zone waves through and the comparison never runs on any of them.
    #[test]
    fn a_filter_the_zone_maps_prove_is_applied_to_no_rows_at_all() {
        let table = counted(VECTOR_SIZE * 5);
        let (_plan, scan) = applying(&table, "(#0.0::INTEGER >= 0::INTEGER)::BOOLEAN");

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 5, "every row came through");
        assert_eq!(scan.waved.load(Ordering::Relaxed), 5, "and no chunk was compared");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 0, "nor ruled out");
    }

    /// All three answers in one scan. Five chunks hold the rows a vector at a time and the filter
    /// keeps everything from halfway through the third one up, so the first two are ruled out, the
    /// third straddles the constant and is compared, and the last two are waved through whole.
    ///
    /// The cutoff is worked out from the vector size rather than written down, which it was until
    /// the vector stopped being 1024 and the constant landed inside the first chunk instead of the
    /// third. Every boundary in these tests is a multiple of the thing under test.
    #[test]
    fn a_scan_skips_waves_through_and_compares_in_the_one_pass() {
        let table = counted(VECTOR_SIZE * 5);
        let cutoff = VECTOR_SIZE * 2 + VECTOR_SIZE / 2;
        let (_plan, scan) =
            applying(&table, &format!("(#0.0::INTEGER >= {cutoff}::INTEGER)::BOOLEAN"));

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 5 - cutoff);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 2, "the first two hold nothing wanted");
        assert_eq!(scan.waved.load(Ordering::Relaxed), 2, "the last two are all of them wanted");
    }

    /// A predicate the zone maps cannot prove as a whole, because one conjunct is `<>`, still has
    /// its other conjunct left out on the chunks that prove that one. The rows are what the whole
    /// predicate keeps either way, which is the thing that could go wrong.
    #[test]
    fn a_conjunct_a_chunk_proves_is_left_out_and_the_rest_still_runs() {
        let table = counted(VECTOR_SIZE * 5);
        let cutoff = VECTOR_SIZE * 2 + VECTOR_SIZE / 2;
        let odd = VECTOR_SIZE * 4 + 3;
        let (_plan, scan) = applying(
            &table,
            &format!(
                "((#0.0::INTEGER >= {cutoff}::INTEGER)::BOOLEAN AND \
                 (#0.0::INTEGER <> {odd}::INTEGER)::BOOLEAN)::BOOLEAN"
            ),
        );
        let pushed = scan.pushed.as_ref().expect("the filter moved into the scan");
        assert!(pushed.probes.is_none(), "the `<>` is not a test, so no chunk is proved whole");
        let settles = pushed.settles.as_ref().expect("the other conjunct is");
        assert_eq!(settles.iter().map(Option::is_some).collect::<Vec<_>>(), [true, false]);

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 5 - cutoff - 1);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 2, "the first two hold nothing wanted");
        assert_eq!(scan.waved.load(Ordering::Relaxed), 0, "and none is wanted whole");
    }

    /// Every part of the table is counted exactly once, as read or as ruled out.
    ///
    /// The invariant matters because the two numbers go on the operator row as "n of m parts
    /// skipped", and an m that is not the table is a sentence that reads as true and is not. There
    /// are two places a part can be ruled out, the walk in [`Source::read`] and the runs
    /// [`Source::morsels`] hands out, and the easy mistake is to count one of them.
    #[test]
    fn the_two_part_counts_add_up_to_the_table() {
        let table = counted(VECTOR_SIZE * 5);
        let cutoff = VECTOR_SIZE * 2 + VECTOR_SIZE / 2;
        let (_plan, mut scan) =
            applying(&table, &format!("(#0.0::INTEGER >= {cutoff}::INTEGER)::BOOLEAN"));
        let counters = Arc::new(Counters::new(0, 0, "Scan"));
        scan = scan.watched(Arc::clone(&counters));

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 5 - cutoff);
        let operator = counters.snapshot();
        assert_eq!(operator.parts_pruned, 2, "the first two hold nothing wanted");
        assert_eq!(
            operator.parts_read + operator.parts_pruned,
            table.rows().chunk_count() as u64,
            "every part is counted once and only once"
        );
    }

    /// The filter is really applied and not only decided about.
    #[test]
    fn a_chunk_the_zone_cannot_decide_comes_back_narrowed_to_the_rows_that_pass() {
        let table = counted(VECTOR_SIZE);
        let (_plan, scan) = applying(&table, "(#0.0::INTEGER < 10::INTEGER)::BOOLEAN");

        assert_eq!(counted_rows(&scan), 10);
        assert_eq!(scan.waved.load(Ordering::Relaxed), 0);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 0);
    }

    /// A predicate the zone maps cannot read still moves into the scan, and still answers.
    ///
    /// The `OR` is what ClickBench 40 writes as `TraficSourceID IN (-1, 6)`, and it reads as no test
    /// at all, because a conjunct under an `OR` says nothing about the row when it is false. That
    /// used to keep the whole filter above the scan. It does not any more: the comparison runs here,
    /// on every chunk, which is exactly what it would have done up there, and one operator and the
    /// chunk handed across to it are gone.
    ///
    /// Both counters staying at zero is the part worth pinning. Nothing may be waved through, since
    /// there is no test that proves anything about a row, and an empty test list read as a proof is
    /// the one way this arrangement returns rows the query asked to be rid of.
    #[test]
    fn a_filter_with_an_or_in_it_moves_into_the_scan_and_no_chunk_is_waved_through() {
        let table = counted(VECTOR_SIZE * 5);
        let high = VECTOR_SIZE * 5 - 10;
        let (_plan, scan) = applying(
            &table,
            &format!(
                "((#0.0::INTEGER < 10::INTEGER)::BOOLEAN \
                 OR (#0.0::INTEGER >= {high}::INTEGER)::BOOLEAN)::BOOLEAN"
            ),
        );

        assert_eq!(counted_rows(&scan), 20, "the ten at each end");
        assert_eq!(scan.waved.load(Ordering::Relaxed), 0, "and nothing was proved about a chunk");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 0);
    }

    /// How many rows the scan produced, over every morsel it hands out.
    fn counted_rows(scan: &Scan<'_>) -> usize {
        let mut rows = 0;
        while let Some(mut morsel) = scan.morsel() {
            let mut chunk = Chunk::empty(&[LogicalType::Integer]);
            loop {
                let progress = scan.read(&mut morsel, &mut chunk).expect("a table scan reads");
                rows += chunk.len();
                if progress == Progress::Done {
                    break;
                }
            }
        }
        rows
    }

    /// The point of the zone maps. Five chunks hold 0 to 10239, and `n = 5000` is in exactly one of
    /// them, so four are never read at all and the filter above never sees their rows.
    #[test]
    fn a_filter_on_a_table_reads_only_the_chunks_that_can_hold_a_match() {
        let table = counted(VECTOR_SIZE * 5);
        assert_eq!(table.rows().chunk_count(), 5);
        let scan = scanning(&table, vec![(0, Op::Equal, Bound::Int(5_000))]);

        assert_eq!(counted_rows(&scan), VECTOR_SIZE, "one chunk's worth");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 4);
    }

    /// The point of the cutoff. Five chunks hold 0 to 5119 and the top N above already holds ten
    /// rows whose worst key is 100, so every chunk but the first holds nothing that can still win.
    #[test]
    fn a_scan_skips_the_parts_the_top_n_above_it_has_already_beaten() {
        let table = counted(VECTOR_SIZE * 5);
        let scan = beaten(&table, Op::LessOrEqual, Some(Bound::Int(100)));

        assert_eq!(counted_rows(&scan), VECTOR_SIZE, "only the chunk holding 0 to 1023");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 4);
    }

    /// The same read the other way up, which is the other comparison the ordering can give.
    #[test]
    fn a_descending_top_n_skips_the_parts_below_its_cutoff() {
        let table = counted(VECTOR_SIZE * 5);
        let cutoff = i128::try_from(VECTOR_SIZE * 4 + VECTOR_SIZE / 2).expect("a small number");
        let scan = beaten(&table, Op::GreaterOrEqual, Some(Bound::Int(cutoff)));

        assert_eq!(counted_rows(&scan), VECTOR_SIZE, "only the last chunk");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 4);
    }

    /// A part that ties the cutoff is read like any other, which is what keeps ties settled the way
    /// they were. The third chunk starts exactly at the cutoff and is read.
    #[test]
    fn a_part_that_only_ties_the_cutoff_is_still_read() {
        let table = counted(VECTOR_SIZE * 5);
        let cutoff = i128::try_from(VECTOR_SIZE * 2).expect("a small number");
        let scan = beaten(&table, Op::LessOrEqual, Some(Bound::Int(cutoff)));

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 3);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 2);
    }

    /// Before the top N has filled its candidates there is nothing it can rule out, which is the
    /// first chunks of every query and the whole of a query whose limit is never reached.
    #[test]
    fn a_top_n_that_has_not_filled_its_candidates_rules_nothing_out() {
        let table = counted(VECTOR_SIZE * 3);
        let scan = beaten(&table, Op::LessOrEqual, None);

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 3);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 0);
    }

    /// A scan with nothing to go on reads everything, which is the case that must not regress.
    #[test]
    fn a_scan_with_no_tests_reads_every_chunk() {
        let table = counted(VECTOR_SIZE * 3);
        let scan = scanning(&table, Vec::new());

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 3);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 0);
    }

    /// The same skipping, from a range no plan could have carried: what a join above this scan found
    /// on its other side. Five chunks hold 0 to 10239 and the build side held keys 5000 to 5100, so
    /// only the chunk those fall in is read.
    #[test]
    fn a_scan_skips_the_chunks_a_joins_build_side_ruled_out() {
        let table = counted(VECTOR_SIZE * 5);
        let sideways = Sideways::new();
        sideways.about(ColumnBinding::new(0, 0));
        sideways.found(Found::of(Some((Bound::Int(5_000), Bound::Int(5_100))), None));
        let mut scan = scanning(&table, Vec::new());
        scan.sideways = Some(sideways);

        assert_eq!(counted_rows(&scan), VECTOR_SIZE, "one chunk's worth");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 4);
    }

    /// The keys themselves, for a build side whose range covers the whole table. Five chunks hold 0
    /// to 10239 and the build side held 100 and 9000, so the three chunks between are never read.
    #[test]
    fn a_scan_skips_the_chunks_between_a_joins_keys() {
        let table = counted(VECTOR_SIZE * 5);
        let sideways = Sideways::new();
        sideways.about(ColumnBinding::new(0, 0));
        let range = Some((Bound::Int(100), Bound::Int(9_000)));
        sideways.found(Found::listing(range, vec![9_000, 100]));
        let mut scan = scanning(&table, Vec::new());
        scan.sideways = Some(sideways);

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 2, "the first chunk and the last");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 3);
    }

    /// The exact tier, which answers by position rather than by value. Five chunks, and the join
    /// above handed down ten rows of the third and one of the fifth, so three chunks are never read
    /// and only those eleven rows come out of the two that are.
    #[test]
    fn a_scan_keeps_exactly_the_rows_a_reduction_handed_down() {
        let table = counted(VECTOR_SIZE * 5);
        let size = u64::try_from(VECTOR_SIZE).expect("a small number");
        let kept: Vec<u64> = (2 * size + 5..2 * size + 15).chain([4 * size + 7]).collect();
        let sideways = Sideways::new();
        sideways.about(ColumnBinding::new(0, 0));
        let rows = rudb_graph::Rids::from_sorted(5 * size, kept).expect("in order");
        sideways.found(Found::exactly(None, rows));
        let mut scan = scanning(&table, Vec::new());
        scan.sideways = Some(sideways);

        assert_eq!(counted_rows(&scan), 11);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 3);
    }

    /// A filter over three keys, hashed the way the build side of a join hashes the column it is
    /// keyed on.
    fn holding(values: &[i32]) -> Blocked {
        let mut filter = Blocked::sized(values.len(), 1 << 20).expect("a filter over three keys");
        let column = Vector::flat(LogicalType::Integer, Data::Int32(values.to_vec().into()))
            .expect("integers are an i32 layout");
        let mut hashes = Vec::new();
        hash(std::slice::from_ref(&column), values.len(), &mut hashes, Across::TwoInputs);
        for word in hashes {
            filter.add(word);
        }
        filter
    }

    /// The row tier. The range keeps the chunk the three keys fall in and the filter throws away
    /// every row of it that is not one of them.
    #[test]
    fn a_scan_drops_the_rows_a_joins_build_side_cannot_match() {
        let table = counted(VECTOR_SIZE * 3);
        let sideways = Sideways::new();
        sideways.about(ColumnBinding::new(0, 0));
        sideways.found(Found::of(
            Some((Bound::Int(2_100), Bound::Int(2_300))),
            Some(holding(&[2_100, 2_200, 2_300])),
        ));
        let mut scan = scanning(&table, Vec::new());
        scan.sideways = Some(sideways);

        assert_eq!(counted_rows(&scan), 3, "the three rows the build side holds a key for");
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 2, "and two chunks were never read");
    }

    /// A filter that keeps nearly every row it sees is dropped once the warmup is over, and one
    /// that turns rows away is kept however long the scan runs.
    #[test]
    fn a_runtime_filter_that_turns_nothing_away_is_given_up_on() {
        let idle = Paying::default();
        let busy = Paying::default();
        for _ in 0..(WARMUP / VECTOR_SIZE) {
            assert!(idle.worth(), "nothing is decided under the warmup");
            idle.saw(VECTOR_SIZE, VECTOR_SIZE);
            busy.saw(VECTOR_SIZE, VECTOR_SIZE / 16);
        }

        assert!(!idle.worth(), "a filter that kept every row is not worth hashing for");
        assert!(busy.worth(), "one that kept a sixteenth of them is");

        // Giving up is final, so the rows that go past afterwards are never counted and the answer
        // cannot drift back.
        assert!(!idle.worth());
    }

    /// The line itself, which is a quarter of the rows dropped.
    #[test]
    fn a_filter_is_worth_hashing_for_once_it_drops_a_quarter_of_the_rows() {
        for (kept, worth) in [(0, true), (740, true), (749, true), (750, false), (1_000, false)] {
            let paying = Paying::default();
            for _ in 0..(WARMUP / 1_000 + 1) {
                paying.saw(1_000, kept);
            }
            assert_eq!(paying.worth(), worth, "{kept} of every thousand rows kept");
        }
    }

    /// The pushed filter counts as loose only after the warmup and only when it keeps more than
    /// half, which is when a Bloom filter goes ahead of it.
    #[test]
    fn a_pushed_filter_is_loose_once_it_keeps_more_than_half() {
        for (kept, loose) in [(0, false), (250, false), (500, false), (501, true), (1_000, true)] {
            let passed = Paying::default();
            assert!(!passed.loose(), "nothing is decided under the warmup");
            for _ in 0..(WARMUP / 1_000 + 1) {
                passed.saw(1_000, kept);
            }
            assert_eq!(passed.loose(), loose, "{kept} of every thousand rows kept");
        }
    }

    /// A join that never armed one, and a build side with no rows, both leave the scan reading
    /// everything. This is the case that must not regress, because every join that cannot use a
    /// runtime filter still makes one.
    #[test]
    fn a_scan_under_a_join_that_offered_nothing_reads_every_chunk() {
        let table = counted(VECTOR_SIZE * 3);
        let mut scan = scanning(&table, Vec::new());
        scan.sideways = Some(Sideways::new());

        assert_eq!(counted_rows(&scan), VECTOR_SIZE * 3);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 0);
    }

    /// Two conjuncts that between them leave no chunk, which is the shape ClickBench 37 has.
    #[test]
    fn conjuncts_that_rule_out_every_chunk_read_nothing() {
        let table = counted(VECTOR_SIZE * 4);
        let split = i128::try_from(VECTOR_SIZE * 2).expect("a small number");
        let scan = scanning(
            &table,
            vec![(0, Op::GreaterOrEqual, Bound::Int(split)), (0, Op::Less, Bound::Int(split))],
        );

        assert_eq!(counted_rows(&scan), 0);
        assert_eq!(scan.skipped.load(Ordering::Relaxed), 4);
    }

    /// The cutting of a row group, driven directly because every committed fixture is smaller than
    /// one morsel and so cannot be cut into more than one.
    #[test]
    fn the_morsels_of_a_row_group_tile_it_once_each() {
        for rows in [1, 2, 7, 9, 10, 100, 4_095, 4_096, 4_097, 122_880, 123_554] {
            for target in [1, 2, 3, 300, 4_096, 32_768] {
                let pieces = cutting(rows, target);
                assert_eq!(pieces.len(), parts(rows, target), "{rows} rows in morsels of {target}");
                let mut at = 0;
                for piece in &pieces {
                    assert_eq!(piece.start, at, "{rows} rows in morsels of {target}");
                    assert!(piece.len() <= target, "{rows} rows in morsels of {target}");
                    at = piece.end;
                }
                assert_eq!(at, rows, "{rows} rows in morsels of {target}");
            }
        }
    }

    /// Even morsels rather than full ones and a remainder, because the remainder is the morsel every
    /// other thread waits for.
    #[test]
    fn a_row_group_is_cut_into_even_morsels() {
        // A DuckDB row group in four, which is 30889 rows three times and 30887 once rather than
        // 32768 three times and 25250 once.
        let pieces = cutting(123_554, 32_768);
        assert_eq!(pieces.len(), 4);
        let longest = pieces.iter().map(std::ops::Range::len).max().expect("four morsels");
        let shortest = pieces.iter().map(std::ops::Range::len).min().expect("four morsels");
        assert_eq!(longest, 30_889);
        assert_eq!(shortest, 30_887);
        assert!(longest - shortest < pieces.len(), "morsels of {longest} and {shortest} rows");
    }

    /// The common case, which is every row group of every file small enough not to be worth cutting.
    #[test]
    fn a_row_group_no_larger_than_a_morsel_is_one_morsel() {
        assert_eq!(cutting(2_048, 32_768), vec![0..2_048]);
        assert_eq!(cutting(32_768, 32_768), vec![0..32_768]);
        assert_eq!(parts(2_048, 32_768), 1);
    }

    /// What stops a file with an empty row group in it from being cut forever. The group is counted
    /// as one morsel by `parts` and handed out as none, which is the safe way round because the
    /// count is only ever used to decide how many copies of a pipeline to build.
    #[test]
    fn an_empty_row_group_is_no_morsels() {
        assert!(next_piece(0, 0, 32_768).is_empty());
        assert_eq!(cutting(0, 32_768), Vec::new());
        assert_eq!(cutting(0, 0), Vec::new());
    }

    /// What a load asks a scan to gather up to. See `NativeSink` in the `rudb` crate.
    const LOAD: usize = 131_072;

    /// A scan with one thread that a load asked to gather takes the fixture's two groups as one
    /// morsel. The rows are the same rows in the same order, and the row numbers carry on across the
    /// join between the groups.
    #[test]
    fn a_scan_a_load_asked_to_gather_takes_small_row_groups_as_one_morsel() {
        let scan = untold(Vec::new());
        scan.gather(LOAD);
        assert_eq!(scan.morsels(1, 0), Some(1));
        assert_eq!(morsels(&scan), [4096], "both groups in one morsel");
        assert_eq!(morsels(&fixture()), [2048, 2048], "and apart when there are two threads");
    }

    /// A query asks for nothing and gets a morsel a group, however few threads it has.
    #[test]
    fn a_scan_nobody_asked_to_gather_hands_out_a_group_at_a_time() {
        let scan = untold(Vec::new());
        scan.gather(0);
        assert_eq!(scan.morsels(1, 0), Some(2));
        assert_eq!(morsels(&scan), [2048, 2048]);
    }

    /// A run stops at a group the filter rules out, and the next morsel starts past it.
    #[test]
    fn a_gathered_run_stops_at_a_group_the_filter_rules_out() {
        let scan = untold(vec![(0, Op::Greater, Bound::Int(1_000))]);
        scan.gather(LOAD);
        assert_eq!(scan.morsels(1, 0), Some(1));
        assert_eq!(morsels(&scan), Vec::<usize>::new());
        assert_eq!(scan.cutting.lock().expect("the lock holds").skipped, 2);
    }

    /// Groups join a run while it stays within the target, and a group as large as the target is
    /// never joined to anything.
    #[test]
    fn small_row_groups_are_gathered_up_to_the_target_and_large_ones_are_left_alone() {
        assert_eq!(gathered(8_000, [8_000, 8_000, 8_000].into_iter(), 20_000), 2);
        assert_eq!(gathered(8_000, [8_000; 40].into_iter(), LOAD), 16);
        assert_eq!(gathered(122_880, [122_880].into_iter(), LOAD), 1, "a DuckDB file");
        assert_eq!(gathered(8_000, [200_000, 8_000].into_iter(), LOAD), 1);
        assert_eq!(gathered(8_000, std::iter::empty(), LOAD), 1);
        assert_eq!(gathered(0, [0, 0].into_iter(), 0), 3, "empty groups cost nothing to join");
    }

    /// The ClickBench sample: 1,203 groups of about eight thousand rows. At thirty two threads it
    /// comes to about eighty morsels, the same as the ten million rows DuckDB writes in 81 groups,
    /// and never to fewer morsels than threads.
    #[test]
    fn the_clickbench_sample_is_gathered_into_as_many_morsels_as_duckdb_writes_groups() {
        let rows = vec![8_312; 1_203];
        let total: usize = rows.iter().sum();
        assert_eq!(morsels_of(&rows, 0, 0), 1_203, "one a group without gathering");
        assert_eq!(gather_target(total, 32, 0), 0, "a query asks for nothing");
        let gather = gather_target(total, 32, LOAD);
        assert_eq!(gather, LOAD);
        assert_eq!(morsels_of(&rows, 0, gather), 1_203_usize.div_ceil(15));
        let many = gather_target(total, 1_000, LOAD);
        assert!(morsels_of(&rows, 0, many) >= 1_000.min(rows.len()) / 2, "threads are kept busy");
        assert_eq!(gather_target(total, 2_000, LOAD), 4_999, "a share each, below a group");
        assert_eq!(morsels_of(&rows, 0, gather_target(total, 2_000, LOAD)), 1_203);
    }

    /// A file whose pages are as long as its row groups, which is every file DuckDB writes. The cut
    /// is turned off rather than made small, because a morsel that starts inside a page decodes that
    /// page whole and then throws most of it away.
    #[test]
    fn a_row_group_that_is_not_worth_cutting_is_one_morsel() {
        assert_eq!(cutting(123_554, 0), vec![0..123_554]);
        assert_eq!(parts(123_554, 0), 1);
        assert_eq!(parts(0, 0), 1);
    }

    /// How finely a file is cut, as a function of the threads there are to keep busy. Driven through
    /// the arithmetic rather than through a scan, because a committed fixture is one row group of
    /// 2048 rows and the question is about a file of nine groups of a hundred and twenty thousand.
    #[test]
    fn a_file_is_cut_only_as_finely_as_there_are_threads_to_want_it() {
        // A million rows in nine row groups, which is what DuckDB writes, with pages small enough
        // that cutting is allowed at all.
        // Sixty four threads want eight pieces of each group and get four, because `MORSEL_ROWS` is
        // the floor on how small a piece is worth being whatever the machine has.
        let rows = 123_554;
        let whole = 12_355_400;
        for (threads, wanted) in [(1, 1), (8, 1), (9, 1), (16, 2), (32, 4), (64, 4)] {
            let cut = cut_rows(1024, whole, rows, 9, threads);
            assert_eq!(parts(rows, cut), wanted, "{threads} threads over nine row groups");
        }
    }

    /// The two measured points are the hundred thousand row line and the million row line, and both
    /// of them are here rather than only in the comment, because a constant with a benchmark behind
    /// it should fail a test when somebody changes it without running one.
    #[test]
    fn native_workers_grow_with_the_rows_there_are_to_divide() {
        for (rows, workers) in [
            (0, 1),
            (1_000, 1),
            (25_000, 1),
            (50_000, 2),
            (100_000, 4),
            (200_000, 8),
            (500_000, 8),
            (1_000_000, 16),
            (2_500_000, 40),
            (9_999_750, 160),
        ] {
            assert_eq!(instances_for(rows, 1), workers, "{rows} rows");
        }
    }

    /// The same rule read the other way, where the rows are few and what is done to each is not.
    ///
    /// ClickBench 39 is the first row of this. It prunes to about twenty four thousand rows and
    /// its aggregate groups five columns, two of them wide strings, so a row costs the pipeline
    /// about a dozen times what reading it cost, and at a weight of one the whole query ran on one
    /// thread of thirty two.
    #[test]
    fn native_workers_grow_with_the_work_behind_each_row() {
        for (rows, weight, workers) in [
            (24_576, 1, 1),
            (24_576, 2, 2),
            (24_576, 7, 7),
            (24_576, 12, 8),
            (1_000, 25, 1),
            (3_000, 9, 2),
            (0, 100, 1),
        ] {
            assert_eq!(instances_for(rows, weight), workers, "{rows} rows at weight {weight}");
        }
    }

    /// A weight cannot make a large query wider, because how far a large query is worth dividing
    /// is a fact about the machine and the measurements say sixteen at a million rows.
    ///
    /// The small slope caps at eight, so any query with enough rows to reach eight on that slope
    /// alone is already there and there is nothing for a weight to add. That is everything from
    /// two hundred thousand live rows up, which is where the whole of this change stops applying.
    #[test]
    fn a_weight_never_moves_the_slope_that_divides_a_large_query() {
        for rows in [200_000, 500_000, 1_000_000, 2_500_000, 9_999_750] {
            let plain = instances_for(rows, 1);
            for weight in [2, 4, 12, 64] {
                assert_eq!(instances_for(rows, weight), plain, "{rows} rows at weight {weight}");
            }
        }
    }

    /// The other half of the rule. However many threads are waiting, a file whose first page costs
    /// a morsel more than a quarter of what the morsel reads is handed out a row group at a time.
    #[test]
    fn a_file_of_coarse_pages_is_not_cut_however_many_threads_there_are() {
        // A hundred bytes a row, so the finest morsel this rule makes of a group of 123_554 rows is
        // `MORSEL_ROWS` rows and 3_276_800 bytes, a quarter of which is 819_200.
        let rows = 123_554;
        let whole = 12_355_400;
        assert_eq!(cut_rows(whole, whole, rows, 9, 64), 0, "one page per chunk, as DuckDB writes");
        assert_eq!(cut_rows(819_201, whole, rows, 9, 64), 0);
        assert_eq!(cut_rows(0, whole, rows, 9, 64), 0);
        assert_eq!(cut_rows(1024, 0, rows, 9, 64), 0, "a group of no bytes is never worth cutting");
        // The largest first page the rule lets through at the finest cut it makes.
        assert!(cut_rows(819_200, whole, rows, 9, 64) > 0);
    }
}