rudb 0.4.13

The embedding API: connections, prepared statements, configuration and results.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
//! End to end tests, from a string of SQL to rows.
//!
//! The layers below have their own tests and this file does not repeat them. `rudb-parse` proves
//! the grammar, `rudb-bind` proves that a query binds to the plan it should, and `rudb-exec` proves
//! that a plan produces the right rows. What is only testable here is the three of them agreeing:
//! a name a query writes has to be the name the binder resolves and the name the executor reads,
//! and a type the binder decided has to be the type the operator produces.

use std::time::Duration;

use rudb_common::{Field, LogicalType, Span, Value, days_from_civil};

use crate::{Config, Database, VECTOR_SIZE, arrow};

/// `t(x INTEGER, s VARCHAR)` with a null in it, plus an empty table to test the degenerate cases.
fn database() -> Database {
    let db = Database::new();
    db.create_table(
        "t",
        vec![Field::new("x", LogicalType::Integer), Field::new("s", LogicalType::Varchar)],
    )
    .unwrap();
    db.append(
        "t",
        &[
            vec![Value::Integer(3), Value::Varchar("a".to_string())],
            vec![Value::Integer(1), Value::Null],
            vec![Value::Integer(2), Value::Varchar("c".to_string())],
            vec![Value::Integer(1), Value::Varchar("a".to_string())],
        ],
    )
    .unwrap();
    db.create_table("empty", vec![Field::new("x", LogicalType::Integer)]).unwrap();
    db
}

/// Every row of a query, which is what most of these assert on.
fn rows(db: &Database, sql: &str) -> Vec<Vec<Value>> {
    db.query(sql).unwrap().rows().collect()
}

#[test]
fn non_recursive_ctes_run_as_inlined_queries() {
    let db = Database::new();
    assert_eq!(
        rows(
            &db,
            "WITH a AS (SELECT 2 AS x), b(y) AS NOT MATERIALIZED (SELECT x + 1 FROM a) SELECT y FROM b",
        ),
        vec![vec![Value::Integer(3)]]
    );
    assert_eq!(
        rows(&db, "WITH t AS (SELECT 1 AS x) SELECT left_t.x + right_t.x FROM t left_t, t right_t",),
        vec![vec![Value::Integer(2)]]
    );
}

/// The first column of a row, for a test that sorts a grouped answer by a `BIGINT` key.
fn first_key(row: &[Value]) -> i64 {
    match row[0] {
        Value::BigInt(key) => key,
        ref other => panic!("the key of this query is a BIGINT, not {other:?}"),
    }
}

/// The message a query fails with.
fn failure(db: &Database, sql: &str) -> String {
    db.query(sql).unwrap_err().message().to_string()
}

fn integer(value: i32) -> Value {
    Value::Integer(value)
}

fn text(value: &str) -> Value {
    Value::Varchar(value.to_string())
}

fn list(values: &[i32]) -> Value {
    Value::List {
        element: LogicalType::Integer,
        values: values.iter().map(|&v| Value::Integer(v)).collect(),
    }
}

#[test]
fn a_star_reads_every_column_in_order() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT * FROM t"),
        vec![
            vec![integer(3), text("a")],
            vec![integer(1), Value::Null],
            vec![integer(2), text("c")],
            vec![integer(1), text("a")],
        ]
    );
}

/// The query from the crate documentation, which is the smallest thing that touches all four
/// layers.
#[test]
fn a_filter_keeps_the_rows_the_predicate_is_true_for() {
    let db = database();
    assert_eq!(rows(&db, "SELECT x FROM t WHERE x > 1"), vec![vec![integer(3)], vec![integer(2)]]);
}

/// A predicate that is null is not a predicate that is true, so the null row goes out. This is the
/// rule that separates SQL from every language whose `if` takes a boolean.
#[test]
fn a_null_predicate_drops_the_row() {
    let db = database();
    assert_eq!(rows(&db, "SELECT x FROM t WHERE s = 'c'"), vec![vec![integer(2)]]);
    assert_eq!(
        rows(&db, "SELECT x FROM t WHERE s <> 'c'"),
        vec![vec![integer(3)], vec![integer(1)]]
    );
}

#[test]
fn a_query_with_no_table_still_runs() {
    let db = database();
    assert_eq!(rows(&db, "SELECT 1 + 1"), vec![vec![integer(2)]]);
}

#[test]
fn an_expression_takes_the_name_it_was_aliased_to() {
    let db = database();
    let result = db.query("SELECT x + 10 AS bumped FROM t WHERE x = 2").unwrap();
    assert_eq!(result.names(), ["bumped"]);
    assert_eq!(result.types(), [LogicalType::Integer]);
    assert_eq!(result.value_at(0, 0), integer(12));
}

#[test]
fn an_aggregate_over_no_rows_is_zero_and_null() {
    let db = database();
    assert_eq!(rows(&db, "SELECT count(*) FROM empty"), vec![vec![Value::BigInt(0)]]);
    assert_eq!(rows(&db, "SELECT sum(x) FROM empty"), vec![vec![Value::Null]]);
}

/// `count(*)` counts rows and `count(s)` counts the rows where `s` is not null. One null in the
/// table is enough to tell them apart.
#[test]
fn count_star_and_count_of_a_column_disagree_about_nulls() {
    let db = database();
    assert_eq!(rows(&db, "SELECT count(*) FROM t"), vec![vec![Value::BigInt(4)]]);
    assert_eq!(rows(&db, "SELECT count(s) FROM t"), vec![vec![Value::BigInt(3)]]);
}

/// `count()` with nothing in it is the third spelling of `count(*)`. It counts rows, it is named
/// after the function it really is, and it works wherever the other two do.
#[test]
fn count_with_no_arguments_counts_rows_the_way_a_star_does() {
    let db = database();
    assert_eq!(rows(&db, "SELECT count() FROM t"), vec![vec![Value::BigInt(4)]]);
    assert_eq!(rows(&db, "SELECT COUNT() FROM t"), vec![vec![Value::BigInt(4)]]);
    assert_eq!(db.query("SELECT count() FROM t").unwrap().names(), ["count_star()"]);
    assert_eq!(rows(&db, "SELECT count() OVER () FROM t LIMIT 1"), vec![vec![Value::BigInt(4)]]);
    assert_eq!(db.query("SELECT count() OVER () FROM t").unwrap().names(), ["count() OVER ()"]);
}

/// A `FILTER` picks which rows one aggregate reads, which is not the same as picking which rows
/// the query has. Every answer here was read off the pinned binary.
#[test]
fn a_filter_narrows_one_aggregate_and_leaves_the_rest_of_the_query_alone() {
    let db = scripted(&[
        "CREATE TABLE f (k INTEGER, v INTEGER)",
        "INSERT INTO f VALUES (1, 1), (1, 2), (1, NULL), (2, 3), (2, 4), (2, NULL)",
    ]);
    assert_eq!(
        rows(&db, "SELECT sum(v) FILTER (WHERE v > 1) FROM f"),
        vec![vec![Value::HugeInt(9)]]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FILTER (WHERE v > 1) FROM f"),
        vec![vec![Value::BigInt(3)]]
    );
    assert_eq!(
        rows(&db, "SELECT count(v) FILTER (WHERE v IS NULL) FROM f"),
        vec![vec![Value::BigInt(0)]]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FILTER (WHERE v IS NULL) FROM f"),
        vec![vec![Value::BigInt(2)]]
    );
    // Two aggregates over the same rows, one of them filtered, which is what the feature is for.
    assert_eq!(
        rows(&db, "SELECT count(*) FILTER (WHERE v > 1), count(*) FROM f"),
        vec![vec![Value::BigInt(3), Value::BigInt(6)]]
    );
    // A group where the predicate holds for nothing still has its row, with the empty answer each
    // aggregate gives over no rows at all.
    assert_eq!(
        rows(
            &db,
            "SELECT k, count(*) FILTER (WHERE v > 2), sum(v) FILTER (WHERE v > 2) \
             FROM f GROUP BY k ORDER BY k"
        ),
        vec![
            vec![integer(1), Value::BigInt(0), Value::Null],
            vec![integer(2), Value::BigInt(2), Value::HugeInt(7)],
        ]
    );
    assert_eq!(rows(&db, "SELECT sum(v) FILTER (WHERE false) FROM f"), vec![vec![Value::Null]]);
    assert_eq!(
        rows(&db, "SELECT sum(DISTINCT v) FILTER (WHERE v > 1) FROM f"),
        vec![vec![Value::HugeInt(9)]]
    );
    // The predicate is cast to BOOLEAN the way a `WHERE` is, so an integer column is a predicate
    // on whether the integer is not zero and the null row drops out of it.
    assert_eq!(rows(&db, "SELECT sum(v) FILTER (WHERE v) FROM f"), vec![vec![Value::HugeInt(10)]]);
}

#[test]
fn a_group_by_produces_one_row_per_distinct_value() {
    let db = database();
    let mut answer = rows(&db, "SELECT x, count(*) FROM t GROUP BY x");
    answer.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(
        answer,
        vec![
            vec![integer(1), Value::BigInt(2)],
            vec![integer(2), Value::BigInt(1)],
            vec![integer(3), Value::BigInt(1)],
        ]
    );
}

/// The compact numeric group state has to keep SUM and AVG nulls independent of COUNT(*).
#[test]
fn grouped_smallint_sum_and_avg_keep_nulls() {
    let db = scripted(&[
        "CREATE TABLE numbers (k INTEGER, a SMALLINT, b SMALLINT)",
        "INSERT INTO numbers VALUES (1, 1, 2), (1, NULL, 4), (1, 2, NULL), \
         (2, NULL, NULL), (2, NULL, NULL)",
    ]);
    assert_eq!(
        rows(&db, "SELECT k, COUNT(*), SUM(a), AVG(b) FROM numbers GROUP BY k ORDER BY k"),
        vec![
            vec![integer(1), Value::BigInt(3), Value::HugeInt(3), Value::Double(3.0)],
            vec![integer(2), Value::BigInt(2), Value::Null, Value::Null],
        ]
    );
}

#[test]
fn an_unordered_limit_keeps_only_the_groups_it_can_return() {
    let db = database();
    let full =
        db.query("SELECT range % 10000 AS k, count(*) FROM range(100000) GROUP BY k").unwrap();
    let full_peak = full.metrics().unwrap().resource.peak_bytes;
    drop(full);
    let limited = db
        .query("SELECT range % 10000 AS k, count(*) FROM range(100000) GROUP BY k LIMIT 10")
        .unwrap();
    assert_eq!(
        limited.rows().collect::<Vec<_>>(),
        (0..10).map(|k| vec![Value::BigInt(k), Value::BigInt(10)]).collect::<Vec<_>>()
    );
    let limited_peak = limited.metrics().unwrap().resource.peak_bytes;
    assert!(limited_peak * 10 < full_peak, "{limited_peak} was not far below {full_peak}");
}

#[test]
fn a_having_clause_filters_groups_rather_than_rows() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT x, count(*) FROM t GROUP BY x HAVING count(*) > 1"),
        vec![vec![integer(1), Value::BigInt(2)]]
    );
}

/// Direction and null placement are two decisions, and `DESC` alone puts nulls first in DuckDB.
#[test]
fn order_by_puts_nulls_where_the_clause_says() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT s FROM t ORDER BY s NULLS LAST"),
        vec![vec![text("a")], vec![text("a")], vec![text("c")], vec![Value::Null]]
    );
    assert_eq!(
        rows(&db, "SELECT s FROM t ORDER BY s NULLS FIRST"),
        vec![vec![Value::Null], vec![text("a")], vec![text("a")], vec![text("c")]]
    );
}

#[test]
fn order_by_and_limit_and_offset_compose() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT x FROM t ORDER BY x DESC LIMIT 2 OFFSET 1"),
        vec![vec![integer(2)], vec![integer(1)]]
    );
}

/// An order by with a limit runs as a top N, which holds the rows that could still come out rather
/// than the whole input. What it has to produce is what the sort produced, over enough rows that it
/// trims several times and over more than one chunk of input.
#[test]
fn an_order_by_with_a_limit_answers_what_the_sort_would_have() {
    let db = Database::new();
    db.create_table("many", vec![Field::new("x", LogicalType::Integer)]).unwrap();
    // Counting down, so the answer is at the end of the input and nothing is right by accident.
    let counted: Vec<Vec<Value>> = (0..5000).rev().map(|x| vec![Value::Integer(x)]).collect();
    db.append("many", &counted).unwrap();
    let wanted: Vec<Vec<Value>> = (7..17).map(|x| vec![Value::Integer(x)]).collect();
    assert_eq!(rows(&db, "SELECT x FROM many ORDER BY x LIMIT 10 OFFSET 7"), wanted);
    db.execute("SET disabled_optimizers = 'top_n'").unwrap();
    assert_eq!(rows(&db, "SELECT x FROM many ORDER BY x LIMIT 10 OFFSET 7"), wanted);
}

/// The group key of a row is written into the buffer the row before it used, which for a string
/// column means the bytes go where the last row's bytes were rather than into a new allocation. The
/// case that breaks a buffer being reused is a column where what is in the slot and what is arriving
/// keep changing shape, so this one alternates strings of different lengths with nulls and with the
/// empty string, and it runs over enough rows to cross several chunks.
#[test]
fn grouping_a_string_column_counts_each_string_once_however_the_rows_are_ordered() {
    let db = Database::new();
    db.create_table("words", vec![Field::new("s", LogicalType::Varchar)]).unwrap();
    let shapes = [Some(""), Some("a"), None, Some("a longer one"), None, Some("ab")];
    let written: Vec<Vec<Value>> = (0..6000)
        .map(|at| match shapes[at % shapes.len()] {
            Some(word) => vec![Value::Varchar(word.to_string())],
            None => vec![Value::Null],
        })
        .collect();
    db.append("words", &written).unwrap();
    let mut answer = rows(&db, "SELECT s, count(*) FROM words GROUP BY s");
    answer.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(
        answer,
        vec![
            vec![Value::Null, Value::BigInt(2000)],
            vec![text(""), Value::BigInt(1000)],
            vec![text("a longer one"), Value::BigInt(1000)],
            vec![text("a"), Value::BigInt(1000)],
            vec![text("ab"), Value::BigInt(1000)],
        ]
    );
    assert_eq!(
        rows(&db, "SELECT count(DISTINCT s) FROM words"),
        vec![vec![Value::BigInt(4)]],
        "a distinct inside an aggregate keys on the same buffer"
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FROM (SELECT DISTINCT s FROM words)"),
        vec![vec![Value::BigInt(5)]],
        "and duplicate elimination counts the null group as a row where count(DISTINCT) does not"
    );
}

/// The same question asked of a string that came out of a cross product, which is where it was
/// answered wrong. A cross product hands the left row down as constant vectors, one value standing
/// for the whole chunk, and the comparison after the hash probe could not read one, so every row
/// opened a group of its own. TPC-H q05, q07 and q10 all returned the same group key once per row
/// because of it, and they are all written with a comma in the `FROM` rather than a `JOIN`.
#[test]
fn grouping_a_string_that_came_out_of_a_cross_product_still_counts_each_string_once() {
    let db = Database::new();
    db.execute("CREATE TABLE w AS SELECT i AS k, 'word' || (i % 3) AS s FROM range(9) t(i)")
        .unwrap();
    db.execute("CREATE TABLE n AS SELECT i AS k FROM range(9) t(i)").unwrap();
    let mut answer = rows(&db, "SELECT s, count(*) FROM w, n WHERE w.k = n.k GROUP BY s");
    answer.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(
        answer,
        vec![
            vec![text("word0"), Value::BigInt(3)],
            vec![text("word1"), Value::BigInt(3)],
            vec![text("word2"), Value::BigInt(3)],
        ]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FROM (SELECT DISTINCT s FROM w, n WHERE w.k = n.k)"),
        vec![vec![Value::BigInt(3)]],
        "and duplicate elimination keys on the same table"
    );
}

#[test]
fn distinct_collapses_equal_rows() {
    let db = database();
    let mut answer = rows(&db, "SELECT DISTINCT x FROM t");
    answer.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(answer, vec![vec![integer(1)], vec![integer(2)], vec![integer(3)]]);
}

#[test]
fn counting_a_distinct_counts_the_distinct_values() {
    // The one wrong answer column pruning can produce. Nothing above the `DISTINCT` names a column,
    // and a plain `DISTINCT` names none either, so the pass that narrows a projection to what is
    // read of it used to leave an operator deduplicating rows with nothing in them, and this came
    // back as 1 on any table with a row in it. `crates/rudb-opt/src/columns.rs` is where it lives.
    let db = database();
    assert_eq!(
        rows(&db, "SELECT count(*) FROM (SELECT DISTINCT x FROM t)"),
        vec![vec![Value::BigInt(3)]]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FROM (SELECT DISTINCT x, s FROM t)"),
        vec![vec![Value::BigInt(4)]]
    );
}

#[test]
fn a_join_matches_on_its_condition() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT a.x, b.s FROM t AS a JOIN t AS b ON a.x = b.x WHERE a.x = 2"),
        vec![vec![integer(2), text("c")]]
    );
}

#[test]
fn a_left_join_keeps_the_left_row_and_pads_the_right() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT t.x, e.x FROM t LEFT JOIN empty AS e ON t.x = e.x WHERE t.x = 3"),
        vec![vec![integer(3), Value::Null]]
    );
}

/// The hash path against the loop it replaces, on the rows that tell them apart.
///
/// An equality between two columns is answered by a lookup and anything else is answered by the
/// nested loop, so `ON l.k = r.k AND l.k >= r.k` runs the loop over a join that means exactly what
/// `ON l.k = r.k` means: the second conjunct is implied by the first whenever the first is true,
/// and when either side is null both of them are null. That gives an oracle inside one engine, and
/// it is the same oracle the hash join in #351 keeps the loop around for.
///
/// The rows are the cases the two disagree about if the lookup gets its null rule from grouping.
/// Nulls on both sides, which match nothing rather than each other. Duplicates on both sides, so a
/// key with two rows on the left and three on the right is six pairs. A key on one side only, for
/// the padding each outer kind does. A two column key, because the key is a row and not a value.
#[test]
fn an_equality_is_looked_up_and_answers_what_the_loop_answers() {
    let db = Database::new();
    db.execute("CREATE TABLE l (k INTEGER, j INTEGER, tag VARCHAR)").unwrap();
    db.execute(
        "INSERT INTO l VALUES (1, 1, 'one'), (1, 1, 'one again'), (2, 2, 'two'), \
         (NULL, 1, 'null key'), (3, NULL, 'null second'), (4, 4, 'left only')",
    )
    .unwrap();
    db.execute("CREATE TABLE r (k INTEGER, j INTEGER, tag VARCHAR)").unwrap();
    db.execute(
        "INSERT INTO r VALUES (1, 1, 'a'), (1, 1, 'b'), (1, 1, 'c'), (2, 9, 'wrong second'), \
         (NULL, 1, 'null key'), (5, 5, 'right only')",
    )
    .unwrap();
    for kind in ["INNER", "LEFT", "RIGHT", "FULL"] {
        for on in ["l.k = r.k", "l.k = r.k AND l.j = r.j"] {
            let listing = |condition: &str| {
                let sql = format!(
                    "SELECT l.tag, r.tag FROM l {kind} JOIN r ON {condition} \
                     ORDER BY l.tag NULLS FIRST, r.tag NULLS FIRST"
                );
                rows(&db, &sql)
            };
            let looked_up = listing(on);
            let looped = listing(&format!("{on} AND l.k >= r.k"));
            assert_eq!(looked_up, looped, "{kind} JOIN ON {on}");
        }
    }
}

/// `IS NOT DISTINCT FROM` looked up, against the loop, on the rows where the null rule decides.
///
/// This comparison is the same lookup as `=` with the opposite answer about a null, so two nulls are
/// one key here rather than two rows that match nothing. The oracle is `coalesce` on both sides
/// against a value no row holds, which says the same thing and is a call rather than a column, so it
/// is answered by the loop.
///
/// The third condition is the one worth the most. One join with both comparisons in it is what the
/// unnesting rules write, since the domain key is equated null safely and the condition the query
/// wrote next to it is an ordinary `=`, and a table built with one rule for every column answers
/// that one wrong in a way no single comparison test would show.
#[test]
fn a_null_safe_equality_is_looked_up_and_answers_what_the_loop_answers() {
    let db = Database::new();
    db.execute("CREATE TABLE l (k INTEGER, j INTEGER, tag VARCHAR)").unwrap();
    db.execute(
        "INSERT INTO l VALUES (1, 1, 'one'), (1, 1, 'one again'), (2, 2, 'two'), \
         (NULL, 1, 'null key'), (NULL, 2, 'null key again'), (4, 4, 'left only')",
    )
    .unwrap();
    db.execute("CREATE TABLE r (k INTEGER, j INTEGER, tag VARCHAR)").unwrap();
    db.execute(
        "INSERT INTO r VALUES (1, 1, 'a'), (1, 1, 'b'), (2, 9, 'wrong second'), \
         (NULL, 1, 'null key'), (NULL, 9, 'null key again'), (5, 5, 'right only')",
    )
    .unwrap();
    let conditions = [
        ("l.k IS NOT DISTINCT FROM r.k", "coalesce(l.k, -1) = coalesce(r.k, -1)"),
        (
            "l.k IS NOT DISTINCT FROM r.k AND l.j IS NOT DISTINCT FROM r.j",
            "coalesce(l.k, -1) = coalesce(r.k, -1) AND coalesce(l.j, -1) = coalesce(r.j, -1)",
        ),
        (
            "l.k IS NOT DISTINCT FROM r.k AND l.j = r.j",
            "coalesce(l.k, -1) = coalesce(r.k, -1) AND l.j = r.j",
        ),
    ];
    for kind in ["INNER", "LEFT", "RIGHT", "FULL", "SEMI", "ANTI"] {
        for (on, oracle) in conditions {
            // A semi join and an anti join produce the driving side's columns and nothing else, so
            // there is no `r.tag` to name or to sort on.
            let one_sided = kind == "SEMI" || kind == "ANTI";
            let listing = |condition: &str| {
                let columns = if one_sided { "l.tag" } else { "l.tag, r.tag" };
                let order =
                    if one_sided { "1 NULLS FIRST" } else { "1 NULLS FIRST, 2 NULLS FIRST" };
                let sql = format!(
                    "SELECT {columns} FROM l {kind} JOIN r ON {condition} ORDER BY {order}"
                );
                rows(&db, &sql)
            };
            assert_eq!(listing(on), listing(oracle), "{kind} JOIN ON {on}");
        }
    }
}

/// The other three kinds the streaming probe answers, against the same oracle.
///
/// `INNER` and `LEFT` are two of the five and the test above has them. The other three are `SEMI`,
/// `ANTI` and `SINGLE`, and they are the ones with the least written about them and the most to get
/// wrong: a semi join keeps a driving row once however many times it matched, an anti join is that
/// question inverted, and a single join refuses a driving row that matched twice. A null rule one row
/// out shows up here and in none of the four above.
///
/// The same trick forces the loop. The second conjunct is implied by the first whenever the first is
/// true and is null whenever the first is null, so the two spellings mean the same thing and are
/// answered two different ways.
#[test]
fn a_semi_an_anti_and_a_single_join_answer_what_the_loop_answers() {
    let db = Database::new();
    db.execute("CREATE TABLE l (k INTEGER, tag VARCHAR)").unwrap();
    db.execute(
        "INSERT INTO l VALUES (1, 'one'), (1, 'one again'), (2, 'two'), (NULL, 'null key'), \
         (4, 'left only')",
    )
    .unwrap();
    db.execute("CREATE TABLE r (k INTEGER, v INTEGER)").unwrap();
    // Two rows on key 1, so a semi join has to produce the driving row once rather than twice.
    db.execute("INSERT INTO r VALUES (1, 10), (1, 11), (2, 20), (NULL, 30), (5, 50)").unwrap();
    let shapes = [
        "SELECT l.tag FROM l SEMI JOIN r ON r.k = l.k{extra}",
        "SELECT l.tag FROM l ANTI JOIN r ON r.k = l.k{extra}",
        // A scalar subquery is a single join, and this one is over the keys that match at most once
        // because the point here is the null rule rather than the error a second match raises.
        "SELECT tag, (SELECT v FROM r WHERE r.k = l.k{extra} AND r.k <> 1) FROM l",
    ];
    for shape in shapes {
        let listing = |extra: &str| {
            rows(&db, &format!("{} ORDER BY 1 NULLS FIRST", shape.replace("{extra}", extra)))
        };
        assert_eq!(listing(""), listing(" AND r.k >= l.k"), "{shape}");
    }
}

/// `*` over a semi or an anti join is the left side, and the right side is out of scope after one.
///
/// These two kinds produce the left side's rows and nothing else, so a star that expanded to both
/// sides asked the join for columns it does not have and the query died with an internal error about
/// a column not being in the schema. That is tamnd/rudb#847. The condition is still bound against
/// both sides, which is the whole point of writing one, so what changes is only what is visible
/// after the join is built.
///
/// The `USING` case is here because that clause drops a column of the right side on its way past, so
/// the boundary between the two sides has to be the one taken before the drop rather than after it.
/// Getting that wrong takes a column of the left side out of the answer.
#[test]
fn a_star_over_a_semi_or_an_anti_join_is_the_left_side_alone() {
    let db = Database::new();
    db.execute("CREATE TABLE a (k INTEGER, v INTEGER)").unwrap();
    db.execute("INSERT INTO a VALUES (1, 10), (2, 20)").unwrap();
    db.execute("CREATE TABLE b (k INTEGER, w INTEGER)").unwrap();
    db.execute("INSERT INTO b VALUES (1, 100)").unwrap();

    let matched = vec![vec![integer(1), integer(10)]];
    assert_eq!(rows(&db, "SELECT * FROM a SEMI JOIN b ON a.k = b.k"), matched);
    assert_eq!(rows(&db, "SELECT * FROM a SEMI JOIN b USING (k)"), matched);
    assert_eq!(rows(&db, "SELECT * FROM a NATURAL SEMI JOIN b"), matched);
    assert_eq!(rows(&db, "SELECT * FROM (SELECT * FROM a SEMI JOIN b ON a.k = b.k) t"), matched);
    assert_eq!(
        rows(&db, "SELECT * FROM a ANTI JOIN b ON a.k = b.k"),
        vec![vec![integer(2), integer(20)]]
    );

    // Naming the right side after the join is a binder error rather than a query about a column
    // nothing produces, which is what the reference binary says too.
    assert!(
        failure(&db, "SELECT b.w FROM a SEMI JOIN b ON a.k = b.k").contains("\"b\""),
        "the right side should be out of scope after a semi join"
    );
}

/// One driving row matching more rows than fit in a chunk.
///
/// The streaming probe answers a driving chunk into an output chunk, and a chunk holds 1024 rows, so
/// a key with five thousand rows behind it is a row that cannot be finished by the call that started
/// it. It has to come out over five calls, each picking up where the last one stopped. Getting that
/// wrong loses the tail of a popular key, which is a wrong answer that only appears on data skewed
/// enough to have one, and skew is what real data is.
#[test]
fn a_driving_row_that_matches_more_rows_than_a_chunk_holds_produces_all_of_them() {
    let db = Database::new();
    db.execute("CREATE TABLE l (k INTEGER, tag VARCHAR)").unwrap();
    db.execute("INSERT INTO l VALUES (1, 'popular'), (2, 'rare'), (3, 'absent')").unwrap();
    db.execute("CREATE TABLE r (k INTEGER, v BIGINT)").unwrap();
    db.execute("INSERT INTO r SELECT 1, i FROM range(5000) AS series(i)").unwrap();
    db.execute("INSERT INTO r VALUES (2, -1)").unwrap();
    // Every one of the five thousand, once each, and the rare key beside them rather than lost
    // behind them.
    assert_eq!(
        rows(&db, "SELECT count(*), count(DISTINCT r.v), sum(r.v) FROM l JOIN r ON l.k = r.k"),
        vec![vec![Value::BigInt(5001), Value::BigInt(5001), Value::HugeInt(12_497_499)]]
    );
    assert_eq!(
        rows(&db, "SELECT l.tag, count(*) FROM l LEFT JOIN r ON l.k = r.k GROUP BY 1 ORDER BY 1"),
        vec![
            vec![text("absent"), Value::BigInt(1)],
            vec![text("popular"), Value::BigInt(5000)],
            vec![text("rare"), Value::BigInt(1)],
        ]
    );
}

#[test]
fn a_union_deduplicates_and_union_all_does_not() {
    let db = database();
    assert_eq!(rows(&db, "SELECT 1 UNION ALL SELECT 1"), vec![vec![integer(1)], vec![integer(1)]]);
    assert_eq!(rows(&db, "SELECT 1 UNION SELECT 1"), vec![vec![integer(1)]]);
}

#[test]
fn a_subquery_in_the_from_clause_is_a_table() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT inner_query.total FROM (SELECT count(*) AS total FROM t) AS inner_query"),
        vec![vec![Value::BigInt(4)]]
    );
}

#[test]
fn a_case_expression_picks_the_first_arm_that_holds() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT CASE WHEN x > 2 THEN 'big' ELSE 'small' END FROM t ORDER BY x"),
        vec![vec![text("small")], vec![text("small")], vec![text("small")], vec![text("big")]]
    );
}

/// A comparison against a string reads the string as the other side's type rather than printing
/// the other side. Every answer here is the answer DuckDB gives.
#[test]
fn a_comparison_with_a_string_happens_in_the_other_type() {
    let db = database();
    // Text comparison would say these two are different, since the day is not padded. The cast is
    // spelled out because DATE '2013-07-15' is a typed literal and the transformer does not cover
    // that rule yet.
    assert_eq!(
        rows(&db, "SELECT CAST('2013-07-15' AS DATE) = '2013-7-15'"),
        vec![vec![Value::Boolean(true)]]
    );
    assert_eq!(
        rows(&db, "SELECT CAST('2013-07-15' AS DATE) >= '2013-07-01'"),
        vec![vec![Value::Boolean(true)]]
    );
    // Text comparison would say ten is less than nine.
    assert_eq!(rows(&db, "SELECT 10 > '9'"), vec![vec![Value::Boolean(true)]]);
    assert_eq!(rows(&db, "SELECT TRUE = 'true'"), vec![vec![Value::Boolean(true)]]);
    // Not here yet: DuckDB answers true to 1 = '1.0', because its string to integer cast rounds
    // rather than refusing a decimal point, and rounds half away from zero. That is a difference
    // in the cast rather than in this rule, and it belongs with the cast.
}

/// 2013-07-15 at a time of day, which is the day ClickBench asks about.
fn moment(hours: i64, minutes: i64, seconds: i64) -> Value {
    let day = i64::from(days_from_civil(2013, 7, 15)) * 86_400_000_000;
    Value::Timestamp(day + hours * 3_600_000_000 + minutes * 60_000_000 + seconds * 1_000_000)
}

/// The two shapes ClickBench needs, which are query 19 and query 43 with the column renamed.
///
/// `EXTRACT` is not a function in the grammar and is a call to `date_part` by the time the binder
/// sees it, so this is also the test that the rewrite survives the trip. The truncation keeps the
/// type it was given, which is why the second query can sort by it and get times rather than text.
#[test]
fn a_timestamp_can_be_taken_apart_and_grouped_by() {
    let db = Database::new();
    db.create_table("hits", vec![Field::new("ts", LogicalType::Timestamp)]).unwrap();
    db.append("hits", &[vec![moment(10, 23, 45)], vec![moment(10, 23, 7)], vec![moment(11, 5, 0)]])
        .unwrap();
    assert_eq!(
        rows(&db, "SELECT extract(minute FROM ts) AS m, COUNT(*) FROM hits GROUP BY m ORDER BY m"),
        vec![vec![Value::BigInt(5), Value::BigInt(1)], vec![Value::BigInt(23), Value::BigInt(2)],]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT DATE_TRUNC('hour', ts) AS h, COUNT(*) AS n FROM hits \
             GROUP BY DATE_TRUNC('hour', ts) ORDER BY DATE_TRUNC('hour', ts)"
        ),
        vec![vec![moment(10, 0, 0), Value::BigInt(2)], vec![moment(11, 0, 0), Value::BigInt(1)],]
    );
}

/// The ClickBench entry's own load recipe, over a column that holds what the Parquet holds.
///
/// `hits.parquet` stores the day as days since the epoch and the time as seconds since the epoch,
/// both as integers, and every query in the published set reads a date and a timestamp. DuckDB's
/// entry closes that with `make_date` and `epoch_ms`, so this is those two over the integers the
/// real file has in it, followed by the thing the queries then do with them.
#[test]
fn the_integers_the_benchmark_stores_become_the_dates_the_benchmark_queries() {
    let db = Database::new();
    db.create_table(
        "hits",
        vec![
            Field::new("EventDate", LogicalType::Integer),
            Field::new("EventTime", LogicalType::BigInt),
        ],
    )
    .unwrap();
    let day = i64::from(days_from_civil(2013, 7, 15));
    db.append(
        "hits",
        &[
            vec![
                Value::Integer(days_from_civil(2013, 7, 15)),
                Value::BigInt(day * 86_400 + 37_425),
            ],
            vec![Value::Integer(days_from_civil(2013, 7, 2)), Value::BigInt(day * 86_400 + 37_387)],
        ],
    )
    .unwrap();
    assert_eq!(
        rows(
            &db,
            "SELECT make_date(EventDate) AS d FROM hits WHERE make_date(EventDate) >= '2013-07-10' \
             ORDER BY d"
        ),
        vec![vec![Value::Date(days_from_civil(2013, 7, 15))]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT extract(minute FROM epoch_ms(EventTime * 1000)) AS m, COUNT(*) FROM hits \
             GROUP BY m ORDER BY m"
        ),
        vec![vec![Value::BigInt(23), Value::BigInt(2)]]
    );
}

/// `SELECT * REPLACE`, which is the other half of that recipe.
///
/// The entry loads the file with one `SELECT * REPLACE (...)` over `read_parquet` rather than by
/// listing all hundred and five columns, so the star has to keep every column it stood for, in
/// order, with the replaced ones substituted in place. The name comes from the replace list and not
/// from the table, which only shows when the two are spelled with different case.
#[test]
fn a_star_can_replace_some_of_what_it_stands_for() {
    let db = Database::new();
    db.create_table(
        "hits",
        vec![
            Field::new("EventDate", LogicalType::Integer),
            Field::new("UserID", LogicalType::BigInt),
        ],
    )
    .unwrap();
    db.append("hits", &[vec![Value::Integer(days_from_civil(2013, 7, 15)), Value::BigInt(7)]])
        .unwrap();
    let result =
        db.query("SELECT * REPLACE (make_date(EventDate) AS eventdate) FROM hits").unwrap();
    assert_eq!(result.names(), &["eventdate", "UserID"]);
    assert_eq!(
        result.rows().collect::<Vec<_>>(),
        vec![vec![Value::Date(days_from_civil(2013, 7, 15)), Value::BigInt(7)]]
    );
    // A qualified star takes one too, and the replacement is an ordinary expression that can read
    // any column in scope and not only the one it is replacing.
    assert_eq!(
        rows(&db, "SELECT hits.* REPLACE (UserID + 1 AS UserID) FROM hits"),
        vec![vec![Value::Integer(days_from_civil(2013, 7, 15)), Value::BigInt(8)]]
    );
    assert_eq!(
        failure(&db, "SELECT * REPLACE (nope + 1 AS nope) FROM hits"),
        "Column \"nope\" in REPLACE list not found in FROM clause Candidate bindings: \
         \"EventDate\", \"UserID\""
    );
}

/// The part is a string wherever it came from, and a specifier that names nothing is DuckDB's
/// message rather than a panic in a match arm.
#[test]
fn a_date_part_reads_its_specifier_three_ways_and_refuses_a_fourth() {
    let db = database();
    let stamp = "CAST('2013-07-15 10:23:45' AS TIMESTAMP)";
    for sql in [
        format!("SELECT extract(minute FROM {stamp})"),
        format!("SELECT extract('minute' FROM {stamp})"),
        format!("SELECT date_part('minute', {stamp})"),
    ] {
        assert_eq!(rows(&db, &sql), vec![vec![Value::BigInt(23)]], "{sql}");
    }
    assert!(failure(&db, &format!("SELECT date_part('qtr', {stamp})")).contains("qtr"));
}

/// A part of an interval is one of its three fields and never a sum of them, so thirty six hours
/// has no days in it and asking for the days says zero.
///
/// The column form is here as well as the single value one because the two are different loops, and
/// the parts an interval does not have are refused in both of them.
#[test]
fn a_part_of_an_interval_is_one_of_its_three_fields() {
    let db = database();
    let lengths = "FROM (VALUES (INTERVAL '14 months'), (INTERVAL '-14 months'), \
                   (CAST(NULL AS INTERVAL))) t(length)";
    let one = |value: i64| vec![vec![Value::BigInt(value)]];
    assert_eq!(rows(&db, "SELECT date_part('hour', INTERVAL '36 hours')"), one(36));
    assert_eq!(rows(&db, "SELECT date_part('day', INTERVAL '36 hours')"), one(0));
    assert_eq!(rows(&db, "SELECT extract(month FROM INTERVAL '14 months')"), one(2));
    assert_eq!(
        rows(&db, &format!("SELECT date_part('month', length) {lengths}")),
        vec![vec![Value::BigInt(2)], vec![Value::BigInt(-2)], vec![Value::Null]]
    );
    let refused = failure(&db, &format!("SELECT date_part('week', length) {lengths}"));
    assert_eq!(refused, "\"interval\" units \"week\" not recognized");
    let refused = failure(&db, "SELECT date_part('week', INTERVAL '5 days')");
    assert_eq!(refused, "\"interval\" units \"week\" not recognized");
}

/// `age` counts a gap in calendar fields, which is not what the subtraction answers, and a day it is
/// short by comes out of the earlier moment's month.
///
/// A date reaches the function by widening to a timestamp, and a time and an interval do not widen
/// anywhere, so those two are refused here the way they are refused upstream.
#[test]
fn a_gap_in_calendar_fields_is_not_the_same_as_a_difference() {
    let db = database();
    let gap = |months: i32, days: i32| vec![vec![Value::Interval { months, days, micros: 0 }]];
    let late = "TIMESTAMP '2020-07-01'";
    let early = "TIMESTAMP '2020-02-28'";
    assert_eq!(rows(&db, &format!("SELECT age({late}, {early})")), gap(4, 2));
    assert_eq!(rows(&db, &format!("SELECT age({early}, {late})")), gap(-4, -2));
    assert_eq!(rows(&db, &format!("SELECT {late} - {early}")), gap(0, 124));
    assert_eq!(rows(&db, "SELECT age(DATE '2020-07-01', DATE '2020-02-28')"), gap(4, 2));
    let moments = "FROM (VALUES (TIMESTAMP '2020-04-30', TIMESTAMP '2020-03-31'), \
                   (TIMESTAMP '2020-01-01', NULL)) t(late, early)";
    assert_eq!(
        rows(&db, &format!("SELECT age(late, early) {moments}")),
        vec![vec![Value::Interval { months: 0, days: 30, micros: 0 }], vec![Value::Null]]
    );
    let refused = failure(&db, "SELECT age(TIME '10:00:00', TIME '09:00:00')");
    assert!(refused.contains("age(TIME, TIME)"), "{refused}");
}

/// `epoch` and `julian` carry a fraction, so `date_part` is declared as a double and the binder
/// narrows it back to a bigint when the specifier is a literal naming a whole part. Midday is in
/// here because that fraction is half a day to `julian` and half of nothing to a date.
///
/// The last case is the reason the narrowing lives in the binder rather than in the function table.
/// A specifier that is a column cannot be looked at while binding, so the answer is a double even
/// when every row of it happens to say `year`, and DuckDB prints `2020.0` there for the same reason.
#[test]
fn the_two_parts_that_carry_a_fraction_are_doubles() {
    let db = database();
    let stamp = "CAST('2020-01-01 12:00:00' AS TIMESTAMP)";
    let one = |value: f64| vec![vec![Value::Double(value)]];
    assert_eq!(rows(&db, &format!("SELECT date_part('epoch', {stamp})")), one(1_577_880_000.0));
    assert_eq!(rows(&db, &format!("SELECT date_part('julian', {stamp})")), one(2_458_850.5));
    assert_eq!(rows(&db, "SELECT date_part('jd', DATE '2020-01-01')"), one(2_458_850.0));
    assert_eq!(rows(&db, "SELECT date_part('epoch', INTERVAL '1 year')"), one(31_557_600.0));
    assert_eq!(
        rows(&db, &format!("SELECT date_part('year', {stamp})")),
        vec![vec![Value::BigInt(2_020)]]
    );
    let parts = "FROM (VALUES ('year'), ('epoch')) t(part)";
    assert_eq!(
        rows(&db, &format!("SELECT date_part(part, DATE '2020-01-01') {parts}")),
        vec![vec![Value::Double(2_020.0)], vec![Value::Double(1_577_836_800.0)]]
    );
}

/// Truncating an interval keeps the fields above the part and clears the ones below it, and the
/// column form has to agree with the single value one about which those are.
#[test]
fn truncating_an_interval_keeps_the_fields_above_the_part() {
    let db = database();
    let length = "INTERVAL '14 months 10 days 06:07:08.9'";
    let months = Value::Interval { months: 14, days: 0, micros: 0 };
    assert_eq!(rows(&db, &format!("SELECT date_trunc('month', {length})")), vec![vec![months]]);
    let week = Value::Interval { months: 14, days: 7, micros: 0 };
    assert_eq!(rows(&db, &format!("SELECT date_trunc('week', {length})")), vec![vec![week]]);
    let lengths = format!("FROM (VALUES ({length}), (CAST(NULL AS INTERVAL))) t(length)");
    let hour = Value::Interval { months: 14, days: 10, micros: 6 * 3_600 * 1_000_000 };
    assert_eq!(
        rows(&db, &format!("SELECT date_trunc('hour', length) {lengths}")),
        vec![vec![hour], vec![Value::Null]]
    );
    let refused = failure(&db, &format!("SELECT date_trunc('era', {length})"));
    assert_eq!(refused, "Specifier type not implemented for DATETRUNC");
}

/// ClickBench query 29 with the aggregates cut down to one, which is the last of the forty three to
/// plan and the only one that needs a regular expression.
///
/// The pattern and the replacement are the ones the benchmark ships, character for character, which
/// is the point of the exercise. A referer with no path does not match, and the answer for a row
/// that does not match is the referer itself, which is what puts a whole URL in the group list
/// rather than dropping the row.
#[test]
fn a_referer_can_be_cut_down_to_its_host_and_grouped_by() {
    let db = Database::new();
    db.create_table("hits", vec![Field::new("Referer", LogicalType::Varchar)]).unwrap();
    db.append(
        "hits",
        &[
            vec![text("http://www.example.com/a/b")],
            vec![text("https://example.com/")],
            vec![text("http://other.org/x?y=1")],
            vec![text("")],
        ],
    )
    .unwrap();
    assert_eq!(
        rows(
            &db,
            "SELECT REGEXP_REPLACE(Referer, '^https?://(?:www\\.)?([^/]+)/.*$', '\\1') AS k, \
             COUNT(*) AS c FROM hits WHERE Referer <> '' GROUP BY k ORDER BY c DESC, k"
        ),
        vec![
            vec![text("example.com"), Value::BigInt(2)],
            vec![text("other.org"), Value::BigInt(1)]
        ]
    );
}

/// The other three, and the two ways a query can get a regular expression wrong. Every answer here
/// was read off DuckDB before it was written down.
#[test]
fn the_regular_expression_functions_answer_the_way_duckdb_does() {
    let db = database();
    assert_eq!(rows(&db, "SELECT regexp_matches('abc', 'b')"), vec![vec![Value::Boolean(true)]]);
    assert_eq!(
        rows(&db, "SELECT regexp_full_match('abc', 'a')"),
        vec![vec![Value::Boolean(false)]]
    );
    assert_eq!(
        rows(&db, "SELECT regexp_extract('abc123', '([a-z]+)([0-9]+)', 2)"),
        vec![vec![text("123")]]
    );
    assert_eq!(rows(&db, "SELECT regexp_extract('abc', 'z')"), vec![vec![text("")]]);
    assert_eq!(
        rows(&db, "SELECT regexp_replace('aXbXc', 'X', '-', 'g')"),
        vec![vec![text("a-b-c")]]
    );
    assert_eq!(rows(&db, "SELECT regexp_replace(NULL, 'a', 'b')"), vec![vec![Value::Null]]);
    // The column is the thing that varies and the pattern is not, which is the shape the kernel
    // compiles once per vector rather than once per row.
    assert_eq!(
        rows(&db, "SELECT s FROM t WHERE regexp_matches(s, '^[ac]$')"),
        vec![vec![text("a")], vec![text("c")], vec![text("a")]]
    );
    assert!(failure(&db, "SELECT regexp_matches('a', '(')").contains("missing )"));
    assert!(
        failure(&db, "SELECT regexp_replace('a', 'a', 'b', 'q')")
            .contains("Unrecognized Regex option q")
    );
}

/// `trim` was the wrong answer this engine was quietest about, so the rules of its shape are
/// asserted where it was seen rather than only in the transformer. Per #313.
///
/// `trim` itself answers now and that is #314. The other rules of the shape still refuse, and the
/// refusal is the point: a rule that wrote a keyword was stepped through as if it were a precedence
/// level, which left the argument behind as the answer.
#[test]
fn a_function_that_is_not_implemented_does_not_answer_its_own_argument() {
    let db = database();
    let message = failure(&db, "SELECT row(1)");
    assert!(message.contains("not supported yet"), "{message}");
    assert!(message.ends_with("RowExpression"), "{message}");
    assert!(failure(&db, "SELECT length(try('a'))").contains("not supported yet"));
}

/// The four string functions with a grammar rule of their own, end to end. Per #314.
///
/// Every answer below was read off the pinned binary one statement at a time, because the index
/// rules are not guessable from each other. The two worth pointing at are a start of zero, which
/// leaves a window that begins before the string and so answers one character short, and a negative
/// length, which runs the window backwards from the start instead of being empty.
#[test]
fn the_string_keywords_answer_the_way_duckdb_does() {
    let db = database();
    let one = |sql: &str| match rows(&db, sql).as_slice() {
        [row] => row.clone(),
        other => panic!("one row, not {}", other.len()),
    };
    assert_eq!(
        one("SELECT substring('abcdef', 2, 3), substring('abcdef' FROM 2 FOR 3)"),
        vec![text("bcd"), text("bcd")]
    );
    assert_eq!(
        one("SELECT substring('abcdef', 2), substring('abcdef' FOR 3), substr('abcdef', 2, 3)"),
        vec![text("bcdef"), text("abc"), text("bcd")]
    );
    assert_eq!(
        one(
            "SELECT substring('abcdef', 0, 3), substring('abcdef', -1, 3), substring('abcdef', 4, -2)"
        ),
        vec![text("ab"), text("f"), text("bc")]
    );
    assert_eq!(
        one(
            "SELECT substring('abcdef', 10, 3), substring('abcdef', -10, 3), substring('abcdef', -10)"
        ),
        vec![text(""), text(""), text("abcdef")]
    );
    assert_eq!(
        one("SELECT position('c' IN 'abcdef'), strpos('abcdef', 'z'), instr('abcdef', 'abc')"),
        vec![Value::BigInt(3), Value::BigInt(0), Value::BigInt(1)]
    );
    assert_eq!(
        one("SELECT trim('  a  '), trim(BOTH 'x' FROM 'xxaxx'), trim('xyaxy', 'xy')"),
        vec![text("a"), text("a"), text("a")]
    );
    assert_eq!(
        one("SELECT trim(LEADING FROM '  a  '), trim(TRAILING FROM '  a  '), ltrim('xxaxx', 'x')"),
        vec![text("a  "), text("  a"), text("axx")]
    );
    assert_eq!(
        one(
            "SELECT overlay('abcdef' PLACING 'X' FROM 2 FOR 1), overlay('abcdef' PLACING 'XY' FROM 2)"
        ),
        vec![text("aXcdef"), text("aXYdef")]
    );
    assert_eq!(
        one(
            "SELECT overlay('abcdef' PLACING 'XY' FROM 2 FOR 0), overlay('abcdef' PLACING 'XY' FROM 2 FOR -1)"
        ),
        vec![text("aXYbcdef"), text("aXYdef")]
    );
    // A null anywhere is a null answer, and the types are the ones upstream declares.
    assert_eq!(
        one("SELECT substring(NULL, 1, 2), trim(NULL), strpos('a', NULL)"),
        vec![Value::Null, Value::Null, Value::Null]
    );
    assert_eq!(
        one("SELECT typeof(substring('abcdef', 2)), typeof(strpos('a', 'b'))"),
        vec![text("VARCHAR"), text("BIGINT")]
    );
    // Over a column, including the null row.
    assert_eq!(
        rows(&db, "SELECT substring(s, 1, 1) FROM t"),
        vec![vec![text("a")], vec![Value::Null], vec![text("c")], vec![text("a")]]
    );
    // The names upstream gives these columns, which are the lowered calls and not what was written.
    // Three of the four are keywords and are quoted, which is #251, and `ltrim` is not a keyword
    // and is not quoted, which is the same rule and is why the last one looks different.
    assert_eq!(
        db.query("SELECT substring(s FROM 2 FOR 3) FROM t").unwrap().names(),
        &["\"substring\"(s, 2, 3)".to_string()]
    );
    assert_eq!(
        db.query("SELECT position('c' IN s) FROM t").unwrap().names(),
        &["\"position\"(s, 'c')".to_string()]
    );
    assert_eq!(
        db.query("SELECT trim(LEADING FROM s) FROM t").unwrap().names(),
        &["ltrim(s)".to_string()]
    );
    // An index has to be a whole number already, which is upstream's rule rather than a rounding.
    let message = failure(&db, "SELECT substring('abcdef', 2.5, 3)");
    assert!(message.contains("\"substring\"(col0 VARCHAR, col1 BIGINT, col2 BIGINT) -> VARCHAR"));
    assert!(failure(&db, "SELECT trim(123)").contains("\"trim\"(col0 VARCHAR) -> VARCHAR"));
    assert!(failure(&db, "SELECT strpos('abcdef')").contains("strpos(col0 VARCHAR, col1 VARCHAR)"));
}

/// An identifier inside a generated column name is quoted where upstream quotes it. Per #251.
///
/// Every name below was read off the pinned binary. The rule turns out to be two rules and neither
/// of them is the obvious one. A word is quoted when it is a keyword in one of the grammar's
/// classes, so `name` and `action` are quoted and `alias` is not, even though `alias` is in the
/// keyword table, because it is spelled by a rule and belongs to no class. And a word is quoted
/// when it is not one plain ASCII word, so a space, a leading digit and an accent all keep their
/// quotes.
///
/// What is not a reason is case. `UserID` comes back unquoted from upstream even though reading
/// that name again would fold it to `userid`, and that is the case ClickBench is made of.
#[test]
fn a_generated_name_quotes_an_identifier_where_duckdb_quotes_one() {
    let db = Database::new();
    let fields = ["name", "alias", "UserID", "my col", "9x"]
        .iter()
        .map(|name| Field::new(*name, LogicalType::Integer))
        .collect();
    db.create_table("q", fields).unwrap();
    let names = |sql: &str| db.query(sql).unwrap().names().to_vec();
    assert_eq!(names("SELECT min(name) FROM q"), &["min(\"name\")".to_string()]);
    assert_eq!(names("SELECT min(alias) FROM q"), &["min(alias)".to_string()]);
    assert_eq!(names("SELECT min(\"UserID\") FROM q"), &["min(UserID)".to_string()]);
    assert_eq!(names("SELECT min(\"my col\") FROM q"), &["min(\"my col\")".to_string()]);
    assert_eq!(names("SELECT min(\"9x\") FROM q"), &["min(\"9x\")".to_string()]);
    // The column on its own is not a generated name at all. It comes from the catalog, so it is
    // the spelling the table was created with and nothing quotes it.
    assert_eq!(names("SELECT name FROM q"), &["name".to_string()]);
    // The same rule everywhere a name is generated, and not only inside an aggregate.
    assert_eq!(names("SELECT name + 1 FROM q"), &["(\"name\" + 1)".to_string()]);
    assert_eq!(
        names("SELECT CAST(name AS VARCHAR) FROM q"),
        &["CAST(\"name\" AS VARCHAR)".to_string()]
    );
    assert_eq!(names("SELECT name IS NULL FROM q"), &["(\"name\" IS NULL)".to_string()]);
}

/// The five string functions a corpus file reaches for constantly, end to end. Per #330.
///
/// Every answer below was read off the pinned binary, because none of it follows from the names.
/// The three that are worth pinning are `concat` dropping a null instead of propagating it, a
/// negative count to `left` counting from the other end instead of raising, and an empty needle to
/// `replace` changing nothing instead of matching everywhere.
#[test]
fn the_five_string_functions_answer_the_way_duckdb_does() {
    let db = database();
    assert_eq!(rows(&db, "SELECT chr(65), chr(233)"), vec![vec![text("A"), text("é")]]);
    assert_eq!(rows(&db, "SELECT length(chr(0))"), vec![vec![Value::BigInt(1)]]);
    assert_eq!(
        rows(&db, "SELECT left('héllo', 2), right('héllo', 2)"),
        vec![vec![text("hé"), text("lo")]]
    );
    assert_eq!(
        rows(&db, "SELECT left('abc', -1), right('abc', -1), left('abc', 0), right('abc', 99)"),
        vec![vec![text("ab"), text("bc"), text(""), text("abc")]]
    );
    assert_eq!(
        rows(&db, "SELECT replace('abc', 'b', 'x'), replace('aaa', '', 'x')"),
        vec![vec![text("axc"), text("aaa")]]
    );
    // `concat` is above the null rule and the other four are not, which is the one line of this
    // that a reader would otherwise have to go and measure.
    assert_eq!(rows(&db, "SELECT concat('a', 1, NULL)"), vec![vec![text("a1")]]);
    assert_eq!(rows(&db, "SELECT concat(NULL)"), vec![vec![text("")]]);
    assert_eq!(
        rows(&db, "SELECT replace('abc', 'b', NULL), left(NULL, 2), chr(NULL)"),
        vec![vec![Value::Null, Value::Null, Value::Null]]
    );
    // The names upstream gives these columns. Three of the five are keywords and are quoted.
    assert_eq!(
        db.query("SELECT LEFT('abc', 2), chr(65)").unwrap().names(),
        &["\"left\"('abc', 2)".to_string(), "chr(65)".to_string()]
    );
    // A code point that is no code point, and the counts and types the signature refuses. `chr`
    // has one overload upstream and it narrows nothing to reach it.
    assert_eq!(failure(&db, "SELECT chr(55296)"), "Invalid UTF8 Codepoint 55296");
    assert!(failure(&db, "SELECT chr(65.9)").contains("chr(col0 INTEGER) -> VARCHAR"));
    assert!(
        failure(&db, "SELECT left('abc')")
            .contains("\"left\"(col0 VARCHAR, col1 BIGINT) -> VARCHAR")
    );
    assert!(failure(&db, "SELECT concat()").contains("concat(col0 ANY, [ANY...]) -> ANY"));
}

/// The interval literal, end to end, which is a function call by the time anything binds it.
/// Per #360.
///
/// The column names are the argument that this is the same rewrite DuckDB does rather than one that
/// happens to agree on the values, so they are asserted next to the answers rather than separately.
/// Every one of them was read off the pinned binary.
#[test]
fn an_interval_literal_is_the_call_duckdb_rewrites_it_into() {
    let db = database();
    let interval = |months, days, micros| Value::Interval { months, days, micros };
    assert_eq!(
        db.query("SELECT INTERVAL 1 DAY").unwrap().names(),
        &["to_days(CAST(trunc(CAST(1 AS DOUBLE)) AS INTEGER))".to_string()]
    );
    assert_eq!(
        db.query("SELECT INTERVAL 90 SECOND").unwrap().names(),
        &["to_seconds(CAST(90 AS DOUBLE))".to_string()]
    );
    assert_eq!(rows(&db, "SELECT INTERVAL 1 DAY"), vec![vec![interval(0, 1, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL 1 DAYS"), vec![vec![interval(0, 1, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL 2 MONTHS"), vec![vec![interval(2, 0, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL 1 WEEK"), vec![vec![interval(0, 7, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL 1 MILLENNIUM"), vec![vec![interval(12_000, 0, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL (1+1) DAY"), vec![vec![interval(0, 2, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL (-1) DAY"), vec![vec![interval(0, -1, 0)]]);
    // The truncation is in the rewrite rather than in the function, so a day and a half is a day,
    // and the two units that keep their fraction do not go through it at all.
    assert_eq!(rows(&db, "SELECT INTERVAL 1.5 DAY"), vec![vec![interval(0, 1, 0)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL 2.7 SECOND"), vec![vec![interval(0, 0, 2_700_000)]]);
    assert_eq!(rows(&db, "SELECT INTERVAL 1 MILLISECOND"), vec![vec![interval(0, 0, 1_000)]]);
    // Written out by hand it is the same call, which is what the rewrite being a rewrite means.
    assert_eq!(rows(&db, "SELECT to_days(1)"), vec![vec![interval(0, 1, 0)]]);
    assert_eq!(rows(&db, "SELECT to_quarters(5)"), vec![vec![interval(15, 0, 0)]]);
    assert_eq!(rows(&db, "SELECT to_days(NULL)"), vec![vec![Value::Null]]);
    // A count reaches its type by widening, so an INTEGER count of hours and a DECIMAL count of
    // seconds both bind, and a DECIMAL count of days does not, because it would have to narrow.
    assert_eq!(rows(&db, "SELECT to_hours(25)"), vec![vec![interval(0, 0, 90_000_000_000)]]);
    assert_eq!(rows(&db, "SELECT to_seconds(1.5)"), vec![vec![interval(0, 0, 1_500_000)]]);
    assert!(failure(&db, "SELECT to_days(1.7)").contains("to_days(col0 INTEGER) -> INTERVAL"));
    // The seven range forms parse and then refuse in upstream's own words, with the units spelled
    // the canonical way rather than the way they were written.
    assert_eq!(failure(&db, "SELECT INTERVAL 1 DAYS TO HOURS"), "DAY TO HOUR is not supported");
    assert_eq!(
        failure(&db, "SELECT to_years(2147483647)"),
        "Interval value 2147483647 years out of range"
    );
}

/// The three spellings of a null check, end to end. Per #306.
///
/// Every answer and every column name below was read off the pinned binary. `nullif` is a macro
/// there, `CASE WHEN a = b THEN NULL ELSE a END`, and the two things that follow from that are worth
/// pointing at: a null on the right is not a match, because `1 = NULL` is null rather than true, and
/// the answer keeps the first argument's type even when the comparison had to widen to happen at all.
#[test]
fn the_null_checks_answer_the_way_duckdb_does() {
    let db = database();
    assert_eq!(rows(&db, "SELECT COALESCE(NULL, 1)"), vec![vec![Value::Integer(1)]]);
    assert_eq!(rows(&db, "SELECT coalesce(NULL, NULL, 3, 4)"), vec![vec![Value::Integer(3)]]);
    assert_eq!(rows(&db, "SELECT coalesce(NULL, NULL)"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT coalesce(2)"), vec![vec![Value::Integer(2)]]);
    assert_eq!(
        rows(&db, "SELECT ifnull(NULL, 3), ifnull(1, 3)"),
        vec![vec![Value::Integer(3), Value::Integer(1)]]
    );
    assert_eq!(
        rows(&db, "SELECT nullif(1, 2), nullif(2, 2)"),
        vec![vec![Value::Integer(1), Value::Null]]
    );
    // A null on either side. The right one is not a match and the left one is the answer.
    assert_eq!(
        rows(&db, "SELECT nullif(1, NULL), nullif(NULL, 1)"),
        vec![vec![Value::Integer(1), Value::Null]]
    );
    assert_eq!(
        rows(&db, "SELECT nullif('a', 'a'), nullif('a', 'b')"),
        vec![vec![Value::Null, text("a")]]
    );
    assert_eq!(rows(&db, "SELECT nullif(TRUE, FALSE)"), vec![vec![Value::Boolean(true)]]);
    // The comparison happens at DECIMAL(11,1) and the answer is still an INTEGER, both measured.
    assert_eq!(
        rows(&db, "SELECT nullif(2, 2.5), typeof(nullif(2, 2.5))"),
        vec![vec![Value::Integer(2), text("INTEGER")]]
    );
    assert_eq!(
        rows(&db, "SELECT typeof(coalesce(1, 2.5)), typeof(nullif(1::BIGINT, 2::SMALLINT))"),
        vec![vec![text("DECIMAL(11,1)"), text("BIGINT")]]
    );
    // Over a column, where the null row is the one that moves.
    assert_eq!(
        rows(&db, "SELECT coalesce(s, 'none') FROM t"),
        vec![vec![text("a")], vec![text("none")], vec![text("c")], vec![text("a")]]
    );
    assert_eq!(
        rows(&db, "SELECT nullif(x, 1) FROM t"),
        vec![
            vec![Value::Integer(3)],
            vec![Value::Null],
            vec![Value::Integer(2)],
            vec![Value::Null]
        ]
    );
    // The names upstream gives these columns. `COALESCE` is an operator there rather than a
    // function, so it is printed in capitals whichever case was written and `IFNULL` becomes it.
    assert_eq!(
        db.query("SELECT coalesce(x, 1) FROM t").unwrap().names(),
        &["COALESCE(x, 1)".to_string()]
    );
    assert_eq!(
        db.query("SELECT IFNULL(x, 1) FROM t").unwrap().names(),
        &["COALESCE(x, 1)".to_string()]
    );
    // This one is quoted because NULLIF is a keyword, and `COALESCE` above it is not because that
    // one is an operator upstream rather than a name the deparser ever writes. Per #251.
    assert_eq!(
        db.query("SELECT NULLIF(x, 1) FROM t").unwrap().names(),
        &["\"nullif\"(x, 1)".to_string()]
    );
    // The counts the grammar refuses, and the one upstream's parser refuses itself.
    assert!(failure(&db, "SELECT nullif(1)").contains("syntax error at or near \")\""));
    assert!(failure(&db, "SELECT nullif(1, 2, 3)").contains("syntax error at or near \",\""));
    assert!(failure(&db, "SELECT coalesce()").contains("syntax error at or near \")\""));
    assert_eq!(failure(&db, "SELECT ifnull(1)"), "Wrong number of arguments to IFNULL.");
    assert_eq!(failure(&db, "SELECT ifnull(1, 2, 3)"), "Wrong number of arguments to IFNULL.");
}

/// `typeof` names the type of an expression, which is decided before a row moves. Per #229.
///
/// Every answer here was read off the pinned binary. The one worth pointing at is `typeof(NULL)`,
/// which is six characters and not four: the quotes are part of the name upstream prints and the
/// reason is visible in `typeof([])`, which comes back as `"NULL"[]`.
#[test]
fn typeof_answers_the_name_of_the_type() {
    let db = database();
    let named = |sql: &str| match rows(&db, sql).as_slice() {
        [row] => row.clone(),
        other => panic!("one row, not {}", other.len()),
    };
    assert_eq!(
        named("SELECT typeof(1), typeof(1.5), typeof('a'), typeof(TRUE), typeof(NULL)"),
        vec![
            text("INTEGER"),
            text("DECIMAL(2,1)"),
            text("VARCHAR"),
            text("BOOLEAN"),
            text("\"NULL\"")
        ]
    );
    assert_eq!(
        named("SELECT typeof(1::BIGINT), typeof('2024-01-01'::DATE), typeof(1 + 2), typeof(1 / 2)"),
        vec![text("BIGINT"), text("DATE"), text("INTEGER"), text("DOUBLE")]
    );
    // Over a table, where the type is the column's and the rows are all the same.
    assert_eq!(
        named("SELECT DISTINCT typeof(x), typeof(s), typeof(x + 1) FROM t"),
        vec![text("INTEGER"), text("VARCHAR"), text("INTEGER")]
    );
    // And over an aggregate, where the type is the one the aggregate accumulates in.
    assert_eq!(
        named("SELECT typeof(count(*)), typeof(sum(x)), typeof(avg(x)) FROM t"),
        vec![text("BIGINT"), text("HUGEINT"), text("DOUBLE")]
    );
    // The name a column gets is the call as it was written, which is what upstream calls it too.
    assert_eq!(db.query("SELECT typeof(x) FROM t").unwrap().names(), &["typeof(x)".to_string()]);
    // One argument, and the sentence for the other counts is the table's own.
    assert!(failure(&db, "SELECT typeof(1, 2)").contains("typeof(col0 ANY) -> VARCHAR"));
}

/// Brackets on a string, which the transformer writes as `array_extract` and `array_slice` and the
/// binder then resolves like any other call. Per #278.
#[test]
fn a_bracket_on_a_string_indexes_it_by_character() {
    let db = database();
    assert_eq!(rows(&db, "SELECT 'abcdef'[2]"), vec![vec![text("b")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[-1]"), vec![vec![text("f")]]);
    // Off either end is the empty string rather than a null, which is a string only rule.
    assert_eq!(rows(&db, "SELECT 'abcdef'[0]"), vec![vec![text("")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[9]"), vec![vec![text("")]]);
    assert_eq!(rows(&db, "SELECT 'héllo'[2]"), vec![vec![text("é")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[2:4]"), vec![vec![text("bcd")]]);
    // The four ways of leaving a bound out, which the transformer fills in before the binder sees
    // them, and the two that clamp.
    assert_eq!(rows(&db, "SELECT 'abcdef'[:3]"), vec![vec![text("abc")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[4:]"), vec![vec![text("def")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[:]"), vec![vec![text("abcdef")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[1:-]"), vec![vec![text("abcdef")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[3:99]"), vec![vec![text("cdef")]]);
    assert_eq!(rows(&db, "SELECT 'abcdef'[4:2]"), vec![vec![text("")]]);
    // A column rather than a literal, and a null row, which stays null through both calls.
    assert_eq!(
        rows(&db, "SELECT s[1], s[1:1] FROM t"),
        vec![
            vec![text("a"), text("a")],
            vec![Value::Null, Value::Null],
            vec![text("c"), text("c")],
            vec![text("a"), text("a")],
        ]
    );
    // The same two calls written out, including the three spellings that are aliases of them.
    assert_eq!(rows(&db, "SELECT array_extract('abcdef', 2)"), vec![vec![text("b")]]);
    assert_eq!(rows(&db, "SELECT list_extract('abcdef', 2)"), vec![vec![text("b")]]);
    assert_eq!(rows(&db, "SELECT list_element('abcdef', 2)"), vec![vec![text("b")]]);
    assert_eq!(rows(&db, "SELECT list_slice('abcdef', 2, 4)"), vec![vec![text("bcd")]]);
}

/// One index into a list. Per #278.
#[test]
fn a_bracket_on_a_list_picks_one_element_out_of_it() {
    let db = database();
    assert_eq!(rows(&db, "SELECT [1,2,3][2]"), vec![vec![integer(2)]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][-1]"), vec![vec![integer(3)]]);
    // Off either end of a list is a null, which is where the list and the string disagree.
    assert_eq!(rows(&db, "SELECT [1,2,3][0]"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][4]"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT list_extract([1,2,3], 2)"), vec![vec![integer(2)]]);
}

/// The list half of #278, which could not be reached through SQL until #302.
///
/// Every rule here was measured in `rudb-kernels/src/subscript.rs` when the slice was written, and
/// none of it could be checked end to end because the answer is a list and a list could not be put in
/// a vector. So the rules were right and the path from the parser to the printed row was untested,
/// which is the split this project tries not to have. These are the same thirteen cases read off the
/// pinned binary.
#[test]
fn a_slice_of_a_list_answers_a_list() {
    let db = database();
    assert_eq!(rows(&db, "SELECT [1,2,3][1:2]"), vec![vec![list(&[1, 2])]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][2:]"), vec![vec![list(&[2, 3])]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][:2]"), vec![vec![list(&[1, 2])]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][:]"), vec![vec![list(&[1, 2, 3])]]);
    // A list is one based, so zero is the same place one is rather than an error or an empty list.
    assert_eq!(rows(&db, "SELECT [1,2,3][0:2]"), vec![vec![list(&[1, 2])]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][-2:-1]"), vec![vec![list(&[2, 3])]]);
    // Both ends are clamped rather than refused, so a slice can name rows that are not there and a
    // backwards slice is the empty list and not an error.
    assert_eq!(rows(&db, "SELECT [1,2,3][2:99]"), vec![vec![list(&[2, 3])]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][-99:99]"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(rows(&db, "SELECT [1,2,3][3:1]"), vec![vec![list(&[])]]);
    assert_eq!(rows(&db, "SELECT array_slice([1,2,3], 2, 3)"), vec![vec![list(&[2, 3])]]);
    assert_eq!(rows(&db, "SELECT list_slice([1,2,3], 1, 1)"), vec![vec![list(&[1])]]);
    // A null bound makes the whole slice null, and a null list slices to a null rather than to an
    // empty list, which is the difference #302 made the vector able to carry.
    assert_eq!(rows(&db, "SELECT [1,2,3][NULL:2]"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT (NULL::INT[])[1:2]"), vec![vec![Value::Null]]);
}

/// A list column, stored and read back. Per #302.
#[test]
fn a_list_column_keeps_an_empty_list_and_a_null_apart() {
    let db = database();
    db.execute("CREATE TABLE lists (a INTEGER[])").unwrap();
    db.execute("INSERT INTO lists VALUES ([1,2,3]), (NULL), ([]), ([4])").unwrap();
    assert_eq!(
        rows(&db, "SELECT a FROM lists"),
        vec![vec![list(&[1, 2, 3])], vec![Value::Null], vec![list(&[])], vec![list(&[4])],]
    );
    // The mask is what tells the empty list from the null, so the count and the null test are the
    // two questions that would give the same answer if it did not.
    assert_eq!(rows(&db, "SELECT count(a) FROM lists"), vec![vec![Value::BigInt(3)]]);
    assert_eq!(
        rows(&db, "SELECT a IS NULL FROM lists"),
        vec![
            vec![Value::Boolean(false)],
            vec![Value::Boolean(true)],
            vec![Value::Boolean(false)],
            vec![Value::Boolean(false)],
        ]
    );
}

/// A list literal is a call to `list_value`. Per #467.
///
/// Which is what lets a column go inside one, and what makes the rule that decides the element type
/// of `[a, b]` the same rule that decides it for `list_value(a, b)`, written once.
#[test]
fn a_list_can_be_written_over_columns_and_not_only_over_constants() {
    let db = database();
    assert_eq!(rows(&db, "SELECT [x, 2] FROM t WHERE x = 3"), vec![vec![list(&[3, 2])]]);
    // Every element promotes to one type, so a list of an integer and a double holds doubles rather
    // than being refused or holding two types.
    assert_eq!(
        rows(&db, "SELECT typeof([x, 2.5::DOUBLE]) FROM t WHERE x = 3"),
        vec![vec![text("DOUBLE[]")]]
    );
    // Nothing to promote is the list of the untyped null, which is the pin's answer for `[]` and is
    // not a guess at what somebody meant to put in it.
    assert_eq!(rows(&db, "SELECT typeof([])"), vec![vec![text("\"NULL\"[]")]]);
    // The same function under its other name.
    assert_eq!(rows(&db, "SELECT list_pack(1, 2)"), vec![vec![list(&[1, 2])]]);
}

/// A list cast is a cast of every element. Per #467.
#[test]
fn a_list_casts_by_casting_every_element_of_it() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT CAST([1, 2] AS BIGINT[])"),
        vec![vec![Value::List {
            element: LogicalType::BigInt,
            values: vec![Value::BigInt(1), Value::BigInt(2)],
        }]]
    );
    assert_eq!(
        rows(&db, "SELECT [1, 2]::VARCHAR[]"),
        vec![vec![Value::List {
            element: LogicalType::Varchar,
            values: vec![text("1"), text("2")]
        }]]
    );
    // The element type comes from the target, so an empty list arrives as the thing it was asked to
    // be rather than staying the list of untyped nulls it was written as.
    assert_eq!(rows(&db, "SELECT CAST([] AS INTEGER[])"), vec![vec![list(&[])]]);
    // A depth of two is the same rule again, because the element of a list of lists is a list.
    assert_eq!(
        rows(&db, "SELECT CAST([[1], [2]] AS VARCHAR[][])"),
        vec![vec![Value::List {
            element: LogicalType::list(LogicalType::Varchar),
            values: vec![
                Value::List { element: LogicalType::Varchar, values: vec![text("1")] },
                Value::List { element: LogicalType::Varchar, values: vec![text("2")] },
            ],
        }]]
    );
}

/// `TRY_CAST` over a list nulls the element that would not go and keeps the list. Per #467.
#[test]
fn a_try_cast_of_a_list_keeps_the_elements_that_went() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT TRY_CAST(['x', '2'] AS INTEGER[])"),
        vec![vec![Value::List {
            element: LogicalType::Integer,
            values: vec![Value::Null, integer(2)],
        }]]
    );
    // Without the TRY it is the element's own failure, reported in the element's types rather than
    // in the list's.
    assert_eq!(
        failure(&db, "SELECT CAST(['x'] AS INTEGER[])"),
        "Could not convert string 'x' to INT32"
    );
    assert_eq!(
        failure(&db, "SELECT CAST([1, 2] AS BLOB[])"),
        "Unimplemented type for cast (INTEGER -> BLOB)"
    );
    // A list going somewhere that is not a list is the whole list's failure and names the list.
    assert_eq!(
        failure(&db, "SELECT CAST([1, 2] AS INTEGER)"),
        "Unimplemented type for cast (INTEGER[] -> INTEGER)"
    );
}

/// Two lists compare the way two words are ordered in a dictionary. Per #467.
#[test]
fn a_list_compares_element_by_element_and_then_by_length() {
    let db = database();
    let yes = vec![vec![Value::Boolean(true)]];
    let no = vec![vec![Value::Boolean(false)]];
    assert_eq!(rows(&db, "SELECT [1, 2] = [1, 2]"), yes);
    assert_eq!(rows(&db, "SELECT [1, 2] = [1, 3]"), no);
    assert_eq!(rows(&db, "SELECT [1, 2] < [1, 3]"), yes);
    assert_eq!(rows(&db, "SELECT ['a'] < ['b']"), yes);
    // The element of a list of lists is a list, so the rule runs at whatever depth it was written.
    assert_eq!(rows(&db, "SELECT [[1], [2]] < [[1], [3]]"), yes);
    // Nothing disagreed before one of them ran out, so the shorter one is the smaller one. A list is
    // never equal to a longer list that starts with it.
    assert_eq!(rows(&db, "SELECT [1, 2] < [1, 2, 3]"), yes);
    assert_eq!(rows(&db, "SELECT [1, 2] = [1, 2, 3]"), no);
    assert_eq!(rows(&db, "SELECT [] < [1]"), yes);
    assert_eq!(rows(&db, "SELECT [] = []"), yes);
}

/// A null inside a list and a list that is null are two different things. Per #467.
///
/// Inside a list a null is the largest value there is and equals itself, which is the sort's rule
/// rather than the comparison's. A list that is null is a null like any other and makes the whole
/// comparison null. Every row here is the pin's.
#[test]
fn a_null_inside_a_list_is_the_largest_element_and_a_null_list_is_still_null() {
    let db = database();
    let yes = vec![vec![Value::Boolean(true)]];
    let no = vec![vec![Value::Boolean(false)]];
    assert_eq!(rows(&db, "SELECT [1, NULL] = [1, NULL]"), yes);
    assert_eq!(rows(&db, "SELECT [1, NULL] = [1, 2]"), no);
    assert_eq!(rows(&db, "SELECT [1, NULL] > [1, 2]"), yes);
    assert_eq!(rows(&db, "SELECT [NULL] < [1]"), no);
    assert_eq!(rows(&db, "SELECT [1, 2] IS DISTINCT FROM [1, NULL]"), yes);
    assert_eq!(rows(&db, "SELECT NULL::INT[] = [1]"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT NULL::INT[] IS NOT DISTINCT FROM NULL::INT[]"), yes);
}

/// Everything that rides on the order rides on it for lists too. Per #467.
///
/// `ORDER BY`, `DISTINCT`, `IN` and `max` all reach the same comparison, so the point of this is
/// that they reach it rather than that any one of them is interesting on its own.
#[test]
fn a_list_sorts_and_groups_and_maxes_like_any_other_value() {
    let db = database();
    db.execute("CREATE TABLE ls (x INTEGER[])").unwrap();
    db.execute("INSERT INTO ls VALUES ([1, NULL]), ([1, 2]), ([NULL]), (NULL), ([])").unwrap();
    // The null list sorts last because nulls sort last, and `[NULL]` sorts after `[1, NULL]` because
    // a null element is larger than any element.
    assert_eq!(
        rows(&db, "SELECT x FROM ls ORDER BY x"),
        vec![
            vec![list(&[])],
            vec![list(&[1, 2])],
            vec![Value::List {
                element: LogicalType::Integer,
                values: vec![integer(1), Value::Null],
            }],
            vec![Value::List { element: LogicalType::Integer, values: vec![Value::Null] }],
            vec![Value::Null],
        ]
    );
    assert_eq!(
        rows(&db, "SELECT max(x) FROM ls"),
        vec![vec![Value::List { element: LogicalType::Integer, values: vec![Value::Null] }]]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FROM (SELECT DISTINCT x FROM ls)"),
        vec![vec![Value::BigInt(5)]]
    );
    assert_eq!(rows(&db, "SELECT [1, 2] IN ([1, 2], [3])"), vec![vec![Value::Boolean(true)]]);
}

/// Two lists joined end to end, by the operator and by the name. Per #467.
#[test]
fn two_lists_concatenate_with_the_operator_and_with_the_name() {
    let db = database();
    assert_eq!(rows(&db, "SELECT [1, 2] || [3]"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(rows(&db, "SELECT [1] || [2] || [3]"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(rows(&db, "SELECT [] || [1]"), vec![vec![list(&[1])]]);
    assert_eq!(rows(&db, "SELECT [1] || []"), vec![vec![list(&[1])]]);
    // The elements promote the way a list literal's do, so the answer is a list of what the two
    // element types meet at rather than a list of the left one's.
    assert_eq!(rows(&db, "SELECT typeof([1] || [2.5::DOUBLE])"), vec![vec![text("DOUBLE[]")]]);
    assert_eq!(rows(&db, "SELECT typeof([1, 2] || [3.5])"), vec![vec![text("DECIMAL(11,1)[]")]]);
    assert_eq!(rows(&db, "SELECT typeof([] || [])"), vec![vec![text("\"NULL\"[]")]]);
    assert_eq!(
        rows(&db, "SELECT [1] || [NULL]"),
        vec![vec![Value::List {
            element: LogicalType::Integer,
            values: vec![integer(1), Value::Null],
        }]]
    );
    // The name, and the three other names the pin answers to for it.
    assert_eq!(rows(&db, "SELECT list_concat([1], [2], [3])"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(rows(&db, "SELECT list_cat([1], [2])"), vec![vec![list(&[1, 2])]]);
    assert_eq!(rows(&db, "SELECT array_concat([1], [2])"), vec![vec![list(&[1, 2])]]);
    assert_eq!(rows(&db, "SELECT array_cat([1], [2])"), vec![vec![list(&[1, 2])]]);
    // A list column and not only a list literal, which is the row at a time path rather than the
    // folder.
    db.execute("CREATE TABLE cs (a INTEGER[], b INTEGER[])").unwrap();
    db.execute("INSERT INTO cs VALUES ([1], [2]), ([], [3, 4])").unwrap();
    assert_eq!(
        rows(&db, "SELECT a || b FROM cs ORDER BY 1"),
        vec![vec![list(&[1, 2])], vec![list(&[3, 4])]]
    );
}

/// `length` counts a list's elements and still counts a string's characters. Per #467.
///
/// It used to cast the list to a string first and count the characters of that, so
/// `length([1, 2, 3])` was 9. Every row here is the pin's.
#[test]
fn the_length_of_a_list_is_how_many_elements_it_has() {
    let db = database();
    let count = |sql: &str| rows(&db, sql);
    assert_eq!(count("SELECT length([1, 2, 3])"), vec![vec![Value::BigInt(3)]]);
    assert_eq!(count("SELECT length([])"), vec![vec![Value::BigInt(0)]]);
    assert_eq!(count("SELECT length([1, NULL])"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(count("SELECT length([[1], [2]])"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(count("SELECT length(NULL::INT[])"), vec![vec![Value::Null]]);
    assert_eq!(count("SELECT len([1, 2])"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(count("SELECT char_length([1, 2])"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(count("SELECT length('héllo')"), vec![vec![Value::BigInt(5)]]);
    assert_eq!(count("SELECT length(NULL)"), vec![vec![Value::Null]]);
    // A list column, and not only a literal the folder answers.
    db.execute("CREATE TABLE ns (x INTEGER[])").unwrap();
    db.execute("INSERT INTO ns VALUES ([1, 2]), ([]), (NULL)").unwrap();
    assert_eq!(
        rows(&db, "SELECT length(x) FROM ns"),
        vec![vec![Value::BigInt(2)], vec![Value::BigInt(0)], vec![Value::Null]]
    );
    assert!(failure(&db, "SELECT length(123)").contains("length(col0 ANY[]) -> BIGINT"));
}

/// `array_length` is the list half of `length`, with a dimension. Per #467.
#[test]
fn array_length_counts_a_list_along_its_first_dimension_and_only_that_one() {
    let db = database();
    assert_eq!(rows(&db, "SELECT array_length([1, 2, 3])"), vec![vec![Value::BigInt(3)]]);
    assert_eq!(rows(&db, "SELECT array_length([[1, 2], [3]], 1)"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(rows(&db, "SELECT array_length([1, 2, 3], NULL)"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT array_length(NULL, 1)"), vec![vec![Value::Null]]);
    assert_eq!(
        failure(&db, "SELECT array_length([1, 2], 2)"),
        "array_length for lists with dimensions other than 1 not implemented"
    );
    // No string reading, and a dimension that is not a whole number is not rounded into one.
    for refused in ["SELECT array_length('abc')", "SELECT array_length([1, 2], 1.5)"] {
        assert!(
            failure(&db, refused).contains("array_length(col0 ANY[], col1 BIGINT) -> BIGINT"),
            "{refused}"
        );
    }
}

/// A list against something that is not a list is neither reading of `||`. Per #467.
///
/// It has to be refused rather than falling back to the string reading, because the string reading
/// would answer it. `[1, 2] || 3` would be `[1, 2]3`, which is not a wrong list so much as a list
/// printed by accident.
#[test]
fn concatenating_a_list_with_something_that_is_not_one_is_refused() {
    let db = database();
    assert_eq!(
        failure(&db, "SELECT [1, 2] || 3"),
        "Cannot concatenate types INTEGER[] and INTEGER - an explicit cast is required"
    );
    assert_eq!(
        failure(&db, "SELECT 'a' || ['b']"),
        "Cannot concatenate types VARCHAR and VARCHAR[] - an explicit cast is required"
    );
    // Two lists whose elements will not meet anywhere is a different sentence, because the two
    // arguments are both the right shape and it is what is inside them that does not agree.
    assert_eq!(
        failure(&db, "SELECT [1] || ['a']"),
        "Cannot concatenate lists of types INTEGER[] and VARCHAR[] - an explicit cast is required"
    );
    // The name splits the two the same way. The wrong shape is the candidate block and elements that
    // will not meet is the sentence above.
    assert_eq!(
        failure(&db, "SELECT list_concat([1], ['a'])"),
        "Cannot concatenate lists of types INTEGER[] and VARCHAR[] - an explicit cast is required"
    );
    assert!(
        failure(&db, "SELECT list_concat([1], 2)").contains("list_concat([ANY[]...]) -> ANY[]")
    );
    // The string reading is untouched by any of this and still takes anything.
    assert_eq!(rows(&db, "SELECT 1 || 'a'"), vec![vec![text("1a")]]);
}

/// The operator propagates a null and the name skips it. Per #467.
///
/// This is the whole of the difference between `||` over two lists and `list_concat` over the same
/// two, and it is the reason the two are not one function with an alias pointing at it. The name
/// follows `concat`'s rule over strings and the operator follows every other operator's.
#[test]
fn a_null_stops_the_operator_and_is_skipped_by_the_name() {
    let db = database();
    assert_eq!(rows(&db, "SELECT [1, 2] || NULL::INT[]"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT [1, 2] || NULL"), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, "SELECT list_concat([1], NULL::INT[])"), vec![vec![list(&[1])]]);
    assert_eq!(rows(&db, "SELECT list_concat([1], NULL)"), vec![vec![list(&[1])]]);
    assert_eq!(rows(&db, "SELECT typeof(list_concat([1], NULL))"), vec![vec![text("INTEGER[]")]]);
    assert_eq!(rows(&db, "SELECT list_concat([1], [2], NULL, [3])"), vec![vec![list(&[1, 2, 3])]]);
    // Every argument was null, which is a null answer and not an empty list. An empty list argument
    // is a different thing and does give one back.
    assert_eq!(rows(&db, "SELECT list_concat(NULL, NULL)"), vec![vec![Value::Null]]);
    assert_eq!(
        rows(&db, "SELECT list_concat([], [])"),
        vec![vec![Value::List { element: LogicalType::Null, values: Vec::new() }]]
    );
}

/// `list_append` and its five relatives are `list_concat` with the value wrapped in a list, which is
/// how the pin defines them. Per #467.
///
/// So everything about them is `list_concat`'s, down to the name in the refusal, and every row here
/// is the pin's.
#[test]
fn appending_to_a_list_is_concatenating_a_list_of_one() {
    let db = database();
    let one = |sql: &str| rows(&db, sql);
    assert_eq!(one("SELECT list_append([1, 2], 3)"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(one("SELECT array_append([1, 2], 3)"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(one("SELECT array_push_back([1, 2], 3)"), vec![vec![list(&[1, 2, 3])]]);
    assert_eq!(one("SELECT list_prepend(0, [1, 2])"), vec![vec![list(&[0, 1, 2])]]);
    assert_eq!(one("SELECT array_prepend(0, [1])"), vec![vec![list(&[0, 1])]]);
    // The list comes first here and the value still goes in front, which is the pin's order and
    // not the one the name suggests.
    assert_eq!(one("SELECT array_push_front([1], 0)"), vec![vec![list(&[0, 1])]]);
    assert_eq!(one("SELECT list_append([], 1)"), vec![vec![list(&[1])]]);
    assert_eq!(
        one("SELECT typeof(list_append([1, 2], 3.5::DOUBLE))"),
        vec![vec![text("DOUBLE[]")]]
    );
    assert_eq!(
        one("SELECT list_append([[1]], [2])"),
        vec![vec![Value::List {
            element: LogicalType::list(LogicalType::Integer),
            values: vec![list(&[1]), list(&[2])],
        }]]
    );
    // A null list is skipped the way `list_concat` skips it, and a null value is an element.
    assert_eq!(one("SELECT list_append(NULL::INT[], 3)"), vec![vec![list(&[3])]]);
    assert_eq!(one("SELECT list_append(NULL, 3)"), vec![vec![list(&[3])]]);
    assert_eq!(one("SELECT list_prepend(1, NULL)"), vec![vec![list(&[1])]]);
    assert_eq!(
        one("SELECT list_append([1, 2], NULL)"),
        vec![vec![Value::List {
            element: LogicalType::Integer,
            values: vec![integer(1), integer(2), Value::Null],
        }]]
    );
    assert_eq!(
        failure(&db, "SELECT list_append([1], 'x'::VARCHAR)"),
        "Cannot concatenate lists of types INTEGER[] and VARCHAR[] - an explicit cast is required"
    );
    assert_eq!(
        failure(&db, "SELECT list_append([1])"),
        "Macro list_append() does not support the supplied arguments. You might need to add \
         explicit type casts.\nCandidate macros:\n\tlist_append(l, e)"
    );
    // A list column, so the expansion runs per row and not only in the folder.
    db.execute("CREATE TABLE ap (x INTEGER[], y INTEGER)").unwrap();
    db.execute("INSERT INTO ap VALUES ([1], 2), (NULL, 3)").unwrap();
    assert_eq!(
        rows(&db, "SELECT list_append(x, y) FROM ap"),
        vec![vec![list(&[1, 2])], vec![list(&[3])]]
    );
}

/// `list_transform` and `list_filter` run a lambda over every element. Per #467.
///
/// Every answer here is the pin's, headings included, since a heading is where the lambda is
/// written back out and the parameter keeps the case it was written in.
#[test]
fn a_lambda_runs_over_every_element() {
    let db = database();
    let one = |sql: &str| rows(&db, sql);
    let bigints = |values: &[i64]| Value::List {
        element: LogicalType::BigInt,
        values: values.iter().map(|&v| Value::BigInt(v)).collect(),
    };
    assert_eq!(
        one("SELECT list_transform([1, 2, 3], lambda x: x + 1)"),
        vec![vec![list(&[2, 3, 4])]]
    );
    assert_eq!(
        db.query("SELECT list_transform([1, 2, 3], lambda x: x + 1)").unwrap().names(),
        ["list_transform(list_value(1, 2, 3), (lambda x: (x + 1)))"]
    );
    assert_eq!(
        db.query("SELECT list_transform([1, 2], lambda X: x + 1)").unwrap().names(),
        ["list_transform(list_value(1, 2), (lambda X: (x + 1)))"]
    );
    // The second parameter is the position, counting from one, and it is a `BIGINT`.
    assert_eq!(
        one("SELECT list_transform([1, 2, 3], lambda x, i: x * i)"),
        vec![vec![bigints(&[1, 4, 9])]]
    );
    assert_eq!(one("SELECT list_filter([1, 2, 3, 4], lambda x: x % 2)"), vec![vec![list(&[1, 3])]]);
    assert_eq!(one("SELECT list_filter([1, 2, NULL], lambda x: NULL)"), vec![vec![list(&[])]]);
    assert_eq!(
        one("SELECT apply([1, 2], lambda x: x::VARCHAR)"),
        vec![vec![Value::List {
            element: LogicalType::Varchar,
            values: vec![text("1"), text("2")]
        }]]
    );
    assert_eq!(
        one("SELECT typeof(list_transform([1, 2], lambda x: x::DOUBLE))"),
        vec![vec![text("DOUBLE[]")]]
    );
    assert_eq!(one("SELECT list_transform(NULL, lambda x: x)"), vec![vec![Value::Null]]);
    assert_eq!(
        one("SELECT typeof(list_transform(NULL, lambda x: x))"),
        vec![vec![text("\"NULL\"")]]
    );
    assert_eq!(one("SELECT list_transform([], lambda x: x + 1)"), vec![vec![list(&[])]]);
    // A lambda inside a lambda, over the inner list and over the outer parameter.
    assert_eq!(
        one("SELECT list_transform([[1, 2], [3]], lambda x: list_transform(x, lambda y: y * 10))"),
        vec![vec![Value::List {
            element: LogicalType::list(LogicalType::Integer),
            values: vec![list(&[10, 20]), list(&[30])],
        }]]
    );
    assert_eq!(
        one("SELECT list_transform([1, 2], lambda x: list_transform([10, 20], lambda y: x + y))"),
        vec![vec![Value::List {
            element: LogicalType::list(LogicalType::Integer),
            values: vec![list(&[11, 21]), list(&[12, 22])],
        }]]
    );
    // A list longer than a chunk goes through the body in pieces, and the position carries on
    // counting across the break.
    let long: Vec<String> = (1..=3000).map(|n| n.to_string()).collect();
    assert_eq!(
        one(&format!("SELECT list_transform([{}], lambda x, i: i)[2999:3000]", long.join(", "))),
        vec![vec![bigints(&[2999, 3000])]]
    );
    assert_eq!(
        one(&format!("SELECT list_filter([{}], lambda x: x > 2998)", long.join(", "))),
        vec![vec![list(&[2999, 3000])]]
    );
}

/// A lambda's body sees the row it is in, and an aggregate in it is over the rows. Per #467.
#[test]
fn a_lambda_reads_the_columns_of_its_row() {
    let db = database();
    db.execute("CREATE TABLE lt (l INTEGER[], k INTEGER)").unwrap();
    db.execute("INSERT INTO lt VALUES ([1, 2], 10), (NULL, 20), ([], 30), ([3], 40)").unwrap();
    assert_eq!(
        rows(&db, "SELECT list_transform(l, lambda x: x + k) FROM lt"),
        vec![vec![list(&[11, 12])], vec![Value::Null], vec![list(&[])], vec![list(&[43])]]
    );
    assert_eq!(
        rows(&db, "SELECT list_filter(l, lambda x, i: i > 1) FROM lt"),
        vec![vec![list(&[2])], vec![Value::Null], vec![list(&[])], vec![list(&[])]]
    );
    assert_eq!(
        rows(&db, "SELECT list_transform([1, 2], lambda x: x + sum(k)) FROM lt"),
        vec![vec![Value::List {
            element: LogicalType::HugeInt,
            values: vec![Value::HugeInt(101), Value::HugeInt(102)],
        }]]
    );
}

/// What the pin refuses about a lambda, in its words. Per #467.
#[test]
fn a_lambda_is_refused_the_way_the_pin_refuses_it() {
    let db = database();
    assert_eq!(
        failure(&db, "SELECT list_transform([1, 0], lambda x: 10 // (x - 1))"),
        "Division by zero in expression (10 // (x - 1)). Use TRY(...) to return NULL for this \
         expression, or SET null_on_division_by_zero=true to return NULL for all divisions by zero."
    );
    assert_eq!(
        failure(&db, "SELECT list_transform([1], lambda x, y, z: x)"),
        "This lambda function only supports up to two lambda parameters!"
    );
    assert_eq!(
        failure(&db, "SELECT list_transform([1], lambda x, x: x)"),
        "table \"0_macro_parameters(x, x)\" has duplicate column name \"x\""
    );
    assert_eq!(
        failure(&db, "SELECT list_transform(1, lambda x: x)"),
        "Invalid LIST argument during lambda function binding!"
    );
    assert_eq!(
        failure(&db, "SELECT list_transform([1])"),
        "No function matches the given name and argument types 'list_transform(INTEGER[])'. You \
         might need to add explicit type casts.\n\tCandidate functions:\n\tlist_transform(col0 \
         ANY[], col1 LAMBDA) -> ANY[]\n"
    );
    assert_eq!(
        failure(&db, "SELECT list_transform([1], x -> x)"),
        "Deprecated lambda arrow (->) detected. Please transition to the new lambda syntax, i.e.., \
         lambda x, i: x + i, before DuckDB's next release.\nUse SET \
         lambda_syntax='ENABLE_SINGLE_ARROW' to revert to the deprecated behavior.\nFor more \
         information, see https://duckdb.org/docs/current/sql/functions/lambda.html."
    );
    assert_eq!(
        failure(&db, "SELECT abs(lambda x: x)"),
        "This scalar function does not support lambdas!"
    );
    assert_eq!(
        failure(&db, "SELECT list_transform([1], lambda x: (SELECT 1))"),
        "subqueries in lambda expressions are not supported"
    );
}

/// `list_reduce` folds a list left to right, and the accumulator's type is the pin's. Per #467.
///
/// The types are the part worth pinning down. The pin binds the body a second time with the
/// accumulator widened to what the first binding made, so a decimal sum gains a digit per binding,
/// and every type below is one it printed.
#[test]
fn a_reduction_folds_a_list_into_one_value() {
    let db = database();
    let one = |sql: &str| rows(&db, sql);
    assert_eq!(one("SELECT list_reduce([1, 2, 3], lambda x, y: x + y)"), vec![vec![integer(6)]]);
    assert_eq!(
        db.query("SELECT list_reduce([1, 2, 3], lambda x, y: x + y)").unwrap().names(),
        ["list_reduce(list_value(1, 2, 3), (lambda x, y: (x + y)))"]
    );
    assert_eq!(
        one("SELECT list_reduce([1, 2, 3], lambda x, y, i: x + y * i)"),
        vec![vec![Value::BigInt(14)]]
    );
    assert_eq!(
        one("SELECT list_reduce([1, 2, 3], lambda x, y: x + y, 100)"),
        vec![vec![integer(106)]]
    );
    assert_eq!(
        one("SELECT list_reduce([1, 2, 3], lambda x, y: x || y::VARCHAR, '')"),
        vec![vec![text("123")]]
    );
    assert_eq!(one("SELECT list_reduce(['a', 'b'], lambda x, y: x || y)"), vec![vec![text("ab")]]);
    assert_eq!(one("SELECT list_reduce([5], lambda x, y: x + y)"), vec![vec![integer(5)]]);
    assert_eq!(one("SELECT list_reduce([]::INT[], lambda x, y: x + y, 7)"), vec![vec![integer(7)]]);
    assert_eq!(
        one("SELECT list_reduce([1, NULL, 3], lambda x, y: x + y)"),
        vec![vec![Value::Null]]
    );
    assert_eq!(one("SELECT list_reduce(NULL, lambda x, y: x + y)"), vec![vec![Value::Null]]);
    assert_eq!(
        one("SELECT typeof(list_reduce(NULL, lambda x, y: x + y))"),
        vec![vec![text("\"NULL\"")]]
    );
    assert_eq!(
        one("SELECT list_reduce([1, 2], lambda x, y: x + y, NULL)"),
        vec![vec![Value::Null]]
    );
    assert_eq!(
        one("SELECT typeof(list_reduce([1, 2], lambda x, y: x + y, NULL))"),
        vec![vec![text("INTEGER")]]
    );
    // The position starts at 2 without an initial value, since the first element is the start.
    assert_eq!(
        one("SELECT list_reduce([1, 2, 3], lambda x, y, i: i)"),
        vec![vec![Value::BigInt(3)]]
    );
    assert_eq!(
        one("SELECT list_reduce([1, 2, 3], lambda x, y, i: i, 0)"),
        vec![vec![Value::BigInt(3)]]
    );
    // A comparison is carried as the element's type, because a boolean meets a number there.
    assert_eq!(one("SELECT list_reduce([1, 2, 3], lambda x, y: x > y)"), vec![vec![integer(0)]]);
    assert_eq!(
        one("SELECT typeof(list_reduce([1, 2], lambda x, y: x > y))"),
        vec![vec![text("INTEGER")]]
    );
    for (sql, ty, answer) in [
        ("list_reduce([1.5, 2, 3], lambda x, y: x + y)", "DECIMAL(13,1)", "6.5"),
        ("list_reduce([1, 2, 3], lambda x, y: x + y, 1.5)", "DECIMAL(12,1)", "7.5"),
        ("list_reduce([1000, 2000, 3000], lambda x, y: x + y, 1.5)", "DECIMAL(12,1)", "6001.5"),
        // The pin prints 225.00 here, one scale digit lost per step, which is tamnd/duckdb#13.
        ("list_reduce([1, 2, 3], lambda x, y: x * 1.5)", "DECIMAL(14,2)", "2.25"),
        ("list_reduce([1, 2, 3], lambda x, y: x + y + 0.5)", "DECIMAL(14,1)", "7.0"),
    ] {
        assert_eq!(one(&format!("SELECT typeof({sql})")), vec![vec![text(ty)]], "{sql}");
        assert_eq!(one(&format!("SELECT ({sql})::VARCHAR")), vec![vec![text(answer)]], "{sql}");
    }
    assert_eq!(
        one("SELECT list_reduce([[1], [2, 3]], lambda x, y: list_concat(x, y))"),
        vec![vec![list(&[1, 2, 3])]]
    );
    assert_eq!(
        one("SELECT array_reduce([1, 2], lambda x, y: x * y), reduce([1, 2], lambda x, y: x * y)"),
        vec![vec![integer(2), integer(2)]]
    );
    // Longer than a chunk, which a reduction runs a position at a time and not in batches.
    let long: Vec<String> = (1..=3000).map(|n| n.to_string()).collect();
    assert_eq!(
        one(&format!("SELECT list_reduce([{}], lambda x, y: x + y)", long.join(", "))),
        vec![vec![integer(4_501_500)]]
    );
}

/// A reduction per row, with lists of different lengths and an initial value from a column.
#[test]
fn a_reduction_runs_per_row() {
    let db = database();
    db.execute("CREATE TABLE lr (l INTEGER[], k INTEGER)").unwrap();
    db.execute("INSERT INTO lr VALUES ([1, 2], 10), (NULL, 20), ([], 30), ([3], 40)").unwrap();
    assert_eq!(
        rows(&db, "SELECT list_reduce(l, lambda x, y: x + y, k) FROM lr"),
        vec![vec![integer(13)], vec![Value::Null], vec![integer(30)], vec![integer(43)]]
    );
    assert_eq!(
        db.query("SELECT list_reduce(l, lambda x, y: x + y, k) FROM lr").unwrap().names(),
        ["list_reduce(l, (lambda x, y: (x + y)), k)"]
    );
    assert_eq!(
        rows(&db, "SELECT list_reduce(l, lambda x, y: x + y, NULL::INT) FROM lr"),
        vec![vec![Value::Null]; 4]
    );
    assert_eq!(
        rows(&db, "SELECT list_reduce(l, lambda x, y: x + y + k) FROM lr WHERE k <> 30"),
        vec![vec![integer(13)], vec![Value::Null], vec![integer(3)]]
    );
    // The empty list is refused when its row is reached, not when the query is bound.
    let error = db.query("SELECT list_reduce(l, lambda x, y: x + y + k) FROM lr").unwrap_err();
    assert_eq!(error.code(), rudb_common::ErrorCode::ParameterNotAllowed);
    assert_eq!(error.message(), "Cannot perform list_reduce on an empty input list");
}

/// What the pin refuses about a reduction, in its words. Per #467.
#[test]
fn a_reduction_is_refused_the_way_the_pin_refuses_it() {
    let db = database();
    assert_eq!(
        failure(&db, "SELECT list_reduce([], lambda x, y: x + y)"),
        "Cannot perform list_reduce on an empty input list"
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([1, 2], lambda x: x)"),
        "list_reduce expects a function with 2 or 3 arguments"
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([1, 2], lambda x, y, z, w: x)"),
        "This lambda function only supports up to three lambda parameters!"
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([1, 2])"),
        "No function matches the given name and argument types 'list_reduce(INTEGER[])'. You \
         might need to add explicit type casts.\n\tCandidate functions:\n\tlist_reduce(col0 \
         ANY[], col1 LAMBDA) -> ANY\n\tlist_reduce(col0 ANY[], col1 LAMBDA, col2 ANY) -> ANY\n"
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([1, 2], lambda x, y: [x, y])"),
        "No common super type between list element type INTEGER and lambda return type INTEGER[]"
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([1, 2], lambda x, y: [x], [0])"),
        "No common super type between initial value type INTEGER[] and lambda return type \
         INTEGER[][]"
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([1, 0], lambda x, y: x // y)"),
        "Division by zero in expression (x // y). Use TRY(...) to return NULL for this \
         expression, or SET null_on_division_by_zero=true to return NULL for all divisions by zero."
    );
    assert_eq!(
        failure(&db, "SELECT list_reduce([100, 100, 100]::TINYINT[], lambda x, y: x + y)"),
        "Overflow in addition of INT8 (100 + 100)!"
    );
}

/// `invoke` runs a lambda once per row over the arguments after it. Per #467.
#[test]
fn an_invoked_lambda_runs_over_its_arguments() {
    let db = database();
    let one = |sql: &str| rows(&db, sql);
    assert_eq!(one("SELECT invoke(lambda x: x * x, 2)"), vec![vec![integer(4)]]);
    assert_eq!(
        db.query("SELECT invoke(lambda x: x * x, 2)").unwrap().names(),
        ["invoke((lambda x: (x * x)), 2)"]
    );
    assert_eq!(one("SELECT invoke(lambda x, y: x * y + y, 3, 4)"), vec![vec![integer(16)]]);
    assert_eq!(
        one("SELECT invoke(lambda x, y, z, w, v: x + v, 1, 2, 3, 4, 5)"),
        vec![vec![integer(6)]]
    );
    assert_eq!(
        one("SELECT invoke(lambda a: invoke(lambda b, c: a + b + c, 4, 5), 3)"),
        vec![vec![integer(12)]]
    );
    assert_eq!(
        one("SELECT invoke(lambda x: invoke(lambda y: y + 5, x + 3), 2)"),
        vec![vec![integer(10)]]
    );
    assert_eq!(
        one("SELECT invoke(lambda name: invoke(lambda age: name || age, 30), 'Alice, ')"),
        vec![vec![text("Alice, 30")]]
    );
    assert_eq!(
        one("SELECT invoke(lambda x: list_transform([10, 20], lambda y, i: x + y + i), 100)"),
        vec![vec![Value::List {
            element: LogicalType::BigInt,
            values: vec![Value::BigInt(111), Value::BigInt(122)]
        }]]
    );
    assert_eq!(one("SELECT invoke(lambda x: x IS NULL, NULL)"), vec![vec![Value::Boolean(true)]]);
    assert_eq!(one("SELECT typeof(invoke(lambda x: x, NULL))"), vec![vec![text("\"NULL\"")]]);
    assert_eq!(
        one("SELECT typeof(invoke(lambda x: x, [1, 2]::INT[2]))"),
        vec![vec![text("INTEGER[2]")]]
    );
    db.execute("CREATE TABLE ti AS SELECT * FROM (VALUES (1, 10), (2, NULL), (NULL, 30)) t(a, b)")
        .unwrap();
    assert_eq!(
        one("SELECT invoke(lambda x, y: coalesce(x, 0) + coalesce(y, 0) + a, a, b) FROM ti"),
        vec![vec![integer(12)], vec![integer(4)], vec![Value::Null]]
    );
    assert_eq!(
        one(
            "SELECT invoke(lambda x: x + sum(a), 1)::INTEGER, typeof(invoke(lambda x: x + sum(a), 1)) FROM ti"
        ),
        vec![vec![integer(4), text("HUGEINT")]]
    );
    assert_eq!(
        one("SELECT i FROM range(0, 10) r(i) WHERE invoke(lambda x: x, i % 2 = 0) ORDER BY i"),
        [0, 2, 4, 6, 8].map(|i| vec![Value::BigInt(i)])
    );
    let long: Vec<Vec<Value>> = (0..3000).map(|i| vec![Value::BigInt(i * 2)]).collect();
    assert_eq!(one("SELECT invoke(lambda x: x * 2, i) FROM range(3000) r(i)"), long);
}

/// What the pin refuses about `invoke`, in its words. Per #467.
#[test]
fn an_invoked_lambda_is_refused_the_way_the_pin_refuses_it() {
    let db = database();
    let candidates = "You might need to add explicit type casts.\n\tCandidate functions:\n\tinvoke(col0 \
                      LAMBDA, col1 ANY, [ANY...]) -> ANY\n";
    assert_eq!(
        failure(&db, "SELECT invoke()"),
        format!("No function matches the given name and argument types 'invoke()'. {candidates}")
    );
    assert_eq!(
        failure(&db, "SELECT invoke(NULL)"),
        format!(
            "No function matches the given name and argument types 'invoke(\"NULL\")'. {candidates}"
        )
    );
    assert_eq!(
        failure(&db, "SELECT invoke(NULL, 1)"),
        "Invalid lambda expression passed to 'invoke' function."
    );
    assert_eq!(
        failure(&db, "SELECT invoke(1, lambda x: x)"),
        "This scalar function requires a lambda expression!"
    );
    assert_eq!(
        failure(&db, "SELECT invoke(lambda x: x + x, 2, 4, 6)"),
        "The number of lambda parameters does not match the number of arguments passed to the \
         'invoke' function, expected 1, got 3."
    );
    assert_eq!(
        failure(&db, "SELECT invoke(lambda x, y, z: x, 1)"),
        "The number of lambda parameters does not match the number of arguments passed to the \
         'invoke' function, expected at least 2, got 1."
    );
    assert_eq!(
        failure(&db, "SELECT invoke(lambda x: x + 1)"),
        "The number of lambda parameters does not match the number of arguments passed to the \
         'invoke' function, expected at least 1, got 0."
    );
    assert_eq!(
        failure(&db, "SELECT invoke(lambda x, x: x, 1, 2)"),
        "table \"0_macro_parameters(x, x)\" has duplicate column name \"x\""
    );
    // The pin ends this with " when casting from source column x", which is #1433 and not this.
    assert!(
        failure(&db, "SELECT invoke(lambda x: x::INTEGER, 'abc')")
            .starts_with("Could not convert string 'abc' to INT32")
    );
    assert_eq!(
        failure(&db, "SELECT invoke(lambda x: x * 2, 9223372036854775807::BIGINT)"),
        "Overflow in multiplication of INT64 (9223372036854775807 * 2)!"
    );
}

/// What the struct vector changes that a query can see today. Per #594.
///
/// One line, and that is the honest size of it. A struct vector exists now, so a query that has to put
/// a struct in one stops erroring, and the only such query a user can write is a cast of a null, because
/// the parser does not build a struct literal yet and there is no `struct_pack` to call instead. That is
/// the same split #302 left for lists and the same order: the vector first so the rest has somewhere to
/// compute into. Both answers here are the pin's.
#[test]
fn a_null_struct_is_a_value_now_rather_than_an_unwritten_vector() {
    let db = database();
    assert_eq!(rows(&db, "SELECT NULL::STRUCT(a INT)"), vec![vec![Value::Null]]);
    assert_eq!(
        rows(&db, "SELECT typeof(NULL::STRUCT(a INT, b VARCHAR))"),
        vec![vec![Value::Varchar("STRUCT(a INTEGER, b VARCHAR)".to_string())]]
    );
    // A struct column in a table is a column the catalog already held and the reader could not read.
    // It has no rows in it, because putting one in needs a literal the parser does not build.
    db.execute("CREATE TABLE structs (a STRUCT(x INTEGER, y VARCHAR))").unwrap();
    assert_eq!(rows(&db, "SELECT count(*) FROM structs"), vec![vec![Value::BigInt(0)]]);
    assert!(rows(&db, "SELECT a FROM structs").is_empty());
}

/// The same one line for the map vector. Per #595.
///
/// A map needs a `map()` function or a `MAP {}` literal before a query can build one, and it has
/// neither yet, so a cast of a null is again the whole visible surface. What it is really for is the
/// eight catalog columns in D2 that are typed `MAP(VARCHAR, VARCHAR)` and are the empty map in every
/// row, which could not be written at all while the form did not exist. Both answers here are the
/// pin's.
#[test]
fn a_null_map_is_a_value_now_rather_than_an_unwritten_vector() {
    let db = database();
    assert_eq!(rows(&db, "SELECT NULL::MAP(VARCHAR, VARCHAR)"), vec![vec![Value::Null]]);
    assert_eq!(
        rows(&db, "SELECT typeof(NULL::MAP(INT, INT))"),
        vec![vec![Value::Varchar("MAP(INTEGER, INTEGER)".to_string())]]
    );
    db.execute("CREATE TABLE maps (tags MAP(VARCHAR, VARCHAR))").unwrap();
    assert_eq!(rows(&db, "SELECT count(*) FROM maps"), vec![vec![Value::BigInt(0)]]);
    assert!(rows(&db, "SELECT tags FROM maps").is_empty());
}

/// `duckdb_types()` through the binder and the executor rather than through a hand written plan.
#[test]
fn the_types_table_answers_a_query_a_client_would_actually_write() {
    let db = database();
    assert_eq!(rows(&db, "SELECT count(*) FROM duckdb_types()"), vec![vec![Value::BigInt(93)]]);
    // A client reading this table is asking whether the engine has a type, so the useful query is a
    // name lookup, and it has to work through the where clause rather than only over the whole
    // table.
    assert_eq!(
        rows(&db, "SELECT logical_type, type_size FROM duckdb_types() WHERE type_name = 'hugeint'"),
        vec![vec![Value::Varchar("HUGEINT".to_string()), Value::BigInt(16)]]
    );
    // The name is case insensitive the way every function name is, and the table is in the default
    // catalog and schema because that is where the pin puts the builtin types.
    assert_eq!(
        rows(&db, "SELECT DISTINCT database_name, schema_name FROM DuckDB_Types()"),
        vec![vec![Value::Varchar("memory".to_string()), Value::Varchar("main".to_string())]]
    );
    // `tags` is why this table needed a map vector, so it is read back here as one rather than only
    // counted.
    assert_eq!(
        rows(&db, "SELECT tags FROM duckdb_types() WHERE type_name = 'boolean'"),
        vec![vec![Value::map(LogicalType::Varchar, LogicalType::Varchar, Vec::new())]]
    );
}

/// `duckdb_functions()` through the binder and the executor, per #465.
#[test]
fn the_functions_table_answers_the_question_a_client_asks_it() {
    let db = database();
    // The query a client actually writes, which is whether a name is there at all.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM duckdb_functions() WHERE function_name = 'sqrt'"),
        vec![vec![Value::BigInt(0)]]
    );
    assert_eq!(
        rows(&db, "SELECT function_type FROM duckdb_functions() WHERE function_name = 'avg'"),
        vec![vec![Value::Varchar("aggregate".to_string())]]
    );
    // Builtins are reported in `system.main` and not in `memory`, which is where the pin puts them
    // and where every client query looks. rudb has no catalog named `system` yet, so this is the one
    // column in the table that names something the catalog does not have.
    assert_eq!(
        rows(&db, "SELECT DISTINCT database_name, schema_name FROM duckdb_functions()"),
        vec![vec![Value::Varchar("system".to_string()), Value::Varchar("main".to_string())]]
    );
    // Every row is a builtin, which is what makes the corpus query for user defined functions come
    // back empty rather than wrong.
    assert!(
        rows(&db, "SELECT function_name FROM duckdb_functions() WHERE NOT internal").is_empty()
    );
    // This table lists itself, because it is a table function and the table lists those.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM duckdb_functions() WHERE function_name LIKE 'duckdb_%'"),
        vec![vec![Value::BigInt(13)]]
    );
}

/// The fourteen names that answer about the connection rather than about the query.
#[test]
fn the_session_context_answers_for_the_clock_the_catalog_and_the_user() {
    let db = database();
    db.execute("SET TimeZone = 'UTC'").expect("a stable zone for written timestamp answers");
    let text = |value: &str| Value::Varchar(value.to_string());
    // The types first, because the type is the half a client reads through a driver and the half
    // that is easy to get wrong. Every one of these was measured against the pin.
    assert_eq!(
        rows(
            &db,
            "SELECT typeof(now()), typeof(current_timestamp), typeof(get_current_timestamp()), \
             typeof(transaction_timestamp()), typeof(current_localtimestamp()), \
             typeof(localtimestamp)"
        ),
        vec![vec![
            text("TIMESTAMP WITH TIME ZONE"),
            text("TIMESTAMP WITH TIME ZONE"),
            text("TIMESTAMP WITH TIME ZONE"),
            text("TIMESTAMP WITH TIME ZONE"),
            text("TIMESTAMP"),
            text("TIMESTAMP"),
        ]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT typeof(current_date), typeof(today()), typeof(current_time), \
             typeof(get_current_time()), typeof(localtime), typeof(current_localtime())"
        ),
        vec![vec![
            text("DATE"),
            text("DATE"),
            text("TIME WITH TIME ZONE"),
            text("TIME WITH TIME ZONE"),
            text("TIME"),
            text("TIME"),
        ]]
    );
    // The four that name something the catalog knows, plus the user, which rudb answers the pin's
    // way because neither engine has users and a client asking wants a name.
    assert_eq!(
        rows(
            &db,
            "SELECT current_schema, current_schema(), current_catalog, current_database(), \
             current_user, session_user, user"
        ),
        vec![vec![
            text("main"),
            text("main"),
            text("memory"),
            text("memory"),
            text("duckdb"),
            text("duckdb"),
            text("duckdb"),
        ]]
    );
    // One instant per statement, whatever spells it and however many rows read it. That is what the
    // pin reports as CONSISTENT_WITHIN_QUERY and what it answers for the same query.
    assert_eq!(
        rows(
            &db,
            "SELECT now() = current_timestamp, now() = transaction_timestamp(), \
             now() = get_current_timestamp(), today() = current_date"
        ),
        vec![vec![
            Value::Boolean(true),
            Value::Boolean(true),
            Value::Boolean(true),
            Value::Boolean(true),
        ]]
    );
    assert_eq!(
        rows(&db, "SELECT count(DISTINCT n) FROM (SELECT now() AS n FROM range(3))"),
        vec![vec![Value::BigInt(1)]]
    );
    // The clock reads forward, which is the only thing about its value a test can hold it to.
    assert_eq!(
        rows(&db, "SELECT now() > TIMESTAMP '2024-01-01', current_date > DATE '2024-01-01'"),
        vec![vec![Value::Boolean(true), Value::Boolean(true)]]
    );
    // A column of the same name wins over the keyword, which was measured on the pin for all ten of
    // the bare spellings and is why the scope is asked before the fold.
    db.execute(
        "CREATE TABLE context(current_date VARCHAR, current_user VARCHAR, \"user\" VARCHAR)",
    )
    .expect("three columns named after keywords");
    db.execute("INSERT INTO context VALUES ('a', 'b', 'c')").expect("one row");
    assert_eq!(
        rows(&db, "SELECT current_date, current_user, user FROM context"),
        vec![vec![text("a"), text("b"), text("c")]]
    );
    // And a name two tables both carry is still the ambiguity error rather than the constant.
    assert_eq!(
        failure(&db, "SELECT current_date FROM context, context AS again"),
        "Ambiguous reference to column name \"current_date\" (use: 'context.current_date' or \
         'again.current_date')"
    );
    // The five names that take one spelling and not the other. `current_database` is a function and
    // not a keyword on the pin, and `current_timestamp` is a keyword and not a function.
    assert_eq!(
        failure(&db, "SELECT current_database"),
        "Referenced column \"current_database\" not found in FROM clause!"
    );
    assert_eq!(
        failure(&db, "SELECT current_timestamp()"),
        "Scalar Function with name current_timestamp does not exist!"
    );
    // A call with arguments is not one of these and reaches the signature table, which has a row per
    // name so that the message is the arity error the pin gives rather than a missing function.
    assert!(
        failure(&db, "SELECT now(1)")
            .starts_with("No function matches the given name and argument types 'now(INTEGER)'"),
        "an arity error rather than a missing function"
    );
    // A zoned value keeps its zone through arithmetic, which is the half of this that is not about
    // the clock at all: `now()` is the first way a query gets one of these, so every operator that
    // takes a timestamp had to learn the zoned kind. All of these were measured against the pin.
    assert_eq!(
        rows(
            &db,
            "SELECT typeof(now() + INTERVAL 1 DAY), typeof(INTERVAL 1 DAY + now()), \
             typeof(now() - now()), typeof(now() - TIMESTAMP '2020-01-01'), \
             typeof(now() - DATE '2020-01-01'), typeof(now() + NULL), \
             typeof(current_time + INTERVAL 1 HOUR), typeof(current_date + current_time)"
        ),
        vec![vec![
            text("TIMESTAMP WITH TIME ZONE"),
            text("TIMESTAMP WITH TIME ZONE"),
            text("INTERVAL"),
            text("INTERVAL"),
            text("INTERVAL"),
            text("TIMESTAMP WITH TIME ZONE"),
            text("TIME WITH TIME ZONE"),
            text("TIMESTAMP WITH TIME ZONE"),
        ]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT typeof(date_part('year', now())), typeof(date_trunc('day', now())), \
             typeof(age(now(), now())), \
             CAST(date_trunc('day', TIMESTAMPTZ '2020-01-02 03:04:05') AS VARCHAR), \
             CAST(TIMESTAMPTZ '2020-01-31 10:00:00' + INTERVAL 1 MONTH AS VARCHAR), \
             CAST(DATE '2020-01-02' + TIMETZ '03:04:05' AS VARCHAR)"
        ),
        vec![vec![
            text("BIGINT"),
            text("TIMESTAMP WITH TIME ZONE"),
            text("INTERVAL"),
            text("2020-01-02 00:00:00+00"),
            text("2020-02-29 10:00:00+00"),
            text("2020-01-02 03:04:05+00"),
        ]]
    );
    // All fourteen are in the function table, which is where a client looks to find out. The count
    // is of the distinct names rather than of the rows, because the pin has two rows for
    // `current_schema` and two for `current_database`, a scalar and a macro with the same name, and
    // the thing worth holding still is that the name is there rather than how many ways it is there.
    assert_eq!(
        rows(
            &db,
            "SELECT count(DISTINCT function_name) FROM duckdb_functions() WHERE function_name IN \
             ('now', 'today', \
             'get_current_timestamp', 'get_current_time', 'transaction_timestamp', \
             'current_localtime', 'current_localtimestamp', 'current_date', 'current_schema', \
             'current_database', 'current_catalog', 'current_user', 'session_user', 'user')"
        ),
        vec![vec![Value::BigInt(14)]]
    );
}

#[test]
fn the_session_time_zone_moves_local_context_and_one_argument_age() {
    let db = database();
    let default_zone = db.setting("TimeZone").expect("the operating-system zone");
    db.execute("SET TimeZone = 'America/New_York'").expect("an IANA zone");
    assert_eq!(
        rows(&db, "SELECT current_setting('timezone')"),
        vec![vec![Value::Varchar("America/New_York".to_string())]]
    );
    let answer = rows(&db, "SELECT now(), localtimestamp, current_time, localtime");
    let [
        Value::TimestampTz(utc),
        Value::Timestamp(local),
        Value::TimeTz(zoned_time),
        Value::Time(local_time),
    ] = &answer[0][..]
    else {
        panic!("the four context values had the wrong types: {:?}", answer[0]);
    };
    assert_eq!(local - utc, -4 * 60 * 60 * 1_000_000, "New York is EDT in September");
    assert_eq!(zoned_time, local_time);
    assert_eq!(
        rows(
            &db,
            "SELECT CAST(stamp AS VARCHAR) FROM (VALUES \
             (TIMESTAMPTZ '2020-01-01 12:00:00+00'), \
             (TIMESTAMPTZ '2020-07-01 12:00:00+00')) AS t(stamp)"
        ),
        vec![
            vec![Value::Varchar("2020-01-01 07:00:00-05".to_string())],
            vec![Value::Varchar("2020-07-01 08:00:00-04".to_string())],
        ]
    );
    assert_eq!(
        rows(&db, "VALUES (CAST(TIMESTAMPTZ '2020-07-01 12:00:00+00' AS VARCHAR))"),
        vec![vec![Value::Varchar("2020-07-01 08:00:00-04".to_string())]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT age(DATE '2020-02-28') = age(current_date, DATE '2020-02-28'), \
             age(TIMESTAMP '2020-02-28 12:00:00') = \
             age(current_date, TIMESTAMP '2020-02-28 12:00:00')"
        ),
        vec![vec![Value::Boolean(true), Value::Boolean(true)]]
    );
    db.execute("SET TIME ZONE 'Asia/Kathmandu'").expect("the standard spelling");
    assert_eq!(db.setting("TimeZone").expect("the zone"), "Asia/Kathmandu");
    db.execute("RESET TimeZone").expect("the default zone");
    assert_eq!(db.setting("timezone").expect("case is ignored"), default_zone);
    db.execute("SET TimeZone = 'UTC'").expect("a different zone");
    db.execute("SET TIME ZONE LOCAL").expect("the local zone");
    assert_eq!(db.setting("TimeZone").expect("the local zone"), default_zone);
    let error = db.execute("SET TimeZone = 'not/a_zone'").expect_err("an unknown zone");
    assert_eq!(error.code().duckdb_name(), "Not implemented Error");
    assert!(error.message().starts_with("Unknown TimeZone 'not/a_zone'!"), "{error}");
}

#[test]
fn the_session_sort_defaults_are_resolved_into_each_sort_key() {
    let db = database();
    db.execute("CREATE TABLE sort_defaults(x INTEGER)").expect("a nullable column");
    db.execute("INSERT INTO sort_defaults VALUES (1), (NULL), (2)").expect("three rows");
    let one = |value| vec![Value::Integer(value)];
    let null = vec![Value::Null];
    assert_eq!(
        rows(&db, "SELECT x FROM sort_defaults ORDER BY x DESC"),
        vec![one(2), one(1), null.clone()]
    );
    db.execute("SET default_order = 'descending'").expect("the descending default");
    assert_eq!(db.setting("default_order").expect("the direction"), "DESC");
    assert_eq!(
        rows(&db, "SELECT x FROM sort_defaults ORDER BY x"),
        vec![one(2), one(1), null.clone()]
    );
    db.execute("SET default_null_order = 'first'").expect("nulls first");
    assert_eq!(
        rows(&db, "SELECT x FROM sort_defaults ORDER BY x"),
        vec![null.clone(), one(2), one(1)]
    );
    db.execute("SET default_null_order = 'sqlite'").expect("the SQLite convention");
    assert_eq!(
        rows(&db, "SELECT x FROM sort_defaults ORDER BY x ASC"),
        vec![null.clone(), one(1), one(2)]
    );
    assert_eq!(
        rows(&db, "SELECT x FROM sort_defaults ORDER BY x DESC"),
        vec![one(2), one(1), null.clone()]
    );
    db.execute("SET default_null_order = 'postgres'").expect("the PostgreSQL convention");
    assert_eq!(
        rows(&db, "SELECT x FROM sort_defaults ORDER BY x DESC"),
        vec![null, one(2), one(1)]
    );
    db.execute("RESET default_order").expect("the direction default");
    db.execute("RESET default_null_order").expect("the null default");
    assert_eq!(db.setting("default_order").expect("the direction"), "ASCENDING");
    assert_eq!(db.setting("default_null_order").expect("the null order"), "NULLS_LAST");
}

#[test]
fn integer_division_is_resolved_while_the_expression_is_bound() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT 7 / 2, typeof(7 / 2)"),
        vec![vec![Value::Double(3.5), text("DOUBLE")]]
    );
    db.execute("SET integer_division = true").expect("integer division");
    assert_eq!(db.setting("integer_division").expect("the setting"), "true");
    assert_eq!(
        rows(&db, "SELECT 7 / 2, typeof(7 / 2), 7.5 / 2.0, typeof(7.5 / 2.0)"),
        vec![vec![Value::Integer(3), text("INTEGER"), Value::Double(3.75), text("DOUBLE")]]
    );
    assert_eq!(db.query("SELECT 7 / 2").expect("a division").names(), ["(7 // 2)".to_string()]);
    db.execute("RESET integer_division").expect("floating point division");
    assert_eq!(db.setting("integer_division").expect("the setting"), "false");
    assert_eq!(rows(&db, "SELECT 7 / 2"), vec![vec![Value::Double(3.5)]]);
    let error = db.execute("SET integer_division = 'off'").expect_err("not a boolean");
    assert_eq!(error.message(), "Failed to cast value: Could not convert string 'off' to BOOL");
}

#[test]
fn zero_division_nulls_are_resolved_while_the_expression_is_bound() {
    let db = database();
    assert_eq!(db.setting("null_on_division_by_zero").expect("the setting"), "false");
    assert!(db.query("SELECT 1 // 0").is_err());
    db.execute("SET null_on_division_by_zero = true").expect("nulling division errors");
    assert_eq!(db.setting("null_on_division_by_zero").expect("the setting"), "true");
    assert_eq!(
        rows(
            &db,
            "SELECT current_setting('null_on_division_by_zero'), typeof(current_setting('null_on_division_by_zero'))"
        ),
        vec![vec![Value::Boolean(true), text("BOOLEAN")]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT 1 / 0, 1 // 0, 1 % 0, 1.0 // 0.0, 1.0 % 0.0, CAST(1 AS FLOAT) / CAST(0 AS FLOAT)"
        ),
        vec![vec![
            Value::Double(f64::INFINITY),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Float(f32::INFINITY),
        ]]
    );
    assert_eq!(
        rows(&db, "SELECT a, 10 // a, 10 % a FROM range(-1, 2) t(a)"),
        vec![
            vec![Value::BigInt(-1), Value::BigInt(-10), Value::BigInt(0)],
            vec![Value::BigInt(0), Value::Null, Value::Null],
            vec![Value::BigInt(1), Value::BigInt(10), Value::BigInt(0)],
        ]
    );
    db.execute("RESET null_on_division_by_zero").expect("division errors");
    assert_eq!(db.setting("null_on_division_by_zero").expect("the setting"), "false");
    assert!(db.query("SELECT 1 // 0").is_err());
    let error = db.execute("SET null_on_division_by_zero = 'off'").expect_err("not a boolean");
    assert_eq!(error.message(), "Failed to cast value: Could not convert string 'off' to BOOL");
}

#[test]
fn ieee_floating_point_ops_are_resolved_while_the_expression_is_bound() {
    let db = database();
    assert_eq!(db.setting("ieee_floating_point_ops").expect("the setting"), "true");
    assert!(
        matches!(rows(&db, "SELECT 0.0::DOUBLE / 0.0::DOUBLE")[0][0], Value::Double(answer) if answer.is_nan())
    );
    assert!(
        matches!(rows(&db, "SELECT 1.0::DOUBLE % 0.0::DOUBLE")[0][0], Value::Double(answer) if answer.is_nan())
    );
    db.execute("SET ieee_floating_point_ops = false").expect("checked floating operations");
    assert_eq!(db.setting("ieee_floating_point_ops").expect("the setting"), "false");
    assert_eq!(
        rows(
            &db,
            "SELECT current_setting('ieee_floating_point_ops'), typeof(current_setting('ieee_floating_point_ops'))"
        ),
        vec![vec![Value::Boolean(false), text("BOOLEAN")]]
    );
    let advice = "Use TRY(...) to return NULL for this expression, or SET null_on_division_by_zero=true to return NULL for all divisions by zero.";
    assert_eq!(
        failure(&db, "SELECT 1.0::DOUBLE / 0.0::DOUBLE"),
        format!("Division by zero in expression (1.0 / 0.0). {advice}")
    );
    assert_eq!(
        failure(&db, "SELECT 1.0::DOUBLE % 0.0::DOUBLE"),
        format!("Division by zero in expression (1.0 % 0.0). {advice}")
    );
    db.create_table("ieee_values", vec![Field::new("a", LogicalType::Double)]).unwrap();
    db.append(
        "ieee_values",
        &[vec![Value::Double(-1.0)], vec![Value::Double(0.0)], vec![Value::Double(1.0)]],
    )
    .unwrap();
    assert_eq!(
        failure(&db, "SELECT 1.0 / a FROM ieee_values"),
        format!("Division by zero in expression (1.0 / a). {advice}")
    );
    assert_eq!(
        failure(&db, "SELECT 1.0 % a FROM ieee_values"),
        format!("Division by zero in expression (1.0 % a). {advice}")
    );
    db.execute("SET null_on_division_by_zero = true").expect("nulling division errors");
    assert_eq!(
        rows(&db, "SELECT 1.0::DOUBLE / 0.0::DOUBLE, 1.0::DOUBLE % 0.0::DOUBLE"),
        vec![vec![Value::Null, Value::Null]]
    );
    db.execute("RESET null_on_division_by_zero").expect("division errors");
    db.execute("RESET ieee_floating_point_ops").expect("IEEE floating operations");
    assert_eq!(db.setting("ieee_floating_point_ops").expect("the setting"), "true");
    let error = db.execute("SET ieee_floating_point_ops = 'off'").expect_err("not a boolean");
    assert_eq!(error.message(), "Failed to cast value: Could not convert string 'off' to BOOL");
}

#[test]
fn timestamp_to_timestamptz_casts_can_be_disabled_while_binding() {
    let db = database();
    db.execute("SET TimeZone = 'UTC'").expect("a deterministic zone");
    assert_eq!(db.setting("disable_timestamptz_casts").expect("the setting"), "false");
    assert_eq!(
        rows(&db, "SELECT TIMESTAMP '2024-01-02 03:04:05'::TIMESTAMPTZ"),
        vec![vec![Value::TimestampTz(1_704_164_645_000_000)]]
    );
    db.execute("SET disable_timestamptz_casts = true").expect("disabled timestamp casts");
    assert_eq!(
        rows(
            &db,
            "SELECT current_setting('disable_timestamptz_casts'), typeof(current_setting('disable_timestamptz_casts'))"
        ),
        vec![vec![Value::Boolean(true), text("BOOLEAN")]]
    );
    let expected = "Casting from TIMESTAMP to TIMESTAMP WITH TIME ZONE without an explicit time zone has been disabled  - use \"AT TIME ZONE ...\"";
    for query in [
        "SELECT TIMESTAMP '2024-01-02 03:04:05'::TIMESTAMPTZ",
        "SELECT TRY_CAST(TIMESTAMP '2024-01-02 03:04:05' AS TIMESTAMPTZ)",
        "SELECT TIMESTAMP '2024-01-02 03:04:05' = TIMESTAMPTZ '2024-01-02 03:04:05+00'",
        "SELECT CASE WHEN true THEN TIMESTAMP '2024-01-02 03:04:05' ELSE TIMESTAMPTZ '2024-01-02 03:04:05+00' END",
        "SELECT DATE '2024-01-02'::TIMESTAMPTZ",
        "VALUES (TIMESTAMP '2024-01-02 03:04:05'), (TIMESTAMPTZ '2024-01-02 03:04:05+00')",
        "SELECT TIMESTAMP '2024-01-02 03:04:05' UNION ALL SELECT TIMESTAMPTZ '2024-01-02 03:04:05+00'",
    ] {
        assert_eq!(failure(&db, query), expected, "{query}");
    }
    db.create_table("zoned", vec![Field::new("z", LogicalType::TimestampTz)]).unwrap();
    assert_eq!(failure(&db, "INSERT INTO zoned SELECT TIMESTAMP '2024-01-02 03:04:05'"), expected);
    assert_eq!(
        rows(&db, "SELECT '2024-01-02 03:04:05'::TIMESTAMPTZ"),
        vec![vec![Value::TimestampTz(1_704_164_645_000_000)]]
    );
    db.execute("RESET disable_timestamptz_casts").expect("timestamp casts restored");
    assert_eq!(db.setting("disable_timestamptz_casts").expect("the setting"), "false");
    let error = db.execute("SET disable_timestamptz_casts = 'off'").expect_err("not a boolean");
    assert_eq!(error.message(), "Failed to cast value: Could not convert string 'off' to BOOL");
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'disable_timestamptz_casts'"
        ),
        vec![vec![
            text("Disable casting from timestamp to timestamptz "),
            text("BOOLEAN"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn a_non_integer_order_literal_needs_the_session_opt_in() {
    let db = database();
    let expected = "ORDER BY non-integer literal has no effect.\n* SET order_by_non_integer_literal=true to allow this behavior.";
    assert_eq!(failure(&db, "SELECT 2 AS x ORDER BY 'a'"), expected);
    assert_eq!(failure(&db, "SELECT 2 AS x ORDER BY 1.5"), expected);
    assert_eq!(failure(&db, "SELECT 2 AS x ORDER BY NULL"), expected);
    db.execute("SET order_by_non_integer_literal = true").expect("constant sort keys");
    assert_eq!(db.setting("order_by_non_integer_literal").expect("the setting"), "true");
    assert_eq!(rows(&db, "SELECT 2 AS x ORDER BY 'a'"), vec![vec![Value::Integer(2)]]);
    db.execute("RESET order_by_non_integer_literal").expect("the default guard");
    assert_eq!(db.setting("order_by_non_integer_literal").expect("the setting"), "false");
}

#[test]
fn regex_operator_semantics_are_resolved_while_the_expression_is_bound() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT 'abc' ~ 'b', 'abc' !~ 'b', 'ABC' ~* 'b', 'ABC' !~* 'b'"),
        vec![vec![
            Value::Boolean(true),
            Value::Boolean(false),
            Value::Boolean(true),
            Value::Boolean(false),
        ]]
    );
    db.execute("SET regex_match_operator_semantics = 'full'").expect("full matching");
    assert_eq!(
        rows(&db, "SELECT 'abc' ~ 'b', 'abc' !~ 'b', 'ABC' ~* 'b', 'ABC' !~* 'b'"),
        vec![vec![
            Value::Boolean(false),
            Value::Boolean(true),
            Value::Boolean(false),
            Value::Boolean(true),
        ]]
    );
    assert_eq!(
        db.query("SELECT 'abc' ~ 'b'").expect("a regex match").names(),
        ["regexp_full_match('abc', 'b')".to_string()]
    );
    assert_eq!(rows(&db, "SELECT 'abc' SIMILAR TO 'b'"), vec![vec![Value::Boolean(false)]]);
    db.execute("RESET regex_match_operator_semantics").expect("partial matching");
    assert_eq!(db.setting("regex_match_operator_semantics").expect("the setting"), "partial");
    let error =
        db.execute("SET regex_match_operator_semantics = 'nope'").expect_err("an unknown mode");
    assert_eq!(error.code().duckdb_name(), "Not implemented Error");
    assert_eq!(
        error.message(),
        "Enum value: unrecognized value \"nope\" for enum \"RegexMatchOperatorSemantics\"\n\nCandidates: \"FULL\""
    );
}

#[test]
fn show_behavior_resolves_a_name_before_execution() {
    let db = database();
    assert_eq!(rows(&db, "SHOW show_behavior"), vec![vec![text("AUTO")]]);
    let mixed = db.query("SHOW ShOw_BeHaViOr").expect("setting names ignore case");
    assert_eq!(mixed.names(), ["ShOw_BeHaViOr"]);
    assert_eq!(mixed.rows().collect::<Vec<_>>(), vec![vec![text("AUTO")]]);
    assert_eq!(
        rows(&db, "SHOW t"),
        vec![
            vec![text("x"), text("INTEGER"), text("YES"), Value::Null, Value::Null, Value::Null],
            vec![text("s"), text("VARCHAR"), text("YES"), Value::Null, Value::Null, Value::Null],
        ]
    );
    db.execute("SET show_behavior = 'setting'").expect("settings only");
    assert_eq!(rows(&db, "SHOW show_behavior"), vec![vec![text("setting")]]);
    assert_eq!(failure(&db, "SHOW t"), "Setting with name \"t\" does not exist");
    db.execute("SET show_behavior = 'table'").expect("tables only");
    assert_eq!(failure(&db, "SHOW show_behavior"), "Table with name show_behavior does not exist!");
    db.execute("RESET show_behavior").expect("automatic resolution");
    assert_eq!(db.setting("show_behavior").expect("the setting"), "AUTO");
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'show_behavior'"
        ),
        vec![vec![
            text(
                "How SHOW resolves a bare identifier: 'auto' (describe a table if one exists, else a setting; deprecated), 'table' (always a table), or 'setting' (always a setting)"
            ),
            text("VARCHAR"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn current_dialect_resolves_through_the_installed_parser_registry() {
    let db = database();
    assert_eq!(rows(&db, "SELECT current_setting('current_dialect')"), vec![vec![text("duckdb")]]);
    db.execute("SET current_dialect = 'DUCKDB'").expect("the installed dialect");
    assert_eq!(db.setting("current_dialect").expect("the dialect"), "DUCKDB");
    assert_eq!(rows(&db, "SELECT 1"), vec![vec![Value::Integer(1)]]);
    let error = db.execute("SET current_dialect = 'cypher'").expect_err("not installed");
    assert_eq!(error.code().duckdb_name(), "Invalid Input Error");
    assert_eq!(error.message(), "Dialect \"cypher\" is not installed");
    db.execute("RESET current_dialect").expect("the default dialect");
    assert_eq!(db.setting("current_dialect").expect("the dialect"), "duckdb");
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'current_dialect'"
        ),
        vec![vec![text("The SQL dialect used by the parser"), text("VARCHAR"), text("GLOBAL")]]
    );
}

#[test]
fn dialect_compatibility_mode_accepts_exactly_the_modes_the_pin_has() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT current_setting('dialect_compatibility_mode')"),
        vec![vec![text("NONE")]]
    );
    db.execute("SET dialect_compatibility_mode = 'spark'").expect("Spark mode");
    assert_eq!(db.setting("dialect_compatibility_mode").expect("the mode"), "spark");
    db.execute("SET GLOBAL dialect_compatibility_mode = 'SPARK'").expect("global Spark mode");
    assert_eq!(db.setting("dialect_compatibility_mode").expect("the mode"), "SPARK");
    db.execute("SET dialect_compatibility_mode = 'none'").expect("no compatibility mode");
    assert_eq!(db.setting("dialect_compatibility_mode").expect("the mode"), "none");
    db.execute("RESET GLOBAL dialect_compatibility_mode").expect("the default mode");
    assert_eq!(db.setting("dialect_compatibility_mode").expect("the mode"), "NONE");
    let error = db.execute("SET dialect_compatibility_mode = 'nope'").expect_err("an unknown mode");
    assert_eq!(error.code().duckdb_name(), "Not implemented Error");
    assert_eq!(
        error.message(),
        "Enum value: unrecognized value \"nope\" for enum \"DialectCompatibilityMode\"\n\nCandidates: \"NONE\""
    );
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'dialect_compatibility_mode'"
        ),
        vec![vec![
            text(
                "Enable SQL dialect compatibility for a certain engine (e.g. `SET dialect_compatibility_mode='spark'`)"
            ),
            text("VARCHAR"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn preserve_identifier_case_folds_only_unquoted_identifiers() {
    let db = database();
    assert_eq!(db.setting("preserve_identifier_case").expect("the mode"), "preserve_case");
    db.execute("CREATE TABLE MixedTable (MixedColumn INTEGER)").expect("preserved names");
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, column_name FROM duckdb_columns() WHERE table_name = 'MixedTable'"
        ),
        vec![vec![text("MixedTable"), text("MixedColumn")]]
    );
    db.execute("SET preserve_identifier_case = 'lowercase'").expect("lowercase names");
    db.execute("CREATE TABLE LowerTable (LowerColumn INTEGER)").expect("lowercase table");
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, column_name FROM duckdb_columns() WHERE table_name = 'lowertable'"
        ),
        vec![vec![text("lowertable"), text("lowercolumn")]]
    );
    db.execute("SET preserve_identifier_case = 'uppercase'").expect("uppercase names");
    db.execute("CREATE TABLE UpperTable (UpperColumn INTEGER)").expect("uppercase table");
    db.execute("CREATE TABLE \"QuotedTable\" (\"QuotedColumn\" INTEGER)").expect("quoted names");
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, column_name FROM duckdb_columns() WHERE table_name IN ('UPPERTABLE', 'QuotedTable') ORDER BY table_name"
        ),
        vec![
            vec![text("QuotedTable"), text("QuotedColumn")],
            vec![text("UPPERTABLE"), text("UPPERCOLUMN")],
        ]
    );
    assert_eq!(rows(&db, "SELECT 'MixedString'"), vec![vec![text("MixedString")]]);
    db.execute("RESET preserve_identifier_case").expect("the default mode");
    assert_eq!(db.setting("preserve_identifier_case").expect("the mode"), "preserve_case");
}

#[test]
fn preserve_identifier_case_keeps_legacy_boolean_aliases_and_metadata() {
    let db = database();
    for truthy in ["true", "1", "'t'", "'y'", "'yes'"] {
        db.execute(&format!("SET preserve_identifier_case = {truthy}")).expect("truthy alias");
        assert_eq!(db.setting("preserve_identifier_case").expect("the mode"), "preserve_case");
    }
    for falsy in ["false", "0", "'f'", "'n'", "'no'"] {
        db.execute(&format!("SET preserve_identifier_case = {falsy}")).expect("falsy alias");
        assert_eq!(db.setting("preserve_identifier_case").expect("the mode"), "lowercase");
    }
    let invalid = db.execute("SET preserve_identifier_case = 'bogus'").expect_err("invalid mode");
    assert_eq!(invalid.code().duckdb_name(), "Invalid Input Error");
    assert_eq!(
        invalid.message(),
        "Unrecognized parameter for option preserve_identifier_case \"bogus\", expected one of: preserve_case, lowercase, uppercase"
    );
    let null = db.execute("SET preserve_identifier_case = NULL").expect_err("null mode");
    assert_eq!(null.message(), "preserve_identifier_case setting cannot be NULL");
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'preserve_identifier_case'"
        ),
        vec![vec![
            text(
                "How to fold non-quoted identifiers: 'preserve_case' keeps the case as written, 'lowercase' lowercases them, 'uppercase' uppercases them"
            ),
            text("VARCHAR"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn allow_parser_override_extension_matches_the_only_installed_mode() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT current_setting('allow_parser_override_extension')"),
        vec![vec![text("DEFAULT")]]
    );
    db.execute("SET allow_parser_override_extension = DEFAULT").expect("the default mode");
    db.execute("SET allow_parser_override_extension = 'default'").expect("case insensitive mode");
    let invalid = db
        .execute("SET allow_parser_override_extension = true")
        .expect_err("the pin has no enabled mode");
    assert_eq!(invalid.code().duckdb_name(), "Not implemented Error");
    assert_eq!(
        invalid.message(),
        "Enum value: unrecognized value \"true\" for enum \"AllowParserOverride\"\n\nCandidates: \"DEFAULT\""
    );
    db.execute("RESET allow_parser_override_extension").expect("reset the mode");
    assert_eq!(db.setting("allow_parser_override_extension").expect("the mode"), "DEFAULT");
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'allow_parser_override_extension'"
        ),
        vec![vec![
            text("Allow extensions to override the current parser"),
            text("VARCHAR"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn warnings_as_errors_matches_the_pin_without_a_logger() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT current_setting('warnings_as_errors')"),
        vec![vec![Value::Boolean(false)]]
    );
    db.execute("SET warnings_as_errors = false").expect("warnings stay warnings");
    db.execute("SET warnings_as_errors = 'no'").expect("the boolean alias");
    let error = db.execute("SET warnings_as_errors = true").expect_err("there is no logger");
    assert_eq!(error.code().duckdb_name(), "Settings Error");
    assert_eq!(
        error.message(),
        "Can not set 'warnings_as_errors=true'; no logger is available. To solve, run: 'SET enable_logging=true;'"
    );
    assert_eq!(db.setting("warnings_as_errors").expect("the setting"), "false");
    db.execute("RESET warnings_as_errors").expect("the default");
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'warnings_as_errors'"
        ),
        vec![vec![text("Escalate all warnings to errors."), text("BOOLEAN"), text("GLOBAL")]]
    );
}

#[test]
fn errors_as_json_structures_errors_at_every_sql_api_boundary() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT current_setting('errors_as_json')"),
        vec![vec![Value::Boolean(false)]]
    );
    db.execute("SET errors_as_json = true").expect("JSON errors");

    let missing = db.query("SELECT * FROM nonexistent_table").expect_err("missing table");
    assert_eq!(missing.code().duckdb_name(), "Catalog Error");
    assert!(missing.message().contains("\"exception_type\":\"Catalog\""));
    assert!(missing.message().contains("\"error_subtype\":\"MISSING_ENTRY\""));
    assert!(!missing.to_string().starts_with("Catalog Error:"));

    let column = db.query("SELECT cbl FROM (VALUES (42)) t(col)").expect_err("missing column");
    assert!(column.message().contains("\"exception_type\":\"Binder\""));
    assert!(column.message().contains("\"error_subtype\":\"COLUMN_NOT_FOUND\""));

    let syntax = db.prepare("SECT 1").expect_err("syntax error");
    assert!(syntax.message().contains("\"exception_type\":\"Parser\""));
    assert!(syntax.message().contains("\"error_subtype\":\"SYNTAX_ERROR\""));
    assert!(syntax.message().contains("\"position\":"));

    assert!(db.plan("SELECT missing").expect_err("plan error").message().starts_with('{'));
    db.execute("RESET errors_as_json").expect("plain errors");
    assert!(
        db.query("SELECT * FROM nonexistent_table")
            .expect_err("plain error")
            .to_string()
            .starts_with("Catalog Error:")
    );
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'errors_as_json'"
        ),
        vec![vec![
            text("Output error messages as structured JSON instead of as a raw string"),
            text("BOOLEAN"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn uncorrelated_scalar_subqueries_are_single_joins() {
    let db = database();
    assert_eq!(rows(&db, "SELECT (SELECT 42)"), vec![vec![Value::Integer(42)]]);
    assert_eq!(rows(&db, "SELECT 1 + (SELECT 2)"), vec![vec![Value::Integer(3)]]);
    assert_eq!(
        rows(&db, "SELECT x, (SELECT 7) FROM (VALUES (1), (2)) t(x) ORDER BY x"),
        vec![
            vec![Value::Integer(1), Value::Integer(7)],
            vec![Value::Integer(2), Value::Integer(7)]
        ]
    );
    assert_eq!(rows(&db, "SELECT (SELECT 1 WHERE false)"), vec![vec![Value::Null]]);
    let several = db
        .query("SELECT (SELECT x FROM (VALUES (1), (2)) t(x))")
        .expect_err("a scalar query has one row at most");
    assert_eq!(several.code().duckdb_name(), "Invalid Input Error");
    assert_eq!(
        several.message(),
        "More than one row returned by a subquery used as an expression - scalar subqueries can only return a single row.\n\nUse \"SET scalar_subquery_error_on_multiple_rows=false\" to revert to previous behavior of returning a random row."
    );
    let plan = db.plan("SELECT (SELECT 42)").expect("the scalar query plans");
    assert!(plan.contains("Join SINGLE"), "{plan}");
}

#[test]
fn scalar_subquery_multiple_row_behavior_is_a_bound_semantic() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT current_setting('scalar_subquery_error_on_multiple_rows')"),
        vec![vec![Value::Boolean(true)]]
    );
    db.execute("SET scalar_subquery_error_on_multiple_rows = false").expect("choose one row");
    assert_eq!(
        rows(&db, "SELECT (SELECT x FROM (VALUES (1), (2)) t(x))"),
        vec![vec![Value::Integer(1)]]
    );
    let plan = db
        .plan("SELECT (SELECT x FROM (VALUES (1), (2)) t(x))")
        .expect("the relaxed scalar query plans");
    assert!(plan.contains("Limit 1 offset 0"), "{plan}");
    db.execute("RESET scalar_subquery_error_on_multiple_rows").expect("restore strict mode");
    assert!(db.query("SELECT (SELECT x FROM (VALUES (1), (2)) t(x))").is_err());
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'scalar_subquery_error_on_multiple_rows'"
        ),
        vec![vec![
            text(
                "Throw an error when a scalar subquery returns more than one row. When disabled, an arbitrary row is returned instead."
            ),
            text("BOOLEAN"),
            text("GLOBAL"),
        ]]
    );
}

#[test]
fn a_correlated_scalar_filter_unnests_to_one_single_join() {
    let db = database();
    let sql = "SELECT k, (SELECT value FROM (VALUES (1, 10), (2, 20)) i(k, value) WHERE i.k = o.k) FROM (VALUES (1), (2), (3)) o(k) ORDER BY k";
    assert_eq!(
        rows(&db, sql),
        vec![
            vec![Value::Integer(1), Value::Integer(10)],
            vec![Value::Integer(2), Value::Integer(20)],
            vec![Value::Integer(3), Value::Null],
        ]
    );
    let plan = db.plan(sql).expect("the correlated scalar query plans");
    assert!(plan.contains("Join SINGLE"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
    assert_eq!(
        rows(
            &db,
            "SELECT (SELECT value FROM (VALUES (1, 10), (1, 20)) i(k, value) WHERE i.k = o.k AND value > 10) FROM (VALUES (1), (2)) o(k) ORDER BY k"
        ),
        vec![vec![Value::Integer(20)], vec![Value::Null]]
    );
    db.execute("SET disabled_optimizers = 'unnest_rewriter'")
        .expect("the upstream optimizer name is accepted");
    assert_eq!(rows(&db, sql)[2], vec![Value::Integer(3), Value::Null]);
}

#[test]
fn a_correlated_scalar_projection_replays_over_distinct_outer_values() {
    let db = database();
    let sql =
        "SELECT k, (SELECT o.k + 1) FROM (VALUES (1), (1), (2), (NULL)) o(k) ORDER BY k NULLS LAST";
    assert_eq!(
        rows(&db, sql),
        vec![
            vec![Value::Integer(1), Value::Integer(2)],
            vec![Value::Integer(1), Value::Integer(2)],
            vec![Value::Integer(2), Value::Integer(3)],
            vec![Value::Null, Value::Null],
        ]
    );
    let plan = db.plan(sql).expect("the correlated scalar projection plans");
    assert!(plan.contains("Join SINGLE"), "{plan}");
    assert!(plan.contains("IS NOT DISTINCT FROM"), "{plan}");
    assert!(plan.contains("__correlated_1"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT o.k + x FROM (VALUES (10)) i(x)) FROM (VALUES (1), (2)) o(k) ORDER BY k"
        ),
        vec![
            vec![Value::Integer(1), Value::Integer(11)],
            vec![Value::Integer(2), Value::Integer(12)],
        ]
    );
    db.execute("CREATE TABLE empty_scalar_source(x INTEGER)")
        .expect("the empty scalar source is created");
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT o.k + x FROM empty_scalar_source) FROM (VALUES (1), (2)) o(k) ORDER BY k"
        ),
        vec![vec![Value::Integer(1), Value::Null], vec![Value::Integer(2), Value::Null],]
    );
    assert!(
        db.query("SELECT (SELECT o.k + x FROM (VALUES (10), (20)) i(x)) FROM (VALUES (1)) o(k)")
            .is_err()
    );
}

#[test]
fn correlated_scalar_aggregates_group_by_hidden_correlation_keys() {
    let db = database();
    let sql = "SELECT k, (SELECT sum(value) FROM (VALUES (1, 10), (1, 20), (2, 5), (NULL, 40)) i(k, value) WHERE i.k = o.k AND value > 5) FROM (VALUES (1), (2), (3), (NULL)) o(k) ORDER BY k NULLS LAST";
    assert_eq!(
        rows(&db, sql),
        vec![
            vec![Value::Integer(1), Value::HugeInt(30)],
            vec![Value::Integer(2), Value::Null],
            vec![Value::Integer(3), Value::Null],
            vec![Value::Null, Value::Null],
        ]
    );
    let plan = db.plan(sql).expect("the correlated scalar aggregate plans");
    assert!(plan.contains("Join SINGLE"), "{plan}");
    assert!(plan.contains("groups=[#1.0::INTEGER]"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn correlated_scalar_counts_keep_zero_for_missing_groups() {
    let db = database();
    let sql = "SELECT k, (SELECT count(*) FROM (VALUES (1, 10), (1, NULL), (2, 5), (NULL, 40)) i(k, value) WHERE i.k = o.k AND value > 5) FROM (VALUES (1), (2), (3), (NULL)) o(k) ORDER BY k NULLS LAST";
    assert_eq!(
        rows(&db, sql),
        vec![
            vec![Value::Integer(1), Value::BigInt(1)],
            vec![Value::Integer(2), Value::BigInt(0)],
            vec![Value::Integer(3), Value::BigInt(0)],
            vec![Value::Null, Value::BigInt(0)],
        ]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT count(1) FROM (VALUES (1), (1), (2)) i(k) WHERE i.k = o.k) FROM (VALUES (1), (3)) o(k) ORDER BY k"
        ),
        vec![vec![Value::Integer(1), Value::BigInt(2)], vec![Value::Integer(3), Value::BigInt(0)],]
    );
    let plan = db.plan(sql).expect("the correlated scalar count plans");
    assert!(plan.contains("Join LEFT"), "{plan}");
    assert!(plan.contains("count(#1.0::INTEGER FILTER"), "{plan}");
    assert!(plan.contains("IS NOT DISTINCT FROM #0.0"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn correlated_scalar_aggregates_use_an_outer_domain_for_arbitrary_predicates() {
    let db = database();
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT count(*) FROM (VALUES (1), (2), (3)) i(x) WHERE i.x < o.k) FROM (VALUES (1), (2), (4)) o(k) ORDER BY k"
        ),
        vec![
            vec![Value::Integer(1), Value::BigInt(0)],
            vec![Value::Integer(2), Value::BigInt(1)],
            vec![Value::Integer(4), Value::BigInt(3)],
        ]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT sum(value) FROM (VALUES (1, 10), (2, 20), (3, 30)) i(x, value) WHERE i.x < o.k) FROM (VALUES (1), (2), (4)) o(k) ORDER BY k"
        ),
        vec![
            vec![Value::Integer(1), Value::Null],
            vec![Value::Integer(2), Value::HugeInt(10)],
            vec![Value::Integer(4), Value::HugeInt(60)],
        ]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT count(*) FROM (VALUES (10), (20)) i(x) WHERE o.k > 1) FROM (VALUES (1), (2)) o(k) ORDER BY k"
        ),
        vec![vec![Value::Integer(1), Value::BigInt(0)], vec![Value::Integer(2), Value::BigInt(2)],]
    );
    let sql = "SELECT (SELECT count(*) FROM (VALUES (NULL), (1)) i(x) WHERE i.x IS DISTINCT FROM o.k) FROM (VALUES (NULL)) o(k)";
    assert_eq!(rows(&db, sql), vec![vec![Value::BigInt(1)]]);
    let plan = db.plan(sql).expect("the arbitrary correlated aggregate plans");
    assert!(plan.contains("Join LEFT"), "{plan}");
    assert!(plan.contains("__inner_"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn correlated_aggregate_arguments_replay_over_the_outer_domain() {
    let db = database();
    let sql = "SELECT k, (SELECT count(o.k) FROM (VALUES (10), (20)) i(x)) FROM (VALUES (1), (NULL)) o(k) ORDER BY k NULLS LAST";
    assert_eq!(
        rows(&db, sql),
        vec![vec![Value::Integer(1), Value::BigInt(2)], vec![Value::Null, Value::BigInt(0)],]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT k, (SELECT sum(o.k) FROM (VALUES (10), (20)) i(x)) FROM (VALUES (1), (NULL)) o(k) ORDER BY k NULLS LAST"
        ),
        vec![vec![Value::Integer(1), Value::HugeInt(2)], vec![Value::Null, Value::Null],]
    );
    db.execute("CREATE TABLE empty_aggregate_source(x INTEGER)")
        .expect("the empty aggregate source is created");
    assert_eq!(
        rows(&db, "SELECT (SELECT count(o.k) FROM empty_aggregate_source) FROM (VALUES (1)) o(k)"),
        vec![vec![Value::BigInt(0)]]
    );
    let plan = db.plan(sql).expect("the correlated aggregate argument plans");
    assert!(plan.contains("Join LEFT"), "{plan}");
    assert!(plan.contains("IS NOT DISTINCT FROM"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn uncorrelated_exists_is_a_single_joined_marker() {
    let db = database();
    let answer = db.query("SELECT EXISTS (SELECT 1)").expect("EXISTS answers");
    assert_eq!(answer.column_name(0), "EXISTS(SELECT 1)");
    assert_eq!(answer.rows().collect::<Vec<_>>(), vec![vec![Value::Boolean(true)]]);
    let negated = db.query("SELECT NOT EXISTS (SELECT 1 WHERE false)").expect("NOT EXISTS answers");
    assert_eq!(negated.column_name(0), "(NOT EXISTS(SELECT 1 WHERE false))");
    assert_eq!(negated.rows().collect::<Vec<_>>(), vec![vec![Value::Boolean(true)]]);
    assert_eq!(
        rows(&db, "SELECT EXISTS (SELECT 1 WHERE false)"),
        vec![vec![Value::Boolean(false)]]
    );
    assert_eq!(
        rows(&db, "SELECT EXISTS (SELECT NULL FROM range(3))"),
        vec![vec![Value::Boolean(true)]]
    );
    assert_eq!(
        rows(&db, "SELECT x FROM (VALUES (1), (2)) t(x) WHERE EXISTS (SELECT 1) ORDER BY x"),
        vec![vec![Value::Integer(1)], vec![Value::Integer(2)]]
    );
    assert!(
        rows(&db, "SELECT x FROM (VALUES (1), (2)) t(x) WHERE EXISTS (SELECT 1 WHERE false)")
            .is_empty()
    );
    let plan = db.plan("SELECT EXISTS (SELECT * FROM range(1000))").expect("EXISTS plans");
    assert!(plan.contains("Join SINGLE"), "{plan}");
    assert!(plan.contains("Limit 1 offset 0"), "{plan}");
}

#[test]
fn correlated_exists_ends_up_a_semi_join_against_the_relation() {
    let db = database();
    let exists = "SELECT k FROM (VALUES (1), (2), (3), (NULL)) o(k) WHERE EXISTS (SELECT 1 FROM (VALUES (1), (1), (3), (NULL)) i(k) WHERE i.k = o.k) ORDER BY k";
    assert_eq!(rows(&db, exists), vec![vec![Value::Integer(1)], vec![Value::Integer(3)]]);
    assert_eq!(
        rows(
            &db,
            "SELECT k FROM (VALUES (1), (2), (3)) o(k) WHERE NOT EXISTS (SELECT 1 FROM (VALUES (1, 5), (2, 20)) i(k, value) WHERE i.k = o.k AND value > 10) ORDER BY k"
        ),
        vec![vec![Value::Integer(1)], vec![Value::Integer(3)]]
    );
    // The keys go through a grouping and a single join on the way here, because that is what the
    // correlated key binds to, and `delim` collapses both of them once the plan is far enough
    // along to see that the grouping is only there to stop the join matching twice.
    let plan = db.plan(exists).expect("the correlated existence query plans");
    assert!(plan.contains("Join SEMI"), "{plan}");
    assert!(!plan.contains("Join SINGLE"), "{plan}");
    assert!(!plan.contains("Aggregate"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
    assert!(!plan.contains("Limit 1"), "{plan}");
}

#[test]
fn correlated_exists_with_an_inequality_ends_up_a_semi_join() {
    let db = database();
    let sql = "SELECT k FROM (VALUES (1), (2), (4)) o(k) WHERE EXISTS (SELECT 1 FROM (VALUES (1), (3)) i(x) WHERE i.x < o.k) ORDER BY k";
    assert_eq!(rows(&db, sql), vec![vec![Value::Integer(2)], vec![Value::Integer(4)]]);
    assert_eq!(
        rows(
            &db,
            "SELECT k FROM (VALUES (1), (2), (4)) o(k) WHERE NOT EXISTS (SELECT 1 FROM (VALUES (1), (3)) i(x) WHERE i.x < o.k AND i.x > 1) ORDER BY k"
        ),
        vec![vec![Value::Integer(1)], vec![Value::Integer(2)]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT EXISTS (SELECT 1 FROM (VALUES (1)) i(x) WHERE i.x IS DISTINCT FROM o.k) FROM (VALUES (NULL)) o(k)"
        ),
        vec![vec![Value::Boolean(true)]]
    );
    let plan = db.plan(sql).expect("the correlated inequality existence query plans");
    // An inequality still decorrelates through a domain of the outer keys, and the deliminator
    // then takes the domain back out, because the outer side of the join back is the relation the
    // domain was standing in for. What is left is the two sides joined on the inequality itself.
    assert!(plan.contains("Join SEMI on=[(#1.0::INTEGER < #0.0::INTEGER)::BOOLEAN]"), "{plan}");
    assert!(!plan.contains("Join SINGLE"), "{plan}");
    assert!(!plan.contains("Aggregate"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn uncorrelated_in_subqueries_are_mark_joins() {
    let db = database();
    let query = |subject: &str, values: &str| {
        format!("SELECT {subject} IN (SELECT x FROM (VALUES {values}) t(x))")
    };
    assert_eq!(rows(&db, &query("1", "(1), (2)")), vec![vec![Value::Boolean(true)]]);
    assert_eq!(rows(&db, &query("3", "(1), (2)")), vec![vec![Value::Boolean(false)]]);
    assert_eq!(rows(&db, &query("3", "(1), (NULL)")), vec![vec![Value::Null]]);
    assert_eq!(rows(&db, &query("NULL", "(1), (2)")), vec![vec![Value::Null]]);
    assert_eq!(
        rows(&db, "SELECT NULL IN (SELECT x FROM (VALUES (1)) t(x) WHERE false)"),
        vec![vec![Value::Boolean(false)]]
    );
    assert_eq!(
        rows(&db, "SELECT 3 NOT IN (SELECT x FROM (VALUES (1), (NULL)) t(x))"),
        vec![vec![Value::Null]]
    );
    let answer = db
        .query("SELECT 1 IN (SELECT x FROM (VALUES (1), (2)) t(x))")
        .expect("the membership query answers");
    assert_eq!(
        answer.column_name(0),
        "(1 = ANY(SELECT x FROM (SELECT * FROM (VALUES (1), (2)) AS valueslist) AS t(x)))"
    );
    let plan = db.plan(&query("1", "(1), (2)")).expect("the membership query plans");
    assert!(plan.contains("Join MARK"), "{plan}");
}

#[test]
fn correlated_membership_filters_unnest_to_mark_joins() {
    let db = database();
    let sql = "SELECT k, k IN (SELECT x FROM (VALUES (1), (2), (NULL)) i(x) WHERE i.x = o.k) FROM (VALUES (1), (3), (NULL)) o(k) ORDER BY k NULLS LAST";
    assert_eq!(
        rows(&db, sql),
        vec![
            vec![Value::Integer(1), Value::Boolean(true)],
            vec![Value::Integer(3), Value::Boolean(false)],
            vec![Value::Null, Value::Boolean(false)],
        ]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT k FROM (VALUES (1), (2), (3)) o(k) WHERE k NOT IN (SELECT x FROM (VALUES (1), (3)) i(x) WHERE i.x = o.k) ORDER BY k"
        ),
        vec![vec![Value::Integer(2)]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT k, k = ALL (SELECT x FROM (VALUES (1), (2)) i(x) WHERE i.x = o.k) FROM (VALUES (1), (3)) o(k) ORDER BY k"
        ),
        vec![
            vec![Value::Integer(1), Value::Boolean(true)],
            vec![Value::Integer(3), Value::Boolean(true)],
        ]
    );
    let plan = db.plan(sql).expect("the correlated membership query plans");
    assert!(plan.contains("Join MARK"), "{plan}");
    assert!(plan.contains("IS NOT DISTINCT FROM TRUE"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn correlated_mark_joins_carry_hidden_filter_columns_after_the_marker() {
    let db = database();
    let sql = "SELECT g, 15 IN (SELECT value FROM (VALUES (1, 10), (1, NULL), (2, 20)) i(g, value) WHERE i.g = o.g) FROM (VALUES (1), (2), (3)) o(g) ORDER BY g";
    assert_eq!(
        rows(&db, sql),
        vec![
            vec![Value::Integer(1), Value::Null],
            vec![Value::Integer(2), Value::Boolean(false)],
            vec![Value::Integer(3), Value::Boolean(false)],
        ]
    );
    let plan = db.plan(sql).expect("the hidden correlation key plans");
    assert!(plan.contains("Join MARK"), "{plan}");
    assert!(plan.contains("__correlated_2"), "{plan}");
    assert!(!plan.contains("DependentJoin"), "{plan}");
}

#[test]
fn uncorrelated_any_and_all_subqueries_are_mark_joins() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT 2 = ANY (SELECT x FROM (VALUES (1), (2)) t(x))"),
        vec![vec![Value::Boolean(true)]]
    );
    assert_eq!(
        rows(&db, "SELECT 2 <> ANY (SELECT x FROM (VALUES (2), (3)) t(x))"),
        vec![vec![Value::Boolean(true)]]
    );
    assert_eq!(
        rows(&db, "SELECT 2 > ALL (SELECT x FROM (VALUES (0), (1)) t(x))"),
        vec![vec![Value::Boolean(true)]]
    );
    assert_eq!(
        rows(&db, "SELECT 2 = ALL (SELECT x FROM (VALUES (2), (NULL)) t(x))"),
        vec![vec![Value::Null]]
    );
    assert_eq!(
        rows(&db, "SELECT NULL = ANY (SELECT x FROM (VALUES (1)) t(x) WHERE false)"),
        vec![vec![Value::Boolean(false)]]
    );
    assert_eq!(
        rows(&db, "SELECT NULL = ALL (SELECT x FROM (VALUES (1)) t(x) WHERE false)"),
        vec![vec![Value::Boolean(true)]]
    );
    let answer = db
        .query("SELECT 2 > ALL (SELECT x FROM (VALUES (0), (1)) t(x))")
        .expect("the universal comparison answers");
    assert_eq!(
        answer.column_name(0),
        "(NOT (2 <= ANY(SELECT x FROM (SELECT * FROM (VALUES (0), (1)) AS valueslist) AS t(x))))"
    );
    let plan = db
        .plan("SELECT 2 = ANY (SELECT x FROM (VALUES (1), (2)) t(x))")
        .expect("the quantified comparison plans");
    assert!(plan.contains("Join MARK"), "{plan}");
}

#[test]
fn the_settings_table_reads_back_what_set_left_behind() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    // A value read through a database is a real one, unlike the same table built with no database
    // behind it, which is what `rudb_exec` tests against. What the default is depends on how much
    // memory the machine has, so what is checked is that something answered rather than what.
    assert!(
        rows(
            &db,
            "SELECT value FROM duckdb_settings() WHERE name = 'memory_limit' AND value IS NOT NULL"
        )
        .len()
            == 1
    );
    db.execute("SET memory_limit = '1GiB'").expect("a size");
    assert_eq!(
        rows(&db, "SELECT value, typed_value FROM duckdb_settings() WHERE name = 'memory_limit'"),
        vec![vec![text("1.0 GiB"), text("1.0 GiB")]]
    );
    // The alias is a row of its own and it reports the same value, because it is the same setting.
    assert_eq!(
        rows(&db, "SELECT value FROM duckdb_settings() WHERE name = 'max_memory'"),
        vec![vec![text("1.0 GiB")]]
    );
    // And writing through the alias moves the setting the other name reads.
    db.execute("SET max_memory = '2GiB'").expect("a size");
    assert_eq!(
        rows(&db, "SELECT value FROM duckdb_settings() WHERE name = 'memory_limit'"),
        vec![vec![text("2.0 GiB")]]
    );
    db.execute("SET worker_threads = 3").expect("a thread count");
    assert_eq!(
        rows(&db, "SELECT value FROM duckdb_settings() WHERE name = 'threads'"),
        vec![vec![text("3")]]
    );
}

#[test]
fn a_setting_read_as_a_value_has_the_type_the_setting_holds() {
    let db = database();
    db.execute("SET threads = 3").expect("a thread count");
    db.execute("SET memory_limit = '1GiB'").expect("a size");
    // A BIGINT and a VARCHAR out of one overload, which is the whole reason the declared return
    // type is ANY. Both were read off the pin.
    assert_eq!(rows(&db, "SELECT current_setting('threads')"), vec![vec![Value::BigInt(3)]]);
    assert_eq!(rows(&db, "SELECT typeof(current_setting('threads'))"), vec![vec![text("BIGINT")]]);
    assert_eq!(rows(&db, "SELECT current_setting('memory_limit')"), vec![vec![text("1.0 GiB")]]);
    assert_eq!(
        rows(&db, "SELECT typeof(current_setting('memory_limit'))"),
        vec![vec![text("VARCHAR")]]
    );
    // An alias reads the setting it points at, both ways round.
    assert_eq!(rows(&db, "SELECT current_setting('worker_threads')"), vec![vec![Value::BigInt(3)]]);
    assert_eq!(rows(&db, "SELECT current_setting('max_memory')"), vec![vec![text("1.0 GiB")]]);
    // The name is matched the way an identifier is, which the pin does as well.
    assert_eq!(rows(&db, "SELECT current_setting('THREADS')"), vec![vec![Value::BigInt(3)]]);
    // The column is named after what was written, since nothing renames a folded call.
    assert_eq!(
        db.query("SELECT current_setting('threads')").expect("a setting").names(),
        ["current_setting('threads')".to_string()]
    );
    // And it reads the setting as it is now rather than as it was when the database opened.
    db.execute("RESET threads").expect("a reset");
    let before = db.opened_with().threads();
    assert_eq!(
        rows(&db, "SELECT current_setting('threads')"),
        vec![vec![Value::BigInt(i64::try_from(before).expect("a thread count fits"))]]
    );
}

#[test]
fn a_setting_read_as_a_value_is_folded_before_the_plan_exists() {
    let db = database();
    db.execute("SET threads = 7").expect("a thread count");
    // The plan holds the number and not the call, which is what the pin does: an EXPLAIN there
    // shows `Projections: 6` over a dummy scan rather than a function over one.
    // The name survives as the column's alias, which is what the pin calls it too, so what is
    // asserted is that the thing projected is the number.
    let plan = db.plan("SELECT current_setting('threads')").expect("a plan");
    assert!(plan.contains("[7::BIGINT AS \"current_setting('threads')\"]"), "{plan}");
}

#[test]
fn a_setting_that_is_not_a_constant_or_not_a_setting_is_refused_the_pins_way() {
    let db = database();
    // A column of names cannot be folded, so the call falls through to the signature table and
    // gets the sentence the pin prints for exactly this.
    assert_eq!(
        failure(&db, "SELECT current_setting(s) FROM t"),
        "The \"setting_name\" argument in function \"current_setting\" must be a constant expression"
    );
    // A name nobody has is the same sentence `SET` gives for it, which is one sentence in one place.
    let unknown = failure(&db, "SELECT current_setting('nope')");
    assert!(unknown.starts_with("unrecognized configuration parameter \"nope\""), "{unknown}");
    assert_eq!(unknown, db.execute("SET nope = 1").unwrap_err().message());
    // The wrong number of arguments is the ordinary arity error with the one overload under it.
    assert_eq!(
        failure(&db, "SELECT current_setting()"),
        "No function matches the given name and argument types 'current_setting()'. You might \
         need to add explicit type casts.\n\tCandidate functions:\n\tcurrent_setting(setting_name \
         VARCHAR) -> ANY\n"
    );
}

#[test]
fn the_settings_table_answers_the_question_a_client_asks_it() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    // The pin's hundred and ninety two rows for a hundred and eighty five settings, because seven
    // of them have a second spelling and the pin gives each spelling a row of its own.
    assert_eq!(rows(&db, "SELECT count(*) FROM duckdb_settings()"), vec![vec![Value::BigInt(192)]]);
    // The description is the pin's sentence word for word, since a client comparing them would
    // otherwise see a difference that is not one.
    assert_eq!(
        rows(
            &db,
            "SELECT description, input_type, scope FROM duckdb_settings() WHERE name = 'threads'"
        ),
        vec![vec![
            text("The number of total threads used by the system."),
            text("BIGINT"),
            text("GLOBAL"),
        ]]
    );
    // The seams are not settings, which is decided in the settings module and checked here because
    // this is the table a reader would find them in if the decision ever changed by accident.
    assert!(rows(&db, "SELECT name FROM duckdb_settings() WHERE name LIKE 'seam%'").is_empty());
    // Every row has a value except the three the pin itself leaves unset, including the hundred and
    // sixty nine rudb does not read, because a client reading this table to find out what an engine
    // is set to should not find a hole where the pin has a value.
    assert_eq!(
        rows(&db, "SELECT name FROM duckdb_settings() WHERE value IS NULL ORDER BY name"),
        vec![
            vec![text("enable_profiling")],
            vec![text("operator_memory_limit")],
            vec![text("parquet_prefetch_column_gap")],
        ]
    );
}

/// A setting rudb takes and does not read still answers `SET`, `RESET`, `current_setting()` and the
/// settings table, because a script that turns a knob in its preamble wanted to keep going and not
/// to be told the engine has never heard the name.
#[test]
fn a_setting_the_engine_does_not_read_still_answers_every_way_of_asking() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    let value = "SELECT value FROM duckdb_settings() WHERE name = 'enable_http_metadata_cache'";
    assert_eq!(rows(&db, value), vec![vec![text("false")]]);
    db.execute("SET enable_http_metadata_cache = true").expect("a knob takes a value");
    assert_eq!(rows(&db, value), vec![vec![text("true")]]);
    assert_eq!(
        rows(&db, "SELECT current_setting('enable_http_metadata_cache')"),
        vec![vec![Value::Boolean(true)]]
    );
    db.execute("RESET enable_http_metadata_cache").expect("a knob resets");
    assert_eq!(rows(&db, value), vec![vec![text("false")]]);
    // A number setting reads back as its own type, which is what `typeof` on the pin says too.
    db.execute("SET partitioned_write_max_open_files = 42").expect("a number knob");
    assert_eq!(
        rows(&db, "SELECT current_setting('partitioned_write_max_open_files')"),
        vec![vec![Value::UBigInt(42)]]
    );
    // And a setting that would change what a query returns is the other half of the rule.
    let error = db.execute("SET preserve_insertion_order = false").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Not implemented Error");
    db.execute("SET preserve_insertion_order = true").expect("the value it already behaves as");
}

/// A pragma that is a statement writes the setting it stands for, and the name carries the value.
///
/// Measured against the pin by snapshotting `duckdb_settings()` either side of each one, which is
/// the only way to fill that table in: `PRAGMA disable_print_progress_bar` writes a setting called
/// `enable_progress_bar_print` and `PRAGMA enable_profiling` writes a word into a `VARCHAR` rather
/// than true into a boolean.
#[test]
fn a_pragma_that_is_a_statement_writes_the_setting_it_stands_for() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    let optimizer = "SELECT value FROM duckdb_settings() WHERE name = 'enable_optimizer'";
    assert_eq!(rows(&db, optimizer), vec![vec![text("true")]]);
    db.execute("PRAGMA disable_optimizer").expect("a pragma that is a statement");
    assert_eq!(rows(&db, optimizer), vec![vec![text("false")]]);
    db.execute("PRAGMA enable_optimizer").expect("and back");
    assert_eq!(rows(&db, optimizer), vec![vec![text("true")]]);
    db.execute("PRAGMA disable_print_progress_bar").expect("the one whose name is not the setting");
    assert_eq!(
        rows(&db, "SELECT current_setting('enable_progress_bar_print')"),
        vec![vec![Value::Boolean(false)]]
    );
    db.execute("PRAGMA enable_profiling").expect("a word rather than a boolean");
    assert_eq!(
        rows(&db, "SELECT current_setting('enable_profiling')"),
        vec![vec![text("query_tree")]]
    );
    // And the pair that turns it off puts it back to nothing rather than to the empty string.
    db.execute("PRAGMA disable_profile").expect("the other spelling of the same statement");
    assert_eq!(rows(&db, "SELECT current_setting('enable_profiling')"), vec![vec![Value::Null]]);
}

/// Nine of the nineteen change nothing a query can see, and succeeding is the whole of what they do.
///
/// Four are deprecated upstream and say so in a warning while doing nothing, and the other five
/// move a flag on the database that `duckdb_settings()` does not list. 692 corpus records are
/// charged to `disable_checkpoint_on_shutdown` alone, all of them a file that says something about
/// checkpoints in its preamble and then goes on to test something else.
#[test]
fn a_pragma_that_changes_nothing_a_query_can_see_still_succeeds() {
    let db = database();
    for statement in [
        "PRAGMA disable_checkpoint_on_shutdown",
        "PRAGMA enable_checkpoint_on_shutdown",
        "PRAGMA disable_object_cache",
        "PRAGMA enable_object_cache",
        "PRAGMA disable_verification",
        "PRAGMA enable_verification",
        "PRAGMA disable_verify_parallelism",
        "PRAGMA verify_parallelism",
        "PRAGMA force_checkpoint",
    ] {
        db.execute(statement).unwrap_or_else(|error| panic!("{statement}: {error}"));
    }
    // A name of the same shape that no engine has is the catalog's complaint, in the words it uses
    // about a pragma rather than the words it uses about a setting.
    let error = db.execute("PRAGMA enable_nothing_at_all").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Catalog Error");
    assert_eq!(
        error.to_string(),
        "Catalog Error: Pragma Function with name enable_nothing_at_all does not exist!"
    );
}

/// Three settings are unset on a fresh connection rather than empty, and null is what they read as.
#[test]
fn a_setting_the_pin_leaves_unset_reads_as_null_rather_than_as_the_empty_string() {
    let db = database();
    let unset = "SELECT name FROM duckdb_settings() WHERE value IS NULL ORDER BY name";
    let text = |value: &str| Value::Varchar(value.to_string());
    assert_eq!(
        rows(&db, unset),
        vec![
            vec![text("enable_profiling")],
            vec![text("operator_memory_limit")],
            vec![text("parquet_prefetch_column_gap")],
        ]
    );
    // The one that is a number is the reason this matters more than a rendering detail. Reading it
    // as its own type went through the empty string and came out an internal error.
    assert_eq!(
        rows(&db, "SELECT current_setting('parquet_prefetch_column_gap')"),
        vec![vec![Value::Null]]
    );
    db.execute("SET parquet_prefetch_column_gap = 64").expect("and it still takes a number");
    assert_eq!(
        rows(&db, "SELECT current_setting('parquet_prefetch_column_gap')"),
        vec![vec![Value::UBigInt(64)]]
    );
}

/// Every pragma in the catalog is one the parser sends to the catalog.
///
/// The parser decides which pragmas are a statement by their spelling and the catalog decides what
/// each one means, so an entry here that the parser reads as a query would never run at all. This
/// is the join between the two halves, and it is a test rather than a shared list because what it
/// is really checking is that a statement written out reaches the place that answers it.
#[test]
fn every_pragma_the_catalog_has_is_one_the_parser_sends_to_it() {
    let db = database();
    for entry in rudb_functions::PRAGMAS {
        let statement = format!("PRAGMA {}", entry.name);
        db.execute(&statement).unwrap_or_else(|error| panic!("{statement}: {error}"));
    }
}

#[test]
fn the_databases_and_schemas_tables_describe_the_catalogs_there_are() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    let yes = Value::Boolean(true);
    let no = Value::Boolean(false);
    // Three databases and five schemas, which is what the pin returns from a session that has
    // attached nothing. `memory` is the one a person creates in and the other two are the engine's.
    assert_eq!(
        rows(
            &db,
            "SELECT database_name, internal, type, readonly FROM duckdb_databases() ORDER BY 1"
        ),
        vec![
            vec![text("memory"), no.clone(), text("duckdb"), no.clone()],
            vec![text("system"), yes.clone(), text("duckdb"), no.clone()],
            vec![text("temp"), yes.clone(), text("duckdb"), no.clone()],
        ]
    );
    // Internal on every row including `memory.main`, which is the pin's answer and reads oddly
    // until you notice that nobody made that schema either.
    assert_eq!(
        rows(
            &db,
            "SELECT database_name, schema_name, internal FROM duckdb_schemas() ORDER BY 1, 2"
        ),
        vec![
            vec![text("memory"), text("main"), yes.clone()],
            vec![text("system"), text("information_schema"), yes.clone()],
            vec![text("system"), text("main"), yes.clone()],
            vec![text("system"), text("pg_catalog"), yes.clone()],
            vec![text("temp"), text("main"), yes],
        ]
    );
    // The join a client writes, which is the whole reason these two carry numbers.
    assert_eq!(
        rows(
            &db,
            "SELECT count(*) FROM duckdb_schemas() s, duckdb_databases() d \
             WHERE s.database_oid = d.database_oid"
        ),
        vec![vec![Value::BigInt(5)]]
    );
}

/// The views a session has without making any, which is where `information_schema` comes from.
///
/// Every value here was read off the pin on the same statements, and the one thing that does not
/// match it is how many of these there are: upstream ships 47 and rudb ships the 12 whose bodies
/// only read table functions it has. See `rudb_catalog::system` for what the other 35 are waiting on.
#[test]
fn the_engine_ships_with_the_views_upstream_ships_with() {
    // A database of its own rather than the shared one, because what these views report is
    // everything in the catalog and the point of the test is that it is exactly what was made here.
    let db = Database::new();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE t(a INTEGER NOT NULL, b VARCHAR)").expect("a table to describe");
    db.execute("CREATE VIEW v AS SELECT a FROM t").expect("a view to describe");
    // The standard view of tables, which is a union of the two wrappers and so lists the table and
    // the view and nothing the engine owns.
    assert_eq!(
        rows(
            &db,
            "SELECT table_catalog, table_schema, table_name, table_type, is_insertable_into \
             FROM information_schema.tables ORDER BY table_name"
        ),
        vec![
            vec![text("memory"), text("main"), text("t"), text("BASE TABLE"), text("YES")],
            vec![text("memory"), text("main"), text("v"), text("VIEW"), text("NO")],
        ]
    );
    // A view's columns are listed next to a table's, and `is_nullable` is the word rather than the
    // boolean, which is the standard's spelling and the reason this view exists at all.
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, column_name, ordinal_position, is_nullable, data_type \
             FROM information_schema.columns ORDER BY table_name, ordinal_position"
        ),
        vec![
            vec![text("t"), text("a"), Value::Integer(1), text("NO"), text("INTEGER")],
            vec![text("t"), text("b"), Value::Integer(2), text("YES"), text("VARCHAR")],
            vec![text("v"), text("a"), Value::Integer(1), text("YES"), text("INTEGER")],
        ]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT character_set_name, default_collate_name FROM information_schema.character_sets"
        ),
        vec![vec![text("UTF8"), text("ucs_basic")]]
    );
    // The wrapper and the table function are different questions. The bare name is what somebody
    // made and the parentheses are what is really there, and the gap is the engine's own views.
    assert_eq!(rows(&db, "SELECT count(*) FROM duckdb_views"), vec![vec![Value::BigInt(1)]]);
    assert_eq!(rows(&db, "SELECT count(*) FROM duckdb_views()"), vec![vec![Value::BigInt(13)]]);
    // Nothing goes into a database the engine owns and nothing comes out of one.
    assert_eq!(
        failure(&db, "CREATE TABLE information_schema.x(a INTEGER)"),
        "Cannot create entry in system catalog"
    );
    // Through `execute` rather than `query`, because a drop answers nothing and the query path
    // complains about that before it gets as far as the catalog.
    assert_eq!(
        db.execute("DROP VIEW duckdb_views").expect_err("an internal entry").message(),
        "Cannot drop internal catalog entry \"duckdb_views\"!"
    );
}

/// The two pragmas that take a name, both of them answered while the query is bound.
///
/// Every row below was read off the pin on the same statements before it was written here.
#[test]
fn the_two_pragmas_describe_a_table_and_a_view_the_way_upstream_does() {
    // A database of its own, because these report the columns of whatever was made here and the
    // shared one has a second table in it that would only be noise.
    let db = Database::new();
    db.execute("CREATE TABLE t(a INTEGER NOT NULL, b VARCHAR)").expect("a table to describe");
    db.execute("CREATE VIEW v AS SELECT a FROM t").expect("a view to describe");
    let no = Value::Boolean(false);
    // `dflt_value` is null and `pk` is false on every row, because `CREATE TABLE` in rudb takes
    // neither a default nor a key yet. `cid` counts from zero, which is SQLite's numbering.
    let table = vec![
        vec![integer(0), text("a"), text("INTEGER"), Value::Boolean(true), Value::Null, no.clone()],
        vec![integer(1), text("b"), text("VARCHAR"), no.clone(), Value::Null, no.clone()],
    ];
    assert_eq!(rows(&db, "SELECT * FROM pragma_table_info('t')"), table);
    // The name is split under the identifier rule rather than taken whole, so a qualified name and
    // a fully qualified one in the wrong case are the same table.
    assert_eq!(rows(&db, "SELECT * FROM pragma_table_info('main.t')"), table);
    assert_eq!(rows(&db, "SELECT * FROM pragma_table_info('MEMORY.MAIN.T')"), table);
    // Every column of a view is nullable whatever the column underneath was declared as, so the
    // `NOT NULL` on `a` is gone by the time it is read through `v`.
    assert_eq!(
        rows(&db, "SELECT * FROM pragma_table_info('v')"),
        vec![vec![integer(0), text("a"), text("INTEGER"), no.clone(), Value::Null, no]]
    );
    // The same two columns again in the six `DESCRIBE` answers with, where nullability is the word
    // rather than the boolean and the sense of it is the other way round.
    assert_eq!(
        rows(&db, "SELECT * FROM pragma_show('t')"),
        vec![
            vec![text("a"), text("INTEGER"), text("NO"), Value::Null, Value::Null, Value::Null],
            vec![text("b"), text("VARCHAR"), text("YES"), Value::Null, Value::Null, Value::Null],
        ]
    );
    // An ordinary relation, which is the point of having these as functions and not only as
    // statements: an alias, a column list of its own, a qualified reference and a filter.
    assert_eq!(
        rows(
            &db,
            "SELECT info.name, info.type FROM pragma_table_info('t') AS info WHERE info.cid = 1"
        ),
        vec![vec![text("b"), text("VARCHAR")]]
    );
    assert_eq!(
        rows(&db, "SELECT n FROM pragma_table_info('t') AS info(c, n, ty, nn, d, k) WHERE c = 0"),
        vec![vec![text("a")]]
    );
}

/// Describing a view is reading it, including one the engine ships with.
#[test]
fn a_pragma_pointed_at_an_internal_view_binds_it_and_reports_its_columns() {
    let db = Database::new();
    let bound = "SELECT column_count FROM duckdb_views() WHERE view_name = 'duckdb_views'";
    assert_eq!(rows(&db, bound), vec![vec![Value::Null]]);
    assert_eq!(
        rows(&db, "SELECT count(*) FROM pragma_table_info('duckdb_views')"),
        vec![vec![Value::BigInt(13)]]
    );
    assert_eq!(rows(&db, bound), vec![vec![Value::BigInt(13)]]);
}

/// The four ways of getting one of these wrong, all four measured on the pin.
#[test]
fn a_pragma_given_a_name_that_is_not_there_says_what_the_catalog_says() {
    let db = database();
    assert_eq!(
        failure(&db, "SELECT * FROM pragma_table_info('nope')"),
        "Table with name nope does not exist!"
    );
    // A null is a name spelled `NULL` rather than a complaint about nulls, because the pin turns
    // whatever it was handed into text and then goes looking for a table called that.
    assert_eq!(
        failure(&db, "SELECT * FROM pragma_show(NULL)"),
        "Table with name NULL does not exist!"
    );
    for wrong in ["pragma_table_info()", "pragma_table_info('a', 'b')", "pragma_show(3)"] {
        let message = failure(&db, &format!("SELECT * FROM {wrong}"));
        assert!(message.starts_with("No function matches the given name"), "{message}");
        assert!(message.contains("(VARCHAR)"), "{message}");
    }
}

/// The four pragmas that report on the build and the process answer in one row each, in the columns
/// the pin names, and none of them takes anything.
#[test]
fn the_four_pragmas_about_the_build_answer_in_one_row_of_their_own_columns() {
    let db = database();
    let shapes = [
        ("pragma_version", vec!["library_version", "source_id", "codename"]),
        ("pragma_platform", vec!["platform"]),
        ("pragma_user_agent", vec!["user_agent"]),
        (
            "pragma_database_size",
            vec![
                "database_name",
                "database_size",
                "block_size",
                "total_blocks",
                "used_blocks",
                "free_blocks",
                "wal_size",
                "memory_usage",
                "memory_limit",
            ],
        ),
    ];
    for (name, columns) in shapes {
        let answer = rows(&db, &format!("SELECT * FROM {name}()"));
        assert_eq!(answer.len(), 1, "{name} answers about one build and one process");
        assert_eq!(answer[0].len(), columns.len(), "{name}");
        let named: Vec<Value> =
            rows(&db, &format!("SELECT column_name FROM (DESCRIBE SELECT * FROM {name}())"))
                .into_iter()
                .map(|row| row[0].clone())
                .collect();
        assert_eq!(named, columns.iter().map(|column| text(column)).collect::<Vec<Value>>());
        let message = failure(&db, &format!("SELECT * FROM {name}('t')"));
        assert!(message.starts_with("No function matches the given name"), "{message}");
        assert!(message.contains(&format!("\"{name}\"()")), "{message}");
    }
}

/// What the version pragma says is true of this engine rather than borrowed from the pin, and the
/// other two strings are built out of the same two facts.
#[test]
fn the_version_pragma_says_what_this_engine_is_and_the_others_agree_with_it() {
    let db = database();
    let version = rows(&db, "SELECT * FROM pragma_version()").remove(0);
    assert_eq!(version[0], text(&format!("v{}", env!("CARGO_PKG_VERSION"))));
    // A build nobody handed a revision to reports an empty source id, which is the honest answer,
    // and the codename is the one DuckDB itself uses before a release is named.
    assert_eq!(version[1], text(""));
    assert_eq!(version[2], text("Development Version"));
    let platform = rows(&db, "SELECT * FROM pragma_platform()").remove(0);
    let Value::Varchar(written) = &platform[0] else { panic!("the platform is text") };
    assert!(written.contains('_'), "{written}");
    assert!(!written.contains("macos") && !written.contains("x86_64"), "{written}");
    let agent = rows(&db, "SELECT * FROM pragma_user_agent()").remove(0);
    assert_eq!(agent[0], text(&format!("rudb/v{}({written})", env!("CARGO_PKG_VERSION"))));
}

/// The size pragma has a row for the one database that is attached and none for the two internal
/// ones, and the numbers are zero because nothing here is on disk.
#[test]
fn the_size_pragma_reports_the_attached_database_and_the_live_memory_budget() {
    let db = database();
    let size = rows(&db, "SELECT * FROM pragma_database_size()");
    assert_eq!(size.len(), 1);
    let row = &size[0];
    assert_eq!(row[0], text("memory"));
    for column in [1, 6] {
        assert_eq!(row[column], text("0 bytes"));
    }
    assert_eq!(
        row[2..=5],
        [Value::BigInt(0), Value::BigInt(0), Value::BigInt(0), Value::BigInt(0)]
    );
    // The last two are read off the budget rather than made up, so a limit somebody set shows up
    // here the same way it shows up in `current_setting`.
    db.execute("SET memory_limit = '1GiB'").expect("a size");
    let after = rows(&db, "SELECT memory_limit FROM pragma_database_size()").remove(0);
    assert_eq!(after[0], text("1.0 GiB"));
    assert_eq!(after[0], rows(&db, "SELECT current_setting('memory_limit')").remove(0)[0]);
}

/// The storage pragma answers in the pin's sixteen columns, and says nothing about rows that have
/// not been written down.
///
/// Both halves matter. The width is the contract a client reads, so it is spelled out here rather
/// than taken from the builder the implementation uses, which would agree with itself whatever it
/// said. The empty answer is the honest one for a table living in memory: the encoder has not run
/// on those rows and will not until a checkpoint, so there is no encoding to report and a row
/// claiming the chunk is stored plain would be a lie. What a real file says is in
/// `tests/storage.rs`, which needs a file to say it.
#[test]
fn the_storage_pragma_answers_in_sixteen_columns_and_leaves_unwritten_rows_out() {
    let db = database();
    let result = db.query("SELECT * FROM pragma_storage_info('t')").expect("the pragma ran");
    assert_eq!(
        result.names(),
        [
            "row_group_id",
            "column_name",
            "column_id",
            "column_path",
            "segment_id",
            "segment_type",
            "start",
            "count",
            "compression",
            "stats",
            "has_updates",
            "persistent",
            "block_id",
            "block_offset",
            "segment_info",
            "additional_block_ids",
        ]
    );
    assert_eq!(
        result.types(),
        [
            LogicalType::BigInt,
            LogicalType::Varchar,
            LogicalType::BigInt,
            LogicalType::Varchar,
            LogicalType::BigInt,
            LogicalType::Varchar,
            LogicalType::BigInt,
            LogicalType::BigInt,
            LogicalType::Varchar,
            LogicalType::Varchar,
            LogicalType::Boolean,
            LogicalType::Boolean,
            LogicalType::BigInt,
            LogicalType::BigInt,
            LogicalType::Varchar,
            LogicalType::list(LogicalType::BigInt),
        ]
    );
    assert_eq!(result.len(), 0, "nothing is on disk here, so nothing is stored in any form");
    // The name goes through the catalog like any other, so a table that is not there says what the
    // catalog says and not something of this function's own.
    assert_eq!(
        failure(&db, "SELECT * FROM pragma_storage_info('nope')"),
        "Table with name nope does not exist!"
    );
}

/// `PRAGMA name` is the call it stands for, so the statement form answers what the function does.
#[test]
fn the_pragma_statement_answers_what_the_function_of_that_name_answers() {
    let db = database();
    for (statement, call) in [
        ("PRAGMA version", "SELECT * FROM pragma_version()"),
        ("PRAGMA platform", "SELECT * FROM pragma_platform()"),
        ("PRAGMA user_agent", "SELECT * FROM pragma_user_agent()"),
        ("PRAGMA database_size", "SELECT * FROM pragma_database_size()"),
        ("PRAGMA table_info('t')", "SELECT * FROM pragma_table_info('t')"),
        ("PRAGMA table_info(t)", "SELECT * FROM pragma_table_info('t')"),
        ("PRAGMA table_info(main.t)", "SELECT * FROM pragma_table_info('main.t')"),
        // The name is looked up without regard to case, the same way every other name is.
        ("PRAGMA VERSION", "SELECT * FROM pragma_version()"),
        // A view rather than a function, which the pragma namespace holds as well.
        ("PRAGMA database_list", "SELECT * FROM pragma_database_list"),
    ] {
        assert_eq!(rows(&db, statement), rows(&db, call), "{statement}");
    }
}

/// `CALL f(...)` is the table function in a `FROM` clause with the clause left off.
///
/// It is the spelling DuckDB's own extensions are driven with, so a client written against the pin
/// reaches for it, and every table function here was already reachable the long way round. The
/// cases below are one of each shape an argument list comes in rather than a list of functions,
/// since what is being tested is the transform and not the functions, which have their own tests.
#[test]
fn a_call_runs_the_table_function_the_same_query_over_it_would() {
    let db = database();
    for (call, query) in [
        ("CALL pragma_version()", "SELECT * FROM pragma_version()"),
        ("CALL range(3)", "SELECT * FROM range(3)"),
        ("CALL range(1, 7, 2)", "SELECT * FROM range(1, 7, 2)"),
        ("CALL pragma_table_info('t')", "SELECT * FROM pragma_table_info('t')"),
        // A schema in front of the name and the name written in another case, both of which the
        // function form takes and neither of which this spelling has any reason to treat its own
        // way, since it reads the name with the same rule.
        ("CALL main.range(3)", "SELECT * FROM main.range(3)"),
        ("call RANGE(3)", "SELECT * FROM range(3)"),
    ] {
        assert_eq!(rows(&db, call), rows(&db, query), "{call}");
    }
    // One plan, which is the argument for building the query rather than a second path to the same
    // rows: two paths would be two places for the planner to be told something different.
    assert_eq!(rows(&db, "EXPLAIN CALL range(3)"), rows(&db, "EXPLAIN SELECT * FROM range(3)"));
    // A name that is not a table function gets the catalog's words about it, the same ones the
    // other spelling gets, rather than anything this transform says on its own.
    assert_eq!(
        failure(&db, "CALL nope()"),
        "Table Function with name nope does not exist!",
        "the catalog answers for the name"
    );
    assert_eq!(failure(&db, "CALL abs(1)"), failure(&db, "SELECT * FROM abs(1)"));
    // The rule has no room for an alias, on the pin as well, so this is a parser error on both and
    // not a clause that is read and dropped.
    assert!(
        failure(&db, "CALL range(3) AS r").starts_with("syntax error at or near \"AS\""),
        "{}",
        failure(&db, "CALL range(3) AS r")
    );
}

/// `PRAGMA name = value` is a `SET` written another way and it moves the same setting.
#[test]
fn a_pragma_with_an_equals_sign_sets_the_setting_a_plain_set_would() {
    let db = database();
    db.execute("PRAGMA memory_limit = '1GiB'").expect("a size");
    assert_eq!(rows(&db, "SELECT current_setting('memory_limit')"), vec![vec![text("1.0 GiB")]]);
    db.execute("PRAGMA threads = 4").expect("a count");
    assert_eq!(rows(&db, "SELECT current_setting('threads')"), vec![vec![Value::BigInt(4)]]);
}

/// The three `show` pragmas, which answer what is here rather than what a query returns.
#[test]
fn the_show_pragmas_list_the_tables_the_databases_and_both_with_the_columns() {
    let db = database();
    db.execute("CREATE VIEW v AS SELECT 1 AS a").expect("a view");
    // Tables and views together and sorted by name, because an unqualified name reaches both and
    // the pin does not say which kind a name is here.
    assert_eq!(
        rows(&db, "PRAGMA show_tables"),
        vec![vec![text("empty")], vec![text("t")], vec![text("v")]]
    );
    assert_eq!(rows(&db, "PRAGMA show_databases"), vec![vec![text("memory")]]);
    let names = |values: Vec<&str>| Value::List {
        element: LogicalType::Varchar,
        values: values.into_iter().map(text).collect(),
    };
    assert_eq!(
        rows(&db, "PRAGMA show_tables_expanded"),
        vec![
            vec![
                text("memory"),
                text("main"),
                text("empty"),
                names(vec!["x"]),
                names(vec!["INTEGER"]),
                Value::Boolean(false),
            ],
            vec![
                text("memory"),
                text("main"),
                text("t"),
                names(vec!["x", "s"]),
                names(vec!["INTEGER", "VARCHAR"]),
                Value::Boolean(false),
            ],
            vec![
                text("memory"),
                text("main"),
                text("v"),
                names(vec!["a"]),
                names(vec!["INTEGER"]),
                Value::Boolean(false),
            ],
        ]
    );
}

/// `SHOW TABLES` and the rest of the special forms are the three pragmas written another way.
#[test]
fn the_show_statement_answers_the_pragma_of_the_same_name() {
    let db = database();
    for (statement, pragma) in [
        ("SHOW TABLES", "PRAGMA show_tables"),
        ("SHOW tables", "PRAGMA show_tables"),
        ("DESCRIBE TABLES", "PRAGMA show_tables"),
        ("SHOW DATABASES", "PRAGMA show_databases"),
        ("DESCRIBE DATABASES", "PRAGMA show_databases"),
        ("SHOW ALL", "PRAGMA show_tables_expanded"),
        ("SHOW ALL TABLES", "PRAGMA show_tables_expanded"),
        ("DESCRIBE ALL", "PRAGMA show_tables_expanded"),
    ] {
        assert_eq!(rows(&db, statement), rows(&db, pragma), "{statement}");
    }
    // A table of that name does not get it back, because the pin reads the word before the name
    // reaches the catalog, and a qualified name is not the special form at all.
    db.execute("CREATE TABLE tables(a INTEGER)").expect("a table named tables");
    assert_eq!(rows(&db, "SHOW TABLES"), rows(&db, "PRAGMA show_tables"));
    assert_eq!(rows(&db, "DESCRIBE TABLES"), rows(&db, "PRAGMA show_tables"));
    assert_eq!(rows(&db, "DESCRIBE main.tables"), rows(&db, "DESCRIBE SELECT * FROM tables"));
}

/// A pragma only name written where a table goes is a name that is not there.
#[test]
fn a_name_that_exists_only_after_the_word_pragma_is_not_a_table_function() {
    let db = database();
    for name in ["pragma_show_tables", "pragma_show_databases", "pragma_show_tables_expanded"] {
        assert_eq!(
            failure(&db, &format!("SELECT * FROM {name}()")),
            format!("Table Function with name {name} does not exist!")
        );
    }
    // The other spelling of the same name works, so the two halves really are separate.
    assert!(!rows(&db, "PRAGMA show_tables").is_empty());
    // And these take no arguments, which is said about the pragma and not about the rewrite.
    let message = failure(&db, "PRAGMA show_tables(1)");
    assert!(message.contains("'show_tables(INTEGER)'"), "{message}");
    assert!(message.ends_with("\tPRAGMA \"show_tables\"\n"), "{message}");
}

/// What a pragma says when it is not one, and when it is one and was called wrongly.
#[test]
fn a_pragma_that_is_wrong_is_complained_about_in_the_spelling_it_was_written_in() {
    let db = database();
    // The name goes back out as it was typed, including its case, which is what the pin does.
    assert_eq!(failure(&db, "PRAGMA nope"), "Pragma Function with name nope does not exist!");
    assert_eq!(failure(&db, "PRAGMA NOPE"), "Pragma Function with name NOPE does not exist!");
    // A pragma that exists and was handed the wrong arguments is told about a pragma and not
    // about the `pragma_` name it was rewritten into, because the rewrite is not what was written.
    let message = failure(&db, "PRAGMA table_info");
    assert!(message.contains("'table_info()'"), "{message}");
    assert!(message.ends_with("\tPRAGMA \"table_info\"(VARCHAR)\n"), "{message}");
    let message = failure(&db, "PRAGMA table_info(1)");
    assert!(message.contains("'table_info(INTEGER)'"), "{message}");
    let message = failure(&db, "PRAGMA version(1)");
    assert!(message.ends_with("\tPRAGMA \"version\"\n"), "{message}");
}

/// A view the engine ships with goes in unbound and is bound at the first read, which is a fact
/// about two columns of `duckdb_views()` and was measured on the pin twice over.
#[test]
fn a_view_the_engine_ships_with_is_bound_when_it_is_first_read() {
    let db = database();
    let unbound = "SELECT column_count, is_bound FROM duckdb_views() WHERE view_name = 'schemata'";
    assert_eq!(rows(&db, unbound), vec![vec![Value::Null, Value::Boolean(false)]]);
    assert_eq!(
        rows(&db, "SELECT count(*) FROM information_schema.schemata"),
        vec![vec![Value::BigInt(5)]]
    );
    assert_eq!(rows(&db, unbound), vec![vec![Value::BigInt(7), Value::Boolean(true)]]);
}

#[test]
fn a_schema_carries_an_oid_of_its_own_and_not_its_databases() {
    let db = database();
    let rows = rows(&db, "SELECT oid, database_oid FROM duckdb_schemas()");
    assert_eq!(rows.len(), 5);
    for row in &rows {
        assert_ne!(row[0], row[1]);
        // Neither of them is the number a detached entry carries, which is what a caller reading
        // these through a join would silently collapse on.
        assert_ne!(row[0], Value::BigInt(0));
        assert_ne!(row[1], Value::BigInt(0));
    }
}

#[test]
fn the_tables_and_columns_tables_describe_what_was_created() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE shapes(x INTEGER, s VARCHAR, d DECIMAL(9,2), b BOOLEAN)")
        .expect("a fresh table");
    db.execute("INSERT INTO shapes VALUES (1, 'a', 1.5, true)").expect("a row");
    // Every value here is the pin's, read off it on the same statements. The `sql` column is the
    // interesting one: it is written back out from the entry rather than stored, which is why the
    // types come back upper case on a statement that did not write them that way.
    assert_eq!(
        rows(
            &db,
            "SELECT column_count, estimated_size, index_count, check_constraint_count, sql \
             FROM duckdb_tables() WHERE table_name = 'shapes'"
        ),
        vec![vec![
            Value::BigInt(4),
            Value::BigInt(1),
            Value::BigInt(0),
            Value::BigInt(0),
            text("CREATE TABLE shapes(x INTEGER, s VARCHAR, d DECIMAL(9,2), b BOOLEAN);"),
        ]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT column_name, column_index, data_type, data_type_id, numeric_precision, \
             numeric_precision_radix, numeric_scale FROM duckdb_columns() \
             WHERE table_name = 'shapes' ORDER BY column_index"
        ),
        vec![
            vec![
                text("x"),
                Value::Integer(1),
                text("INTEGER"),
                Value::BigInt(13),
                Value::Integer(32),
                Value::Integer(2),
                Value::Integer(0),
            ],
            vec![
                text("s"),
                Value::Integer(2),
                text("VARCHAR"),
                Value::BigInt(25),
                Value::Null,
                Value::Null,
                Value::Null,
            ],
            vec![
                text("d"),
                Value::Integer(3),
                text("DECIMAL(9,2)"),
                Value::BigInt(21),
                Value::Integer(9),
                Value::Integer(10),
                Value::Integer(2),
            ],
            vec![
                text("b"),
                Value::Integer(4),
                text("BOOLEAN"),
                Value::BigInt(10),
                Value::Null,
                Value::Null,
                Value::Null,
            ],
        ]
    );
}

#[test]
fn a_table_and_its_columns_agree_on_the_oid_they_join_on() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE joined(x INTEGER, s VARCHAR)").expect("a fresh table");
    // The join a client writes, and the reason every one of these tables carries an oid.
    assert_eq!(
        rows(
            &db,
            "SELECT c.column_name FROM duckdb_columns() c, duckdb_tables() t \
             WHERE c.table_oid = t.table_oid AND t.table_name = 'joined' ORDER BY c.column_index"
        ),
        vec![vec![text("x")], vec![text("s")]]
    );
}

#[test]
fn a_not_null_column_says_so_in_both_tables() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE n(a INTEGER NOT NULL, b INTEGER)").expect("a fresh table");
    assert_eq!(
        rows(&db, "SELECT sql FROM duckdb_tables() WHERE table_name = 'n'"),
        vec![vec![text("CREATE TABLE n(a INTEGER NOT NULL, b INTEGER);")]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT is_nullable FROM duckdb_columns() WHERE table_name = 'n' ORDER BY column_index"
        ),
        vec![vec![Value::Boolean(false)], vec![Value::Boolean(true)]]
    );
}

#[test]
fn a_view_lists_its_columns_the_way_a_table_does() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE base(x INTEGER NOT NULL, s VARCHAR, d DECIMAL(9,2))")
        .expect("a fresh table");
    db.execute("CREATE VIEW v AS SELECT x, s, d, x + 1 AS e FROM base").expect("a fresh view");
    db.execute("CREATE VIEW w(p, q) AS SELECT x, s FROM base").expect("a view with an alias list");
    // Every one of these was read off the pin. A computed column is reported as the type it comes
    // out as, the alias list is what the columns answer to, and `is_nullable` is true even on the
    // column that reads a NOT NULL column straight through.
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, column_name, column_index, is_nullable, data_type \
             FROM duckdb_columns() WHERE table_name IN ('v', 'w') \
             ORDER BY table_name, column_index"
        ),
        vec![
            vec![text("v"), text("x"), Value::Integer(1), Value::Boolean(true), text("INTEGER")],
            vec![text("v"), text("s"), Value::Integer(2), Value::Boolean(true), text("VARCHAR")],
            vec![
                text("v"),
                text("d"),
                Value::Integer(3),
                Value::Boolean(true),
                text("DECIMAL(9,2)")
            ],
            vec![text("v"), text("e"), Value::Integer(4), Value::Boolean(true), text("INTEGER")],
            vec![text("w"), text("p"), Value::Integer(1), Value::Boolean(true), text("INTEGER")],
            vec![text("w"), text("q"), Value::Integer(2), Value::Boolean(true), text("VARCHAR")],
        ]
    );
    // A view's columns carry the view's oid rather than the oid of whatever is underneath it, which
    // is the join a client writes and the one that would silently return the wrong rows.
    assert_eq!(
        rows(
            &db,
            "SELECT count(*) FROM duckdb_columns() c, duckdb_tables() t \
             WHERE c.table_oid = t.table_oid AND c.table_name IN ('v', 'w')"
        ),
        vec![vec![Value::BigInt(0)]]
    );
}

/// Every row of the fifth catalog table, against what the pin answers for the same two views.
#[test]
fn a_view_reports_itself_and_the_statement_it_was_written_as() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE base(x INTEGER, s VARCHAR)").expect("a fresh table");
    db.execute("CREATE VIEW v AS SELECT x, s FROM base WHERE x > 0").expect("a fresh view");
    // Odd spacing, a comment, a lower case keyword and a name in the wrong case, none of which
    // survives into the column. What the pin reports is the statement written back out, so the
    // comment goes, the spacing is normalised, the keywords come back upper case and the names come
    // back in the case they were written in.
    db.execute("CREATE VIEW w(p) AS -- a comment\n   select   X  as Y from  base")
        .expect("a view with an alias list");
    assert_eq!(
        rows(
            &db,
            "SELECT view_name, column_count, internal, temporary, is_bound, sql \
             FROM duckdb_views() WHERE view_name IN ('v', 'w') ORDER BY view_name"
        ),
        vec![
            vec![
                text("v"),
                Value::BigInt(2),
                Value::Boolean(false),
                Value::Boolean(false),
                Value::Boolean(true),
                text("CREATE VIEW v AS SELECT x, s FROM base WHERE (x > 0);"),
            ],
            vec![
                text("w"),
                Value::BigInt(1),
                Value::Boolean(false),
                Value::Boolean(false),
                Value::Boolean(true),
                text("CREATE VIEW w (p) AS SELECT X AS Y FROM base;"),
            ],
        ]
    );
    // The join a client writes, which is the whole point of the oid columns being filled in.
    assert_eq!(
        rows(
            &db,
            "SELECT count(*) FROM duckdb_views() v, duckdb_columns() c \
             WHERE v.view_oid = c.table_oid"
        ),
        vec![vec![Value::BigInt(3)]]
    );
    // And a table is not a view, so neither table lists what the other one does.
    assert!(rows(&db, "SELECT view_name FROM duckdb_views() WHERE view_name = 'base'").is_empty());
    assert!(rows(&db, "SELECT table_name FROM duckdb_tables() WHERE table_name = 'v'").is_empty());
}

/// The two tables a client reads on connect to find out what engine it got.
#[test]
fn the_engine_answers_for_its_optimizer_passes_and_its_extensions() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    // Forty four on the pin and forty four here, because the table is the set of names
    // `SET disabled_optimizers` takes and rudb takes every one of them.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM duckdb_optimizers()"),
        vec![vec![Value::BigInt(44)]]
    );
    // The eight rudb has actually written are all in that list rather than names of its own, which
    // is what makes a corpus file written against DuckDB turn off the pass it meant.
    assert_eq!(
        rows(
            &db,
            "SELECT count(*) FROM duckdb_optimizers() WHERE name IN ('expression_rewriter', \
             'distinct_aggregate_rewrite', 'filter_pushdown', 'empty_result_pullup', \
             'unused_columns', 'limit_pushdown', 'top_n', 'late_materialization')"
        ),
        vec![vec![Value::BigInt(8)]]
    );
    // And the name the table gives is a name the setting takes.
    db.execute("SET disabled_optimizers = 'join_order,filter_pushdown'").expect("both names take");
    // Thirty one extensions on the pin and thirty one here. Two of them are true here where six are
    // true there, and the two are the two rudb has.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM duckdb_extensions()"),
        vec![vec![Value::BigInt(31)]]
    );
    assert_eq!(
        rows(&db, "SELECT extension_name FROM duckdb_extensions() WHERE loaded ORDER BY 1"),
        vec![vec![text("core_functions")], vec![text("parquet")]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT loaded, installed, install_path, install_mode, signature_key_fingerprint \
             FROM duckdb_extensions() WHERE extension_name = 'parquet'"
        ),
        vec![vec![
            Value::Boolean(true),
            Value::Boolean(true),
            text("(BUILT-IN)"),
            text("STATICALLY_LINKED"),
            Value::Null,
        ]]
    );
    // The three empty strings on a row for something that is not here are empty strings and not
    // nulls, which was measured, and the fingerprint is null on every row either way.
    assert_eq!(
        rows(
            &db,
            "SELECT loaded, installed, install_path, extension_version, install_mode, \
             installed_from FROM duckdb_extensions() WHERE extension_name = 'spatial'"
        ),
        vec![vec![
            Value::Boolean(false),
            Value::Boolean(false),
            text(""),
            text(""),
            text("NOT_INSTALLED"),
            text(""),
        ]]
    );
    // The aliases are the pin's, because they are a fact about the extension rather than about this
    // engine, and an extension with none carries an empty list rather than a null.
    let aliases = |values: Vec<&str>| Value::List {
        element: LogicalType::Varchar,
        values: values.into_iter().map(text).collect(),
    };
    assert_eq!(
        rows(
            &db,
            "SELECT aliases FROM duckdb_extensions() \
             WHERE extension_name IN ('httpfs', 'parquet') ORDER BY extension_name"
        ),
        vec![vec![aliases(vec!["http", "https", "s3"])], vec![aliases(Vec::new())]]
    );
    // Both tables are table functions, so both list themselves in the function table.
    assert_eq!(
        rows(
            &db,
            "SELECT count(*) FROM duckdb_functions() \
             WHERE function_name IN ('duckdb_extensions', 'duckdb_optimizers')"
        ),
        vec![vec![Value::BigInt(2)]]
    );
}

#[test]
fn the_engine_lists_its_parser_dialect_and_no_grammar_extensions() {
    let db = database();
    assert_eq!(rows(&db, "SELECT * FROM duckdb_dialects()"), vec![vec![text("duckdb")]]);
    assert!(rows(&db, "SELECT * FROM duckdb_grammar_extensions()").is_empty());
    assert_eq!(
        rows(
            &db,
            "SELECT column_name, column_type FROM (DESCRIBE SELECT * FROM duckdb_grammar_extensions())"
        ),
        vec![vec![text("name"), text("VARCHAR")], vec![text("description"), text("VARCHAR")]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT count(*) FROM duckdb_functions() WHERE function_name IN ('duckdb_dialects', 'duckdb_grammar_extensions')"
        ),
        vec![vec![Value::BigInt(2)]]
    );
}

/// A view over a star follows the table under it, and the column list follows with it.
#[test]
fn a_star_in_a_view_is_expanded_again_every_time_the_view_is_read() {
    let db = database();
    let text = |value: &str| Value::Varchar(value.to_string());
    db.execute("CREATE TABLE base(x INTEGER)").expect("a fresh table");
    db.execute("CREATE VIEW star AS SELECT * FROM base").expect("a fresh view");
    assert_eq!(
        rows(&db, "SELECT column_name FROM duckdb_columns() WHERE table_name = 'star'"),
        vec![vec![text("x")]]
    );
    // Reading the view rewrites the list. There is no ALTER TABLE yet, so this cannot yet be made
    // to report a different answer than it did before, and the point of it here is that a read does
    // not damage the list either.
    db.query("SELECT * FROM star").expect("the view reads");
    assert_eq!(
        rows(&db, "SELECT column_name FROM duckdb_columns() WHERE table_name = 'star'"),
        vec![vec![text("x")]]
    );
}

/// The four ways a subscript is refused, in DuckDB's words. Per #278.
#[test]
fn the_subscripts_that_are_refused_say_what_duckdb_says() {
    let db = database();
    let error = db.query("SELECT 'abcdef'[]").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Parser Error");
    assert_eq!(error.message(), "Empty subscript '[]' is not allowed");
    // A step on a string is not implemented upstream either, and the suggested rewrite is
    // upstream's, unbalanced parenthesis and all.
    let error = db.query("SELECT 'abcdef'[1:6:2]").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Not implemented Error");
    assert!(error.message().starts_with("Slice with steps has not been implemented"), "{error}");
    // A number is neither a list nor a string, and this is the one message that names the function
    // rather than listing what it would have taken.
    let error = db.query("SELECT array_slice(1, 2, 3)").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Binder Error");
    assert_eq!(error.message(), "ARRAY_SLICE can only operate on LISTs and VARCHARs");
    // An index is a whole number and is not cast to one, so a decimal is no call at all.
    assert!(failure(&db, "SELECT 'abcdef'[1.5]").starts_with("No function matches"));
}

/// A dollar quoted string is the text between the tags and nothing else. Per #276.
///
/// The comparison is the test worth having. The literal on its own looked plausible in the shell
/// output, and what the bug really did was make a dollar quoted string unequal to the same string
/// written the ordinary way, with nothing raising and nothing looking odd in the plan.
#[test]
fn a_dollar_quoted_string_is_the_text_between_the_tags() {
    let db = Database::new();
    assert_eq!(rows(&db, "SELECT $$dollar quoted$$"), vec![vec![text("dollar quoted")]]);
    assert_eq!(rows(&db, "SELECT $tag$body$tag$"), vec![vec![text("body")]]);
    assert_eq!(rows(&db, "SELECT $$a$$ = 'a'"), vec![vec![Value::Boolean(true)]]);
    // The name a column gets is rendered from the value, so it comes right with it.
    assert_eq!(db.query("SELECT $$a$$").unwrap().names(), &["'a'".to_string()]);
}

/// A string that will not read as the other type raises, rather than quietly comparing as text and
/// answering false.
#[test]
fn a_string_that_is_not_the_other_type_is_a_conversion_error() {
    let db = database();
    assert!(failure(&db, "SELECT 1 = 'abc'").contains("abc"));
    assert!(failure(&db, "SELECT CAST('2013-07-15' AS DATE) = 'nope'").contains("nope"));
}

#[test]
fn a_cast_that_cannot_hold_the_value_is_an_error_and_try_cast_is_null() {
    let db = database();
    assert!(failure(&db, "SELECT CAST('oops' AS INTEGER)").contains("oops"));
    assert_eq!(rows(&db, "SELECT TRY_CAST('oops' AS INTEGER)"), vec![vec![Value::Null]]);
}

/// What a failed cast says, per #322, which is a different sentence for each shape of failure and
/// names the types by the integer they are stored in rather than by the way they are written.
///
/// A pair DuckDB has no cast for is a conversion error and not a missing feature, which is why the
/// last line here answers null instead of raising: `TRY_CAST` swallows a conversion error.
#[test]
fn a_failed_cast_says_what_duckdb_says() {
    let db = database();
    assert_eq!(failure(&db, "SELECT 'abc'::TINYINT"), "Could not convert string 'abc' to INT8");
    assert_eq!(failure(&db, "SELECT '300'::TINYINT"), "Could not convert string '300' to INT8");
    assert_eq!(
        failure(&db, "SELECT 'abc'::DECIMAL(4,1)"),
        "Could not convert string \"abc\" to DECIMAL(4,1)"
    );
    assert_eq!(
        failure(&db, "SELECT 300::INTEGER::TINYINT"),
        "Type INT32 with value 300 can't be cast because the value is out of range for the \
         destination type INT8"
    );
    assert_eq!(
        failure(&db, "SELECT 999.9::DECIMAL(4,1)::TINYINT"),
        "Failed to cast decimal value 1000 to type INT8"
    );
    assert_eq!(
        failure(&db, "SELECT 200000::DECIMAL(4,1)"),
        "Could not cast value 200000 to DECIMAL(4,1)"
    );
    assert_eq!(
        failure(&db, "SELECT 200000.5::DECIMAL(7,1)::DECIMAL(4,1)"),
        "Casting value \"200000.5\" to type DECIMAL(4,1) failed: value is out of range!"
    );
    assert_eq!(
        failure(&db, "SELECT DATE '1970-01-01'::INTEGER"),
        "Unimplemented type for cast (DATE -> INTEGER)"
    );
    assert_eq!(rows(&db, "SELECT TRY_CAST(DATE '1970-01-01' AS INTEGER)"), vec![vec![Value::Null]]);
}

/// A written date that names a day that does not exist, per #322.
///
/// This one was a wrong answer and not only a wrong message: the thirty first of April used to
/// come back as the first of May. The time on the end of a date is thrown away but still has to be
/// a time, and the two sentences are the format one and the range one.
#[test]
fn a_written_day_that_does_not_exist_is_refused() {
    let db = database();
    assert_eq!(
        failure(&db, "SELECT '2021-04-31'::DATE"),
        "date field value out of range: \"2021-04-31\""
    );
    assert_eq!(
        failure(&db, "SELECT '2021-02-29 10:00:00'::TIMESTAMP"),
        "timestamp field value out of range: \"2021-02-29 10:00:00\""
    );
    assert_eq!(
        failure(&db, "SELECT 'yesterday'::DATE"),
        "invalid date field format: \"yesterday\", expected format is (YYYY-MM-DD)"
    );
    assert_eq!(
        rows(&db, "SELECT '2020-02-29 10:30:00'::DATE"),
        vec![vec![Value::Date(days_from_civil(2020, 2, 29))]]
    );
}

/// A TIME can be made now, per #228, which was a type you could declare a column of and never put
/// a value in.
///
/// The written form is read with a lot more slack than a date or a timestamp is, which is upstream
/// and not a decision here: the seconds are optional, a date in front is thrown away, and anything
/// after the numbers is ignored. The printing is the other half, since a TIME prints the fraction
/// it has and no more, so `12:34:56.100` comes back as `12:34:56.1`.
#[test]
fn a_time_can_be_written_and_read_back() {
    let db = database();
    let at = |hours: i64, minutes: i64, seconds: i64, micros: i64| {
        vec![vec![Value::Time(((hours * 60 + minutes) * 60 + seconds) * 1_000_000 + micros)]]
    };
    assert_eq!(rows(&db, "SELECT TIME '12:34:56'"), at(12, 34, 56, 0));
    assert_eq!(rows(&db, "SELECT CAST('12:34:56' AS TIME)"), at(12, 34, 56, 0));
    assert_eq!(rows(&db, "SELECT '12:34'::TIME"), at(12, 34, 0, 0));
    assert_eq!(rows(&db, "SELECT '12:34:56.1234567'::TIME"), at(12, 34, 56, 123_456));
    assert_eq!(rows(&db, "SELECT '2024-01-02 03:04:05'::TIME"), at(3, 4, 5, 0));
    assert_eq!(rows(&db, "SELECT '12:34:56 UTC'::TIME"), at(12, 34, 56, 0));
    assert_eq!(rows(&db, "SELECT TIMESTAMP '2024-01-02 03:04:05'::TIME"), at(3, 4, 5, 0));
    assert_eq!(rows(&db, "SELECT '12:34:56.100'::TIME::VARCHAR"), vec![vec![text("12:34:56.1")]]);
    assert_eq!(rows(&db, "SELECT typeof(TIME '12:34:56')"), vec![vec![text("TIME")]]);
    assert_eq!(
        failure(&db, "SELECT '25:00:00'::TIME"),
        "time field value out of range: \"25:00:00\", expected format is ([YYYY-MM-DD ]HH:MM:SS[.MS])"
    );
    assert_eq!(rows(&db, "SELECT TRY_CAST('25:00:00' AS TIME)"), vec![vec![Value::Null]]);
    assert_eq!(
        failure(&db, "SELECT DATE '2024-01-02'::TIME"),
        "Unimplemented type for cast (DATE -> TIME)"
    );
}

/// The third spelling of a cast, the one TPC-H q14 is written in.
///
/// The keyword is not a keyword the way `CAST` is, it is the type name, so the check that matters
/// is that the same list of types works here as works in the other two spellings. The name of the
/// column is the same too, because it is the same node: the pinned binary calls all three of them
/// `CAST('1995-09-01' AS DATE)`.
#[test]
fn a_type_in_front_of_a_string_is_a_cast_of_that_string() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT DATE '2013-07-15'"),
        vec![vec![Value::Date(days_from_civil(2013, 7, 15))]]
    );
    assert_eq!(
        rows(&db, "SELECT date '2013-07-15'"),
        vec![vec![Value::Date(days_from_civil(2013, 7, 15))]],
        "the type is a name and names are not case sensitive"
    );
    assert_eq!(rows(&db, "SELECT INTEGER '42' + 1"), vec![vec![Value::Integer(43)]]);
    assert_eq!(rows(&db, "SELECT VARCHAR 'hi'"), vec![vec![text("hi")]]);
    let result = db.query("SELECT DATE '1995-09-01'").unwrap();
    assert_eq!(result.names(), &["CAST('1995-09-01' AS DATE)"]);
    assert!(failure(&db, "SELECT DATE 'nope'").contains("nope"));
}

/// A prefix in front of a string picks a different decoding, per #329.
///
/// The prefix is not part of the value and never was, so the thing to check end to end is that the
/// value is the decoded one and the column name is the name the pinned binary gives it. The escapes
/// themselves are checked one at a time where they are decoded, in the parser.
#[test]
fn a_prefix_in_front_of_a_string_decides_how_the_string_is_read() {
    let db = database();
    assert_eq!(rows(&db, "SELECT E'a\\tb'"), vec![vec![text("a\tb")]]);
    assert_eq!(rows(&db, "SELECT e'a\\u00e9b'"), vec![vec![text("aéb")]]);
    assert_eq!(rows(&db, "SELECT N'abc'"), vec![vec![text("abc")]]);
    assert_eq!(rows(&db, "SELECT B'101'"), vec![vec![text("b101")]], "not a bit string upstream");
    assert_eq!(rows(&db, "SELECT length(E'a\\nb')"), vec![vec![Value::BigInt(3)]]);
    // An escape string is named after the value and not the spelling, so it ends up with the name a
    // plain string of the same value has. An N string is named as the cast it is.
    let result = db.query("SELECT E'ab', N'ab'").unwrap();
    assert_eq!(result.names(), &["'ab'", "CAST('ab' AS VARCHAR)"]);
}

/// A hex string is a blob, per #329, which is the prefix that changes the type and not the value.
#[test]
fn a_hex_string_answers_with_the_bytes_it_names() {
    let db = database();
    assert_eq!(rows(&db, "SELECT x'ff'"), vec![vec![Value::Blob(vec![0xff])]]);
    assert_eq!(rows(&db, "SELECT X'4142'"), vec![vec![Value::Blob(b"AB".to_vec())]]);
    assert_eq!(rows(&db, "SELECT x''"), vec![vec![Value::Blob(Vec::new())]]);
    // The same blob written the other way round, which is the cast this literal is built on.
    assert_eq!(rows(&db, "SELECT x'ff' = '\\xFF'::BLOB"), vec![vec![Value::Boolean(true)]]);
    let result = db.query("SELECT x'ff41'").unwrap();
    assert_eq!(result.names(), &["'\\xFFA'::BLOB"]);
    assert_eq!(result.types(), &[LogicalType::Blob]);
    // The digits are not looked at until the cast, so this is a conversion error and not a syntax
    // one, and it is the message the same cast written the other way round gives.
    assert!(failure(&db, "SELECT x'41zz'").contains("string -> blob conversion of string"));
}

#[test]
fn a_missing_table_names_the_table() {
    let db = database();
    assert!(failure(&db, "SELECT * FROM nope").contains("nope"));
}

#[test]
fn a_missing_column_names_the_column() {
    let db = database();
    assert!(failure(&db, "SELECT nope FROM t").contains("nope"));
}

#[test]
fn a_syntax_error_is_a_parser_error_rather_than_a_panic() {
    let db = database();
    let error = db.query("SELECT FROM WHERE").unwrap_err();
    assert_eq!(error.code(), rudb_common::ErrorCode::Parser);
}

/// The convenience path, and the check that it refuses anything that is not one cell.
#[test]
fn value_returns_one_cell_and_refuses_anything_else() {
    let db = database();
    assert_eq!(db.value("SELECT count(*) FROM t").unwrap(), Value::BigInt(4));
    assert!(db.value("SELECT * FROM t").is_err());
}

/// The plan text is the interface the optimizer tests are written against, so it is worth one test
/// here that a bound query prints in the form `rudb_plan` parses.
#[test]
fn a_plan_prints_parent_before_child() {
    let db = database();
    let text = db.plan("SELECT x FROM t WHERE x > 1").unwrap();
    let lines: Vec<&str> = text.lines().collect();
    assert!(lines[0].starts_with("Project"), "{text}");
    assert!(lines[1].trim_start().starts_with("Filter"), "{text}");
    assert!(lines[2].trim_start().starts_with("Get memory.main.t"), "{text}");
}

/// A predicate nothing can satisfy is answered without reading the table. The plan is the assertion
/// rather than the timing, because the whole point is that the scan is not in it, and with the pass
/// turned off the same query has to give the same answer the slow way.
#[test]
fn a_query_that_cannot_match_answers_nothing_without_reading_the_table() {
    let db = database();
    let text = db.plan("SELECT x FROM t WHERE false").unwrap();
    assert!(!text.contains("Get memory.main.t"), "{text}");
    assert_eq!(rows(&db, "SELECT x FROM t WHERE false"), Vec::<Vec<Value>>::new());
    assert_eq!(rows(&db, "SELECT x FROM t LIMIT 0"), Vec::<Vec<Value>>::new());
    db.execute("SET disabled_optimizers = 'empty_result_pullup'").unwrap();
    let text = db.plan("SELECT x FROM t WHERE false").unwrap();
    assert!(text.contains("Get memory.main.t"), "{text}");
    assert_eq!(rows(&db, "SELECT x FROM t WHERE false"), Vec::<Vec<Value>>::new());
    assert_eq!(rows(&db, "SELECT x FROM t LIMIT 0"), Vec::<Vec<Value>>::new());
}

/// The case the pullup has to stop at. An ungrouped aggregate over no rows produces one row, so
/// `count(*)` of nothing is zero and not an empty answer, and a `min` of nothing is null.
#[test]
fn an_aggregate_over_a_query_that_cannot_match_still_answers_its_row() {
    let db = database();
    assert_eq!(
        rows(&db, "SELECT count(*), min(x) FROM t WHERE false"),
        vec![vec![Value::BigInt(0), Value::Null]]
    );
    assert_eq!(
        rows(&db, "SELECT x, count(*) FROM t WHERE false GROUP BY x"),
        Vec::<Vec<Value>>::new()
    );
    db.execute("SET disabled_optimizers = 'empty_result_pullup'").unwrap();
    assert_eq!(
        rows(&db, "SELECT count(*), min(x) FROM t WHERE false"),
        vec![vec![Value::BigInt(0), Value::Null]]
    );
}

/// The other half of constant pruning. A predicate that is always true keeps every row, so the
/// filter is not rebuilt at all rather than left to be evaluated once per row to say so.
#[test]
fn a_predicate_that_is_always_true_leaves_no_filter_behind() {
    let db = database();
    let text = db.plan("SELECT x FROM t WHERE true").unwrap();
    assert!(!text.contains("Filter"), "{text}");
    let text = db.plan("SELECT x FROM t WHERE true AND x > 1").unwrap();
    assert!(text.contains("Filter (#0.0::INTEGER > 1::INTEGER)::BOOLEAN"), "{text}");
    assert_eq!(
        rows(&db, "SELECT x FROM t WHERE true AND x > 1"),
        vec![vec![integer(3)], vec![integer(2)]]
    );
}

/// A limit ends up under the projection above it, so the expressions are evaluated for the rows
/// that come out rather than for a whole chunk of rows that were going to be dropped. The rows are
/// the same either way, which is the half of this worth asserting, and the plan is the other half.
#[test]
fn a_limit_ends_up_under_the_projection_and_answers_the_same_rows() {
    let db = database();
    let text = db.plan("SELECT x + 1 AS y FROM t LIMIT 2").unwrap();
    let lines: Vec<&str> = text.lines().collect();
    assert!(lines[0].starts_with("Project"), "{text}");
    assert!(lines[1].trim_start().starts_with("Limit 2 offset 0"), "{text}");
    let wanted = vec![vec![integer(4)], vec![integer(2)]];
    assert_eq!(rows(&db, "SELECT x + 1 AS y FROM t LIMIT 2"), wanted);
    db.execute("SET disabled_optimizers = 'limit_pushdown'").unwrap();
    let text = db.plan("SELECT x + 1 AS y FROM t LIMIT 2").unwrap();
    assert!(text.lines().next().unwrap().starts_with("Limit 2 offset 0"), "{text}");
    assert_eq!(rows(&db, "SELECT x + 1 AS y FROM t LIMIT 2"), wanted);
}

/// The pass runs before top N, so an order by with a limit is still fused rather than left as a
/// sort with a limit that has wandered off above it.
#[test]
fn an_order_by_with_a_limit_is_still_one_operator_after_the_limit_has_moved() {
    let db = database();
    let printed = db.plan("SELECT s FROM t ORDER BY x DESC LIMIT 2").unwrap();
    assert!(printed.contains("TopN 2 offset 0"), "{printed}");
    assert_eq!(
        rows(&db, "SELECT s FROM t ORDER BY x DESC LIMIT 2"),
        vec![vec![text("a")], vec![text("c")]]
    );
}

#[test]
fn creating_a_table_twice_is_an_error_and_dropping_it_makes_room_again() {
    let db = Database::new();
    db.create_table("t", vec![Field::new("x", LogicalType::Integer)]).unwrap();
    assert!(db.create_table("t", vec![Field::new("x", LogicalType::Integer)]).is_err());
    db.drop_table("t").unwrap();
    db.create_table("t", vec![Field::new("x", LogicalType::Integer)]).unwrap();
}

/// More rows than one vector holds, so the result is more than one chunk and the row indexing has
/// to walk across the boundary.
#[test]
fn a_result_wider_than_one_vector_reads_across_chunks() {
    let db = Database::new();
    db.create_table("big", vec![Field::new("x", LogicalType::Integer)]).unwrap();
    // A vector and a half, worked out from the constant rather than written down, so that the row
    // count stays on the far side of the boundary whatever the boundary is. This test was 2,500
    // rows and stopped crossing anything the day the vector went past that.
    let total = VECTOR_SIZE + VECTOR_SIZE / 2;
    let mut rows = Vec::new();
    for x in 0..i32::try_from(total).unwrap() {
        rows.push(vec![Value::Integer(x)]);
    }
    db.append("big", &rows).unwrap();

    let first = i32::try_from(VECTOR_SIZE).unwrap();
    let last = i32::try_from(total).unwrap() - 1;
    let result = db.query("SELECT x FROM big").unwrap();
    assert_eq!(result.len(), total);
    assert!(result.chunks().len() > 1);
    assert_eq!(result.value_at(0, 0), integer(0));
    assert_eq!(result.value_at(VECTOR_SIZE, 0), integer(first));
    assert_eq!(result.value_at(total - 1, 0), integer(last));
    assert_eq!(result.row(total), None);
    assert_eq!(result.value_at(total, 0), Value::Null);
    assert_eq!(db.value("SELECT count(*) FROM big").unwrap(), Value::BigInt(total as i64));
}

#[test]
fn a_table_can_be_named_with_its_schema_and_its_catalog() {
    let db = database();
    assert_eq!(db.value("SELECT count(*) FROM main.t").unwrap(), Value::BigInt(4));
    assert_eq!(db.value("SELECT count(*) FROM memory.main.t").unwrap(), Value::BigInt(4));
    assert_eq!(db.table_len("main.t").unwrap(), 4);
}

// The statements that write. Everything above builds its tables through the Rust API, which is
// still the way a program embedded in something else does it. These build them through SQL, which
// is what the sqllogictest corpus needs and what a person at a prompt does.

/// A database built entirely out of SQL.
fn scripted(statements: &[&str]) -> Database {
    let db = Database::new();
    for statement in statements {
        db.execute(statement).unwrap_or_else(|error| panic!("{statement}: {error}"));
    }
    db
}

/// The message a statement fails with.
fn refusal(db: &Database, sql: &str) -> String {
    db.execute(sql).unwrap_err().message().to_string()
}

#[test]
fn a_table_can_be_created_filled_and_read_without_leaving_sql() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER, b VARCHAR)",
        "INSERT INTO t VALUES (1, 'one'), (2, 'two')",
    ]);
    assert_eq!(
        rows(&db, "SELECT a, b FROM t"),
        vec![vec![integer(1), text("one")], vec![integer(2), text("two")],]
    );
}

#[test]
fn a_value_is_cast_to_the_column_it_lands_in() {
    // The literal is a `TINYINT` the way it is written and the column is a `BIGINT`, and the cast
    // that reconciles them is in the plan rather than in the append, so the column holds one type.
    let db = scripted(&["CREATE TABLE t (a BIGINT, b DOUBLE)", "INSERT INTO t VALUES (1, 2)"]);
    assert_eq!(rows(&db, "SELECT a, b FROM t"), vec![vec![Value::BigInt(1), Value::Double(2.0)]]);
}

#[test]
fn a_column_the_insert_did_not_name_is_null() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER, b VARCHAR, c INTEGER)",
        "INSERT INTO t (c, a) VALUES (30, 10)",
    ]);
    // The list also says the order, so the thirty is in `c` and the ten is in `a`.
    assert_eq!(
        rows(&db, "SELECT a, b, c FROM t"),
        vec![vec![integer(10), Value::Null, integer(30)]]
    );
}

#[test]
fn an_insert_that_reads_its_own_target_sees_the_rows_that_were_there_when_it_started() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "INSERT INTO t VALUES (1), (2)",
        "INSERT INTO t SELECT a + 10 FROM t",
    ]);
    assert_eq!(
        rows(&db, "SELECT a FROM t"),
        vec![vec![integer(1)], vec![integer(2)], vec![integer(11)], vec![integer(12)],]
    );
}

#[test]
fn create_table_as_takes_its_types_from_the_query() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "INSERT INTO t VALUES (1), (2), (3)",
        "CREATE TABLE counted AS SELECT count(*) AS n, sum(a) AS total FROM t",
    ]);
    assert_eq!(
        db.query("SELECT n, total FROM counted").unwrap().types(),
        &[LogicalType::BigInt, LogicalType::HugeInt]
    );
    assert_eq!(
        rows(&db, "SELECT n, total FROM counted"),
        vec![vec![Value::BigInt(3), Value::HugeInt(6)]]
    );
}

#[test]
fn a_create_table_as_can_rename_the_query_s_columns() {
    let db = scripted(&["CREATE TABLE t (x, y) AS SELECT 1, 'a'"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["x", "y"]);
}

#[test]
fn a_short_column_list_renames_the_front_and_leaves_the_rest_to_the_query() {
    // Not an error. duckdb v1.4.1 makes a table of `a` and `2` here, where the second name is what
    // the query calls that column, which for a bare literal is the literal.
    let db = scripted(&["CREATE TABLE t (a) AS SELECT 1, 2"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a", "2"]);
}

#[test]
fn a_column_list_longer_than_the_query_is_the_error_duckdb_writes_for_it() {
    let db = Database::new();
    assert_eq!(
        refusal(&db, "CREATE TABLE t (a, b, c) AS SELECT 1, 2"),
        "Target table has more colum names than query result."
    );
}

#[test]
fn a_query_that_names_two_columns_the_same_gets_them_renamed_apart() {
    // A query may produce two columns of one name and `SELECT 1 AS a, 2 AS a` prints both, so
    // turning one into a table has to decide, and DuckDB renames rather than refusing.
    let db = scripted(&["CREATE TABLE t AS SELECT 1 AS a, 2 AS a, 3 AS a"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a", "a_1", "a_2"]);
}

#[test]
fn a_renamed_column_steps_past_a_name_the_query_already_used() {
    let db = scripted(&["CREATE TABLE t AS SELECT 1 AS a, 2 AS a, 3 AS a_1"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a", "a_1", "a_1_1"]);
    let db = scripted(&["CREATE TABLE t AS SELECT 1 AS a_1, 2 AS a, 3 AS a"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a_1", "a", "a_2"]);
}

#[test]
fn the_renaming_is_case_insensitive_and_keeps_the_case_it_was_written_in() {
    let db = scripted(&["CREATE TABLE t AS SELECT 1 AS a, 2 AS A"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a", "A_1"]);
}

#[test]
fn a_column_list_turns_the_renaming_off_and_a_repeat_becomes_an_error() {
    // Which is the rule duckdb v1.4.1 follows: with a list, even a short one, the names are the
    // ones written or the ones the query gave, and two the same is a refusal.
    let db = scripted(&["CREATE TABLE t (z) AS SELECT 1 AS a, 2 AS a"]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["z", "a"]);
    let db = Database::new();
    assert_eq!(
        refusal(&db, "CREATE TABLE t (z) AS SELECT 1 AS a, 2 AS a, 3 AS a"),
        "Column with name a already exists!"
    );
    let db = Database::new();
    assert_eq!(
        refusal(&db, "CREATE TABLE t (a, a) AS SELECT 1, 2"),
        "Column with name a already exists!"
    );
}

#[test]
fn two_columns_of_one_name_in_a_plain_create_is_the_same_error() {
    let db = Database::new();
    assert_eq!(
        refusal(&db, "CREATE TABLE t (Abc INTEGER, aBC VARCHAR)"),
        "Column with name aBC already exists!"
    );
}

#[test]
fn if_not_exists_leaves_the_table_and_its_rows_alone() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "INSERT INTO t VALUES (1)",
        "CREATE TABLE IF NOT EXISTS t (b VARCHAR, c VARCHAR)",
    ]);
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a"]);
    assert_eq!(db.table_len("t").unwrap(), 1);
    // Without it, the second create is an error and the table is still the first one.
    assert!(refusal(&db, "CREATE TABLE t (b VARCHAR)").contains("already exists"));
    assert_eq!(db.query("SELECT * FROM t").unwrap().names(), &["a"]);
}

#[test]
fn or_replace_runs_the_query_against_the_table_it_is_about_to_replace() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "INSERT INTO t VALUES (1), (2), (3)",
        "CREATE OR REPLACE TABLE t AS SELECT a * 2 AS a FROM t",
    ]);
    assert_eq!(
        rows(&db, "SELECT a FROM t"),
        vec![vec![integer(2)], vec![integer(4)], vec![integer(6)],]
    );
}

#[test]
fn dropping_takes_a_list_and_if_exists_forgives_a_name_that_is_not_there() {
    let db = scripted(&[
        "CREATE TABLE a (x INTEGER)",
        "CREATE TABLE b (x INTEGER)",
        "DROP TABLE a, b",
        "DROP TABLE IF EXISTS a",
    ]);
    assert!(db.with_catalog(|catalog| catalog.tables().next().is_none()));
    assert!(refusal(&db, "DROP TABLE a").contains("does not exist"));
}

#[test]
fn values_is_a_query_and_the_columns_take_the_type_every_row_agrees_on() {
    let db = Database::new();
    let result = db.query("VALUES (1, 'a'), (2.5, 'b')").unwrap();
    assert_eq!(result.names(), &["col0", "col1"]);
    // The integer column widens to hold the integer's digits as well as the fraction, which is
    // `promote_numeric`'s rule and the reason it is eleven wide and not two.
    assert_eq!(
        result.types(),
        &[LogicalType::Decimal { width: 11, scale: 1 }, LogicalType::Varchar]
    );
    assert_eq!(result.len(), 2);
    assert_eq!(
        rows(&db, "SELECT col0 FROM (VALUES (3), (1), (2)) ORDER BY col0"),
        vec![vec![integer(1)], vec![integer(2)], vec![integer(3)]]
    );
}

/// `1.5 * 1.5` is 2.25, which needs the two decimal places a product of two of them has.
///
/// It used to answer 2.2 typed `DECIMAL(2,1)`, because the result kept what the operands promote
/// to and the last digit of the answer had nowhere to go. Per #243. The second query is the same
/// rule through the kernel rather than a value at a time, since a column of them takes the run at a
/// time path and that one multiplies unscaled values at their own scales.
#[test]
fn multiplying_two_decimals_keeps_the_digits_of_both_of_them() {
    let db = Database::new();
    let sql = "SELECT 1.5 * 1.5 AS p";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::Decimal { width: 4, scale: 2 }]);
    assert_eq!(rows(&db, sql), vec![vec![Value::Decimal { unscaled: 225, width: 4, scale: 2 }]]);
    let run = "SELECT a * 1.5 AS p FROM range(3) t(a)";
    assert_eq!(db.query(run).unwrap().types(), &[LogicalType::Decimal { width: 21, scale: 1 }]);
    let decimal = |unscaled| vec![Value::Decimal { unscaled, width: 21, scale: 1 }];
    assert_eq!(rows(&db, run), vec![decimal(0), decimal(15), decimal(30)]);
}

/// `//` is integer division only when there are integers on both sides of it.
///
/// Measured on the pinned binary: `7.5 // 2.5` is the DOUBLE 3.0, `7.5 // 2` is 3.75 and
/// `7.9 // 1.0` is 7.9, so once a side is not an integer it is `/` under another spelling and it
/// neither keeps the decimal type nor truncates the answer.
#[test]
fn integer_division_with_a_decimal_in_it_is_a_double() {
    let db = Database::new();
    let sql = "SELECT 7.5 // 2.5 AS q";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::Double]);
    assert_eq!(rows(&db, sql), vec![vec![Value::Double(3.0)]]);
    let sql = "SELECT 7.5 // 2 AS q";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::Double]);
    assert_eq!(rows(&db, sql), vec![vec![Value::Double(3.75)]]);
    let sql = "SELECT 7.9 // 1.0 AS q";
    assert_eq!(rows(&db, sql), vec![vec![Value::Double(7.9)]]);
    let sql = "SELECT 7 // 2 AS q";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::Integer]);
    assert_eq!(rows(&db, sql), vec![vec![Value::Integer(3)]]);
    // A run rather than one value, so the vectorized loop answers the same as the folded constant.
    let run = "SELECT a // 2.0 AS q FROM range(4) t(a)";
    assert_eq!(db.query(run).unwrap().types(), &[LogicalType::Double]);
    let double = |value| vec![Value::Double(value)];
    assert_eq!(rows(&db, run), vec![double(0.0), double(0.5), double(1.0), double(1.5)]);
}

/// Every overflow sentence, word for word and mark for mark against the pinned binary. Per #257.
///
/// The type is the integer the value is stored in rather than the type it was written as, a decimal
/// prints its operands with the point taken out, an integer ends on `!` and a decimal on `;`, a
/// decimal subtraction is called `subtract`, and a decimal multiplication ends on advice about how
/// to get out of the overflow instead of on punctuation.
#[test]
fn an_overflow_says_what_duckdb_says() {
    let db = Database::new();
    let cases = [
        ("127::TINYINT + 1::TINYINT", "Overflow in addition of INT8 (127 + 1)!"),
        ("(-128)::TINYINT - 1::TINYINT", "Overflow in subtraction of INT8 (-128 - 1)!"),
        ("127::TINYINT * 2::TINYINT", "Overflow in multiplication of INT8 (127 * 2)!"),
        ("32767::SMALLINT + 1::SMALLINT", "Overflow in addition of INT16 (32767 + 1)!"),
        ("2147483647::INTEGER + 1::INTEGER", "Overflow in addition of INT32 (2147483647 + 1)!"),
        (
            "9223372036854775807::BIGINT + 1::BIGINT",
            "Overflow in addition of INT64 (9223372036854775807 + 1)!",
        ),
        ("255::UTINYINT + 1::UTINYINT", "Overflow in addition of UINT8 (255 + 1)!"),
        ("65535::USMALLINT + 1::USMALLINT", "Overflow in addition of UINT16 (65535 + 1)!"),
        ("4294967295::UINTEGER + 1::UINTEGER", "Overflow in addition of UINT32 (4294967295 + 1)!"),
        (
            "18446744073709551615::UBIGINT + 1::UBIGINT",
            "Overflow in addition of UINT64 (18446744073709551615 + 1)!",
        ),
        (
            "170141183460469231731687303715884105727::HUGEINT + 1::HUGEINT",
            "Overflow in addition of INT128 (170141183460469231731687303715884105727 + 1)!",
        ),
        (
            "99999999999999999999999999999999999999::DECIMAL(38,0) + 1::DECIMAL(38,0)",
            "Overflow in addition of DECIMAL(38) (99999999999999999999999999999999999999 + 1);",
        ),
        (
            "(-99999999999999999999999999999999999999)::DECIMAL(38,0) - 1::DECIMAL(38,0)",
            "Overflow in subtract of DECIMAL(38) (-99999999999999999999999999999999999999 - 1);",
        ),
        (
            "9999999999.99::DECIMAL(38,2) * 9999999999999999999999999999.99::DECIMAL(38,2)",
            concat!(
                "Overflow in multiplication of DECIMAL(38) ",
                "(999999999999 * 999999999999999999999999999999). ",
                "You might want to add an explicit cast to a decimal with a smaller scale.",
            ),
        ),
        (
            "9999999999.9999::DECIMAL(18,4) * 99999999.9999::DECIMAL(18,4)",
            concat!(
                "Overflow in multiplication of DECIMAL(18) (99999999999999 * 999999999999). ",
                "You might want to add an explicit cast to a bigger decimal.",
            ),
        ),
        ("abs((-2147483648)::INTEGER)", "Overflow on abs(-2147483648)"),
        ("abs((-32768)::SMALLINT)", "Overflow on abs(-32768)"),
        ("abs((-9223372036854775808)::BIGINT)", "Overflow on abs(-9223372036854775808)"),
        // Negation names neither the type nor the value, per #264. A HUGEINT is the only width that
        // reaches it as a constant, because the folder widens the other four instead.
        (
            "-((-170141183460469231731687303715884105728)::HUGEINT)",
            "Overflow in negation of numeric value!",
        ),
    ];
    for (expression, expected) in cases {
        assert_eq!(failure(&db, &format!("SELECT {expression}")), expected, "{expression}");
    }
    // The vectorized loop, which is a second copy of each of these messages and has to say the same
    // thing. One operand comes out of a column so that the constant folder leaves the row alone.
    let one = "FROM range(1, 2) t(a)";
    let sql = format!("SELECT 127::TINYINT + a::TINYINT {one}");
    assert_eq!(failure(&db, &sql), "Overflow in addition of INT8 (127 + 1)!");
    let big = "99999999999999999999999999999999999999::DECIMAL(38,0)";
    let sql = format!("SELECT {big} + a::DECIMAL(38,0) {one}");
    let expected =
        "Overflow in addition of DECIMAL(38) (99999999999999999999999999999999999999 + 1);";
    assert_eq!(failure(&db, &sql), expected);
    let sql = "SELECT abs(a::INTEGER) FROM range(-2147483648, -2147483647) t(a)";
    assert_eq!(failure(&db, sql), "Overflow on abs(-2147483648)");
    let sql = "SELECT -(a::INTEGER) FROM range(-2147483648, -2147483647) t(a)";
    assert_eq!(failure(&db, sql), "Overflow in negation of numeric value!");
}

/// Negating the smallest value of a signed type widens by a step instead of raising. Per #264.
///
/// The type of the answer depends on the value, which is why it is the constant folder that does it
/// and why a column keeps the type it has: nothing knows what is in the column until the loop is
/// running, and upstream keeps the type there too. Measured on the pinned binary, where
/// `typeof(-((-128)::TINYINT))` is SMALLINT and `typeof(-((-127)::TINYINT))` is TINYINT.
#[test]
fn negating_the_smallest_value_of_a_type_widens_by_one_step() {
    let db = Database::new();
    let widened = [
        ("(-128)::TINYINT", LogicalType::SmallInt, Value::SmallInt(128)),
        ("(-32768)::SMALLINT", LogicalType::Integer, Value::Integer(32768)),
        ("(-2147483648)::INTEGER", LogicalType::BigInt, Value::BigInt(2147483648)),
        (
            "(-9223372036854775808)::BIGINT",
            LogicalType::HugeInt,
            Value::HugeInt(9223372036854775808),
        ),
    ];
    for (argument, ty, answer) in widened {
        let sql = format!("SELECT -({argument}) AS n");
        assert_eq!(db.query(&sql).unwrap().types(), &[ty], "{argument}");
        assert_eq!(rows(&db, &sql), vec![vec![answer]], "{argument}");
    }
    // Every other value keeps the type it was written with, so the widening is about the one value
    // in each type that has no negative and not about the type.
    let sql = "SELECT -((-127)::TINYINT) AS n";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::TinyInt]);
    assert_eq!(rows(&db, sql), vec![vec![Value::TinyInt(127)]]);
    // A column keeps its type as well, and the row that has no negative in it raises instead.
    let sql = "SELECT -(a::INTEGER) AS n FROM range(-2147483647, -2147483646) t(a)";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::Integer]);
    assert_eq!(rows(&db, sql), vec![vec![Value::Integer(2147483647)]]);
}

/// Something written where a number goes that is not one, and the three answers upstream has. Per
/// #277.
///
/// `SELECT 1e` is a refusal on the pinned binary as well, which is worth writing down because it
/// looks like it should be `1` aliased `e`. It is that one byte later. The tokenizer gives the
/// exponent marker back when there is anything at all behind it to give it back into, and at the end
/// of the input there is not. So what was wrong here was the class and the words rather than the
/// refusal, and all four of these were read off the binary.
#[test]
fn a_number_that_is_not_a_number_says_what_duckdb_says() {
    let db = Database::new();
    let cases = [
        ("SELECT 1e", "Invalid Input Error", "Could not convert string '1e' to DOUBLE"),
        ("SELECT 1e-", "Invalid Input Error", "Could not convert string '1e-' to DOUBLE"),
        ("SELECT 1e2e", "Parser Error", "Already found scientific notation"),
        (
            "SELECT 1.2.3",
            "Invalid Input Error",
            "Failed to cast value: Could not convert string \"1.2.3\" to DECIMAL(4,1)",
        ),
    ];
    for (sql, class, message) in cases {
        let error = db.query(sql).unwrap_err();
        assert_eq!(error.code().duckdb_name(), class, "{sql}");
        assert_eq!(error.message(), message, "{sql}");
    }
}

/// An underscore between two digits is a separator and not part of the number. Per #277.
///
/// The tokenizer has taken these as one number token since it was written, and the binder then
/// refused every one of them, so nothing could be written with a separator in it at all. The type
/// counts digits, so the separator has to be gone before the counting rather than after it.
#[test]
fn an_underscore_in_a_number_is_a_separator() {
    let db = Database::new();
    assert_eq!(rows(&db, "SELECT 1_000"), vec![vec![integer(1000)]]);
    assert_eq!(rows(&db, "SELECT 1_000_000"), vec![vec![integer(1_000_000)]]);
    assert_eq!(rows(&db, "SELECT 1e1_0"), vec![vec![Value::Double(1e10)]]);
    let sql = "SELECT 1_0.5_0";
    assert_eq!(db.query(sql).unwrap().types(), &[LogicalType::Decimal { width: 4, scale: 2 }]);
    assert_eq!(rows(&db, sql), vec![vec![Value::Decimal { unscaled: 1050, width: 4, scale: 2 }]]);
}

/// A zero divisor is three different things, one per operator. Per #262.
///
/// `/` never raises, because the binder has already promoted both sides to DOUBLE and IEEE
/// arithmetic answers an infinity or a nan. `//` always raises, whatever it was given. `%` raises on
/// integers and on decimals and answers a nan on floats. All three were measured on the pinned
/// binary, and none of them is null, which is what this used to answer for all of them.
#[test]
fn dividing_by_zero_raises_on_two_of_the_three_operators() {
    let db = Database::new();
    let double = |value| vec![vec![Value::Double(value)]];
    assert_eq!(rows(&db, "SELECT 7 / 0"), double(f64::INFINITY));
    assert_eq!(rows(&db, "SELECT (-7) / 0"), double(f64::NEG_INFINITY));
    assert!(matches!(rows(&db, "SELECT 0 / 0")[0][0], Value::Double(answer) if answer.is_nan()));
    assert!(matches!(rows(&db, "SELECT 7.5::DOUBLE % 0.0::DOUBLE")[0][0],
            Value::Double(answer) if answer.is_nan()));
    // The vectorized loop, which reaches the zero at the third row rather than at the first.
    let run = rows(&db, "SELECT 10 / a FROM range(-1, 2) t(a)");
    assert_eq!(run[0], vec![Value::Double(-10.0)]);
    assert!(matches!(run[1][0], Value::Double(answer) if answer.is_infinite()));
    assert_eq!(run[2], vec![Value::Double(10.0)]);
}

/// The division by zero sentence, word for word against the pinned binary. Per #262.
///
/// It quotes the expression rather than the two values, and the expression it quotes is the bound
/// one: the casts the binder put in are in the text, a column is the name its table gives it, and a
/// literal is the value it was folded to.
#[test]
fn dividing_by_zero_says_what_duckdb_says() {
    let db = Database::new();
    db.create_table("z", vec![Field::new("a", LogicalType::Integer)]).unwrap();
    db.append("z", &[vec![Value::Integer(1)], vec![Value::Integer(-2)]]).unwrap();
    let advice = "Use TRY(...) to return NULL for this expression, or SET \
                  null_on_division_by_zero=true to return NULL for all divisions by zero.";
    let cases = [
        ("SELECT 7 // 0", "(7 // 0)"),
        ("SELECT 7 % 0", "(7 % 0)"),
        ("SELECT 7.50 % 0.00", "(7.50 % 0.00)"),
        ("SELECT 7.0::DOUBLE // 0.0::DOUBLE", "(7.0 // 0.0)"),
        // A column, so the constant folder leaves the row alone and the vectorized loop is what
        // raises. Everything below this line goes through that loop.
        ("SELECT a // 0 FROM z", "(a // 0)"),
        ("SELECT a % 0 FROM z", "(a % 0)"),
        ("SELECT a::DOUBLE // 0.0 FROM z", "(CAST(a AS DOUBLE) // 0.0)"),
        ("SELECT (a + 1) // 0 FROM z", "((a + 1) // 0)"),
        ("SELECT abs(a) % 0 FROM z", "(abs(a) % 0)"),
        // Unary minus brackets its operand where a binary operator does not, which is measured and
        // is what DuckDB does for every operator that is not binary.
        ("SELECT -a // 0 FROM z", "(-(a) // 0)"),
        ("SELECT 10 // (a - a) FROM z", "(10 // (a - a))"),
        ("SELECT 10.5 % (a - a) FROM z", "(10.5 % CAST((a - a) AS DECIMAL(11,1)))"),
    ];
    for (sql, quoted) in cases {
        let expected = format!("Division by zero in expression {quoted}. {advice}");
        assert_eq!(failure(&db, sql), expected, "{sql}");
    }
    let error = db.query("SELECT a // 0 FROM z").expect_err("integer division by zero raises");
    assert_eq!(error.span(), Some(Span::new(7, 13)));
}

/// The sum of the two largest `DECIMAL(18,0)` values, which does not fit in a `DECIMAL(18,0)`.
///
/// It used to raise `Out of Range Error: Overflow in addition`, because the result kept the
/// operands' own type and two eighteen digit numbers add to nineteen digits. DuckDB answers
/// 1999999999999999998 typed `DECIMAL(19,0)` and now so does this. Per #243.
#[test]
fn adding_two_decimals_widens_the_answer_by_the_digit_the_carry_needs() {
    let db = Database::new();
    let sql = "SELECT 999999999999999999::DECIMAL(18,0) + 999999999999999999::DECIMAL(18,0) AS s";
    let result = db.query(sql).unwrap();
    assert_eq!(result.types(), &[LogicalType::Decimal { width: 19, scale: 0 }]);
    assert_eq!(
        rows(&db, sql),
        vec![vec![Value::Decimal { unscaled: 1_999_999_999_999_999_998, width: 19, scale: 0 }]]
    );
    // The mixed case, where nothing overflows and the type was one digit narrower than upstream.
    assert_eq!(
        db.query("SELECT 2.0 + 1::INTEGER").unwrap().types(),
        &[LogicalType::Decimal { width: 12, scale: 1 }]
    );
}

#[test]
fn a_statement_that_writes_something_the_answer_would_depend_on_is_refused() {
    // Each of these parses and each of them would be a wrong answer if it were accepted and the
    // clause ignored, which is the rule the front end follows everywhere else.
    let db = scripted(&["CREATE TABLE t (a INTEGER)"]);
    for statement in [
        "CREATE TABLE u (a INTEGER PRIMARY KEY)",
        "INSERT INTO t VALUES (1) RETURNING a",
        "INSERT INTO t (a, a) VALUES (1, 2)",
        "CREATE TABLE u (a INTEGER, a VARCHAR)",
    ] {
        let message = refusal(&db, statement);
        assert!(!message.is_empty(), "{statement} was accepted");
    }
    assert!(db.with_catalog(|catalog| catalog.tables().all(|table| table.name().table != "u")));
}

/// A temporary table goes in the `temp` database and a bare name finds it before the stored one.
///
/// Two tables of one name exist at the same time here, which is the whole of what `temp` is for.
/// The `CREATE` writes into `memory` because a create that did not say otherwise always does, and
/// every read after it goes to `temp` because that is the front of the search path, so the two
/// directions genuinely disagree and the pin disagrees the same way.
#[test]
fn a_temporary_table_shadows_a_stored_one_of_the_same_name() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "INSERT INTO t VALUES (99)",
        "CREATE TEMPORARY TABLE t (a INTEGER)",
        "INSERT INTO t VALUES (1)",
    ]);
    assert_eq!(rows(&db, "SELECT a FROM t"), vec![vec![integer(1)]]);
    assert_eq!(rows(&db, "SELECT a FROM temp.t"), vec![vec![integer(1)]]);
    assert_eq!(rows(&db, "SELECT a FROM temp.main.t"), vec![vec![integer(1)]]);
    assert_eq!(rows(&db, "SELECT a FROM memory.main.t"), vec![vec![integer(99)]]);
    // Two parts reading as a schema and a table, and `main` is a schema of `temp` before it is a
    // schema of `memory`, so this one is the temporary table as well.
    assert_eq!(rows(&db, "SELECT a FROM main.t"), vec![vec![integer(1)]]);
}

/// The one that is dropped is the one a bare name finds, so dropping twice leaves neither.
#[test]
fn a_drop_takes_the_temporary_table_first_and_the_stored_one_after() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "CREATE TEMPORARY TABLE t (a INTEGER)",
        "DROP TABLE t",
    ]);
    assert_eq!(
        rows(&db, "SELECT database_name FROM duckdb_tables() WHERE table_name = 't'"),
        vec![vec![text("memory")]]
    );
    db.execute("DROP TABLE t").expect("the stored one is still there");
    assert_eq!(
        rows(&db, "SELECT database_name FROM duckdb_tables() WHERE table_name = 't'"),
        Vec::<Vec<Value>>::new()
    );
}

/// Everything temporary lands in `temp` whatever way the name was written, and a name that says a
/// different database is refused in the pin's own words.
#[test]
fn a_temporary_name_can_only_say_the_temp_database() {
    let db = scripted(&["CREATE TABLE stored (a INTEGER)"]);
    for statement in [
        "CREATE TEMPORARY TABLE one (a INTEGER)",
        "CREATE TEMPORARY TABLE temp.two (a INTEGER)",
        "CREATE TEMPORARY TABLE main.three (a INTEGER)",
        "CREATE TEMPORARY TABLE temp.main.four (a INTEGER)",
        "CREATE TEMPORARY VIEW five AS SELECT 1 AS a",
        "CREATE TEMPORARY VIEW main.six AS SELECT 1 AS a",
    ] {
        db.execute(statement).unwrap_or_else(|error| panic!("{statement}: {error}"));
    }
    assert_eq!(
        rows(&db, "SELECT count(*) FROM duckdb_tables() WHERE database_name = 'temp'"),
        vec![vec![Value::BigInt(4)]]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FROM duckdb_views() WHERE database_name = 'temp'"),
        vec![vec![Value::BigInt(2)]]
    );
    let outside = "TEMPORARY table names can *only* use the \"temp\" catalog";
    for statement in [
        "CREATE TEMPORARY TABLE memory.seven (a INTEGER)",
        "CREATE TEMPORARY TABLE memory.main.seven (a INTEGER)",
        "CREATE TEMPORARY TABLE system.main.seven (a INTEGER)",
        "CREATE TEMPORARY VIEW memory.seven AS SELECT 1 AS a",
    ] {
        assert_eq!(refusal(&db, statement), outside, "{statement}");
    }
    // A schema in `temp` other than `main` cannot exist, because `CREATE SCHEMA` cannot put one
    // there, so this reports the schema missing rather than reaching for the `memory` one.
    assert_eq!(
        refusal(&db, "CREATE TEMPORARY TABLE nowhere.eight (a INTEGER)"),
        "Schema with name nowhere does not exist!"
    );
}

/// A create that is not temporary still cannot name `temp`, which is the other half of the rule and
/// a different sentence.
#[test]
fn a_create_that_is_not_temporary_cannot_name_the_temp_database() {
    let db = scripted(&[]);
    let inside = "Only TEMPORARY table names can use the \"temp\" catalog";
    assert_eq!(refusal(&db, "CREATE TABLE temp.a (i INTEGER)"), inside);
    assert_eq!(refusal(&db, "CREATE VIEW temp.b AS SELECT 1"), inside);
}

/// The catalog tables report a temporary entry as temporary and as not internal.
///
/// Those two are the same answer everywhere else and they come apart here: the `temp` database is
/// the engine's, and the table in it is somebody's. Measured off the pin, which prints exactly this
/// pair for a temporary table, a temporary view and the columns of both.
#[test]
fn a_temporary_entry_is_temporary_and_is_not_internal() {
    let db = scripted(&[
        "CREATE TABLE stored (a INTEGER)",
        "CREATE TEMPORARY TABLE tt (b INTEGER)",
        "CREATE TEMPORARY VIEW tv AS SELECT 1 AS c",
    ]);
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, internal, temporary FROM duckdb_tables() ORDER BY table_name"
        ),
        vec![
            vec![text("stored"), Value::Boolean(false), Value::Boolean(false)],
            vec![text("tt"), Value::Boolean(false), Value::Boolean(true)],
        ]
    );
    assert_eq!(
        rows(&db, "SELECT internal, temporary FROM duckdb_views() WHERE view_name = 'tv'"),
        vec![vec![Value::Boolean(false), Value::Boolean(true)]]
    );
    assert_eq!(
        rows(
            &db,
            "SELECT table_name, internal FROM duckdb_columns() \
             WHERE table_name IN ('tt', 'tv') ORDER BY table_name"
        ),
        vec![vec![text("tt"), Value::Boolean(false)], vec![text("tv"), Value::Boolean(false)],]
    );
    // The database holding them is internal all the same, and so is its schema.
    assert_eq!(
        rows(&db, "SELECT internal FROM duckdb_databases() WHERE database_name = 'temp'"),
        vec![vec![Value::Boolean(true)]]
    );
    assert_eq!(
        rows(&db, "SELECT internal FROM duckdb_schemas() WHERE database_name = 'temp'"),
        vec![vec![Value::Boolean(true)]]
    );
}

/// A temporary table is a `LOCAL TEMPORARY` in `information_schema`, and a temporary view is just a
/// view, which is the pin's answer to both.
#[test]
fn information_schema_calls_a_temporary_table_a_local_temporary() {
    let db = scripted(&[
        "CREATE TABLE stored (a INTEGER)",
        "CREATE TEMPORARY TABLE tt (b INTEGER)",
        "CREATE TEMPORARY VIEW tv AS SELECT 1 AS c",
    ]);
    assert_eq!(
        rows(
            &db,
            "SELECT table_catalog, table_name, table_type FROM information_schema.tables \
             ORDER BY table_name"
        ),
        vec![
            vec![text("memory"), text("stored"), text("BASE TABLE")],
            vec![text("temp"), text("tt"), text("LOCAL TEMPORARY")],
            vec![text("temp"), text("tv"), text("VIEW")],
        ]
    );
}

/// The two listings that say what is around report a temporary entry, even though the listing of
/// databases leaves `temp` out.
///
/// Both are the pin's answer and the difference between them is the question being asked. One asks
/// which databases a name can be written into, where `temp` is not one, and the others ask what is
/// there to read, where it is.
#[test]
fn the_listings_show_a_temporary_entry_beside_a_stored_one() {
    let db = scripted(&[
        "CREATE TABLE stored (a INTEGER)",
        "CREATE TEMPORARY TABLE tt (b INTEGER)",
        "CREATE TEMPORARY VIEW tv AS SELECT 1 AS c",
    ]);
    assert_eq!(
        rows(&db, "PRAGMA show_tables"),
        vec![vec![text("stored")], vec![text("tt")], vec![text("tv")],]
    );
    assert_eq!(
        rows(&db, "PRAGMA show_tables_expanded")
            .into_iter()
            .map(|row| (row[0].clone(), row[2].clone(), row[5].clone()))
            .collect::<Vec<_>>(),
        vec![
            (text("memory"), text("stored"), Value::Boolean(false)),
            (text("temp"), text("tt"), Value::Boolean(true)),
            (text("temp"), text("tv"), Value::Boolean(true)),
        ]
    );
    assert_eq!(rows(&db, "PRAGMA show_databases"), vec![vec![text("memory")]]);
}

/// A temporary table built from a query, and a temporary one replaced, neither of which touches the
/// stored table of the same name.
#[test]
fn a_temporary_table_takes_the_clauses_a_stored_one_takes() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER)",
        "CREATE TEMPORARY TABLE t AS SELECT 7 AS a, 8 AS b",
    ]);
    assert_eq!(rows(&db, "SELECT a, b FROM t"), vec![vec![integer(7), integer(8)]]);
    db.execute("CREATE OR REPLACE TEMPORARY TABLE t (a INTEGER, b INTEGER, c INTEGER)")
        .expect("replacing the temporary one");
    assert_eq!(
        rows(
            &db,
            "SELECT database_name, column_count FROM duckdb_tables() \
             WHERE table_name = 't' ORDER BY database_name"
        ),
        vec![vec![text("memory"), Value::BigInt(1)], vec![text("temp"), Value::BigInt(3)]]
    );
    db.execute("CREATE TEMPORARY TABLE IF NOT EXISTS t (a INTEGER)").expect("already there");
    assert_eq!(
        refusal(&db, "CREATE TEMPORARY TABLE t (a INTEGER)"),
        "Table with name \"t\" already exists!"
    );
}

#[test]
fn a_not_null_column_refuses_a_null_and_keeps_what_came_before_it() {
    let db = scripted(&[
        "CREATE TABLE t (a INTEGER NOT NULL, b VARCHAR)",
        "INSERT INTO t VALUES (1, NULL)",
    ]);
    assert_eq!(refusal(&db, "INSERT INTO t VALUES (NULL, 'x')"), "NOT NULL constraint failed: t.a");
    // The whole statement is refused rather than the row, so the table is what it was before it.
    assert_eq!(db.table_len("t").unwrap(), 1);
    assert_eq!(rows(&db, "SELECT a, b FROM t"), vec![vec![integer(1), Value::Null]]);
}

#[test]
fn a_table_function_produces_rows_where_a_table_would() {
    let db = Database::new();
    assert_eq!(
        rows(&db, "SELECT * FROM range(3)"),
        vec![vec![Value::BigInt(0)], vec![Value::BigInt(1)], vec![Value::BigInt(2)]]
    );
    assert_eq!(
        rows(&db, "SELECT * FROM generate_series(3)"),
        vec![
            vec![Value::BigInt(0)],
            vec![Value::BigInt(1)],
            vec![Value::BigInt(2)],
            vec![Value::BigInt(3)],
        ]
    );
}

#[test]
fn the_column_is_called_what_the_function_is_called_until_it_is_aliased() {
    let db = Database::new();
    let result = db.query("SELECT * FROM range(2)").unwrap();
    assert_eq!(result.names()[0], "range");
    let result = db.query("SELECT i FROM range(2) t(i)").unwrap();
    assert_eq!(result.names()[0], "i");
    // The table alias without a column list renames the table and not the column, which is what
    // makes t.range legal here and t.i not.
    let result = db.query("SELECT t.range FROM range(2) t").unwrap();
    assert_eq!(result.names()[0], "range");
}

#[test]
fn a_table_function_joins_and_aggregates_like_anything_else_in_a_from_clause() {
    let db = database();
    assert_eq!(rows(&db, "SELECT count(*) FROM range(10)"), vec![vec![Value::BigInt(10)]]);
    assert_eq!(rows(&db, "SELECT sum(range) FROM range(1, 5)"), vec![vec![Value::HugeInt(10)]]);
    assert_eq!(
        rows(&db, "SELECT count(*) FROM t, range(3)"),
        vec![vec![Value::BigInt(12)]],
        "four rows against three is twelve"
    );
    assert_eq!(
        rows(&db, "SELECT x FROM t JOIN range(2) ON t.x = range ORDER BY x"),
        vec![vec![integer(1)], vec![integer(1)]]
    );
}

#[test]
fn the_arguments_are_expressions_and_a_column_in_one_is_lateral() {
    let db = database();
    assert_eq!(rows(&db, "SELECT count(*) FROM range(2 + 3)"), vec![vec![Value::BigInt(5)]]);
    // `FROM t, range(t.x)` is LATERAL, and the rows of `t` are 3, 1, 2 and 1, so this is seven.
    // There is nothing underneath a table function for the domain to be pushed into, since its
    // arguments are what produce its rows, so the domain is what the call is made over instead.
    // See `Node::LateralFunction`.
    assert_eq!(rows(&db, "SELECT count(*) FROM t, range(t.x)"), vec![vec![Value::BigInt(7)]]);
    // Only the entries to the left. `range` is the first entry here and there is nothing for `t.x`
    // to name, which is a binder error and not a lateral reference read backwards.
    let message = failure(&db, "SELECT count(*) FROM range(t.x), t");
    assert!(message.contains("t"), "{message}");
}

#[test]
fn a_table_function_that_does_not_exist_says_so_rather_than_being_read_as_a_table() {
    let db = Database::new();
    // `read_json` is the next file reader people will write and it is not one of the four, so it is
    // the one that has to come back named rather than being taken for a table.
    let message = failure(&db, "SELECT * FROM read_json('x.json')");
    assert!(message.contains("read_json"), "{message}");
    let message = failure(&db, "SELECT * FROM nowhere.range(3)");
    assert!(message.contains("nowhere"), "{message}");
    let message = failure(&db, "SELECT * FROM range(1, 2, 3, 4)");
    assert!(message.contains("range"), "{message}");
}

#[test]
fn a_step_of_zero_is_the_one_call_that_is_an_error_rather_than_an_empty_result() {
    let db = Database::new();
    let message = failure(&db, "SELECT * FROM range(1, 5, 0)");
    assert!(message.contains("interval cannot be 0"), "{message}");
    assert!(rows(&db, "SELECT * FROM range(5, 1)").is_empty());
    assert!(rows(&db, "SELECT * FROM range(NULL)").is_empty());
}

#[test]
fn a_range_wider_than_one_chunk_comes_out_whole_and_in_order() {
    // Three thousand crosses the vector boundary, so this is the test that the chunking does not
    // repeat a value or drop one at the seam.
    let db = Database::new();
    let result = db.query("SELECT count(*), min(range), max(range) FROM range(3000)").unwrap();
    let row = result.rows().next().unwrap();
    assert_eq!(row, vec![Value::BigInt(3000), Value::BigInt(0), Value::BigInt(2999)]);
}

#[test]
fn a_result_converts_to_arrow_columns_with_the_names_the_query_gave_them() {
    let db = database();
    let result = db.query("SELECT x, s FROM t ORDER BY x, s").unwrap();
    let batches = result.to_arrow().unwrap();
    assert_eq!(batches.len(), result.chunk_count());
    let batch = &batches[0];
    assert_eq!(batch.len(), 4);
    assert_eq!(
        batch.schema().fields,
        vec![
            arrow::Field::new("x", arrow::DataType::Int32),
            arrow::Field::new("s", arrow::DataType::Utf8)
        ]
    );
    // The rows sort to 1 a, 1 null, 2 c, 3 a, so the integers are four little endian words and the
    // strings are the three non null ones back to back with the null taking no bytes.
    assert_eq!(
        batch.column(0).unwrap().values(),
        &[1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]
    );
    assert_eq!(batch.column(1).unwrap().values(), b"aca");
    assert_eq!(batch.column(1).unwrap().null_count(), 1);
}

#[test]
fn a_query_that_produced_no_rows_still_says_what_its_columns_were() {
    let db = database();
    let result = db.query("SELECT x FROM empty").unwrap();
    assert!(result.to_arrow().unwrap().is_empty());
    // Which is exactly why the schema is a separate call. A consumer reading it off the first batch
    // would have no batch to read it off, and the query still has a column and that column still
    // has a type.
    let schema = result.arrow_schema().unwrap();
    assert_eq!(schema.fields, vec![arrow::Field::new("x", arrow::DataType::Int32)]);
    let empty = arrow::RecordBatch::empty(schema).unwrap();
    assert_eq!(empty.width(), 1);
    assert!(empty.is_empty());
}

#[test]
fn an_aggregate_over_a_wide_result_keeps_every_chunk_as_its_own_batch() {
    // Batches are per chunk rather than one for the whole result, so a result that crossed the
    // vector boundary is the case that proves the rows are all there and none of them are twice.
    let db = Database::new();
    let total = VECTOR_SIZE + VECTOR_SIZE / 2;
    let result = db.query(&format!("SELECT range FROM range({total})")).unwrap();
    let batches = result.to_arrow().unwrap();
    assert!(batches.len() > 1, "{total} rows is more than one chunk");
    let rows: usize = batches.iter().map(rudb_arrow::RecordBatch::len).sum();
    assert_eq!(rows, total);
    let bytes: usize = batches.iter().map(|batch| batch.column(0).unwrap().values().len()).sum();
    assert_eq!(bytes, total * 8);
}

#[test]
fn a_database_remembers_what_it_was_opened_with() {
    let config = Config::new()
        .with_memory_limit_text("2GB")
        .unwrap()
        .with_threads(3)
        .unwrap()
        .with_query_timeout(Duration::from_secs(10));
    let db = Database::open_with(":memory:", config).unwrap();
    assert_eq!(db.config().memory_limit(), Some(2_000_000_000));
    assert_eq!(db.config().threads(), 3);
    assert_eq!(db.config().query_timeout(), Some(Duration::from_secs(10)));
    // The settings survive the query path, which is the whole point of reading them back: a harness
    // that opened the database is the thing that reports what the run used.
    db.query("SELECT 1").unwrap();
    assert_eq!(db.config().threads(), 3);
}

#[test]
fn a_database_opened_the_plain_way_has_the_defaults() {
    let db = Database::new();
    assert_eq!(db.config(), Config::default());
    // Eighty percent of the machine, so the number belongs to the machine and what is asserted is
    // that the database took it. The one it must not be is no limit, since that is the state where
    // a runaway query is stopped by the allocator and takes the process with it. See #219.
    assert_eq!(db.config().memory_limit(), rudb_io::default_memory_limit());
    assert_eq!(db.memory().limit(), db.config().memory_limit());
}

/// The query that runs long enough to be stopped, which is a count nobody waits for.
const FOREVER: &str = "SELECT count(*) FROM range(100000000000)";

#[test]
fn a_query_that_runs_too_long_is_stopped_by_the_limit_it_was_opened_with() {
    let db = Database::with_config(Config::new().with_query_timeout(Duration::from_millis(50)));
    let started = std::time::Instant::now();
    let error = db.query(FOREVER).expect_err("that does not finish");
    assert_eq!(error.code().duckdb_name(), "Interrupt Error");
    assert!(error.message().contains("50 millisecond"), "{error}");
    // The limit is on the statement rather than a suggestion, so this has to be over in about the
    // time it was given rather than in the time the query would have taken, which is hours.
    assert!(started.elapsed() < Duration::from_secs(10), "{:?}", started.elapsed());
}

#[test]
fn a_limit_does_not_stop_a_query_that_finishes_inside_it() {
    let db = Database::with_config(Config::new().with_query_timeout(Duration::from_secs(60)));
    assert_eq!(db.value("SELECT 42").unwrap(), Value::Integer(42));
    // And the clock starts again for the next statement rather than carrying on from the first.
    assert_eq!(db.value("SELECT 43").unwrap(), Value::Integer(43));
}

#[test]
fn a_join_that_runs_too_long_is_stopped_partway_through_its_own_loop() {
    // The timeout is checked between the chunks an operator produces, and a nested loop join
    // produces its first chunk only after the whole join is done. Fifty thousand left rows against
    // twenty thousand right ones is a billion comparisons and about a minute, all of it inside one
    // call, so without a check in the loop itself the clock below is read once, at the end.
    // A second rather than the fifty milliseconds the other limit tests use, because the limit is
    // on the database and not on the query, so the two statements below are run under it too. They
    // are a few milliseconds of work on an idle machine and they were over fifty on a busy one,
    // which failed the gate here on a setup line rather than on anything this test is about. A
    // second is two hundred times what the setup needs and a sixtieth of what the join needs, so it
    // still proves the only thing at issue, which is that the join is stopped inside its own loop.
    //
    // The condition is `<` rather than `=` because an equality is answered by a lookup now and this
    // test is about the loop. A join that finishes in four hundred milliseconds proves nothing
    // about a clock read at the end of one.
    let db = Database::with_config(Config::new().with_query_timeout(Duration::from_secs(1)));
    db.execute("CREATE TABLE l AS SELECT i AS k FROM range(50000) t(i)").unwrap();
    db.execute("CREATE TABLE r AS SELECT i * 2 AS k FROM range(20000) t(i)").unwrap();
    let started = std::time::Instant::now();
    let error =
        db.query("SELECT count(*) FROM l JOIN r ON l.k < r.k").expect_err("that does not finish");
    assert_eq!(error.code().duckdb_name(), "Interrupt Error");
    // One left row's pass over the right side is what it may overshoot by, which is milliseconds.
    assert!(started.elapsed() < Duration::from_secs(10), "{:?}", started.elapsed());
}

#[test]
fn another_thread_can_interrupt_a_running_query() {
    let db = Database::new();
    let connection = db.connect();
    let stopper = connection.clone();
    let watchdog = std::thread::spawn(move || {
        std::thread::sleep(Duration::from_millis(50));
        stopper.interrupt();
    });
    let error = connection.query(FOREVER).expect_err("the other thread stopped it");
    watchdog.join().expect("the watchdog ran");
    assert_eq!(error.code().duckdb_name(), "Interrupt Error");
    assert_eq!(error.message(), "Interrupted!");
}

#[test]
fn an_interrupt_stops_the_statement_it_was_meant_for_and_not_the_next_one() {
    let db = Database::new();
    let connection = db.connect();
    connection.interrupt();
    // The flag is cleared at the top of each statement, so an interrupt nothing was running under
    // is dropped rather than killing whatever comes next.
    assert_eq!(connection.value("SELECT 1").unwrap(), Value::Integer(1));
}

#[test]
fn a_statement_that_writes_is_stoppable_too_and_leaves_nothing_behind() {
    let db = Database::with_config(Config::new().with_query_timeout(Duration::from_millis(50)));
    db.execute("CREATE TABLE big (x BIGINT)").unwrap();
    let error = db
        .execute("INSERT INTO big SELECT x FROM range(100000000000) t(x)")
        .expect_err("that does not finish");
    assert_eq!(error.code().duckdb_name(), "Interrupt Error");
    // Nothing was appended, because the source runs to completion before anything is. That is not
    // a rollback, it is the absence of a partial write, and it stops being enough the day the
    // writes stream.
    assert_eq!(db.table_len("big").unwrap(), 0);
}

/// A memory limit small enough that a sort over a few hundred thousand rows cannot fit in it.
///
/// A row buffered by a pipeline breaker is a `Vec<Value>` today, which is a hundred bytes or so for
/// one number, so a megabyte is a few thousand rows. The queries below are far larger than that, so
/// the test is about the limit stopping them and not about where exactly the boundary sits.
const SMALL: u64 = 1 << 20;

#[test]
fn a_query_that_buffers_too_much_is_stopped_by_the_memory_limit() {
    let db = Database::with_config(Config::new().with_memory_limit(SMALL));
    let error = db.query("SELECT * FROM range(10000000) ORDER BY range").expect_err("too large");
    assert_eq!(error.code().duckdb_name(), "Out of Memory Error");
    assert!(error.message().contains("could not allocate"), "{error}");
    assert!(error.message().contains("1.0 MiB used"), "{error}");
}

#[test]
fn every_operator_that_buffers_is_held_to_the_limit() {
    // One query per pipeline breaker, because a limit that catches the sort and not the grouping is
    // a limit somebody finds out about from a dead process rather than from an error.
    let queries = [
        "SELECT * FROM range(10000000) ORDER BY range",
        "SELECT range, count(*) FROM range(10000000) GROUP BY range",
        "SELECT DISTINCT range FROM range(10000000)",
        "SELECT * FROM range(10000000) UNION ALL SELECT * FROM range(10)",
        "SELECT * FROM range(10000000) a, range(10000000) b WHERE a.range = b.range",
    ];
    for query in queries {
        let db = Database::with_config(Config::new().with_memory_limit(SMALL));
        let error = db.query(query).err().unwrap_or_else(|| panic!("{query} should not have fit"));
        assert_eq!(error.code().duckdb_name(), "Out of Memory Error", "{query}");
    }
}

/// A group key is charged what the table kept rather than what the buffer it was read into had
/// room for. Per #269.
#[test]
fn one_long_group_key_does_not_charge_every_short_one_for_its_length() {
    // Four megabytes, where the honest charge for these rows is a few hundred kilobytes and the
    // charge this is guarding against is two hundred copies of the long key, which is twenty
    // megabytes.
    let db = Database::with_config(Config::new().with_memory_limit(4 << 20));
    db.create_table("k", vec![Field::new("s", LogicalType::Varchar)]).unwrap();
    // The long one first, because the buffer being reused is what carries its length into every
    // key after it and a buffer never gives room back.
    let mut rows = vec![vec![Value::Varchar("x".repeat(100_000))]];
    rows.extend((0..200).map(|n| vec![Value::Varchar(format!("group {n}"))]));
    db.append("k", &rows).unwrap();
    let result = db.query("SELECT s, count(*) FROM k GROUP BY s").expect("the table fits");
    assert_eq!(result.len(), 201);
    // And the same query with the limit taken away agrees about the rows, so what is being tested
    // is the accounting rather than the grouping.
    let loose = Database::new();
    loose.create_table("k", vec![Field::new("s", LogicalType::Varchar)]).unwrap();
    loose.append("k", &rows).unwrap();
    assert_eq!(loose.query("SELECT s, count(*) FROM k GROUP BY s").unwrap().len(), 201);
}

/// A group by whose table does not fit the budget spills and answers anyway. Per #220.
///
/// This used to be the test for #272, which is the charge for the rows a group by builds out of its
/// table, and the way it asserted that charge was that a million groups did not fit in three
/// hundred megabytes. They do now, because a table that cannot grow any further stops growing and
/// the rows that would have gone into it go to a file instead. The charge is still there and is
/// still what decides when that happens, so the boundary this test sits on is the same one, and
/// what is on the far side of it has changed from an error to an answer.
#[test]
fn a_group_by_too_large_for_its_budget_spills_rather_than_stopping() {
    // A million groups against three hundred megabytes, where the table and the rows and the chunks
    // come to more than that between them.
    let db = Database::with_config(Config::new().with_memory_limit(300 << 20));
    let query = "SELECT range, count(*) FROM range(1000000) GROUP BY range";
    assert_eq!(db.query(query).expect("it spills rather than stopping").len(), 1_000_000);
    assert!(
        db.memory().peak() <= 300 << 20,
        "{} was held to get there, against a limit of {}",
        db.memory().peak(),
        300 << 20
    );
    // And a budget that does not hold the answer still refuses, quickly. Spilling moves the rows
    // out of the table and not out of the result, so there is a size of budget no number of passes
    // gets a query under, and the aggregate says so while its spill file is small rather than after
    // it has written the whole input out and read it back sixty four times.
    let tight = Database::with_config(Config::new().with_memory_limit(SMALL));
    let error = tight.query(query).err().unwrap_or_else(|| panic!("a megabyte holds none of this"));
    assert_eq!(error.code().duckdb_name(), "Out of Memory Error");
}

#[test]
fn the_budget_is_given_back_when_the_query_stops() {
    let db = Database::with_config(Config::new().with_memory_limit(SMALL));
    db.query("SELECT * FROM range(10000000) ORDER BY range").expect_err("too large");
    assert_eq!(db.memory().used(), 0, "an operator that failed still gave its rows back");
    // And the database is usable, which is the difference between an error and a dead process.
    assert_eq!(db.value("SELECT 1").unwrap(), Value::Integer(1));
}

#[test]
fn a_result_holds_its_bytes_until_it_is_dropped() {
    let db = Database::with_config(Config::new().with_memory_limit(SMALL));
    let result = db.query("SELECT * FROM range(1000)").unwrap();
    assert!(result.footprint() > 0, "a thousand rows are charged something");
    assert_eq!(db.memory().used(), result.footprint());
    drop(result);
    assert_eq!(db.memory().used(), 0);
}

#[test]
fn query_metrics_report_the_memory_budget_high_water_mark() {
    let db = Database::new();
    let result = db.query("SELECT range, count(*) FROM range(10000) GROUP BY range").unwrap();
    let measured = result.metrics().expect("a query carries execution metrics");
    assert!(measured.resource.peak_bytes > 0, "a buffering operator reserved memory");
    assert_eq!(measured.resource.peak_bytes, db.memory().peak());
}

#[test]
fn a_database_with_no_limit_counts_what_it_holds_anyway() {
    // So that a program can watch the number before it decides what limit to set. It has to ask for
    // no limit now, because a database opened the plain way has one.
    let db = Database::with_config(Config::new().with_no_memory_limit());
    let result = db.query("SELECT * FROM range(1000)").unwrap();
    assert_eq!(db.memory().limit(), None);
    assert_eq!(db.memory().used(), result.footprint());
}

#[test]
fn the_limit_is_on_the_database_rather_than_on_each_query() {
    // Two results alive at once are held to one limit between them, which is what DuckDB's
    // memory_limit means and the only reading that is any use.
    let db = Database::with_config(Config::new().with_memory_limit(SMALL));
    // Eighty thousand eight byte numbers is most of a megabyte and two of them is more than one.
    let wide = "SELECT range FROM range(80000)";
    let first = db.query(wide).expect("one fits");
    let error = db.query(wide).expect_err("there is no room for a second copy");
    assert_eq!(error.code().duckdb_name(), "Out of Memory Error");
    drop(first);
    db.query(wide).expect("the room came back");
}

#[test]
fn set_disabled_optimizers_turns_a_pass_off_for_the_statements_that_follow() {
    // The whole reason this statement exists: a plan that is wrong after optimization and right
    // before it is a plan whose pass can be named, and naming it is how the bisector will work.
    let db = Database::new();
    let folded = db.plan("SELECT 1 + 2").unwrap();
    assert!(folded.contains('3'), "{folded}");
    db.execute("SET disabled_optimizers = 'expression_rewriter'").unwrap();
    let unfolded = db.plan("SELECT 1 + 2").unwrap();
    assert!(unfolded.contains("\"+\""), "{unfolded}");
    assert_eq!(db.setting("disabled_optimizers").unwrap(), "expression_rewriter");
    db.execute("RESET disabled_optimizers").unwrap();
    assert_eq!(db.plan("SELECT 1 + 2").unwrap(), folded);
}

#[test]
fn every_name_the_pass_list_publishes_is_a_name_the_statement_accepts() {
    // The two have to agree or the corpus cannot turn the optimizer off, since the way it does
    // that is to join this list with commas and hand it to the statement. A pass added without a
    // name the setting knows would fail here rather than in another repository.
    let names = crate::optimizers();
    assert!(names.len() >= 6, "{names:?}");
    let db = Database::new();
    for name in &names {
        db.execute(&format!("SET disabled_optimizers = '{name}'")).expect(name);
    }
    let all = names.join(",");
    db.execute(&format!("SET disabled_optimizers = '{all}'")).expect("every pass off at once");
    // Read back in alphabetical order rather than in the order they run, because that is the order
    // the binary reads them back in and the setting is a compatibility surface.
    let mut sorted = names.clone();
    sorted.sort_unstable();
    assert_eq!(db.setting("disabled_optimizers").unwrap(), sorted.join(","));
    // And with all of them off the query still answers, out of the plan the binder produced.
    assert_eq!(db.value("SELECT 1 + 2").unwrap(), Value::Integer(3));
    let unoptimized = db.plan("SELECT 1 + 2").unwrap();
    assert!(unoptimized.contains("\"+\""), "{unoptimized}");
}

#[test]
fn a_pass_that_nobody_has_is_refused_by_the_statement_that_named_it() {
    let db = Database::new();
    let error = db.execute("SET disabled_optimizers = 'no_such_pass'").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Parser Error");
    assert_eq!(db.setting("disabled_optimizers").unwrap(), "", "a refused set changed nothing");
}

#[test]
fn a_pass_duckdb_has_and_rudb_has_not_built_is_taken_and_does_nothing() {
    // Forty five files in the upstream corpus run a SET disabled_optimizers and most of them name
    // a pass rudb has not written. Refusing those fails the SET, and a failed SET in a
    // sqllogictest file ends the file, so every record after it goes unasked over a pass whose
    // absence changes no answer.
    let db = Database::new();
    let folded = db.plan("SELECT 1 + 2").unwrap();
    for name in ["compressed_materialization", "join_filter_pushdown", "common_subexpressions"] {
        db.execute(&format!("SET disabled_optimizers = '{name}'")).expect(name);
        assert_eq!(db.setting("disabled_optimizers").unwrap(), name);
        assert_eq!(db.plan("SELECT 1 + 2").unwrap(), folded, "{name} turned something off");
    }
}

#[test]
fn the_name_is_read_without_regard_to_case_because_two_corpus_files_shout_it() {
    let db = Database::new();
    db.execute("SET disabled_optimizers = 'LATE_MATERIALIZATION'").unwrap();
    assert_eq!(db.setting("disabled_optimizers").unwrap(), "late_materialization");
    db.execute("SET disabled_optimizers = 'Top_N'").unwrap();
    assert_eq!(db.setting("disabled_optimizers").unwrap(), "top_n");
}

#[test]
fn the_setting_reads_back_as_what_was_understood_rather_than_as_what_was_written() {
    // The binary tidies the list on the way in, so ' TOP_N , join_order , top_n ,' reads back as
    // join_order,top_n. A database that kept the text as written would answer current_setting
    // differently for every spelling but the tidy one.
    let db = Database::new();
    db.execute("SET disabled_optimizers = ' TOP_N , join_order , top_n ,'").unwrap();
    assert_eq!(db.setting("disabled_optimizers").unwrap(), "join_order,top_n");
}

#[test]
fn every_name_the_binary_accepts_is_a_name_the_statement_accepts() {
    // The list is written down rather than discovered, so the thing that can go wrong with it is a
    // typo, and a typo in it is a corpus file that fails on a name the binary is happy with.
    let db = Database::new();
    for name in rudb_opt::UPSTREAM {
        db.execute(&format!("SET disabled_optimizers = '{name}'")).expect(name);
        assert_eq!(db.setting("disabled_optimizers").unwrap(), name);
    }
    assert_eq!(rudb_opt::UPSTREAM.len(), 44, "the pinned binary lists forty four");
    // The table remains DuckDB's list. A local pass may have a local name, but adding one must not
    // make an introspection query claim that DuckDB has it too.
    assert!(!rudb_opt::UPSTREAM.contains(&"dependent_group_keys"));
}

#[test]
fn set_memory_limit_moves_the_budget_the_queries_after_it_are_held_to() {
    // Opened with no limit so that the reset at the end has one unambiguous thing to go back to.
    let db = Database::with_config(Config::new().with_no_memory_limit());
    assert_eq!(db.memory().limit(), None);
    db.execute("SET memory_limit = '1GiB'").unwrap();
    assert_eq!(db.memory().limit(), Some(1 << 30));
    assert_eq!(db.config().memory_limit(), Some(1 << 30));
    assert_eq!(db.setting("memory_limit").unwrap(), "1.0 GiB");
    // A gigabyte and a gibibyte are two different numbers, which is what the binary does.
    db.execute("SET memory_limit = '1GB'").unwrap();
    assert_eq!(db.memory().limit(), Some(1_000_000_000));
    db.execute("RESET memory_limit").unwrap();
    assert_eq!(db.memory().limit(), None, "back to what the database was opened with");
}

#[test]
fn a_memory_limit_set_in_the_middle_of_a_session_refuses_the_next_query_that_passes_it() {
    let db = Database::new();
    let wide = "SELECT range FROM range(80000)";
    db.query(wide).expect("no limit yet");
    // Eighty thousand eight byte numbers is six hundred and forty kilobytes, so a hundred is not
    // enough room for them and the query that ran a moment ago stops running.
    db.execute("SET memory_limit = '100KB'").unwrap();
    let error = db.query(wide).expect_err("the limit is on now");
    assert_eq!(error.code().duckdb_name(), "Out of Memory Error");
}

#[test]
fn set_threads_is_recorded_even_though_nothing_runs_in_parallel_yet() {
    let db = Database::with_config(Config::new().with_threads(2).unwrap());
    db.execute("SET threads = 8").unwrap();
    assert_eq!(db.config().threads(), 8);
    assert_eq!(db.setting("threads").unwrap(), "8");
    let error = db.execute("SET threads = 0").unwrap_err();
    assert_eq!(error.to_string(), "Syntax Error: Must have at least 1 thread!");
    db.execute("RESET threads").unwrap();
    assert_eq!(db.config().threads(), 2, "reset is what the database was opened with");
}

#[test]
fn a_relationship_is_declared_by_a_setting_and_read_back_from_one() {
    let db = Database::new();
    assert_eq!(db.setting("graph_links").unwrap(), "", "a fresh database declares nothing");

    db.execute(
        "SET graph_links = 'lineitem(l_orderkey) -> orders(o_orderkey), \
         orders(o_custkey) -> customer(c_custkey)'",
    )
    .unwrap();
    let declared = rudb_graph::parse_links(&db.setting("graph_links").unwrap()).unwrap();
    assert_eq!(declared.len(), 2);
    assert_eq!(declared[0].name(), "lineitem(l_orderkey) -> orders(o_orderkey)");
    assert_eq!(
        declared[0].cardinality,
        rudb_graph::Cardinality::Unverified,
        "a declaration says what the author believes and the build says what is true"
    );

    // Refused where the author can see it. A declaration that does not parse would otherwise be
    // carried to a checkpoint that silently built nothing out of it.
    let error = db.execute("SET graph_links = 'lineitem(l_orderkey)'").unwrap_err();
    assert!(error.to_string().contains("child(column) -> parent(column)"), "{error}");
    assert_eq!(
        rudb_graph::parse_links(&db.setting("graph_links").unwrap()).unwrap().len(),
        2,
        "and the declaration that worked is still there"
    );

    db.execute("RESET graph_links").unwrap();
    assert_eq!(db.setting("graph_links").unwrap(), "");
}

#[test]
fn rudb_links_says_what_is_declared_and_what_of_it_is_built() {
    let db = Database::new();
    assert!(rows(&db, "SELECT name FROM rudb_links()").is_empty(), "nothing declared, no rows");

    db.create_table("customer", vec![Field::new("c_custkey", LogicalType::Integer)]).unwrap();
    db.append("customer", &[vec![Value::Integer(1)], vec![Value::Integer(2)]]).unwrap();
    db.execute(
        "SET graph_links = 'orders(o_custkey) -> customer(c_custkey), \
         lineitem(l_orderkey) -> orders(o_orderkey)'",
    )
    .unwrap();

    // The declaration is a row whether or not anything was built, because a reader who cannot tell
    // a relationship nobody declared from one nothing acted on cannot tell which to fix.
    let listed = rows(
        &db,
        "SELECT parent_table, cardinality, key_map, key_map_bytes, link, note FROM rudb_links() \
         ORDER BY parent_table",
    );
    assert_eq!(
        listed,
        vec![
            vec![
                Value::Varchar("customer".into()),
                Value::Varchar("unverified".into()),
                Value::Null,
                Value::Null,
                Value::Null,
                Value::Varchar("no key map is stored".into()),
            ],
            vec![
                Value::Varchar("orders".into()),
                Value::Varchar("unverified".into()),
                Value::Null,
                Value::Null,
                Value::Null,
                Value::Varchar("no table of that name".into()),
            ],
        ],
        "a memory table holds no sections and a table that is not here holds nothing at all"
    );
    assert_eq!(
        rows(
            &db,
            "SELECT child_table, child_key, parent_key FROM rudb_links() WHERE \
             parent_table = 'customer'"
        ),
        vec![vec![
            Value::Varchar("orders".into()),
            Value::Varchar("o_custkey".into()),
            Value::Varchar("c_custkey".into()),
        ]]
    );
}

#[test]
fn a_forward_link_takes_the_monotone_form_only_when_every_child_row_has_a_parent_above_the_last() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-forms-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("CREATE TABLE customer (c_custkey INTEGER)").unwrap();
    db.execute("CREATE TABLE orders (o_custkey INTEGER)").unwrap();
    db.execute("CREATE TABLE returns (r_custkey INTEGER)").unwrap();
    db.execute("INSERT INTO customer SELECT i FROM range(1, 4001) AS r(i)").unwrap();
    // Three orders per customer in customer order, which is the shape the loader gives lineitem
    // over orders, and the one section 3.4 keeps as a bit vector rather than a packed column.
    db.execute("INSERT INTO orders SELECT 1 + (i - 1) / 3 FROM range(1, 10001) AS r(i)").unwrap();
    // The same shape with one row whose key is past the last customer, which refuses the monotone
    // form because the bit vector has no way to say that a child found nothing.
    db.execute("INSERT INTO returns SELECT 1 + (i - 1) / 3 FROM range(1, 10001) AS r(i)").unwrap();
    db.execute("INSERT INTO returns VALUES (9999)").unwrap();
    db.execute(
        "SET graph_links = 'orders(o_custkey) -> customer(c_custkey), \
         returns(r_custkey) -> customer(c_custkey)'",
    )
    .unwrap();
    db.execute("CHECKPOINT").unwrap();

    assert_eq!(
        rows(
            &db,
            "SELECT child_table, cardinality, link, note FROM rudb_links() ORDER BY child_table"
        ),
        vec![
            vec![
                Value::Varchar("orders".into()),
                Value::Varchar("exactly one".into()),
                Value::Varchar("monotone".into()),
                Value::Null,
            ],
            vec![
                Value::Varchar("returns".into()),
                Value::Varchar("at most one".into()),
                Value::Varchar("packed".into()),
                Value::Varchar("some child rows have no parent, so this is not exactly one".into()),
            ],
        ],
        "one unmatched child row costs the relationship both its form and its totality"
    );
    // A form is a representation and not an answer: the join counts the same either way.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey"),
        vec![vec![Value::BigInt(10000)]]
    );
    assert_eq!(
        rows(&db, "SELECT count(*) FROM returns r JOIN customer c ON r.r_custkey = c.c_custkey"),
        vec![vec![Value::BigInt(10000)]]
    );

    drop(db);
    std::fs::remove_file(&path).ok();
}

#[test]
fn rudb_links_says_what_shape_the_relationship_turned_out_to_have() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-degrees-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("CREATE TABLE customer (c_custkey INTEGER)").unwrap();
    db.execute("CREATE TABLE orders (o_custkey INTEGER)").unwrap();
    db.execute("CREATE TABLE skewed (s_custkey INTEGER)").unwrap();
    db.execute("INSERT INTO customer SELECT i FROM range(1, 4001) AS r(i)").unwrap();
    // Even: two and a half orders per customer, every customer reached.
    db.execute("INSERT INTO orders SELECT 1 + (i - 1) / 3 FROM range(1, 10001) AS r(i)").unwrap();
    // Skewed: the same ten thousand children, nine thousand of them on one customer and the rest
    // one each, so a thousand customers have a child and three thousand have none.
    db.execute("INSERT INTO skewed SELECT 1 FROM range(1, 9001) AS r(i)").unwrap();
    db.execute("INSERT INTO skewed SELECT 1 + i FROM range(1, 1001) AS r(i)").unwrap();
    db.execute(
        "SET graph_links = 'orders(o_custkey) -> customer(c_custkey), \
         skewed(s_custkey) -> customer(c_custkey)'",
    )
    .unwrap();
    db.execute("CHECKPOINT").unwrap();

    let held = rows(
        &db,
        "SELECT child_table, degree_max, degree_p99, parent_unique, child_total FROM rudb_links() \
         ORDER BY child_table",
    );
    assert_eq!(
        held,
        vec![
            vec![
                Value::Varchar("orders".into()),
                Value::BigInt(3),
                Value::BigInt(3),
                Value::Boolean(true),
                Value::Boolean(true),
            ],
            // The same ten thousand children over the same four thousand parents, and the tail is
            // three orders of magnitude away. That difference is the whole reason the histogram is
            // stored rather than just the mean, which is 2.5 for both.
            vec![
                Value::Varchar("skewed".into()),
                Value::BigInt(9000),
                Value::BigInt(1),
                Value::Boolean(true),
                Value::Boolean(true),
            ],
        ],
        "the shape of the relationship, not the shape of the declaration"
    );
    // A clustered child gathers a short distance and a scattered one a long distance, which is what
    // the link join decision would otherwise have to guess at plan time. Both of the tables above
    // are clustered, so this needs a third whose keys walk the parent table in strides.
    db.execute("CREATE TABLE scattered (x_custkey INTEGER)").unwrap();
    db.execute("INSERT INTO scattered SELECT 1 + (i * 1237) % 4000 FROM range(1, 10001) AS r(i)")
        .unwrap();
    db.execute(
        "SET graph_links = 'orders(o_custkey) -> customer(c_custkey), \
         scattered(x_custkey) -> customer(c_custkey)'",
    )
    .unwrap();
    db.execute("CHECKPOINT").unwrap();
    let locality = rows(
        &db,
        "SELECT child_table FROM rudb_links() WHERE gather_locality < 1 ORDER BY child_table",
    );
    assert_eq!(
        locality,
        vec![vec![Value::Varchar("orders".into())]],
        "the clustered one gathers inside a cache line and the scattered one does not"
    );
    let far = rows(&db, "SELECT child_table FROM rudb_links() WHERE gather_locality > 1000");
    assert_eq!(far, vec![vec![Value::Varchar("scattered".into())]]);

    drop(db);
    std::fs::remove_file(&path).ok();
}

#[test]
fn a_join_over_a_built_relationship_is_planned_as_a_link_join_and_answers_the_same() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-linkjoin-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("CREATE TABLE customer (c_custkey INTEGER, c_name VARCHAR)").unwrap();
    db.execute("CREATE TABLE orders (o_orderkey INTEGER, o_custkey INTEGER)").unwrap();
    db.execute("INSERT INTO customer SELECT i, 'c' || i FROM range(1, 4001) AS r(i)").unwrap();
    db.execute("INSERT INTO orders SELECT i, 1 + i % 4000 FROM range(1, 10001) AS r(i)").unwrap();
    db.execute("SET graph_links = 'orders(o_custkey) -> customer(c_custkey)'").unwrap();
    db.execute("CHECKPOINT").unwrap();
    // The layer is opt in, per `spec/graph/09-measurement.md` section 9.2 and the default in
    // `rudb_common::rules`. The checkpoint above wrote the link either way, which is the point of
    // the default: a file carries the section and a session decides whether anything reads it.
    db.execute("SET graph_sections = 'on'").unwrap();
    // Every order here has a customer, so the relationship carries both certificates and the join
    // below reads no customer column, which is exactly the join the elimination pass deletes. This
    // test is about which algorithm answers a join, so it turns that pass off and keeps one. The
    // elimination has its own test, and its own switch, which is what section 9.2 asks for.
    db.execute("SET stats_join_elimination = false").unwrap();

    // Four thousand customers fit in any cache there is, so the rule declines them, which is the
    // rule working rather than the pass failing. The setting is what a test uses to ask about the
    // other side of the crossover without writing a parent that really does not fit.
    let sql = "SELECT count(*), sum(o_orderkey) FROM orders JOIN customer ON o_custkey = c_custkey";
    let explained = |db: &Database, sql: &str| match db
        .query(&format!("EXPLAIN {sql}"))
        .expect("the explain ran")
        .value_at(0, 1)
    {
        Value::Varchar(text) => text,
        other => panic!("the plan came back as {other:?}"),
    };
    let plan = explained(&db, sql);
    assert!(!plan.contains("LinkJoin"), "four thousand customers were worth a link");
    // Section 6.7. The reason is on the line, with the two numbers the rule read, so a reader who
    // expected a link join finds out it was the size of the parent and not a missing link.
    assert!(
        plan.contains(
            "[builds a hash table, because the parent is 4000 rows and 16000 bytes \
             projected, which fits in cache]"
        ),
        "the plan does not say why it built a hash table:\n{plan}"
    );

    db.execute("SET graph_cache_bytes = 1").unwrap();
    assert_eq!(db.setting("graph_cache_bytes").unwrap(), "1 bytes");
    let plan = explained(&db, sql);
    assert!(plan.contains("LinkJoin"), "the join was not planned as a link join:\n{plan}");
    assert!(
        plan.contains("[reads the link, because the parent does not fit in cache"),
        "the plan does not say why it read the link:\n{plan}"
    );

    // The whole of section 3.1: the sections change the time and not the answer. The control is
    // the same query in the same process with the pass turned off.
    let linked = rows(&db, sql);
    db.execute("SET disabled_optimizers = 'link_join'").unwrap();
    assert!(!explained(&db, sql).contains("LinkJoin"), "the pass is still on");
    assert_eq!(linked, rows(&db, sql), "the link join answered a different question");
    db.execute("RESET disabled_optimizers").unwrap();

    // Section 9.2, the other switch and the one the ablation actually uses. Turning the pass off
    // leaves the sections readable and stops one rewrite. Turning the sections off takes the whole
    // layer away, and the two have to agree or the ablation is measuring the wrong thing.
    db.execute("SET graph_sections = 'off'").unwrap();
    assert!(!explained(&db, sql).contains("LinkJoin"), "the sections are still being read");
    assert_eq!(linked, rows(&db, sql), "the layer changed an answer rather than a time");
    db.execute("SET graph_sections = 'on'").unwrap();
    assert!(explained(&db, sql).contains("LinkJoin"), "turning the sections back on did nothing");

    db.execute("RESET graph_cache_bytes").unwrap();
    assert_eq!(db.setting("graph_cache_bytes").unwrap(), "8.0 MiB", "reset is the default");

    drop(db);
    std::fs::remove_file(&path).ok();
}

/// The query above joins to the parent and reads no column of it, so the gather never runs.
///
/// That is not a contrived shape, it is the shape a foreign key join takes when the parent is only
/// there to filter, and it is worth having a test of. It is also the reason the link join shipped
/// unable to read a parent column at all: `Parent` lays the parts of a column end to end with
/// `rudb_vector::concat`, which declines anything that is not already flat, and a column written by
/// rudb's own writer is bit packed or dictionary encoded and never flat. So every link join that
/// actually gathered something failed, reporting that it was out of memory when it had twenty
/// gigabytes free.
///
/// It went unnoticed because the parent here is built by `INSERT` and checkpointed inside one
/// session, and the sizes above are small enough that what comes back is flat. `cargo xtask
/// sections` found it on the first run over a TPC-H directory. This is that finding as a test: a
/// parent column of each of the two forms real data arrives in, gathered, with the answer checked
/// against the same query with the layer off.
#[test]
fn a_link_join_gathers_a_parent_column_that_was_stored_in_a_form_that_is_not_flat() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-gather-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    // `c_grade` is four distinct strings over four thousand rows, which the writer dictionary
    // encodes, and `c_balance` is a small range of integers, which it bit packs. Those are the two
    // forms the probe on a real TPC-H file came back with.
    db.execute("CREATE TABLE customer (c_custkey INTEGER, c_grade VARCHAR, c_balance INTEGER)")
        .unwrap();
    db.execute("CREATE TABLE orders (o_orderkey INTEGER, o_custkey INTEGER)").unwrap();
    db.execute(
        "INSERT INTO customer SELECT i, ['bronze', 'silver', 'gold', 'platinum'][1 + i % 4], \
         100 + i % 50 FROM range(1, 4001) AS r(i)",
    )
    .unwrap();
    db.execute("INSERT INTO orders SELECT i, 1 + i % 4000 FROM range(1, 10001) AS r(i)").unwrap();
    db.execute("SET graph_links = 'orders(o_custkey) -> customer(c_custkey)'").unwrap();
    db.execute("CHECKPOINT").unwrap();
    db.execute("SET graph_sections = 'on'").unwrap();
    // Reopened, because the encoding is what the writer chose and a table still holding the chunks
    // it was inserted as would hand back the flat ones the bug hid behind.
    drop(db);
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("SET graph_links = 'orders(o_custkey) -> customer(c_custkey)'").unwrap();
    db.execute("SET graph_sections = 'on'").unwrap();

    let sql = "SELECT c_grade, count(*), sum(c_balance) FROM orders JOIN customer \
               ON o_custkey = c_custkey GROUP BY c_grade ORDER BY c_grade";
    let plan = |db: &Database| match db
        .query(&format!("EXPLAIN {sql}"))
        .expect("the explain ran")
        .value_at(0, 1)
    {
        Value::Varchar(text) => text,
        other => panic!("the plan came back as {other:?}"),
    };

    db.execute("SET graph_sections = 'off'").unwrap();
    let hashed = rows(&db, sql);
    assert!(!hashed.is_empty(), "the control answered nothing, so it is not a control");

    db.execute("SET graph_sections = 'on'").unwrap();
    db.execute("SET graph_cache_bytes = 1").unwrap();
    assert!(
        plan(&db).contains("LinkJoin"),
        "the join was not planned as a link join:\n{}",
        plan(&db)
    );
    // The assertion that would have caught it. Before the fix this was an out of memory error and
    // not a wrong answer, so a comparison of rows would never have seen it either.
    assert_eq!(rows(&db, sql), hashed, "gathering a parent column changed the answer");

    drop(db);
    std::fs::remove_file(&path).ok();
}

#[test]
fn a_checkpoint_builds_a_key_map_over_the_parent_of_every_declared_relationship() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-checkpoint-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let declaration = "SET graph_links = 'orders(o_custkey) -> customer(c_custkey)'";
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("CREATE TABLE customer (c_custkey INTEGER, c_name VARCHAR)").unwrap();
    db.execute("CREATE TABLE orders (o_orderkey INTEGER, o_custkey INTEGER)").unwrap();
    db.execute("INSERT INTO customer SELECT i, 'c' || i FROM range(1, 4001) AS r(i)").unwrap();
    db.execute("INSERT INTO orders SELECT i, 1 + i % 4000 FROM range(1, 10001) AS r(i)").unwrap();
    db.execute(declaration).unwrap();
    db.execute("CHECKPOINT").unwrap();

    let built = rows(
        &db,
        "SELECT cardinality, key_map, key_map_bytes > 0, link, link_bytes > 0, note FROM \
         rudb_links()",
    );
    assert_eq!(
        built,
        vec![vec![
            Value::Varchar("exactly one".into()),
            Value::Varchar("identity".into()),
            Value::Boolean(true),
            Value::Varchar("packed".into()),
            Value::Boolean(true),
            Value::Null,
        ]],
        "a distinct ascending key column maps by subtraction, the second pass stored a forward \
         link beside it, and every child row found a parent through it"
    );
    // The queries the declaration was made for are the ones that have to keep their answers.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey"),
        vec![vec![Value::BigInt(10000)]]
    );
    drop(db);

    // A reader that opens the file again finds the sections, and one that never heard of the
    // setting finds the same rows: section 3.1 says the sections change the time and not the
    // answer.
    let reopened = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    assert_eq!(
        rows(&reopened, "SELECT count(*), max(c_custkey) FROM customer"),
        vec![vec![Value::BigInt(4000), Value::Integer(4000)]]
    );
    assert!(
        rows(&reopened, "SELECT name FROM rudb_links()").is_empty(),
        "a declaration is a session's and not the file's"
    );
    reopened.execute(declaration).unwrap();
    assert_eq!(
        rows(&reopened, "SELECT key_map FROM rudb_links()"),
        vec![vec![Value::Varchar("identity".into())]],
        "and the map that was built is read back without being built again"
    );
    drop(reopened);
    std::fs::remove_file(path).expect("the temporary native database is removed");
}

#[test]
fn rudb_links_says_what_a_structure_it_decided_against_would_have_cost() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-refused-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("CREATE TABLE customer (c_custkey INTEGER)").unwrap();
    db.execute("CREATE TABLE orders (o_custkey INTEGER)").unwrap();
    // The keys run one to four thousand twice over, so every customer key has a second row with
    // the same key and the parent side is not a key at all. The build encodes the map before it
    // finds that out, because finding it out is what encoding it is, and then keeps nothing.
    db.execute("INSERT INTO customer SELECT 1 + i % 4000 FROM range(0, 8000) AS r(i)").unwrap();
    db.execute("INSERT INTO orders SELECT 1 + i % 4000 FROM range(0, 10000) AS r(i)").unwrap();
    db.execute("SET graph_links = 'orders(o_custkey) -> customer(c_custkey)'").unwrap();
    db.execute("CHECKPOINT").unwrap();

    let listed = rows(
        &db,
        "SELECT cardinality, key_map, key_map_bytes > 0, link, link_bytes, note FROM rudb_links()",
    );
    assert_eq!(
        listed,
        vec![vec![
            Value::Varchar("unverified".into()),
            Value::Null,
            Value::Boolean(true),
            Value::Null,
            Value::Null,
            Value::Varchar(
                "the key map was measured and not kept, so key_map_bytes is what it would cost"
                    .into()
            ),
        ]],
        "a structure that is not there has no form and still has a size"
    );
    // The size is the point of the row: somebody reading it is deciding whether the structure is
    // worth having, and a null would leave them building it to find out. It is the payload the
    // build actually encoded and not an estimate of one, which for eight thousand unsorted keys is
    // a few bytes each.
    let Value::BigInt(bytes) = rows(&db, "SELECT key_map_bytes FROM rudb_links()")[0][0] else {
        panic!("a size");
    };
    assert!(bytes > 8000, "eight thousand keys do not encode in {bytes} bytes");

    // And the join answers the same question it would have with the map, which is section 3.1.
    assert_eq!(
        rows(&db, "SELECT count(*) FROM orders o JOIN customer c ON o.o_custkey = c.c_custkey"),
        vec![vec![Value::BigInt(20000)]]
    );

    drop(db);
    std::fs::remove_file(&path).ok();
}

#[test]
fn a_seam_is_set_and_read_back_through_the_statement_everything_else_goes_through() {
    let db = Database::new();
    assert_eq!(db.setting("seam.hash.table").unwrap(), "default");

    // Quoted, because a seam name has dots in it and DuckDB's grammar has no dot in an identifier.
    db.execute("SET \"seam.hash.table\" = 'unchained'").unwrap();
    assert_eq!(db.setting("seam.hash.table").unwrap(), "unchained");
    assert_eq!(db.seams().pinned(crate::seam::SeamId::HashTable), Some("unchained"));

    // Underscores for the dots is the spelling that needs no quotes, and it is the same seam.
    db.execute("SET seam_hash_table = 'linear-chained'").unwrap();
    assert_eq!(db.setting("hash.table").unwrap(), "linear-chained");

    db.execute("RESET seam_hash_table").unwrap();
    assert_eq!(db.setting("seam.hash.table").unwrap(), "default");
    assert_eq!(db.seams().pinned(crate::seam::SeamId::HashTable), None);
}

#[test]
fn the_policy_is_a_seam_like_the_rest_and_a_mistyped_one_names_the_seams() {
    let db = Database::new();
    db.execute("SET seam_policy = 'reference'").unwrap();
    assert_eq!(db.seams().mode(), crate::seam::PolicyMode::Reference);
    assert_eq!(db.setting("seam.policy").unwrap(), "reference");

    let error = db.execute("SET \"seam.hash.tabel\" = 'unchained'").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Catalog Error");
    assert!(error.message().contains("rudb_strategies()"), "{error}");

    let error = db.execute("SET seam_policy = 'clever'").unwrap_err();
    assert!(error.message().contains("reference, default or adaptive-bandit"), "{error}");
}

#[test]
fn a_hint_pins_a_seam_for_one_query_and_leaves_the_session_alone() {
    let db = Database::new();
    let sql = "SELECT /*+ hash.table(unchained) */ 42";
    assert_eq!(db.value(sql).unwrap(), Value::Integer(42));

    let seams = db.seams_for(sql).unwrap();
    assert_eq!(seams.pinned(crate::seam::SeamId::HashTable), Some("unchained"));
    assert_eq!(db.seams().pinned(crate::seam::SeamId::HashTable), None, "the session is untouched");
}

#[test]
fn a_hint_naming_a_seam_nobody_has_fails_the_query_rather_than_being_ignored() {
    let db = Database::new();
    let error = db.query("SELECT /*+ hash.tabel(unchained) */ 42").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Catalog Error");
    assert!(error.message().contains("no seam called hash.tabel"), "{error}");

    // A comment without the plus is a comment, whatever is written in it.
    assert_eq!(db.value("SELECT /* hash.tabel(unchained) */ 42").unwrap(), Value::Integer(42));
}

#[test]
fn a_name_that_is_not_a_setting_says_so_and_offers_the_nearest_ones() {
    let db = Database::new();
    let error = db.execute("SET bogus = 1").unwrap_err();
    assert_eq!(error.code().duckdb_name(), "Catalog Error");
    assert_eq!(error.message(), "unrecognized configuration parameter \"bogus\"");
    // A near miss gets the name it missed, and not the other hundred and ninety one.
    let error = db.execute("SET thread = 1").unwrap_err();
    assert!(error.message().contains("\"threads\""), "{error}");
    assert!(!error.message().contains("\"memory_limit\""), "{error}");
}

#[test]
fn the_two_scopes_this_database_does_not_have_are_two_different_sentences() {
    let db = Database::new();
    let error = db.execute("SET LOCAL threads = 2").unwrap_err();
    assert_eq!(error.to_string(), "Not implemented Error: SET LOCAL is not implemented.");
    let error = db.execute("SET SESSION threads = 2").unwrap_err();
    assert_eq!(error.to_string(), "Catalog Error: option \"threads\" cannot be set locally");
    db.execute("SET GLOBAL threads = 2").expect("global is the scope every setting here has");
    assert_eq!(db.config().threads(), 2);
}

#[test]
fn the_settings_a_database_was_opened_with_are_what_reset_goes_back_to() {
    let db = Database::with_config(Config::new().with_memory_limit(SMALL).with_threads(2).unwrap());
    db.execute("SET memory_limit = '4GB'").unwrap();
    db.execute("SET threads = 16").unwrap();
    assert_eq!(db.opened_with().memory_limit(), Some(SMALL));
    db.execute("RESET memory_limit").unwrap();
    db.execute("RESET threads").unwrap();
    assert_eq!(db.config(), db.opened_with());
    assert_eq!(db.memory().limit(), Some(SMALL));
}

#[test]
fn rudb_strategies_lists_every_seam_with_the_milestone_that_owes_it() {
    let db = database();
    let listed = rows(&db, "SELECT DISTINCT seam, milestone FROM rudb_strategies() ORDER BY seam");
    assert_eq!(listed.len(), 27, "the seam list is closed and this is its length");
    for row in &listed {
        let Value::Varchar(milestone) = &row[1] else { panic!("a milestone per seam") };
        assert!(milestone.starts_with('F'), "{milestone} is not a milestone");
    }
}

#[test]
fn a_seam_nobody_has_implemented_reads_back_as_planned_rather_than_missing() {
    let db = database();
    let listed = rows(
        &db,
        "SELECT count(*) FROM rudb_strategies() WHERE implementation IS NULL AND seam_description \
         IS NOT NULL",
    );
    assert_eq!(
        listed,
        vec![vec![Value::BigInt(26)]],
        "one seam has implementations and the rest say what milestone owes them"
    );
}

/// The first seam with implementations in the tree, as the table function shows it.
///
/// Three rows rather than one, the reference marked, and the description of what each one does,
/// which is what somebody deciding whether to sweep this seam reads before they do.
#[test]
fn the_compaction_seam_lists_its_three_implementations() {
    let db = database();
    let listed = rows(
        &db,
        "SELECT implementation, is_reference FROM rudb_strategies() WHERE seam = \
         'chunk.compaction'",
    );
    let names: Vec<&Value> = listed.iter().map(|row| &row[0]).collect();
    assert_eq!(
        names,
        vec![
            &Value::Varchar("never".to_owned()),
            &Value::Varchar("fixed-threshold".to_owned()),
            &Value::Varchar("learned-gain".to_owned()),
        ]
    );
    assert_eq!(listed[0][1], Value::Boolean(true), "the one that copies nothing is the reference");
}

#[test]
fn rudb_strategies_takes_no_arguments() {
    let db = database();
    assert!(
        failure(&db, "SELECT * FROM rudb_strategies(1)").contains("\"rudb_strategies\"()"),
        "a call with an argument is a binder error rather than an ignored argument"
    );
}

/// The operator of this kind, for a test that wants to look at one.
fn operator<'a>(metrics: &'a rudb_metrics::Document, kind: &str) -> &'a rudb_metrics::Operator {
    metrics
        .operators
        .iter()
        .find(|operator| operator.kind == kind)
        .unwrap_or_else(|| panic!("a {kind} in {:?}", metrics.operators))
}

/// The operators of a small query and what each of them reported.
///
/// There is no filter operator in here to read, and there is no way to write one over a stored
/// table any more: a filter sitting directly on a `Get` is applied by the scan and gets no operator
/// of its own, whatever its predicate is. So the scan is both halves of this, and the rows flowing
/// from one operator to the next are read off the projection above it instead.
#[test]
fn a_query_reports_what_every_operator_in_it_did() {
    let db = database();
    let sql = "SELECT x FROM t WHERE x > 1";
    let result = db.query(sql).unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    assert_eq!(metrics.query.sql, sql);
    let scan = operator(metrics, "Scan");
    let project = operator(metrics, "Project");
    assert_eq!(scan.detail.as_deref(), Some("t"), "a scan says what it read");
    assert_eq!(scan.rows_out, 2, "two of the four rows are over one and the scan is the filter");
    assert_eq!(project.rows_in, 2, "what the scan produced is what the one above it was handed");
    assert_eq!(project.rows_out, 2, "a projection keeps every row it is given");
    assert_eq!(project.pipeline, scan.pipeline, "nothing here breaks a pipeline");
    assert!(metrics.timing.execute_ns > 0, "running it took longer than nothing");
    assert!(
        metrics.operators.iter().all(|operator| operator.reference_impl),
        "everything at tier 0 is the reference implementation and the document says so"
    );
    assert_eq!(scan.implementations.len(), 1, "the filter it took in sits on one registered seam");
    assert_eq!(scan.implementations[0].seam, "chunk.compaction");
    assert_eq!(scan.implementations[0].name, "never");
}

/// Pinning a seam under an operator takes the reference marker off that operator and no other.
///
/// What the flag was supposed to do all along and could not, because it was set to true on every
/// operator whatever had run. It is read off the scan because the scan is what applies the filter
/// the compaction seam belongs to, and the seams of a filter that moved down have to move down with
/// it: the scan's row is then the only row in the document, and a seam reported nowhere is a seam
/// nobody can tell ran. Reading the document back to check a pinned compaction strategy answered no
/// on every query whose filter moved into the scan, which is most of ClickBench.
#[test]
fn an_operator_that_was_pinned_off_the_reference_stops_being_marked_as_one() {
    let db = database();
    db.execute("SET seam_chunk_compaction = 'learned-gain'").unwrap();
    let result = db.query("SELECT x FROM t WHERE x > 1").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let scan = operator(metrics, "Scan");
    assert!(!scan.reference_impl, "{:?}", scan.implementations);
    assert_eq!(scan.implementations[0].seam, "chunk.compaction");
    assert_eq!(scan.implementations[0].name, "learned-gain");
    assert!(
        operator(metrics, "Project").reference_impl,
        "a projection sits on no registered seam, and pinning one under the scan is not about it"
    );
}

/// The ids of a query with nothing pushed anywhere, which is where they run consecutively.
///
/// A query with a filter over a stored table leaves a gap instead, because the filter node keeps
/// the number the plan gave it and never gets an operator. That is the test below this one. This
/// one is about the promise the numbering makes on its own, so it asks for a plan where every node
/// became an operator. It groups rather than counting the lot, because an ungrouped count is
/// answered out of the stored summary and there is then no scan in the query to number.
#[test]
fn every_operator_has_its_own_id_and_a_parent_is_numbered_before_its_children() {
    let db = database();
    let result = db.query("SELECT count(*) FROM t GROUP BY x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let ids: Vec<u32> = metrics.operators.iter().map(|operator| operator.id).collect();
    assert_eq!(ids, (0..u32::try_from(ids.len()).unwrap()).collect::<Vec<_>>());
    let scan = operator(metrics, "Scan");
    let aggregate = operator(metrics, "Aggregate");
    assert!(aggregate.id < scan.id, "the aggregate is above the scan, so it is numbered first");
}

/// A filter the scan applied itself has no row in the document, and leaves its number unused.
///
/// The numbering comes from the plan and the plan still has the filter node in it, so the operators
/// that were built keep the numbers the plan gave them and the one that was not built leaves a gap.
/// Rising and unique is what the numbers promise, not consecutive, and a reader that wants the node
/// behind an id finds it whether or not its neighbour was built.
#[test]
fn a_filter_the_scan_applied_has_no_operator_and_leaves_its_number_unused() {
    let db = database();
    let result = db.query("SELECT count(*) FROM t WHERE x > 1").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    assert!(
        !metrics.operators.iter().any(|operator| operator.kind == "Filter"),
        "{:?}",
        metrics.operators
    );
    let ids: Vec<u32> = metrics.operators.iter().map(|operator| operator.id).collect();
    assert!(ids.windows(2).all(|pair| pair[0] < pair[1]), "the ids still rise: {ids:?}");
    assert_eq!(operator(metrics, "Scan").rows_out, 2, "the scan produced the rows that passed");
}

#[test]
fn a_sort_is_a_pipeline_that_the_one_above_it_waits_for() {
    let db = database();
    let result = db.query("SELECT x FROM t ORDER BY x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let sort = operator(metrics, "Sort");
    assert_eq!(metrics.pipelines.len(), 2, "a sort breaks the pipeline in two");
    assert_eq!(metrics.pipelines[0].id, 0);
    assert_eq!(metrics.pipelines[0].depends_on, vec![1], "the root waits for the sort");
    assert_eq!(sort.pipeline, 1, "the sort ends the pipeline below");
    assert_eq!(sort.rows_in, 4, "every row went into it");
    assert!(metrics.pipelines[1].wall_ns > 0, "a pipeline's time is its operators' time");
}

#[test]
fn a_join_is_three_pipelines_in_the_order_they_have_to_run() {
    let db = database();
    let result = db.query("SELECT t.x FROM t JOIN t AS u ON t.x = u.x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    assert_eq!(metrics.pipelines.len(), 3, "one to gather the build side, one to probe, one above");
    let gather = operator(metrics, "Gather");
    let probe = operator(metrics, "Probe");
    assert_eq!(
        metrics.pipelines[0].depends_on,
        vec![probe.pipeline],
        "the root waits for the probe"
    );
    assert_eq!(
        metrics.pipelines[usize::try_from(probe.pipeline).unwrap()].depends_on,
        vec![gather.pipeline],
        "the probe waits for the side that is gathered first"
    );
    assert_eq!(gather.rows_in, 4, "the whole right side was gathered");
    // The driving side is not gathered, which is the whole of what the streaming probe bought.
    // Three pipelines either way, and the difference is that the third one now has the scan in it
    // rather than waiting for one to finish before this one starts on its rows.
    let driving = metrics
        .operators
        .iter()
        .filter(|operator| operator.kind == "Scan")
        .find(|scan| scan.pipeline == probe.pipeline)
        .expect("the driving scan is in the probe's own pipeline");
    assert_eq!(driving.rows_out, 4, "every driving row went straight into the probe");
}

/// The name in the profile says which of the two operators answered, because they cost very
/// different things and a profile that called both of them a join would not let anybody tell.
#[test]
fn a_join_no_lookup_answers_says_so_in_the_profile() {
    let db = database();
    let result = db.query("SELECT t.x FROM t JOIN t AS u ON t.x < u.x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    operator(metrics, "Join");
    assert!(
        !metrics.operators.iter().any(|operator| operator.kind == "Probe"),
        "a range condition is not a lookup"
    );
}

/// The one edge in the document. Without it a reader can add up what a query moved and cannot
/// check that any of it adds up, because the check is an operator's input against what fed it.
#[test]
fn every_operator_but_the_one_that_answers_names_what_consumed_its_rows() {
    let db = database();
    let result = db.query("SELECT sum(x) FROM (SELECT x FROM t WHERE x > 1) ORDER BY 1").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let roots: Vec<u32> = metrics
        .operators
        .iter()
        .filter(|operator| operator.parent.is_none())
        .map(|operator| operator.id)
        .collect();
    assert_eq!(roots, vec![0], "one operator produces the answer and it is the first one");
    for operator in &metrics.operators {
        let Some(parent) = operator.parent else { continue };
        let parent = metrics
            .operators
            .iter()
            .find(|other| other.id == parent)
            .expect("a parent is an operator in the same document");
        assert!(parent.id < operator.id, "a parent is numbered before everything under it");
    }
}

/// A parent id pointing at an operator with no row is worse than no parent id, because a reader
/// walking up the chain stops there and reports a gap it cannot tell from a missing measurement.
/// A node folded into another is what puts a hole in the numbering, so the shapes below are the
/// ones where that happens: a filter taken into a scan, a join, a set operation, a materialised
/// `WITH` and a window.
#[test]
fn no_operator_hangs_under_a_row_that_is_not_there() {
    let db = database();
    for sql in [
        "SELECT x FROM t WHERE x > 1",
        "SELECT sum(x) FROM t WHERE x > 1 GROUP BY s ORDER BY 1",
        "SELECT t.x FROM t JOIN t AS u ON t.x = u.x WHERE t.x > 1",
        "SELECT t.x FROM t LEFT JOIN t AS u ON t.x = u.x",
        "SELECT x FROM t WHERE x IN (SELECT x FROM t WHERE x > 1)",
        "SELECT x FROM t UNION SELECT x FROM t",
        "WITH c AS MATERIALIZED (SELECT x FROM t WHERE x > 1) SELECT x FROM c ORDER BY 1",
        "SELECT x, count(*) OVER (PARTITION BY s) FROM t",
        "SELECT x FROM t ORDER BY 1 LIMIT 2",
        "SELECT a.x FROM t AS a, t AS b WHERE a.x > b.x",
    ] {
        let result = db.query(sql).unwrap_or_else(|error| panic!("{sql} did not run: {error}"));
        let metrics = result.metrics().expect("a query that ran has metrics");
        // A materialisation is filled and read back rather than handed upwards, so it is an
        // operator with nothing above it and a query holding one has two.
        let held = metrics.operators.iter().filter(|one| one.kind.contains("CTE")).count();
        let roots = metrics.operators.iter().filter(|one| one.parent.is_none()).count();
        assert_eq!(roots, 1 + held, "{sql} has one operator the answer is read from");
        for one in &metrics.operators {
            let Some(parent) = one.parent else { continue };
            assert!(
                metrics.operators.iter().any(|other| other.id == parent),
                "{sql}: operator {} hangs under {parent}, which has no row",
                one.id
            );
        }
    }
}

/// The check the harness runs, run here so that a shape which stops adding up is a failing test
/// rather than a suite that refuses to publish a number weeks later.
///
/// Every row an operator was handed came out of the operators under it, so the two counts are the
/// same rows counted at the two ends of one handover and there is no tolerance on it. The shapes
/// that have broken it are the two the operator tree is not the plan tree in: a join whose build
/// side the optimizer turned around, and a cross product, which is called again with what is left of
/// its own output and would otherwise count that as input.
#[test]
fn what_an_operator_was_handed_is_what_the_operators_under_it_produced() {
    let db = database();
    for sql in [
        "SELECT x FROM t WHERE x > 1",
        "SELECT sum(x) FROM t WHERE x > 1 GROUP BY s ORDER BY 1",
        "SELECT t.x FROM t JOIN t AS u ON t.x = u.x WHERE t.x > 1",
        "SELECT t.x FROM t LEFT JOIN t AS u ON t.x = u.x",
        "SELECT t.x FROM t JOIN t AS u ON t.x > u.x AND t.x < u.x + 5",
        "SELECT x FROM t WHERE x IN (SELECT x FROM t WHERE x > 1)",
        "SELECT x FROM t UNION SELECT x FROM t",
        "WITH c AS MATERIALIZED (SELECT x FROM t WHERE x > 1) SELECT x FROM c ORDER BY 1",
        "SELECT x, count(*) OVER (PARTITION BY s) FROM t",
        "SELECT a.x FROM t AS a, t AS b WHERE a.x > b.x",
    ] {
        let result = db.query(sql).unwrap_or_else(|error| panic!("{sql} did not run: {error}"));
        let metrics = result.metrics().expect("a query that ran has metrics");
        for one in &metrics.operators {
            let below: u64 = metrics
                .operators
                .iter()
                .filter(|other| other.parent == Some(one.id))
                .map(|other| other.rows_out)
                .sum();
            if !metrics.operators.iter().any(|other| other.parent == Some(one.id)) {
                continue;
            }
            assert_eq!(
                one.rows_in, below,
                "{sql}: operator {} ({}) was handed {} rows and the operators under it made {below}",
                one.id, one.kind, one.rows_in
            );
        }
    }
}

/// The gathered side feeds the operator that holds it, which is the one place the operator tree is
/// a different shape than the plan. A reader that walked the plan instead would compare the join's
/// input against rows the join never saw.
#[test]
fn the_side_a_join_gathers_hangs_under_the_operator_that_holds_it() {
    let db = database();
    let result = db.query("SELECT t.x FROM t JOIN t AS u ON t.x = u.x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let gather = operator(metrics, "Gather");
    let probe = operator(metrics, "Probe");
    assert_eq!(gather.parent, Some(probe.id), "the held side is handed to the join");
    let held = metrics
        .operators
        .iter()
        .find(|other| other.parent == Some(gather.id))
        .expect("something fills the gather");
    assert_eq!(held.rows_out, gather.rows_in, "and what it produced is what the gather took");
}

/// A join is the one operator with two inputs and one row in the document, so its own row counts
/// the driving side alone. Without this the gathered side, which is the half of the query most of
/// the memory and most of the surprises are in, is not in the document at all.
#[test]
fn a_join_says_what_it_built_and_what_it_chose() {
    let db = database();
    let result = db.query("SELECT t.x FROM t JOIN t AS u ON t.x = u.x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let joined = operator(metrics, "Probe").joined.clone().expect("a join reports what it did");
    assert_eq!(joined.algorithm, rudb_metrics::Algorithm::Hash);
    assert_eq!(joined.build_rows, 4, "the whole gathered side went into the table");
    assert!(joined.build_bytes > 0, "a table that holds four rows cost something to hold them");
    let declined = &joined.declined;
    assert_eq!(declined.len(), 1, "the nested loop was the only other answer");
    assert_eq!(declined[0].algorithm, rudb_metrics::Algorithm::Loop);
    assert!(declined[0].reason.contains("equality"), "{}", declined[0].reason);
}

/// The reason is the point of recording it. A nested loop over two sides is the two multiplied, and
/// the fix is nearly always a condition the binder could not find an equality in, which is a thing
/// somebody reading a slow query has to be told rather than left to guess from the time.
#[test]
fn a_join_with_no_lookup_says_why_it_had_to_walk_every_pair() {
    let db = database();
    let result = db.query("SELECT t.x FROM t JOIN t AS u ON t.x < u.x").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    let joined = operator(metrics, "Join").joined.clone().expect("a join reports what it did");
    assert_eq!(joined.algorithm, rudb_metrics::Algorithm::Loop);
    assert_eq!(joined.build_rows, 4, "the gathered side is walked once per driving row");
    assert_eq!(joined.declined.len(), 1, "the table was the only other answer");
    assert_eq!(joined.declined[0].algorithm, rudb_metrics::Algorithm::Hash);
    assert!(joined.declined[0].reason.contains("no conjunct"), "{}", joined.declined[0].reason);
}

/// Everything that is not a join leaves the key out rather than writing a null into it, because the
/// rest of the plan is most of the plan and a reader filtering for joins should not have to know
/// which of the null shaped rows are one.
#[test]
fn an_operator_that_is_not_a_join_has_no_join_record() {
    let db = database();
    let result = db.query("SELECT sum(x) FROM t WHERE x > 1").unwrap();
    let metrics = result.metrics().expect("a query that ran has metrics");
    assert!(
        metrics.operators.iter().all(|operator| operator.joined.is_none()),
        "no operator in this query has two inputs"
    );
}

#[test]
fn a_statement_that_runs_no_plan_has_nothing_to_report() {
    let db = database();
    assert!(db.query("EXPLAIN SELECT 1").unwrap().metrics().is_none(), "explain runs nothing");
    assert!(db.execute("SET threads = 2").unwrap().metrics().is_none(), "a setting runs nothing");
}

#[test]
fn the_document_a_query_produces_is_the_json_a_harness_reads() {
    let db = database();
    // Filtered so that the query actually reads the rows. `SELECT count(*) FROM t` is answered out
    // of the table's statistics and has no scan in it to report, which is a different thing for this
    // test to be about.
    let sql = "SELECT count(*) FROM t WHERE x > 1";
    let result = db.query(sql).unwrap();
    let written = result.metrics().expect("a query that ran has metrics").render();
    assert!(written.starts_with("{\n  \"schema\": 1,"), "{written}");
    assert!(written.contains(&format!("\"sql\": \"{sql}\"")), "{written}");
    assert!(written.contains("\"kind\": \"Scan\""), "{written}");
    assert!(written.contains("\"reference_impl\": true"), "{written}");
}

#[test]
fn related_integer_sums_keep_null_and_empty_rules() {
    let db = database();
    let query = "SELECT sum(x), sum(x + 1) FROM \
                 (VALUES (1::SMALLINT), (NULL::SMALLINT), (3::SMALLINT)) AS v(x)";
    assert_eq!(rows(&db, query), vec![vec![Value::HugeInt(4), Value::HugeInt(6)]]);
    let empty = format!("{query} WHERE false");
    assert_eq!(rows(&db, &empty), vec![vec![Value::Null, Value::Null]]);
}

/// A database that will run a query on `threads` of them, whatever the machine has.
///
/// Pinned rather than left at the default, because the default is however many cores the test
/// runner happens to have and a parallel test that runs on one core proves nothing. Eight is enough
/// to shuffle the order of anything that depends on it and small enough not to matter on a busy
/// machine.
fn threaded(threads: usize) -> Database {
    Database::with_config(Config::new().with_threads(threads).unwrap())
}

#[test]
fn a_query_on_eight_threads_answers_what_it_answers_on_one() {
    let sql = "SELECT count(*), sum(range), min(range), max(range) \
               FROM range(1000000) WHERE range % 7 = 0";
    assert_eq!(rows(&threaded(8), sql), rows(&threaded(1), sql));
}

#[test]
fn a_group_by_on_eight_threads_finds_every_group_exactly_once() {
    let sql = "SELECT range % 1000 AS k, count(*), sum(range) FROM range(1000000) GROUP BY k";
    let mut many = rows(&threaded(8), sql);
    let mut one = rows(&threaded(1), sql);
    many.sort_by_key(|row| format!("{:?}", row[0]));
    one.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(many.len(), 1000);
    assert_eq!(many, one);
}

/// The nine ClickBench queries that did not get faster when pipelines learned to run wide all have
/// one of these in them, because the aggregate refused a second instance and the refusal dropped the
/// scan under it back to one thread. See #509.
#[test]
fn a_count_distinct_on_eight_threads_counts_each_value_once() {
    let sql = "SELECT count(DISTINCT range % 977) FROM range(200000)";
    assert_eq!(rows(&threaded(8), sql), [vec![Value::BigInt(977)]]);
    assert_eq!(rows(&threaded(8), sql), rows(&threaded(1), sql));
}

/// Distinct sets merge by offering their values to the kept set, so an aggregate other than a count
/// gets the same answer rather than only the counting one being right.
#[test]
fn every_distinct_aggregate_on_eight_threads_answers_what_it_answers_on_one() {
    let sql = "SELECT sum(DISTINCT range % 1009), min(DISTINCT range % 1009), \
               max(DISTINCT range % 1009), count(DISTINCT range % 1009) FROM range(300000)";
    let total: i64 = (0..1009i64).sum();
    assert_eq!(
        rows(&threaded(8), sql),
        [vec![
            Value::HugeInt(i128::from(total)),
            Value::BigInt(0),
            Value::BigInt(1008),
            Value::BigInt(1009)
        ]]
    );
    assert_eq!(rows(&threaded(8), sql), rows(&threaded(1), sql));
}

/// A distinct count inside a group by, where the sets being merged belong to groups that may or may
/// not be in the table they are merged into.
#[test]
fn a_grouped_count_distinct_on_eight_threads_finds_every_group_and_every_value() {
    let sql = "SELECT range % 13 AS k, count(DISTINCT range % 91), count(*) \
               FROM range(400000) GROUP BY k";
    let mut many = rows(&threaded(8), sql);
    let mut one = rows(&threaded(1), sql);
    many.sort_by_key(|row| format!("{:?}", row[0]));
    one.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(many.len(), 13);
    for row in &many {
        assert_eq!(row[1], Value::BigInt(7), "91 over 13 is seven values in each group");
    }
    assert_eq!(many, one);
}

/// A distinct over a string, which takes the other set in the operator, since a BIGINT argument gets
/// a set of integers and everything else gets a set of rows.
#[test]
fn a_count_distinct_over_strings_on_eight_threads_counts_each_string_once() {
    let sql = "SELECT count(DISTINCT 'tag' || (range % 641)) FROM range(200000)";
    assert_eq!(rows(&threaded(8), sql), [vec![Value::BigInt(641)]]);
    assert_eq!(rows(&threaded(8), sql), rows(&threaded(1), sql));
}

/// Eight instances of a group by, each row landing in the partition its key hashes to.
///
/// Every instance sees part of every group, so each group is reached from eight threads and has to
/// come out holding all eight contributions exactly once. What the answer checks is that the split
/// loses nothing and doubles nothing: the number of groups, the counts and the sums all come out as
/// if one thread had done it.
#[test]
fn a_high_cardinality_group_by_on_eight_threads_merges_to_the_same_answer() {
    let sql = "SELECT range % 50000 AS g, count(*), sum(range) FROM range(400000) GROUP BY g";
    let many = rows(&threaded(8), sql);
    assert_eq!(many.len(), 50_000);
    let mut sorted = many.clone();
    sorted.sort_by_key(|row| match row[0] {
        Value::BigInt(key) => key,
        _ => unreachable!("the key is a BIGINT"),
    });
    assert_eq!(sorted[0], vec![Value::BigInt(0), Value::BigInt(8), Value::HugeInt(1_400_000)]);
    let mut one = rows(&threaded(1), sql);
    one.sort_by_key(|row| match row[0] {
        Value::BigInt(key) => key,
        _ => unreachable!("the key is a BIGINT"),
    });
    assert_eq!(sorted, one);
}

/// A distinct set split across the partitions, where the sets are the expensive half of a group.
///
/// Enough groups to be spread over every partition, and a `DISTINCT` inside each one so that what a
/// partition accumulates is a set rather than just a running total. Each group holds three values
/// and every instance offers it some of all three.
#[test]
fn a_grouped_count_distinct_over_many_groups_on_eight_threads_agrees_with_one_thread() {
    let sql = "SELECT range % 30000 AS k, count(DISTINCT range % 90000), count(*) \
               FROM range(900000) GROUP BY k";
    let many = rows(&threaded(8), sql);
    assert_eq!(many.len(), 30_000);
    for row in &many {
        assert_eq!(row[1], Value::BigInt(3), "90000 over 30000 is three values in each group");
        assert_eq!(row[2], Value::BigInt(30), "900000 over 30000 is thirty rows in each group");
    }
    let mut sorted = many;
    let mut one = rows(&threaded(1), sql);
    sorted.sort_by_key(|row| format!("{:?}", row[0]));
    one.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(sorted, one);
}

/// A group by on eight threads whose budget makes it spill on the way to partitioning.
///
/// The awkward case, which is an instance whose own table runs out of room before it has handed its
/// groups to the partitions. Its spill file covers every partition, so it cannot be scattered, and
/// what happens instead is that the groups still in the table are scattered and the file is read
/// back and spread row by row. Both halves have to land, and each row has to land once.
///
/// Three hundred megabytes against a million groups is the same boundary
/// `a_group_by_too_large_for_its_budget_spills_rather_than_stopping` sits on, with eight threads
/// under it so that the instances race for the budget rather than taking it in turn.
#[test]
fn a_group_by_that_spills_on_eight_threads_answers_what_it_answers_on_one() {
    let sql = "SELECT range % 900000 AS k, count(*), sum(range) FROM range(1800000) GROUP BY k";
    let db = Database::with_config(
        Config::new().with_memory_limit(300 << 20).with_threads(8).expect("eight threads"),
    );
    let answer = db.query(sql).expect("it spills rather than stopping");
    let mut many: Vec<Vec<Value>> = answer.rows().collect();
    assert_eq!(many.len(), 900_000);
    let mut one = rows(&threaded(1), sql);
    many.sort_by_key(|row| first_key(row));
    one.sort_by_key(|row| first_key(row));
    assert_eq!(many, one, "every row landed once whether it went through a spill file or not");
}

/// The same over a string key, since a partition gathers its keys out of the chunk before folding.
#[test]
fn a_group_by_a_string_with_many_groups_on_eight_threads_agrees_with_one_thread() {
    let sql = "SELECT 'k' || (range % 25000) AS k, count(*), min(range), max(range) \
               FROM range(500000) GROUP BY k";
    let many = rows(&threaded(8), sql);
    assert_eq!(many.len(), 25_000);
    let mut sorted = many;
    let mut one = rows(&threaded(1), sql);
    sorted.sort_by_key(|row| format!("{:?}", row[0]));
    one.sort_by_key(|row| format!("{:?}", row[0]));
    assert_eq!(sorted, one);
}

#[test]
fn a_query_with_no_order_by_keeps_the_source_order_on_eight_threads() {
    let rows = rows(&threaded(8), "SELECT range FROM range(200000) WHERE range % 3 = 0");
    let read: Vec<Value> = rows.into_iter().map(|row| row[0].clone()).collect();
    let expected: Vec<Value> = (0..200_000i64).step_by(3).map(Value::BigInt).collect();
    assert_eq!(read, expected, "the scan was cut into morsels and put back together in order");
}

#[test]
fn an_order_by_on_eight_threads_is_still_in_order() {
    let rows = rows(
        &threaded(8),
        "SELECT range FROM range(100000) WHERE range % 1000 = 0 ORDER BY range DESC LIMIT 5",
    );
    assert_eq!(
        rows,
        vec![
            vec![Value::BigInt(99000)],
            vec![Value::BigInt(98000)],
            vec![Value::BigInt(97000)],
            vec![Value::BigInt(96000)],
            vec![Value::BigInt(95000)],
        ]
    );
}

#[test]
fn a_query_on_several_threads_says_so_in_its_metrics() {
    let db = threaded(8);
    let result =
        db.query("SELECT count(*), sum(range) FROM range(2000000) WHERE range % 7 = 0").unwrap();
    let metrics = result.metrics().unwrap();
    assert_eq!(metrics.settings.threads, 8, "the document says what the query was allowed");
    let widest = metrics.pipelines.iter().map(|pipeline| pipeline.instances).max().unwrap();
    assert_eq!(widest, 8, "and the scan says it used all of them");
    if rudb_metrics::thread_cpu_ns().is_some() {
        assert!(metrics.resource.cpu_ns > 0);
    }
}

#[test]
fn setting_threads_changes_what_the_next_query_may_use() {
    let db = threaded(8);
    db.execute("SET threads = 1").unwrap();
    assert_eq!(db.config().threads(), 1);
    let metrics = db.query("SELECT count(*) FROM range(1000000)").unwrap();
    let metrics = metrics.metrics().unwrap();
    assert_eq!(metrics.settings.threads, 1);
    let widest = metrics.pipelines.iter().map(|pipeline| pipeline.instances).max().unwrap();
    assert_eq!(widest, 1, "a setting nothing obeys is not a setting");
}

/// A group by whose instances spill before they partition, so the spill files are handed over too.
///
/// The awkward case. An instance that runs out of budget while it still holds its groups to itself
/// writes the rows it had no room for to a file, and that file covers every partition rather than
/// one of them. So when the instance does partition, the groups it still holds are scattered and
/// the file is read back and spread row by row. Both halves have to land, and each row exactly once.
///
/// Ten thousand groups is under the threshold, so what tips this instance into partitioning is the
/// budget rather than the group count, which is the only way to reach the case. The distinct set is
/// what fills the budget at so few groups: fifty values per group is fifty allocations per group
/// where a plain count is one counter. Twenty four megabytes is in the middle of the band that
/// crowds without running out, which on the machine this was written on runs from twelve to thirty
/// two. A budget outside the band still gives the right answer, it just covers less.
#[test]
fn a_group_by_that_spills_before_it_partitions_lands_the_file_and_the_table() {
    let sql = "SELECT range % 10000 AS k, count(DISTINCT range), count(*), sum(range) \
               FROM range(500000) GROUP BY k";
    let db = Database::with_config(
        Config::new().with_memory_limit(24 << 20).with_threads(8).expect("eight threads"),
    );
    let answer = db.query(sql).expect("it spills rather than stopping");
    let mut many: Vec<Vec<Value>> = answer.rows().collect();
    assert_eq!(many.len(), 10_000);
    let mut one = rows(&threaded(1), sql);
    many.sort_by_key(|row| first_key(row));
    one.sort_by_key(|row| first_key(row));
    assert_eq!(many, one, "every row landed once whether it came back from a file or not");
}

/// A table built from a query keeps whatever form the query produced, and a caller still gets flat
/// columns because a result set is the thing that flattens rather than the thing that stores.
///
/// The one that matters is the first assertion. Draining a query happens on one thread, so a copy
/// made there is a copy made while the rest of the pool has nothing to do, and on a wide load it is
/// most of what the statement costs. A dictionary column is the case where the copy is largest,
/// because flattening it writes one value per row out of a body that held one per distinct value.
#[test]
fn a_table_built_from_a_query_keeps_the_dictionary_the_query_produced() {
    let db = Database::new();
    db.execute("CREATE TABLE src AS SELECT range % 4 AS k FROM range(4096)").expect("src builds");
    // A filter over a small distinct set is what leaves a dictionary behind, which is the same
    // thing a parquet scan of a dictionary encoded column hands up.
    db.execute("CREATE TABLE kept AS SELECT k FROM src WHERE k < 3").expect("kept builds");
    let forms = db.with_catalog(|catalog| {
        let name = rudb_catalog::QualifiedName::new("memory", "main", "kept");
        let table = catalog.table(&name).expect("the table is there");
        (0..table.rows().chunk_count())
            .filter_map(|at| table.rows().chunk(at))
            .map(|chunk| chunk.column(0).expect("one column").form())
            .collect::<Vec<_>>()
    });
    assert!(!forms.is_empty(), "the table has chunks");
    assert!(
        forms.iter().any(|&form| form != rudb_vector::Form::Flat),
        "every chunk was flattened on the way in, so the drain still pays for a copy nobody wants"
    );
    let answer = db.query("SELECT count(*), sum(k) FROM kept").expect("it reads back");
    let rows: Vec<Vec<Value>> = answer.rows().collect();
    assert_eq!(rows, vec![vec![Value::BigInt(3072), Value::HugeInt(3072)]]);
}

#[test]
fn a_file_backed_insert_streams_into_a_snapshot_that_a_new_process_can_read() {
    let path = std::env::temp_dir().join(format!(
        "rudb-native-checkpoint-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let database = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the new native database opens");
    database.execute("CREATE TABLE hits (id INTEGER, name VARCHAR)").expect("the table is made");
    database
        .execute("INSERT INTO hits VALUES (1, 'one'), (2, NULL), (3, 'three')")
        .expect("the rows are inserted");
    assert!(path.exists(), "the insert publishes its snapshot");
    assert!(database.with_catalog(|catalog| {
        let name = rudb_catalog::QualifiedName::new("memory", "main", "hits");
        catalog.table(&name).expect("the table is there").rows().is_native()
    }));
    database.execute("CHECKPOINT").expect("checkpoint sees an already committed snapshot");
    drop(database);

    let reopened = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the committed native database reopens");
    assert_eq!(
        rows(&reopened, "SELECT count(*), sum(id), min(name) FROM hits"),
        vec![vec![Value::BigInt(3), Value::HugeInt(6), Value::Varchar("one".into())]]
    );
    std::fs::remove_file(path).expect("the temporary native database is removed");
}

/// A schema committed by an earlier session is still loaded as a stream rather than through memory.
///
/// What says whether a table already in the file is in the way of the one being written is how many
/// rows it holds, and one that holds none is not in the way: the generation being written takes its
/// place in the catalog. Before that was asked, the name alone was enough to refuse, so a schema
/// created in one session and loaded in the next took the in-memory path and the load needed memory
/// the size of the table. That is not a corner. `CREATE TABLE` in one statement and `INSERT INTO
/// ... SELECT` in the next is what every loading script writes, ClickBench's included.
///
/// Native rather than in memory is the observable, and it is the whole of the difference. A load
/// that streamed has its rows in the file the moment the statement returns and nothing of the table
/// in memory. A load that did not is holding all of them and reaches the file at the next
/// checkpoint.
#[test]
fn an_insert_into_a_committed_empty_table_streams_into_the_file() {
    let path = std::env::temp_dir().join(format!(
        "rudb-native-empty-reload-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let database = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the new native database opens");
    database.execute("CREATE TABLE hits (id INTEGER, name VARCHAR)").expect("the table is made");
    database.execute("CHECKPOINT").expect("the empty table is committed on its own");
    drop(database);

    let reopened = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the committed empty table reopens");
    reopened
        .execute("INSERT INTO hits VALUES (1, 'one'), (2, NULL), (3, 'three')")
        .expect("the rows are inserted");
    assert!(reopened.with_catalog(|catalog| {
        let name = rudb_catalog::QualifiedName::new("memory", "main", "hits");
        catalog.table(&name).expect("the table is there").rows().is_native()
    }));
    drop(reopened);

    let again = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the appended generation reopens");
    assert_eq!(
        rows(&again, "SELECT count(*), sum(id), min(name) FROM hits"),
        vec![vec![Value::BigInt(3), Value::HugeInt(6), Value::Varchar("one".into())]]
    );
    std::fs::remove_file(path).expect("the temporary native database is removed");
}

/// The same load with a table beside it in the file, whose rows the new generation carries forward.
///
/// The generation being written names every table the file will hold, so the ones it is not writing
/// are carried by their directory pointer and their pages are not read. The check that they are all
/// there is what keeps a table with rows only in memory from being dropped by a generation that
/// never knew about it, and taking the empty target out of the file's side of that comparison is
/// the part of this that could go wrong quietly.
#[test]
fn a_committed_empty_table_streams_beside_a_table_that_holds_rows() {
    let path = std::env::temp_dir().join(format!(
        "rudb-native-empty-neighbour-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let database = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the new native database opens");
    database.execute("CREATE TABLE kept (id INTEGER)").expect("the neighbour is made");
    database.execute("INSERT INTO kept VALUES (7), (8)").expect("the neighbour gets rows");
    database.execute("CREATE TABLE hits (id INTEGER, name VARCHAR)").expect("the target is made");
    database.execute("CHECKPOINT").expect("both tables are committed");
    drop(database);

    let reopened = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the committed pair reopens");
    reopened.execute("INSERT INTO hits VALUES (1, 'one'), (2, 'two')").expect("the rows go in");
    assert!(reopened.with_catalog(|catalog| {
        let name = rudb_catalog::QualifiedName::new("memory", "main", "hits");
        catalog.table(&name).expect("the table is there").rows().is_native()
    }));
    drop(reopened);

    let again = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the appended generation reopens");
    assert_eq!(rows(&again, "SELECT count(*) FROM hits"), vec![vec![Value::BigInt(2)]]);
    // The neighbour is the half that a wrong carry would lose, and it is still all there.
    assert_eq!(
        rows(&again, "SELECT count(*), sum(id) FROM kept"),
        vec![vec![Value::BigInt(2), Value::HugeInt(15)]]
    );
    std::fs::remove_file(path).expect("the temporary native database is removed");
}

/// A temporary table never reaches the file, so reopening the file does not find one.
///
/// This is the part that would be quietly wrong rather than loudly wrong: a checkpoint that wrote
/// the temporary table would leave a table in the file that nobody created, and it would come back
/// as a stored table the next time the file was opened.
#[test]
fn a_temporary_table_is_not_written_into_a_file_backed_database() {
    let path = std::env::temp_dir().join(format!(
        "rudb-native-temporary-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let database = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the new native database opens");
    database.execute("CREATE TABLE kept (id INTEGER)").expect("the stored table is made");
    database.execute("INSERT INTO kept VALUES (1), (2)").expect("the stored rows go in");
    database
        .execute("CREATE TEMPORARY TABLE gone (id INTEGER)")
        .expect("the temporary table is made");
    database.execute("INSERT INTO gone VALUES (3), (4), (5)").expect("the temporary rows go in");
    assert_eq!(rows(&database, "SELECT count(*) FROM gone"), vec![vec![Value::BigInt(3)]]);
    database.execute("CHECKPOINT").expect("the checkpoint writes the stored table only");
    drop(database);

    let reopened = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the committed native database reopens");
    assert_eq!(rows(&reopened, "SELECT count(*) FROM kept"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(
        rows(&reopened, "SELECT table_name FROM duckdb_tables() ORDER BY table_name"),
        vec![vec![text("kept")]]
    );
    std::fs::remove_file(path).expect("the temporary native database is removed");
}

#[test]
fn a_native_frequency_synopsis_answers_count_topn() {
    let path = std::env::temp_dir().join(format!(
        "rudb-native-frequency-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let database = Database::open(path.to_str().expect("a UTF-8 temporary path"))
        .expect("the new native database opens");
    database.execute("CREATE TABLE hits (id BIGINT)").expect("the table is made");
    database
        .execute(
            "INSERT INTO hits SELECT CASE WHEN range < 1000 THEN 0 WHEN range < 1500 THEN 1 \
             WHEN range < 1750 THEN 2 ELSE range END FROM range(5000)",
        )
        .expect("the rows are inserted");
    assert_eq!(
        rows(&database, "SELECT id, count(*) AS c FROM hits GROUP BY id ORDER BY c DESC LIMIT 3",),
        vec![
            vec![Value::BigInt(0), Value::BigInt(1000)],
            vec![Value::BigInt(1), Value::BigInt(500)],
            vec![Value::BigInt(2), Value::BigInt(250)],
        ]
    );
    assert_eq!(
        rows(
            &database,
            "SELECT id, id - 1, id - 2, count(*) AS c FROM hits \
             GROUP BY id, id - 1, id - 2 ORDER BY c DESC LIMIT 3",
        ),
        vec![
            vec![Value::BigInt(0), Value::BigInt(-1), Value::BigInt(-2), Value::BigInt(1000)],
            vec![Value::BigInt(1), Value::BigInt(0), Value::BigInt(-1), Value::BigInt(500)],
            vec![Value::BigInt(2), Value::BigInt(1), Value::BigInt(0), Value::BigInt(250)],
        ]
    );
    drop(database);
    std::fs::remove_file(path).expect("the temporary native database is removed");
}

#[test]
fn a_join_a_certificate_says_changes_nothing_is_deleted_and_the_row_counts_agree() {
    let path = std::env::temp_dir().join(format!(
        "rudb-graph-eliminate-{}-{}.rdb",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("the clock advances")
            .as_nanos()
    ));
    let db = Database::open(path.to_str().expect("a UTF-8 temporary path")).unwrap();
    db.execute("CREATE TABLE customer (c_custkey INTEGER, c_name VARCHAR)").unwrap();
    db.execute("CREATE TABLE orders (o_orderkey INTEGER, o_custkey INTEGER)").unwrap();
    db.execute("CREATE TABLE returns (r_orderkey INTEGER, r_custkey INTEGER)").unwrap();
    db.execute("INSERT INTO customer SELECT i, 'c' || i FROM range(1, 4001) AS r(i)").unwrap();
    db.execute("INSERT INTO orders SELECT i, 1 + i % 4000 FROM range(1, 10001) AS r(i)").unwrap();
    // The same, and then one row whose customer does not exist, which is what costs a relationship
    // its totality certificate and so its licence to have its join deleted.
    db.execute("INSERT INTO returns SELECT i, 1 + i % 4000 FROM range(1, 10001) AS r(i)").unwrap();
    db.execute("INSERT INTO returns VALUES (99999, 99999)").unwrap();
    db.execute(
        "SET graph_links = 'orders(o_custkey) -> customer(c_custkey), \
         returns(r_custkey) -> customer(c_custkey)'",
    )
    .unwrap();
    db.execute("CHECKPOINT").unwrap();
    db.execute("SET graph_sections = 'on'").unwrap();

    let explained = |sql: &str| match db
        .query(&format!("EXPLAIN {sql}"))
        .expect("the explain ran")
        .value_at(0, 1)
    {
        Value::Varchar(text) => text,
        other => panic!("the plan came back as {other:?}"),
    };

    // Every order has a customer and this reads no column of one, so the join is doing nothing at
    // all to the row set and the plan says so by not having one.
    let counted = "SELECT count(*), sum(o_orderkey) FROM orders \
                   JOIN customer ON o_custkey = c_custkey";
    assert!(!explained(counted).contains("Join"), "{}", explained(counted));
    let answer = rows(&db, counted);
    assert_eq!(answer, vec![vec![Value::BigInt(10000), Value::HugeInt(50_005_000)]]);
    // The control, which is the same query in the same process with the rule off. This is the row
    // count that says the rewrite changed nothing, and it is the one G4's fifth exit criterion
    // asks for.
    db.execute("SET stats_join_elimination = false").unwrap();
    assert!(explained(counted).contains("Join"), "the rule is still on");
    assert_eq!(answer, rows(&db, counted), "the join was doing something after all");
    db.execute("SET stats_join_elimination = true").unwrap();

    // One unmatched child row, so the join drops it and deleting the join would not. Which rows a
    // join drops is the thing a certificate is about, and this relationship has no certificate for
    // it, so the join stays.
    let partial = "SELECT count(*) FROM returns JOIN customer ON r_custkey = c_custkey";
    assert!(explained(partial).contains("Join"), "{}", explained(partial));
    assert_eq!(rows(&db, partial), vec![vec![Value::BigInt(10000)]], "the stray row is dropped");

    // And the outer join over the total relationship is an inner join, which is a cheaper operator
    // answering the same question. The parent's column is read here, and read for its value rather
    // than counted, so the join stays and only its kind changes.
    let outer = "SELECT max(c_name) FROM orders LEFT JOIN customer ON o_custkey = c_custkey";
    let plan = explained(outer);
    assert!(plan.contains("Join INNER"), "nothing was padded, so nothing was preserved:\n{plan}");
    assert_eq!(rows(&db, outer), vec![vec![Value::Varchar("c999".into())]]);
    // The one that is not total keeps its left join, and the difference in the answer is the row
    // the left join pads and the inner join would have dropped.
    let partial_outer = "SELECT count(*) FROM returns LEFT JOIN customer ON r_custkey = c_custkey";
    assert!(explained(partial_outer).contains("Join LEFT"), "{}", explained(partial_outer));
    assert_eq!(rows(&db, partial_outer), vec![vec![Value::BigInt(10001)]]);

    drop(db);
    std::fs::remove_file(&path).ok();
}

#[test]
fn the_conjunct_the_statistics_call_selective_runs_first_and_the_answer_does_not_change() {
    // A thousand distinct keys over two thousand rows, so `k = 7` keeps a thousandth, and a string
    // function over a varchar is the dearest thing in the predicate. The order that puts the
    // equality first asks the function about two rows instead of about two thousand.
    let db = Database::new();
    db.execute("CREATE TABLE t (k INTEGER, s VARCHAR)").unwrap();
    db.execute("INSERT INTO t SELECT i % 1000, 'row' || i FROM range(0, 2000) AS r(i)").unwrap();

    let query = "SELECT count(*) FROM t WHERE upper(s) = 'ROW7' AND k = 7";
    let plan = db.plan(query).unwrap();
    let filter = plan.lines().find(|line| line.contains("Filter")).expect("a filter is planned");
    let (first, second) = filter.split_once(" AND ").expect("two conjuncts are printed");
    assert!(first.contains("#0.0"), "the counted equality goes in front:\n{plan}");
    assert!(second.contains("upper"), "and the string function goes behind it:\n{plan}");

    // The control. Reordering a conjunction is a rewrite that has to answer the same question, and
    // the switch is what lets the same query be asked both ways.
    let answer = rows(&db, query);
    db.execute("SET stats_filter_order = false").unwrap();
    let unordered = db.plan(query).unwrap();
    let written = unordered.lines().find(|line| line.contains("Filter")).expect("still a filter");
    assert!(
        written.split_once(" AND ").expect("two conjuncts").0.contains("upper"),
        "with the rule off the predicate is in the order it was written:\n{unordered}"
    );
    assert_eq!(rows(&db, query), answer, "the order the conjuncts run in is not an answer");
    assert_eq!(answer, vec![vec![Value::BigInt(1)]]);
}

#[test]
fn a_decimal_sum_is_as_wide_as_a_decimal_goes() {
    let db = database();
    let one = |sql: &str| rows(&db, sql);
    assert_eq!(
        one("SELECT sum(99.9::DECIMAL(3,1)) FROM range(100)"),
        vec![vec![Value::Decimal { unscaled: 99_900, width: 38, scale: 1 }]]
    );
    assert_eq!(
        one("SELECT typeof(sum(x)) FROM (SELECT 1.5::DECIMAL(4,1) x)"),
        vec![vec![text("DECIMAL(38,1)")]]
    );
}

#[test]
fn an_aggregate_runs_over_the_elements_of_a_list() {
    let db = database();
    let one = |sql: &str| rows(&db, sql);
    assert_eq!(one("SELECT list_aggr([1, 2, 3], 'sum')"), vec![vec![Value::HugeInt(6)]]);
    assert_eq!(
        db.query("SELECT list_aggr([1, 2, 3], 'sum')").unwrap().names(),
        ["list_aggr(list_value(1, 2, 3), 'sum')"]
    );
    assert_eq!(one("SELECT list_aggr([1, 2, 3], 'avg')"), vec![vec![Value::Double(2.0)]]);
    assert_eq!(one("SELECT list_aggr([1, NULL, 3], 'count')"), vec![vec![Value::BigInt(2)]]);
    assert_eq!(one("SELECT list_aggr(['b', 'a'], 'min')"), vec![vec![text("a")]]);
    assert_eq!(one("SELECT list_aggregate([1, 2], 'SUM')"), vec![vec![Value::HugeInt(3)]]);
    assert_eq!(one("SELECT array_aggr([1, 2], 'max')"), vec![vec![integer(2)]]);
    assert_eq!(one("SELECT aggregate([1], 'sum')"), vec![vec![Value::HugeInt(1)]]);
    assert_eq!(one("SELECT list_aggr([1, 2], 's' || 'um')"), vec![vec![Value::HugeInt(3)]]);
    assert_eq!(
        one("SELECT list_aggr([], 'sum'), list_aggr([], 'count'), list_aggr(NULL, 'count')"),
        vec![vec![Value::Null, Value::BigInt(0), Value::Null]]
    );
    assert_eq!(one("SELECT list_aggr(NULL, 'nope')"), vec![vec![Value::Null]]);
    assert_eq!(
        one("SELECT typeof(list_aggr([1, 2]::TINYINT[], 'sum')), \
             typeof(list_aggr([1, 2]::TINYINT[], 'min')), \
             typeof(list_aggr([1.5]::FLOAT[], 'sum')), \
             typeof(list_aggr([1, 2]::DECIMAL(4,1)[], 'sum'))"),
        vec![vec![text("HUGEINT"), text("TINYINT"), text("DOUBLE"), text("DECIMAL(38,1)")]]
    );
    assert_eq!(
        one("SELECT list_aggr(x, 'sum') FROM (VALUES ([1, 2]), (NULL), ([3])) v(x)"),
        vec![vec![Value::HugeInt(3)], vec![Value::Null], vec![Value::HugeInt(3)]]
    );
}

#[test]
fn an_aggregate_over_a_list_is_refused_the_way_the_pin_refuses_it() {
    let db = database();
    let error = |sql: &str| db.query(sql).unwrap_err().to_string();
    assert!(
        error("SELECT list_aggr([1, 2], 'nope')")
            .starts_with("Catalog Error: Aggregate Function with name nope does not exist!")
    );
    assert!(
        error("SELECT list_aggr([1, 2], 'lower')")
            .starts_with("Catalog Error: lower is not an aggregate function")
    );
    assert!(
        error("SELECT list_aggr([1, 2], NULL)")
            .starts_with("Catalog Error: Aggregate Function with name NULL does not exist!")
    );
    assert!(error("SELECT list_aggr([1, 2])").starts_with(
        "Binder Error: No function matches the given name and argument types \
         'list_aggr(INTEGER[])'. You might need to add explicit type casts.\n\tCandidate \
         functions:\n\tlist_aggr(col0 ANY[], col1 VARCHAR, [ANY...]) -> ANY\n"
    ));
    db.execute("CREATE TABLE f AS SELECT 'sum' AS f").unwrap();
    assert!(error("SELECT list_aggr([1, 2], f) FROM f").starts_with(
        "Binder Error: The \"col1\" argument in function \"list_aggr\" must be a constant \
             expression"
    ));
    assert!(error("SELECT list_aggr(['a'], 'sum')").starts_with(
        "Binder Error: No matching aggregate function\nBinder Error: No function matches the \
         given name and argument types 'sum(VARCHAR)'."
    ));
    assert!(error("SELECT list_aggr([1, 2], 'sum', 3)").starts_with(
        "Binder Error: No matching aggregate function\nBinder Error: No function matches the \
         given name and argument types 'sum(INTEGER, INTEGER)'."
    ));
}