datui-lib 0.4.0

Data Exploration in the Terminal (library)
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
//! Opening a dataset, from the request to its first rows, and its one owner.
//!
//! [`Loader`] holds the open in flight ([`Load`]): where it was asked from, the paths it
//! was asked for, the phase it is in, what the loading screen says about it, and what
//! it holds — its stop flag and footer counter, the download it fetched, the IPC file
//! its Arrow streams were converted to, and the hold on the generation while the user
//! is asked about a download. The app tells it what happened (an open asked for, a
//! phase's worker answering or failing, the user's answer to the download question) and
//! it says what to do next ([`Step`]). The app carries the step out, runs the workers
//! and installs the dataset. Nothing else keeps a copy of the open's state.
//!
//! - **Identity.** Every load has a [`LoadId`], and the jobs of its phases carry it. An
//!   answer is taken only while its load is the one in flight and in the phase that asked
//!   for it: one from an open that was abandoned or replaced installs nothing and changes
//!   no title, and its payload is dropped with it.
//! - **Retirement.** Abandoning, replacing or failing a load drops what it holds: its
//!   stop flag is raised, so a download or a conversion stops at its next chunk and
//!   removes its file and a footer pass stops issuing reads; its download and converted
//!   file are let go; its hold is released.
//! - **Handover.** The dataset is built holding the load's download or converted file,
//!   with everything else the open found ([`crate::widgets::datatable::OpenFacts`]), and
//!   on install takes the load's footer counter. From then on they are the dataset's:
//!   what is left of the load is the read of the first rows ([`Phase::FirstRows`]), and
//!   abandoning that stops neither.
//!
//! The home screen's looks at a path, analyses and charts are not loads, and are not here.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use polars::prelude::LazyFrame;

use crate::download::TempDownload;
use crate::schema_union::FooterProgress;
use crate::unfinished::{Unfinished, Writer};
use crate::widgets::datatable::DataTableState;
use crate::{CompressionFormat, FileFormat, OpenOptions, source, stdin};

use crate::jobs::Hold;
#[cfg(any(feature = "http", feature = "cloud"))]
use crate::jobs::Jobs;

/// Names one open, from the moment it is asked for until it is done.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct LoadId(u64);

impl LoadId {
    /// A load's name for a job a test starts by hand.
    #[cfg(test)]
    pub(crate) fn for_tests(n: u64) -> Self {
        Self(n)
    }
}

/// A remote file to download once the user agrees; its size is what the probe found.
#[cfg(any(feature = "http", feature = "cloud"))]
#[derive(Clone)]
pub(crate) enum PendingDownload {
    #[cfg(feature = "http")]
    Http {
        url: String,
        size: Option<u64>,
        options: OpenOptions,
    },
    #[cfg(feature = "cloud")]
    S3 {
        url: String,
        size: Option<u64>,
        options: OpenOptions,
    },
    #[cfg(feature = "cloud")]
    Gcs {
        url: String,
        size: Option<u64>,
        options: OpenOptions,
    },
    #[cfg(feature = "cloud")]
    Azure {
        url: String,
        size: Option<u64>,
        options: OpenOptions,
    },
    /// Arrow in S3, GCS or Azure, one object or a prefix: `objects` are what the
    /// probe's listing chose, and `size` is its streams' bytes, which are downloaded
    /// and converted; its IPC files are scanned where they are (`cloud_arrow`).
    #[cfg(feature = "cloud")]
    Arrow {
        url: String,
        objects: Vec<crate::cloud_arrow::Object>,
        size: Option<u64>,
        options: OpenOptions,
    },
}

#[cfg(any(feature = "http", feature = "cloud"))]
impl PendingDownload {
    /// The url, the size the probe found, and the open options — the same three
    /// fields whichever store this came from.
    pub(crate) fn parts(&self) -> (&str, Option<u64>, &OpenOptions) {
        match self {
            #[cfg(feature = "http")]
            PendingDownload::Http { url, size, options } => (url, *size, options),
            #[cfg(feature = "cloud")]
            PendingDownload::S3 { url, size, options } => (url, *size, options),
            #[cfg(feature = "cloud")]
            PendingDownload::Gcs { url, size, options } => (url, *size, options),
            #[cfg(feature = "cloud")]
            PendingDownload::Azure { url, size, options } => (url, *size, options),
            #[cfg(feature = "cloud")]
            PendingDownload::Arrow {
                url, size, options, ..
            } => (url, *size, options),
        }
    }

    /// How many Arrow streams the download is, and how many IPC files are read in
    /// place beside them, for the question about it.
    pub(crate) fn arrow_files(&self) -> Option<(usize, usize)> {
        match self {
            #[cfg(feature = "cloud")]
            PendingDownload::Arrow { objects, .. } => {
                let streams = objects.iter().filter(|o| o.stream).count();
                Some((streams, objects.len() - streams))
            }
            _ => None,
        }
    }

    /// The download once the user has agreed to it: it never asks again, so it has
    /// no limit to stop at.
    pub(crate) fn asked(mut self) -> Self {
        match &mut self {
            #[cfg(feature = "http")]
            PendingDownload::Http { options, .. } => options.download_unasked = None,
            #[cfg(feature = "cloud")]
            PendingDownload::S3 { options, .. }
            | PendingDownload::Gcs { options, .. }
            | PendingDownload::Azure { options, .. }
            | PendingDownload::Arrow { options, .. } => options.download_unasked = None,
        }
        self
    }

    /// Replace the placeholder size with what the probe actually found.
    pub(crate) fn with_size(mut self, found: Option<u64>) -> Self {
        match &mut self {
            #[cfg(feature = "http")]
            PendingDownload::Http { size, .. } => *size = found,
            #[cfg(feature = "cloud")]
            PendingDownload::S3 { size, .. } => *size = found,
            #[cfg(feature = "cloud")]
            PendingDownload::Gcs { size, .. } => *size = found,
            #[cfg(feature = "cloud")]
            PendingDownload::Azure { size, .. } => *size = found,
            #[cfg(feature = "cloud")]
            PendingDownload::Arrow { size, .. } => *size = found,
        }
        self
    }
}

/// Paths asked to be opened, and what the screen and the recents need of them. Built
/// once, when the open is asked for, and not changed after.
#[derive(Clone)]
pub(crate) struct OpenRequest {
    pub(crate) paths: Vec<PathBuf>,
    pub(crate) options: OpenOptions,
    /// The first path's size on disk, for the loading screen; 0 for a URL.
    pub(crate) size: u64,
    /// The path recorded as a recent if the dataset installs: the first, as named,
    /// unless it is a local path that is not there.
    pub(crate) recent: Option<PathBuf>,
    /// What the loading screen names in place of the first path: a table inside a
    /// database.
    pub(crate) shown: Option<PathBuf>,
    /// Ask before reading more than this many bytes whole into memory
    /// (`[read] memory_warning`); `None` never asks.
    pub(crate) warn_in_memory_above: Option<u64>,
}

impl OpenRequest {
    /// The request for `paths`, asking the filesystem for the first one's size and
    /// whether it is there.
    ///
    /// Every open records a recent, not just those started from the home screen — most
    /// datasets are named on the command line, and those are exactly the ones worth
    /// getting back to. An object-store URL counts doubly: `s3://bucket/warehouse/events`
    /// is far more painful to retype than any local path, and it is recorded verbatim.
    /// Kept as named, since what is installed may be a download's temporary copy.
    ///
    /// A table inside a file of tables, as the home screen lists one (`app.db/users`,
    /// `run.npz/weights`), is the file opened with `--table`, and is recorded as the
    /// table.
    pub(crate) fn named(
        mut paths: Vec<PathBuf>,
        mut options: OpenOptions,
        formats: &crate::formats::Registry,
    ) -> Self {
        let first = paths[0].clone();
        let piped = stdin::is_stdin(&first);
        let local = !piped && matches!(source::input_source(&first), source::InputSource::Local(_));
        let mut table = None;
        if local
            && paths.len() == 1
            && let Some((db, name)) = crate::members::split(&first)
        {
            table = Some(first.clone());
            options.table = Some(name);
            paths = vec![db];
        } else if local
            && first.is_file()
            && let Some(name) = options.table.as_deref()
            && crate::members::holder(&first).is_some()
        {
            table = Some(crate::members::place(&first, name));
        } else if local
            && paths.len() == 1
            && options.table.is_none()
            && let Some((dir, split)) = crate::hf_splits::split_place(&first)
        {
            // A split of a Hugging Face cache, as the home screen lists it.
            table = Some(first.clone());
            options.table = Some(split);
            paths = vec![dir];
        } else if local
            && first.is_dir()
            && let Some(split) = options.table.as_deref()
            && crate::hf_splits::cache_splits(&first)
                .iter()
                .any(|s| s == split)
        {
            table = Some(first.join(split));
        } else if local
            && paths.len() == 1
            && options.table.is_none()
            && let Some((file, variant)) = crate::members::split_variant(&first, formats)
        {
            // A variant of a file a spec reads as several, as the home screen lists it.
            table = Some(first.clone());
            options.table = Some(variant);
            paths = vec![file];
        } else if local
            && let Some(variant) = options.table.as_deref()
            && first.is_file()
            && formats.variants_of(&first).is_some()
        {
            table = Some(crate::members::place(&first, variant));
        }
        let first = &paths[0];
        let size = if local {
            std::fs::metadata(first).map(|m| m.len()).unwrap_or(0)
        } else {
            0
        };
        // Standard input cannot be opened again from a list.
        let recent = table
            .clone()
            .or_else(|| (!piped && (!local || first.exists())).then(|| first.clone()));
        Self {
            paths,
            options,
            size,
            recent,
            shown: table,
            warn_in_memory_above: None,
        }
    }
}

/// Where an open stands. Each phase but the first rows is waiting on one worker, or on
/// the user.
pub(crate) enum Phase {
    /// Asked for, before its first step: the frame between a key and the open it asks
    /// for, or a startup open before its paths are looked at. Says what its caller says.
    Starting {
        label: String,
        percent: u16,
    },
    /// Whether the paths named on the command line are there, and which is a directory.
    LookingAtPaths,
    /// The spec a `--format` URL names, fetched before anything is read with it.
    ReadingSpec,
    /// What a directory named on the command line holds, before it is opened.
    LookingAtDirectory,
    /// A remote model's headers, read by range rather than downloaded.
    #[cfg(any(feature = "http", feature = "cloud"))]
    ReadingHeaders,
    /// The size of a remote file, to put the download to the user; `note` says why it
    /// is downloaded when it would not have been.
    #[cfg(any(feature = "http", feature = "cloud"))]
    CheckingSize {
        note: Option<&'static str>,
    },
    /// Waiting on the user to agree to read files whole into memory, past the size
    /// that asks first. Holds the generation meanwhile, once the app gives it a hold.
    ConfirmingRead {
        scan: Box<Scan>,
        _hold: Option<Hold>,
    },
    /// Waiting on the user to agree to the download. Holds the generation meanwhile:
    /// nothing is running, and the open is very much unfinished.
    #[cfg(any(feature = "http", feature = "cloud"))]
    Confirming {
        pending: Box<PendingDownload>,
        note: Option<&'static str>,
        _hold: Hold,
    },
    #[cfg(any(feature = "http", feature = "cloud"))]
    Downloading,
    /// Standard input being read to a file; `read` counts its bytes.
    Spooling {
        read: Arc<AtomicU64>,
    },
    Decompressing,
    /// A compressed file a format spec reads, being decompressed to a copy.
    DecompressingRecords,
    /// The decompressed copy's records, read through the spec.
    ReadingRecords,
    /// Files being converted to ones the dataset scans, `read` of their `total` bytes:
    /// Arrow IPC streams to one IPC file, or GPS logs to their table.
    Converting {
        what: Conversion,
        read: Arc<AtomicU64>,
        total: u64,
    },
    /// A CSV read with its string columns parsed.
    ScanningStrings,
    /// A CSV whose footer rows are dropped (`--footer-rows`): the scan counts every
    /// row of the file first, which is the wait.
    CountingFooter,
    /// The scan; `downloaded` when it reads a download rather than what was named.
    Scanning {
        downloaded: bool,
    },
    /// The scan of text read as lines, which indexes them.
    ReadingLines,
    /// The schema, and whatever the dataset needs before its first rows.
    ReadingSchema,
    /// Installed: its first rows are being read. The dataset is the one on screen, and
    /// what the load held is its now.
    FirstRows,
}

impl Phase {
    /// What the loading screen and the control bar call this phase, and the flat
    /// percentage the bar shows beside it (0 for none).
    pub(crate) fn label(&self) -> (&str, u16) {
        match self {
            Phase::Starting { label, percent } => (label, *percent),
            Phase::LookingAtPaths => ("Scanning input", 10),
            Phase::ReadingSpec => ("Reading spec", 5),
            Phase::LookingAtDirectory => (crate::App::LOOKING_AT_A_DIRECTORY, 5),
            #[cfg(any(feature = "http", feature = "cloud"))]
            Phase::ReadingHeaders => ("Reading headers", 20),
            #[cfg(any(feature = "http", feature = "cloud"))]
            Phase::CheckingSize { .. } | Phase::Confirming { .. } => ("Checking size", 0),
            #[cfg(any(feature = "http", feature = "cloud"))]
            Phase::Downloading => ("Downloading", 20),
            Phase::Spooling { .. } => ("Reading stdin", 5),
            Phase::ConfirmingRead { .. } => ("Scanning input", 0),
            Phase::Decompressing | Phase::DecompressingRecords => ("Decompressing", 30),
            Phase::ReadingRecords => ("Reading records", 35),
            Phase::Converting { what, read, total } => {
                let done = read.load(Ordering::Relaxed).min(*total);
                // Up to the scan or the schema read that follows.
                let share = (done * 20).checked_div(*total).unwrap_or(0);
                (what.label(), 10 + share as u16)
            }
            Phase::ScanningStrings => ("Scanning string columns", 55),
            Phase::CountingFooter => (COUNTING_FOOTER, 10),
            Phase::Scanning { downloaded: false } => ("Scanning input", 10),
            Phase::Scanning { downloaded: true } => ("Scanning", 30),
            Phase::ReadingLines => (READING_LINES, 10),
            Phase::ReadingSchema => ("Reading schema", 40),
            Phase::FirstRows => ("Loading buffer", 70),
        }
    }

    /// Whether the open waits on the user's answer to a question.
    fn asks(&self) -> bool {
        match self {
            Phase::ConfirmingRead { .. } => true,
            #[cfg(any(feature = "http", feature = "cloud"))]
            Phase::Confirming { .. } => true,
            _ => false,
        }
    }

    /// Whether this phase's worker answers with the dataset itself.
    fn builds_the_dataset(&self) -> bool {
        match self {
            Phase::ReadingSchema | Phase::Decompressing => true,
            #[cfg(any(feature = "http", feature = "cloud"))]
            Phase::ReadingHeaders => true,
            _ => false,
        }
    }

    /// Whether the open has not started any work of its own yet, so an open asked for
    /// now carries it on rather than replacing it: the `Open` a key or a look returns.
    fn starting(&self) -> bool {
        matches!(
            self,
            Phase::Starting { .. } | Phase::LookingAtPaths | Phase::LookingAtDirectory
        )
    }
}

/// A download a load fetched, and the URL it was fetched from: standard input's `-`
/// for what was piped in.
#[derive(Clone)]
struct Fetched {
    url: PathBuf,
    file: TempDownload,
    /// What the listing of a store's Arrow chose, read again with the file: each
    /// input's place, the split and the `--table` that chose it.
    arrow: Option<KeptArrow>,
}

#[derive(Clone)]
struct KeptArrow {
    parts: Arc<Vec<crate::ipc_stream::Part>>,
    splits: Option<Arc<crate::hf_splits::Splits>>,
    table: Option<String>,
}

impl Fetched {
    /// Whether an open with `options` reads what this download holds: not when it
    /// asks for another split.
    fn serves(&self, options: &OpenOptions) -> bool {
        self.arrow
            .as_ref()
            .is_none_or(|arrow| arrow.table == options.table)
    }
}

/// The open in flight.
pub(crate) struct Load {
    id: LoadId,
    /// Chosen on the home screen, which is where its failure is reported: the dataset
    /// left over from before is not what the user was looking at when they chose.
    from_home: bool,
    phase: Phase,
    /// What the screen names: the path as asked for (a URL, not the temporary file it
    /// landed in), and its size on disk.
    path: Option<PathBuf>,
    size: u64,
    /// The paths asked for, which `H` opens again; `None` for a frame handed over.
    paths: Option<Vec<PathBuf>>,
    recent: Option<PathBuf>,
    /// This load's footer counter, and through it its stop flag.
    progress: Arc<FooterProgress>,
    /// The stop flag again, for the workers that write files, and where they claim them.
    writer: Writer,
    download: Option<Fetched>,
    /// The IPC files its Arrow streams or GPS logs were converted to, which the
    /// dataset scans.
    converted: Vec<TempDownload>,
    /// See [`OpenRequest::warn_in_memory_above`].
    warn_in_memory_above: Option<u64>,
}

/// A scan held while the user is asked about it.
pub(crate) struct Scan {
    paths: Vec<PathBuf>,
    options: OpenOptions,
    display: Option<PathBuf>,
}

/// What a read whole into memory would take, put to the user before it starts.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct InMemory {
    /// The files' bytes on disk.
    pub(crate) bytes: u64,
    pub(crate) format: FileFormat,
    pub(crate) files: usize,
    /// The first file, as the loading screen names it.
    pub(crate) name: PathBuf,
}

impl Load {
    pub(crate) fn phase(&self) -> &Phase {
        &self.phase
    }

    /// The files the frame about to have its schema read is made from: the download,
    /// and what a conversion wrote.
    fn made(&self) -> Made {
        Made {
            download: self.download.as_ref().map(|fetched| fetched.file.clone()),
            converted: self.converted.clone(),
            ..Made::default()
        }
    }

    /// The path the screen names, if it names one.
    pub(crate) fn path(&self) -> Option<&Path> {
        self.path.as_deref()
    }

    /// The size the screen shows beside the path: while standard input is read, what
    /// has come in so far.
    pub(crate) fn size(&self) -> u64 {
        match &self.phase {
            Phase::Spooling { read } => read.load(Ordering::Relaxed),
            _ => self.size,
        }
    }
}

/// What the app does next for the open.
pub(crate) enum Step {
    /// Nothing: the answer was for an open nobody is waiting on, or the phase waits.
    Nothing,
    /// The open cannot be read at all, and the session ends saying why.
    Crash(String),
    /// Read the headers of the remote model at `url` as `format`, by range; `writer`
    /// carries the load's stop flag.
    #[cfg(any(feature = "http", feature = "cloud"))]
    ReadHeaders {
        url: PathBuf,
        format: FileFormat,
        options: OpenOptions,
        writer: Writer,
    },
    /// Find the remote file's size.
    #[cfg(any(feature = "http", feature = "cloud"))]
    Probe(PendingDownload),
    /// Ask the user whether to download it.
    #[cfg(any(feature = "http", feature = "cloud"))]
    Ask(PendingDownload),
    /// Ask the user whether to read files this large whole into memory.
    AskRead(InMemory),
    /// Download it, writing through `writer`: the load's stop flag, and its claim on
    /// the file for quitting to find.
    #[cfg(any(feature = "http", feature = "cloud"))]
    Download {
        pending: PendingDownload,
        writer: Writer,
    },
    /// Fetch the spec at `url`, asking `writer`'s stop flag before the request.
    FetchSpec {
        url: PathBuf,
        options: OpenOptions,
        writer: Writer,
    },
    /// Read standard input to a file through `writer`, counting its bytes in `read`.
    Spool {
        options: OpenOptions,
        writer: Writer,
        read: Arc<AtomicU64>,
    },
    /// Decompress the CSV in `file`, writing through `writer`; `path` names it on screen
    /// and in errors.
    Decompress {
        file: PathBuf,
        path: PathBuf,
        options: OpenOptions,
        writer: Writer,
        /// The download `file` is, given to the dataset built from it.
        download: Option<TempDownload>,
    },
    /// Decompress `file`, which `choice`'s spec reads, to a copy written through
    /// `writer`; `path` names it on screen and in errors.
    DecompressRecords {
        file: PathBuf,
        path: PathBuf,
        choice: crate::formats::Choice,
        options: OpenOptions,
        writer: Writer,
    },
    /// Read the records of `copy`, the decompressed file `path` names, with
    /// `choice`'s spec.
    ReadRecords {
        copy: PathBuf,
        path: PathBuf,
        choice: crate::formats::Choice,
        options: OpenOptions,
    },
    /// Convert `files` as `what` says into temporary IPC files written through
    /// `writer`, counting the bytes read in `read`; `path` names them on screen and in
    /// errors.
    Convert {
        what: Conversion,
        files: Vec<PathBuf>,
        path: Option<PathBuf>,
        options: OpenOptions,
        writer: Writer,
        read: Arc<AtomicU64>,
    },
    /// Scan `paths`, saying `status` on the control bar; `display` names the dataset when
    /// what is scanned is a download.
    Scan {
        paths: Vec<PathBuf>,
        options: OpenOptions,
        display: Option<PathBuf>,
        status: &'static str,
    },
    /// Read the scan's schema, reporting footers to `progress`.
    ReadSchema {
        lf: Box<LazyFrame>,
        path: Option<PathBuf>,
        options: OpenOptions,
        progress: Arc<FooterProgress>,
        /// What the open made the frame from, given to the dataset built from it.
        made: Made,
    },
    /// Install the dataset, then read its first rows.
    Install(Box<Loaded>),
    /// The open failed. Its load is retired.
    Failed(Failed),
    /// The open found a SQLite database of several tables and was asked for none: the
    /// home screen lists them. Its load is retired.
    Tables(Tables),
    /// The open found a local file no reader and no spec takes, or was asked for its
    /// bytes: the hex view shows it. Its load is retired.
    Hex(Hex),
}

/// A local file to show in the hex view.
#[derive(Debug)]
pub(crate) struct Hex {
    pub(crate) file: PathBuf,
    pub(crate) from_home: bool,
    /// Asked for (`--hex`) rather than fallen back to.
    pub(crate) asked: bool,
    /// `--hex-width`: the bytes a row holds.
    pub(crate) record_size: Option<usize>,
}

/// A file of several tables, to be listed on the home screen.
#[derive(Debug)]
pub(crate) struct Tables {
    /// The database file, as the user named it.
    pub(crate) database: PathBuf,
    pub(crate) from_home: bool,
}

/// What a conversion turns into files the dataset scans.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Conversion {
    /// Arrow IPC streams, into one IPC file; the IPC files among them stay put.
    Streams,
    /// GPS logs of this format, each into IPC files of its own, read as one table; or
    /// a VCD dump, FIX log or SDF file into its own.
    Text(FileFormat),
}

impl Conversion {
    /// What the loading screen calls it.
    pub(crate) fn label(self) -> &'static str {
        match self {
            Conversion::Streams => "Converting Arrow stream",
            Conversion::Text(format) => format.conversion().label,
        }
    }

    /// What the control bar says while it runs.
    pub(crate) fn status(self) -> &'static str {
        match self {
            Conversion::Streams => "Converting Arrow stream...",
            Conversion::Text(format) => format.conversion().status,
        }
    }
}

/// What a conversion wrote. Dropped unused, it removes its files.
pub(crate) enum Converted {
    /// The streams in one IPC file, and where each input's rows are: scanned next.
    Streams {
        file: TempDownload,
        parts: Vec<crate::ipc_stream::Part>,
    },
    /// The logs' IPC files and the frame over them, which only needs its schema read,
    /// and what reading them noticed.
    Frame {
        files: Vec<TempDownload>,
        lf: Box<LazyFrame>,
        notes: Vec<crate::notes::Note>,
        other_tables: Vec<String>,
        /// What the file says besides its rows, for the Info panel.
        detail: Option<Arc<crate::text_formats::Detail>>,
    },
}

/// The files an open made or fetched for the frame it scans, and what making them
/// found: handed to the dataset as it is built, which holds the files from then on.
#[derive(Default)]
pub(crate) struct Made {
    /// The download the frame reads.
    pub(crate) download: Option<TempDownload>,
    /// The files a conversion wrote.
    pub(crate) converted: Vec<TempDownload>,
    pub(crate) notes: Vec<crate::notes::Note>,
    pub(crate) other_tables: Vec<String>,
    pub(crate) detail: Option<Arc<crate::text_formats::Detail>>,
}

/// A failed open: why, and whether it was chosen on the home screen.
#[derive(Debug)]
pub(crate) struct Failed {
    pub(crate) message: String,
    pub(crate) from_home: bool,
}

/// A dataset read and ready to install, with everything the load hands over to it.
pub(crate) struct Loaded {
    pub(crate) state: DataTableState,
    /// What names the dataset: the path or URL asked for.
    pub(crate) path: Option<PathBuf>,
    pub(crate) options: OpenOptions,
    pub(crate) debug_label: Option<String>,
    /// The paths asked for, which `H` opens again; `None` for a frame handed over.
    pub(crate) paths: Option<Vec<PathBuf>>,
    /// Recorded as a recent once installed.
    pub(crate) recent: Option<PathBuf>,
    pub(crate) from_home: bool,
    /// The footer counter the dataset's own pass reports to from now on.
    pub(crate) footers: Arc<FooterProgress>,
}

/// What a phase's worker found.
pub(crate) enum LoadAnswer {
    /// The scan's frame; `path` names the dataset.
    Scanned {
        lf: Box<LazyFrame>,
        path: Option<PathBuf>,
        options: OpenOptions,
    },
    /// The scan found one compressed delimited file, `file`, to decompress first.
    Compressed {
        file: PathBuf,
        path: Option<PathBuf>,
        options: OpenOptions,
    },
    /// The spec a `--format` URL names, and the open's options to carry it in.
    SpecFetched {
        spec: Arc<crate::formats::Spec>,
        options: OpenOptions,
    },
    /// The scan found a compressed file, `file`, that `choice`'s spec reads once it is
    /// decompressed.
    CompressedRecords {
        file: PathBuf,
        path: Option<PathBuf>,
        choice: crate::formats::Choice,
        options: OpenOptions,
    },
    /// The decompressed copy of a file a spec reads. Dropped unused, it removes itself.
    DecompressedRecords {
        copy: TempDownload,
        path: PathBuf,
        choice: crate::formats::Choice,
        options: OpenOptions,
    },
    /// The scan found `files`, `bytes` in all as stored, which have to be converted as
    /// `what` says before they can be read: Arrow IPC streams, or GPS logs.
    Convert {
        what: Conversion,
        files: Vec<PathBuf>,
        bytes: u64,
        path: Option<PathBuf>,
        options: OpenOptions,
    },
    /// What the conversion wrote. Dropped unused, it removes its files.
    Converted {
        converted: Converted,
        path: Option<PathBuf>,
        options: OpenOptions,
    },
    /// The scan found a file of several tables (a SQLite database, a NumPy archive),
    /// `file`, its tables named `tables`, and no `--table` to say which.
    Tables {
        file: PathBuf,
        tables: Vec<String>,
        path: Option<PathBuf>,
    },
    /// The scan found a local file no reader and no spec takes, or `--hex` asked for
    /// its bytes.
    Hex {
        file: PathBuf,
        asked: bool,
        record_size: Option<usize>,
    },
    /// The dataset, its schema read.
    SchemaRead {
        state: Box<DataTableState>,
        path: Option<PathBuf>,
        options: OpenOptions,
        debug_label: Option<String>,
    },
    /// The remote model's server sends whole files, not ranges: it is downloaded.
    #[cfg(any(feature = "http", feature = "cloud"))]
    NoRanges { options: OpenOptions },
    /// The remote file's size.
    #[cfg(any(feature = "http", feature = "cloud"))]
    Sized(PendingDownload),
    /// A download started without asking passed its limit and was stopped, its
    /// partial file removed: the user is asked before it is fetched whole.
    #[cfg(any(feature = "http", feature = "cloud"))]
    PastLimit(PendingDownload),
    /// The remote file, downloaded. Dropped unused, it removes the file.
    #[cfg(any(feature = "http", feature = "cloud"))]
    Downloaded {
        download: TempDownload,
        options: OpenOptions,
    },
    /// Standard input, read to a file, and `options` with the format it holds.
    Spooled {
        download: TempDownload,
        options: OpenOptions,
    },
    /// Standard input, recorded to the file `--tee` named, which is read from here on
    /// as any file is.
    Recorded { file: PathBuf, options: OpenOptions },
}

/// A load put down before it finished: which, and what of it the app has to put down.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Retired {
    pub(crate) id: LoadId,
    /// It was asking the user about a download: the question goes with it.
    pub(crate) asking: bool,
}

/// The owner of the open in flight, and of the last download, kept to be read again.
#[derive(Default)]
pub(crate) struct Loader {
    next_id: u64,
    load: Option<Load>,
    /// The last remote file downloaded by an open that installed, kept so opening the
    /// same URL again reads it rather than downloading it again: how `H` re-reads a
    /// download. Let go when different data is opened; the dataset scanning it holds
    /// the file too, so it is removed once both have let go. Standard input, read once,
    /// is kept the same way.
    kept: Option<Fetched>,
    /// The files every load's workers have written and not yet let go, for quitting to
    /// remove. See [`crate::unfinished`].
    unfinished: Unfinished,
}

impl Loader {
    /// The files this loader's opens are writing, swept when the app exits.
    pub(crate) fn unfinished(&self) -> &Unfinished {
        &self.unfinished
    }

    /// The open in flight, if there is one.
    pub(crate) fn current(&self) -> Option<&Load> {
        self.load.as_ref()
    }

    pub(crate) fn id(&self) -> Option<LoadId> {
        self.load.as_ref().map(|load| load.id)
    }

    fn current_in(&self, id: LoadId, phase: impl Fn(&Phase) -> bool) -> bool {
        self.load
            .as_ref()
            .is_some_and(|load| load.id == id && phase(&load.phase))
    }

    /// Whether `id` is still looking at the paths named on the command line.
    pub(crate) fn looking_at_paths(&self, id: LoadId) -> bool {
        self.current_in(id, |phase| matches!(phase, Phase::LookingAtPaths))
    }

    /// Whether `id` is still looking at a directory named on the command line.
    pub(crate) fn looking_at_directory(&self, id: LoadId) -> bool {
        self.current_in(id, |phase| matches!(phase, Phase::LookingAtDirectory))
    }

    /// Whether an open is on its way and its dataset is not installed yet. Whatever
    /// table is up meanwhile belongs to the dataset being replaced.
    pub(crate) fn awaiting_dataset(&self) -> bool {
        self.load
            .as_ref()
            .is_some_and(|load| !matches!(load.phase, Phase::FirstRows))
    }

    /// Whether the user waits on the open: keys are held while it works. Not while it
    /// asks about a download, which the question's own keys answer, and not once the
    /// dataset is up, when the read of its first rows holds them.
    pub(crate) fn waits(&self) -> bool {
        self.awaiting_dataset() && !self.asking()
    }

    /// Whether the open is waiting on the user to agree to a download.
    pub(crate) fn asking(&self) -> bool {
        self.load.as_ref().is_some_and(|load| load.phase.asks())
    }

    /// Give the question being asked the hold that keeps the generation while it waits.
    pub(crate) fn hold_while_asking(&mut self, hold: Hold) {
        if let Some(Load {
            phase: Phase::ConfirmingRead { _hold, .. },
            ..
        }) = self.load.as_mut()
        {
            *_hold = Some(hold);
        }
    }

    /// Why the download being asked about is a download, when it would not have been.
    #[cfg(any(feature = "http", feature = "cloud"))]
    pub(crate) fn download_note(&self) -> Option<&'static str> {
        match self.load.as_ref().map(|load| &load.phase) {
            Some(Phase::Confirming { note, .. }) => *note,
            _ => None,
        }
    }

    /// The footer counter of the open that has not installed yet: what the loading
    /// screen counts.
    pub(crate) fn progress(&self) -> Option<&Arc<FooterProgress>> {
        self.load
            .as_ref()
            .filter(|load| !matches!(load.phase, Phase::FirstRows))
            .map(|load| &load.progress)
    }

    /// Put down the open in flight, if there is one, and what it holds. Its stop flag is
    /// raised unless its dataset is installed, when the counter is the dataset's.
    pub(crate) fn retire(&mut self) -> Option<Retired> {
        let load = self.load.take()?;
        if !matches!(load.phase, Phase::FirstRows) {
            load.progress.cancel();
        }
        Some(Retired {
            id: load.id,
            asking: load.phase.asks(),
        })
    }

    /// Make way for an open being asked for: the load in flight is retired, unless it
    /// has not started work of its own yet, in which case the open carries it on.
    pub(crate) fn make_way(&mut self) -> Option<Retired> {
        if self.load.as_ref().is_some_and(|load| load.phase.starting()) {
            return None;
        }
        self.retire()
    }

    /// The load an open being asked for belongs to: the starting one, or a new one.
    fn start(&mut self, from_home: bool) -> &mut Load {
        // A caller that did not make way would leave a load doing work behind with its
        // stop flag down; retired here so its download and its footer pass stop.
        if self
            .load
            .as_ref()
            .is_some_and(|load| !load.phase.starting())
        {
            debug_assert!(false, "an open started without making way");
            self.retire();
        }
        if self.load.is_none() {
            self.next_id = self.next_id.wrapping_add(1);
            let progress = Arc::<FooterProgress>::default();
            self.load = Some(Load {
                id: LoadId(self.next_id),
                from_home,
                phase: Phase::Starting {
                    label: "Loading".to_string(),
                    percent: 0,
                },
                path: None,
                size: 0,
                paths: None,
                recent: None,
                writer: self.unfinished.writer(progress.cancel_flag()),
                progress,
                download: None,
                converted: Vec::new(),
                warn_in_memory_above: None,
            });
        }
        let load = self.load.as_mut().expect("started just above");
        load.from_home |= from_home;
        load
    }

    /// An open is on its way, asked for from the home screen when `from_home`: the
    /// screen is handed over to it now, saying `label`.
    pub(crate) fn announce(&mut self, from_home: bool, label: String, percent: u16) -> LoadId {
        let load = self.start(from_home);
        load.phase = Phase::Starting { label, percent };
        load.id
    }

    /// An open whose dataset is up and whose first rows are being read, with nothing
    /// running: for tests of what ends that wait.
    #[cfg(test)]
    pub(crate) fn first_rows_for_tests(&mut self) {
        self.retire();
        self.start(false).phase = Phase::FirstRows;
    }

    /// Give the starting load's path a size, as an open asking the filesystem does.
    #[cfg(test)]
    pub(crate) fn size_for_tests(&mut self, size: u64) {
        if let Some(load) = self.load.as_mut() {
            load.size = size;
        }
    }

    /// Put `path` on the loading screen, so a wait says what it is waiting for.
    pub(crate) fn name(&mut self, path: PathBuf) {
        if let Some(load) = self
            .load
            .as_mut()
            .filter(|load| !matches!(load.phase, Phase::FirstRows))
        {
            load.path = Some(stdin::named(&path));
        }
    }

    /// The paths named on the command line are looked at before they are opened.
    pub(crate) fn look_at_paths(&mut self) -> LoadId {
        let load = self.start(false);
        load.phase = Phase::LookingAtPaths;
        load.id
    }

    /// A directory named on the command line is looked at before it is opened.
    pub(crate) fn look_at_directory(&mut self, dir: PathBuf) -> LoadId {
        let load = self.start(false);
        load.phase = Phase::LookingAtDirectory;
        load.path = Some(dir);
        load.id
    }

    /// Open `request`: carry on the starting load, or begin one. The last download is
    /// let go unless this opens it again.
    pub(crate) fn open(&mut self, request: OpenRequest) -> Step {
        if !(request.paths.len() == 1
            && self
                .kept
                .as_ref()
                .is_some_and(|kept| kept.url == request.paths[0]))
        {
            self.kept = None;
        }
        let OpenRequest {
            paths,
            mut options,
            size,
            recent,
            shown,
            warn_in_memory_above,
        } = request;
        // What an earlier load found of its Arrow is not this one's to read.
        options.arrow_parts = None;
        let prepared = options
            .prepared
            .take()
            .and_then(|handoff| handoff.lock().ok()?.take());
        let load = self.start(false);
        load.path = Some(shown.unwrap_or_else(|| stdin::named(&paths[0])));
        load.size = size;
        load.recent = recent;
        load.paths = Some(paths.clone());
        load.warn_in_memory_above = warn_in_memory_above;
        match prepared {
            Some(prepared) => self.install_prepared(*prepared),
            None => self.first_step(paths, options),
        }
    }

    /// Install the dataset the home screen's preview built: its scan, schema and first
    /// page are read already, so the open goes straight to its first rows, which are
    /// on hand.
    fn install_prepared(&mut self, prepared: crate::home_preview::Prepared) -> Step {
        let crate::home_preview::Prepared {
            state,
            options,
            debug_label,
            progress,
        } = prepared;
        let writer = self.unfinished.writer(progress.cancel_flag());
        let load = self.load.as_mut().expect("started by the open");
        load.phase = Phase::FirstRows;
        // The dataset was built reporting to the preview's counter, which is the one
        // its own footer pass goes on with.
        load.progress = progress;
        load.writer = writer;
        Step::Install(Box::new(Loaded {
            state: *state,
            path: load.path.clone(),
            options,
            debug_label,
            paths: load.paths.clone(),
            recent: load.recent.clone(),
            from_home: load.from_home,
            footers: load.progress.clone(),
        }))
    }

    /// Open a frame handed over (the Python binding's): only its schema is read.
    pub(crate) fn open_frame(&mut self, lf: LazyFrame, options: OpenOptions) -> Step {
        let load = self.start(false);
        load.path = None;
        load.size = 0;
        load.paths = None;
        load.recent = None;
        load.phase = Phase::ReadingSchema;
        Step::ReadSchema {
            lf: Box::new(lf),
            path: None,
            options,
            progress: load.progress.clone(),
            made: Made::default(),
        }
    }

    /// What the open of `paths` does first: read standard input, decompress, download,
    /// or scan.
    fn first_step(&mut self, paths: Vec<PathBuf>, options: OpenOptions) -> Step {
        let first = paths[0].clone();
        let src = source::input_source(&first);
        if paths.len() > 1 {
            let only_one = match &src {
                source::InputSource::S3(_) => {
                    Some("Only one S3 URL at a time. Open a single s3:// path.")
                }
                source::InputSource::Gcs(_) => {
                    Some("Only one GCS URL at a time. Open a single gs:// path.")
                }
                source::InputSource::Azure(_) => {
                    Some("Only one Azure URL at a time. Open a single abfss:// path.")
                }
                source::InputSource::Http(_) => {
                    Some("Only one HTTP/HTTPS URL at a time. Open a single URL.")
                }
                source::InputSource::Local(_) => None,
            };
            if let Some(message) = only_one {
                self.load = None;
                return Step::Crash(message.to_string());
            }
        }
        if let Some(message) = stdin::refuse(&paths, true) {
            self.load = None;
            return Step::Crash(message.to_string());
        }
        if options.follow
            && let Some(message) = crate::follow::refuse_paths(&paths, &options)
        {
            self.load = None;
            return Step::Crash(message);
        }
        // A remote spec is fetched once, before any phase reads with it; the open
        // then starts again from here with it in hand.
        if let Some(url) = options
            .spec_file
            .clone()
            .filter(|file| options.spec_fetched.is_none() && source::is_remote_url(file))
        {
            let load = self.load.as_mut().expect("an open has a load");
            load.phase = Phase::ReadingSpec;
            return Step::FetchSpec {
                url,
                options,
                writer: load.writer.clone(),
            };
        }
        if stdin::is_stdin(&first) {
            // Read once: opened again (`H`), the copy on hand is read.
            if let Some(kept) = self.kept.clone().filter(|kept| {
                kept.url == first && kept.file.path().exists() && kept.serves(&options)
            }) {
                return self.read_download(kept, options);
            }
            let load = self.load.as_mut().expect("an open has a load");
            let read = Arc::<AtomicU64>::default();
            load.phase = Phase::Spooling { read: read.clone() };
            return Step::Spool {
                options,
                writer: load.writer.clone(),
                read,
            };
        }
        let load = self.load.as_mut().expect("an open has a load");
        let compression = options
            .compression
            .or_else(|| CompressionFormat::from_extension(&first));
        let delimited = delimited_format(&first, &options);
        if matches!(src, source::InputSource::Local(_))
            && paths.len() == 1
            && compression.is_some()
            && let Some(format) = delimited
        {
            load.phase = Phase::Decompressing;
            return Step::Decompress {
                file: first.clone(),
                path: first,
                options: OpenOptions {
                    format: Some(format),
                    ..options
                },
                writer: load.writer.clone(),
                download: None,
            };
        }
        // Opened again, and downloaded already: read the copy on hand.
        #[cfg(any(feature = "http", feature = "cloud"))]
        if let Some(kept) = self
            .kept
            .clone()
            .filter(|kept| paths.len() == 1 && kept.file.path().exists() && kept.serves(&options))
        {
            return self.read_download(kept, options);
        }
        // A remote model's headers are all it needs: read by range, not downloaded.
        #[cfg(any(feature = "http", feature = "cloud"))]
        if paths.len() == 1
            && let Some(format) = crate::remote_model::model_format(&first, options.format)
        {
            let load = self.load.as_mut().expect("an open has a load");
            load.phase = Phase::ReadingHeaders;
            return Step::ReadHeaders {
                url: first,
                format,
                options,
                writer: load.writer.clone(),
            };
        }
        #[cfg(any(feature = "http", feature = "cloud"))]
        if let Some(pending) = remote_download(&src, &options) {
            let load = self.load.as_mut().expect("an open has a load");
            load.phase = Phase::CheckingSize { note: None };
            return Step::Probe(pending);
        }
        let load = self.load.as_mut().expect("an open has a load");
        if counts_footer(&paths, &options) {
            load.phase = Phase::CountingFooter;
            let display = load.path.clone().filter(|shown| *shown != paths[0]);
            return Step::Scan {
                paths,
                options,
                display,
                status: COUNTING_FOOTER_STATUS,
            };
        }
        if paths.len() == 1 && delimited.is_some() && options.parse_strings.is_some() {
            load.phase = Phase::ScanningStrings;
            return Step::Scan {
                paths,
                options,
                display: None,
                status: "Scanning string columns...",
            };
        }
        // A table inside a database goes by its path there, on screen and once open.
        let display = load.path.clone().filter(|shown| *shown != paths[0]);
        // A large file read whole is put to the user before the read starts.
        if let Some(limit) = load.warn_in_memory_above
            && let Some(read) = in_memory(&paths, &options)
            && read.bytes > limit
        {
            load.phase = Phase::ConfirmingRead {
                scan: Box::new(Scan {
                    paths,
                    options,
                    display,
                }),
                _hold: None,
            };
            return Step::AskRead(read);
        }
        if reads_lines(&paths, &options) {
            load.phase = Phase::ReadingLines;
            return Step::Scan {
                paths,
                options,
                display,
                status: READING_LINES_STATUS,
            };
        }
        load.phase = Phase::Scanning { downloaded: false };
        Step::Scan {
            paths,
            options,
            display,
            status: "Scanning input...",
        }
    }

    /// Read a download: decompress it first if it is compressed CSV, TSV or PSV, else
    /// scan it.
    /// Either way the dataset is named by the URL, not the temporary file, and what
    /// was piped in by `stdin`.
    fn read_download(&mut self, fetched: Fetched, mut options: OpenOptions) -> Step {
        if let Some(arrow) = &fetched.arrow {
            options.format = Some(FileFormat::Arrow);
            options.hive = false;
            options.arrow_parts = Some(arrow.parts.clone());
            options.splits = arrow.splits.clone();
        }
        let load = self.load.as_mut().expect("a download read has a load");
        let file = fetched.file.path().to_path_buf();
        let url = stdin::named(&fetched.url);
        let download = fetched.file.clone();
        load.download = Some(fetched);
        // A compressed CSV, TSV or PSV has to be decompressed before it can be scanned,
        // as it is when opened from disk; scanning the download directly read `.gz` as
        // a format and refused it.
        let compressed = options
            .compression
            .or_else(|| CompressionFormat::from_extension(&file))
            .is_some();
        if compressed && let Some(format) = delimited_format(&file, &options) {
            load.phase = Phase::Decompressing;
            return Step::Decompress {
                file,
                path: url,
                options: OpenOptions {
                    format: Some(format),
                    ..options
                },
                writer: load.writer.clone(),
                download: Some(download),
            };
        }
        let paths = vec![file];
        let status = if counts_footer(&paths, &options) {
            load.phase = Phase::CountingFooter;
            COUNTING_FOOTER_STATUS
        } else if reads_lines(&paths, &options) {
            load.phase = Phase::ReadingLines;
            READING_LINES_STATUS
        } else {
            load.phase = Phase::Scanning { downloaded: true };
            "Scanning..."
        };
        Step::Scan {
            paths,
            options,
            display: Some(url),
            status,
        }
    }

    /// A worker of `id` answered. Taken only while `id` is in flight and in the phase
    /// that asked; otherwise the answer, and whatever it carries, is dropped.
    pub(crate) fn answered(
        &mut self,
        id: LoadId,
        answer: LoadAnswer,
        #[cfg(any(feature = "http", feature = "cloud"))] jobs: &Jobs,
    ) -> Step {
        let Some(load) = self.load.as_mut().filter(|load| load.id == id) else {
            return Step::Nothing;
        };
        match (answer, &load.phase) {
            (
                LoadAnswer::Scanned { lf, path, options },
                Phase::Scanning { .. }
                | Phase::ScanningStrings
                | Phase::CountingFooter
                | Phase::ReadingRecords
                | Phase::ReadingLines,
            ) => {
                load.phase = Phase::ReadingSchema;
                Step::ReadSchema {
                    lf,
                    path,
                    options,
                    progress: load.progress.clone(),
                    made: load.made(),
                }
            }
            (
                LoadAnswer::Convert {
                    what,
                    files,
                    bytes,
                    path,
                    options,
                },
                Phase::Scanning { .. }
                | Phase::ScanningStrings
                | Phase::CountingFooter
                | Phase::ReadingLines,
            ) if load.converted.is_empty() => {
                let read = Arc::<AtomicU64>::default();
                load.phase = Phase::Converting {
                    what,
                    read: read.clone(),
                    total: bytes,
                };
                Step::Convert {
                    what,
                    files,
                    path,
                    options,
                    writer: load.writer.clone(),
                    read,
                }
            }
            (
                LoadAnswer::Converted {
                    converted: Converted::Streams { file, parts },
                    path,
                    options,
                },
                Phase::Converting { .. },
            ) => {
                let paths = vec![file.path().to_path_buf()];
                // A download or a pipe's spool converted is kept as its copy, to be read
                // again without converting, and the stream is let go rather than held
                // beside the copy for the session.
                if let Some(fetched) = load.download.as_mut() {
                    fetched.file = file.clone();
                }
                load.converted = vec![file];
                load.phase = Phase::Scanning { downloaded: true };
                Step::Scan {
                    paths,
                    options: OpenOptions {
                        format: Some(FileFormat::Arrow),
                        hive: false,
                        arrow_parts: Some(Arc::new(parts)),
                        ..options
                    },
                    display: path,
                    status: "Scanning...",
                }
            }
            (
                LoadAnswer::Converted {
                    converted:
                        Converted::Frame {
                            files,
                            lf,
                            notes,
                            other_tables,
                            detail,
                        },
                    path,
                    options,
                },
                Phase::Converting { .. },
            ) => {
                load.converted = files;
                load.phase = Phase::ReadingSchema;
                Step::ReadSchema {
                    lf,
                    path,
                    options,
                    progress: load.progress.clone(),
                    made: Made {
                        notes,
                        other_tables,
                        detail,
                        ..load.made()
                    },
                }
            }
            (
                LoadAnswer::Compressed {
                    file,
                    path,
                    options,
                },
                Phase::Scanning { .. }
                | Phase::ScanningStrings
                | Phase::CountingFooter
                | Phase::ReadingLines,
            ) => {
                load.phase = Phase::Decompressing;
                Step::Decompress {
                    path: path.unwrap_or_else(|| file.clone()),
                    file,
                    options,
                    writer: load.writer.clone(),
                    download: load.download.as_ref().map(|fetched| fetched.file.clone()),
                }
            }
            (LoadAnswer::SpecFetched { spec, options }, Phase::ReadingSpec) => {
                let paths = load.paths.clone().unwrap_or_default();
                if paths.is_empty() {
                    return Step::Nothing;
                }
                self.first_step(
                    paths,
                    OpenOptions {
                        spec_fetched: Some(spec),
                        ..options
                    },
                )
            }
            (
                LoadAnswer::CompressedRecords {
                    file,
                    path,
                    choice,
                    options,
                },
                Phase::Scanning { .. } | Phase::ScanningStrings | Phase::ReadingLines,
            ) => {
                load.phase = Phase::DecompressingRecords;
                Step::DecompressRecords {
                    path: path.unwrap_or_else(|| file.clone()),
                    file,
                    choice,
                    options,
                    writer: load.writer.clone(),
                }
            }
            (
                LoadAnswer::DecompressedRecords {
                    copy,
                    path,
                    choice,
                    options,
                },
                Phase::DecompressingRecords,
            ) => {
                // The load holds the copy until the dataset built from it does.
                let file = copy.path().to_path_buf();
                load.converted = vec![copy];
                load.phase = Phase::ReadingRecords;
                Step::ReadRecords {
                    copy: file,
                    path,
                    choice,
                    options,
                }
            }
            (
                LoadAnswer::Hex {
                    file,
                    asked,
                    record_size,
                },
                Phase::Scanning { .. }
                | Phase::ScanningStrings
                | Phase::CountingFooter
                | Phase::ReadingLines,
            ) => {
                let from_home = load.from_home;
                // A download is a temporary file the load owns; it has no bytes to show
                // once the load is put down.
                let fetched = load.download.is_some();
                let named = load.path.clone();
                self.retire();
                if fetched {
                    let message = match named {
                        Some(path) => crate::error_display::file_message(&path, crate::UNSUPPORTED),
                        None => crate::UNSUPPORTED.to_string(),
                    };
                    return Step::Failed(Failed { message, from_home });
                }
                Step::Hex(Hex {
                    file,
                    from_home,
                    asked,
                    record_size,
                })
            }
            (
                LoadAnswer::Tables { file, tables, path },
                Phase::Scanning { .. }
                | Phase::ScanningStrings
                | Phase::CountingFooter
                | Phase::ReadingLines,
            ) => {
                let from_home = load.from_home;
                let database = path.unwrap_or(file);
                // A download or standard input has no place on the home screen to list
                // the tables at: the table is named on the command line instead.
                let fetched = load.download.is_some();
                self.retire();
                if fetched {
                    let shown = tables.iter().take(20).cloned().collect::<Vec<_>>();
                    let more = tables.len().saturating_sub(shown.len());
                    let more = match more {
                        0 => String::new(),
                        n => format!(" and {n} more"),
                    };
                    return Step::Failed(Failed {
                        message: format!(
                            "{} holds {} tables: {}{more}. Open one with --table NAME.",
                            database.display(),
                            tables.len(),
                            shown.join(", ")
                        ),
                        from_home,
                    });
                }
                Step::Tables(Tables {
                    database,
                    from_home,
                })
            }
            (
                LoadAnswer::SchemaRead {
                    state,
                    path,
                    options,
                    debug_label,
                },
                phase,
            ) if phase.builds_the_dataset() => {
                load.phase = Phase::FirstRows;
                // The dataset was built holding its download (`Step::ReadSchema`); the
                // loader keeps it too, to be read again.
                if let Some(fetched) = load.download.take() {
                    self.kept = Some(fetched);
                }
                let state = *state;
                let load = self.load.as_ref().expect("installing its load");
                Step::Install(Box::new(Loaded {
                    state,
                    path,
                    options,
                    debug_label,
                    paths: load.paths.clone(),
                    recent: load.recent.clone(),
                    from_home: load.from_home,
                    footers: load.progress.clone(),
                }))
            }
            // Arrow in a store with no stream in it: nothing to download or ask about.
            #[cfg(feature = "cloud")]
            (
                LoadAnswer::Sized(PendingDownload::Arrow {
                    url,
                    objects,
                    options,
                    ..
                }),
                Phase::CheckingSize { .. },
            ) if crate::cloud_arrow::in_place(&objects).is_some() => {
                load.phase = Phase::Scanning { downloaded: false };
                let parts = crate::cloud_arrow::in_place(&objects).unwrap_or_default();
                Step::Scan {
                    paths: vec![PathBuf::from(url)],
                    options: OpenOptions {
                        format: Some(FileFormat::Arrow),
                        hive: false,
                        arrow_parts: Some(Arc::new(parts)),
                        ..options
                    },
                    display: None,
                    status: "Scanning...",
                }
            }
            #[cfg(any(feature = "http", feature = "cloud"))]
            (LoadAnswer::NoRanges { options }, Phase::ReadingHeaders) => {
                let first = load.paths.as_ref().and_then(|paths| paths.first().cloned());
                match first
                    .and_then(|first| remote_download(&source::input_source(&first), &options))
                {
                    Some(pending) => {
                        load.phase = Phase::CheckingSize {
                            note: Some(NO_RANGES),
                        };
                        Step::Probe(pending)
                    }
                    None => self.failed(id, NO_RANGES),
                }
            }
            // A small file of the built-in catalog: its row said what it is and what it
            // weighs, so it is fetched without a question.
            #[cfg(any(feature = "http", feature = "cloud"))]
            (LoadAnswer::Sized(pending), Phase::CheckingSize { note: None })
                if pending
                    .parts()
                    .2
                    .download_unasked
                    .is_some_and(|unasked| unasked.covers(pending.parts().1)) =>
            {
                load.phase = Phase::Downloading;
                Step::Download {
                    pending,
                    writer: load.writer.clone(),
                }
            }
            #[cfg(any(feature = "http", feature = "cloud"))]
            (LoadAnswer::Sized(pending), Phase::CheckingSize { note }) => {
                load.phase = Phase::Confirming {
                    pending: Box::new(pending.clone()),
                    note: *note,
                    _hold: jobs.hold(),
                };
                Step::Ask(pending)
            }
            // More arrived than the catalog said: asked once, as any large download is.
            // Its size is unknown now, whatever the server said.
            #[cfg(any(feature = "http", feature = "cloud"))]
            (LoadAnswer::PastLimit(pending), Phase::Downloading) => {
                let pending = pending.with_size(None);
                load.phase = Phase::Confirming {
                    pending: Box::new(pending.clone()),
                    note: Some(PAST_LIMIT),
                    _hold: jobs.hold(),
                };
                Step::Ask(pending)
            }
            #[cfg(any(feature = "http", feature = "cloud"))]
            (LoadAnswer::Downloaded { download, options }, Phase::Downloading) => {
                let fetched = Fetched {
                    // The URL the open was asked for, which is what opening it again names.
                    url: load.path.clone().unwrap_or_default(),
                    file: download,
                    arrow: options.arrow_parts.clone().map(|parts| KeptArrow {
                        parts,
                        splits: options.splits.clone(),
                        table: options.table.clone(),
                    }),
                };
                self.read_download(fetched, options)
            }
            (LoadAnswer::Recorded { file, options }, Phase::Spooling { read }) => {
                load.size = read.load(Ordering::Relaxed);
                // The user's file, not a copy: `H` reads it again, and it is a recent.
                self.kept = None;
                load.path = Some(file.clone());
                load.paths = Some(vec![file.clone()]);
                load.recent = Some(file.clone());
                let paths = vec![file];
                let status = if counts_footer(&paths, &options) {
                    load.phase = Phase::CountingFooter;
                    COUNTING_FOOTER_STATUS
                } else {
                    load.phase = Phase::Scanning { downloaded: false };
                    "Scanning input..."
                };
                Step::Scan {
                    paths,
                    options,
                    display: None,
                    status,
                }
            }
            (LoadAnswer::Spooled { download, options }, Phase::Spooling { read }) => {
                load.size = read.load(Ordering::Relaxed);
                let fetched = Fetched {
                    url: PathBuf::from(stdin::PATH),
                    file: download,
                    arrow: None,
                };
                self.read_download(fetched, options)
            }
            _ => Step::Nothing,
        }
    }

    /// A worker of `id` failed. The open ends there, with its reason, unless it is no
    /// longer the one in flight or its dataset is already up.
    pub(crate) fn failed(&mut self, id: LoadId, message: &str) -> Step {
        let Some(load) = self.load.as_ref().filter(|load| load.id == id) else {
            return Step::Nothing;
        };
        if matches!(load.phase, Phase::FirstRows) {
            return Step::Nothing;
        }
        let from_home = load.from_home;
        // A download is read from a temp file the user never typed: the reason names
        // the URL they did, or `stdin`.
        let mut message = match &load.download {
            Some(fetched) => crate::error_display::named_by_source(
                message,
                fetched.file.path(),
                &stdin::named(&fetched.url),
            ),
            None => message.to_string(),
        };
        // So are the IPC files a conversion wrote.
        if let Some(path) = &load.path {
            for converted in &load.converted {
                message = crate::error_display::named_by_source(&message, converted.path(), path);
            }
        }
        self.retire();
        Step::Failed(Failed { message, from_home })
    }

    /// The user agreed to what the open asked: let go of the hold, and fetch the
    /// download or start the read.
    pub(crate) fn confirmed(&mut self) -> Step {
        let Some(load) = self.load.as_mut().filter(|load| load.phase.asks()) else {
            return Step::Nothing;
        };
        // The hold goes as the phase changes: the job the caller starts next holds the
        // generation before anything else can look at it.
        match std::mem::replace(&mut load.phase, Phase::Scanning { downloaded: false }) {
            Phase::ConfirmingRead { scan, .. } => {
                let Scan {
                    paths,
                    options,
                    display,
                } = *scan;
                Step::Scan {
                    paths,
                    options,
                    display,
                    status: "Scanning input...",
                }
            }
            #[cfg(any(feature = "http", feature = "cloud"))]
            Phase::Confirming { pending, .. } => {
                load.phase = Phase::Downloading;
                Step::Download {
                    pending: pending.asked(),
                    writer: load.writer.clone(),
                }
            }
            _ => unreachable!("a phase that asks"),
        }
    }

    /// The first rows of the installed dataset are on screen, or will not be read: the
    /// open is done.
    pub(crate) fn first_rows_settled(&mut self) {
        if self
            .load
            .as_ref()
            .is_some_and(|load| matches!(load.phase, Phase::FirstRows))
        {
            self.load = None;
        }
    }
}

impl Drop for Loader {
    /// A download still running stops at its next chunk and removes its partial file.
    fn drop(&mut self) {
        self.retire();
    }
}

/// What the loading screen and the control bar say while text is read as lines.
const READING_LINES: &str = "Reading as lines";
const READING_LINES_STATUS: &str = "Reading as lines...";

/// Whether `paths` are read as lines, as `--format` or their names say: a name that
/// says text may still hold a format its bytes say (a candump `.log`), so this is
/// what the open expects, for its loading line.
fn reads_lines(paths: &[PathBuf], options: &OpenOptions) -> bool {
    options
        .format
        .or_else(|| match paths {
            [one] => FileFormat::from_path(one),
            _ => None,
        })
        .is_some_and(FileFormat::is_lines)
}

/// The delimited format (CSV, TSV, PSV) or text `path` is read as, if it is one: `--format`
/// when given, else the extension, looking through a compression suffix
/// (`x.tsv.gz` is TSV).
/// What reading `paths` would read whole into memory, by what their names and the
/// options say (the bytes are not looked at here, on the event thread): the local
/// files of a format read in memory, as [`FileFormat::read_mode`] says. `None` when
/// none is.
pub(crate) fn in_memory(paths: &[PathBuf], options: &OpenOptions) -> Option<InMemory> {
    let mut found: Option<InMemory> = None;
    for path in paths {
        if !matches!(source::input_source(path), source::InputSource::Local(_)) {
            continue;
        }
        let compression = options
            .compression
            .or_else(|| CompressionFormat::from_extension(path));
        // By name, or by the first bytes the open will judge it by: journal JSON
        // piped to a file with no name, or in a `.json` one.
        let format = options.format.or_else(|| match compression {
            Some(_) => path
                .file_stem()
                .and_then(|stem| FileFormat::from_path(Path::new(stem))),
            None => match FileFormat::from_path(path) {
                Some(named) => Some(crate::readers::refined(path, named).unwrap_or(named)),
                None => crate::readers::sniff_open(path, None),
            },
        });
        let Some(format) = format else {
            continue;
        };
        let stored = match compression {
            Some(_) => crate::Stored::Compressed {
                in_memory: options.decompress_in_memory,
            },
            None => crate::Stored::Plain,
        };
        // A model file's table comes from its header, which is what a URL of one is
        // read in place for: small however large the file.
        if format.read_mode(stored) != Some(crate::ReadMode::InMemory)
            || format.http_file() == crate::RemoteRead::InPlace
        {
            continue;
        }
        let Some(bytes) = std::fs::metadata(path)
            .ok()
            .filter(|m| m.is_file())
            .map(|m| m.len())
        else {
            continue;
        };
        match &mut found {
            Some(read) => {
                read.bytes += bytes;
                read.files += 1;
            }
            None => {
                found = Some(InMemory {
                    bytes,
                    format,
                    files: 1,
                    name: path.clone(),
                })
            }
        }
    }
    found
}

/// What the loading screen says while a CSV is counted to drop its footer rows.
const COUNTING_FOOTER: &str = "Counting rows to skip the footer";
/// The control bar's line for the same wait.
const COUNTING_FOOTER_STATUS: &str = "Counting rows to skip the footer...";

/// Whether the scan of `paths` counts every row first: delimited text whose footer
/// rows are dropped (`--footer-rows`).
fn counts_footer(paths: &[PathBuf], options: &OpenOptions) -> bool {
    options.skip_tail_rows.is_some_and(|n| n > 0)
        && paths.iter().all(|p| delimited_format(p, options).is_some())
}

pub(crate) fn delimited_format(path: &Path, options: &OpenOptions) -> Option<FileFormat> {
    let format = options.format.or_else(|| {
        FileFormat::from_path(path).or_else(|| {
            CompressionFormat::from_extension(path)?;
            FileFormat::from_path(Path::new(path.file_stem()?))
        })
    })?;
    format.decompressed_once().then_some(format)
}

/// Why a catalog file that started without a question asks partway.
#[cfg(any(feature = "http", feature = "cloud"))]
pub(crate) const PAST_LIMIT: &str =
    "This catalog file passed 50 MB, more than it may download without asking.";

/// Why a remote model is downloaded rather than read by its headers.
#[cfg(any(feature = "http", feature = "cloud"))]
pub(crate) const NO_RANGES: &str = "The server does not send byte ranges, so the model's header cannot be read without downloading the whole file.";

/// The download a remote source needs before it can be read, if it needs one: an HTTP
/// file always, and one object of a store that cannot be scanned in place. A format
/// spec reads local bytes, so one object it reads is downloaded whatever its name.
#[cfg(any(feature = "http", feature = "cloud"))]
fn remote_download(src: &source::InputSource, options: &OpenOptions) -> Option<PendingDownload> {
    #[cfg(feature = "cloud")]
    let spec = options.spec_file.is_some() || options.spec_name.is_some();
    #[cfg(feature = "cloud")]
    let should_download =
        |url: &str| should_download(url) || (spec && !source::is_prefix_or_glob(url));
    #[cfg(feature = "cloud")]
    if !spec && let Some(arrow) = cloud_arrow(src, options) {
        return Some(arrow);
    }
    let options = options.clone();
    match src {
        #[cfg(feature = "http")]
        source::InputSource::Http(url) => Some(PendingDownload::Http {
            url: url.clone(),
            size: None,
            options,
        }),
        #[cfg(feature = "cloud")]
        source::InputSource::S3(url) => {
            let full = format!("s3://{url}");
            should_download(&full).then_some(PendingDownload::S3 {
                url: full,
                size: None,
                options,
            })
        }
        #[cfg(feature = "cloud")]
        source::InputSource::Gcs(url) => {
            let full = format!("gs://{url}");
            should_download(&full).then_some(PendingDownload::Gcs {
                url: full,
                size: None,
                options,
            })
        }
        #[cfg(feature = "cloud")]
        source::InputSource::Azure(url) => should_download(url).then(|| PendingDownload::Azure {
            url: url.clone(),
            size: None,
            options,
        }),
        _ => None,
    }
}

/// Arrow in a store, one object or a prefix the listing said holds it, but not a glob:
/// the probe lists it and tells its streams, which are downloaded, from its IPC files,
/// which are scanned in place.
#[cfg(feature = "cloud")]
fn cloud_arrow(src: &source::InputSource, options: &OpenOptions) -> Option<PendingDownload> {
    let url = match src {
        source::InputSource::S3(url) => format!("s3://{url}"),
        source::InputSource::Gcs(url) => format!("gs://{url}"),
        source::InputSource::Azure(url) => url.clone(),
        _ => return None,
    };
    if url.contains('*') {
        return None;
    }
    let arrow = if url.ends_with('/') {
        options.format == Some(FileFormat::Arrow)
    } else {
        // Compressed, it is a download like any other compressed object.
        options.compression.is_none()
            && options
                .format
                .or_else(|| FileFormat::from_path(Path::new(&url)))
                == Some(FileFormat::Arrow)
    };
    arrow.then(|| PendingDownload::Arrow {
        url,
        objects: Vec::new(),
        size: None,
        options: options.clone(),
    })
}

#[cfg(feature = "cloud")]
fn should_download(url: &str) -> bool {
    let (_, ext) = source::url_path_extension(url);
    source::cloud_path_should_download(ext.as_deref(), source::is_prefix_or_glob(url))
}

#[cfg(test)]
mod tests {
    use super::*;
    use polars::prelude::IntoLazy;

    /// An open routes a remote file as `FileFormat::bucket_object` and `http_file` say,
    /// which the format table in `docs/formats/index.md` and the home screen's marker
    /// read: read in place, or downloaded first. A bucket's Arrow is listed first, and read in place
    /// unless the listing finds a stream; a compressed object is downloaded.
    #[cfg(all(feature = "http", feature = "cloud"))]
    #[test]
    fn remote_files_are_routed_as_their_format_says() {
        use crate::{FileFormat, RemoteRead, Stored};
        let in_place = |url: &str, stream: bool| {
            let path = Path::new(url);
            crate::remote_model::model_format(path, None).is_some()
                || match remote_download(&source::input_source(path), &OpenOptions::default()) {
                    None => true,
                    Some(PendingDownload::Arrow { .. }) => {
                        let object = crate::cloud_arrow::Object {
                            url: url.to_string(),
                            size: 10,
                            stream,
                        };
                        crate::cloud_arrow::in_place(&[object]).is_some()
                    }
                    Some(_) => false,
                }
        };
        let mut seen = Vec::new();
        for ext in [
            "parquet",
            "csv",
            "tsv",
            "psv",
            "json",
            "jsonl",
            "arrow",
            "avro",
            "orc",
            "xlsx",
            "safetensors",
            "gguf",
            "nmea",
            "gpx",
            "wav",
            "mid",
            "db",
            "vcd",
            "sdf",
            "log",
            "npy",
            "elf",
            "ulg",
        ] {
            let format = FileFormat::from_extension(ext).expect(ext);
            seen.push(format);
            let https = format!("https://example.com/d/x.{ext}");
            assert_eq!(
                in_place(&https, false),
                format.http_file() == RemoteRead::InPlace,
                "{https}"
            );
            for url in [format!("s3://b/d/x.{ext}"), format!("gs://b/d/x.{ext}")] {
                let said = |stored| format.bucket_object(stored) == RemoteRead::InPlace;
                assert_eq!(in_place(&url, false), said(Stored::Plain), "{url}");
                if format == FileFormat::Arrow {
                    assert_eq!(in_place(&url, true), said(Stored::Stream), "{url}: stream");
                }
                let gz = format!("{url}.gz");
                let compressed = Stored::Compressed { in_memory: false };
                assert_eq!(in_place(&gz, false), said(compressed), "{gz}");
            }
        }
        // A FIX log has no extension, so no URL names it: it is found by its content
        // once downloaded, as `http_file` and `bucket_object` say for it.
        assert_eq!(FileFormat::Fix.http_file(), RemoteRead::Downloaded);
        assert_eq!(
            FileFormat::Fix.bucket_object(Stored::Plain),
            RemoteRead::Downloaded
        );
        seen.push(FileFormat::Fix);
        // A DataFlash log is a `.bin`, found by its first bytes, as a FIX log is.
        assert_eq!(FileFormat::Dataflash.http_file(), RemoteRead::Downloaded);
        assert_eq!(
            FileFormat::Dataflash.bucket_object(Stored::Plain),
            RemoteRead::Downloaded
        );
        seen.push(FileFormat::Dataflash);
        // So is a candump log, whatever its name.
        assert_eq!(FileFormat::Candump.http_file(), RemoteRead::Downloaded);
        assert_eq!(
            FileFormat::Candump.bucket_object(Stored::Plain),
            RemoteRead::Downloaded
        );
        seen.push(FileFormat::Candump);
        // And journal JSON, from a file or a pipe.
        assert_eq!(FileFormat::Journal.http_file(), RemoteRead::Downloaded);
        assert_eq!(
            FileFormat::Journal.bucket_object(Stored::Plain),
            RemoteRead::Downloaded
        );
        seen.push(FileFormat::Journal);
        for f in FileFormat::ALL {
            assert!(seen.contains(&f), "{} is checked", f.name());
        }
    }

    fn frame() -> LazyFrame {
        polars::df!("a" => [1i32, 2, 3]).unwrap().lazy()
    }

    fn state() -> Box<DataTableState> {
        Box::new(DataTableState::from_lazyframe(frame(), &OpenOptions::default()).unwrap())
    }

    fn request(path: &str) -> OpenRequest {
        OpenRequest {
            paths: vec![PathBuf::from(path)],
            options: OpenOptions::default(),
            size: 7,
            recent: Some(PathBuf::from(path)),
            shown: None,
            warn_in_memory_above: None,
        }
    }

    #[cfg(any(feature = "http", feature = "cloud"))]
    fn jobs() -> Jobs {
        Jobs::new(std::sync::mpsc::channel().0)
    }

    /// `answered`, with the job owner the download question needs.
    fn answer(loader: &mut Loader, id: LoadId, answer: LoadAnswer) -> Step {
        loader.answered(
            id,
            answer,
            #[cfg(any(feature = "http", feature = "cloud"))]
            &jobs(),
        )
    }

    fn scanned(path: &str) -> LoadAnswer {
        LoadAnswer::Scanned {
            lf: Box::new(frame()),
            path: Some(PathBuf::from(path)),
            options: OpenOptions::default(),
        }
    }

    fn schema_read(path: &str) -> LoadAnswer {
        LoadAnswer::SchemaRead {
            state: state(),
            path: Some(PathBuf::from(path)),
            options: OpenOptions::default(),
            debug_label: None,
        }
    }

    /// A local file goes scan, schema, install, first rows, done; each step says what to
    /// run, and the screen says each phase in turn.
    #[test]
    fn a_local_open_runs_its_phases_in_order() {
        let mut loader = Loader::default();
        let step = loader.open(request("data.parquet"));
        let id = loader.id().expect("a load");
        assert!(matches!(
            step,
            Step::Scan { ref paths, display: None, status: "Scanning input...", .. }
                if paths == &[PathBuf::from("data.parquet")]
        ));
        let load = loader.current().unwrap();
        assert_eq!(load.phase().label(), ("Scanning input", 10));
        assert_eq!(load.path(), Some(Path::new("data.parquet")));
        assert_eq!(load.size(), 7);
        assert!(loader.awaiting_dataset() && loader.waits());

        let Step::ReadSchema { progress, .. } = answer(&mut loader, id, scanned("data.parquet"))
        else {
            panic!("the scan goes on to the schema");
        };
        assert!(Arc::ptr_eq(&progress, loader.progress().unwrap()));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading schema", 40)
        );

        let Step::Install(loaded) = answer(&mut loader, id, schema_read("data.parquet")) else {
            panic!("the schema goes on to the install");
        };
        assert_eq!(loaded.path.as_deref(), Some(Path::new("data.parquet")));
        assert_eq!(
            loaded.paths.as_deref(),
            Some(&[PathBuf::from("data.parquet")][..])
        );
        assert_eq!(loaded.recent.as_deref(), Some(Path::new("data.parquet")));
        assert!(!loaded.from_home);
        assert!(
            Arc::ptr_eq(&loaded.footers, &progress),
            "the dataset takes the load's footer counter"
        );
        assert!(!loader.awaiting_dataset(), "the dataset is up");
        assert!(!loader.waits(), "the read of its rows holds the keys");
        assert!(loader.progress().is_none(), "the counter is the dataset's");
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Loading buffer", 70)
        );

        loader.first_rows_settled();
        assert!(loader.current().is_none(), "the open is done");
    }

    /// Arrow IPC streams found by the scan are converted, counting their bytes, and the
    /// IPC file they become is scanned and held by the dataset, named by what was asked
    /// for; a failure names that too, and putting the load down lets the file go.
    #[test]
    fn streams_are_converted_then_scanned() {
        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let _ = loader.open(request("cache.arrow"));
        let id = loader.id().unwrap();
        let Step::Convert {
            files, path, read, ..
        } = answer(
            &mut loader,
            id,
            LoadAnswer::Convert {
                what: Conversion::Streams,
                files: vec![PathBuf::from("cache.arrow")],
                bytes: 200,
                path: Some(PathBuf::from("cache.arrow")),
                options: OpenOptions::default(),
            },
        )
        else {
            panic!("the streams are converted");
        };
        assert_eq!(files, [PathBuf::from("cache.arrow")]);
        assert_eq!(path.as_deref(), Some(Path::new("cache.arrow")));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Converting Arrow stream", 10)
        );
        read.store(100, Ordering::Relaxed);
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Converting Arrow stream", 20),
            "the bar moves with the bytes read"
        );
        assert!(loader.waits());

        let converted =
            TempDownload::keep(TempDownload::create(Some(dir.path()), Some("arrow")).unwrap());
        let temp = converted.path().to_path_buf();
        let Step::Scan {
            paths,
            options,
            display,
            ..
        } = answer(
            &mut loader,
            id,
            LoadAnswer::Converted {
                converted: Converted::Streams {
                    file: converted,
                    parts: Vec::new(),
                },
                path: Some(PathBuf::from("cache.arrow")),
                options: OpenOptions::default(),
            },
        )
        else {
            panic!("the IPC file is scanned");
        };
        assert_eq!(paths, std::slice::from_ref(&temp));
        assert_eq!(options.format, Some(FileFormat::Arrow));
        assert_eq!(display.as_deref(), Some(Path::new("cache.arrow")));
        assert!(
            matches!(
                answer(
                    &mut loader,
                    id,
                    LoadAnswer::Convert {
                        what: Conversion::Streams,
                        files: vec![temp.clone()],
                        bytes: 1,
                        path: None,
                        options: OpenOptions::default(),
                    },
                ),
                Step::Nothing
            ),
            "converted once"
        );
        let Step::ReadSchema { made, .. } = answer(&mut loader, id, scanned("cache.arrow")) else {
            panic!("the schema is read");
        };
        assert!(made.download.is_none(), "nothing was downloaded");
        assert_eq!(
            made.converted
                .iter()
                .map(|d| d.path().to_path_buf())
                .collect::<Vec<_>>(),
            std::slice::from_ref(&temp),
            "the dataset holds the converted file"
        );
        drop(made);
        let Step::Failed(failed) = loader.failed(id, &format!("could not read {}", temp.display()))
        else {
            panic!("the open fails");
        };
        assert_eq!(failed.message, "could not read cache.arrow");
        assert!(!temp.exists(), "the retired load let the file go");
    }

    /// An answer from a load that is no longer in flight, or from a phase it has left,
    /// changes nothing: no install, no new phase, no title.
    #[test]
    fn an_old_load_s_answers_change_nothing() {
        let mut loader = Loader::default();
        let _ = loader.open(request("first.csv"));
        let first = loader.id().unwrap();
        assert!(loader.make_way().is_some(), "the first is doing work");
        let _ = loader.open(request("second.csv"));
        let second = loader.id().unwrap();
        assert_ne!(first, second);

        assert!(matches!(
            answer(&mut loader, first, scanned("first.csv")),
            Step::Nothing
        ));
        assert!(matches!(
            answer(&mut loader, first, schema_read("first.csv")),
            Step::Nothing
        ));
        assert!(matches!(loader.failed(first, "gone"), Step::Nothing));
        assert_eq!(loader.id(), Some(second));
        let load = loader.current().unwrap();
        assert_eq!(load.path(), Some(Path::new("second.csv")));
        assert_eq!(load.phase().label(), ("Scanning input", 10));

        // An answer for a phase the load is not in is dropped too: a schema before the
        // scan has answered.
        assert!(matches!(
            answer(&mut loader, second, schema_read("second.csv")),
            Step::Nothing
        ));
        assert!(loader.awaiting_dataset());
    }

    /// A conversion put down mid-way is told to stop through its writer, and its file,
    /// answered late or for a phase the load is not in, is dropped and so removed.
    #[test]
    fn a_converted_file_nobody_wants_is_removed() {
        let dir = tempfile::tempdir().unwrap();
        let converted = || {
            let file =
                TempDownload::keep(TempDownload::create(Some(dir.path()), Some("arrow")).unwrap());
            let path = file.path().to_path_buf();
            (
                LoadAnswer::Converted {
                    converted: Converted::Streams {
                        file,
                        parts: Vec::new(),
                    },
                    path: Some(PathBuf::from("cache.arrow")),
                    options: OpenOptions::default(),
                },
                path,
            )
        };
        let mut loader = Loader::default();
        let _ = loader.open(request("cache.arrow"));
        let first = loader.id().unwrap();

        let (early, early_path) = converted();
        assert!(matches!(answer(&mut loader, first, early), Step::Nothing));
        assert!(!early_path.exists(), "not converting yet");

        let Step::Convert { writer, .. } = answer(
            &mut loader,
            first,
            LoadAnswer::Convert {
                what: Conversion::Streams,
                files: vec![PathBuf::from("cache.arrow")],
                bytes: 1,
                path: Some(PathBuf::from("cache.arrow")),
                options: OpenOptions::default(),
            },
        ) else {
            panic!("the streams are converted");
        };
        assert!(!writer.stopped());
        assert!(loader.make_way().is_some(), "the conversion is work");
        assert!(writer.stopped(), "Ctrl+O or another open stops it");

        let _ = loader.open(request("other.csv"));
        let (late, late_path) = converted();
        assert!(matches!(answer(&mut loader, first, late), Step::Nothing));
        assert!(!late_path.exists(), "the replaced load's file");
        assert_eq!(
            loader.current().unwrap().path(),
            Some(Path::new("other.csv"))
        );
    }

    /// Opening something new replaces a load doing work, and stops it: its footer pass
    /// and its download read the stop flag.
    #[test]
    fn a_superseding_open_stops_the_one_it_replaces() {
        let mut loader = Loader::default();
        let _ = loader.open(request("big"));
        let id = loader.id().unwrap();
        let Step::ReadSchema { progress, .. } = answer(&mut loader, id, scanned("big")) else {
            panic!("reading the schema");
        };
        let retired = loader.make_way().expect("the load is replaced");
        assert!(!retired.asking);
        assert!(progress.is_cancelled(), "its pass stops issuing reads");
        assert!(loader.current().is_none());
    }

    /// The frame between a key and its open, and the look at named paths, are carried on
    /// by the open they lead to: one load, one identity, and the origin it was asked from.
    #[test]
    fn an_open_carries_on_the_load_that_announced_it() {
        let mut loader = Loader::default();
        let announced = loader.announce(true, "Scanning input".to_string(), 10);
        loader.name(PathBuf::from("chosen.csv"));
        assert_eq!(
            loader.current().unwrap().path(),
            Some(Path::new("chosen.csv"))
        );
        assert!(loader.waits(), "the keys wait from the key that asked");
        assert!(loader.make_way().is_none(), "nothing to replace yet");
        let _ = loader.open(request("chosen.csv"));
        assert_eq!(loader.id(), Some(announced));

        let Step::Install(loaded) = ({
            let _ = answer(&mut loader, announced, scanned("chosen.csv"));
            answer(&mut loader, announced, schema_read("chosen.csv"))
        }) else {
            panic!("installs");
        };
        assert!(loaded.from_home, "a failure would be reported at home");

        // A look at named paths, then the directory it found, then the open of it.
        let mut loader = Loader::default();
        let look = loader.look_at_paths();
        assert!(loader.looking_at_paths(look));
        assert!(loader.make_way().is_none());
        let directory = loader.look_at_directory(PathBuf::from("dir"));
        assert_eq!(directory, look);
        assert!(!loader.looking_at_paths(look), "that look is answered");
        assert!(loader.looking_at_directory(look));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            (crate::App::LOOKING_AT_A_DIRECTORY, 5)
        );
        let _ = loader.open(request("dir"));
        assert_eq!(loader.id(), Some(look));
        assert!(
            !loader.looking_at_directory(look),
            "nor is a late look taken"
        );
    }

    /// A worker's failure ends its load with the reason and where it was asked from, and
    /// retires it; one from a load already installed, or gone, is not the open's.
    #[test]
    fn a_failed_phase_retires_its_load() {
        let mut loader = Loader::default();
        let id = loader.announce(true, "Scanning input".to_string(), 10);
        let _ = loader.open(request("broken.parquet"));
        let Step::ReadSchema { progress, .. } = answer(&mut loader, id, scanned("broken.parquet"))
        else {
            panic!("reading the schema");
        };
        let Step::Failed(failed) = loader.failed(id, "not parquet") else {
            panic!("the open fails");
        };
        assert_eq!(failed.message, "not parquet");
        assert!(failed.from_home);
        assert!(loader.current().is_none());
        assert!(progress.is_cancelled());
        assert!(matches!(loader.failed(id, "again"), Step::Nothing));

        let id = {
            let _ = loader.open(request("good.parquet"));
            loader.id().unwrap()
        };
        let _ = answer(&mut loader, id, scanned("good.parquet"));
        let Step::Install(loaded) = answer(&mut loader, id, schema_read("good.parquet")) else {
            panic!("installs");
        };
        assert!(
            matches!(loader.failed(id, "the rows"), Step::Nothing),
            "the rows' failure is the table's"
        );
        assert!(!loaded.footers.is_cancelled());
        assert!(
            loader.retire().is_some(),
            "going home puts down the read of the first rows"
        );
        assert!(
            !loaded.footers.is_cancelled(),
            "but not the dataset's footer pass"
        );
    }

    /// A file read whole into memory past the size that asks is put to the user before
    /// its scan; agreeing scans it, and one in memory under the size, or read lazily,
    /// is scanned without asking.
    #[test]
    fn a_large_read_into_memory_is_asked_about_first() {
        let dir = tempfile::tempdir().unwrap();
        let json = dir.path().join("big.json");
        std::fs::write(&json, "[{\"a\": 1}]").unwrap();
        let csv = dir.path().join("big.csv");
        std::fs::write(&csv, "a\n1\n").unwrap();
        let open = |loader: &mut Loader, path: &Path, limit: u64| {
            loader.open(OpenRequest {
                warn_in_memory_above: Some(limit),
                ..request(&path.to_string_lossy())
            })
        };
        let mut loader = Loader::default();
        let Step::AskRead(read) = open(&mut loader, &json, 4) else {
            panic!("a JSON file past the size is asked about");
        };
        assert_eq!(read.format, FileFormat::Json);
        assert_eq!((read.bytes, read.files), (10, 1));
        assert!(loader.asking() && loader.awaiting_dataset() && !loader.waits());
        assert!(matches!(loader.confirmed(), Step::Scan { ref paths, .. } if paths[0] == json));
        assert!(!loader.asking());
        let mut loader = Loader::default();
        assert!(matches!(open(&mut loader, &json, 10), Step::Scan { .. }));
        let mut loader = Loader::default();
        assert!(matches!(open(&mut loader, &csv, 0), Step::Scan { .. }));
        let mut loader = Loader::default();
        let _ = open(&mut loader, &json, 0);
        assert!(loader.retire().is_some_and(|retired| retired.asking));
    }

    /// Journal JSON is read whole too, and asked about by what its bytes say, named or
    /// not.
    #[test]
    fn a_large_journal_is_asked_about_first() {
        let dir = tempfile::tempdir().unwrap();
        let entry = "{\"__CURSOR\":\"s=1\",\"__REALTIME_TIMESTAMP\":\"1\",\"MESSAGE\":\"m\"}\n";
        for name in ["journal", "journal.json"] {
            let path = dir.path().join(name);
            std::fs::write(&path, entry).unwrap();
            let mut loader = Loader::default();
            let step = loader.open(OpenRequest {
                warn_in_memory_above: Some(4),
                ..request(&path.to_string_lossy())
            });
            let Step::AskRead(read) = step else {
                panic!("{name}: a journal past the size is asked about");
            };
            assert_eq!(read.format, FileFormat::Journal, "{name}");
        }
    }

    /// A CSV whose footer rows are dropped counts the file in its scan, and says so;
    /// a file of another format, or with no footer to drop, scans as ever.
    #[test]
    fn a_footer_to_drop_says_the_file_is_counted() {
        let footer = |n| OpenOptions {
            skip_tail_rows: Some(n),
            parse_strings: Some(crate::ParseStringsTarget::All),
            ..OpenOptions::default()
        };
        let mut loader = Loader::default();
        let step = loader.open(OpenRequest {
            options: footer(2),
            ..request("vendor.csv")
        });
        assert!(
            matches!(step, Step::Scan { status, .. } if status == COUNTING_FOOTER_STATUS),
            "{:?}",
            loader.current().map(|l| l.phase().label())
        );
        assert_eq!(
            loader.current().unwrap().phase().label(),
            (COUNTING_FOOTER, 10)
        );
        for (path, n) in [("vendor.csv", 0), ("vendor.parquet", 2)] {
            let mut loader = Loader::default();
            let step = loader.open(OpenRequest {
                options: footer(n),
                ..request(path)
            });
            assert!(
                matches!(step, Step::Scan { status, .. } if status != COUNTING_FOOTER_STATUS),
                "{path}"
            );
        }
    }

    /// Several URLs at once cannot be read, and say so.
    #[test]
    fn several_urls_end_the_session() {
        let mut loader = Loader::default();
        let step = loader.open(OpenRequest {
            paths: vec![PathBuf::from("s3://a/x.csv"), PathBuf::from("s3://a/y.csv")],
            options: OpenOptions::default(),
            size: 0,
            recent: None,
            shown: None,
            warn_in_memory_above: None,
        });
        assert!(matches!(step, Step::Crash(message) if message.contains("S3")));
        assert!(loader.current().is_none());
    }

    /// A compressed TSV or PSV is decompressed as a CSV is, told its format so it is
    /// split on its own separator (#567); the format comes from `--format`, else the
    /// name under the compression suffix.
    #[test]
    fn a_compressed_delimited_file_is_decompressed_as_its_format() {
        let opts = |format: Option<FileFormat>| OpenOptions {
            format,
            ..OpenOptions::default()
        };
        for (name, format, expected) in [
            ("x.tsv.gz", None, Some(FileFormat::Tsv)),
            ("x.PSV.xz", None, Some(FileFormat::Psv)),
            ("x.csv.zst", None, Some(FileFormat::Csv)),
            ("x.tsv", None, Some(FileFormat::Tsv)),
            ("x.gz", Some(FileFormat::Tsv), Some(FileFormat::Tsv)),
            ("x.csv.gz", Some(FileFormat::Psv), Some(FileFormat::Psv)),
            ("x.json.gz", None, None),
            ("x.gz", None, None),
            ("x.tsv.gz", Some(FileFormat::Parquet), None),
        ] {
            assert_eq!(
                delimited_format(Path::new(name), &opts(format)),
                expected,
                "{name} {format:?}"
            );
        }

        let mut loader = Loader::default();
        assert!(matches!(
            loader.open(request("logs.tsv.gz")),
            Step::Decompress { ref options, .. } if options.format == Some(FileFormat::Tsv)
        ));
    }

    /// A scan that finds one compressed delimited file, as a directory of one does, hands
    /// it to the decompress step under the name the open was asked for (#576); the
    /// answer of a load in another phase changes nothing.
    #[test]
    fn a_compressed_file_the_scan_found_is_decompressed() {
        let compressed = || LoadAnswer::Compressed {
            file: PathBuf::from("dir/data.tsv.gz"),
            path: Some(PathBuf::from("dir")),
            options: OpenOptions {
                format: Some(FileFormat::Tsv),
                ..OpenOptions::default()
            },
        };
        let mut loader = Loader::default();
        assert!(matches!(loader.open(request("dir")), Step::Scan { .. }));
        let id = loader.id().unwrap();
        assert!(matches!(
            answer(&mut loader, id, compressed()),
            Step::Decompress { ref file, ref path, ref options, download: None, .. }
                if file == Path::new("dir/data.tsv.gz")
                    && path == Path::new("dir")
                    && options.format == Some(FileFormat::Tsv)
        ));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Decompressing", 30)
        );
        assert!(matches!(
            answer(&mut loader, id, compressed()),
            Step::Nothing
        ));
        assert!(matches!(
            answer(&mut loader, id, schema_read("dir")),
            Step::Install(_)
        ));
    }

    /// A format spec reads local bytes: one object it reads is downloaded first, whatever
    /// its name says, and a prefix or a glob goes on to the scan, which refuses it.
    #[cfg(feature = "cloud")]
    #[test]
    fn a_remote_object_a_spec_reads_is_downloaded_first() {
        let asked = [
            OpenOptions {
                spec_name: Some("vendor.feed".into()),
                ..OpenOptions::default()
            },
            OpenOptions {
                spec_file: Some(PathBuf::from("feed.toml")),
                ..OpenOptions::default()
            },
        ];
        let open = |url: &str, options: &OpenOptions| {
            Loader::default().open(OpenRequest {
                options: options.clone(),
                ..request(url)
            })
        };
        for options in &asked {
            for url in [
                "s3://b/day",
                "s3://b/day.parquet",
                "gs://b/day.arrow",
                "abfss://c@acct.dfs.core.windows.net/day.l2",
            ] {
                assert!(
                    matches!(open(url, options), Step::Probe(_)),
                    "{url} is downloaded"
                );
            }
            for url in ["s3://b/days/", "gs://b/days/*.l2"] {
                assert!(
                    matches!(open(url, options), Step::Scan { .. }),
                    "{url} goes on to the scan"
                );
            }
        }
        // Without a spec, Parquet is still read in place.
        assert!(matches!(
            open("s3://b/day.parquet", &OpenOptions::default()),
            Step::Scan { .. }
        ));
    }

    /// `--format` naming a URL: the spec is fetched first, in a phase of its own, then the
    /// open goes on as it would have, carrying the spec; a second answer does nothing.
    #[test]
    fn a_remote_spec_is_fetched_before_the_open_goes_on() {
        let spec = Arc::new(
            crate::formats::Spec::parse(
                "name = \"t.rec\"\n[records]\nfields = [{ name = \"v\", type = \"u1\" }]\n",
                None,
            )
            .unwrap(),
        );
        let options = OpenOptions {
            spec_file: Some(PathBuf::from("https://example.com/t.toml")),
            ..OpenOptions::default()
        };
        let mut loader = Loader::default();
        let Step::FetchSpec { url, options, .. } = loader.open(OpenRequest {
            options,
            ..request("day.rec")
        }) else {
            panic!("the spec is fetched first");
        };
        assert_eq!(url, Path::new("https://example.com/t.toml"));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading spec", 5)
        );
        let id = loader.id().unwrap();
        let fetched = || LoadAnswer::SpecFetched {
            spec: spec.clone(),
            options: options.clone(),
        };
        let Step::Scan { paths, options, .. } = answer(&mut loader, id, fetched()) else {
            panic!("the open goes on to its scan");
        };
        assert_eq!(paths, [PathBuf::from("day.rec")]);
        assert!(options.spec_fetched.is_some_and(|s| s.name == "t.rec"));
        assert!(matches!(answer(&mut loader, id, fetched()), Step::Nothing));
    }

    /// A compressed file a spec reads is decompressed in a phase of its own, which a
    /// retired load stops, then its copy's records are read in another; the copy goes
    /// to the dataset with the frame, and a second answer for a phase left does nothing.
    #[test]
    fn a_compressed_file_a_spec_reads_is_decompressed_then_its_records_read() {
        let spec = crate::formats::Spec::parse(
            "name = \"t.rec\"\n[records]\nfields = [{ name = \"v\", type = \"u1\" }]\n",
            None,
        )
        .unwrap();
        let choice = crate::formats::Choice {
            spec: Arc::new(spec),
            by: crate::formats::Chosen::Glob,
            also: Vec::new(),
        };
        let compressed = || LoadAnswer::CompressedRecords {
            file: PathBuf::from("day.rec.zst"),
            path: None,
            choice: choice.clone(),
            options: OpenOptions::default(),
        };
        let mut loader = Loader::default();
        let _ = loader.open(request("day.rec.zst"));
        let id = loader.id().unwrap();
        let Step::DecompressRecords {
            file, path, writer, ..
        } = answer(&mut loader, id, compressed())
        else {
            panic!("the file is decompressed");
        };
        assert_eq!(
            (file.as_path(), path.as_path()),
            (Path::new("day.rec.zst"), Path::new("day.rec.zst"))
        );
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Decompressing", 30)
        );
        assert!(loader.waits());
        assert!(matches!(
            answer(&mut loader, id, compressed()),
            Step::Nothing
        ));
        loader.retire();
        assert!(writer.stopped(), "Esc or another open stops the copy");

        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let _ = loader.open(request("day.rec.zst"));
        let id = loader.id().unwrap();
        let _ = answer(&mut loader, id, compressed());
        let copy = TempDownload::keep(TempDownload::create(Some(dir.path()), None).unwrap());
        let temp = copy.path().to_path_buf();
        let decompressed = || LoadAnswer::DecompressedRecords {
            copy: copy.clone(),
            path: PathBuf::from("day.rec.zst"),
            choice: choice.clone(),
            options: OpenOptions::default(),
        };
        let Step::ReadRecords { copy: read, .. } = answer(&mut loader, id, decompressed()) else {
            panic!("the copy's records are read");
        };
        assert_eq!(read, temp);
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading records", 35)
        );
        assert!(matches!(
            answer(&mut loader, id, decompressed()),
            Step::Nothing
        ));
        let Step::ReadSchema { made, .. } = answer(&mut loader, id, scanned("day.rec.zst")) else {
            panic!("the frame's schema is read");
        };
        assert_eq!(made.converted.len(), 1, "the dataset holds the copy");
        drop((made, copy));
        loader.retire();
        assert!(!temp.exists(), "the retired load let the copy go");
    }

    /// GPS logs the scan found are converted under the name the open was asked for;
    /// a second answer for the phase it left changes nothing; the frame the conversion
    /// built has its schema read holding its files and notes, and installs; putting the
    /// load down mid-conversion stops the writer, so its files go.
    #[test]
    fn gps_logs_the_scan_found_are_converted() {
        let convert = || LoadAnswer::Convert {
            what: Conversion::Text(FileFormat::Nmea),
            files: vec![PathBuf::from("logs/a.nmea"), PathBuf::from("logs/b.nmea")],
            bytes: 200,
            path: Some(PathBuf::from("logs")),
            options: OpenOptions {
                format: Some(FileFormat::Nmea),
                ..OpenOptions::default()
            },
        };
        let mut loader = Loader::default();
        let _ = loader.open(request("logs"));
        let id = loader.id().unwrap();
        let Step::Convert {
            what,
            files,
            writer,
            read,
            ..
        } = answer(&mut loader, id, convert())
        else {
            panic!("the logs are converted");
        };
        assert_eq!(what, Conversion::Text(FileFormat::Nmea));
        assert_eq!(what.status(), "Reading NMEA...");
        assert_eq!(files.len(), 2);
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading NMEA log", 10)
        );
        read.store(100, Ordering::Relaxed);
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading NMEA log", 20),
            "the bar moves with the bytes read"
        );
        assert!(loader.waits());
        assert!(matches!(answer(&mut loader, id, convert()), Step::Nothing));
        assert!(!writer.stopped());
        loader.retire();
        assert!(
            writer.stopped(),
            "Ctrl+O or another open stops the conversion"
        );

        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let _ = loader.open(request("logs"));
        let id = loader.id().unwrap();
        assert!(matches!(
            answer(&mut loader, id, convert()),
            Step::Convert { .. }
        ));
        let file =
            TempDownload::keep(TempDownload::create(Some(dir.path()), Some("arrow")).unwrap());
        let temp = file.path().to_path_buf();
        let note = crate::notes::Note {
            summary: "1 line left out: not NMEA".to_string(),
            scope: "of 9 lines in 2 logs".to_string(),
            read_as_text: None,
            passed_over: None,
        };
        let Step::ReadSchema { made, path, .. } = answer(
            &mut loader,
            id,
            LoadAnswer::Converted {
                converted: Converted::Frame {
                    files: vec![file],
                    lf: Box::new(frame()),
                    notes: vec![note],
                    other_tables: vec!["GSV 3".to_string()],
                    detail: None,
                },
                path: Some(PathBuf::from("logs")),
                options: OpenOptions::default(),
            },
        ) else {
            panic!("the frame's schema is read");
        };
        assert_eq!(path.as_deref(), Some(Path::new("logs")));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading schema", 40)
        );
        assert_eq!(made.converted.len(), 1);
        assert_eq!(made.notes.len(), 1);
        assert_eq!(made.other_tables, ["GSV 3"]);
        drop(made);
        let Step::Failed(failed) = loader.failed(id, &format!("could not read {}", temp.display()))
        else {
            panic!("the open fails");
        };
        assert_eq!(failed.message, "could not read logs", "named as asked for");
        assert!(!temp.exists(), "the retired load let the files go");
    }

    /// A database of several tables, opened without `--table`, puts the load down and
    /// lands on its tables, where it was asked from; a second answer changes nothing.
    /// Piped in, there is no place to list them, and the open fails naming them.
    #[test]
    fn a_database_of_several_tables_lands_on_them() {
        let tables = |file: &str| LoadAnswer::Tables {
            file: PathBuf::from(file),
            tables: vec!["users".to_string(), "orders".to_string()],
            path: Some(PathBuf::from(file)),
        };
        let mut loader = Loader::default();
        let _ = loader.open(request("shop.db"));
        let id = loader.id().unwrap();
        let Step::Tables(landed) = answer(&mut loader, id, tables("shop.db")) else {
            panic!("the tables are listed");
        };
        assert_eq!(landed.database, Path::new("shop.db"));
        assert!(!landed.from_home);
        assert!(loader.current().is_none(), "the load is put down");
        assert!(matches!(
            answer(&mut loader, id, tables("shop.db")),
            Step::Nothing
        ));

        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let Step::Spool { .. } = loader.open(OpenRequest::named(
            vec![PathBuf::from("-")],
            OpenOptions::default(),
            &Default::default(),
        )) else {
            panic!("standard input is read first");
        };
        let id = loader.id().unwrap();
        let spool = TempDownload::keep(TempDownload::create(Some(dir.path()), None).unwrap());
        let options = OpenOptions {
            format: Some(FileFormat::Sqlite),
            ..Default::default()
        };
        let Step::Scan { paths, .. } = answer(
            &mut loader,
            id,
            LoadAnswer::Spooled {
                download: spool,
                options,
            },
        ) else {
            panic!("the spool is scanned");
        };
        let Step::Failed(failed) = answer(
            &mut loader,
            id,
            LoadAnswer::Tables {
                file: paths[0].clone(),
                tables: vec!["users".to_string(), "orders".to_string()],
                path: Some(PathBuf::from("stdin")),
            },
        ) else {
            panic!("piped in, the table has to be named");
        };
        assert_eq!(
            failed.message,
            "stdin holds 2 tables: users, orders. Open one with --table NAME."
        );
    }

    /// A table named by its path inside a database is the database opened with
    /// `--table`, recorded and shown as the table.
    #[test]
    fn a_path_inside_a_database_opens_the_database() {
        let dir = tempfile::tempdir().unwrap();
        let db = dir.path().join("shop.db");
        let mut header = crate::sqlite::MAGIC.to_vec();
        header.resize(512, 0);
        std::fs::write(&db, header).unwrap();
        let request = OpenRequest::named(
            vec![db.join("orders")],
            OpenOptions::default(),
            &Default::default(),
        );
        assert_eq!(request.paths, std::slice::from_ref(&db));
        assert_eq!(request.options.table.as_deref(), Some("orders"));
        assert_eq!(request.recent, Some(db.join("orders")));
        assert_eq!(request.shown, Some(db.join("orders")));

        let named = OpenRequest::named(
            vec![db.clone()],
            OpenOptions {
                table: Some("users".to_string()),
                ..Default::default()
            },
            &Default::default(),
        );
        assert_eq!(named.paths, std::slice::from_ref(&db));
        assert_eq!(
            named.recent,
            Some(db.join("users")),
            "--table is recorded too"
        );

        let plain = OpenRequest::named(
            vec![db.clone()],
            OpenOptions::default(),
            &Default::default(),
        );
        assert_eq!(plain.recent, Some(db));
        assert_eq!(plain.shown, None);
    }

    /// A local compressed CSV is decompressed rather than scanned; a CSV read with its
    /// strings parsed scans saying so; a frame handed over goes straight to its schema
    /// and has no path to open again.
    #[test]
    fn the_first_step_follows_what_was_asked_for() {
        let mut loader = Loader::default();
        assert!(matches!(
            loader.open(request("logs.csv.gz")),
            Step::Decompress { ref file, ref path, .. }
                if file == Path::new("logs.csv.gz") && path == Path::new("logs.csv.gz")
        ));
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Decompressing", 30)
        );
        let id = loader.id().unwrap();
        assert!(matches!(
            answer(&mut loader, id, schema_read("logs.csv.gz")),
            Step::Install(_)
        ));

        let mut loader = Loader::default();
        let mut parsed = request("table.csv");
        parsed.options.parse_strings = Some(crate::ParseStringsTarget::All);
        assert!(matches!(
            loader.open(parsed),
            Step::Scan {
                status: "Scanning string columns...",
                ..
            }
        ));

        let mut loader = Loader::default();
        let Step::ReadSchema { path: None, .. } =
            loader.open_frame(frame(), OpenOptions::default())
        else {
            panic!("a frame's schema is read");
        };
        let id = loader.id().unwrap();
        let Step::Install(loaded) = answer(
            &mut loader,
            id,
            LoadAnswer::SchemaRead {
                state: state(),
                path: None,
                options: OpenOptions::default(),
                debug_label: None,
            },
        ) else {
            panic!("installs");
        };
        assert!(loaded.paths.is_none() && loaded.recent.is_none());
    }

    fn downloaded(dir: &Path, body: &str, extension: &str) -> TempDownload {
        use std::io::Write;
        let mut file = TempDownload::create(Some(dir), Some(extension)).unwrap();
        file.write_all(body.as_bytes()).unwrap();
        TempDownload::keep(file)
    }

    /// A remote model's headers are read by range, and that worker's dataset installs
    /// under the URL. A server without byte ranges sends it to the download question,
    /// which says why.
    #[cfg(feature = "http")]
    #[test]
    fn a_remote_model_reads_its_headers_or_falls_back_to_the_download() {
        let url = "https://example.com/m/model.safetensors";
        let jobs = jobs();
        let mut loader = Loader::default();
        let Step::ReadHeaders {
            url: read, format, ..
        } = loader.open(request(url))
        else {
            panic!("the headers are read, not downloaded");
        };
        assert_eq!(
            (read.as_path(), format),
            (Path::new(url), FileFormat::Safetensors)
        );
        let id = loader.id().unwrap();
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Reading headers", 20)
        );
        assert!(loader.waits());
        let Step::Install(loaded) = loader.answered(id, schema_read(url), &jobs) else {
            panic!("the headers are the dataset");
        };
        assert_eq!(loaded.recent.as_deref(), Some(Path::new(url)));

        let mut loader = Loader::default();
        let _ = loader.open(request(url));
        let id = loader.id().unwrap();
        let Step::Probe(pending) = loader.answered(
            id,
            LoadAnswer::NoRanges {
                options: OpenOptions::default(),
            },
            &jobs,
        ) else {
            panic!("no ranges: the file is sized for its download");
        };
        assert_eq!(pending.parts().0, url);
        assert_eq!(loader.download_note(), None, "not asked yet");
        assert!(matches!(
            loader.answered(id, LoadAnswer::Sized(pending), &jobs),
            Step::Ask(_)
        ));
        assert_eq!(loader.download_note(), Some(NO_RANGES));
        // An answer for a phase it has left changes nothing.
        assert!(matches!(
            loader.answered(id, schema_read(url), &jobs),
            Step::Nothing
        ));
    }

    /// Arrow in a bucket, a prefix the listing said holds it or one object, is listed
    /// first, not sent to the Parquet scan; a prefix of another format, or a glob, is
    /// scanned in place. A listing of IPC files only is scanned in place, unasked.
    #[cfg(feature = "cloud")]
    #[test]
    fn arrow_in_a_bucket_is_listed_first() {
        let open = |url: &str, format: Option<FileFormat>| {
            let mut loader = Loader::default();
            let mut request = request(url);
            request.options.format = format;
            request.options.hive = url.ends_with('/');
            let step = loader.open(request);
            (loader, step)
        };
        for url in ["s3://lake/hf/", "gs://lake/hf/", "s3://lake/one.arrow"] {
            let (_, step) = open(url, url.ends_with('/').then_some(FileFormat::Arrow));
            let Step::Probe(PendingDownload::Arrow { url: listed, .. }) = step else {
                panic!("{url}: listed first");
            };
            assert_eq!(listed, url);
        }
        assert!(matches!(
            open("s3://lake/hf/", Some(FileFormat::Csv)).1,
            Step::Scan { .. }
        ));
        assert!(matches!(
            open("s3://lake/hf/*.arrow", Some(FileFormat::Arrow)).1,
            Step::Scan { .. }
        ));

        let object = |name: &str, stream: bool| crate::cloud_arrow::Object {
            url: format!("s3://lake/hf/{name}"),
            size: 10,
            stream,
        };
        let jobs = jobs();
        let (mut loader, _) = open("s3://lake/hf/", Some(FileFormat::Arrow));
        let id = loader.id().unwrap();
        let listed = |objects| {
            LoadAnswer::Sized(PendingDownload::Arrow {
                url: "s3://lake/hf/".to_string(),
                objects,
                size: Some(0),
                options: OpenOptions::default(),
            })
        };
        let Step::Scan { paths, options, .. } = loader.answered(
            id,
            listed(vec![object("a.arrow", false), object("b.arrow", false)]),
            &jobs,
        ) else {
            panic!("IPC files only: scanned in place");
        };
        assert_eq!(paths, [PathBuf::from("s3://lake/hf/")]);
        assert_eq!(
            options.arrow_parts.as_deref(),
            Some(&vec![
                crate::ipc_stream::Part::InPlace(PathBuf::from("s3://lake/hf/a.arrow")),
                crate::ipc_stream::Part::InPlace(PathBuf::from("s3://lake/hf/b.arrow")),
            ])
        );
        let (mut loader, _) = open("s3://lake/hf/", Some(FileFormat::Arrow));
        let id = loader.id().unwrap();
        assert!(matches!(
            loader.answered(
                id,
                listed(vec![object("a.arrow", false), object("s.arrow", true)]),
                &jobs
            ),
            Step::Ask(PendingDownload::Arrow { .. })
        ));
    }

    /// A small file of the built-in catalog is downloaded as soon as its size is known,
    /// with no question; a large one, one the server and the catalog say nothing of,
    /// and any other URL are asked about (#547 M5).
    #[cfg(feature = "http")]
    #[test]
    fn a_small_catalog_file_downloads_without_asking() {
        let url = "https://example.com/penguins.csv";
        let jobs = jobs();
        let unasked = |listed| crate::UnaskedDownload {
            limit: 1_000,
            listed,
        };
        let ask = |options: Option<crate::UnaskedDownload>, size: Option<u64>| {
            let mut loader = Loader::default();
            let mut request = request(url);
            request.options.download_unasked = options;
            let Step::Probe(pending) = loader.open(request) else {
                panic!("the size is asked first");
            };
            let id = loader.id().unwrap();
            let step = loader.answered(id, LoadAnswer::Sized(pending.with_size(size)), &jobs);
            (matches!(step, Step::Download { .. }), loader)
        };
        let (downloads, loader) = ask(Some(unasked(None)), Some(800));
        assert!(downloads, "under the limit: no question");
        assert!(!loader.asking());
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Downloading", 20)
        );
        assert!(
            ask(Some(unasked(Some(800))), None).0,
            "the catalog's size stands in"
        );
        assert!(
            !ask(Some(unasked(Some(800))), Some(5_000)).0,
            "the server's size wins"
        );
        assert!(!ask(Some(unasked(None)), None).0, "nothing says how big");
        assert!(!ask(None, Some(10)).0, "a URL from anywhere else asks");
    }

    /// A catalog file fetched without a question that runs past its limit is asked
    /// about once, its size unknown; agreed to, it downloads with no limit, and a
    /// large catalog file asked about up front is not asked about again either.
    #[cfg(feature = "http")]
    #[test]
    fn an_unasked_download_past_its_limit_asks_once() {
        let url = "https://example.com/penguins.csv";
        let jobs = jobs();
        let unasked = Some(crate::UnaskedDownload {
            limit: 1_000,
            listed: Some(800),
        });
        let mut loader = Loader::default();
        let mut asked_for = request(url);
        asked_for.options.download_unasked = unasked;
        let Step::Probe(pending) = loader.open(asked_for) else {
            panic!("the size is asked first");
        };
        let id = loader.id().unwrap();
        let Step::Download { pending, .. } =
            loader.answered(id, LoadAnswer::Sized(pending.with_size(None)), &jobs)
        else {
            panic!("small by the catalog: no question");
        };
        assert!(
            pending.parts().2.download_unasked.is_some(),
            "it has a limit"
        );

        let Step::Ask(asked) = loader.answered(id, LoadAnswer::PastLimit(pending), &jobs) else {
            panic!("past the limit, it asks");
        };
        assert_eq!(asked.parts().1, None, "its size is not what anyone said");
        assert_eq!(loader.download_note(), Some(PAST_LIMIT));
        assert!(jobs.would_strand(), "the generation is held while it asks");
        let Step::Download { pending, .. } = loader.confirmed() else {
            panic!("agreed to, it downloads");
        };
        assert!(pending.parts().2.download_unasked.is_none(), "asked once");

        // Large by the server: asked up front, and not again.
        let mut loader = Loader::default();
        let mut asked_for = request(url);
        asked_for.options.download_unasked = unasked;
        let Step::Probe(pending) = loader.open(asked_for) else {
            panic!("the size is asked first");
        };
        let id = loader.id().unwrap();
        let step = loader.answered(id, LoadAnswer::Sized(pending.with_size(Some(5_000))), &jobs);
        assert!(matches!(step, Step::Ask(_)));
        let Step::Download { pending, .. } = loader.confirmed() else {
            panic!("agreed to, it downloads");
        };
        assert!(pending.parts().2.download_unasked.is_none());
    }

    #[cfg(any(feature = "http", feature = "cloud"))]
    #[test]
    fn the_past_limit_note_names_the_limit() {
        assert_eq!(crate::UnaskedDownload::LIMIT, 50 * 1024 * 1024);
        assert!(PAST_LIMIT.contains("50 MB"));
    }

    /// A remote file is sized, put to the user, downloaded, then scanned under its URL;
    /// the installed dataset holds the file, and opening the URL again reads that copy
    /// rather than asking again.
    #[cfg(feature = "http")]
    #[test]
    fn a_remote_file_is_asked_about_then_downloaded_and_kept() {
        let url = "https://example.com/data.csv";
        let dir = tempfile::tempdir().unwrap();
        let jobs = jobs();
        let mut loader = Loader::default();
        let Step::Probe(pending) = loader.open(request(url)) else {
            panic!("the size is asked first");
        };
        let id = loader.id().unwrap();
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Checking size", 0)
        );

        let sized = pending.with_size(Some(42));
        assert!(matches!(
            loader.answered(id, LoadAnswer::Sized(sized), &jobs),
            Step::Ask(ref p) if p.parts().1 == Some(42)
        ));
        assert!(loader.asking() && loader.awaiting_dataset());
        assert!(!loader.waits(), "the question has the keys");
        assert!(jobs.would_strand(), "the generation is held while it asks");

        let Step::Download { .. } = loader.confirmed() else {
            panic!("agreed to, it downloads");
        };
        assert!(!jobs.would_strand(), "the hold goes with the question");
        assert!(matches!(loader.confirmed(), Step::Nothing), "asked once");
        assert_eq!(
            loader.current().unwrap().phase().label(),
            ("Downloading", 20)
        );

        let file = downloaded(dir.path(), "a\n1\n", "csv");
        let at = file.path().to_path_buf();
        let Step::Scan {
            paths,
            display,
            status,
            ..
        } = loader.answered(
            id,
            LoadAnswer::Downloaded {
                download: file,
                options: OpenOptions::default(),
            },
            &jobs,
        )
        else {
            panic!("the download is scanned");
        };
        assert_eq!(paths, vec![at.clone()]);
        assert_eq!(display.as_deref(), Some(Path::new(url)), "named by its URL");
        assert_eq!(status, "Scanning...");
        let Step::ReadSchema {
            made: Made {
                download: Some(download),
                ..
            },
            ..
        } = loader.answered(id, scanned(url), &jobs)
        else {
            panic!("the schema read is handed the download");
        };
        assert_eq!(download.path(), at, "the file the scan reads");
        // As the worker builds it: the dataset holds its file from the start.
        let state = DataTableState::from_lazyframe(frame(), &OpenOptions::default())
            .unwrap()
            .with_open(crate::widgets::datatable::OpenFacts {
                download: Some(download),
                ..Default::default()
            });
        let read = LoadAnswer::SchemaRead {
            state: Box::new(state),
            path: Some(PathBuf::from(url)),
            options: OpenOptions::default(),
            debug_label: None,
        };
        let Step::Install(loaded) = loader.answered(id, read, &jobs) else {
            panic!("installs");
        };
        assert!(
            loaded.state.scans_a_download(),
            "the dataset holds its file"
        );
        loader.first_rows_settled();
        drop(loaded);
        assert!(at.exists(), "and the loader keeps it to read again");

        let Step::Scan { paths, display, .. } = loader.open(request(url)) else {
            panic!("the copy on hand is scanned, with no question");
        };
        assert_eq!(paths, vec![at.clone()]);
        assert_eq!(display.as_deref(), Some(Path::new(url)));

        // Opening something else lets the copy go.
        assert!(loader.make_way().is_some());
        let _ = loader.open(request("local.csv"));
        assert!(!at.exists(), "nothing else held it");
    }

    /// The download handed to the schema read is claimed like any file the open wrote:
    /// quitting while that read is out removes it, though the worker still holds it.
    #[cfg(feature = "http")]
    #[test]
    fn quitting_mid_schema_read_sweeps_the_download_the_worker_holds() {
        use std::time::{Duration, Instant};
        let url = "https://example.com/data.csv";
        let dir = tempfile::tempdir().unwrap();
        let jobs = jobs();
        let mut loader = Loader::default();
        let Step::Probe(pending) = loader.open(request(url)) else {
            panic!("the size is asked first");
        };
        let id = loader.id().unwrap();
        let _ = loader.answered(id, LoadAnswer::Sized(pending.with_size(Some(4))), &jobs);
        let Step::Download { writer, .. } = loader.confirmed() else {
            panic!("agreed to, it downloads");
        };
        let file = crate::download::read_to_temp(
            Some(dir.path()),
            Some("csv"),
            || Ok((std::io::Cursor::new(b"a\n1\n".to_vec()), Some(4))),
            &writer,
            None,
        )
        .unwrap();
        let at = file.path().to_path_buf();
        let downloaded = LoadAnswer::Downloaded {
            download: file,
            options: OpenOptions::default(),
        };
        let _ = loader.answered(id, downloaded, &jobs);
        let Step::ReadSchema {
            made: Made {
                download: Some(held),
                ..
            },
            ..
        } = loader.answered(id, scanned(url), &jobs)
        else {
            panic!("the schema read is handed the download");
        };

        // Quitting drops the app, and the loader with it; the worker is still reading.
        let unfinished = loader.unfinished().clone();
        drop(loader);
        assert!(at.exists(), "the worker's copy keeps it");
        unfinished.sweep(Instant::now() + Duration::from_millis(50));
        assert!(!at.exists(), "the sweep removes it");
        // The worker ending later finds nothing to remove.
        drop(held);
    }

    /// Declining the download, a superseding open and a stale download each retire what
    /// the load held: the question's hold, and the file.
    #[cfg(feature = "http")]
    #[test]
    fn a_remote_open_put_down_leaves_nothing_behind() {
        let url = "https://example.com/data.csv";
        let dir = tempfile::tempdir().unwrap();
        let jobs = jobs();
        let mut loader = Loader::default();
        let Step::Probe(pending) = loader.open(request(url)) else {
            panic!("probe");
        };
        let id = loader.id().unwrap();
        let _ = loader.answered(id, LoadAnswer::Sized(pending), &jobs);
        let retired = loader.retire().expect("declined");
        assert!(retired.asking, "the question goes with it");
        assert!(!jobs.would_strand(), "and so does its hold");

        // A download answering for a load replaced meanwhile is dropped, and its file
        // with it.
        let Step::Probe(pending) = loader.open(request(url)) else {
            panic!("probe");
        };
        let old = loader.id().unwrap();
        let _ = loader.answered(old, LoadAnswer::Sized(pending), &jobs);
        let Step::Download { writer, .. } = loader.confirmed() else {
            panic!("download");
        };
        assert!(loader.make_way().is_some());
        assert!(writer.stopped(), "the download in flight is told to stop");
        let _ = loader.open(request("local.csv"));
        let file = downloaded(dir.path(), "a\n1\n", "csv");
        let at = file.path().to_path_buf();
        assert!(matches!(
            loader.answered(
                old,
                LoadAnswer::Downloaded {
                    download: file,
                    options: OpenOptions::default()
                },
                &jobs
            ),
            Step::Nothing
        ));
        assert!(!at.exists(), "the stale download's file went with it");

        // A download whose scan fails is let go, not kept.
        assert!(loader.make_way().is_some());
        let Step::Probe(pending) = loader.open(request(url)) else {
            panic!("probe");
        };
        let id = loader.id().unwrap();
        let _ = loader.answered(id, LoadAnswer::Sized(pending), &jobs);
        let _ = loader.confirmed();
        let file = downloaded(dir.path(), "a\n1\n", "csv");
        let at = file.path().to_path_buf();
        let _ = loader.answered(
            id,
            LoadAnswer::Downloaded {
                download: file,
                options: OpenOptions::default(),
            },
            &jobs,
        );
        let reason = format!(
            "\"{}\": Bad csv.\nIt stopped at {}.",
            at.display(),
            at.display()
        );
        let Step::Failed(failed) = loader.failed(id, &reason) else {
            panic!("the open fails");
        };
        assert_eq!(
            failed.message,
            format!("\"{url}\": Bad csv.\nIt stopped at {url}."),
            "named by the URL opened, never the temp file (#511)"
        );
        assert!(!at.exists(), "a failed open keeps no download");
    }

    /// A compressed CSV, TSV or PSV downloaded is decompressed under its URL as its
    /// format, as one opened from disk is (#567).
    #[cfg(feature = "http")]
    #[test]
    fn a_downloaded_compressed_csv_is_decompressed() {
        for (ext, format) in [
            ("csv.gz", FileFormat::Csv),
            ("tsv.zst", FileFormat::Tsv),
            ("psv.xz", FileFormat::Psv),
        ] {
            let url = format!("https://example.com/data.{ext}");
            let dir = tempfile::tempdir().unwrap();
            let jobs = jobs();
            let mut loader = Loader::default();
            let Step::Probe(pending) = loader.open(request(&url)) else {
                panic!("probe");
            };
            let id = loader.id().unwrap();
            let _ = loader.answered(id, LoadAnswer::Sized(pending), &jobs);
            let _ = loader.confirmed();
            let file = downloaded(dir.path(), "", ext);
            let step = loader.answered(
                id,
                LoadAnswer::Downloaded {
                    download: file,
                    options: OpenOptions::default(),
                },
                &jobs,
            );
            assert!(
                matches!(
                    step,
                    Step::Decompress { ref path, ref options, .. }
                        if path == Path::new(&url) && options.format == Some(format)
                ),
                "{ext}"
            );
        }
    }

    /// Standard input is read to a file first, its bytes counted on the loading
    /// screen, then scanned or decompressed as `stdin`; it is no recent, and opened
    /// again the copy on hand is read rather than the spent pipe.
    #[test]
    fn stdin_is_spooled_then_read_as_stdin() {
        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let request = OpenRequest::named(
            vec![PathBuf::from("-")],
            OpenOptions::default(),
            &Default::default(),
        );
        assert_eq!(request.recent, None, "not recorded in recents");
        let Step::Spool { writer, read, .. } = loader.open(request) else {
            panic!("standard input is read first");
        };
        let id = loader.id().unwrap();
        let load = loader.current().unwrap();
        assert_eq!(load.phase().label(), ("Reading stdin", 5));
        assert_eq!(load.path(), Some(Path::new("stdin")));
        read.store(1234, Ordering::Relaxed);
        assert_eq!(loader.current().unwrap().size(), 1234, "what has come in");
        assert!(loader.waits());

        let file = {
            use std::io::Write;
            let mut file = TempDownload::create(Some(dir.path()), None).unwrap();
            file.write_all(b"a\n1\n").unwrap();
            TempDownload::keep(file)
        };
        let at = file.path().to_path_buf();
        let options = OpenOptions {
            format: Some(FileFormat::Csv),
            ..Default::default()
        };
        let Step::Scan { paths, display, .. } = answer(
            &mut loader,
            id,
            LoadAnswer::Spooled {
                download: file,
                options: options.clone(),
            },
        ) else {
            panic!("the file is scanned");
        };
        assert_eq!(paths, vec![at.clone()]);
        assert_eq!(display.as_deref(), Some(Path::new("stdin")));
        let Step::ReadSchema {
            made: Made {
                download: Some(_), ..
            },
            ..
        } = answer(&mut loader, id, scanned("stdin"))
        else {
            panic!("the dataset holds the file");
        };
        let Step::Install(loaded) = answer(&mut loader, id, schema_read("stdin")) else {
            panic!("installs");
        };
        assert_eq!(loaded.recent, None);
        assert_eq!(loaded.paths.as_deref(), Some(&[PathBuf::from("-")][..]));
        loader.first_rows_settled();
        drop(writer);

        let Step::Scan { paths, display, .. } = loader.open(OpenRequest::named(
            vec![PathBuf::from("-")],
            options,
            &Default::default(),
        )) else {
            panic!("the copy on hand is read again");
        };
        assert_eq!(paths, vec![at]);
        assert_eq!(display.as_deref(), Some(Path::new("stdin")));

        // Standard input with another path cannot be read.
        assert!(loader.make_way().is_some());
        let step = loader.open(OpenRequest::named(
            vec![PathBuf::from("-"), PathBuf::from("a.csv")],
            OpenOptions::default(),
            &Default::default(),
        ));
        assert!(matches!(step, Step::Crash(_)));
    }

    /// A stream piped in is converted, and the copy, not the spool, is what is kept:
    /// the spool goes once converted, and opened again the copy is scanned as it is.
    #[test]
    fn a_converted_pipe_keeps_the_copy_not_the_stream() {
        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let options = OpenOptions {
            format: Some(FileFormat::Arrow),
            ..Default::default()
        };
        let Step::Spool { .. } = loader.open(OpenRequest::named(
            vec![PathBuf::from("-")],
            OpenOptions::default(),
            &Default::default(),
        )) else {
            panic!("standard input is read first");
        };
        let id = loader.id().unwrap();
        let spool = downloaded(dir.path(), "stream", "tmp");
        let spooled = spool.path().to_path_buf();
        let Step::Scan { paths, .. } = answer(
            &mut loader,
            id,
            LoadAnswer::Spooled {
                download: spool,
                options: options.clone(),
            },
        ) else {
            panic!("the spool is scanned");
        };
        let Step::Convert { .. } = answer(
            &mut loader,
            id,
            LoadAnswer::Convert {
                what: Conversion::Streams,
                files: paths,
                bytes: 6,
                path: Some(PathBuf::from("stdin")),
                options: options.clone(),
            },
        ) else {
            panic!("the stream is converted");
        };
        let copy = downloaded(dir.path(), "ipc file", "arrow");
        let converted = copy.path().to_path_buf();
        let Step::Scan { paths, display, .. } = answer(
            &mut loader,
            id,
            LoadAnswer::Converted {
                converted: Converted::Streams {
                    file: copy,
                    parts: Vec::new(),
                },
                path: Some(PathBuf::from("stdin")),
                options: options.clone(),
            },
        ) else {
            panic!("the copy is scanned");
        };
        assert_eq!(paths, std::slice::from_ref(&converted));
        assert_eq!(display.as_deref(), Some(Path::new("stdin")));
        assert!(!spooled.exists(), "the spool goes once converted");

        let Step::ReadSchema {
            made: Made {
                download: Some(_), ..
            },
            ..
        } = answer(&mut loader, id, scanned("stdin"))
        else {
            panic!("the dataset holds the copy");
        };
        let Step::Install(loaded) = answer(&mut loader, id, schema_read("stdin")) else {
            panic!("installs");
        };
        loader.first_rows_settled();
        drop(loaded);
        assert!(converted.exists(), "kept to read again");
        let Step::Scan { paths, .. } = loader.open(OpenRequest::named(
            vec![PathBuf::from("-")],
            options,
            &Default::default(),
        )) else {
            panic!("the copy on hand is read again");
        };
        assert_eq!(paths, [converted]);
    }

    /// A compressed CSV piped in is decompressed as `stdin`; putting the read down
    /// stops its writer, which removes the partial file.
    #[test]
    fn piped_compressed_csv_is_decompressed_and_a_stop_reaches_the_spool() {
        let dir = tempfile::tempdir().unwrap();
        let mut loader = Loader::default();
        let Step::Spool { writer, .. } = loader.open(OpenRequest::named(
            vec![PathBuf::from("-")],
            OpenOptions::default(),
            &Default::default(),
        )) else {
            panic!("spool");
        };
        assert!(loader.make_way().is_some(), "Ctrl+O puts it down");
        assert!(writer.stopped(), "and the spool stops at its next chunk");

        let Step::Spool { .. } = loader.open(OpenRequest::named(
            vec![PathBuf::from("-")],
            OpenOptions::default(),
            &Default::default(),
        )) else {
            panic!("spool");
        };
        let id = loader.id().unwrap();
        let file = TempDownload::keep(TempDownload::create(Some(dir.path()), None).unwrap());
        let step = answer(
            &mut loader,
            id,
            LoadAnswer::Spooled {
                download: file,
                options: OpenOptions {
                    format: Some(FileFormat::Csv),
                    compression: Some(CompressionFormat::Gzip),
                    ..Default::default()
                },
            },
        );
        assert!(matches!(
            step,
            Step::Decompress { ref path, download: Some(_), .. } if path == Path::new("stdin")
        ));
    }
}