squonk 1.0.0

Extensible, fast, multi-dialect SQL tokenizer and parser for Rust
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Moderately AI Inc.

//! The SELECT body grammar: projection, `FROM`, `WHERE`, `GROUP BY`, `HAVING`.
//!
//! [`parse_select`](Parser::parse_select) assembles one `SELECT` body in SQL
//! clause order. It is the operand the set-operation folder in [`super::query`]
//! repeats, and it recurses (through `parse_query`) for a derived-table subquery
//! in [`super::from`]. Every expression position — projection items, the `WHERE`
//! predicate, `GROUP BY` keys, and `HAVING` — defers to the Pratt core in
//! [`super::expr`], so operator precedence lives in exactly one place.

use crate::ast::{
    AliasSpelling, Expr, Extension, GroupByAllSpelling, GroupByItem, HierarchicalClause, Ident,
    IntoTarget, Keyword, LateralView, Literal, NamedWindow, ObjectName, OnlySyntax,
    RelationInheritance, RollupSpelling, SampleClause, SampleUnit, Select, SelectDistinct,
    SelectItem, SelectSpelling, SetQuantifier, Span, Spanned, TableFactor, TableWithJoins,
    WildcardOptions, WildcardRename, WildcardReplace,
};
use crate::error::ParseResult;
use crate::tokenizer::{Operator, Punctuation, TokenKind};
use thin_vec::{ThinVec, thin_vec};

use super::Dialect;
use super::clause_marks::ClauseKw;
use super::engine::Parser;
use super::expr::number_literal_kind;

/// The clauses shared by the SELECT-first and FROM-first SELECT bodies, parsed after the
/// projection/`FROM` prefix (see [`Parser::parse_select_body_tail`]). Collected so both
/// the SELECT-first assembler and [`Parser::parse_select_from_first`] read this tail from
/// one grammar, differing only in the projection/`FROM` order that precedes it.
struct SelectBodyTail<X: Extension> {
    lateral_views: ThinVec<LateralView<X>>,
    selection: Option<Expr<X>>,
    connect_by: Option<Box<HierarchicalClause<X>>>,
    group_by: ThinVec<GroupByItem<X>>,
    group_by_quantifier: Option<SetQuantifier>,
    group_by_all: Option<GroupByAllSpelling>,
    having: Option<Expr<X>>,
    windows: ThinVec<NamedWindow<X>>,
    qualify: Option<Box<Expr<X>>>,
    sample: Option<Box<SampleClause>>,
}

/// The three outputs of parsing a `GROUP BY` body: the grouping items, PostgreSQL's
/// optional `DISTINCT`/`ALL` set-quantifier ([`Select::group_by_quantifier`]), and
/// DuckDB's `GROUP BY ALL` mode tag ([`Select::group_by_all`]). At most one of the
/// latter two is ever set (they are MECE — see [`Parser::parse_group_by_body`]).
type GroupByBody<X> = (
    ThinVec<GroupByItem<X>>,
    Option<SetQuantifier>,
    Option<GroupByAllSpelling>,
);

impl<'a, D: Dialect> Parser<'a, D> {
    /// Parse a `SELECT [DISTINCT] <projection> [FROM …] [WHERE …] [GROUP BY …]
    /// [HAVING …]` body.
    ///
    /// Each clause is optional and consumed in SQL order. The leading `SELECT`
    /// keyword is validated here (not asserted) because this is also reached
    /// speculatively as a set-operation operand and as a derived-table subquery,
    /// where a missing `SELECT` is a real parse error rather than a dispatch bug.
    pub(super) fn parse_select(&mut self) -> ParseResult<Select<D::Ext>> {
        // Saved before any clause keyword is eaten; the clauses recorded through the
        // body below carry a placeholder owner until this `Select`'s id is minted and
        // patched onto them at the end (their keywords are consumed before the node
        // exists). A nested subquery's clauses sit past its own later checkpoint, so
        // this patch never re-owns them.
        let clause_marks_start = self.clause_marks_checkpoint();
        if !self.peek_is_keyword(Keyword::Select)? {
            return Err(self.unexpected("`SELECT`"));
        }
        let keyword = self
            .advance()?
            .expect("peek_is_keyword confirmed a SELECT token is present");

        let distinct = self.parse_select_distinct()?;
        let straight_join = self.parse_straight_join_modifier()?;
        let projection = self.parse_projection(distinct.as_ref())?;
        let into = self.parse_select_into()?;
        let from = if self.peek_is_keyword(Keyword::From)? {
            if self.capturing_clause_marks() {
                let offset = self.current_span()?.start();
                self.record_clause_mark(ClauseKw::From, offset);
            }
            self.parse_from()?
        } else {
            ThinVec::new()
        };
        let tail = self.parse_select_body_tail(!from.is_empty())?;

        let span = keyword.span.union(self.preceding_span());
        let meta = self.make_meta(span);
        if self.capturing_clause_marks() {
            self.patch_clause_marks(clause_marks_start, meta.node_id);
        }
        Ok(Select {
            distinct,
            straight_join,
            projection,
            into,
            from,
            lateral_views: tail.lateral_views,
            selection: tail.selection,
            connect_by: tail.connect_by,
            group_by: tail.group_by,
            group_by_quantifier: tail.group_by_quantifier,
            group_by_all: tail.group_by_all,
            having: tail.having,
            windows: tail.windows,
            qualify: tail.qualify,
            sample: tail.sample,
            spelling: SelectSpelling::Select,
            meta,
        })
    }

    /// Parse DuckDB's FROM-first SELECT body: `FROM <tables> [SELECT [DISTINCT]
    /// <projection>] <tail>`.
    ///
    /// The `FROM` clause leads; the projection, when written, sits immediately after it,
    /// or is omitted — the bare `FROM <tables>` form is an implicit `SELECT *`. DuckDB
    /// rejects a projection that trails any other clause (`FROM t WHERE x SELECT y` /
    /// `FROM t GROUP BY a SELECT a` are syntax errors; probed on 1.5.4), so the `SELECT`
    /// is read only in this one position and every following clause parses in its
    /// ordinary place through the shared
    /// [`parse_select_body_tail`](Self::parse_select_body_tail). The result is the
    /// canonical [`Select`] shape tagged [`SelectSpelling::FromFirst`] — one shape,
    /// surface recorded: DuckDB serializes `FROM t SELECT x` identically to
    /// `SELECT x FROM t`, and bare `FROM t` identically to `SELECT * FROM t`.
    /// `ORDER BY`/`LIMIT` bind on the enclosing [`Query`](crate::ast::Query), exactly as
    /// for a SELECT-first body.
    ///
    /// Gated at every caller by
    /// [`SelectSyntax::from_first`](crate::ast::dialect::SelectSyntax) (only reached at a
    /// leading `FROM` when the flag is on). The fields written only in the SELECT-first
    /// order — MySQL's [`straight_join`](Select::straight_join) and PostgreSQL's
    /// [`into`](Select::into) — stay at their defaults.
    pub(super) fn parse_select_from_first(&mut self) -> ParseResult<Select<D::Ext>> {
        let clause_marks_start = self.clause_marks_checkpoint();
        let start = self.current_span()?;
        if self.capturing_clause_marks() {
            self.record_clause_mark(ClauseKw::From, start.start());
        }
        let from = self.parse_from()?;
        let (distinct, projection) = if self.eat_keyword(Keyword::Select)? {
            let distinct = self.parse_select_distinct()?;
            let projection = self.parse_projection(distinct.as_ref())?;
            (distinct, projection)
        } else {
            // Bare `FROM <tables>` — the implicit `SELECT *`. The wildcard is synthetic
            // (no source token), so it borrows the FROM-clause span, mirroring how
            // `parse_table_command` anchors its synthesized wildcard.
            (
                None,
                thin_vec![SelectItem::Wildcard {
                    options: None,
                    alias: None,
                    alias_spelling: AliasSpelling::As,
                    meta: self.make_meta(start),
                }],
            )
        };
        // `parse_from` above guarantees a non-empty FROM list, so the lateral-view
        // position is reachable (`FROM t SELECT x LATERAL VIEW …` under Lenient, the
        // one preset that combines both gates).
        let tail = self.parse_select_body_tail(true)?;
        let span = start.union(self.preceding_span());
        let meta = self.make_meta(span);
        if self.capturing_clause_marks() {
            self.patch_clause_marks(clause_marks_start, meta.node_id);
        }
        Ok(Select {
            distinct,
            straight_join: false,
            projection,
            into: None,
            from,
            lateral_views: tail.lateral_views,
            selection: tail.selection,
            connect_by: tail.connect_by,
            group_by: tail.group_by,
            group_by_quantifier: tail.group_by_quantifier,
            group_by_all: tail.group_by_all,
            having: tail.having,
            windows: tail.windows,
            qualify: tail.qualify,
            sample: tail.sample,
            spelling: SelectSpelling::FromFirst,
            meta,
        })
    }

    /// Parse the clauses shared by the SELECT-first and FROM-first bodies, in SQL order
    /// after the projection/`FROM` prefix: `[LATERAL VIEW …]* [WHERE …]
    /// [[START WITH …] CONNECT BY [NOCYCLE] …] [GROUP BY … | GROUP BY ALL] [HAVING …]
    /// [WINDOW …] [QUALIFY …]`.
    ///
    /// `has_from` is whether the body wrote a (non-empty) `FROM` clause: Hive/Spark
    /// attach the lateral views inside the FROM clause (after its last relation), so a
    /// FROM-less body never reads them — its `LATERAL` is left unconsumed and rejects.
    fn parse_select_body_tail(&mut self, has_from: bool) -> ParseResult<SelectBodyTail<D::Ext>> {
        let lateral_views = if has_from {
            self.parse_lateral_views()?
        } else {
            ThinVec::new()
        };
        let selection = if self.eat_keyword(Keyword::Where)? {
            if self.capturing_clause_marks() {
                self.record_clause_mark(ClauseKw::Where, self.preceding_span().start());
            }
            Some(self.parse_expr()?)
        } else {
            None
        };
        let connect_by = self.parse_hierarchical_clause()?;
        let (group_by, group_by_quantifier, group_by_all) = if self.eat_keyword(Keyword::Group)? {
            if self.capturing_clause_marks() {
                self.record_clause_mark(ClauseKw::GroupBy, self.preceding_span().start());
            }
            self.expect_keyword(Keyword::By)?;
            self.parse_group_by_body()?
        } else {
            (ThinVec::new(), None, None)
        };
        let having = if self.eat_keyword(Keyword::Having)? {
            if self.capturing_clause_marks() {
                self.record_clause_mark(ClauseKw::Having, self.preceding_span().start());
            }
            Some(self.parse_expr()?)
        } else {
            None
        };
        let windows = self.parse_window_clause()?;
        let qualify = self.parse_qualify()?;
        let sample = self.parse_using_sample()?;
        Ok(SelectBodyTail {
            lateral_views,
            selection,
            connect_by,
            group_by,
            group_by_quantifier,
            group_by_all,
            having,
            windows,
            qualify,
            sample,
        })
    }

    /// Parse the Hive/Spark `LATERAL VIEW [OUTER] <generator>(args) <alias>
    /// [AS <col> [, …]]` clauses that follow the `FROM` clause
    /// ([`Select::lateral_views`]); empty when none lead. Repeatable — each iteration
    /// consumes one whole clause, so views chain (`… LATERAL VIEW explode(a) t1
    /// LATERAL VIEW explode(t1.x) t2 AS c`).
    ///
    /// Gated by [`SelectSyntax::lateral_view_clause`](crate::ast::dialect::SelectSyntax)
    /// — on for Hive/Databricks/Lenient; off the flag the `LATERAL` keyword is left
    /// unconsumed and surfaces as the usual trailing-token error. Claims a `LATERAL`
    /// only when `VIEW` follows, so the standard LATERAL derived-table/function factor
    /// ([`TableFactorSyntax::lateral`](crate::ast::dialect::TableExpressionSyntax)) —
    /// which reads its `LATERAL` at a table-factor *head*, a position this parser never
    /// occupies — can never race it for the shared lead, under any preset combination
    /// (Lenient enables both).
    fn parse_lateral_views(&mut self) -> ParseResult<ThinVec<LateralView<D::Ext>>> {
        let mut views = ThinVec::new();
        if !self.features().select_syntax.lateral_view_clause {
            return Ok(views);
        }
        while self.peek_is_keyword(Keyword::Lateral)?
            && self.peek_nth_is_keyword(1, Keyword::View)?
        {
            views.push(self.parse_lateral_view()?);
        }
        Ok(views)
    }

    /// Parse one `LATERAL VIEW` clause, cursor on its `LATERAL` (the caller confirmed
    /// the `VIEW` follow token).
    ///
    /// The generator must be a parenthesized function call (Hive's `function` / Spark's
    /// `qualifiedName '(' … ')'` productions) and the table alias is required (both
    /// grammars make it non-optional); the `AS` before the column list is optional —
    /// Spark's grammar spells `AS?` while Hive requires the keyword, a documented
    /// conservative-direction over-acceptance for the Hive preset (see [`LateralView`]).
    /// The alias and columns are `ColId`s, so a following clause keyword (`WHERE`,
    /// `GROUP`, another `LATERAL`, …) is never swallowed as a name.
    fn parse_lateral_view(&mut self) -> ParseResult<LateralView<D::Ext>> {
        let start = self.current_span()?;
        if self.capturing_clause_marks() {
            self.record_clause_mark(ClauseKw::LateralView, start.start());
        }
        self.expect_keyword(Keyword::Lateral)?;
        self.expect_keyword(Keyword::View)?;
        let outer = self.eat_keyword(Keyword::Outer)?;
        let name_start = self.current_span()?;
        let name = self.parse_object_name_with(self.features().reserved_function_name)?;
        if !self.peek_is_punct(Punctuation::LParen)? {
            return Err(self.unexpected("`(` opening the generator function's arguments"));
        }
        let function = self.parse_function_call(name, name_start)?;
        // A generator is `func_expr_windowless`-shaped: the windowed/aggregate wrapper
        // clauses are not a valid table-generating call in Hive or Spark (mirrors the
        // FROM table-function reject).
        if function.over.is_some() || function.filter.is_some() || function.within_group.is_some() {
            let span = function.meta.span;
            return Err(self.error_at(
                span,
                "a plain generator call: a LATERAL VIEW generator cannot carry an \
                 `OVER`, `FILTER`, or `WITHIN GROUP` clause",
                self.span_text(span).to_owned(),
            ));
        }
        let alias = self.parse_ident()?;
        let columns = if self.eat_keyword(Keyword::As)?
            || self
                .peek()?
                .is_some_and(|token| self.token_can_be_column_name(token))
        {
            self.parse_comma_separated(Self::parse_ident)?
        } else {
            ThinVec::new()
        };
        let meta = self.make_meta(start.union(self.preceding_span()));
        Ok(LateralView {
            outer,
            function,
            alias,
            columns,
            meta,
        })
    }

    /// Parse the Oracle-style hierarchical query clause
    /// `[START WITH <cond>] CONNECT BY [NOCYCLE] <cond>` that follows the `WHERE` clause
    /// and precedes `GROUP BY` ([`Select::connect_by`]); `None` when absent.
    ///
    /// `START WITH` and `CONNECT BY` may be written in **either order** (Oracle admits
    /// both `START WITH … CONNECT BY …` and `CONNECT BY … START WITH …`); `CONNECT BY` is
    /// required and `START WITH` optional. The written order is recorded on
    /// [`HierarchicalClause::start_with_leads`] for an exact round-trip.
    ///
    /// Gated by [`SelectSyntax::connect_by_clause`](crate::ast::dialect::SelectSyntax) —
    /// on for Snowflake/Lenient; off the flag the `START`/`CONNECT` keyword is left
    /// unconsumed and surfaces as the usual trailing-token error.
    fn parse_hierarchical_clause(
        &mut self,
    ) -> ParseResult<Option<Box<HierarchicalClause<D::Ext>>>> {
        if !self.features().select_syntax.connect_by_clause {
            return Ok(None);
        }
        let start = self.current_span()?;
        if self.peek_is_keyword(Keyword::Start)? && self.peek_nth_is_keyword(1, Keyword::With)? {
            // START WITH leads; CONNECT BY is mandatory and follows.
            if self.capturing_clause_marks() {
                self.record_clause_mark(ClauseKw::StartWith, start.start());
            }
            let start_with = self.parse_start_with()?;
            if self.capturing_clause_marks() {
                let offset = self.current_span()?.start();
                self.record_clause_mark(ClauseKw::ConnectBy, offset);
            }
            let (nocycle, connect_by) = self.parse_connect_by()?;
            let meta = self.make_meta(start.union(self.preceding_span()));
            Ok(Some(Box::new(HierarchicalClause {
                start_with: Some(start_with),
                nocycle,
                connect_by,
                start_with_leads: true,
                meta,
            })))
        } else if self.peek_is_keyword(Keyword::Connect)?
            && self.peek_nth_is_keyword(1, Keyword::By)?
        {
            // CONNECT BY leads; a trailing START WITH is optional.
            if self.capturing_clause_marks() {
                self.record_clause_mark(ClauseKw::ConnectBy, start.start());
            }
            let (nocycle, connect_by) = self.parse_connect_by()?;
            let start_with = if self.peek_is_keyword(Keyword::Start)?
                && self.peek_nth_is_keyword(1, Keyword::With)?
            {
                if self.capturing_clause_marks() {
                    let offset = self.current_span()?.start();
                    self.record_clause_mark(ClauseKw::StartWith, offset);
                }
                Some(self.parse_start_with()?)
            } else {
                None
            };
            let meta = self.make_meta(start.union(self.preceding_span()));
            Ok(Some(Box::new(HierarchicalClause {
                start_with,
                nocycle,
                connect_by,
                start_with_leads: false,
                meta,
            })))
        } else {
            Ok(None)
        }
    }

    /// Parse a `START WITH <condition>` root-row seed. The condition is an ordinary
    /// predicate — `PRIOR` is meaningful only in the `CONNECT BY` walk, so it is *not*
    /// recognized here (a bare `prior` in `START WITH` stays an ordinary column name).
    fn parse_start_with(&mut self) -> ParseResult<Expr<D::Ext>> {
        self.expect_keyword(Keyword::Start)?;
        self.expect_keyword(Keyword::With)?;
        self.parse_expr()
    }

    /// Parse a `CONNECT BY [NOCYCLE] <condition>`, returning the `NOCYCLE` flag and the
    /// parent/child predicate.
    ///
    /// `NOCYCLE` is an Oracle-only word absent from the shared keyword inventory, so it is
    /// matched as a contextual keyword right after `CONNECT BY` (Snowflake's docs omit it —
    /// accepting it under the one atomic gate is a documented over-acceptance). The
    /// condition parses with [`Parser::in_connect_by`] armed, turning `PRIOR` into the
    /// [`UnaryOperator::Prior`](crate::ast::UnaryOperator) prefix operator; it is an
    /// ordinary expression otherwise, so it rides the guarded expression path and honours
    /// precedence. The flag is restored even on error.
    fn parse_connect_by(&mut self) -> ParseResult<(bool, Expr<D::Ext>)> {
        self.expect_keyword(Keyword::Connect)?;
        self.expect_keyword(Keyword::By)?;
        let nocycle = self.eat_contextual_keyword("NOCYCLE")?;
        let saved = self.in_connect_by;
        self.in_connect_by = true;
        let condition = self.parse_expr();
        self.in_connect_by = saved;
        Ok((nocycle, condition?))
    }

    /// Parse DuckDB's `USING SAMPLE <entry>` query-level sample clause; `None` when
    /// absent. Positioned after `QUALIFY` and before the enclosing query's `ORDER BY`
    /// (the reverse order is a DuckDB syntax error). Gated by
    /// [`QueryTailSyntax::using_sample`](crate::ast::dialect::SelectSyntax); off the flag the
    /// `USING` keyword is left unconsumed and surfaces as the usual trailing-token error.
    ///
    /// The entry is DuckDB's `tablesample_entry`, whose two equivalent surface shapes
    /// (count-first and method-first) fold to the one canonical [`SampleClause`]: a
    /// count-first `<size> [ROWS|PERCENT|%] [ '(' method [',' seed] ')' ]`, a method-first
    /// `method '(' <size> [unit] ')' [REPEATABLE '(' seed ')']`, and the parenthesized
    /// count `'(' <size> [unit] ')'`.
    fn parse_using_sample(&mut self) -> ParseResult<Option<Box<SampleClause>>> {
        if !(self.features().query_tail_syntax.using_sample
            && self.peek_is_keyword(Keyword::Using)?
            && self.peek_nth_is_contextual_keyword(1, "SAMPLE")?)
        {
            return Ok(None);
        }
        let start = self.current_span()?;
        self.advance()?; // USING
        self.expect_contextual_keyword("SAMPLE")?;
        let is_number = self
            .peek()?
            .is_some_and(|token| token.kind == TokenKind::Number);
        let (method, size, unit, seed) = if self.peek_is_punct(Punctuation::LParen)? {
            // `USING SAMPLE ( <size> [unit] )` — the parenthesized bare count.
            self.advance()?; // (
            let (size, unit) = self.parse_sample_size()?;
            self.expect_punct(Punctuation::RParen, "`)` to close the sample size")?;
            (None, size, unit, None)
        } else if is_number {
            // Count-first: `<size> [unit] [ '(' method [',' seed] ')' ]`.
            let (size, unit) = self.parse_sample_size()?;
            let (method, seed) = if self.eat_punct(Punctuation::LParen)? {
                let method = self.parse_object_name()?;
                let seed = if self.eat_punct(Punctuation::Comma)? {
                    Some(self.parse_sample_literal()?)
                } else {
                    None
                };
                self.expect_punct(Punctuation::RParen, "`)` to close the sample method")?;
                (Some(method), seed)
            } else {
                (None, None)
            };
            (method, size, unit, seed)
        } else {
            // Method-first: `method '(' <size> [unit] ')' [REPEATABLE '(' seed ')']`.
            let method = self.parse_object_name()?;
            self.expect_punct(Punctuation::LParen, "`(` after the sample method")?;
            let (size, unit) = self.parse_sample_size()?;
            self.expect_punct(Punctuation::RParen, "`)` to close the sample size")?;
            let seed = self.parse_sample_repeatable()?;
            (Some(method), size, unit, seed)
        };
        let span = start.union(self.preceding_span());
        let meta = self.make_meta(span);
        Ok(Some(Box::new(SampleClause {
            method,
            size,
            unit,
            seed,
            meta,
        })))
    }

    /// Parse a sample size: a numeric literal followed by an optional unit —
    /// `ROWS`, the `PERCENT` keyword, or the `%` sign; a bare number is a row count.
    fn parse_sample_size(&mut self) -> ParseResult<(Literal, SampleUnit)> {
        let size = self.parse_sample_literal()?;
        let unit = if self.eat_keyword(Keyword::Rows)? {
            SampleUnit::Rows
        } else if self.eat_keyword(Keyword::Percent)? {
            SampleUnit::Percent
        } else if self.eat_op(Operator::Percent)? {
            SampleUnit::PercentSign
        } else {
            SampleUnit::Count
        };
        Ok((size, unit))
    }

    /// Parse a bare numeric literal (a sample size or seed); DuckDB rejects a negative
    /// or general-expression value here, so only a `Number` token is admitted.
    fn parse_sample_literal(&mut self) -> ParseResult<Literal> {
        let Some(token) = self.peek()? else {
            return Err(self.unexpected("a numeric sample size"));
        };
        if token.kind != TokenKind::Number {
            return Err(self.unexpected("a numeric sample size"));
        }
        self.advance()?;
        let kind = number_literal_kind(self.span_text(token.span), self.parse_float_as_decimal());
        Ok(Literal {
            kind,
            meta: self.make_meta(token.span),
        })
    }

    /// Parse the optional `REPEATABLE ( <seed> )` random-seed tail of a method-first
    /// sample entry; `None` when absent.
    fn parse_sample_repeatable(&mut self) -> ParseResult<Option<Literal>> {
        if !self.eat_contextual_keyword("REPEATABLE")? {
            return Ok(None);
        }
        self.expect_punct(Punctuation::LParen, "`(` after `REPEATABLE`")?;
        let seed = self.parse_sample_literal()?;
        self.expect_punct(Punctuation::RParen, "`)` to close `REPEATABLE`")?;
        Ok(Some(seed))
    }

    /// Parse DuckDB's `QUALIFY <predicate>` post-window filter; `None` when absent.
    ///
    /// Positioned after the `WINDOW` clause per DuckDB's grammar order (`… HAVING …
    /// WINDOW … QUALIFY …`; the reverse is a DuckDB syntax error). Gated by
    /// [`SelectSyntax::qualify`](crate::ast::dialect::SelectSyntax): a dialect without
    /// it leaves `QUALIFY` unconsumed, so the trailing tokens surface as a clean parse
    /// error (the same reject mechanism the other SELECT gates use). Reaching this
    /// point at all under DuckDB relies on its reservation of `QUALIFY` (the
    /// `DUCKDB_RESERVED_*` sets): an unreserved word would already have been swallowed
    /// as a projection or FROM-relation bare alias. A predicate with no window
    /// function is accepted — DuckDB rejects that at bind time, past the parse-level
    /// contract.
    fn parse_qualify(&mut self) -> ParseResult<Option<Box<Expr<D::Ext>>>> {
        if !self.features().select_syntax.qualify || !self.eat_keyword(Keyword::Qualify)? {
            return Ok(None);
        }
        if self.capturing_clause_marks() {
            self.record_clause_mark(ClauseKw::Qualify, self.preceding_span().start());
        }
        Ok(Some(Box::new(self.parse_expr()?)))
    }

    /// Parse PostgreSQL's `INTO [TEMP | TEMPORARY] <table>` create-table target,
    /// written between the projection and `FROM`; `None` when absent.
    ///
    /// Gated by [`SelectSyntax::select_into`](crate::ast::dialect::SelectSyntax): a
    /// dialect without it leaves `INTO` unconsumed, so the trailing tokens surface as
    /// a clean parse error (the same reject mechanism the other SELECT gates use).
    /// This is the *materialize-into-a-new-table* form (equivalent to `CREATE TABLE
    /// … AS`); the SQL-standard `SELECT … INTO <variable>` (PSM variable assignment)
    /// is a different construct and is deliberately not parsed here.
    fn parse_select_into(&mut self) -> ParseResult<Option<Box<IntoTarget>>> {
        if !self.features().select_syntax.select_into {
            return Ok(None);
        }
        if !self.eat_keyword(Keyword::Into)? {
            return Ok(None);
        }
        let start = self.preceding_span();
        let temporary = self.parse_temporary_table_kind()?;
        // PostgreSQL's `OptTempTableName` admits an optional `TABLE` noise keyword after
        // the temporary marker (`INTO TABLE t`, `INTO TEMP TABLE t`); it carries no
        // meaning beyond the target already being a table, so — like a join side's
        // trailing `OUTER` — it is consumed and dropped rather than kept as a spelling.
        let _ = self.eat_keyword(Keyword::Table)?;
        let name = self.parse_object_name()?;
        let meta = self.make_meta(start.union(self.preceding_span()));
        Ok(Some(Box::new(IntoTarget {
            temporary,
            name,
            meta,
        })))
    }

    /// Parse the body after a consumed `GROUP BY`: an optional set-quantifier, then
    /// the grouping-item list (or DuckDB's `ALL` mode). Returns the grouping items, the
    /// PostgreSQL `DISTINCT`/`ALL` quantifier ([`Select::group_by_quantifier`]), and the
    /// DuckDB `GROUP BY ALL` mode tag ([`Select::group_by_all`], with its `ALL`/`*`
    /// spelling).
    ///
    /// Two constructs share the `ALL` keyword but are mutually exclusive by shape and
    /// kept MECE here:
    /// - PostgreSQL's quantifier ([`GroupingSyntax::group_by_set_quantifier`](crate::ast::dialect::SelectSyntax)):
    ///   `GROUP BY {DISTINCT | ALL} <items>`, a prefix on a *non-empty* item list
    ///   (PostgreSQL rejects a bare `GROUP BY ALL`/`GROUP BY DISTINCT`; probed on
    ///   pg_query PG-17).
    /// - DuckDB's mode ([`GroupingSyntax::group_by_all`](crate::ast::dialect::SelectSyntax)):
    ///   `GROUP BY ALL`, where `ALL` *is* the whole clause (empty item list; DuckDB
    ///   syntax-errors on anything trailing it, probed on 1.5.4). Its `GROUP BY *`
    ///   shorthand ([`GroupByAllSpelling::Star`]) opens the same mode from a bare
    ///   wildcard, disambiguated by the same end-of-clause lookahead.
    ///
    /// `ALL` therefore disambiguates by lookahead: it opens the DuckDB mode only when the
    /// mode gate is on and the grouping clause ends right after it (`at_group_by_end`);
    /// otherwise, under the quantifier gate, it is the quantifier prefixing the item list.
    /// Under Lenient (both gates on) this makes bare `GROUP BY ALL` the mode and
    /// `GROUP BY ALL <items>` the quantifier — the honest superset of the two dialects.
    fn parse_group_by_body(&mut self) -> ParseResult<GroupByBody<D::Ext>> {
        let features = self.features().grouping_syntax;
        // `DISTINCT` is only ever the PostgreSQL quantifier (it has no DuckDB-mode
        // reading), so a following item list is mandatory — a bare `GROUP BY DISTINCT`
        // falls into `parse_group_by_list` and rejects, matching PostgreSQL.
        if features.group_by_set_quantifier && self.eat_keyword(Keyword::Distinct)? {
            let items = self.parse_group_by_list()?;
            return Ok((items, Some(SetQuantifier::Distinct), None));
        }
        // DuckDB's `GROUP BY *` shorthand for the `ALL` mode: a bare wildcard standing
        // for the whole clause. Bare-only (DuckDB binder-rejects `GROUP BY *, x`, probed
        // on 1.5.4), so it opens the mode only when the clause ends right after the star;
        // otherwise the star falls through to the item grammar and errors, as every
        // dialect rejects a bare `*` grouping key.
        if features.group_by_all
            && self.peek_is_op(Operator::Star)?
            && self.at_group_by_end_after(1)?
        {
            self.advance()?; // *
            return Ok((ThinVec::new(), None, Some(GroupByAllSpelling::Star)));
        }
        if self.peek_is_keyword(Keyword::All)? {
            // DuckDB mode: `ALL` stands alone (the clause ends immediately after it).
            // The lookahead over the eaten `ALL` keeps the two constructs MECE without a
            // speculative parse.
            let all_is_mode = features.group_by_all && self.at_group_by_end_after(1)?;
            if all_is_mode {
                self.advance()?; // ALL
                return Ok((ThinVec::new(), None, Some(GroupByAllSpelling::Keyword)));
            }
            if features.group_by_set_quantifier {
                self.advance()?; // ALL
                let items = self.parse_group_by_list()?;
                return Ok((items, Some(SetQuantifier::All), None));
            }
            // Neither gate claims `ALL` here: fall through to the item grammar, where
            // every shipped dialect reserves it — a clean parse error (matching DuckDB's
            // reject of `GROUP BY ALL <items>` and PostgreSQL's of a bare `GROUP BY ALL`).
        }
        let items = self.parse_group_by_list()?;
        Ok((items, None, None))
    }

    /// Parse the comma-separated grouping-item list, folding MySQL's trailing
    /// `WITH ROLLUP` modifier. DuckDB tolerates a single trailing comma before the
    /// clause that follows the keys (`GROUP BY a, b,`); the follower is exactly
    /// [`at_group_by_end`](Self::at_group_by_end).
    fn parse_group_by_list(&mut self) -> ParseResult<ThinVec<GroupByItem<D::Ext>>> {
        let items =
            self.parse_comma_separated_trailing(Self::parse_group_by_item, Self::at_group_by_end)?;
        self.wrap_with_rollup(items)
    }

    /// Parse one `GROUP BY` item: an ordinary grouping expression or, when
    /// [`GroupingSyntax::grouping_sets`](crate::ast::dialect::SelectSyntax) is on, one
    /// of the SQL:1999 grouping-set constructs (`ROLLUP (…)`, `CUBE (…)`,
    /// `GROUPING SETS (…)`, or the empty grouping set `()`).
    ///
    /// PostgreSQL lowers those keyword forms in GROUP BY item position for any case
    /// spelling, so they are their own grammar node, not [`FunctionCall`](crate::ast::FunctionCall)s. With the
    /// gate off (MySQL) every item falls through to [`parse_expr`](Self::parse_expr),
    /// so an unquoted `rollup (a, b)` parses as an ordinary function call — MySQL's
    /// stored-function reading.
    fn parse_group_by_item(&mut self) -> ParseResult<GroupByItem<D::Ext>> {
        if self.features().grouping_syntax.grouping_sets {
            if let Some(item) = self.parse_grouping_set_construct()? {
                return Ok(item);
            }
        }
        let expr = self.parse_expr()?;
        let meta = self.make_meta(expr.span());
        Ok(GroupByItem::Expr { expr, meta })
    }

    /// Parse a grouping-set `GROUP BY` construct when the current tokens begin one;
    /// `None` when they do not (the caller then parses an ordinary expression).
    ///
    /// Only the *unquoted* keyword forms are constructs: a quoted `"rollup"(…)`
    /// tokenizes as a `QuotedIdent`, never `Keyword::Rollup`, so it stays a function
    /// call — matching PostgreSQL, which lowers only the bare keyword. Each keyword
    /// form additionally requires its opening `(`, so a bare `rollup` / `grouping`
    /// remains an ordinary column or function reference (PG's unreserved words).
    fn parse_grouping_set_construct(&mut self) -> ParseResult<Option<GroupByItem<D::Ext>>> {
        // `ROLLUP (expr_list)` / `CUBE (expr_list)`: a plain expression list.
        if self.peek_is_keyword(Keyword::Rollup)?
            && self.peek_nth_is_punct(1, Punctuation::LParen)?
        {
            let start = self.advance_span()?;
            let exprs = self.parse_paren_expr_list()?;
            let meta = self.make_meta(start.union(self.preceding_span()));
            return Ok(Some(GroupByItem::Rollup {
                exprs,
                spelling: RollupSpelling::Function,
                meta,
            }));
        }
        if self.peek_is_keyword(Keyword::Cube)? && self.peek_nth_is_punct(1, Punctuation::LParen)? {
            let start = self.advance_span()?;
            let exprs = self.parse_paren_expr_list()?;
            let meta = self.make_meta(start.union(self.preceding_span()));
            return Ok(Some(GroupByItem::Cube { exprs, meta }));
        }
        // `GROUPING SETS (group_by_list)`: the members are themselves GROUP BY items,
        // so `ROLLUP`/`CUBE`/nested `GROUPING SETS`/`()` may appear inside.
        if self.peek_is_keyword(Keyword::Grouping)?
            && self.peek_nth_is_keyword(1, Keyword::Sets)?
            && self.peek_nth_is_punct(2, Punctuation::LParen)?
        {
            let start = self.advance_span()?; // GROUPING
            self.advance()?; // SETS
            self.expect_punct(Punctuation::LParen, "`(` after `GROUPING SETS`")?;
            let sets = self.parse_comma_separated_trailing(Self::parse_group_by_item, |p| {
                p.trailing_comma_at(Punctuation::RParen)
            })?;
            self.expect_punct(Punctuation::RParen, "`)` to close the `GROUPING SETS` list")?;
            let meta = self.make_meta(start.union(self.preceding_span()));
            return Ok(Some(GroupByItem::GroupingSets { sets, meta }));
        }
        // The empty grouping set `()` — the grand total. Distinguished from a
        // parenthesized expression by the immediately following `)`.
        if self.peek_is_punct(Punctuation::LParen)?
            && self.peek_nth_is_punct(1, Punctuation::RParen)?
        {
            let start = self.advance_span()?; // (
            self.advance()?; // )
            let meta = self.make_meta(start.union(self.preceding_span()));
            return Ok(Some(GroupByItem::Empty { meta }));
        }
        Ok(None)
    }

    /// Fold MySQL's trailing `GROUP BY <keys> WITH ROLLUP` modifier into the one
    /// canonical [`GroupByItem::Rollup`] shape, tagged [`RollupSpelling::WithRollup`]
    /// so rendering round-trips the surface. Returns `items` unchanged when
    /// the modifier is absent.
    ///
    /// Gated by [`GroupingSyntax::with_rollup`](crate::ast::dialect::SelectSyntax); when
    /// off (PostgreSQL/ANSI, which spell the super-aggregate `ROLLUP (…)`) the `WITH`
    /// is left unconsumed and surfaces as a trailing-input parse error. MySQL's
    /// `WITH ROLLUP` wraps *plain* grouping keys only, so each item must be a
    /// [`GroupByItem::Expr`]; a grouping-set item (reachable only under Lenient, which
    /// enables `grouping_sets` and this gate together) is a clean error.
    fn wrap_with_rollup(
        &mut self,
        items: ThinVec<GroupByItem<D::Ext>>,
    ) -> ParseResult<ThinVec<GroupByItem<D::Ext>>> {
        if !self.features().grouping_syntax.with_rollup
            || !self.peek_is_keyword(Keyword::With)?
            || !self.peek_nth_is_keyword(1, Keyword::Rollup)?
        {
            return Ok(items);
        }
        // The wrapping node spans the whole key list plus the trailing modifier: from
        // the first key to the consumed `ROLLUP`. `parse_comma_separated` yields at
        // least one item, so `first()` is present.
        let start = items
            .first()
            .map_or_else(|| self.preceding_span(), Spanned::span);
        let mut exprs = ThinVec::with_capacity(items.len());
        for item in items {
            match item {
                GroupByItem::Expr { expr, .. } => exprs.push(expr),
                other => {
                    return Err(self.error_at(
                        other.span(),
                        "a plain grouping expression before `WITH ROLLUP`",
                        "a grouping-set construct",
                    ));
                }
            }
        }
        self.advance()?; // WITH
        self.advance()?; // ROLLUP
        let meta = self.make_meta(start.union(self.preceding_span()));
        Ok(thin_vec![GroupByItem::Rollup {
            exprs,
            spelling: RollupSpelling::WithRollup,
            meta,
        }])
    }

    /// Consume the current token and return its span. Used where a keyword or
    /// punctuation has already been confirmed by a peek, so its presence is an
    /// invariant.
    pub(super) fn advance_span(&mut self) -> ParseResult<Span> {
        let token = self
            .advance()?
            .expect("a preceding peek confirmed a token is present");
        Ok(token.span)
    }

    /// Parse a parenthesized comma-separated expression list `( expr, … )`, the
    /// `ROLLUP`/`CUBE` argument shape. The opening `(` is confirmed by the caller's
    /// peek, so it is expected here rather than re-peeked. DuckDB tolerates a single
    /// trailing comma before the closing `)` (`ROLLUP(a, b,)`; engine-probed on 1.5.4),
    /// so this is the trailing-tolerant analogue of
    /// [`parse_comma_separated_exprs`](Self::parse_comma_separated_exprs) — not that
    /// shared combinator, whose remaining callers (`PARTITION BY`, `PIVOT`, …) reject the
    /// trailing comma.
    fn parse_paren_expr_list(&mut self) -> ParseResult<ThinVec<Expr<D::Ext>>> {
        self.expect_punct(Punctuation::LParen, "`(`")?;
        let exprs = self.parse_comma_separated_trailing(Self::parse_expr, |p| {
            p.trailing_comma_at(Punctuation::RParen)
        })?;
        self.expect_punct(Punctuation::RParen, "`)` to close the grouping list")?;
        Ok(exprs)
    }

    /// Parse the optional `ALL` / `DISTINCT` / `DISTINCT ON (...)` set quantifier
    /// that follows the `SELECT` keyword; `None` when none is written.
    ///
    /// `ALL` and `DISTINCT` are the standard set quantifiers ([`SetQuantifier`]);
    /// `DISTINCT ON (<expr>, ...)` is the PostgreSQL extension, gated by
    /// [`SelectSyntax::distinct_on`](crate::ast::dialect::SelectSyntax) so a dialect
    /// without it leaves `ON` unconsumed and the projection parse rejects it.
    fn parse_select_distinct(&mut self) -> ParseResult<Option<SelectDistinct<D::Ext>>> {
        if self.eat_keyword(Keyword::All)? {
            let span = self.preceding_span();
            let meta = self.make_meta(span);
            return Ok(Some(SelectDistinct::Quantifier {
                quantifier: SetQuantifier::All,
                meta,
            }));
        }
        if self.eat_keyword(Keyword::Distinct)? {
            let start = self.preceding_span();
            if self.features().select_syntax.distinct_on && self.eat_keyword(Keyword::On)? {
                self.expect_punct(Punctuation::LParen, "`(` after `DISTINCT ON`")?;
                // DuckDB tolerates a single trailing comma in the key list
                // (`DISTINCT ON (a,)`; engine-probed on 1.5.4), so this list site opts into
                // the trailing-tolerant combinator rather than the shared
                // `parse_comma_separated_exprs` — whose other callers (`PARTITION BY`,
                // `PIVOT`, …) must keep rejecting the trailing comma.
                let exprs = self.parse_comma_separated_trailing(Self::parse_expr, |p| {
                    p.trailing_comma_at(Punctuation::RParen)
                })?;
                self.expect_punct(Punctuation::RParen, "`)` to close the `DISTINCT ON` list")?;
                let meta = self.make_meta(start.union(self.preceding_span()));
                return Ok(Some(SelectDistinct::On { exprs, meta }));
            }
            let meta = self.make_meta(start);
            return Ok(Some(SelectDistinct::Quantifier {
                quantifier: SetQuantifier::Distinct,
                meta,
            }));
        }
        Ok(None)
    }

    /// Parse the optional MySQL `STRAIGHT_JOIN` SELECT modifier — the query-wide form
    /// of the join-order hint, written after the `DISTINCT`/`ALL` quantifier and
    /// before the projection. `true` when present.
    ///
    /// Gated by [`JoinSyntax::straight_join`](crate::ast::dialect::TableExpressionSyntax),
    /// the one flag that also admits the `STRAIGHT_JOIN` join operator. A dialect
    /// without it leaves the word unconsumed, so `STRAIGHT_JOIN` falls through to the
    /// projection grammar as an ordinary (non-reserved) identifier — the same reject
    /// mechanism the other SELECT gates use.
    fn parse_straight_join_modifier(&mut self) -> ParseResult<bool> {
        Ok(self.features().join_syntax.straight_join && self.eat_keyword(Keyword::StraightJoin)?)
    }

    /// Parse the SELECT projection list.
    ///
    /// Ordinarily a non-empty comma-separated list (no trailing comma). Under
    /// [`SelectSyntax::empty_target_list`](crate::ast::dialect::SelectSyntax)
    /// (PostgreSQL/Lenient) the list may be empty — libpg_query's raw grammar makes the
    /// projection optional before any clause (`SELECT`, `SELECT FROM t`, `SELECT;`) —
    /// so the first item is required only when a projection actually begins.
    ///
    /// The empty form is admitted only for a plain or explicit-`ALL` SELECT: PostgreSQL
    /// splits `SELECT opt_all_clause opt_target_list` (empty allowed) from
    /// `SELECT distinct_clause target_list` (required), so `SELECT DISTINCT` /
    /// `SELECT DISTINCT ON (…)` with no items is a syntax error there and here.
    fn parse_projection(
        &mut self,
        distinct: Option<&SelectDistinct<D::Ext>>,
    ) -> ParseResult<ThinVec<SelectItem<D::Ext>>> {
        let empty_allowed = self.features().select_syntax.empty_target_list
            && matches!(
                distinct,
                None | Some(SelectDistinct::Quantifier {
                    quantifier: SetQuantifier::All,
                    ..
                })
            );
        if empty_allowed && self.at_empty_target_list()? {
            return Ok(ThinVec::new());
        }
        // DuckDB tolerates a trailing comma before the clause that follows the projection
        // (`SELECT a, b, FROM t`); the follower is exactly `at_empty_target_list`.
        let items = self
            .parse_comma_separated_trailing(Self::parse_select_item, Self::at_empty_target_list)?;
        Ok(items)
    }

    /// True when the projection is empty: the next token is a clause keyword that
    /// follows the target list, a set operator, or a statement terminator (`;`, `)`,
    /// end of input) — never a select item.
    ///
    /// A `target_el` begins with `*` or an expression, and every token here is instead
    /// a *reserved* clause keyword or punctuation, so it can never begin one — the
    /// disjointness that lets an empty list be recognized by its follower rather than
    /// by enumerating the (dialect-dependent, open) set of expression starts. The
    /// followers are exactly the clauses `parse_select` and `parse_query_after_with`
    /// consume after the projection; any other token routes to
    /// [`parse_select_item`](Self::parse_select_item), which reports the precise error.
    fn at_empty_target_list(&mut self) -> ParseResult<bool> {
        let Some(token) = self.peek()? else {
            return Ok(true); // end of input: a bare `SELECT`
        };
        Ok(match token.kind {
            // Statement terminator / subquery or paren close.
            TokenKind::Punctuation(Punctuation::Semicolon | Punctuation::RParen) => true,
            // The SELECT-body clauses (`INTO`/`FROM`/`WHERE`/`GROUP`/`HAVING`/`WINDOW`),
            // the query-tail clauses (`ORDER`/`LIMIT`/`OFFSET`/`FETCH`), and the set
            // operators (`UNION`/`INTERSECT`/`EXCEPT`) that may follow the target list.
            TokenKind::Keyword(keyword) => matches!(
                keyword,
                Keyword::Into
                    | Keyword::From
                    | Keyword::Where
                    | Keyword::Group
                    | Keyword::Having
                    | Keyword::Window
                    | Keyword::Order
                    | Keyword::Limit
                    | Keyword::Offset
                    | Keyword::Fetch
                    | Keyword::Union
                    | Keyword::Intersect
                    | Keyword::Except
            ),
            _ => false,
        })
    }

    /// True when the `GROUP BY` key list has ended: the next token is a clause keyword
    /// that follows the grouping keys, a set operator, or a statement terminator (`;`,
    /// `)`, end of input) — never a grouping item. The trailing-comma closer for the
    /// open-ended `GROUP BY` list (`GROUP BY a, b,`), the grouping-key analogue of
    /// [`at_empty_target_list`](Self::at_empty_target_list).
    ///
    /// A grouping item is a `ROLLUP`/`CUBE`/`GROUPING SETS` construct or a general
    /// expression, and every token here is instead a *reserved* follower keyword or a
    /// terminator, so it can never begin one — the same disjointness that lets
    /// `at_empty_target_list` recognize the empty projection by its follower rather than
    /// by enumerating the (open) set of expression starts. The followers are exactly the
    /// clauses [`parse_select_body_tail`](Self::parse_select_body_tail) and the enclosing
    /// query tail consume after the keys — `HAVING`/`WINDOW`/`QUALIFY`/`USING SAMPLE`
    /// (the last opened by its `USING` keyword) and then `ORDER`/`LIMIT`/`OFFSET`/`FETCH`
    /// and the set operators. Consulted only under
    /// [`SelectSyntax::trailing_comma`](crate::ast::dialect::SelectSyntax) and only after
    /// a comma (see [`parse_comma_separated_trailing`](Self::parse_comma_separated_trailing)),
    /// so a flag-off dialect leaves the dangling comma for `parse_group_by_item` to
    /// reject — the standard parse error. DuckDB accepts the comma before every one of
    /// these followers (engine-probed on 1.5.4).
    fn at_group_by_end(&mut self) -> ParseResult<bool> {
        self.at_group_by_end_after(0)
    }

    /// [`at_group_by_end`](Self::at_group_by_end) evaluated at the token `offset` places
    /// ahead of the cursor — used to disambiguate DuckDB's `GROUP BY ALL` mode (no
    /// grouping item follows `ALL`) from PostgreSQL's `ALL <items>` quantifier
    /// (`offset` = 1 looks past the not-yet-consumed `ALL`). Offset 0 is the plain
    /// current-token check.
    fn at_group_by_end_after(&mut self, offset: usize) -> ParseResult<bool> {
        let Some(token) = self.peek_nth(offset)? else {
            return Ok(true); // end of input: `GROUP BY a,` closing the statement
        };
        Ok(match token.kind {
            TokenKind::Punctuation(Punctuation::Semicolon | Punctuation::RParen) => true,
            TokenKind::Keyword(keyword) => matches!(
                keyword,
                Keyword::Having
                    | Keyword::Window
                    | Keyword::Qualify
                    | Keyword::Using
                    | Keyword::Order
                    | Keyword::Limit
                    | Keyword::Offset
                    | Keyword::Fetch
                    | Keyword::Union
                    | Keyword::Intersect
                    | Keyword::Except
            ),
            _ => false,
        })
    }

    /// Parse the `TABLE <relation_expr>` command into the canonical [`Select`] shape.
    ///
    /// `TABLE name` is the SQL `<explicit table>` short form for `SELECT * FROM name`
    /// (accepted by every shipped dialect, so it is ungated like `TRUNCATE`). It
    /// lowers to a wildcard projection over the one relation, tagged
    /// [`SelectSpelling::TableCommand`] so the renderer round-trips `TABLE name` (the
    /// star-projection shape PostgreSQL itself lowers `TABLE t` to, so the differential
    /// oracle compares one shape). Only a bare `relation_expr` follows — a qualified
    /// name with the optional PostgreSQL `ONLY`/`*` inheritance markers, and *no* alias
    /// or sample (`TABLE t x` is a syntax error) — while `ORDER BY`/`LIMIT` and set
    /// operations compose outside, on the enclosing query, exactly as for a `SELECT`.
    pub(super) fn parse_table_command(&mut self) -> ParseResult<Select<D::Ext>> {
        let keyword = self
            .advance()?
            .expect("parse_table_command is reached only at the TABLE keyword");
        let relation = self.parse_table_command_relation()?;
        let wildcard_meta = self.make_meta(keyword.span);
        let relation_span = relation.span();
        let table_meta = self.make_meta(relation_span);
        let span = keyword.span.union(self.preceding_span());
        let meta = self.make_meta(span);
        Ok(Select {
            distinct: None,
            straight_join: false,
            projection: thin_vec![SelectItem::Wildcard {
                options: None,
                alias: None,
                alias_spelling: AliasSpelling::As,
                meta: wildcard_meta,
            }],
            into: None,
            from: thin_vec![TableWithJoins {
                relation,
                joins: ThinVec::new(),
                meta: table_meta,
            }],
            lateral_views: ThinVec::new(),
            connect_by: None,
            selection: None,
            group_by: ThinVec::new(),
            group_by_quantifier: None,
            group_by_all: None,
            having: None,
            windows: ThinVec::new(),
            qualify: None,
            sample: None,
            spelling: SelectSpelling::TableCommand,
            meta,
        })
    }

    /// Parse the `relation_expr` after `TABLE`: `[ONLY] qualified_name [*]` or
    /// `ONLY ( qualified_name )`, yielding a bare [`TableFactor::Table`] (no alias, no
    /// sample). The `ONLY`/`*` inheritance markers share the FROM-relation
    /// [`only`](crate::ast::dialect::TableExpressionSyntax::only) gate, so a dialect
    /// without PostgreSQL inheritance accepts only the plain `TABLE name`.
    fn parse_table_command_relation(&mut self) -> ParseResult<TableFactor<D::Ext>> {
        let start = self.current_span()?;
        // `ONLY` is the inheritance-suppression marker only when a name (or the
        // parenthesized form) follows; otherwise a bare `ONLY` is an ordinary relation
        // name (mirroring the FROM-clause `relation_expr` disambiguation).
        if self.peek_is_keyword(Keyword::Only)?
            && (self.peek_nth_is_punct(1, Punctuation::LParen)?
                || self
                    .peek_nth(1)?
                    .is_some_and(|token| self.token_can_be_column_name(token)))
        {
            if !self.features().table_expressions.only {
                return Err(self.unexpected("a table relation supported by this dialect"));
            }
            self.advance()?; // ONLY
            let (only, name) = if self.eat_punct(Punctuation::LParen)? {
                let name = self.parse_object_name()?;
                self.expect_punct(Punctuation::RParen, "`)` to close the `ONLY` table name")?;
                (OnlySyntax::Parenthesized, name)
            } else {
                (OnlySyntax::Bare, self.parse_object_name()?)
            };
            let meta = self.make_meta(start.union(self.preceding_span()));
            return Ok(TableFactor::Table {
                name,
                inheritance: RelationInheritance::Only(only),
                // The `TABLE <name>` command takes a bare relation — no PartiQL path, no
                // version modifier, no MySQL tails.
                json_path: ThinVec::new(),
                version: None,
                partition: ThinVec::new(),
                alias: None,
                indexed_by: None,
                index_hints: ThinVec::new(),
                sample: None,
                table_hints: ThinVec::new(),
                meta,
            });
        }

        let name = self.parse_relation_name(start)?;
        let inheritance = self.parse_descendant_star()?;
        let meta = self.make_meta(start.union(self.preceding_span()));
        Ok(TableFactor::Table {
            name,
            inheritance,
            json_path: ThinVec::new(),
            version: None,
            partition: ThinVec::new(),
            alias: None,
            indexed_by: None,
            index_hints: ThinVec::new(),
            sample: None,
            table_hints: ThinVec::new(),
            meta,
        })
    }

    /// Parse a `qualified_name` in relation position, capped at the dialect's relation
    /// depth (`catalog.schema.table` for the catalog-qualified presets, `schema.table` for
    /// SQLite) exactly as the FROM-clause relation is
    /// ([`max_relation_name_parts`](Self::max_relation_name_parts)).
    fn parse_relation_name(&mut self, start: Span) -> ParseResult<ObjectName> {
        let head_reserved = self.features().reserved_column_name;
        let Some(token) = self.peek()? else {
            return Err(self.unexpected("a table name after `TABLE`"));
        };
        if !self.token_admissible(token, head_reserved) {
            return Err(self.unexpected("a table name after `TABLE`"));
        }
        let name = self.parse_object_name_with(head_reserved)?;
        if name.0.len() > self.max_relation_name_parts() {
            let span = start.union(self.preceding_span());
            let found = self.span_text(span).to_owned();
            return Err(self.error_at(span, self.relation_name_depth_expected(), found));
        }
        Ok(name)
    }

    /// Parse one projection item: `*`, `<name>.*`, or `<expr> [[AS] alias]`.
    ///
    /// A bare `*` is the wildcard. Otherwise the item is parsed as an expression;
    /// a dotted name stops before a `.` that is not followed by a word (see
    /// [`parse_object_name`](Parser::parse_object_name)), so a trailing `.*` is
    /// still unconsumed and identifies a qualified wildcard. Anything else is an
    /// expression, optionally aliased.
    pub(super) fn parse_select_item(&mut self) -> ParseResult<SelectItem<D::Ext>> {
        // A leading `*COLUMNS(...)` is DuckDB's columns-unpack expression, not the bare
        // wildcard: `SELECT *COLUMNS('a') + 42` spreads the matched columns into an
        // ordinary value expression (`Expr::Columns` with `ColumnsSpelling::Unpack`), so
        // it must reach the expression parser rather than be consumed as `SELECT *`. The
        // primary-position arm already handles the unpack prefix; this guard only keeps
        // the wildcard branch from claiming the `*` first.
        if self.peek_is_op(Operator::Star)? && !self.peek_is_columns_unpack_prefix()? {
            let token = self
                .advance()?
                .expect("peek_is_op confirmed a wildcard token is present");
            let options = self.parse_gated_wildcard_options(token.span)?;
            let (alias, alias_spelling) = self.parse_gated_wildcard_alias()?;
            let span = token.span.union(self.preceding_span());
            let meta = self.make_meta(span);
            return Ok(SelectItem::Wildcard {
                options,
                alias,
                alias_spelling,
                meta,
            });
        }
        self.parse_projection_value_item()
    }

    /// Parse the value form of a projection item (a SELECT target or a `RETURNING`
    /// item), after any leading bare `*` has been ruled out: an expression, a
    /// qualified wildcard `<name>.*`, or a value-position composite star `(expr).*`,
    /// each with an optional alias.
    ///
    /// The target expression is parsed with the value-position `.*` star selector
    /// suppressed at its top level, so a bare-name `t.*` stays a
    /// [`SelectItem::QualifiedWildcard`] (and admits the DuckDB wildcard modifiers)
    /// rather than folding into a value expression. A trailing `.*` on a *non-name*
    /// base — `(func()).*`, `(a).b.*` — has no qualified-name form, so under
    /// [`ExpressionSyntax::field_wildcard`](crate::ast::dialect::ExpressionSyntax::field_wildcard)
    /// it folds into a value composite-star expression; with the flag off it is left
    /// unconsumed and rejects downstream, as before.
    pub(super) fn parse_projection_value_item(&mut self) -> ParseResult<SelectItem<D::Ext>> {
        // DuckDB's prefix colon alias: `<alias> : <expr>`, the alias written before the
        // value. It folds onto the ordinary trailing-alias field (DuckDB canonicalizes it
        // to `AS`), so once the prefix is read the item takes no trailing alias — a
        // following `AS y` / bare word is left unconsumed and rejects, matching the engine.
        if self.peek_starts_prefix_colon_alias()? {
            let alias = self.parse_bare_alias_ident()?;
            let start = alias.span();
            self.expect_punct(Punctuation::Colon, "`:` after a prefix alias")?;
            let expr = self.parse_expr()?;
            let span = start.union(self.preceding_span());
            let meta = self.make_meta(span);
            return Ok(SelectItem::Expr {
                expr,
                alias: Some(alias),
                alias_spelling: AliasSpelling::PrefixColon,
                meta,
            });
        }
        let expr = self.parse_projection_target_expr()?;
        let qualified_wildcard =
            self.peek_is_punct(Punctuation::Dot)? && self.peek_nth_is_op(1, Operator::Star)?;
        if qualified_wildcard {
            // `<name>.*`: a dotted column name immediately followed by `.*` — the
            // select-list qualified wildcard, admitted under every preset.
            if let Expr::Column { name, .. } = expr {
                self.advance()?; // `.`
                let star = self
                    .advance()?
                    .expect("peek confirmed a `*` follows the dot");
                let options = self.parse_gated_wildcard_options(star.span)?;
                let (alias, alias_spelling) = self.parse_gated_qualified_wildcard_alias()?;
                let span = name.span().union(self.preceding_span());
                let meta = self.make_meta(span);
                return Ok(SelectItem::QualifiedWildcard {
                    name,
                    options,
                    alias,
                    alias_spelling,
                    meta,
                });
            }
            // A non-name base: `(func()).*` composite expansion in a target position.
            // The star suppression only defers the fold to here; it still needs the
            // value-star flag on.
            if self.features().expression_syntax.field_wildcard {
                self.advance()?; // `.`
                self.advance()?; // `*`
                let star = self.build_field_wildcard(expr);
                let start = star.span();
                let (alias, alias_spelling) = self.parse_optional_alias()?;
                let span = alias
                    .as_ref()
                    .map_or(start, |alias| start.union(alias.span()));
                let meta = self.make_meta(span);
                return Ok(SelectItem::Expr {
                    expr: star,
                    alias,
                    alias_spelling,
                    meta,
                });
            }
        }
        let start = expr.span();
        let (alias, alias_spelling) = self.parse_optional_alias()?;
        let span = alias
            .as_ref()
            .map_or(start, |alias| start.union(alias.span()));
        let meta = self.make_meta(span);
        Ok(SelectItem::Expr {
            expr,
            alias,
            alias_spelling,
            meta,
        })
    }

    /// Parse the DuckDB wildcard-modifier tail after a select-list/`RETURNING`
    /// `*`/`t.*`, but only when [`SelectSyntax::wildcard_modifiers`](crate::ast::dialect::SelectSyntax::wildcard_modifiers) is on. When off,
    /// a trailing `EXCLUDE`/`REPLACE`/`RENAME` is left unconsumed and surfaces as the
    /// usual downstream parse error — the over-acceptance guard for non-DuckDB
    /// dialects. `star_span` anchors the synthesized [`WildcardOptions`] span.
    pub(super) fn parse_gated_wildcard_options(
        &mut self,
        star_span: Span,
    ) -> ParseResult<Option<Box<WildcardOptions<D::Ext>>>> {
        if !self.features().select_syntax.wildcard_modifiers {
            return Ok(None);
        }
        self.parse_wildcard_modifier_tail(star_span)
    }

    /// Parse the optional alias on a select-list/`RETURNING` *bare* `*`, gated on the
    /// same DuckDB star axis as the modifiers
    /// ([`SelectSyntax::wildcard_modifiers`](crate::ast::dialect::SelectSyntax::wildcard_modifiers)).
    /// DuckDB admits `SELECT * AS idx` — the alias renames *every* star-expanded column
    /// (engine-probed on 1.5.4) — and the bare `SELECT * idx`, each written *after* any
    /// `EXCLUDE`/`REPLACE`/`RENAME` tail (`* EXCLUDE (a) AS idx`; `* AS idx EXCLUDE (a)`
    /// is a syntax error there and here, since the modifier tail is consumed first).
    /// When the gate is off, the word is left unconsumed so `SELECT * x` rejects
    /// downstream, exactly as before. Reuses
    /// [`parse_optional_alias`](Self::parse_optional_alias) — the same machinery a
    /// [`SelectItem::Expr`] projection uses — so the [`AliasSpelling`] tag (bare vs `AS`)
    /// rides for free; a star alias never reaches the `PrefixColon` form. The *qualified*
    /// `t.*` alias is a separate axis
    /// ([`parse_gated_qualified_wildcard_alias`](Self::parse_gated_qualified_wildcard_alias)):
    /// the bare-`*` rename-all is DuckDB-only, but a qualified wildcard's plain alias is a
    /// PostgreSQL surface too, so the two do not share a gate.
    pub(super) fn parse_gated_wildcard_alias(
        &mut self,
    ) -> ParseResult<(Option<Ident>, AliasSpelling)> {
        if !self.features().select_syntax.wildcard_modifiers {
            return Ok((None, AliasSpelling::As));
        }
        self.parse_optional_alias()
    }

    /// Parse the optional alias on a select-list/`RETURNING` *qualified* wildcard `t.*`,
    /// gated on
    /// [`SelectSyntax::qualified_wildcard_alias`](crate::ast::dialect::SelectSyntax::qualified_wildcard_alias).
    /// PostgreSQL reads `t.*` as an ordinary column-reference expression, so it takes the
    /// very same `[AS] label` projection alias an expression does (`SELECT t.* x` /
    /// `SELECT t.* AS x`; the bare form admits the `BareColLabel` reserved set, the `AS`
    /// form the full `ColLabel` set — libpg_query-measured, matching
    /// [`parse_optional_alias`](Self::parse_optional_alias) exactly, which is why it is
    /// reused). DuckDB admits it too (its qualified star aliases like the bare one). When
    /// the gate is off (ANSI/MySQL/SQLite, where `t.*` is a non-aliasable wildcard
    /// production — engine-measured reject), the word is left unconsumed and `SELECT t.* x`
    /// rejects downstream, exactly as before. Kept distinct from
    /// [`parse_gated_wildcard_alias`](Self::parse_gated_wildcard_alias) because the bare-`*`
    /// alias and this qualified one have different dialect boundaries.
    pub(super) fn parse_gated_qualified_wildcard_alias(
        &mut self,
    ) -> ParseResult<(Option<Ident>, AliasSpelling)> {
        if !self.features().select_syntax.qualified_wildcard_alias {
            return Ok((None, AliasSpelling::As));
        }
        self.parse_optional_alias()
    }

    /// Parse `[EXCLUDE …] [REPLACE …] [RENAME …]` in DuckDB's fixed surface order,
    /// each modifier optional and at most once. Parsing strictly in this sequence is
    /// what rejects an out-of-order or repeated modifier: a `REPLACE (…) EXCLUDE (…)`
    /// leaves the second keyword unconsumed for the caller to reject, exactly as
    /// DuckDB syntax-errors on it (probed on 1.5.4). Returns `None` when no modifier
    /// is present. Unconditional (no feature gate) so the `COLUMNS(*)` star form can
    /// reuse it directly; the select-list gate lives in
    /// [`parse_gated_wildcard_options`](Self::parse_gated_wildcard_options).
    pub(super) fn parse_wildcard_modifier_tail(
        &mut self,
        star_span: Span,
    ) -> ParseResult<Option<Box<WildcardOptions<D::Ext>>>> {
        let exclude = if self.eat_keyword(Keyword::Exclude)? {
            self.parse_wildcard_column_list()?
        } else {
            ThinVec::new()
        };
        let replace = if self.eat_keyword(Keyword::Replace)? {
            self.parse_wildcard_parenthesizable(Self::parse_wildcard_replace_item)?
        } else {
            ThinVec::new()
        };
        let rename = if self.eat_keyword(Keyword::Rename)? {
            self.parse_wildcard_parenthesizable(Self::parse_wildcard_rename_item)?
        } else {
            ThinVec::new()
        };
        if exclude.is_empty() && replace.is_empty() && rename.is_empty() {
            return Ok(None);
        }
        let span = star_span.union(self.preceding_span());
        let meta = self.make_meta(span);
        Ok(Some(Box::new(WildcardOptions {
            exclude,
            replace,
            rename,
            meta,
        })))
    }

    /// Parse the `EXCLUDE` operand: a parenthesized comma list `(a, t.b)` or a single
    /// bare column reference `a` (the bare form takes exactly one item — DuckDB reads
    /// `EXCLUDE a, b` as `EXCLUDE (a)` plus a second projection item).
    fn parse_wildcard_column_list(&mut self) -> ParseResult<ThinVec<ObjectName>> {
        self.parse_wildcard_parenthesizable(Self::parse_object_name)
    }

    /// Shared `( item [, item]* )`-or-bare-`item` shape behind all three modifiers:
    /// a parenthesized comma list, else a single bare item. The parenthesized form
    /// tolerates a single trailing comma before `)` under DuckDB's list tolerance
    /// (`* EXCLUDE (a,)`, `* REPLACE (e AS c,)`, `* RENAME (a AS b,)`; engine-probed on
    /// 1.5.4) — covering the select-list/`RETURNING`, `ORDER BY *`, and `COLUMNS(*)`
    /// modifier lists that all route through here. The bare (unparenthesized) form takes
    /// exactly one item, so no trailing comma is possible there.
    fn parse_wildcard_parenthesizable<T>(
        &mut self,
        parse_item: impl Fn(&mut Self) -> ParseResult<T> + Copy,
    ) -> ParseResult<ThinVec<T>> {
        if self.eat_punct(Punctuation::LParen)? {
            let items = self.parse_comma_separated_trailing(parse_item, |p| {
                p.trailing_comma_at(Punctuation::RParen)
            })?;
            self.expect_punct(
                Punctuation::RParen,
                "`)` to close the wildcard modifier list",
            )?;
            Ok(items)
        } else {
            Ok(thin_vec![parse_item(self)?])
        }
    }

    /// One `REPLACE` entry: `<expr> AS <col>` — the replacement expression and the
    /// output column it stands in for.
    fn parse_wildcard_replace_item(&mut self) -> ParseResult<WildcardReplace<D::Ext>> {
        let start = self.current_span()?;
        let expr = self.parse_expr()?;
        self.expect_keyword(Keyword::As)?;
        let column = self.parse_ident()?;
        let span = start.union(self.preceding_span());
        let meta = self.make_meta(span);
        Ok(WildcardReplace { expr, column, meta })
    }

    /// One `RENAME` entry: `<col> AS <new>` — the source column (which DuckDB permits
    /// to be qualified) renamed to the unqualified output name.
    fn parse_wildcard_rename_item(&mut self) -> ParseResult<WildcardRename> {
        let start = self.current_span()?;
        let column = self.parse_object_name()?;
        self.expect_keyword(Keyword::As)?;
        let alias = self.parse_ident()?;
        let span = start.union(self.preceding_span());
        let meta = self.make_meta(span);
        Ok(WildcardRename {
            column,
            alias,
            meta,
        })
    }

    /// Parse a non-empty comma-separated list: one `f`, then `f` after each comma —
    /// the shared shape behind ~30 comma lists across the parser
    /// (`eval-parser-generic-comma-separated-combinator`). `#[inline]` is
    /// LOAD-BEARING: it collapses each monomorphization back into the caller, so the
    /// ~30 sites cost no more machine code than the inline loops they replaced
    /// (measured size-neutral).
    /// Dropping `#[inline]` re-emits ~30 standalone functions and regrows the artifact.
    #[inline]
    pub(super) fn parse_comma_separated<T>(
        &mut self,
        mut f: impl FnMut(&mut Self) -> ParseResult<T>,
    ) -> ParseResult<ThinVec<T>> {
        let mut items = thin_vec![f(self)?];
        while self.eat_punct(Punctuation::Comma)? {
            items.push(f(self)?);
        }
        Ok(items)
    }

    /// Parse a comma-separated expression list (one or more; no trailing comma).
    ///
    /// The expression lists that reject a trailing comma (the window `PARTITION BY` list,
    /// the `PIVOT` grouping list, and similar positions); each item is a full Pratt
    /// expression. (A `GROUP BY` item is the richer
    /// [`GroupByItem`], parsed by [`parse_group_by_item`](Self::parse_group_by_item); the
    /// `ROLLUP`/`CUBE` argument lists and the `DISTINCT ON` key list tolerate a trailing
    /// comma under DuckDB and route through the trailing-aware
    /// [`parse_comma_separated_trailing`](Self::parse_comma_separated_trailing) instead.)
    pub(super) fn parse_comma_separated_exprs(&mut self) -> ParseResult<ThinVec<Expr<D::Ext>>> {
        self.parse_comma_separated(Self::parse_expr)
    }

    /// [`parse_comma_separated`](Self::parse_comma_separated) that, under
    /// [`SelectSyntax::trailing_comma`](crate::ast::dialect::SelectSyntax) — DuckDB's list
    /// tolerance — discards a single trailing comma before the list's closer. `at_close`
    /// reports whether the cursor sits at that closer; it is consulted only after a comma
    /// and only when the dialect tolerates the trailing comma, so a flag-off dialect
    /// parses exactly as `parse_comma_separated` (the dangling comma then fails in `f`, the
    /// standard reject). Applied per accepting list site rather than folded into
    /// `parse_comma_separated`, because DuckDB rejects the trailing comma in the
    /// function-argument, `ORDER BY`, and row-constructor lists that also route through the
    /// shared combinator (see the flag docs). `#[inline]` is load-bearing for the same
    /// size-neutral reason as `parse_comma_separated`.
    #[inline]
    pub(super) fn parse_comma_separated_trailing<T>(
        &mut self,
        mut f: impl FnMut(&mut Self) -> ParseResult<T>,
        mut at_close: impl FnMut(&mut Self) -> ParseResult<bool>,
    ) -> ParseResult<ThinVec<T>> {
        let mut items = thin_vec![f(self)?];
        while self.eat_punct(Punctuation::Comma)? {
            if self.features().select_syntax.trailing_comma && at_close(self)? {
                break;
            }
            items.push(f(self)?);
        }
        Ok(items)
    }

    /// The trailing-comma closer test for a list whose closing delimiter is a single
    /// punctuation (`)` / `]` / `}`): true when the dialect tolerates a trailing comma
    /// ([`SelectSyntax::trailing_comma`](crate::ast::dialect::SelectSyntax)) *and* the
    /// cursor sits at `close`. Used at the bespoke element loops (list / struct / map
    /// literals) whose shape does not route through
    /// [`parse_comma_separated_trailing`](Self::parse_comma_separated_trailing).
    pub(super) fn trailing_comma_at(&mut self, close: Punctuation) -> ParseResult<bool> {
        Ok(self.features().select_syntax.trailing_comma && self.peek_is_punct(close)?)
    }

    /// Parse an optional projection alias: `AS <ColLabel>` or a bare `<BareColLabel>`.
    ///
    /// The two positions reserve different keyword sets (prod-keyword-position-reserved-sets):
    /// an explicit `AS` introduces a `ColLabel`, which admits *every* keyword
    /// (`SELECT a AS select` is valid), while a bare alias is a `BareColLabel`,
    /// which rejects the `AS_LABEL` keywords — so `SELECT a over` / `SELECT a filter`
    /// are not aliases (and `FROM t WHERE …` still cannot read `WHERE` as an alias).
    pub(super) fn parse_optional_alias(&mut self) -> ParseResult<(Option<Ident>, AliasSpelling)> {
        if self.eat_keyword(Keyword::As)? {
            // MySQL admits a string literal as a projection alias (`SELECT 1 AS 'x'`);
            // this position is column-specific, so the string form does not leak into
            // table/schema-name aliases (which share `parse_as_alias_ident`).
            if self.features().select_syntax.alias_string_literals {
                if let Some(ident) = self.parse_string_alias_ident()? {
                    return Ok((Some(ident), AliasSpelling::As));
                }
            }
            // MySQL has no PostgreSQL `ColLabel` relaxation on the projection `AS` alias — a
            // reserved word is rejected there exactly as in the bare-alias position — so it
            // routes this one position to the stricter `reserved_bare_alias` set, while the
            // dotted-name continuation (which also flows through `parse_as_alias_ident`)
            // keeps the permissive `reserved_as_label` set.
            if self.features().select_syntax.as_alias_rejects_reserved {
                return Ok((Some(self.parse_bare_alias_ident()?), AliasSpelling::As));
            }
            return Ok((Some(self.parse_as_alias_ident()?), AliasSpelling::As));
        }
        // A FROM-less `SELECT <expr> SETTINGS name = …` reaches the query-tail settings
        // clause; the bare word `SETTINGS` here opens that clause, not a projection alias
        // named `settings`, so decline it (the same discriminator the table-alias
        // position uses).
        if self.peek_opens_settings_clause()? {
            return Ok((None, AliasSpelling::As));
        }
        // Likewise a FROM-less `SELECT <expr> FORMAT <name>` reaches the query-tail format
        // clause; the bare word `FORMAT` here opens that clause, not a projection alias
        // named `format`, so decline it (the same discriminator the table-alias position
        // uses).
        if self.peek_opens_format_clause()? {
            return Ok((None, AliasSpelling::As));
        }
        // A bare (`AS`-less) string-literal alias (`SELECT 1 'x'`), SQLite's and MySQL's
        // rule: a `String` token in bare-alias position is the column name. For MySQL the
        // adjacent-string-concat overlap is already resolved — a string primary folds its
        // following string continuations during expression parsing, so a string only reaches
        // here after a non-string expression. DuckDB accepts only the `AS 'x'` form, so this
        // rides its own axis rather than `alias_string_literals`.
        if self.features().select_syntax.bare_alias_string_literals {
            if let Some(ident) = self.parse_string_alias_ident()? {
                return Ok((Some(ident), AliasSpelling::Bare));
            }
        }
        if self.peek_can_start_bare_alias()? {
            Ok((Some(self.parse_bare_alias_ident()?), AliasSpelling::Bare))
        } else {
            Ok((None, AliasSpelling::As))
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::ast::dialect::{
        FeatureDelta, FeatureSet, GroupingSyntax, QueryTailSyntax, SelectSyntax, TableFactorSyntax,
    };
    use crate::ast::{
        Expr, GroupByItem, NoExt, OnlySyntax, RelationInheritance, Resolver as _, RollupSpelling,
        Select, SelectDistinct, SelectItem, SelectSpelling, SetExpr, SetQuantifier, Span,
        Statement, TableFactor, TemporaryTableKind,
    };
    use crate::parser::{FeatureDialect, Parsed, TestDialect, parse_with};

    /// ANSI plus the PostgreSQL SELECT-clause extensions, isolating the new forms
    /// from the rest of the PostgreSQL preset (casing, etc.).
    const PG_SELECT_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet = FeatureSet::ANSI.with(
            FeatureDelta::EMPTY
                .select_syntax(SelectSyntax::POSTGRES)
                .query_tail_syntax(QueryTailSyntax::POSTGRES)
                .grouping_syntax(GroupingSyntax::POSTGRES),
        );
        FeatureDialect {
            features: &FEATURES,
        }
    };

    /// ANSI plus the `QUALIFY` clause flag alone, isolating the clause gate from the
    /// DuckDb preset's `QUALIFY` keyword *reservation* (tested with the preset in
    /// `crate::dialect::duckdb`).
    const QUALIFY_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet =
            FeatureSet::ANSI.with(FeatureDelta::EMPTY.select_syntax(SelectSyntax {
                qualify: true,
                ..SelectSyntax::ANSI
            }));
        FeatureDialect {
            features: &FEATURES,
        }
    };

    /// ANSI plus the `GROUP BY ALL` / `ORDER BY ALL` clause-mode flags alone,
    /// isolating the two gates from the rest of the DuckDb preset. Implements
    /// `RenderDialect` for the exact-text round-trip checks (the stock DuckDb
    /// preset has no Tier-1 render target yet).
    const BY_ALL_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet =
            FeatureSet::ANSI.with(FeatureDelta::EMPTY.grouping_syntax(GroupingSyntax {
                group_by_all: true,
                order_by_all: true,
                ..GroupingSyntax::ANSI
            }));
        FeatureDialect {
            features: &FEATURES,
        }
    };

    /// ANSI plus the FROM-first flag alone (and the two `*_by_all` clause modes it
    /// co-occurs with in the corpus), isolating the `from_first` gate from the rest of
    /// the DuckDb preset. Implements `RenderDialect` for the exact-text round-trip checks.
    const FROM_FIRST_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet = FeatureSet::ANSI.with(
            FeatureDelta::EMPTY
                .select_syntax(SelectSyntax {
                    from_first: true,
                    ..SelectSyntax::ANSI
                })
                .grouping_syntax(GroupingSyntax {
                    group_by_all: true,
                    order_by_all: true,
                    ..GroupingSyntax::ANSI
                }),
        );
        FeatureDialect {
            features: &FEATURES,
        }
    };

    /// ANSI plus DuckDB's prefix-colon-alias flag alone, isolating the gate from the rest
    /// of the DuckDb preset so the accept/reject boundary is attributed to this one flag.
    const COLON_ALIAS_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet =
            FeatureSet::ANSI.with(FeatureDelta::EMPTY.select_syntax(SelectSyntax {
                prefix_colon_alias: true,
                ..SelectSyntax::ANSI
            }));
        FeatureDialect {
            features: &FEATURES,
        }
    };

    /// DuckDB's prefix colon alias `SELECT j : 42` folds onto the ordinary alias field —
    /// the alias `j` on the value `42`, identical shape to `42 AS j` (no new node, no
    /// spelling tag). Gated by the flag: off under plain ANSI, a `:` head rejects.
    #[test]
    fn prefix_colon_alias_projection_folds_onto_alias_field() {
        let parsed =
            parse_with("SELECT j : 42", COLON_ALIAS_DIALECT).expect("prefix colon alias parses");
        let [SelectItem::Expr { alias, .. }] = projection(&parsed) else {
            panic!("expected one aliased expression item");
        };
        let alias = alias.as_ref().expect("the prefix alias is captured");
        assert_eq!(parsed.resolver().resolve(alias.sym), "j");

        // Canonical render rewrites the colon to a trailing `AS` — matching DuckDB's own
        // json round-trip (`SELECT j : 42` → `SELECT 42 AS j`), the reuse this feature
        // rides: no new node, no spelling tag.
        let rendered = crate::render::Renderer::new(COLON_ALIAS_DIALECT)
            .render_parsed(&parsed)
            .expect("renders");
        assert_eq!(rendered, "SELECT 42 AS j");

        // The gate, honoured as data: the same text rejects with the flag off.
        assert!(parse_with("SELECT j : 42", TestDialect).is_err());
    }

    /// The three alias spellings coexist in one projection list: prefix `j1 : 42`, trailing
    /// `42 AS j2`, and bare `42 j3` (a corpus line). Each item captures its own alias.
    #[test]
    fn prefix_colon_alias_coexists_with_as_and_bare_aliases() {
        let parsed = parse_with("SELECT j1 : 42, 42 AS j2, 42 j3", COLON_ALIAS_DIALECT)
            .expect("mixed alias spellings parse");
        let names: Vec<&str> = projection(&parsed)
            .iter()
            .map(|item| {
                let SelectItem::Expr { alias, .. } = item else {
                    panic!("expected an aliased expression item");
                };
                parsed
                    .resolver()
                    .resolve(alias.as_ref().expect("an alias").sym)
            })
            .collect();
        assert_eq!(names, ["j1", "j2", "j3"]);
    }

    /// The prefix alias is mutually exclusive with a trailing alias (`SELECT a : 42 AS b`
    /// rejects, probed on 1.5.4), and the `:` head fires only for a lone identifier that
    /// immediately abuts the colon — a qualified name (`a.b :`) and a call (`f() :`) are
    /// structurally rejected, and the alias never chains (`p : q : 42`). The admissible
    /// LHS follows the shared bare-alias reserved set (dialect data), so a quoted
    /// identifier is admitted.
    #[test]
    fn prefix_colon_alias_rejects_trailing_alias_and_non_identifier_lhs() {
        assert!(parse_with("SELECT a : 42 AS b", COLON_ALIAS_DIALECT).is_err());
        assert!(parse_with("SELECT a.b : 42", COLON_ALIAS_DIALECT).is_err());
        assert!(parse_with("SELECT foo() : 42", COLON_ALIAS_DIALECT).is_err());
        assert!(parse_with("SELECT p : q : 42", COLON_ALIAS_DIALECT).is_err());
        // A quoted identifier LHS is admitted (`"my col" : 42`).
        assert!(parse_with("SELECT \"my col\" : 42", COLON_ALIAS_DIALECT).is_ok());
    }

    fn projection(parsed: &Parsed) -> &[SelectItem<NoExt>] {
        &select_of(parsed).projection
    }

    fn select_of(parsed: &Parsed) -> &Select<NoExt> {
        let Statement::Query { query, .. } = &parsed.statements()[0] else {
            panic!("expected a query statement");
        };
        let SetExpr::Select { select, .. } = &query.body else {
            panic!("expected a plain SELECT body");
        };
        select
    }

    #[test]
    fn explicit_as_alias_is_captured() {
        let parsed = parse_with("SELECT a AS x", TestDialect).expect("aliased projection parses");
        let SelectItem::Expr {
            alias: Some(alias), ..
        } = &projection(&parsed)[0]
        else {
            panic!("expected an aliased expression item");
        };
        assert_eq!(parsed.resolver().resolve(alias.sym), "x");
    }

    #[test]
    fn implicit_alias_without_as_is_captured() {
        // `a b` aliases the column `a` as `b` with the `AS` keyword elided.
        let parsed = parse_with("SELECT a b", TestDialect).expect("implicit alias parses");
        let SelectItem::Expr {
            alias: Some(alias), ..
        } = &projection(&parsed)[0]
        else {
            panic!("expected an implicitly aliased item");
        };
        assert_eq!(parsed.resolver().resolve(alias.sym), "b");
    }

    #[test]
    fn non_reserved_keyword_can_be_a_column_or_alias() {
        // `Range` is an unreserved keyword usable as a bare column (`ColId`); an
        // `AS` alias is a ColLabel that admits any keyword (`Desc`, reserved); and a
        // bare alias is a BareColLabel that admits non-`AS_LABEL` keywords (`Nulls`).
        let parsed = parse_with("SELECT Range, a AS Desc, b Nulls", TestDialect)
            .expect("contextual keywords can be identifiers");
        let items = projection(&parsed);

        let SelectItem::Expr {
            expr: Expr::Column { name, .. },
            alias: None,
            ..
        } = &items[0]
        else {
            panic!("expected `Range` as a bare column");
        };
        assert_eq!(parsed.resolver().resolve(name.0[0].sym), "Range");

        let SelectItem::Expr {
            alias: Some(alias), ..
        } = &items[1]
        else {
            panic!("expected `Desc` as an explicit alias");
        };
        assert_eq!(parsed.resolver().resolve(alias.sym), "Desc");

        let SelectItem::Expr {
            alias: Some(alias), ..
        } = &items[2]
        else {
            panic!("expected `Nulls` as an implicit alias");
        };
        assert_eq!(parsed.resolver().resolve(alias.sym), "Nulls");
    }

    #[test]
    fn reserved_keyword_is_accepted_as_an_explicit_alias() {
        // D1 (`prod-keyword-position-reserved-sets`): an `AS` alias is a ColLabel,
        // which admits *every* keyword — including reserved ones like `FROM` — so
        // `SELECT a AS from` parses, matching PostgreSQL.
        let parsed = parse_with("SELECT a AS from", TestDialect)
            .expect("a reserved keyword is a valid AS alias");
        let SelectItem::Expr {
            alias: Some(alias), ..
        } = &projection(&parsed)[0]
        else {
            panic!("expected an aliased projection");
        };
        assert_eq!(parsed.resolver().resolve(alias.sym), "from");
    }

    #[test]
    fn reserved_keyword_cannot_start_a_projection_expression() {
        let err = parse_with("SELECT FROM", TestDialect)
            .expect_err("reserved keyword is not a column expression");
        assert_eq!(err.span, Span::new(7, 11));
    }

    #[test]
    fn a_clause_keyword_is_not_taken_as_an_implicit_alias() {
        // `FROM` must not be read as the alias of the projection column `a`.
        let parsed = parse_with("SELECT a FROM t", TestDialect).expect("FROM ends the projection");
        assert!(matches!(
            projection(&parsed)[0],
            SelectItem::Expr { alias: None, .. },
        ));
    }

    #[test]
    fn mysql_reserved_words_gate_identifiers_per_dialect() {
        use crate::dialect::{Ansi, MySql, Postgres};

        // Forward divergence (mysql-reserved-word-set): `RLIKE` is reserved in MySQL
        // 8.0 but free under ANSI/PostgreSQL. The word now lives in the shared
        // inventory, yet the dialect reject sets gate it differently — the same
        // mechanism the position sets already use, no new identifier logic.
        parse_with("SELECT rlike FROM t", MySql)
            .expect_err("MySQL reserves RLIKE as a column name");
        let ansi = parse_with("SELECT rlike FROM t", Ansi)
            .expect("ANSI leaves RLIKE free as an identifier");
        assert_eq!(
            ansi.resolver().resolve(match &projection(&ansi)[0] {
                SelectItem::Expr {
                    expr: Expr::Column { name, .. },
                    ..
                } => name.0[0].sym,
                _ => panic!("expected `rlike` as a bare column under ANSI"),
            }),
            "rlike",
        );
        parse_with("SELECT rlike FROM t", Postgres)
            .expect("PostgreSQL leaves RLIKE free as an identifier");

        // A MySQL bare alias is also gated: `STRAIGHT_JOIN` cannot alias under MySQL
        // but can under ANSI (it is non-reserved there).
        parse_with("SELECT a straight_join", MySql)
            .expect_err("MySQL reserves STRAIGHT_JOIN, so it is not a bare alias");
        parse_with("SELECT a straight_join", Ansi)
            .expect("ANSI admits STRAIGHT_JOIN as a bare alias");

        // Reverse divergence: PostgreSQL reserves `OFFSET`, MySQL does not, so the
        // gate swings the other way — MySQL admits it as a bare column, PostgreSQL
        // rejects it.
        parse_with("SELECT offset FROM t", MySql)
            .expect("MySQL leaves OFFSET free as a column name");
        parse_with("SELECT offset FROM t", Postgres).expect_err("PostgreSQL reserves OFFSET");
    }

    #[test]
    fn mysql_straight_join_modifier_rides_select() {
        use crate::dialect::{Ansi, MySql};

        // The MySQL `SELECT STRAIGHT_JOIN ...` modifier sets the `straight_join` flag
        // and is consumed before the projection, so `a` is the sole projection column.
        let parsed = parse_with("SELECT STRAIGHT_JOIN a FROM t", MySql)
            .expect("MySQL parses the STRAIGHT_JOIN modifier");
        let select = select_of(&parsed);
        assert!(select.straight_join, "the modifier flag is set");
        assert_eq!(
            select.projection.len(),
            1,
            "STRAIGHT_JOIN is the modifier, not a projection item",
        );

        // Gated: under ANSI the flag is off, so `STRAIGHT_JOIN` is read as a column
        // reference aliased `a` (a non-reserved word there), not the modifier.
        let ansi = parse_with("SELECT STRAIGHT_JOIN a FROM t", Ansi)
            .expect("ANSI reads STRAIGHT_JOIN as an ordinary identifier");
        let ansi_select = select_of(&ansi);
        assert!(
            !ansi_select.straight_join,
            "ANSI sets no STRAIGHT_JOIN modifier"
        );
        assert!(matches!(
            ansi_select.projection[0],
            SelectItem::Expr { alias: Some(_), .. },
        ));
    }

    #[test]
    fn qualified_wildcard_keeps_its_object_name() {
        let parsed = parse_with("SELECT t.*", TestDialect).expect("qualified wildcard parses");
        let SelectItem::QualifiedWildcard { name, .. } = &projection(&parsed)[0] else {
            panic!("expected a qualified wildcard");
        };
        assert_eq!(name.0.len(), 1, "the `t` prefix");
        assert_eq!(parsed.resolver().resolve(name.0[0].sym), "t");
    }

    #[test]
    fn multi_part_qualified_wildcard_keeps_the_whole_prefix() {
        let parsed = parse_with("SELECT a.b.*", TestDialect).expect("dotted wildcard parses");
        let SelectItem::QualifiedWildcard { name, .. } = &projection(&parsed)[0] else {
            panic!("expected a qualified wildcard");
        };
        assert_eq!(name.0.len(), 2, "the `a.b` prefix");
    }

    #[test]
    fn bare_wildcard_is_unchanged() {
        let parsed = parse_with("SELECT *", TestDialect).expect("bare wildcard parses");
        assert!(matches!(
            projection(&parsed)[0],
            SelectItem::Wildcard { .. }
        ));
    }

    #[test]
    fn qualified_wildcard_takes_a_projection_alias() {
        use crate::ast::AliasSpelling;
        use crate::dialect::{Ansi, MySql, Postgres, Sqlite};

        // PostgreSQL reads `t.*` as an ordinary columnref, so it takes the standard
        // `[AS] label` projection alias (engine-probed against libpg_query). The alias folds
        // onto the qualified-wildcard item's slot, carrying its bare-vs-`AS` spelling.
        let bare = parse_with("SELECT t.* a FROM t", Postgres).expect("`t.* a` parses under PG");
        let SelectItem::QualifiedWildcard {
            name,
            alias: Some(alias),
            alias_spelling,
            ..
        } = &projection(&bare)[0]
        else {
            panic!("expected an aliased qualified wildcard");
        };
        assert_eq!(bare.resolver().resolve(name.0[0].sym), "t");
        assert_eq!(bare.resolver().resolve(alias.sym), "a");
        assert_eq!(*alias_spelling, AliasSpelling::Bare);

        let as_form =
            parse_with("SELECT t.* AS a FROM t", Postgres).expect("`t.* AS a` parses under PG");
        let SelectItem::QualifiedWildcard {
            alias: Some(alias),
            alias_spelling,
            ..
        } = &projection(&as_form)[0]
        else {
            panic!("expected an aliased qualified wildcard");
        };
        assert_eq!(as_form.resolver().resolve(alias.sym), "a");
        assert_eq!(*alias_spelling, AliasSpelling::As);

        // A multi-part prefix aliases the same way (`s.t.* a`).
        parse_with("SELECT s.t.* a FROM s.t", Postgres).expect("`s.t.* a` parses under PG");

        // The minimized fuzz reproducer (parse-qualified-wildcard-bare-alias): `hEE.*` then
        // the bare label `LC`, which PG accepts and we used to reject.
        parse_with("SELECT hEE.*LC;", Postgres).expect("the fuzz reproducer parses under PG");

        // Asymmetry: a *bare* `*` is the non-aliasable `target_el: '*'` production, so a
        // trailing word rejects even under PG (matches libpg_query).
        assert!(
            parse_with("SELECT * a FROM t", Postgres).is_err(),
            "PG rejects a bare-star alias",
        );

        // Gated: dialects whose `t.*` is a non-aliasable production keep the flag off, so the
        // trailing alias rejects (engine-measured Reject on ANSI/rusqlite/mysql:8).
        assert!(
            parse_with("SELECT t.* a FROM t", Ansi).is_err(),
            "ANSI rejects the qualified-wildcard alias (gate off)",
        );
        assert!(
            parse_with("SELECT t.* a FROM t", MySql).is_err(),
            "MySQL rejects the qualified-wildcard alias (gate off)",
        );
        assert!(
            parse_with("SELECT t.* a FROM t", Sqlite).is_err(),
            "SQLite rejects the qualified-wildcard alias (gate off)",
        );
    }

    #[test]
    fn no_quantifier_leaves_distinct_unset() {
        let parsed = parse_with("SELECT a", TestDialect).expect("bare projection parses");
        assert!(select_of(&parsed).distinct.is_none());
    }

    #[test]
    fn explicit_all_quantifier_is_captured() {
        // `SELECT ALL` is the explicit spelling of the default; it is preserved as a
        // distinct AST state so it round-trips (mirroring `ORDER BY … ASC`).
        let parsed = parse_with("SELECT ALL a", TestDialect).expect("SELECT ALL parses");
        assert!(matches!(
            select_of(&parsed).distinct,
            Some(SelectDistinct::Quantifier {
                quantifier: SetQuantifier::All,
                ..
            }),
        ));
    }

    #[test]
    fn distinct_quantifier_is_captured() {
        let parsed = parse_with("SELECT DISTINCT a", TestDialect).expect("SELECT DISTINCT parses");
        assert!(matches!(
            select_of(&parsed).distinct,
            Some(SelectDistinct::Quantifier {
                quantifier: SetQuantifier::Distinct,
                ..
            }),
        ));
    }

    #[test]
    fn distinct_on_keeps_its_key_expressions() {
        let parsed = parse_with("SELECT DISTINCT ON (a, b) c FROM t", PG_SELECT_DIALECT)
            .expect("DISTINCT ON parses under PostgreSQL");
        let Some(SelectDistinct::On { exprs, .. }) = &select_of(&parsed).distinct else {
            panic!("expected a DISTINCT ON quantifier");
        };
        assert_eq!(exprs.len(), 2, "both ON keys are retained");
        assert!(matches!(exprs[0], Expr::Column { .. }));
    }

    #[test]
    fn distinct_on_is_rejected_without_the_dialect_gate() {
        // ANSI does not gate `DISTINCT ON`, so `ON` is read as a projection token and
        // rejected (it is a reserved keyword, not a column expression).
        let err = parse_with("SELECT DISTINCT ON (a) c", TestDialect)
            .expect_err("ANSI rejects DISTINCT ON");
        // `ON` sits at bytes 16..18, right after `SELECT DISTINCT `.
        assert_eq!(err.span, Span::new(16, 18));
    }

    #[test]
    fn select_into_captures_the_target_table() {
        // PostgreSQL's `SELECT … INTO <table>` create-table form: the target sits
        // between the projection and `FROM`. `INTO` is reserved as a bare alias, so it
        // is not swallowed as the alias of the projection column.
        let parsed =
            parse_with("SELECT a INTO t FROM s", PG_SELECT_DIALECT).expect("SELECT INTO parses");
        let select = select_of(&parsed);
        let into = select.into.as_ref().expect("the INTO target is captured");
        assert!(into.temporary.is_none(), "no TEMP marker was written");
        assert_eq!(into.name.0.len(), 1, "an unqualified target name");
        assert_eq!(parsed.resolver().resolve(into.name.0[0].sym), "t");
        // The projection column is not aliased `into` — `INTO` ended the projection.
        assert!(matches!(
            select.projection[0],
            SelectItem::Expr { alias: None, .. },
        ));
        // The `FROM` clause after the target still parses.
        assert_eq!(select.from.len(), 1, "the FROM source is retained");
    }

    #[test]
    fn select_into_temp_marks_a_temporary_target() {
        let parsed = parse_with("SELECT a INTO TEMP t FROM s", PG_SELECT_DIALECT)
            .expect("SELECT INTO TEMP parses");
        let into = select_of(&parsed)
            .into
            .as_ref()
            .expect("the INTO target is captured");
        assert_eq!(into.temporary, Some(TemporaryTableKind::Temp));
    }

    #[test]
    fn select_into_temporary_keeps_the_long_spelling() {
        // The `TEMPORARY` long form is a distinct surface spelling from `TEMP`; it is
        // preserved (not folded to `TEMP`) so the target round-trips exactly, reusing
        // the same `TemporaryTableKind` shape `CREATE TABLE` uses.
        let parsed = parse_with("SELECT a INTO TEMPORARY t FROM s", PG_SELECT_DIALECT)
            .expect("SELECT INTO TEMPORARY parses");
        let into = select_of(&parsed)
            .into
            .as_ref()
            .expect("the INTO target is captured");
        assert_eq!(into.temporary, Some(TemporaryTableKind::Temporary));
    }

    #[test]
    fn select_into_accepts_the_optional_table_noise_keyword() {
        // PostgreSQL's `OptTempTableName` admits an optional `TABLE` after the (optional)
        // temporary marker; it is pure noise, so it is consumed and dropped — the target and
        // its temporary axis are captured identically to the bare spelling.
        let plain = parse_with("SELECT a INTO TABLE t FROM s", PG_SELECT_DIALECT)
            .expect("SELECT INTO TABLE parses");
        let into = select_of(&plain)
            .into
            .as_ref()
            .expect("INTO target captured");
        assert_eq!(into.temporary, None);

        let temp = parse_with("SELECT a INTO TEMP TABLE t FROM s", PG_SELECT_DIALECT)
            .expect("SELECT INTO TEMP TABLE parses");
        let temp_into = select_of(&temp)
            .into
            .as_ref()
            .expect("INTO target captured");
        assert_eq!(temp_into.temporary, Some(TemporaryTableKind::Temp));
    }

    #[test]
    fn select_into_is_rejected_when_the_gate_is_off() {
        use crate::dialect::{Ansi, MySql};

        // ANSI's bare `SELECT … INTO` is PSM variable assignment, not the create-table
        // form, so the gate is off: `INTO` is left unconsumed and the trailing
        // `INTO t FROM s` is a parse error. MySQL has no such form and also rejects it.
        assert!(
            parse_with("SELECT a INTO t FROM s", Ansi).is_err(),
            "ANSI does not accept the SELECT INTO create-table form",
        );
        assert!(
            parse_with("SELECT a INTO t FROM s", MySql).is_err(),
            "MySQL has no SELECT INTO create-table form",
        );
    }

    fn group_by_of(parsed: &Parsed) -> &[GroupByItem<NoExt>] {
        &select_of(parsed).group_by
    }

    #[test]
    fn group_by_rollup_parses_as_a_grouping_construct() {
        // `ROLLUP (a, b)` is the grouping construct, not a call to a function named
        // `rollup` — PostgreSQL lowers the unquoted keyword in this position.
        let parsed = parse_with("SELECT a FROM t GROUP BY ROLLUP (a, b)", PG_SELECT_DIALECT)
            .expect("ROLLUP grouping set parses");
        let group_by = group_by_of(&parsed);
        assert_eq!(group_by.len(), 1, "one GROUP BY item");
        let GroupByItem::Rollup { exprs, .. } = &group_by[0] else {
            panic!("expected a ROLLUP grouping item, got {:?}", group_by[0]);
        };
        assert_eq!(exprs.len(), 2, "both ROLLUP keys are retained");
        assert!(matches!(exprs[0], Expr::Column { .. }));
    }

    #[test]
    fn group_by_cube_parses_as_a_grouping_construct() {
        let parsed = parse_with("SELECT a FROM t GROUP BY CUBE (a, b)", PG_SELECT_DIALECT)
            .expect("CUBE grouping set parses");
        assert!(matches!(
            &group_by_of(&parsed)[0],
            GroupByItem::Cube { exprs, .. } if exprs.len() == 2,
        ));
    }

    #[test]
    fn group_by_grouping_sets_nests_grouping_items() {
        // GROUPING SETS nests PG's `group_by_list`, so ROLLUP / empty / plain-expr
        // items may appear inside (the recursive `group_by_item`).
        let parsed = parse_with(
            "SELECT a FROM t GROUP BY GROUPING SETS (ROLLUP (a, b), (c), ())",
            PG_SELECT_DIALECT,
        )
        .expect("GROUPING SETS parses");
        let GroupByItem::GroupingSets { sets, .. } = &group_by_of(&parsed)[0] else {
            panic!("expected a GROUPING SETS item");
        };
        assert_eq!(sets.len(), 3, "three nested grouping items");
        assert!(matches!(sets[0], GroupByItem::Rollup { .. }));
        assert!(matches!(sets[1], GroupByItem::Expr { .. }));
        assert!(matches!(sets[2], GroupByItem::Empty { .. }));
    }

    #[test]
    fn group_by_empty_grouping_set_parses() {
        // A bare `()` is the empty grouping set (grand total), admitted as a top-level
        // GROUP BY item per PG's `empty_grouping_set`.
        let parsed = parse_with("SELECT a FROM t GROUP BY ()", PG_SELECT_DIALECT)
            .expect("empty grouping set parses");
        assert!(matches!(group_by_of(&parsed)[0], GroupByItem::Empty { .. }));
    }

    #[test]
    fn group_by_plain_expressions_stay_plain_items() {
        let parsed = parse_with("SELECT a FROM t GROUP BY a, b + c", PG_SELECT_DIALECT)
            .expect("plain GROUP BY keys parse");
        let group_by = group_by_of(&parsed);
        assert_eq!(group_by.len(), 2);
        assert!(
            group_by
                .iter()
                .all(|item| matches!(item, GroupByItem::Expr { .. }))
        );
    }

    #[test]
    fn quoted_rollup_in_group_by_stays_a_function_call() {
        // A quoted `"rollup"` is a delimited identifier, never `Keyword::Rollup`, so it
        // is a function call — matching PostgreSQL, which lowers only the bare keyword.
        let parsed = parse_with(
            "SELECT a FROM t GROUP BY \"rollup\" (a, b)",
            PG_SELECT_DIALECT,
        )
        .expect("quoted rollup parses as a function call");
        let GroupByItem::Expr {
            expr: Expr::Function { call, .. },
            ..
        } = &group_by_of(&parsed)[0]
        else {
            panic!("expected a function call, not a grouping construct");
        };
        assert_eq!(parsed.resolver().resolve(call.name.0[0].sym), "rollup");
    }

    #[test]
    fn bare_rollup_column_falls_through_to_an_expression() {
        // `rollup` without a following `(` is an ordinary (unreserved in PG) column
        // reference, not the construct — the `(` is what admits ROLLUP as a grouping
        // set.
        let parsed = parse_with("SELECT a FROM t GROUP BY rollup", PG_SELECT_DIALECT)
            .expect("bare rollup is a column");
        let GroupByItem::Expr {
            expr: Expr::Column { name, .. },
            ..
        } = &group_by_of(&parsed)[0]
        else {
            panic!("expected a bare column");
        };
        assert_eq!(parsed.resolver().resolve(name.0[0].sym), "rollup");
    }

    #[test]
    fn group_by_rollup_is_a_function_call_when_the_gate_is_off() {
        use crate::dialect::MySql;

        // With `grouping_sets` off (MySQL), `ROLLUP (a, b)` falls through to the
        // expression grammar as an ordinary function call — MySQL resolves it as a
        // stored-function reference (`rollup` is non-reserved there). MySQL's own
        // grouping surface is the distinct trailing `WITH ROLLUP`, not modelled here.
        let parsed = parse_with("SELECT a FROM t GROUP BY ROLLUP (a, b)", MySql)
            .expect("MySQL reads rollup as a function call");
        assert!(matches!(
            &group_by_of(&parsed)[0],
            GroupByItem::Expr {
                expr: Expr::Function { .. },
                ..
            },
        ));
    }

    #[test]
    fn group_by_with_rollup_wraps_the_key_list_under_mysql() {
        use crate::dialect::MySql;

        // MySQL's trailing `WITH ROLLUP` applies to the whole key list, so it
        // canonicalizes into one `GroupByItem::Rollup` (spelling `WithRollup`) wrapping
        // every key — the same node `ROLLUP (…)` produces (ADR-0011), tagged so render
        // reproduces the surface.
        let parsed = parse_with("SELECT a FROM t GROUP BY a, b WITH ROLLUP", MySql)
            .expect("MySQL `WITH ROLLUP` parses");
        let group_by = group_by_of(&parsed);
        assert_eq!(
            group_by.len(),
            1,
            "the key list folds into one wrapping item"
        );
        let GroupByItem::Rollup {
            exprs, spelling, ..
        } = &group_by[0]
        else {
            panic!("expected a wrapping ROLLUP item, got {:?}", group_by[0]);
        };
        assert_eq!(*spelling, RollupSpelling::WithRollup);
        assert_eq!(exprs.len(), 2, "both keys are retained inside the wrap");
        assert!(exprs.iter().all(|e| matches!(e, Expr::Column { .. })));
    }

    #[test]
    fn group_by_rollup_round_trips_each_spelling_byte_identically() {
        use crate::dialect::{Lenient, MySql, Postgres};
        use crate::render::Renderer;

        // MySQL's trailing modifier renders back as written (the `WithRollup` tag). MySQL
        // is not itself a render target, so the round-trip renders under Lenient — the
        // permissive superset that also accepts `WITH ROLLUP` — proving the tag, not the
        // target dialect, drives the trailing form.
        let mysql = "SELECT a FROM t GROUP BY a, b WITH ROLLUP";
        let parsed = parse_with(mysql, MySql).expect("MySQL `WITH ROLLUP` parses");
        assert_eq!(
            Renderer::new(Lenient)
                .render_parsed(&parsed)
                .expect("`WITH ROLLUP` renders"),
            mysql,
        );

        // The SQL:1999 item form is unchanged by the added tag (the `Function` default).
        let ansi = "SELECT a FROM t GROUP BY ROLLUP (a, b)";
        let parsed = parse_with(ansi, Postgres).expect("`ROLLUP (…)` parses");
        assert_eq!(
            Renderer::new(Postgres)
                .render_parsed(&parsed)
                .expect("`ROLLUP (…)` renders"),
            ansi,
        );
    }

    #[test]
    fn group_by_with_rollup_is_rejected_where_the_gate_is_off() {
        use crate::dialect::{Ansi, Postgres};

        // PostgreSQL and ANSI spell the super-aggregate `ROLLUP (…)`; with `with_rollup`
        // off the trailing `WITH` is left unconsumed and surfaces as trailing input — a
        // clean parse error, not an over-acceptance. (MySQL-only surface: differential
        // pg parity is N/A, self-attested until the MySQL oracle lands.)
        assert!(
            parse_with("SELECT a FROM t GROUP BY a, b WITH ROLLUP", Postgres).is_err(),
            "PostgreSQL rejects the MySQL `WITH ROLLUP` modifier",
        );
        assert!(
            parse_with("SELECT a FROM t GROUP BY a, b WITH ROLLUP", Ansi).is_err(),
            "ANSI rejects the MySQL `WITH ROLLUP` modifier",
        );
    }

    #[test]
    fn group_by_all_parses_as_the_clause_mode() {
        use crate::dialect::DuckDb;

        let parsed = parse_with("SELECT i, sum(j) FROM t GROUP BY ALL", DuckDb)
            .expect("GROUP BY ALL parses under DuckDb");
        let select = select_of(&parsed);
        assert_eq!(
            select.group_by_all,
            Some(crate::ast::GroupByAllSpelling::Keyword),
            "the mode is set, spelled with the `ALL` keyword",
        );
        assert!(
            select.group_by.is_empty(),
            "the key list stays empty — ALL is a mode of the clause, never an item"
        );
    }

    #[test]
    fn group_by_star_parses_as_the_clause_mode() {
        use crate::dialect::DuckDb;

        // DuckDB's `GROUP BY *` shorthand opens the same mode as `GROUP BY ALL`
        // (probed on 1.5.4: identical result to `GROUP BY ALL`), tagged with its
        // `*` spelling so it round-trips.
        let parsed = parse_with("SELECT i, sum(j) FROM t GROUP BY *", DuckDb)
            .expect("GROUP BY * parses under DuckDb");
        let select = select_of(&parsed);
        assert_eq!(
            select.group_by_all,
            Some(crate::ast::GroupByAllSpelling::Star),
            "the mode is set, spelled with the `*` shorthand",
        );
        assert!(
            select.group_by.is_empty(),
            "the key list stays empty — `*` is the mode, never an item"
        );
    }

    #[test]
    fn group_by_star_rejects_mixing_and_bind_only_forms() {
        use crate::dialect::DuckDb;

        // Probed on DuckDB 1.5.4: `GROUP BY *` is bare-only. The engine binder-rejects
        // `*` beside a sibling item ("STAR expression is not supported here"), and our
        // parse-level contract rejects the same shapes — the mode consumes only its
        // wildcard, so a leftover comma/item/paren fails as trailing input. No
        // over-acceptance beyond the bare form.
        assert!(parse_with("SELECT i FROM t GROUP BY *, i", DuckDb).is_err());
        assert!(parse_with("SELECT i FROM t GROUP BY i, *", DuckDb).is_err());
        assert!(parse_with("SELECT i FROM t GROUP BY ROLLUP (i), *", DuckDb).is_err());
        assert!(parse_with("SELECT i FROM t GROUP BY *(i)", DuckDb).is_err());
    }

    #[test]
    fn group_by_star_is_rejected_where_the_gate_is_off() {
        use crate::dialect::{Ansi, MySql, Postgres, Sqlite};

        // The `*` shorthand rides the same `group_by_all` gate as the keyword; with it
        // off, a bare `*` cannot open a grouping clause — the over-acceptance guard.
        let sql = "SELECT a, count(*) FROM t GROUP BY *";
        assert!(parse_with(sql, Ansi).is_err(), "ANSI rejects GROUP BY *");
        assert!(
            parse_with(sql, Postgres).is_err(),
            "PostgreSQL rejects GROUP BY *"
        );
        assert!(parse_with(sql, MySql).is_err(), "MySQL rejects GROUP BY *");
        assert!(
            parse_with(sql, Sqlite).is_err(),
            "SQLite rejects GROUP BY *"
        );
    }

    #[test]
    fn group_by_all_composes_with_having() {
        use crate::dialect::DuckDb;

        // Probed on DuckDB 1.5.4: `HAVING` follows the mode as it follows a key list.
        let parsed = parse_with(
            "SELECT i, sum(j) FROM t GROUP BY ALL HAVING sum(j) > 1",
            DuckDb,
        )
        .expect("HAVING follows the mode");
        let select = select_of(&parsed);
        assert_eq!(
            select.group_by_all,
            Some(crate::ast::GroupByAllSpelling::Keyword)
        );
        assert!(select.having.is_some());
    }

    #[test]
    fn quoted_all_in_group_by_stays_a_column() {
        use crate::dialect::DuckDb;

        // The disambiguation trap: `ALL` is reserved under DuckDB, so a column named
        // `all` must be quoted — and the quoted spelling tokenizes as an identifier,
        // an ordinary grouping key, exactly as the engine reads it (a bare `all`
        // there is the mode; probed on 1.5.4).
        let parsed = parse_with("SELECT \"all\" FROM t GROUP BY \"all\"", DuckDb)
            .expect("a quoted all is an ordinary key");
        let select = select_of(&parsed);
        assert!(select.group_by_all.is_none());
        assert_eq!(select.group_by.len(), 1);
    }

    #[test]
    fn group_by_all_rejects_mixing_with_keys_and_grouping_sets() {
        use crate::dialect::DuckDb;

        // Probed on DuckDB 1.5.4: `ALL` admits no sibling item in either order — the
        // mode consumes only its keyword, so the leftovers fail as trailing input,
        // the same verdict as the engine's syntax errors.
        assert!(parse_with("SELECT i FROM t GROUP BY ALL, i", DuckDb).is_err());
        assert!(parse_with("SELECT i FROM t GROUP BY i, ALL", DuckDb).is_err());
        assert!(parse_with("SELECT i FROM t GROUP BY ROLLUP (i), ALL", DuckDb).is_err());
        assert!(parse_with("SELECT i FROM t GROUP BY ALL (i)", DuckDb).is_err());
    }

    #[test]
    fn group_by_all_is_rejected_where_the_gate_is_off() {
        use crate::dialect::{Ansi, MySql, Postgres, Sqlite};

        // Every shipped dialect reserves `ALL`, so with the gate off the keyword
        // cannot open a grouping expression — the over-acceptance guard on all four.
        let sql = "SELECT a, count(*) FROM t GROUP BY ALL";
        assert!(parse_with(sql, Ansi).is_err(), "ANSI rejects GROUP BY ALL");
        assert!(
            parse_with(sql, Postgres).is_err(),
            "PostgreSQL rejects GROUP BY ALL"
        );
        assert!(
            parse_with(sql, MySql).is_err(),
            "MySQL rejects GROUP BY ALL"
        );
        assert!(
            parse_with(sql, Sqlite).is_err(),
            "SQLite rejects GROUP BY ALL"
        );
    }

    #[test]
    fn group_by_all_round_trips_byte_identically() {
        use crate::render::Renderer;

        for sql in [
            "SELECT i, sum(j) FROM t GROUP BY ALL",
            "SELECT i, sum(j) FROM t GROUP BY ALL HAVING sum(j) > 1",
            "SELECT i, j FROM t GROUP BY ALL ORDER BY ALL",
        ] {
            let parsed = parse_with(sql, BY_ALL_DIALECT).expect("GROUP BY ALL parses");
            assert_eq!(
                Renderer::new(BY_ALL_DIALECT)
                    .render_parsed(&parsed)
                    .expect("GROUP BY ALL renders"),
                sql,
            );
        }
    }

    #[test]
    fn group_by_star_normalizes_to_all_under_target_dialect() {
        use crate::render::Renderer;

        // The `*` shorthand is a spelling of the `ALL` mode: a target-dialect re-spell
        // (the `Renderer` path) canonicalizes it onto `GROUP BY ALL`, exactly as the
        // spelling-tag doctrine prescribes (byte-exact `*` replay is the source-fidelity
        // lane's job — see the ast render tests). The keyword form is already canonical.
        for (source, expected) in [
            (
                "SELECT i, sum(j) FROM t GROUP BY *",
                "SELECT i, sum(j) FROM t GROUP BY ALL",
            ),
            (
                "SELECT i, sum(j) FROM t GROUP BY * HAVING sum(j) > 1",
                "SELECT i, sum(j) FROM t GROUP BY ALL HAVING sum(j) > 1",
            ),
        ] {
            let parsed = parse_with(source, BY_ALL_DIALECT).expect("GROUP BY * parses");
            assert_eq!(
                Renderer::new(BY_ALL_DIALECT)
                    .render_parsed(&parsed)
                    .expect("GROUP BY * renders"),
                expected,
            );
        }
    }

    #[test]
    fn group_by_distinct_quantifier_prefixes_the_key_list() {
        use crate::dialect::Postgres;

        // PostgreSQL's SQL:2016 grouping-set quantifier: `DISTINCT` prefixes a non-empty
        // grouping list, recorded on `group_by_quantifier` while the items parse normally
        // (probed on pg_query PG-17: `GROUP BY DISTINCT a, b` accepts).
        let parsed = parse_with("SELECT a FROM t GROUP BY DISTINCT a, b", Postgres)
            .expect("GROUP BY DISTINCT parses under PostgreSQL");
        let select = select_of(&parsed);
        assert_eq!(select.group_by_quantifier, Some(SetQuantifier::Distinct));
        assert!(
            select.group_by_all.is_none(),
            "the quantifier is not the DuckDB mode"
        );
        assert_eq!(select.group_by.len(), 2, "both grouping keys are retained");
    }

    #[test]
    fn group_by_quantifier_governs_grouping_set_constructs() {
        use crate::dialect::Postgres;

        // The quantifier admits the grouping-set constructs after it (probed on pg_query
        // PG-17: `GROUP BY {ALL | DISTINCT} rollup(…) / grouping sets (…) / ()`).
        let all = parse_with("SELECT a FROM t GROUP BY ALL ROLLUP (a, b)", Postgres)
            .expect("GROUP BY ALL over ROLLUP parses");
        let select = select_of(&all);
        assert_eq!(select.group_by_quantifier, Some(SetQuantifier::All));
        assert!(matches!(select.group_by[0], GroupByItem::Rollup { .. }));

        let distinct = parse_with(
            "SELECT a FROM t GROUP BY DISTINCT GROUPING SETS ((a), (b)), ()",
            Postgres,
        )
        .expect("GROUP BY DISTINCT over GROUPING SETS parses");
        let select = select_of(&distinct);
        assert_eq!(select.group_by_quantifier, Some(SetQuantifier::Distinct));
        assert!(matches!(
            select.group_by[0],
            GroupByItem::GroupingSets { .. }
        ));
        assert!(matches!(select.group_by[1], GroupByItem::Empty { .. }));
    }

    #[test]
    fn group_by_quantifier_requires_a_grouping_list() {
        use crate::dialect::Postgres;

        // The quantifier is a prefix, not a standalone clause: PostgreSQL rejects a bare
        // `GROUP BY {ALL | DISTINCT}`, a trailing quantifier, and a doubled one (all
        // "syntax error" on pg_query PG-17). This is exactly what keeps the quantifier
        // MECE with DuckDB's standalone `GROUP BY ALL` mode.
        for sql in [
            "SELECT a FROM t GROUP BY DISTINCT",
            "SELECT a FROM t GROUP BY ALL",
            "SELECT a FROM t GROUP BY a DISTINCT",
            "SELECT a FROM t GROUP BY DISTINCT ALL a",
        ] {
            assert!(
                parse_with(sql, Postgres).is_err(),
                "PostgreSQL rejects `{sql}` (the quantifier needs a following list)",
            );
        }
    }

    #[test]
    fn group_by_quantifier_is_rejected_where_the_gate_is_off() {
        use crate::dialect::{Ansi, DuckDb, MySql, Sqlite};

        // Every non-PostgreSQL shipped dialect reserves `DISTINCT`/`ALL`, so with the
        // gate off the keyword cannot open a grouping expression — the over-acceptance
        // guard. (DuckDB's own `GROUP BY ALL` mode is the standalone bare form, tested
        // separately; `GROUP BY DISTINCT a` has no DuckDB reading.)
        let sql = "SELECT a FROM t GROUP BY DISTINCT a";
        assert!(
            parse_with(sql, Ansi).is_err(),
            "ANSI rejects the quantifier"
        );
        assert!(
            parse_with(sql, MySql).is_err(),
            "MySQL rejects the quantifier"
        );
        assert!(
            parse_with(sql, Sqlite).is_err(),
            "SQLite rejects the quantifier"
        );
        assert!(
            parse_with(sql, DuckDb).is_err(),
            "DuckDB has no GROUP BY DISTINCT quantifier"
        );
    }

    #[test]
    fn group_by_quantifier_and_duckdb_mode_disambiguate_under_lenient() {
        use crate::dialect::Lenient;

        // The crux of the two-flag design: Lenient enables BOTH the PostgreSQL quantifier
        // and DuckDB's `GROUP BY ALL` mode, and they stay MECE by lookahead — a bare
        // `GROUP BY ALL` is the mode (empty item list), while `GROUP BY ALL <items>` is
        // the quantifier prefixing the list. No genuine ambiguity, so the quantifier
        // ships on for Lenient (the honest superset) rather than being disabled.
        let mode = parse_with("SELECT i, sum(j) FROM t GROUP BY ALL", Lenient)
            .expect("bare GROUP BY ALL is the DuckDB mode under Lenient");
        let select = select_of(&mode);
        assert_eq!(
            select.group_by_all,
            Some(crate::ast::GroupByAllSpelling::Keyword),
            "bare ALL is the mode"
        );
        assert_eq!(select.group_by_quantifier, None);
        assert!(select.group_by.is_empty());

        let quantifier = parse_with("SELECT a FROM t GROUP BY ALL a, b", Lenient)
            .expect("GROUP BY ALL over a list is the PostgreSQL quantifier under Lenient");
        let select = select_of(&quantifier);
        assert!(
            select.group_by_all.is_none(),
            "ALL over a list is not the mode"
        );
        assert_eq!(select.group_by_quantifier, Some(SetQuantifier::All));
        assert_eq!(select.group_by.len(), 2);

        let distinct = parse_with("SELECT a FROM t GROUP BY DISTINCT a", Lenient)
            .expect("GROUP BY DISTINCT is the quantifier under Lenient");
        assert_eq!(
            select_of(&distinct).group_by_quantifier,
            Some(SetQuantifier::Distinct)
        );
    }

    #[test]
    fn group_by_quantifier_round_trips_byte_identically() {
        use crate::dialect::Postgres;
        use crate::render::Renderer;

        // The quantifier renders back before the item list; `None` (no quantifier) stays
        // absent, so an ordinary `GROUP BY a` is unchanged.
        for sql in [
            "SELECT a FROM t GROUP BY DISTINCT a, b",
            "SELECT a FROM t GROUP BY ALL a, b",
            "SELECT a FROM t GROUP BY DISTINCT ROLLUP (a, b)",
            "SELECT a FROM t GROUP BY a",
        ] {
            let parsed = parse_with(sql, Postgres).expect("quantified GROUP BY parses");
            assert_eq!(
                Renderer::new(Postgres)
                    .render_parsed(&parsed)
                    .expect("quantified GROUP BY renders"),
                sql,
            );
        }
    }

    #[test]
    fn with_rollup_requires_plain_grouping_keys() {
        use crate::dialect::Lenient;

        // `WITH ROLLUP` wraps plain keys only. Lenient enables both `grouping_sets` and
        // `with_rollup`, so a grouping-set item can reach the wrap — it is a clean error
        // rather than a nonsensical rollup-of-a-rollup.
        let err = parse_with(
            "SELECT a FROM t GROUP BY ROLLUP (a, b) WITH ROLLUP",
            Lenient,
        )
        .expect_err("a grouping-set key before `WITH ROLLUP` is rejected");
        assert!(
            err.to_string().contains("WITH ROLLUP"),
            "the error names the offending modifier, got: {err}",
        );
    }

    /// The name + inheritance of the sole relation of a `TABLE`-command Select.
    fn table_command_relation(parsed: &Parsed) -> &TableFactor<NoExt> {
        let select = select_of(parsed);
        assert_eq!(
            select.spelling,
            SelectSpelling::TableCommand,
            "the body is tagged as a TABLE command",
        );
        assert_eq!(select.projection.len(), 1, "one wildcard projection item");
        assert!(matches!(select.projection[0], SelectItem::Wildcard { .. }));
        assert_eq!(select.from.len(), 1, "the one named relation");
        &select.from[0].relation
    }

    #[test]
    fn table_command_lowers_to_a_wildcard_star_projection() {
        // `TABLE t` is `SELECT * FROM t`, so it canonicalizes to a wildcard projection
        // over the one relation, tagged `TableCommand`. It is ungated (standard SQL
        // `<explicit table>`), so a bare `TABLE t` parses even under the ANSI test dialect.
        let parsed = parse_with("TABLE t", TestDialect).expect("TABLE command parses");
        let TableFactor::Table {
            name,
            inheritance: RelationInheritance::Plain,
            alias: None,
            sample: None,
            ..
        } = table_command_relation(&parsed)
        else {
            panic!("expected a plain named relation with no alias or sample");
        };
        assert_eq!(name.0.len(), 1);
        assert_eq!(parsed.resolver().resolve(name.0[0].sym), "t");
    }

    #[test]
    fn table_command_keeps_qualified_names() {
        let parsed = parse_with("TABLE s.t", TestDialect).expect("qualified TABLE parses");
        let TableFactor::Table { name, .. } = table_command_relation(&parsed) else {
            panic!("expected a named relation");
        };
        assert_eq!(name.0.len(), 2, "schema-qualified");
        assert_eq!(parsed.resolver().resolve(name.0[0].sym), "s");
        assert_eq!(parsed.resolver().resolve(name.0[1].sym), "t");
    }

    #[test]
    fn table_command_carries_postgres_inheritance_markers() {
        use crate::dialect::Postgres;

        // The `ONLY`/`*` `relation_expr` markers ride the PostgreSQL inheritance gate,
        // so they parse under the full Postgres preset (not the ANSI-based test dialect).
        let only = parse_with("TABLE ONLY t", Postgres).expect("TABLE ONLY parses");
        assert!(matches!(
            table_command_relation(&only),
            TableFactor::Table {
                inheritance: RelationInheritance::Only(OnlySyntax::Bare),
                ..
            },
        ));

        let only_paren = parse_with("TABLE ONLY (t)", Postgres).expect("TABLE ONLY (t) parses");
        assert!(matches!(
            table_command_relation(&only_paren),
            TableFactor::Table {
                inheritance: RelationInheritance::Only(OnlySyntax::Parenthesized),
                ..
            },
        ));

        let star = parse_with("TABLE t *", Postgres).expect("TABLE t * parses");
        assert!(matches!(
            table_command_relation(&star),
            TableFactor::Table {
                inheritance: RelationInheritance::Descendants,
                ..
            },
        ));
    }

    #[test]
    fn table_command_rejects_alias_and_trailing_clauses() {
        // `TABLE relation_expr` takes only the bare relation — no alias, no `WHERE`, no
        // parenthesized subquery, and at most three name parts — matching PostgreSQL.
        for sql in [
            "TABLE t x",
            "TABLE t AS x",
            "TABLE t WHERE a = 1",
            "TABLE (SELECT 1)",
            "TABLE a.b.c.d",
            "TABLE",
        ] {
            assert!(
                parse_with(sql, TestDialect).is_err(),
                "{sql:?} must be rejected",
            );
        }
    }

    #[test]
    fn table_command_inheritance_markers_need_the_gate() {
        // Under a dialect without PostgreSQL inheritance (the ANSI test dialect), the
        // `ONLY`/`*` markers are rejected while the plain `TABLE t` still parses.
        assert!(parse_with("TABLE ONLY t", TestDialect).is_err());
        assert!(parse_with("TABLE t *", TestDialect).is_err());
        assert!(parse_with("TABLE t", TestDialect).is_ok());
    }

    #[test]
    fn table_command_round_trips_each_spelling() {
        use crate::dialect::Postgres;
        use crate::render::Renderer;

        for sql in [
            "TABLE t",
            "TABLE s.t",
            "TABLE ONLY t",
            "TABLE ONLY (t)",
            "TABLE t *",
        ] {
            let parsed = parse_with(sql, Postgres).expect("TABLE parses");
            assert_eq!(
                Renderer::new(Postgres)
                    .render_parsed(&parsed)
                    .expect("TABLE renders"),
                sql,
                "the TABLE command round-trips its short-form spelling",
            );
        }
    }

    #[test]
    fn empty_target_list_parses_under_the_gate() {
        // libpg_query's raw grammar makes the projection optional before any clause, so
        // a bare `SELECT` and each empty-projection-then-clause form parse under Postgres.
        for sql in [
            "SELECT",
            "SELECT FROM t",
            "SELECT WHERE a = 1",
            "SELECT GROUP BY a",
            "SELECT ORDER BY 1",
            "SELECT ALL FROM t",
        ] {
            let parsed = parse_with(sql, PG_SELECT_DIALECT)
                .unwrap_or_else(|err| panic!("{sql:?} should parse: {err:?}"));
            assert!(
                select_of(&parsed).projection.is_empty(),
                "{sql:?} has an empty projection",
            );
        }
    }

    #[test]
    fn empty_target_list_is_rejected_without_the_gate() {
        // ANSI/MySQL require ≥1 select item, so `FROM` — a reserved keyword, not an
        // expression — is a parse error where the projection is required.
        assert!(parse_with("SELECT FROM t", TestDialect).is_err());
        assert!(parse_with("SELECT", TestDialect).is_err());
    }

    #[test]
    fn empty_target_list_still_requires_a_list_after_distinct() {
        // PostgreSQL splits `SELECT opt_all_clause opt_target_list` (empty allowed) from
        // `SELECT distinct_clause target_list` (required), so a `DISTINCT` head with no
        // items is a syntax error even under the empty-target-list gate.
        assert!(parse_with("SELECT DISTINCT", PG_SELECT_DIALECT).is_err());
        assert!(parse_with("SELECT DISTINCT FROM t", PG_SELECT_DIALECT).is_err());
        // A plain or explicit-`ALL` head still admits the empty list.
        assert!(parse_with("SELECT ALL", PG_SELECT_DIALECT).is_ok());
    }

    #[test]
    fn empty_target_list_round_trips_as_a_bare_select() {
        use crate::dialect::Postgres;
        use crate::render::Renderer;

        for sql in ["SELECT", "SELECT FROM t", "SELECT WHERE a = 1"] {
            let parsed = parse_with(sql, Postgres).expect("empty SELECT parses");
            assert_eq!(
                Renderer::new(Postgres)
                    .render_parsed(&parsed)
                    .expect("empty SELECT renders"),
                sql,
            );
        }
    }

    #[test]
    fn qualify_clause_parses_under_the_gate() {
        // After a GROUP BY key the word cannot be an alias, so the flag alone admits
        // the clause even without DuckDB's keyword reservation.
        let parsed = parse_with(
            "SELECT a FROM t GROUP BY a QUALIFY row_number() OVER () = 1",
            QUALIFY_DIALECT,
        )
        .expect("QUALIFY parses under the gate");
        let select = select_of(&parsed);
        assert!(
            select.qualify.is_some(),
            "the QUALIFY predicate is captured"
        );
        assert!(
            select.having.is_none(),
            "QUALIFY is its own slot, never folded into HAVING"
        );
    }

    #[test]
    fn qualify_is_rejected_without_the_gate() {
        // With the flag off (ANSI) the keyword is left unconsumed after the GROUP BY
        // key, so the trailing `QUALIFY …` is a clean parse error — the
        // over-acceptance guard.
        assert!(
            parse_with(
                "SELECT a FROM t GROUP BY a QUALIFY row_number() OVER () = 1",
                TestDialect,
            )
            .is_err(),
            "ANSI rejects the QUALIFY clause",
        );
    }

    // --- Hive/Spark LATERAL VIEW (planner-parity-select-lateral-view) ---------

    /// ANSI plus the `lateral_view_clause` flag alone, isolating the clause gate from
    /// the rest of the (feature-gated) Hive/Databricks presets. Renders for the
    /// exact-text round-trip checks.
    const LATERAL_VIEW_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet =
            FeatureSet::ANSI.with(FeatureDelta::EMPTY.select_syntax(SelectSyntax {
                lateral_view_clause: true,
                ..SelectSyntax::ANSI
            }));
        FeatureDialect {
            features: &FEATURES,
        }
    };

    /// ANSI plus BOTH the `lateral_view_clause` and derived-table `lateral` gates —
    /// the Lenient combination — used to prove the shared `LATERAL` lead partitions
    /// unambiguously between the two (position + `VIEW` follow token).
    const LATERAL_BOTH_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet = FeatureSet::ANSI.with(
            FeatureDelta::EMPTY
                .select_syntax(SelectSyntax {
                    lateral_view_clause: true,
                    ..SelectSyntax::ANSI
                })
                .table_factor_syntax(TableFactorSyntax {
                    lateral: true,
                    // The lateral *function* factor (`FROM t, LATERAL f(x) v`) needs
                    // the table-function grammar on to exercise the factor side.
                    table_functions: true,
                    ..TableFactorSyntax::ANSI
                }),
        );
        FeatureDialect {
            features: &FEATURES,
        }
    };

    #[test]
    fn lateral_view_parses_and_round_trips_under_the_gate() {
        use crate::render::Renderer;

        for sql in [
            "SELECT a FROM t LATERAL VIEW explode(col) v",
            "SELECT a FROM t LATERAL VIEW OUTER explode(col) v",
            "SELECT a FROM t LATERAL VIEW explode(col) v AS c",
            "SELECT a FROM t LATERAL VIEW OUTER explode(col) v AS c1, c2",
            "SELECT a FROM t LATERAL VIEW json_tuple(j, 'k1', 'k2') jt AS k1, k2",
            "SELECT a FROM t LATERAL VIEW db.explode(col) v AS c",
            "SELECT a FROM t LATERAL VIEW stack(2, 'a', 1, 'b', 2) s AS key, value",
            // Repeatable, and a later view may reference an earlier one's output.
            "SELECT a FROM t LATERAL VIEW explode(m) kv AS k, vs LATERAL VIEW explode(vs) x AS v",
            // Position: after the whole FROM list (joins included), before WHERE.
            "SELECT a FROM t JOIN u ON t.id = u.id LATERAL VIEW explode(col) v AS c WHERE v.c = 1",
            "SELECT a FROM t, u LATERAL VIEW explode(col) v GROUP BY a",
        ] {
            let parsed =
                parse_with(sql, LATERAL_VIEW_DIALECT).unwrap_or_else(|e| panic!("{sql:?}: {e:?}"));
            assert!(
                !select_of(&parsed).lateral_views.is_empty(),
                "{sql:?} populates lateral_views",
            );
            assert_eq!(
                Renderer::new(LATERAL_VIEW_DIALECT)
                    .render_parsed(&parsed)
                    .expect("renders"),
                sql,
                "{sql:?} round-trips",
            );
        }

        // Structural check: OUTER, the generator call, the alias, and the column
        // aliases land as typed fields.
        let parsed = parse_with(
            "SELECT a FROM t LATERAL VIEW OUTER explode(m) kv AS k, v",
            LATERAL_VIEW_DIALECT,
        )
        .expect("parses");
        let views = &select_of(&parsed).lateral_views;
        assert_eq!(views.len(), 1);
        let view = &views[0];
        assert!(view.outer);
        assert_eq!(view.function.args.len(), 1);
        assert_eq!(view.columns.len(), 2);
    }

    #[test]
    fn lateral_view_as_keyword_is_optional_and_canonicalized() {
        use crate::render::Renderer;

        // Spark's grammar spells the column-alias `AS` as optional (`AS?`); the bare
        // spelling parses to the same shape and re-renders with the canonical `AS`
        // (a structural, not byte-exact, round-trip). Hive proper requires `AS` —
        // the documented conservative-direction over-acceptance.
        let bare = parse_with(
            "SELECT a FROM t LATERAL VIEW explode(m) kv k, v",
            LATERAL_VIEW_DIALECT,
        )
        .expect("the AS-less Spark spelling parses");
        assert_eq!(select_of(&bare).lateral_views[0].columns.len(), 2);
        assert_eq!(
            Renderer::new(LATERAL_VIEW_DIALECT)
                .render_parsed(&bare)
                .expect("renders"),
            "SELECT a FROM t LATERAL VIEW explode(m) kv AS k, v",
            "the bare spelling canonicalizes to AS",
        );
    }

    #[test]
    fn lateral_view_requires_a_generator_call_and_table_alias() {
        // The generator must be a parenthesized call…
        assert!(
            parse_with(
                "SELECT a FROM t LATERAL VIEW explode v",
                LATERAL_VIEW_DIALECT
            )
            .is_err(),
            "a bare generator name without `(…)` rejects",
        );
        // …without the windowed/aggregate wrapper clauses…
        assert!(
            parse_with(
                "SELECT a FROM t LATERAL VIEW rank() OVER () v",
                LATERAL_VIEW_DIALECT,
            )
            .is_err(),
            "a windowed generator rejects",
        );
        // …and the table alias is required (both engine grammars make it
        // non-optional): a reserved clause keyword cannot fill the slot.
        assert!(
            parse_with(
                "SELECT a FROM t LATERAL VIEW explode(col) WHERE a = 1",
                LATERAL_VIEW_DIALECT,
            )
            .is_err(),
            "a missing table alias rejects",
        );
    }

    #[test]
    fn lateral_view_is_rejected_without_the_gate_and_without_from() {
        // With the flag off (ANSI) the post-FROM `LATERAL` is left unconsumed and the
        // trailing clause is a clean parse error — the over-acceptance guard.
        assert!(
            parse_with("SELECT a FROM t LATERAL VIEW explode(col) v", TestDialect).is_err(),
            "ANSI rejects the LATERAL VIEW clause",
        );
        // Hive/Spark attach lateral views inside the FROM clause, so a FROM-less body
        // never reads them even with the gate on.
        assert!(
            parse_with("SELECT 1 LATERAL VIEW explode(col) v", LATERAL_VIEW_DIALECT).is_err(),
            "a FROM-less body rejects the clause",
        );
    }

    #[test]
    fn lateral_view_and_lateral_factor_partition_on_position_and_follow_token() {
        use crate::render::Renderer;

        // Under the Lenient combination both `LATERAL` gates are on. At a table-factor
        // head, `LATERAL` (no `VIEW` follow) is the derived-table/function factor…
        let factor = parse_with("SELECT a FROM t, LATERAL f(t.x) v", LATERAL_BOTH_DIALECT)
            .expect("the lateral function factor parses");
        let factor_select = select_of(&factor);
        assert_eq!(factor_select.from.len(), 2);
        assert!(
            matches!(
                factor_select.from[1].relation,
                TableFactor::Function { lateral: true, .. }
            ),
            "the factor grammar claims the factor-head LATERAL",
        );
        assert!(factor_select.lateral_views.is_empty());

        // …after the complete FROM list, `LATERAL VIEW` is the view clause…
        let clause = parse_with(
            "SELECT a FROM t LATERAL VIEW explode(col) v AS c",
            LATERAL_BOTH_DIALECT,
        )
        .expect("the view clause parses with both gates on");
        assert_eq!(select_of(&clause).lateral_views.len(), 1);

        // …and both compose on one query, each claiming its own `LATERAL`.
        let both = parse_with(
            "SELECT a FROM t, LATERAL f(t.x) v LATERAL VIEW explode(v.col) w AS c",
            LATERAL_BOTH_DIALECT,
        )
        .expect("factor + clause compose");
        let both_select = select_of(&both);
        assert!(matches!(
            both_select.from[1].relation,
            TableFactor::Function { lateral: true, .. }
        ));
        assert_eq!(both_select.lateral_views.len(), 1);
        assert_eq!(
            Renderer::new(LATERAL_BOTH_DIALECT)
                .render_parsed(&both)
                .expect("renders"),
            // The function factor canonicalizes its bare alias to `AS`; the view
            // clause's alias position has no `AS` in either engine grammar.
            "SELECT a FROM t, LATERAL f(t.x) AS v LATERAL VIEW explode(v.col) w AS c",
        );

        // A `LATERAL VIEW` head at a comma-item position is invalid in every engine
        // (Hive/Spark attach views to a preceding source, never after `,`), and the
        // factor grammar's reject keeps it a clean error rather than a silent misread.
        assert!(
            parse_with(
                "SELECT a FROM t, LATERAL VIEW explode(col) v",
                LATERAL_BOTH_DIALECT,
            )
            .is_err(),
            "a comma-position LATERAL VIEW rejects",
        );
    }

    // --- Oracle/Snowflake CONNECT BY / START WITH (planner-parity-select-connect-by) ---

    /// ANSI plus the `connect_by_clause` flag alone, isolating the hierarchical query
    /// clause gate from the rest of the (feature-gated) Snowflake preset. Renders for the
    /// exact-text round-trip checks.
    const CONNECT_BY_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet =
            FeatureSet::ANSI.with(FeatureDelta::EMPTY.select_syntax(SelectSyntax {
                connect_by_clause: true,
                ..SelectSyntax::ANSI
            }));
        FeatureDialect {
            features: &FEATURES,
        }
    };

    #[test]
    fn connect_by_parses_and_round_trips_under_the_gate() {
        use crate::render::Renderer;

        for sql in [
            // CONNECT BY alone; PRIOR on either side of the equality.
            "SELECT a FROM t CONNECT BY PRIOR id = pid",
            "SELECT a FROM t CONNECT BY id = PRIOR pid",
            // START WITH leads (Snowflake's documented order).
            "SELECT a FROM t START WITH pid IS NULL CONNECT BY PRIOR id = pid",
            // START WITH trails (Oracle's other legal order) — the order round-trips.
            "SELECT a FROM t CONNECT BY PRIOR id = pid START WITH pid IS NULL",
            // NOCYCLE (Oracle; the documented over-acceptance under the one gate).
            "SELECT a FROM t CONNECT BY NOCYCLE PRIOR id = pid",
            "SELECT a FROM t START WITH id = 1 CONNECT BY NOCYCLE PRIOR id = pid",
            // Multi-conjunct condition; PRIOR is an ordinary prefix operator in it.
            "SELECT a FROM t CONNECT BY PRIOR id = pid AND lvl < 5",
            // Position: after WHERE and before GROUP BY.
            "SELECT a FROM t WHERE a > 0 CONNECT BY PRIOR id = pid",
            "SELECT dept, COUNT(*) FROM t CONNECT BY PRIOR id = pid GROUP BY dept",
        ] {
            let parsed =
                parse_with(sql, CONNECT_BY_DIALECT).unwrap_or_else(|e| panic!("{sql:?}: {e:?}"));
            assert!(
                select_of(&parsed).connect_by.is_some(),
                "{sql:?} populates connect_by",
            );
            assert_eq!(
                Renderer::new(CONNECT_BY_DIALECT)
                    .render_parsed(&parsed)
                    .expect("renders"),
                sql,
                "{sql:?} round-trips",
            );
        }

        // Structural check: START WITH, NOCYCLE, the order tag, and the PRIOR operator
        // land as typed fields.
        let parsed = parse_with(
            "SELECT a FROM t START WITH pid IS NULL CONNECT BY NOCYCLE PRIOR id = pid",
            CONNECT_BY_DIALECT,
        )
        .expect("parses");
        let clause = select_of(&parsed)
            .connect_by
            .as_ref()
            .expect("connect_by is set");
        assert!(clause.start_with.is_some());
        assert!(clause.nocycle);
        assert!(clause.start_with_leads);
        assert!(matches!(
            clause.connect_by,
            Expr::BinaryOp { ref left, .. }
                if matches!(**left, Expr::UnaryOp { op: crate::ast::UnaryOperator::Prior, .. })
        ));
    }

    #[test]
    fn connect_by_start_with_order_is_preserved() {
        use crate::render::Renderer;

        // Both legal orders parse to distinct order tags and each re-renders in its own
        // written order (the spelling-fidelity round-trip).
        let leads = parse_with(
            "SELECT a FROM t START WITH id = 1 CONNECT BY PRIOR id = pid",
            CONNECT_BY_DIALECT,
        )
        .expect("START WITH first parses");
        assert!(
            select_of(&leads)
                .connect_by
                .as_ref()
                .unwrap()
                .start_with_leads
        );

        let trails = parse_with(
            "SELECT a FROM t CONNECT BY PRIOR id = pid START WITH id = 1",
            CONNECT_BY_DIALECT,
        )
        .expect("CONNECT BY first parses");
        assert!(
            !select_of(&trails)
                .connect_by
                .as_ref()
                .unwrap()
                .start_with_leads
        );

        for (parsed, expected) in [
            (
                &leads,
                "SELECT a FROM t START WITH id = 1 CONNECT BY PRIOR id = pid",
            ),
            (
                &trails,
                "SELECT a FROM t CONNECT BY PRIOR id = pid START WITH id = 1",
            ),
        ] {
            assert_eq!(
                Renderer::new(CONNECT_BY_DIALECT)
                    .render_parsed(parsed)
                    .expect("renders"),
                expected,
            );
        }
    }

    #[test]
    fn prior_binds_tighter_than_comparison() {
        // `PRIOR a = b` groups as `(PRIOR a) = b` (PRIOR at the unary-sign rank), so the
        // fully-parenthesized render brackets the operator, never the whole equality.
        use crate::render::{RenderConfig, RenderMode, Renderer};
        let parsed = parse_with(
            "SELECT a FROM t CONNECT BY PRIOR id = pid",
            CONNECT_BY_DIALECT,
        )
        .expect("parses");
        let renderer = Renderer::with_config(
            CONNECT_BY_DIALECT,
            RenderConfig {
                mode: RenderMode::Parenthesized,
                ..RenderConfig::default()
            },
        );
        assert_eq!(
            renderer.render_parsed(&parsed).expect("renders"),
            // Fully-parenthesized mode brackets every operator; `(PRIOR id)` nests inside
            // the equality, proving PRIOR binds tighter than `=`.
            "SELECT a FROM t CONNECT BY ((PRIOR id) = pid)",
        );
    }

    #[test]
    fn prior_is_scoped_to_the_connect_by_condition() {
        // Outside a CONNECT BY condition `PRIOR` stays an ordinary (non-reserved) column
        // name — in the projection, in START WITH, and inside a subquery of the condition.
        let projection = parse_with(
            "SELECT prior FROM t CONNECT BY PRIOR id = pid",
            CONNECT_BY_DIALECT,
        )
        .expect("bare `prior` in the projection is a column");
        assert!(select_of(&projection).connect_by.is_some());

        // START WITH does not recognize PRIOR — a bare `prior` there is a column.
        parse_with(
            "SELECT a FROM t START WITH prior = 1 CONNECT BY id = pid",
            CONNECT_BY_DIALECT,
        )
        .expect("bare `prior` in START WITH is a column");

        // A nested query inside the CONNECT BY condition resets the context, so its own
        // `prior` is a column, not the operator.
        parse_with(
            "SELECT a FROM t CONNECT BY id = (SELECT prior FROM u)",
            CONNECT_BY_DIALECT,
        )
        .expect("bare `prior` in a subquery of the condition is a column");
    }

    #[test]
    fn connect_by_is_rejected_without_the_gate() {
        // With the flag off (the stock ANSI TestDialect) the post-WHERE `CONNECT BY` is
        // left unconsumed and the trailing clause is a clean parse error — the
        // over-acceptance guard.
        assert!(
            parse_with("SELECT a FROM t CONNECT BY PRIOR id = pid", TestDialect).is_err(),
            "ANSI rejects the CONNECT BY clause",
        );
        assert!(
            parse_with(
                "SELECT a FROM t START WITH id = 1 CONNECT BY id = pid",
                TestDialect
            )
            .is_err(),
            "ANSI rejects the START WITH clause",
        );
    }

    // --- DuckDB FROM-first SELECT (duckdb-from-first-select) ------------------

    #[test]
    fn from_first_tags_the_spelling_and_keeps_the_canonical_body() {
        // `FROM t SELECT a` is the ordinary Select body written FROM-first: the projection
        // and FROM populate exactly as for `SELECT a FROM t`; only the surface tag differs.
        let from_first =
            parse_with("FROM t SELECT a", FROM_FIRST_DIALECT).expect("FROM-first parses");
        let ff = select_of(&from_first);
        assert_eq!(ff.spelling, SelectSpelling::FromFirst);
        assert_eq!(ff.projection.len(), 1);
        assert!(matches!(ff.projection[0], SelectItem::Expr { .. }));
        assert_eq!(ff.from.len(), 1);
        assert!(ff.into.is_none() && !ff.straight_join);

        let select_first =
            parse_with("SELECT a FROM t", FROM_FIRST_DIALECT).expect("SELECT-first parses");
        let sf = select_of(&select_first);
        assert_eq!(sf.spelling, SelectSpelling::Select);
        assert_eq!(ff.projection.len(), sf.projection.len());
        assert_eq!(ff.from.len(), sf.from.len());
    }

    #[test]
    fn bare_from_is_an_implicit_wildcard() {
        // Bare `FROM t` (no SELECT) canonicalizes to `SELECT * FROM t` plus the tag.
        let parsed = parse_with("FROM t", FROM_FIRST_DIALECT).expect("bare FROM parses");
        let select = select_of(&parsed);
        assert_eq!(select.spelling, SelectSpelling::FromFirst);
        assert!(matches!(
            select.projection.as_slice(),
            [SelectItem::Wildcard { .. }]
        ));
        assert_eq!(select.from.len(), 1);
        assert!(select.selection.is_none() && select.distinct.is_none());
    }

    #[test]
    fn from_first_composes_the_full_tail() {
        // The projection sits right after FROM; WHERE/GROUP BY/HAVING then parse in
        // ordinary order after it.
        let parsed = parse_with(
            "FROM t SELECT a, sum(b) WHERE a > 1 GROUP BY a HAVING sum(b) > 2",
            FROM_FIRST_DIALECT,
        )
        .expect("FROM-first with a full tail parses");
        let select = select_of(&parsed);
        assert_eq!(select.spelling, SelectSpelling::FromFirst);
        assert!(select.selection.is_some(), "WHERE parsed");
        assert_eq!(select.group_by.len(), 1, "GROUP BY key parsed");
        assert!(select.having.is_some(), "HAVING parsed");

        // GROUP BY ALL co-occurs with FROM-first heavily in the corpus.
        let by_all = parse_with("FROM t GROUP BY ALL", FROM_FIRST_DIALECT)
            .expect("FROM t GROUP BY ALL parses");
        let by_all = select_of(&by_all);
        assert!(by_all.group_by_all.is_some() && by_all.group_by.is_empty());
    }

    #[test]
    fn from_first_rejected_when_flag_off() {
        use crate::dialect::{Ansi, MySql, Postgres, Sqlite};
        // A statement-position FROM must stay a clean parse error everywhere the gate is
        // off — the over-acceptance guard the differential oracle relies on.
        for sql in ["FROM t SELECT a", "FROM t", "FROM t WHERE a > 1"] {
            assert!(parse_with(sql, Ansi).is_err(), "Ansi rejects {sql:?}");
            assert!(
                parse_with(sql, Postgres).is_err(),
                "Postgres rejects {sql:?}"
            );
            assert!(parse_with(sql, MySql).is_err(), "MySQL rejects {sql:?}");
            assert!(parse_with(sql, Sqlite).is_err(), "SQLite rejects {sql:?}");
        }
    }

    #[test]
    fn from_first_projection_binds_only_immediately_after_from() {
        // DuckDB parses a projection only immediately after the FROM clause; a `SELECT`
        // that trails `WHERE`/`GROUP BY` does not join the FROM-first body (`FROM t WHERE x
        // SELECT y` / `FROM t GROUP BY a SELECT a` are single-statement syntax errors;
        // probed on 1.5.4). Separator-less, these now reject on our side too — the top-level
        // statement list is `;`-delimited (pg-do-statement-separator-divergence), so a trailing
        // `SELECT` with no `;` is a syntax error rather than being silently split off, matching
        // DuckDB's single-statement rejection.
        for sql in ["FROM t WHERE a > 1 SELECT a", "FROM t GROUP BY a SELECT a"] {
            assert!(
                parse_with(sql, FROM_FIRST_DIALECT).is_err(),
                "separator-less juxtaposed statements reject in {sql:?}",
            );
        }
        // The projection-binding property, verified on the `;`-separated form: our from-first
        // body never binds a post-tail `SELECT` — the leading `FROM t` keeps its implicit `*`,
        // the tail consumes `WHERE`/`GROUP BY`, and the `SELECT` is the next statement rather
        // than the projection, so the FROM-first select still carries the wildcard, not `a`.
        for sql in [
            "FROM t WHERE a > 1; SELECT a",
            "FROM t GROUP BY a; SELECT a",
        ] {
            let parsed =
                parse_with(sql, FROM_FIRST_DIALECT).expect("`;`-separated statements parse");
            let first = select_of(&parsed);
            assert_eq!(first.spelling, SelectSpelling::FromFirst);
            assert!(
                matches!(first.projection.as_slice(), [SelectItem::Wildcard { .. }]),
                "the trailing SELECT must not bind as the FROM-first projection in {sql:?}",
            );
            assert_eq!(
                parsed.statements().len(),
                2,
                "the trailing SELECT splits into its own statement in {sql:?}",
            );
        }
    }

    #[test]
    fn from_first_round_trips_byte_identically() {
        use crate::render::Renderer;
        for sql in [
            "FROM t",
            "FROM t SELECT a",
            "FROM t SELECT a, b",
            "FROM t SELECT DISTINCT a",
            "FROM t SELECT a WHERE a > 1 GROUP BY a HAVING a > 2",
            "FROM t GROUP BY ALL",
            "FROM t ORDER BY a",
            "FROM a SELECT x UNION FROM b SELECT y",
        ] {
            let parsed = parse_with(sql, FROM_FIRST_DIALECT).expect("FROM-first parses");
            assert_eq!(
                Renderer::new(FROM_FIRST_DIALECT)
                    .render_parsed(&parsed)
                    .expect("FROM-first renders"),
                sql,
            );
        }
    }

    #[test]
    fn bare_star_from_first_normalizes_to_the_bare_form() {
        use crate::render::Renderer;
        // Explicit `FROM t SELECT *` carries the same shape as bare `FROM t` (a single
        // wildcard, no DISTINCT); the one canonical render is the bare form, so the
        // `SELECT *` normalizes away (ADR-0011, the single-tag-state decision).
        let parsed =
            parse_with("FROM t SELECT *", FROM_FIRST_DIALECT).expect("FROM t SELECT * parses");
        assert_eq!(
            Renderer::new(FROM_FIRST_DIALECT)
                .render_parsed(&parsed)
                .expect("renders"),
            "FROM t",
        );
    }

    #[test]
    fn from_first_composes_in_set_operations_and_subqueries() {
        // The FROM-first primary routes through the same query entry as SELECT, so it
        // composes as a set operand, a parenthesized operand, a scalar subquery, and a
        // CTE body — one gated choke point, every position for free.
        for sql in [
            "FROM a SELECT x UNION FROM b SELECT y",
            "(FROM a SELECT x) UNION SELECT y FROM b",
            "SELECT (FROM t SELECT max(a))",
            "WITH c AS (FROM t SELECT a) SELECT * FROM c",
            "WITH c AS (FROM t) FROM c",
        ] {
            assert!(
                parse_with(sql, FROM_FIRST_DIALECT).is_ok(),
                "{sql:?} parses"
            );
        }
    }

    /// ANSI plus the `wildcard_modifiers` flag alone, isolating the wildcard-tail gate
    /// from the rest of the DuckDb preset (the COLUMNS expression is the separate
    /// `columns_expression` gate, exercised with the preset in
    /// `crate::dialect::duckdb`).
    const STAR_MODIFIERS_DIALECT: FeatureDialect = {
        const FEATURES: FeatureSet =
            FeatureSet::ANSI.with(FeatureDelta::EMPTY.select_syntax(SelectSyntax {
                wildcard_modifiers: true,
                ..SelectSyntax::ANSI
            }));
        FeatureDialect {
            features: &FEATURES,
        }
    };

    #[test]
    fn wildcard_modifiers_ride_the_gate_not_the_preset() {
        // The flag alone (over ANSI) admits the tail on both the bare and the
        // qualified wildcard, and a plain `*` stays modifier-free (`options: None`).
        let parsed = parse_with(
            "SELECT * EXCLUDE (a) REPLACE (b + 1 AS b) RENAME (c AS d), t.* EXCLUDE (e), * FROM t",
            STAR_MODIFIERS_DIALECT,
        )
        .expect("the gated modifiers parse");
        let items = projection(&parsed);
        let SelectItem::Wildcard {
            options: Some(options),
            ..
        } = &items[0]
        else {
            panic!("expected the modifier-bearing wildcard");
        };
        assert_eq!(
            (
                options.exclude.len(),
                options.replace.len(),
                options.rename.len()
            ),
            (1, 1, 1),
        );
        assert!(matches!(
            &items[1],
            SelectItem::QualifiedWildcard {
                options: Some(_),
                ..
            }
        ));
        assert!(matches!(
            &items[2],
            SelectItem::Wildcard { options: None, .. }
        ));

        // The same text under plain ANSI leaves `EXCLUDE` unconsumed — a clean error.
        assert!(parse_with("SELECT * EXCLUDE (a) FROM t", TestDialect).is_err());
    }

    #[test]
    fn wildcard_modifier_spans_cover_the_whole_item() {
        // The wildcard item's span stretches over its modifier tail (the meta is the
        // side-table key for diagnostics/slicing), and the options node anchors at the
        // star.
        let sql = "SELECT * EXCLUDE (a) FROM t";
        let parsed = parse_with(sql, STAR_MODIFIERS_DIALECT).expect("parses");
        let SelectItem::Wildcard {
            options: Some(options),
            meta,
            ..
        } = &projection(&parsed)[0]
        else {
            panic!("expected the modifier-bearing wildcard");
        };
        assert_eq!(
            meta.span,
            Span::new(7, 20),
            "item span covers `* EXCLUDE (a)`",
        );
        assert_eq!(options.meta.span, meta.span, "options anchor at the star");
    }
}