surrealdb-expr 4.0.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
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
//! Lowering from runtime syntax to the logical plan.
//!
//! Pure and schema-free: no transaction, no catalog, no authority. A statement
//! lowers to the same tree for every caller, and everything that depends on
//! *who is asking* or *what the schema says* is added later by a pass over the
//! result.
//!
//! Lowering takes runtime syntax only. A construct with no logical form is
//! reported as [`NoLogicalForm`] before any transaction is touched, and the
//! statement fails.

use std::collections::VecDeque;
use std::fmt::{self, Display, Formatter};
use std::ops::Bound;

use surrealdb_strand::Strand;
use surrealdb_types::ToSql;

use super::expr::{
	LogicalCall, LogicalClosure, LogicalDestructureField, LogicalExpr, LogicalField, LogicalFields,
	LogicalIf, LogicalLiteral, LogicalLookup, LogicalLookupSubject, LogicalOrder, LogicalOrdering,
	LogicalPostfixOperator, LogicalRecordIdKey, LogicalRecordIdKeyRange, LogicalRecordIdLit,
	LogicalRecurse, LogicalRecurseInstruction, LogicalRecursePath, OutputPath, SourceRef,
};
use super::{
	LogicalAggregate, LogicalAssignment, LogicalBreak, LogicalContinue, LogicalData, LogicalDdl,
	LogicalDdlStatement, LogicalEval, LogicalExplain, LogicalFetch, LogicalFilter, LogicalForeach,
	LogicalIfOp, LogicalInfo, LogicalInsertRows, LogicalInserts, LogicalLet, LogicalLimit,
	LogicalOnly, LogicalOp, LogicalProject, LogicalReturn, LogicalSequence, LogicalSleep,
	LogicalSort, LogicalSource, LogicalSplit, LogicalSubquery, LogicalThrow, LogicalTimeout,
	LogicalUnionAll, LogicalValues, LogicalVersionScope, LogicalWrite, LogicalWriteSource,
	LogicalWriteStatement,
};
use crate::expr as surface;
use crate::expr::part::Recurse;
use crate::expr::statements::{IfelseStatement, InfoStatement, SelectStatement};

/// Why a tree has no logical plan.
///
/// Every statement and expression the parser produces has a logical form,
/// schema statements and mutations included. What is reported here is a tree
/// no parser builds: a shape that only a caller constructing the surface
/// syntax by hand can write, such as an `INSERT` whose data clause names no
/// rows. It is a malformed input rather than a capability the engine lacks, so
/// a caller raises it as an error and has no other engine to hand the tree to.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NoLogicalForm {
	/// A construct no parser produces.
	Unsupported(String),
}

impl Display for NoLogicalForm {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		match self {
			NoLogicalForm::Unsupported(what) => write!(f, "{what} has no logical form"),
		}
	}
}

impl std::error::Error for NoLogicalForm {}

type Lowered<T> = Result<T, NoLogicalForm>;

/// Lower an expression in statement position to the operator that runs it.
///
/// A `SELECT` lowers to its pipeline; a mutation to a [`LogicalOp::Write`]; a
/// block to a [`LogicalOp::Sequence`]; `LET`, `RETURN`, `FOR`, `INFO`, `SLEEP`
/// and `EXPLAIN` to their operators; `IF`, `THROW`, `BREAK` and `CONTINUE` to
/// [`LogicalOp::If`], [`LogicalOp::Throw`], [`LogicalOp::Break`] and
/// [`LogicalOp::Continue`]. Any other expression — an arithmetic expression,
/// a function call — is a [`LogicalOp::Eval`] of its scalar form. A schema
/// statement is a [`LogicalOp::Ddl`] carrying it whole.
///
/// Statement position is the top level, a statement of a block (a `FOR` body
/// included), a `LET` value and an arm of a statement `IF`. The value of a
/// `RETURN` and the statement an `EXPLAIN` describes are lowered by
/// [`lower_value_statement`], which keeps an `IF`, `THROW`, `BREAK` or
/// `CONTINUE` there in its scalar form.
pub fn lower(expr: impl Into<surface::Expr>) -> Lowered<LogicalOp> {
	match expr.into() {
		surface::Expr::IfElse(stmt) => lower_if_statement(*stmt),
		surface::Expr::Throw(value) => Ok(LogicalOp::Throw(LogicalThrow {
			value: lower_expr(*value)?,
		})),
		surface::Expr::Break => Ok(LogicalOp::Break(LogicalBreak)),
		surface::Expr::Continue => Ok(LogicalOp::Continue(LogicalContinue)),
		surface::Expr::Select(select) => lower_select(*select),
		surface::Expr::Block(block) => lower_block(*block),
		surface::Expr::Let(stmt) => Ok(LogicalOp::Let(LogicalLet {
			name: stmt.name.into_string(),
			kind: stmt.kind,
			value: Box::new(lower(stmt.what)?),
			synthetic: false,
		})),
		surface::Expr::Return(stmt) => Ok(LogicalOp::Return(LogicalReturn {
			what: Box::new(lower_value_statement(stmt.what)?),
			fetch: lower_fetchs(stmt.fetch)?,
		})),
		surface::Expr::Foreach(stmt) => Ok(LogicalOp::Foreach(LogicalForeach {
			param: stmt.param,
			range: lower_expr(stmt.range)?,
			body: Box::new(lower_block(stmt.block)?),
		})),
		surface::Expr::Info(info) => Ok(LogicalOp::Info(Box::new(lower_info(*info)?))),
		surface::Expr::Sleep(stmt) => Ok(LogicalOp::Sleep(LogicalSleep {
			duration: LogicalExpr::Literal(LogicalLiteral::Duration(stmt.duration)),
		})),
		surface::Expr::Explain {
			format,
			analyze,
			statement,
		} => Ok(LogicalOp::Explain(LogicalExplain {
			input: Box::new(lower_value_statement(*statement)?),
			analyze,
			format,
		})),
		surface::Expr::Create(stmt) => {
			// Targets, not rows read from storage. A creating statement's
			// subject does not exist yet, so the target is carried as the
			// value it evaluates to — a table to generate an id in, or a
			// record id to write at — the distinction `Value::Table` versus
			// `Value::RecordId` draws.
			let targets = lower_exprs(stmt.what.clone())?;
			let timeout = stmt.timeout.clone();
			with_timeout(
				LogicalOp::Write(LogicalWrite {
					data: stmt.data.clone().map(lower_data).transpose()?.flatten(),
					duplicate_update: None,
					cond: None,
					statement: LogicalWriteStatement::Create(stmt),
					source: LogicalWriteSource::Targets(targets),
					trace: crate::expr::logical::LookupTrace::None,
				}),
				timeout,
			)
		}
		surface::Expr::Update(stmt) => {
			// Rows: `UPDATE` writes records that already exist, so its targets
			// are read exactly as the same targets in a `FROM` clause are.
			let rows = lower_write_rows(stmt.what.clone(), stmt.with.as_ref())?;
			let timeout = stmt.timeout.clone();
			let explain = stmt.explain.as_ref().map(|surface::Explain(analyze)| *analyze);
			let op = with_timeout(
				LogicalOp::Write(LogicalWrite {
					data: stmt.data.clone().map(lower_data).transpose()?.flatten(),
					duplicate_update: None,
					cond: stmt.cond.clone().map(|c| lower_expr(c.0)).transpose()?,
					statement: LogicalWriteStatement::Update(stmt),
					source: LogicalWriteSource::Rows(Box::new(rows)),
					trace: crate::expr::logical::LookupTrace::None,
				}),
				timeout,
			)?;
			Ok(with_explain(op, explain))
		}
		surface::Expr::Upsert(stmt) => {
			// Targets, as for `CREATE`: an `UPSERT` subject need not exist, and
			// a reading source yields nothing for one that does not, which
			// would silently drop the write the statement exists to perform.
			// The write operator reconciles the two cases itself, retrying a
			// creating attempt that conflicts with an existing record as an
			// update.
			let targets = lower_exprs(stmt.what.clone())?;
			let timeout = stmt.timeout.clone();
			let explain = stmt.explain.as_ref().map(|surface::Explain(analyze)| *analyze);
			let op = with_timeout(
				LogicalOp::Write(LogicalWrite {
					data: stmt.data.clone().map(lower_data).transpose()?.flatten(),
					duplicate_update: None,
					cond: stmt.cond.clone().map(|c| lower_expr(c.0)).transpose()?,
					statement: LogicalWriteStatement::Upsert(stmt),
					source: LogicalWriteSource::Targets(targets),
					trace: crate::expr::logical::LookupTrace::None,
				}),
				timeout,
			)?;
			Ok(with_explain(op, explain))
		}
		surface::Expr::Delete(stmt) => {
			// Rows, as for `UPDATE`: a record must be read before it can be
			// removed.
			let rows = lower_write_rows(stmt.what.clone(), stmt.with.as_ref())?;
			let timeout = stmt.timeout.clone();
			let explain = stmt.explain.as_ref().map(|surface::Explain(analyze)| *analyze);
			let op = with_timeout(
				LogicalOp::Write(LogicalWrite {
					data: None,
					duplicate_update: None,
					cond: stmt.cond.clone().map(|c| lower_expr(c.0)).transpose()?,
					statement: LogicalWriteStatement::Delete(stmt),
					source: LogicalWriteSource::Rows(Box::new(rows)),
					trace: crate::expr::logical::LookupTrace::None,
				}),
				timeout,
			)?;
			Ok(with_explain(op, explain))
		}
		surface::Expr::Insert(stmt) => {
			// `INSERT` names no subjects to read: its rows *are* its subjects,
			// so they are the source. Nothing applies them to a record the
			// statement named elsewhere, which is what separates them from
			// every other form's data clause.
			let inserts = lower_inserts(stmt.into.clone(), stmt.data.clone())?;
			let timeout = stmt.timeout.clone();
			with_timeout(
				LogicalOp::Write(LogicalWrite {
					data: None,
					duplicate_update: stmt.update.clone().map(lower_data).transpose()?.flatten(),
					cond: None,
					statement: LogicalWriteStatement::Insert(stmt),
					source: LogicalWriteSource::Inserts(inserts),
					trace: crate::expr::logical::LookupTrace::None,
				}),
				timeout,
			)
		}
		surface::Expr::Relate(stmt) => {
			// Targets: the edge record `RELATE` writes does not exist yet, and
			// its endpoints are used as the ids they evaluate to rather than
			// read as documents. The list is `[from, through, to]`,
			// the order `LogicalWriteSource::Targets` documents for this form.
			let targets =
				lower_exprs(vec![stmt.from.clone(), stmt.through.clone(), stmt.to.clone()])?;
			let timeout = stmt.timeout.clone();
			with_timeout(
				LogicalOp::Write(LogicalWrite {
					data: stmt.data.clone().map(lower_data).transpose()?.flatten(),
					duplicate_update: None,
					cond: None,
					statement: LogicalWriteStatement::Relate(stmt),
					source: LogicalWriteSource::Targets(targets),
					trace: crate::expr::logical::LookupTrace::None,
				}),
				timeout,
			)
		}
		// A schema statement is carried whole. Its clauses stay surface
		// expressions, resolved where the catalog write is formed rather than
		// lowered here: a bare name in a DDL name position denotes itself, and
		// lowering it would make it a read of the row the statement runs
		// under.
		surface::Expr::Define(stmt) => Ok(LogicalOp::Ddl(LogicalDdl {
			statement: LogicalDdlStatement::Define(stmt),
		})),
		surface::Expr::Remove(stmt) => Ok(LogicalOp::Ddl(LogicalDdl {
			statement: LogicalDdlStatement::Remove(stmt),
		})),
		surface::Expr::Rebuild(stmt) => Ok(LogicalOp::Ddl(LogicalDdl {
			statement: LogicalDdlStatement::Rebuild(stmt),
		})),
		surface::Expr::Alter(stmt) => Ok(LogicalOp::Ddl(LogicalDdl {
			statement: LogicalDdlStatement::Alter(stmt),
		})),
		scalar @ (surface::Expr::Literal(_)
		| surface::Expr::Param(_)
		| surface::Expr::Idiom(_)
		| surface::Expr::Table(_)
		| surface::Expr::Mock(_)
		| surface::Expr::Constant(_)
		| surface::Expr::Prefix {
			..
		}
		| surface::Expr::Postfix {
			..
		}
		| surface::Expr::Binary {
			..
		}
		| surface::Expr::FunctionCall(_)
		| surface::Expr::Closure(_)) => Ok(LogicalOp::Eval(LogicalEval {
			expr: lower_expr(scalar)?,
		})),
	}
}

/// [`lower`] for a statement whose value is used where it stands — the value
/// of a `RETURN`, the statement an `EXPLAIN` describes: an `IF`, `THROW`,
/// `BREAK` or `CONTINUE` there is a [`LogicalOp::Eval`] of its scalar form,
/// and anything else lowers as [`lower`] lowers it.
pub fn lower_value_statement(expr: impl Into<surface::Expr>) -> Lowered<LogicalOp> {
	let expr = expr.into();
	if matches!(
		expr,
		surface::Expr::IfElse(_)
			| surface::Expr::Throw(_)
			| surface::Expr::Break
			| surface::Expr::Continue
	) {
		return Ok(LogicalOp::Eval(LogicalEval {
			expr: lower_expr(expr)?,
		}));
	}
	lower(expr)
}

/// An `IF` in statement position: each arm lowered as a statement, so a block
/// arm is its [`LogicalOp::Sequence`] and a nested `IF` another
/// [`LogicalOp::If`].
///
/// An `IF` with an arm written as a `LET` — a `LET` as an expression, which
/// planning refuses whether or not the arm is taken — is a
/// [`LogicalOp::Eval`] of its scalar form, where that refusal is raised as
/// the statement is planned.
fn lower_if_statement(stmt: IfelseStatement) -> Lowered<LogicalOp> {
	if if_arm_is_a_let(&stmt) {
		return Ok(LogicalOp::Eval(LogicalEval {
			expr: lower_expr(surface::Expr::IfElse(Box::new(stmt)))?,
		}));
	}
	let branches = stmt
		.exprs
		.into_iter()
		.map(|(cond, then)| Ok((lower_expr(cond)?, lower(then)?)))
		.collect::<Lowered<Vec<_>>>()?;
	let otherwise = stmt.close.map(lower).transpose()?;
	Ok(LogicalOp::If(LogicalIfOp::new(branches, otherwise)))
}

/// Whether an arm of `stmt` is a `LET` written in place of a value, here or in
/// an `IF` written as an arm. An arm that is a block holding a `LET` is not:
/// the `LET` is a statement of the block.
fn if_arm_is_a_let(stmt: &IfelseStatement) -> bool {
	fn is_a_let(arm: &surface::Expr) -> bool {
		if let surface::Expr::IfElse(nested) = arm {
			if_arm_is_a_let(nested)
		} else {
			matches!(arm, surface::Expr::Let(_))
		}
	}
	stmt.exprs.iter().any(|(_, then)| is_a_let(then)) || stmt.close.as_ref().is_some_and(is_a_let)
}

/// Lower an expression in scalar position.
///
/// Scalars map to their [`LogicalExpr`] form. A statement in scalar position —
/// a `SELECT`, a mutation, a block, a `RETURN`, a `LET` — becomes
/// [`LogicalExpr::Subquery`] of the operator [`lower`] produces for it. DDL
/// statements have no logical form.
pub fn lower_expr(expr: impl Into<surface::Expr>) -> Lowered<LogicalExpr> {
	Ok(match expr.into() {
		surface::Expr::Literal(literal) => lower_literal(literal)?,
		surface::Expr::Param(param) => LogicalExpr::Param(param),
		surface::Expr::Idiom(idiom) => lower_idiom(idiom)?,
		surface::Expr::Table(table) => LogicalExpr::Table(table),
		surface::Expr::Mock(mock) => LogicalExpr::Mock(mock),
		surface::Expr::Constant(constant) => LogicalExpr::Constant(constant),
		surface::Expr::Prefix {
			op,
			expr,
		} => LogicalExpr::Prefix {
			op,
			expr: Box::new(lower_expr(*expr)?),
		},
		surface::Expr::Postfix {
			expr,
			op,
		} => lower_postfix(lower_expr(*expr)?, op)?,
		surface::Expr::Binary {
			left,
			op,
			right,
		} => LogicalExpr::Binary {
			left: Box::new(lower_expr(*left)?),
			op,
			right: Box::new(lower_expr(*right)?),
		},
		surface::Expr::FunctionCall(call) => LogicalExpr::Call(Box::new(LogicalCall {
			receiver: call.receiver,
			args: lower_exprs(call.arguments)?,
		})),
		surface::Expr::Closure(closure) => {
			let body_sql = std::sync::Arc::from(closure.body.to_sql());
			LogicalExpr::Closure(Box::new(LogicalClosure {
				args: closure.args,
				returns: closure.returns,
				body: lower_expr(closure.body)?,
				body_sql,
			}))
		}
		surface::Expr::Break => LogicalExpr::Break,
		surface::Expr::Continue => LogicalExpr::Continue,
		surface::Expr::Throw(expr) => LogicalExpr::Throw(Box::new(lower_expr(*expr)?)),
		surface::Expr::IfElse(stmt) => LogicalExpr::If(Box::new(lower_if(*stmt)?)),
		statement @ (surface::Expr::Block(_)
		| surface::Expr::Return(_)
		| surface::Expr::Select(_)
		| surface::Expr::Info(_)
		| surface::Expr::Foreach(_)
		| surface::Expr::Let(_)
		| surface::Expr::Sleep(_)
		| surface::Expr::Explain {
			..
		}
		| surface::Expr::Create(_)
		| surface::Expr::Update(_)
		| surface::Expr::Upsert(_)
		| surface::Expr::Delete(_)
		| surface::Expr::Insert(_)
		| surface::Expr::Relate(_)
		| surface::Expr::Define(_)
		| surface::Expr::Remove(_)
		| surface::Expr::Rebuild(_)
		| surface::Expr::Alter(_)) => LogicalExpr::Subquery(Box::new(lower(statement)?)),
	})
}

/// Lower a `SELECT` into its operator tree.
///
/// The operator order is the pipeline the physical planner builds:
///
/// ```text
/// Explain ← Only ← VersionScope ← Timeout ← Fetch ← Project ← Limit ← Sort
///         ← Aggregate ← Split ← Filter ← Source/Values/Subquery/UnionAll
/// ```
///
/// read outermost-first; the tree below is written with the leaves last.
/// Clauses that are absent contribute no operator, so a bare
/// `SELECT * FROM t` lowers to `Project` over `Source`.
pub fn lower_select(select: impl Into<SelectStatement>) -> Lowered<LogicalOp> {
	let select = select.into();
	let SelectStatement {
		fields,
		omit,
		only,
		what,
		with,
		cond,
		split,
		group,
		order,
		limit,
		start,
		fetch,
		version,
		timeout,
		explain,
		tempfiles,
		for_update,
	} = select;

	let input = lower_sources(what, with.as_ref(), for_update)?;
	let cond = cond.map(|c| lower_expr(c.0)).transpose()?;
	let split = split
		.map(|splits| splits.into_iter().map(|s| lower_idiom(s.0)).collect::<Lowered<_>>())
		.transpose()?;
	let fields = lower_fields(fields)?;
	let group = group
		.map(|groups| groups.0.into_iter().map(|g| lower_idiom(g.0)).collect::<Lowered<_>>())
		.transpose()?;
	let order = order.map(lower_ordering).transpose()?;
	let limit = limit.map(|l| lower_expr(l.0)).transpose()?;
	let start = start.map(|s| lower_expr(s.0)).transpose()?;
	let omit = lower_exprs(omit)?;
	let fetch = lower_fetchs(fetch)?;
	let timeout = if is_absent(&timeout) {
		None
	} else {
		Some(lower_expr(timeout)?)
	};
	let version = if is_absent(&version) {
		None
	} else {
		Some(lower_expr(version)?)
	};
	Ok(select_pipeline(SelectClauses {
		input,
		fields,
		omit,
		only,
		cond,
		split,
		group,
		order,
		limit,
		start,
		fetch,
		version,
		timeout,
		explain: explain.map(|surface::Explain(analyze)| analyze),
		tempfiles,
	}))
}

/// Decoded clauses shared by surface and flat-tree front ends.
pub(crate) struct SelectClauses {
	pub input: LogicalOp,
	pub fields: LogicalFields,
	pub omit: Vec<LogicalExpr>,
	pub only: bool,
	pub cond: Option<LogicalExpr>,
	pub split: Option<Vec<LogicalExpr>>,
	pub group: Option<Vec<LogicalExpr>>,
	pub order: Option<LogicalOrdering>,
	pub limit: Option<LogicalExpr>,
	pub start: Option<LogicalExpr>,
	pub fetch: Vec<LogicalExpr>,
	pub version: Option<LogicalExpr>,
	pub timeout: Option<LogicalExpr>,
	pub explain: Option<bool>,
	pub tempfiles: bool,
}

/// Assemble the relational pipeline after each clause has been decoded.
pub(crate) fn select_pipeline(clauses: SelectClauses) -> LogicalOp {
	let SelectClauses {
		input,
		fields,
		omit,
		only,
		cond,
		split,
		group,
		order,
		limit,
		start,
		fetch,
		version,
		timeout,
		explain,
		tempfiles,
	} = clauses;
	// FROM.
	let mut op = input;

	// WHERE.
	if let Some(cond) = cond {
		op = LogicalOp::Filter(LogicalFilter {
			input: Box::new(op),
			predicate: cond,
		});
	}

	// SPLIT ON.
	if let Some(splits) = split {
		op = LogicalOp::Split(LogicalSplit {
			input: Box::new(op),
			exprs: splits,
		});
	}

	// GROUP BY. The aggregation evaluates the projection itself, so the
	// projection below is reduced to a pass-through carrying only OMIT.
	let grouped = group.is_some();
	if let Some(groups) = group {
		op = LogicalOp::Aggregate(LogicalAggregate {
			input: Box::new(op),
			group: groups,
			fields: fields.clone(),
		});
	}

	// ORDER BY.
	if let Some(ordering) = order {
		op = LogicalOp::Sort(LogicalSort {
			input: Box::new(op),
			ordering,
			tempfiles,
		});
	}

	// LIMIT / START.
	if limit.is_some() || start.is_some() {
		op = LogicalOp::Limit(LogicalLimit {
			input: Box::new(op),
			limit,
			start,
		});
	}

	// The projection. A grouped query has already projected, so only OMIT is
	// left to apply; a grouped one with nothing to omit needs no operator.
	if !grouped || !omit.is_empty() {
		op = LogicalOp::Project(LogicalProject {
			input: Box::new(op),
			fields: if grouped {
				LogicalFields::all()
			} else {
				fields
			},
			omit,
		});
	}

	// FETCH.
	let fetches = fetch;
	if !fetches.is_empty() {
		op = LogicalOp::Fetch(LogicalFetch {
			input: Box::new(op),
			fetches,
		});
	}

	// TIMEOUT, VERSION and ONLY. The first two are absent as a `NONE` literal
	// rather than as an `Option`.
	if let Some(timeout) = timeout {
		op = LogicalOp::Timeout(LogicalTimeout {
			input: Box::new(op),
			timeout,
		});
	}
	if let Some(version) = version {
		op = LogicalOp::VersionScope(LogicalVersionScope {
			input: Box::new(op),
			version,
		});
	}
	if only {
		op = LogicalOp::Only(LogicalOnly {
			input: Box::new(op),
		});
	}

	// EXPLAIN describes everything below it, so it wraps last. The clause form
	// has no FORMAT and answers with the structured plan; only the statement
	// form (`EXPLAIN [FORMAT ...] SELECT ...`) chooses text.
	if let Some(analyze) = explain {
		op = LogicalOp::Explain(LogicalExplain {
			input: Box::new(op),
			analyze,
			format: crate::expr::ExplainFormat::Json,
		});
	}

	op
}

/// Lower a statement's list of targets to the subtree that reads them.
///
/// Several targets concatenate; the `WITH` hint and `FOR UPDATE` apply to each
/// stored source, matching how the physical planner resolves one access path
/// per source. The same list means the same subtree wherever it is written, so
/// a `SELECT`'s `FROM` and an updating mutation's targets share this lowering.
fn lower_sources(
	what: Vec<surface::Expr>,
	with: Option<&surface::With>,
	for_update: bool,
) -> Lowered<LogicalOp> {
	let leaves = what
		.into_iter()
		.map(|source| lower_source(source, with, for_update))
		.collect::<Lowered<Vec<_>>>()?;
	Ok(match leaves.len() {
		1 => leaves.into_iter().next().expect("length checked"),
		_ => LogicalOp::UnionAll(LogicalUnionAll {
			inputs: leaves,
		}),
	})
}

/// Lower an updating mutation's targets to the rows it writes: the same
/// subtree the same targets produce in a `FROM` clause. `FOR UPDATE` has no
/// spelling on these statements — the mutation's own write takes the lock.
///
/// The statement's `WHERE` is deliberately left out of that subtree, and must
/// stay out of it. On the updating shapes the per-record table permission gate
/// runs before the statement's condition, so that a condition with side
/// effects runs only for records the caller may write
/// (`doc/adr/0001-streaming-write-path.md`). Everything in this subtree runs
/// beneath the write operator and therefore ahead of every one of its stages,
/// so a condition lowered into a [`LogicalOp::Filter`] here — or pushed into
/// the scan — would be evaluated before that gate. The condition stays a
/// clause of the statement, which the write operator's `CheckWhereCondition`
/// stage evaluates in its place in the order.
fn lower_write_rows(what: Vec<surface::Expr>, with: Option<&surface::With>) -> Lowered<LogicalOp> {
	lower_sources(what, with, false)
}

/// A mutation's data clause in logical form, so the rules can rewrite what it
/// evaluates.
///
/// The places stay as written: a place is a location rather than a value, and
/// what a path names is settled where the write is compiled. An empty clause
/// has no form — there is nothing to evaluate — and the `INSERT` spellings
/// that name rows rather than a clause are carried by
/// [`LogicalWriteSource::Inserts`] instead.
fn lower_data(data: surface::Data) -> Lowered<Option<LogicalData>> {
	Ok(Some(match data {
		surface::Data::EmptyExpression => return Ok(None),
		surface::Data::SetExpression(assignments)
		| surface::Data::UpdateExpression(assignments) => LogicalData::Set(
			assignments
				.into_iter()
				.map(|a| {
					Ok(LogicalAssignment {
						place: a.place,
						operator: a.operator,
						value: lower_expr(a.value)?,
					})
				})
				.collect::<Lowered<Vec<_>>>()?,
		),
		surface::Data::UnsetExpression(paths) => LogicalData::Unset(paths),
		surface::Data::PatchExpression(expr) => LogicalData::Patch(lower_expr(expr)?),
		surface::Data::MergeExpression(expr) => LogicalData::Merge(lower_expr(expr)?),
		surface::Data::ReplaceExpression(expr) => LogicalData::Replace(lower_expr(expr)?),
		surface::Data::ContentExpression(expr) => LogicalData::Content(lower_expr(expr)?),
		surface::Data::SingleExpression(expr) => LogicalData::Single(lower_expr(expr)?),
		surface::Data::ValuesExpression(rows) => LogicalData::Values(
			rows.into_iter()
				.map(|row| {
					row.into_iter()
						.map(|(path, value)| Ok((path, lower_expr(value)?)))
						.collect::<Lowered<Vec<_>>>()
				})
				.collect::<Lowered<Vec<_>>>()?,
		),
	}))
}

/// Lower `INSERT`'s subjects: its `INTO` table, and the rows it writes.
///
/// The rows are the source rather than a clause. Every other form applies its
/// data to a subject named elsewhere; `INSERT` has no such subject, so each row
/// *is* one — the record to write, with the table taken from `INTO` where the
/// statement has one and from the row's own record id where it does not.
///
/// Only the two spellings that name rows have a form here. Any other data
/// clause on an `INSERT` names none, and lowering declines rather than
/// silently writing records the statement did not describe.
fn lower_inserts(into: Option<surface::Expr>, data: surface::Data) -> Lowered<LogicalInserts> {
	let rows = match data {
		surface::Data::ValuesExpression(tuples) => LogicalInsertRows::Tuples(
			tuples
				.into_iter()
				.map(|tuple| {
					tuple
						.into_iter()
						.map(|(path, value)| Ok((path, lower_expr(value)?)))
						.collect::<Lowered<Vec<_>>>()
				})
				.collect::<Lowered<Vec<_>>>()?,
		),
		surface::Data::SingleExpression(expr) => LogicalInsertRows::Value(lower_expr(expr)?),
		surface::Data::EmptyExpression
		| surface::Data::SetExpression(_)
		| surface::Data::UnsetExpression(_)
		| surface::Data::PatchExpression(_)
		| surface::Data::MergeExpression(_)
		| surface::Data::ReplaceExpression(_)
		| surface::Data::ContentExpression(_)
		| surface::Data::UpdateExpression(_) => {
			return Err(NoLogicalForm::Unsupported(
				"an INSERT whose data clause names no rows".to_string(),
			));
		}
	};
	Ok(LogicalInserts {
		into: into.map(lower_expr).transpose()?,
		rows,
	})
}

/// Lower one `FROM` expression to its leaf.
///
/// A nested `SELECT` is lowered eagerly. A literal that produces rows without
/// reading storage becomes [`LogicalOp::Values`]. Everything else is a
/// [`LogicalOp::Source`] whose [`SourceRef`] classifies it syntactically for
/// the physical planner to resolve.
///
/// `FOR UPDATE` is carried on each source of the statement, so the physical
/// planner judges every target against the clause's rules in one place. A
/// nested `SELECT` under the clause is therefore not a [`LogicalOp::Subquery`]
/// leaf, which has no flag to carry, but a [`LogicalOp::Source`] holding the
/// nested plan as a [`LogicalExpr::Subquery`] scalar — a
/// [`SourceRef::Dynamic`] — which the planner rejects as it rejects any other
/// target that is not a record.
fn lower_source(
	source: surface::Expr,
	with: Option<&crate::expr::With>,
	for_update: bool,
) -> Lowered<LogicalOp> {
	if let surface::Expr::Select(inner) = source {
		let inner = lower_select(*inner)?;
		return Ok(if for_update {
			LogicalOp::Source(LogicalSource {
				source: SourceRef::classify(LogicalExpr::Subquery(Box::new(inner))),
				schema: None,
				with: with.cloned(),
				for_update: true,
			})
		} else {
			LogicalOp::Subquery(LogicalSubquery {
				input: Box::new(inner),
			})
		});
	}
	let source = lower_expr(source)?;
	Ok(if is_value_literal(&source) {
		LogicalOp::Values(LogicalValues {
			written: matches!(source, LogicalExpr::Array(_)),
			expr: source,
			for_update,
		})
	} else {
		LogicalOp::Source(LogicalSource {
			source: SourceRef::classify(source),
			schema: None,
			with: with.cloned(),
			for_update,
		})
	})
}

/// Whether a `FROM` expression is a literal that yields rows from its own
/// value rather than from storage. A record id is not one: it names stored
/// rows. Neither is an object literal, which is treated as a source so its
/// `ONLY` form errors when empty.
///
/// The classification is purely syntactic, so a pass that rewrites a
/// [`LogicalOp::Source`]'s expression into one of these forms re-applies it
/// to turn the node into a [`LogicalOp::Values`].
pub fn is_value_literal(expr: &LogicalExpr) -> bool {
	match expr {
		LogicalExpr::Array(_) => true,
		LogicalExpr::Literal(literal) => match literal {
			LogicalLiteral::String(_)
			| LogicalLiteral::Integer(_)
			| LogicalLiteral::Float(_)
			| LogicalLiteral::Decimal(_)
			| LogicalLiteral::BigDecimal(_)
			| LogicalLiteral::I8(_)
			| LogicalLiteral::U8(_)
			| LogicalLiteral::I16(_)
			| LogicalLiteral::U16(_)
			| LogicalLiteral::I32(_)
			| LogicalLiteral::U32(_)
			| LogicalLiteral::U64(_)
			| LogicalLiteral::F16(_)
			| LogicalLiteral::F32(_)
			| LogicalLiteral::Bool(_)
			| LogicalLiteral::None
			| LogicalLiteral::Null => true,
			// Treated as stored sources so that `ONLY` over one errors on an
			// empty result rather than yielding `NONE`. Matched exhaustively:
			// a new literal must be classified here deliberately.
			LogicalLiteral::Bytes(_)
			| LogicalLiteral::Regex(_)
			| LogicalLiteral::Geometry(_)
			| LogicalLiteral::File(_)
			| LogicalLiteral::Uuid(_)
			| LogicalLiteral::Datetime(_)
			| LogicalLiteral::Date(_)
			| LogicalLiteral::Time(_)
			| LogicalLiteral::Timestamp(_)
			| LogicalLiteral::Interval(_)
			| LogicalLiteral::Duration(_)
			| LogicalLiteral::UnboundedRange => false,
		},
		LogicalExpr::Set(_)
		| LogicalExpr::Object(_)
		| LogicalExpr::RecordId(_)
		| LogicalExpr::Param(_)
		| LogicalExpr::Table(_)
		| LogicalExpr::Mock(_)
		| LogicalExpr::Constant(_)
		| LogicalExpr::Field(_)
		| LogicalExpr::Doc
		| LogicalExpr::Current
		| LogicalExpr::Access {
			..
		}
		| LogicalExpr::Index {
			..
		}
		| LogicalExpr::First(_)
		| LogicalExpr::Last(_)
		| LogicalExpr::Each(_)
		| LogicalExpr::Flatten {
			..
		}
		| LogicalExpr::Filter {
			..
		}
		| LogicalExpr::Method {
			..
		}
		| LogicalExpr::Destructure {
			..
		}
		| LogicalExpr::Optional {
			..
		}
		| LogicalExpr::Lookup {
			..
		}
		| LogicalExpr::Recurse(_)
		| LogicalExpr::RepeatRecurse(_)
		| LogicalExpr::Broadcast {
			..
		}
		| LogicalExpr::Prefix {
			..
		}
		| LogicalExpr::Postfix {
			..
		}
		| LogicalExpr::Invoke {
			..
		}
		| LogicalExpr::Binary {
			..
		}
		| LogicalExpr::Call(_)
		| LogicalExpr::Closure(_)
		| LogicalExpr::If(_)
		| LogicalExpr::Throw(_)
		| LogicalExpr::Break
		| LogicalExpr::Continue
		| LogicalExpr::SetField {
			..
		}
		| LogicalExpr::Subquery(_) => false,
	}
}

/// Whether an optional clause expression is the absent marker. `VERSION` and
/// `TIMEOUT` hold a bare expression and spell "not written" as `NONE`.
/// Wrap a mutation in its `EXPLAIN` clause, `analyze` being whether the clause
/// said `FULL`.
///
/// Outermost, as in a `SELECT`: what the plan describes is everything beneath
/// it, the timeout included. The clause form has no `FORMAT` and answers with
/// the structured plan. Only the three updating forms have the clause; the
/// creating ones have no grammar for it.
fn with_explain(op: LogicalOp, analyze: Option<bool>) -> LogicalOp {
	match analyze {
		Some(analyze) => LogicalOp::Explain(LogicalExplain {
			input: Box::new(op),
			analyze,
			format: crate::expr::ExplainFormat::Json,
		}),
		None => op,
	}
}

/// Wrap a mutation in its `TIMEOUT` clause.
///
/// A mutation carries the clause on the statement rather than on a source, so
/// it becomes the node above the write operator — the same node a `SELECT`'s
/// `TIMEOUT` lowers to, holding the same budget over everything beneath it.
fn with_timeout(op: LogicalOp, timeout: surface::Expr) -> Lowered<LogicalOp> {
	if is_absent(&timeout) {
		return Ok(op);
	}
	Ok(LogicalOp::Timeout(LogicalTimeout {
		input: Box::new(op),
		timeout: lower_expr(timeout)?,
	}))
}

fn is_absent(expr: &surface::Expr) -> bool {
	matches!(expr, surface::Expr::Literal(surface::Literal::None))
}

fn lower_block(block: surface::Block) -> Lowered<LogicalOp> {
	Ok(LogicalOp::Sequence(LogicalSequence {
		stmts: block.0.into_iter().map(lower).collect::<Lowered<_>>()?,
	}))
}

fn lower_exprs(exprs: Vec<surface::Expr>) -> Lowered<Vec<LogicalExpr>> {
	exprs.into_iter().map(lower_expr).collect()
}

fn lower_fetchs(fetch: Option<surface::Fetchs>) -> Lowered<Vec<LogicalExpr>> {
	fetch.map_or(Ok(Vec::new()), |fetchs| fetchs.into_iter().map(|f| lower_expr(f.0)).collect())
}

fn lower_if(stmt: IfelseStatement) -> Lowered<LogicalIf> {
	Ok(LogicalIf {
		branches: stmt
			.exprs
			.into_iter()
			.map(|(cond, then)| Ok((lower_expr(cond)?, lower_expr(then)?)))
			.collect::<Lowered<_>>()?,
		otherwise: stmt.close.map(lower_expr).transpose()?,
	})
}

fn lower_info(info: InfoStatement) -> Lowered<LogicalInfo> {
	let version = |v: Option<surface::Expr>| v.map(lower_expr).transpose();
	Ok(match info {
		InfoStatement::Root(structure, v) => LogicalInfo::Root {
			structure,
			version: version(v)?,
		},
		InfoStatement::Ns(structure, v) => LogicalInfo::Ns {
			structure,
			version: version(v)?,
		},
		InfoStatement::Db(structure, v) => LogicalInfo::Db {
			structure,
			version: version(v)?,
		},
		InfoStatement::Tb(table, structure, v) => LogicalInfo::Table {
			table: lower_expr(table)?,
			structure,
			version: version(v)?,
		},
		InfoStatement::User(user, base, structure) => LogicalInfo::User {
			user: lower_expr(user)?,
			base,
			structure,
		},
		InfoStatement::Index(index, table, structure) => LogicalInfo::Index {
			index: lower_expr(index)?,
			table: lower_expr(table)?,
			structure,
		},
	})
}

fn lower_literal(literal: surface::Literal) -> Lowered<LogicalExpr> {
	let leaf = |literal| Ok(LogicalExpr::Literal(literal));
	match literal {
		surface::Literal::None => leaf(LogicalLiteral::None),
		surface::Literal::Null => leaf(LogicalLiteral::Null),
		surface::Literal::UnboundedRange => leaf(LogicalLiteral::UnboundedRange),
		surface::Literal::Bool(x) => leaf(LogicalLiteral::Bool(x)),
		surface::Literal::Float(x) => leaf(LogicalLiteral::Float(x)),
		surface::Literal::Integer(x) => leaf(LogicalLiteral::Integer(x)),
		surface::Literal::Decimal(x) => leaf(LogicalLiteral::Decimal(x)),
		surface::Literal::BigDecimal(x) => leaf(LogicalLiteral::BigDecimal(x)),
		surface::Literal::I8(x) => leaf(LogicalLiteral::I8(x)),
		surface::Literal::U8(x) => leaf(LogicalLiteral::U8(x)),
		surface::Literal::I16(x) => leaf(LogicalLiteral::I16(x)),
		surface::Literal::U16(x) => leaf(LogicalLiteral::U16(x)),
		surface::Literal::I32(x) => leaf(LogicalLiteral::I32(x)),
		surface::Literal::U32(x) => leaf(LogicalLiteral::U32(x)),
		surface::Literal::U64(x) => leaf(LogicalLiteral::U64(x)),
		surface::Literal::F16(x) => leaf(LogicalLiteral::F16(x)),
		surface::Literal::F32(x) => leaf(LogicalLiteral::F32(x)),
		surface::Literal::Duration(x) => leaf(LogicalLiteral::Duration(x)),
		surface::Literal::String(x) => leaf(LogicalLiteral::String(x)),
		surface::Literal::Datetime(x) => leaf(LogicalLiteral::Datetime(x)),
		surface::Literal::Date(x) => leaf(LogicalLiteral::Date(x)),
		surface::Literal::Time(x) => leaf(LogicalLiteral::Time(x)),
		surface::Literal::Timestamp(x) => leaf(LogicalLiteral::Timestamp(x)),
		surface::Literal::Interval(x) => leaf(LogicalLiteral::Interval(x)),
		surface::Literal::Uuid(x) => leaf(LogicalLiteral::Uuid(x)),
		surface::Literal::Regex(x) => leaf(LogicalLiteral::Regex(x)),
		surface::Literal::Geometry(x) => leaf(LogicalLiteral::Geometry(x)),
		surface::Literal::File(x) => leaf(LogicalLiteral::File(x)),
		surface::Literal::Bytes(x) => leaf(LogicalLiteral::Bytes(x)),
		surface::Literal::RecordId(rid) => {
			Ok(LogicalExpr::RecordId(Box::new(lower_record_id(rid)?)))
		}
		surface::Literal::Array(items) => Ok(LogicalExpr::Array(lower_exprs(items)?)),
		surface::Literal::Set(items) => Ok(LogicalExpr::Set(lower_exprs(items)?)),
		surface::Literal::Object(entries) => {
			// An object literal in GeoJSON shape denotes a geometry; the
			// shared conversion decides.
			if let Some(geometry) = crate::parser::geometry::collect_geometry(&entries) {
				return leaf(LogicalLiteral::Geometry(geometry));
			}
			Ok(LogicalExpr::Object(lower_entries(entries)?))
		}
	}
}

fn lower_entries(
	entries: Vec<surface::literal::ObjectEntry>,
) -> Lowered<Vec<(String, LogicalExpr)>> {
	entries.into_iter().map(|e| Ok((e.key.into_string(), lower_expr(e.value)?))).collect()
}

fn lower_record_id(rid: surface::RecordIdLit) -> Lowered<LogicalRecordIdLit> {
	Ok(LogicalRecordIdLit {
		table: rid.table,
		key: lower_record_id_key(rid.key)?,
	})
}

fn lower_record_id_key(key: surface::RecordIdKeyLit) -> Lowered<LogicalRecordIdKey> {
	Ok(match key {
		surface::RecordIdKeyLit::Number(x) => LogicalRecordIdKey::Number(x),
		surface::RecordIdKeyLit::String(x) => LogicalRecordIdKey::String(x),
		surface::RecordIdKeyLit::Uuid(x) => LogicalRecordIdKey::Uuid(x),
		surface::RecordIdKeyLit::Array(items) => LogicalRecordIdKey::Array(lower_exprs(items)?),
		surface::RecordIdKeyLit::Object(entries) => {
			LogicalRecordIdKey::Object(lower_entries(entries)?)
		}
		surface::RecordIdKeyLit::Generate(generator) => LogicalRecordIdKey::Generate(generator),
		surface::RecordIdKeyLit::Range(range) => {
			LogicalRecordIdKey::Range(Box::new(lower_record_id_range(*range)?))
		}
	})
}

fn lower_record_id_range(range: surface::RecordIdKeyRangeLit) -> Lowered<LogicalRecordIdKeyRange> {
	let bound = |b: Bound<surface::RecordIdKeyLit>| {
		Ok(match b {
			Bound::Included(key) => Bound::Included(lower_record_id_key(key)?),
			Bound::Excluded(key) => Bound::Excluded(lower_record_id_key(key)?),
			Bound::Unbounded => Bound::Unbounded,
		})
	};
	Ok(LogicalRecordIdKeyRange {
		start: bound(range.start)?,
		end: bound(range.end)?,
	})
}

// ---------------------------------------------------------------------------
// Paths
// ---------------------------------------------------------------------------

/// One step of a surface idiom, with the expressions it holds lowered.
pub(crate) enum Step {
	Access(Strand),
	Index(LogicalExpr),
	First,
	Last,
	Each,
	Flatten,
	Filter(LogicalExpr),
	Method(Strand, Vec<LogicalExpr>),
	Destructure(Vec<LogicalDestructureField>),
	Optional,
	Lookup(Box<LogicalLookup>),
	/// The body is the written `(…)` path, when there is one.
	Recurse(Recurse, Option<LogicalExpr>, Option<LogicalRecurseInstruction>),
	RepeatRecurse,
}

impl Step {
	/// Whether the step applies to each element of an array on its own:
	/// a field read, `.*` or a destructure.
	fn is_mapping(&self) -> bool {
		matches!(self, Step::Access(_) | Step::Each | Step::Destructure(_))
	}
}

/// Whether the array-continuation rule applies while folding a sequence of
/// steps.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum PathMode {
	/// The path as written: a mapping step or a lookup over an array applies
	/// the rest of the path to each element ([`LogicalExpr::Broadcast`]).
	Continuation,
	/// A body — of an [`LogicalExpr::Optional`], a [`LogicalExpr::Recurse`]
	/// or a destructure alias — whose steps apply one after another to the
	/// value as it is.
	Plain,
}

/// The kind of the step folded last, as far as the lookup-continuation rule
/// distinguishes them.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Prev {
	Lookup,
	Flatten,
	Filter,
	Other,
}

/// Lower an idiom in expression position.
///
/// The first part is the root: a field is a [`LogicalExpr::Field`], `@` is
/// [`LogicalExpr::Doc`], a starting expression is itself, and a leading lookup
/// applies to [`LogicalExpr::Current`], the row. Every later part is a step
/// folded onto the root by [`fold_steps`] in [`PathMode::Continuation`].
///
/// A root written after the first part restarts the path ([`restart_at_root`]).
fn lower_idiom(idiom: surface::Idiom) -> Lowered<LogicalExpr> {
	let mut parts = restart_at_root(idiom.0).into_iter().peekable();
	let base = match parts.peek() {
		Some(surface::Part::Field(_)) => match parts.next() {
			Some(surface::Part::Field(name)) => LogicalExpr::Field(name),
			_ => unreachable!("peeked a field"),
		},
		Some(surface::Part::Start(_)) => match parts.next() {
			Some(surface::Part::Start(start)) => lower_expr(start)?,
			_ => unreachable!("peeked a start"),
		},
		Some(surface::Part::Doc) => {
			parts.next();
			LogicalExpr::Doc
		}
		Some(_) | None => LogicalExpr::Current,
	};
	let steps = lower_steps(parts)?;
	Ok(fold_steps(base, steps, PathMode::Continuation))
}

/// Lower an idiom that is a body over [`LogicalExpr::Current`]: the `(…)`
/// path of a recursion, or the path of a destructure alias. Every part is a
/// step, a leading field included; a leading starting expression or `@`
/// replaces `Current` as the base. Bodies fold in [`PathMode::Plain`].
fn lower_body(idiom: surface::Idiom) -> Lowered<LogicalExpr> {
	let mut parts = restart_at_root(idiom.0).into_iter().peekable();
	let base = match parts.peek() {
		Some(surface::Part::Start(_)) => match parts.next() {
			Some(surface::Part::Start(start)) => lower_expr(start)?,
			_ => unreachable!("peeked a start"),
		},
		Some(surface::Part::Doc) => {
			parts.next();
			LogicalExpr::Doc
		}
		Some(_) | None => LogicalExpr::Current,
	};
	let steps = lower_steps(parts)?;
	Ok(fold_steps(base, steps, PathMode::Plain))
}

/// The parts of a path from its last root on.
///
/// A starting expression or `@` is a root: it is the value the parts after it
/// apply to, wherever it is written. The parser writes one only first, so a
/// path holding one later is a tree built without it, and the parts before the
/// root are the value it replaces: they are not read, which is what a root
/// means rather than a step applied to what precedes it.
fn restart_at_root(mut parts: Vec<surface::Part>) -> Vec<surface::Part> {
	match parts
		.iter()
		.rposition(|part| matches!(part, surface::Part::Start(_) | surface::Part::Doc))
	{
		Some(at) if at > 0 => parts.split_off(at),
		_ => parts,
	}
}

fn lower_steps(parts: impl Iterator<Item = surface::Part>) -> Lowered<VecDeque<Step>> {
	parts
		.map(|part| {
			Ok(match part {
				surface::Part::All => Step::Each,
				surface::Part::Flatten => Step::Flatten,
				surface::Part::Last => Step::Last,
				surface::Part::First => Step::First,
				surface::Part::Field(name) => Step::Access(name),
				surface::Part::Where(expr) => Step::Filter(lower_expr(expr)?),
				surface::Part::Lookup(lookup) => Step::Lookup(Box::new(lower_lookup(*lookup)?)),
				surface::Part::Value(expr) => Step::Index(lower_expr(expr)?),
				surface::Part::Method(name, args) => Step::Method(name, lower_exprs(args)?),
				surface::Part::Destructure(parts) => Step::Destructure(lower_destructure(parts)?),
				surface::Part::Optional => Step::Optional,
				surface::Part::Recurse(recurse, nested, instruction) => Step::Recurse(
					recurse,
					nested.map(lower_body).transpose()?,
					instruction.map(lower_instruction).transpose()?,
				),
				surface::Part::RepeatRecurse => Step::RepeatRecurse,
				// A root is consumed before the steps are lowered
				// ([`restart_at_root`]): only the first part of a path can be
				// one by the time it gets here.
				surface::Part::Start(_) | surface::Part::Doc => {
					return Err(NoLogicalForm::Unsupported(
						"a root part that is not the first of its path".to_string(),
					));
				}
			})
		})
		.collect()
}

/// Fold `steps` onto `base`, one form per step, placing the forms that carry
/// a path's sequence semantics:
///
/// - **Array continuation** ([`PathMode::Continuation`] only). A mapping step (a field read, `.*`,
///   a destructure) followed by a step that is not a mapping step becomes `Broadcast { base, body
///   }`, where `body` is the step applied to `Current` followed by the rest of the path, folded on.
///   When every remaining step is a mapping step the steps nest plainly, since each maps over an
///   array base on its own. A lookup, or a run of consecutive lookups, whose previous step is not a
///   lookup or a flatten and which is followed by more steps or is a run of more than one hop
///   becomes a `Broadcast` the same way; its per-element results are flattened when the base is a
///   lookup or a flatten followed only by filters.
/// - **Auto-flatten.** A lookup (or run of lookups) followed by a filter has an implicit `Flatten`
///   between them, in every mode.
/// - **Optional.** `.?` takes the rest of the path as its body.
/// - **Recursion.** `.{n}` with no `(…)` takes the rest of the path as its body, up to and
///   including a `.@` at the body's top level; the steps after `.@` apply to the recursion's
///   result.
///
/// Bodies fold in [`PathMode::Plain`], except a `Broadcast` body, which
/// continues in the mode it was reached in: when the base is not an array,
/// the rest of the path is evaluated as the path it is, continuation
/// included.
pub(crate) fn fold_steps(base: LogicalExpr, steps: VecDeque<Step>, mode: PathMode) -> LogicalExpr {
	fold_from(base, steps, mode, Prev::Other, false)
}

/// [`fold_steps`] resumed after a step of kind `prev`, with
/// `array_from_lookup` recording whether the value so far is the result of a
/// lookup or a flatten narrowed only by filters.
fn fold_from(
	mut base: LogicalExpr,
	mut steps: VecDeque<Step>,
	mode: PathMode,
	mut prev: Prev,
	mut array_from_lookup: bool,
) -> LogicalExpr {
	while let Some(step) = steps.pop_front() {
		match step {
			Step::Optional => {
				let then = fold_steps(LogicalExpr::Current, steps, PathMode::Plain);
				return LogicalExpr::Optional {
					base: Box::new(base),
					then: Box::new(then),
				};
			}
			Step::Recurse(recurse, Some(nested), instruction) => {
				base = LogicalExpr::Recurse(Box::new(LogicalRecurse {
					base,
					recurse,
					path: Some(LogicalRecursePath::Nested(nested)),
					instruction,
				}));
				prev = Prev::Other;
				array_from_lookup = false;
			}
			Step::Recurse(recurse, None, instruction) => {
				// The body runs up to and including the first `.@` at this
				// level; what follows applies once to the result.
				let suffix = match steps.iter().position(|s| matches!(s, Step::RepeatRecurse)) {
					Some(pos) => steps.split_off(pos + 1),
					None => VecDeque::new(),
				};
				let path = if steps.is_empty() {
					None
				} else {
					Some(LogicalRecursePath::Trailing(fold_steps(
						LogicalExpr::Current,
						steps,
						PathMode::Plain,
					)))
				};
				base = LogicalExpr::Recurse(Box::new(LogicalRecurse {
					base,
					recurse,
					path,
					instruction,
				}));
				steps = suffix;
				prev = Prev::Other;
				array_from_lookup = false;
			}
			Step::Lookup(first) => {
				let mut lookups = vec![*first];
				while let Some(Step::Lookup(_)) = steps.front() {
					if let Some(Step::Lookup(lookup)) = steps.pop_front() {
						lookups.push(*lookup);
					}
				}
				let fused = lookups.len() > 1;
				let followed = !steps.is_empty();
				let continues = mode == PathMode::Continuation
					&& !matches!(prev, Prev::Lookup | Prev::Flatten)
					&& (followed || fused);
				if continues {
					let inner = apply_lookups(LogicalExpr::Current, lookups, &steps);
					let body = fold_from(inner, steps, mode, Prev::Lookup, true);
					return LogicalExpr::Broadcast {
						base: Box::new(base),
						body: Box::new(body),
						flatten: array_from_lookup,
					};
				}
				base = apply_lookups(base, lookups, &steps);
				prev = Prev::Lookup;
				array_from_lookup = true;
			}
			step @ (Step::Access(_) | Step::Each | Step::Destructure(_)) => {
				let broadcast = mode == PathMode::Continuation
					&& !steps.is_empty()
					&& !steps.iter().all(Step::is_mapping);
				if broadcast {
					let inner = apply_step(LogicalExpr::Current, step);
					let body = fold_from(inner, steps, mode, Prev::Other, false);
					return LogicalExpr::Broadcast {
						base: Box::new(base),
						body: Box::new(body),
						flatten: false,
					};
				}
				base = apply_step(base, step);
				prev = Prev::Other;
				array_from_lookup = false;
			}
			Step::Filter(predicate) => {
				base = LogicalExpr::Filter {
					base: Box::new(base),
					predicate: Box::new(predicate),
				};
				// A filter narrows the array without changing where it came
				// from.
				prev = Prev::Filter;
			}
			Step::Flatten => {
				base = LogicalExpr::Flatten {
					base: Box::new(base),
					explicit: true,
				};
				prev = Prev::Flatten;
				array_from_lookup = true;
			}
			step @ (Step::Index(_)
			| Step::First
			| Step::Last
			| Step::Method(..)
			| Step::RepeatRecurse) => {
				base = apply_step(base, step);
				prev = Prev::Other;
				array_from_lookup = false;
			}
		}
	}
	base
}

/// Nest a run of consecutive lookups onto `base`, with the implicit flatten
/// a following filter expects.
fn apply_lookups(
	mut base: LogicalExpr,
	lookups: Vec<LogicalLookup>,
	rest: &VecDeque<Step>,
) -> LogicalExpr {
	for lookup in lookups {
		base = LogicalExpr::Lookup {
			base: Box::new(base),
			lookup: Box::new(lookup),
		};
	}
	if let Some(Step::Filter(_)) = rest.front() {
		base = LogicalExpr::Flatten {
			base: Box::new(base),
			explicit: false,
		};
	}
	base
}

/// The form of one step applied to `base`. Steps with sequence semantics of
/// their own — optional, recursion, lookup runs — are placed by
/// [`fold_from`], never here.
fn apply_step(base: LogicalExpr, step: Step) -> LogicalExpr {
	let base = Box::new(base);
	match step {
		Step::Access(field) => LogicalExpr::Access {
			base,
			field,
		},
		Step::Index(index) => LogicalExpr::Index {
			base,
			index: Box::new(index),
		},
		Step::First => LogicalExpr::First(base),
		Step::Last => LogicalExpr::Last(base),
		Step::Each => LogicalExpr::Each(base),
		Step::Flatten => LogicalExpr::Flatten {
			base,
			explicit: true,
		},
		Step::Filter(predicate) => LogicalExpr::Filter {
			base,
			predicate: Box::new(predicate),
		},
		Step::Method(name, args) => LogicalExpr::Method {
			base,
			name,
			args,
		},
		Step::Destructure(fields) => LogicalExpr::Destructure {
			base,
			fields,
		},
		Step::RepeatRecurse => LogicalExpr::RepeatRecurse(base),
		Step::Lookup(lookup) => LogicalExpr::Lookup {
			base,
			lookup,
		},
		Step::Recurse(recurse, nested, instruction) => {
			LogicalExpr::Recurse(Box::new(LogicalRecurse {
				base: *base,
				recurse,
				path: nested.map(LogicalRecursePath::Nested),
				instruction,
			}))
		}
		Step::Optional => LogicalExpr::Optional {
			base,
			then: Box::new(LogicalExpr::Current),
		},
	}
}

/// Lower an idiom that names an output position: a projection alias, a
/// lookup alias.
fn lower_output_path(idiom: surface::Idiom) -> Lowered<OutputPath> {
	Ok(lower_idiom(idiom)?.output_path())
}

pub(crate) fn lower_destructure(
	parts: Vec<surface::part::DestructurePart>,
) -> Lowered<Vec<LogicalDestructureField>> {
	parts
		.into_iter()
		.map(|part| {
			Ok(match part {
				surface::part::DestructurePart::All(name) => LogicalDestructureField::All(name),
				surface::part::DestructurePart::Field(name) => LogicalDestructureField::Field(name),
				surface::part::DestructurePart::Aliased(name, idiom) => {
					LogicalDestructureField::Aliased(name, lower_body(idiom)?)
				}
				surface::part::DestructurePart::Destructure(name, parts) => {
					LogicalDestructureField::Destructure(name, lower_destructure(parts)?)
				}
				surface::part::DestructurePart::Rest => LogicalDestructureField::Rest,
			})
		})
		.collect()
}

pub(crate) fn lower_instruction(
	instruction: surface::part::RecurseInstruction,
) -> Lowered<LogicalRecurseInstruction> {
	Ok(match instruction {
		surface::part::RecurseInstruction::Path {
			inclusive,
		} => LogicalRecurseInstruction::Path {
			inclusive,
		},
		surface::part::RecurseInstruction::Collect {
			inclusive,
		} => LogicalRecurseInstruction::Collect {
			inclusive,
		},
		surface::part::RecurseInstruction::Shortest {
			expects,
			inclusive,
		} => LogicalRecurseInstruction::Shortest {
			expects: lower_expr(expects)?,
			inclusive,
		},
	})
}

pub(crate) fn lower_lookup(lookup: surface::lookup::Lookup) -> Lowered<LogicalLookup> {
	Ok(LogicalLookup {
		kind: lookup.kind,
		expr: lookup.expr.map(lower_fields).transpose()?,
		only: lookup.only,
		what: lookup.what.into_iter().map(lower_lookup_subject).collect::<Lowered<_>>()?,
		cond: lookup.cond.map(|c| lower_expr(c.0)).transpose()?,
		split: lookup
			.split
			.map(|s| s.0.into_iter().map(|x| lower_idiom(x.0)).collect::<Lowered<_>>())
			.transpose()?,
		group: lookup
			.group
			.map(|g| g.0.into_iter().map(|x| lower_idiom(x.0)).collect::<Lowered<_>>())
			.transpose()?,
		order: lookup.order.map(lower_ordering).transpose()?,
		limit: lookup.limit.map(|l| lower_expr(l.0)).transpose()?,
		start: lookup.start.map(|s| lower_expr(s.0)).transpose()?,
		alias: lookup.alias.map(lower_output_path).transpose()?,
		trace: crate::expr::logical::LookupTrace::None,
	})
}

fn lower_lookup_subject(subject: surface::lookup::LookupSubject) -> Lowered<LogicalLookupSubject> {
	Ok(match subject {
		surface::lookup::LookupSubject::Table {
			table,
			referencing_field,
		} => LogicalLookupSubject::Table {
			table,
			referencing_field,
		},
		surface::lookup::LookupSubject::Range {
			table,
			range,
			referencing_field,
		} => LogicalLookupSubject::Range {
			table,
			range: lower_record_id_range(range)?,
			referencing_field,
		},
	})
}

/// A projection's fields, as a `SELECT` or a `RETURN` clause writes them.
pub fn lower_fields(fields: impl Into<surface::Fields>) -> Lowered<LogicalFields> {
	Ok(match fields.into() {
		surface::Fields::Value(selector) => LogicalFields::Value {
			expr: lower_expr(selector.expr)?,
			alias: selector.alias.map(lower_output_path).transpose()?,
		},
		surface::Fields::Select(fields) => LogicalFields::Select(
			fields
				.into_iter()
				.map(|field| {
					Ok(match field {
						surface::Field::All => LogicalField::All,
						surface::Field::Single(selector) => lower_field(selector)?,
					})
				})
				.collect::<Lowered<_>>()?,
		),
	})
}

/// A projected field. An unaliased field whose derived name would be spelled
/// from a nested plan — a statement in scalar position — is aliased to the
/// SurrealQL text of the expression as written, which is the key such a
/// projection takes; every other field keeps the alias the user
/// wrote, or none, and is named by [`LogicalExpr::output_path`].
fn lower_field(selector: surface::field::Selector) -> Lowered<LogicalField> {
	let written = match &selector.alias {
		Some(_) => None,
		None => Some(selector.expr.to_sql()),
	};
	let expr = lower_expr(selector.expr)?;
	let alias = match selector.alias {
		Some(alias) => Some(lower_output_path(alias)?),
		None if expr.output_name_renders_a_statement() => written.map(OutputPath::field),
		None => None,
	};
	Ok(LogicalField::user(expr, alias))
}

fn lower_ordering(ordering: surface::order::Ordering) -> Lowered<LogicalOrdering> {
	Ok(match ordering {
		surface::order::Ordering::Random => LogicalOrdering::Random,
		surface::order::Ordering::Order(list) => LogicalOrdering::Order(
			list.0
				.into_iter()
				.map(|order| {
					Ok(LogicalOrder {
						value: lower_idiom(order.value)?,
						collate: order.collate,
						numeric: order.numeric,
						direction: order.direction,
					})
				})
				.collect::<Lowered<_>>()?,
		),
	})
}

/// A postfix application of `op` to the lowered `expr`. The two range
/// operators stay operators. A method or closure call written after an
/// expression lowers to the node the idiom lowering builds for the same
/// construct — [`LogicalExpr::Method`] and [`LogicalExpr::Invoke`] — so every
/// pass meets one node per construct whichever way it was spelled.
fn lower_postfix(expr: LogicalExpr, op: surface::PostfixOperator) -> Lowered<LogicalExpr> {
	let expr = Box::new(expr);
	Ok(match op {
		surface::PostfixOperator::Range => LogicalExpr::Postfix {
			expr,
			op: LogicalPostfixOperator::Range,
		},
		surface::PostfixOperator::RangeSkip => LogicalExpr::Postfix {
			expr,
			op: LogicalPostfixOperator::RangeSkip,
		},
		surface::PostfixOperator::MethodCall(name, args) => LogicalExpr::Method {
			base: expr,
			name: name.into(),
			args: lower_exprs(args)?,
		},
		surface::PostfixOperator::Call(args) => LogicalExpr::Invoke {
			target: expr,
			args: lower_exprs(args)?,
		},
	})
}

#[cfg(test)]
mod tests {
	use std::collections::BTreeSet;

	use surrealdb_types::ToSql;

	use super::*;
	use crate::expr as sql;
	use crate::expr::logical::LogicalRecursePath;
	use crate::parser::TopLevelExpr;

	/// The shape of a lowered tree, outermost operator first, so a test can
	/// state the pipeline it expects without matching on every field.
	fn shape(op: &LogicalOp) -> Vec<&'static str> {
		let mut out = vec![op.name()];
		let mut current = op;
		while let Some(child) = current.children().first() {
			out.push(child.name());
			current = child;
		}
		out
	}

	fn parse(sql: &str) -> sql::Expr {
		let ast = crate::parser::parse(sql).expect("test statement should parse");
		let top = ast.expressions.into_iter().next().expect("one statement");
		let TopLevelExpr::Expr(expr) = top else {
			panic!("expected a bare expression");
		};
		expr
	}

	fn lower_sql(sql: &str) -> LogicalOp {
		lower(parse(sql)).expect("statement should lower")
	}

	/// An `IF`, `THROW`, `BREAK` and `CONTINUE` in statement position are
	/// operators of their own: at the top level, in a block, in a `FOR` body,
	/// as a `LET` value and as an arm of another `IF`.
	#[test]
	fn control_flow_in_statement_position_lowers_to_operators() {
		assert_eq!(
			lower_sql("IF $a { 1 } ELSE { 2 }").to_string(),
			"If[$a    ELSE]\n  Sequence\n    Eval[1]\n  Sequence\n    Eval[2]"
		);
		assert_eq!(lower_sql("THROW 'x'").to_string(), "Throw['x']");
		assert_eq!(lower_sql("BREAK").to_string(), "Break");
		assert_eq!(lower_sql("CONTINUE").to_string(), "Continue");
		assert_eq!(
			lower_sql("{ IF $a { BREAK }; CONTINUE; THROW 1 }").to_string(),
			"Sequence\n  If[$a]\n    Sequence\n      Break\n  Continue\n  Throw[1]"
		);
		assert_eq!(
			lower_sql("FOR $x IN [1] { IF $x { CONTINUE } }").to_string(),
			"Foreach[$x IN [1]]\n  Sequence\n    If[$x]\n      Sequence\n        Continue"
		);
		assert_eq!(
			lower_sql("LET $v = IF $a THEN 1 END").to_string(),
			"Let[$v]\n  If[$a]\n    Eval[1]"
		);
		assert_eq!(
			lower_sql("IF $a THEN IF $b THEN 1 ELSE 2 END END").to_string(),
			"If[$a]\n  If[$b    ELSE]\n    Eval[1]\n    Eval[2]"
		);
	}

	/// A statement `IF` with an arm written as a `LET`, at any depth of the
	/// `IF`s written as arms, keeps its scalar form; a block arm holding a
	/// `LET` does not.
	#[test]
	fn a_statement_if_with_a_let_arm_stays_an_expression() {
		assert!(matches!(lower_sql("IF $c THEN (LET $x = 1) ELSE 3 END"), LogicalOp::Eval(_)));
		assert!(matches!(lower_sql("IF $c THEN 1 ELSE (LET $x = 1) END"), LogicalOp::Eval(_)));
		assert!(matches!(
			lower_sql("IF $t THEN 1 ELSE IF $c THEN (LET $x = 1) ELSE 3 END"),
			LogicalOp::Eval(_)
		));
		assert!(matches!(lower_sql("IF $t { LET $x = 1; } ELSE { 3 }"), LogicalOp::If(_)));
	}

	/// The value of a `RETURN`, and an `IF` inside an expression, stay in
	/// value position.
	#[test]
	fn control_flow_in_value_position_stays_an_expression() {
		assert_eq!(
			lower_sql("RETURN IF $a THEN 1 END").to_string(),
			"Return\n  Eval[IF $a THEN 1 END]"
		);
		assert_eq!(lower_sql("RETURN THROW 'x'").to_string(), "Return\n  Eval[THROW 'x']");
		assert!(matches!(lower_sql("[IF $a THEN 1 END]"), LogicalOp::Eval(_)));
	}

	/// The scalar form of a statement `IF` evaluates to what the operator
	/// yields: a block arm becomes the subquery of its block.
	#[test]
	fn a_statement_if_has_its_value_position_form() {
		let LogicalOp::If(ifs) = lower_sql("IF $a { 1 } ELSE IF $b { 2 }") else {
			panic!("an IF operator");
		};
		let scalar = ifs.to_scalar();
		assert_eq!(scalar.branches.len(), 2);
		assert!(
			scalar.branches.iter().all(
				|(_, arm)| matches!(arm, LogicalExpr::Subquery(block) if block.name() == "Sequence")
			),
			"a block arm is the subquery of its block"
		);
		assert!(scalar.otherwise.is_none());
	}

	fn expr(sql: &str) -> LogicalExpr {
		let sql::Expr::Return(out) = parse(&format!("RETURN {sql}")) else {
			panic!("expected a RETURN");
		};
		lower_expr(out.what).expect("expression should lower")
	}

	/// A method or closure call written after an expression lowers to the
	/// node the idiom lowering builds for the same construct, so no pass can
	/// meet one construct under two spellings. The range operators stay
	/// postfix operators.
	#[test]
	fn a_postfix_call_lowers_to_the_node_of_its_construct() {
		let one = || {
			let sql::Expr::Return(out) = parse("RETURN 1") else {
				panic!("expected a RETURN");
			};
			out.what
		};
		let postfix = |op| {
			lower_expr(sql::Expr::Postfix {
				expr: Box::new(one()),
				op,
			})
			.expect("expression should lower")
		};

		let method = postfix(sql::PostfixOperator::MethodCall("len".to_owned(), vec![]));
		assert!(
			matches!(
				&method,
				LogicalExpr::Method { name, args, .. } if name.as_str() == "len" && args.is_empty()
			),
			"{method:?}"
		);
		assert!(method.to_sql().ends_with(".len()"), "{}", method.to_sql());

		let call = postfix(sql::PostfixOperator::Call(vec![one()]));
		assert!(matches!(&call, LogicalExpr::Invoke { args, .. } if args.len() == 1), "{call:?}");
		assert_eq!(call.to_sql(), "(1)(1)");

		let range = postfix(sql::PostfixOperator::Range);
		assert!(matches!(
			&range,
			LogicalExpr::Postfix {
				op: LogicalPostfixOperator::Range,
				..
			}
		));
		assert_eq!(range.to_sql(), "1..");
	}

	#[test]
	fn a_bare_select_is_a_projection_over_a_source() {
		assert_eq!(shape(&lower_sql("SELECT * FROM t")), ["Project", "Source"]);
	}

	#[test]
	fn a_where_clause_becomes_a_filter_under_the_projection() {
		assert_eq!(
			shape(&lower_sql("SELECT * FROM t WHERE a > 1")),
			["Project", "Filter", "Source"]
		);
	}

	#[test]
	fn the_pipeline_order_matches_the_physical_planner() {
		// SPLIT and GROUP are mutually exclusive in the grammar, so the full
		// pipeline is only reachable one of the two at a time.
		assert_eq!(
			shape(&lower_sql(
				"SELECT d FROM t WHERE b > 1 SPLIT d ORDER BY d LIMIT 1 FETCH f TIMEOUT 1s"
			)),
			["Timeout", "Fetch", "Project", "Limit", "Sort", "Split", "Filter", "Source"]
		);
	}

	#[test]
	fn a_grouped_pipeline_puts_the_aggregation_below_the_sort() {
		assert_eq!(
			shape(&lower_sql(
				"SELECT d FROM t WHERE b > 1 GROUP BY d ORDER BY d LIMIT 1 FETCH f TIMEOUT 1s"
			)),
			["Timeout", "Fetch", "Limit", "Sort", "Aggregate", "Filter", "Source"]
		);
	}

	#[test]
	fn several_sources_concatenate() {
		let op = lower_sql("SELECT * FROM a, b");
		assert_eq!(shape(&op), ["Project", "UnionAll", "Source"]);
		let LogicalOp::Project(LogicalProject {
			input,
			..
		}) = &op
		else {
			panic!("expected a projection");
		};
		assert_eq!(input.children().len(), 2);
	}

	#[test]
	fn a_literal_source_is_values_and_a_record_id_is_not() {
		assert_eq!(shape(&lower_sql("SELECT * FROM [1, 2, 3]")), ["Project", "Values"]);
		assert_eq!(shape(&lower_sql("SELECT * FROM 'x'")), ["Project", "Values"]);
		assert_eq!(shape(&lower_sql("SELECT * FROM t:1")), ["Project", "Source"]);
		let mixed = lower_sql("SELECT * FROM t, [1]");
		let names: Vec<_> = mixed.children()[0].children().iter().map(LogicalOp::name).collect();
		assert_eq!(names, ["Source", "Values"]);
	}

	#[test]
	fn a_source_is_classified_by_its_syntax() {
		fn source_of(sql: &str) -> SourceRef {
			let op = lower_sql(sql);
			let LogicalOp::Project(LogicalProject {
				input,
				..
			}) = op
			else {
				panic!("expected a projection");
			};
			let LogicalOp::Source(LogicalSource {
				source,
				..
			}) = *input
			else {
				panic!("expected a source");
			};
			source
		}
		assert!(matches!(source_of("SELECT * FROM t"), SourceRef::Table(_)));
		assert!(matches!(source_of("SELECT * FROM t:1"), SourceRef::RecordId(_)));
		assert!(matches!(source_of("SELECT * FROM t:1..5"), SourceRef::RecordId(_)));
		assert!(matches!(source_of("SELECT * FROM |t:3|"), SourceRef::Mock(_)));
		assert!(matches!(source_of("SELECT * FROM $p"), SourceRef::Param(_)));
		assert!(matches!(source_of("SELECT * FROM a->likes->b"), SourceRef::Dynamic(_)));
		assert!(matches!(source_of("SELECT * FROM { a: 1 }"), SourceRef::Dynamic(_)));
	}

	#[test]
	fn a_subquery_source_is_lowered_eagerly() {
		// The inner statement's own pipeline sits under the Subquery node, so
		// the outer WHERE can be pushed through it by a pass.
		assert_eq!(
			shape(&lower_sql("SELECT * FROM (SELECT * FROM t WHERE a) WHERE b")),
			["Project", "Filter", "Subquery", "Project", "Filter", "Source"]
		);
	}

	#[test]
	fn a_subquery_source_under_for_update_is_a_locked_dynamic_source() {
		// The flag has to reach the physical planner's target check, and a
		// `Subquery` leaf cannot carry it, so the nested plan is held as the
		// scalar of a `Source` that does. Only the flagged form takes this
		// shape; without the clause the nested statement stays a leaf.
		let op = lower_sql("SELECT * FROM (SELECT * FROM t:1) FOR UPDATE");
		assert_eq!(shape(&op), ["Project", "Source"]);
		let LogicalOp::Project(LogicalProject {
			input,
			..
		}) = op
		else {
			panic!("expected a projection");
		};
		let LogicalOp::Source(LogicalSource {
			source,
			for_update,
			..
		}) = *input
		else {
			panic!("expected a source");
		};
		assert!(for_update, "the clause is carried on the source");
		let SourceRef::Dynamic(LogicalExpr::Subquery(nested)) = source else {
			panic!("expected the nested plan as a dynamic scalar, got {source:?}");
		};
		assert_eq!(shape(&nested), ["Project", "Source"]);
	}

	#[test]
	fn the_with_hint_is_attached_to_every_stored_source() {
		let op = lower_sql("SELECT * FROM a, b, [1] WITH NOINDEX");
		let leaves = op.children()[0].children();
		let names: Vec<_> = leaves.iter().map(LogicalOp::name).collect();
		assert_eq!(names, ["Source", "Source", "Values"]);
		for leaf in leaves {
			if let LogicalOp::Source(LogicalSource {
				with,
				..
			}) = leaf
			{
				assert!(with.is_some(), "stored sources carry the hint");
			}
		}
	}

	#[test]
	fn a_grouped_query_projects_in_the_aggregation_and_omits_the_projection() {
		// The aggregation carries the fields, so no Project is emitted above
		// it unless OMIT has something to do.
		assert_eq!(shape(&lower_sql("SELECT count() FROM t GROUP ALL")), ["Aggregate", "Source"]);
		assert_eq!(
			shape(&lower_sql("SELECT count() OMIT x FROM t GROUP ALL")),
			["Project", "Aggregate", "Source"]
		);
	}

	#[test]
	fn an_absent_version_or_timeout_contributes_no_operator() {
		// Both are held as a bare expression whose absent form is a NONE
		// literal, so a naive lowering would wrap every statement in both.
		assert_eq!(shape(&lower_sql("SELECT * FROM t")), ["Project", "Source"]);
	}

	#[test]
	fn only_wraps_the_whole_pipeline() {
		assert_eq!(shape(&lower_sql("SELECT * FROM ONLY t:1")), ["Only", "Project", "Source"]);
	}

	#[test]
	fn a_where_clause_is_a_user_filter_never_a_permission_filter() {
		let op = lower_sql("SELECT * FROM t WHERE a > 1");
		assert!(op.any(|n| matches!(n, LogicalOp::Filter(_))));
		assert!(!op.any(|n| matches!(n, LogicalOp::PermissionFilter(_))));
	}

	#[test]
	fn every_scalar_in_a_rendered_plan_is_surrealql() {
		let rendered = lower_sql("SELECT a FROM t WHERE b > 1 LIMIT 2").to_string();
		assert!(rendered.contains("b > 1"), "predicate should render as SurrealQL: {rendered}");
		assert!(rendered.contains("LIMIT 2"), "limit should render: {rendered}");
		assert!(rendered.contains("Source[t]"), "source should name its table: {rendered}");
	}

	/// Scalars whose logical rendering must equal the expression layer's, so
	/// that a plan's scalars print the same whichever type holds them. A
	/// [`LogicalExpr::Subquery`] is the intentional exception: it renders as
	/// the plan it holds, so no statement appears here.
	const SCALAR_CORPUS: &[&str] = &[
		// Literals.
		"NONE",
		"NULL",
		"true",
		"1",
		"-1",
		"1.5",
		"1.5dec",
		"'x'",
		"d'2024-01-01T00:00:00Z'",
		"1h30m",
		"u'018e3b7a-0000-7000-8000-000000000000'",
		"/a+/",
		"..",
		// Collections and record ids.
		"[1, 2, 'x']",
		"[]",
		"{ a: 1, b: [2] }",
		"{ }",
		"{ type: 'Point', coordinates: [1.0, 2.0] }",
		"t:1",
		"t:['a', 1]",
		"t:{ a: 1, b: 'x' }",
		"t:1..5",
		"t:1>..=5",
		"t:..5",
		"t:uuid()",
		"t:⟨a b⟩",
		// Paths.
		"a.b[0].c[$]",
		"a[WHERE b > 1].c",
		"a.*",
		"a[*]",
		"a.{ b, c: d.e }",
		"a.{ b.{ c } }",
		"$p.name",
		"(1 + 2).b",
		"a.b.{2..}(.c)",
		"a.{1..3+collect}",
		"a.{..+path+inclusive}",
		"a.?.b",
		"->likes->person",
		"->(likes WHERE x > 1 AS l)",
		"<-knows<-person.name",
		"<->knows",
		"->likes:1..5",
		"<~refs",
		"->(likes, follows)",
		// Calls, methods, closures.
		"count(a, 'x')",
		"fn::custom(1)",
		"math::PI",
		"a.map(|$v| $v + 1)",
		"a.len()",
		"$f(1)",
		"|$x: int| $x + 1",
		"|$x: int, $y: string| $x",
		"|| 1",
		"|$x: int| a + $x",
		// Control flow as values.
		"IF a THEN 1 ELSE 2 END",
		"IF a THEN 1 ELSE IF b THEN 2 ELSE 3 END",
		"IF a THEN 1 END",
		"[IF a THEN 1 END]",
		"THROW 'x'",
		"BREAK",
		"CONTINUE",
		// Prefix and postfix operators.
		"!a",
		"-a",
		"+a",
		"-(1 + 2) * 3",
		"-(-1)",
		"<int> a",
		"<string> (a + b)",
		"..5",
		"..=5",
		"a..",
		"a>..",
		// Every binary operator.
		"a AND b",
		"a OR b",
		"a ?? b",
		"a ?: b",
		"a = b",
		"a == b",
		"a != b",
		"a ?= b",
		"a *= b",
		"a < b",
		"a <= b",
		"a > b",
		"a >= b",
		"a CONTAINS b",
		"a CONTAINSNOT b",
		"a CONTAINSALL b",
		"a CONTAINSANY b",
		"a CONTAINSNONE b",
		"a IN b",
		"a NOT IN b",
		"a ALLINSIDE b",
		"a ANYINSIDE b",
		"a NONEINSIDE b",
		"a OUTSIDE b",
		"a INTERSECTS b",
		"a @@ 'x'",
		"a @1@ 'x'",
		"a <|3|> b",
		"a <|3,COSINE|> b",
		"a + b",
		"a - b",
		"a * b",
		"a / b",
		"a % b",
		"a ** b",
		"a..b",
		"a..=b",
		"a>..b",
		"a>..=b",
		// Precedence and associativity.
		"a + b * c",
		"(a + b) * c",
		"a - (b - c)",
		"a ** (b ** c)",
		"(a ** b) ** c",
		"a AND (b OR c)",
		"(a = b) = c",
	];

	#[test]
	fn scalar_rendering_preserves_precedence_and_normalized_geometry() {
		for sql in SCALAR_CORPUS {
			let sql::Expr::Return(out) = parse(&format!("RETURN {sql}")) else {
				panic!("expected a RETURN");
			};
			let expected = match out.what.as_static_value() {
				Some(crate::val::Value::Geometry(geometry)) => {
					surface::Literal::Geometry(geometry).to_sql()
				}
				_ => out.what.to_sql(),
			};
			let lowered = lower_expr(out.what).expect("expression should lower");
			assert_eq!(
				lowered.to_sql(),
				expected,
				"rendering of `{sql}` differs from the expression layer"
			);
		}
	}

	/// Every part kind the idiom grammar produces, in the positions the
	/// lowering rules distinguish: roots (a field, `@`, a starting
	/// expression, a leading lookup), mapping steps before and after
	/// non-mapping steps, lookup runs with and without filters, optional
	/// chains, both recursion forms with and without `.@`, and destructure
	/// aliases written as paths and as expressions.
	const IDIOM_CORPUS: &[&str] = &[
		"a",
		"a.b",
		"a.b.c",
		"a[0]",
		"a[$]",
		"a[1]",
		"a[-1]",
		"a[1..3]",
		"a[b]",
		"a[$i]",
		"a.*",
		"a[*]",
		"a.*.b",
		"a.b.*",
		"a.*.*",
		"a.*[0]",
		"a…",
		"a...",
		"a.b…",
		"a[WHERE b > 1]",
		"a[WHERE b > 1].c",
		"a[WHERE b > 1][0].c",
		"a.b[0]",
		"a.b[0].c",
		"a.b[0].c[0]",
		"a.b.c[0]",
		"a.b.c.d[WHERE e][$]",
		"a.len()",
		"a.b.len()",
		"a.slice(1, 2).c",
		"a.map(|$v| $v.b)",
		"a.{ b }",
		"a.{ b, c }",
		"a.{ b.* }",
		"a.{ b: c }",
		"a.{ b: c.d }",
		"a.{ b: c[0].d }",
		"a.{ b: $this.c }",
		"a.{ b: c * 2 }",
		"a.{ b: (c + 1).d }",
		"a.{ b: [1, 2][0] }",
		"a.{ b: ->likes->person }",
		"a.{ b.{ c, d: e } }",
		"a.{ b, c: d.e[0] }.f",
		"a.{ b }[0]",
		"a.?",
		"a.?.b",
		"a.?.b.c",
		"a.?.b[0]",
		"a.?.b.?.c",
		"a.b.?.c[0]",
		"a[0].?.b",
		"->likes",
		"->likes->person",
		"->likes->person->follows",
		"->likes->person.name",
		"->likes->person.name[0]",
		"->likes[WHERE x > 1]",
		"->likes[WHERE x > 1]->person",
		"->likes[WHERE x > 1]->person.name",
		"->likes…->person",
		"->likes...->person",
		"->likes[0]",
		"->likes.out",
		"->likes.out->person",
		"<-knows",
		"<-knows<-person",
		"<->knows",
		"<->knows<->person",
		"<~refs",
		"<~refs<~more",
		"<~(refs FIELD a)",
		"->?",
		"->(likes, follows)",
		"->(likes WHERE x > 1)",
		"->(likes WHERE x > 1 AS l)",
		"->(likes AS l)->person",
		"->(likes AS l).name",
		"->likes:1..5",
		"->likes:1..",
		"->likes:..5",
		"->(likes:1..)",
		"->(SELECT * FROM likes)",
		"->(SELECT VALUE out FROM likes WHERE x > 1 ORDER BY y LIMIT 1 START 1)",
		"->(SELECT VALUE out FROM ONLY likes)",
		"->(SELECT * FROM likes SPLIT ON a)",
		"->(SELECT * FROM likes GROUP BY b)",
		"->(SELECT * FROM likes GROUP ALL)",
		"a->likes",
		"a->likes->person",
		"a->likes->person.name",
		"a.b->likes",
		"a.b->likes->person[0]",
		"a.{..}",
		"a.{2}",
		"a.{1..3}",
		"a.{..3}",
		"a.{2..}",
		"a.{..}(.b)",
		"a.{..}(.b.c)",
		"a.{..}(->knows->person)",
		"a.{..}(->knows->person).name",
		"a.{..}.b",
		"a.{..}.b.c",
		"a.{..}->knows->person",
		"a.{..}->knows->person.name",
		"a.{..}.b.@",
		"a.{..}.b.@.c",
		"a.{..}.{ name, friends: ->knows->person.@ }",
		"a.{1..3}(.{ name, friends: ->knows->person.@ })",
		"a.{..+path}(->knows->person)",
		"a.{..+path+inclusive}(->knows->person)",
		"a.{..+collect}(->knows->person)",
		"a.{..+collect+inclusive}(->knows->person)",
		"a.{..+shortest=person:1}(->knows->person)",
		"a.{..+shortest=person:1+inclusive}(->knows->person)",
		"a.{..+shortest=$target}(->knows->person)",
		"@.a",
		"@.a.b[0]",
		"$p.a",
		"$p.a.b",
		"$p.a[0]",
		"$p.a[0].b",
		"$p->likes->person",
		"$p[0]",
		"$p[WHERE x]",
		"$p.?.a",
		"$p.{..}(.a)",
		"(1 + 2).b",
		"(-1).abs()",
		"(-1.5).abs()",
		"(-1.5dec).abs()",
		"(!a).b",
		"(a..).len()",
		"(a.b).c[0]",
		"(a.b[0]).c[0]",
		"[1, 2][0]",
		"[1, 2].len()",
		"[{ a: [1, 2] }, { a: [3, 4] }].a[0]",
		"{ a: 1 }.a",
		"{ a: { b: 1 } }.a.b",
		"person:1.name",
		"person:1->likes->person",
		"person:1.{ name, age }",
		"'x'.len()",
		"count().a",
		"fn::f().a[0]",
		"`a.b`",
		"`a.b`.c",
		"⟨a b⟩.⟨c d⟩",
		"a.`b c`",
		"a.function",
		"a.b[WHERE c IN [1, 2]].d.e",
		"a.b[WHERE c][0].d[WHERE e][$]",
		"a[WHERE b].c[WHERE d]->e->f[WHERE g].h",
		// A grouped path start. Every step but a field access and `.*` reads as
		// the body of a broadcast over the access to its left, so these are all
		// different expressions from the same text without the parentheses.
		"(a.b)[0]",
		"(a.b)[$]",
		"(a.b)[WHERE c]",
		"(a.b).len()",
		"(a.b).*",
		"(a.b).?.c",
		"(a.b).{ x }",
		"(a.b)->likes->person",
		"(a.b.c)[0]",
		"(a.b)[0].c",
		"((a.b)[0].c)[1]",
		"($p.a)[0]",
		"((1 + 2).b)[0]",
		"(person:1.name)[0]",
	];

	/// Every idiom renders as an idiom that reads back as the same expression,
	/// in both the single-line and the pretty layout.
	///
	/// This is a round trip rather than a comparison against the surface text,
	/// because the two do not always agree and should not. The surface
	/// parenthesises a path start that is itself a path whenever it *might*
	/// matter, without proving that it does; lowering keeps only the grouping
	/// that changes the expression. `(a.b).c` and `a.b.c` lower to one tree, so
	/// no rendering of that tree can reproduce both spellings, and the
	/// canonical one is right.
	///
	/// What has to hold is that the rendering parses back to the expression it
	/// came from. That is the property the stored text depends on — a
	/// definition is re-read from its rendering — and it is the stronger claim:
	/// a grouping this layer drops but must not would reparse as a broadcast
	/// over the access to its left, which byte-for-byte-against-the-surface
	/// never checked, since the corpus is written in whatever spelling the
	/// surface happens to produce.
	#[test]
	fn a_path_renders_as_the_idiom_it_was_lowered_from() {
		let lower_of = |sql: &str| {
			let sql::Expr::Return(out) = parse(&format!("RETURN {sql}")) else {
				panic!("expected a RETURN for `{sql}`");
			};
			lower_expr(out.what).expect("expression should lower")
		};
		for sql in IDIOM_CORPUS {
			let lowered = lower_of(sql);
			for (layout, rendered) in [("", lowered.to_sql()), (" pretty", lowered.to_sql_pretty())]
			{
				assert_eq!(
					lower_of(&rendered),
					lowered,
					"the{layout} rendering of `{sql}` is `{rendered}`, which is a different \
					 expression"
				);
			}
		}
	}

	/// Grouping a path changes nothing when only mapping steps follow it.
	///
	/// `(a.b).c` and `a.b.c` apply `.c` to the same values, so both lower to
	/// one chain and the plan holds nothing that says which spelling produced
	/// it. The plan renders the unparenthesised form, which reads back as the
	/// same plan.
	///
	/// Grouping that does change the meaning is kept: a following step that
	/// broadcasts folds over the group as a unit, so the plan holds the group
	/// as the base of that fold and renders its parentheses. `(a.b).c[0]` in
	/// the corpus above covers that, and it renders byte for byte as written.
	#[test]
	fn grouping_a_path_before_a_mapping_step_is_canonicalised_away() {
		let grouped = lower_idiom("(a.b).c");
		assert_eq!(shape_of(&grouped), shape_of(&lower_idiom("a.b.c")));
		assert_eq!(grouped.to_sql(), "a.b.c");
	}

	/// Every idiom renders as a path that reads back as the plan it came from.
	///
	/// Two paths built from the same steps can differ only in where a fold
	/// sits, so a rendering that drops what separates them names a different
	/// plan: `(a.b).c[0]` folds over `a.b`, where `a.b.c[0]` folds over `a`
	/// and maps `.b` within it. Rendering is idempotent for the same reason —
	/// a plan that read back as itself renders the same way again.
	#[test]
	fn a_path_reads_back_as_the_plan_it_was_rendered_from() {
		for sql in IDIOM_CORPUS {
			let lowered = lower_idiom(sql);
			let rendered = lowered.to_sql();
			let reread = lower_idiom(&rendered);
			assert_eq!(
				shape_of(&reread),
				shape_of(&lowered),
				"`{sql}` rendered as `{rendered}`, which is a different plan"
			);
			assert_eq!(reread.to_sql(), rendered, "rendering `{sql}` is not idempotent");
		}
	}

	/// A root written after the first part of a path, which the parser never
	/// produces, is the value the rest of the path applies to: the parts
	/// before it are replaced, not read.
	#[test]
	fn a_root_inside_a_path_restarts_it() {
		let field = |name: &str| surface::Part::Field(name.into());
		let doc_then_b =
			super::lower_idiom(surface::Idiom(vec![field("a"), surface::Part::Doc, field("b")]))
				.expect("lowers");
		assert_eq!(shape_of(&doc_then_b), shape_of(&lower_idiom("@.b")));

		let start = surface::Expr::Param(surface::Param::from("p".to_owned()));
		let start_then_b = super::lower_idiom(surface::Idiom(vec![
			field("a"),
			surface::Part::Start(start),
			field("b"),
		]))
		.expect("lowers");
		assert_eq!(shape_of(&start_then_b), shape_of(&lower_idiom("$p.b")));

		// The last root wins when several are written.
		let twice = super::lower_idiom(surface::Idiom(vec![
			surface::Part::Doc,
			field("a"),
			surface::Part::Doc,
			field("b"),
		]))
		.expect("lowers");
		assert_eq!(shape_of(&twice), shape_of(&lower_idiom("@.b")));

		// A recursion body takes a root the same way.
		let body =
			super::lower_body(surface::Idiom(vec![field("a"), surface::Part::Doc, field("b")]))
				.expect("lowers");
		assert_eq!(shape_of(&body), shape_of(&lower_idiom("@.b")));
	}

	/// Lowers an idiom written as surface SurrealQL.
	fn lower_idiom(sql: &str) -> LogicalExpr {
		let sql::Expr::Return(out) = parse(&format!("RETURN {sql}")) else {
			panic!("expected a RETURN");
		};
		lower_expr(out.what).expect("expression should lower")
	}

	/// A compact description of a path's structure: each form by its
	/// surface spelling over its base, with `B(base; body)` for a broadcast
	/// (`B*` when it flattens its results), `?(base; then)` for an optional,
	/// `R(base; body)` / `R(base; (body))` for a trailing / nested recursion,
	/// `~` for the implicit flatten, `…` for the written one and `_` for the
	/// current value.
	fn shape_of(e: &LogicalExpr) -> String {
		match e {
			LogicalExpr::Field(name) => name.to_string(),
			LogicalExpr::Doc => "@".to_string(),
			LogicalExpr::Current => "_".to_string(),
			LogicalExpr::Access {
				base,
				field,
			} => format!("{}.{field}", shape_of(base)),
			LogicalExpr::Index {
				base,
				..
			} => format!("{}[i]", shape_of(base)),
			LogicalExpr::First(base) => format!("{}[first]", shape_of(base)),
			LogicalExpr::Last(base) => format!("{}[$]", shape_of(base)),
			LogicalExpr::Each(base) => format!("{}.*", shape_of(base)),
			LogicalExpr::Flatten {
				base,
				explicit,
			} => format!(
				"{}{}",
				shape_of(base),
				if *explicit {
					"…"
				} else {
					"~"
				}
			),
			LogicalExpr::Filter {
				base,
				..
			} => format!("{}[W]", shape_of(base)),
			LogicalExpr::Method {
				base,
				name,
				..
			} => format!("{}.{name}()", shape_of(base)),
			LogicalExpr::Destructure {
				base,
				..
			} => format!("{}.{{}}", shape_of(base)),
			LogicalExpr::Optional {
				base,
				then,
			} => format!("?({}; {})", shape_of(base), shape_of(then)),
			LogicalExpr::Lookup {
				base,
				lookup,
			} => format!("{}{}", shape_of(base), lookup.to_sql()),
			LogicalExpr::Recurse(r) => match &r.path {
				None => format!("R({})", shape_of(&r.base)),
				Some(LogicalRecursePath::Nested(body)) => {
					format!("R({}; ({}))", shape_of(&r.base), shape_of(body))
				}
				Some(LogicalRecursePath::Trailing(body)) => {
					format!("R({}; {})", shape_of(&r.base), shape_of(body))
				}
			},
			LogicalExpr::RepeatRecurse(base) => format!("{}.@", shape_of(base)),
			LogicalExpr::Broadcast {
				base,
				body,
				flatten,
			} => format!(
				"B{}({}; {})",
				if *flatten {
					"*"
				} else {
					""
				},
				shape_of(base),
				shape_of(body)
			),
			other @ (LogicalExpr::Literal(_)
			| LogicalExpr::Array(_)
			| LogicalExpr::Set(_)
			| LogicalExpr::Object(_)
			| LogicalExpr::RecordId(_)
			| LogicalExpr::Param(_)
			| LogicalExpr::Table(_)
			| LogicalExpr::Mock(_)
			| LogicalExpr::Constant(_)
			| LogicalExpr::Prefix {
				..
			}
			| LogicalExpr::Postfix {
				..
			}
			| LogicalExpr::Invoke {
				..
			}
			| LogicalExpr::Binary {
				..
			}
			| LogicalExpr::Call(_)
			| LogicalExpr::Closure(_)
			| LogicalExpr::If(_)
			| LogicalExpr::Throw(_)
			| LogicalExpr::Break
			| LogicalExpr::Continue
			| LogicalExpr::SetField {
				..
			}
			| LogicalExpr::Subquery(_)) => other.to_sql(),
		}
	}

	/// The placement of the sequence-semantics forms, one case per rule.
	#[test]
	fn lowering_places_the_sequence_forms_by_the_continuation_rules() {
		let cases = [
			// Mapping steps followed only by mapping steps nest plainly.
			("a", "a"),
			("a.b", "a.b"),
			("a.b.c", "a.b.c"),
			("a.*.b", "a.*.b"),
			("a.{ b }.c", "a.{}.c"),
			// A mapping step followed by a non-mapping step broadcasts, and
			// the body continues the fold so a later mapping step broadcasts
			// again for the case where the base is not an array.
			("a[0]", "a[i]"),
			("a.b[0]", "B(a; _.b[i])"),
			("a.b.c[0]", "B(a; B(_.b; _.c[i]))"),
			("a.b[1].c[0]", "B(a; B(_.b[i]; _.c[i]))"),
			("a.*[0]", "B(a; _.*[i])"),
			("a.{ b }[0]", "B(a; _.{}[i])"),
			("a.b.len()", "B(a; _.b.len())"),
			("a[0].b", "a[i].b"),
			("a[WHERE b].c", "a[W].c"),
			// A starting expression is the base of the first step.
			("$p.a", "$p.a"),
			("$p.a[0]", "B($p; _.a[i])"),
			("(a.b).c[0]", "B(a.b; _.c[i])"),
			("[1].a[0]", "B([1]; _.a[i])"),
			// A lookup run over anything but a lookup or a flatten, followed
			// by more steps or of more than one hop, broadcasts.
			("->likes", "_->likes"),
			("->likes->person", "B(_; _->likes->person)"),
			("->likes.out", "B(_; _->likes.out)"),
			("a->likes", "a->likes"),
			("a->likes->person", "B(a; _->likes->person)"),
			("a->likes.out", "B(a; _->likes.out)"),
			("a.b->likes", "B(a; _.b->likes)"),
			// A lookup followed by a filter has an implicit flatten; the
			// filter keeps the array's lookup origin, so a further lookup run
			// over it that continues broadcasts with flattening. A single
			// final hop does not continue.
			("->likes[WHERE x]", "B(_; _->likes~[W])"),
			("->likes[WHERE x]->person", "B(_; _->likes~[W]->person)"),
			("->likes[WHERE x]->person.name", "B(_; B*(_->likes~[W]; _->person.name))"),
			("a->likes[WHERE x]->person", "B(a; _->likes~[W]->person)"),
			// A written flatten blocks the broadcast of the lookup directly
			// after it, and marks the array as coming from a lookup.
			("->likes…->person", "B(_; _->likes…->person)"),
			("a…->likes->person", "a…->likes->person"),
			("a.b…->likes->person", "B(a; _.b…->likes->person)"),
			("a…[WHERE x]->likes.out", "B*(a…[W]; _->likes.out)"),
			// Optional takes the rest as a plain body.
			("a.?", "?(a; _)"),
			("a.?.b.c[0]", "?(a; _.b.c[i])"),
			("a.b.?.c[0]", "B(a; ?(_.b; _.c[i]))"),
			// A nested recursion is one step; a trailing one takes the rest up
			// to and including `.@`, and the suffix wraps it.
			("a.{..}", "R(a)"),
			("a.{..}(.b).c", "R(a; (_.b)).c"),
			("a.{..}.b.c[0]", "R(a; _.b.c[i])"),
			("a.{..}.b.@", "R(a; _.b.@)"),
			("a.{..}.b.@.c[0]", "B(R(a; _.b.@); _.c[i])"),
			("a.{..}->knows->person", "R(a; _->knows->person)"),
			("a.b.{..}(->knows)", "B(a; R(_.b; (_->knows)))"),
			// The row's record id.
			("@.a", "@.a"),
		];
		for (sql, want) in cases {
			assert_eq!(shape_of(&expr(sql)), want, "shape of `{sql}`");
		}
	}

	/// A body written as an idiom starts from the value it is applied to,
	/// even when its first part is a field, and a starting expression replaces
	/// that value.
	#[test]
	fn bodies_are_paths_over_the_current_value() {
		let LogicalExpr::Destructure {
			fields,
			..
		} = expr("a.{ b: c.d[1], e: $this.f, g: h * 2 }")
		else {
			panic!("expected a destructure");
		};
		let bodies: Vec<String> = fields
			.iter()
			.map(|field| match field {
				LogicalDestructureField::Aliased(_, body) => shape_of(body),
				other @ (LogicalDestructureField::All(_)
				| LogicalDestructureField::Field(_)
				| LogicalDestructureField::Destructure(..)
				| LogicalDestructureField::Rest) => {
					panic!("expected an alias, got {other:?}")
				}
			})
			.collect();
		// No broadcast inside a body: `c.d[1]` nests plainly.
		assert_eq!(bodies, ["_.c.d[i]", "$this.f", "h * 2"]);
	}

	#[test]
	fn a_block_is_a_sequence_as_a_statement_and_a_subquery_as_a_scalar() {
		let statement = lower_sql("{ LET $a = 1; RETURN $a + 1; }");
		assert_eq!(statement.name(), "Sequence");
		assert_eq!(
			statement.children().iter().map(LogicalOp::name).collect::<Vec<_>>(),
			["Let", "Return"]
		);
		let scalar = expr("1 + { LET $a = 1; RETURN $a + 1; }");
		let LogicalExpr::Binary {
			right,
			..
		} = scalar
		else {
			panic!("expected a binary expression");
		};
		let LogicalExpr::Subquery(nested) = *right else {
			panic!("expected a subquery");
		};
		assert_eq!(nested.name(), "Sequence");
	}

	#[test]
	fn control_flow_and_if_have_one_encoding_per_position() {
		// A top-level IF is the IF operator; one nested in an expression is
		// the scalar IF.
		assert!(matches!(lower_sql("IF true THEN 1 ELSE 2 END"), LogicalOp::If(_)));
		assert!(
			matches!(expr("[IF true THEN 1 END]"), LogicalExpr::Array(items) if matches!(items[0], LogicalExpr::If(_)))
		);
		assert!(matches!(lower_sql("BREAK"), LogicalOp::Break(_)));
		assert!(matches!(lower_sql("THROW 'x'"), LogicalOp::Throw(_)));
	}

	#[test]
	fn a_statement_in_scalar_position_is_a_subquery() {
		for sql in ["SELECT * FROM t", "RETURN 1", "LET $x = 1", "INFO FOR DB", "SLEEP 1ms"] {
			let lowered = expr(&format!("[{sql}]"));
			let LogicalExpr::Array(items) = lowered else {
				panic!("expected an array");
			};
			assert!(matches!(items[0], LogicalExpr::Subquery(_)), "`{sql}` should be a subquery");
		}
	}

	/// How lowering classifies a statement of each surface variant. Exhaustive
	/// on purpose: a variant added to the surface AST fails to compile here
	/// until it is classified, and the corpus below has one statement per
	/// variant so the classification is exercised rather than declared.
	///
	/// Total, because lowering is: every variant the parser produces has a
	/// logical form. A variant that does not would return a `Result` here
	/// again.
	fn expected(expr: &sql::Expr) -> &'static str {
		match expr {
			sql::Expr::Literal(_)
			| sql::Expr::Param(_)
			| sql::Expr::Idiom(_)
			| sql::Expr::Table(_)
			| sql::Expr::Constant(_)
			| sql::Expr::Prefix {
				..
			}
			| sql::Expr::Postfix {
				..
			}
			| sql::Expr::Binary {
				..
			}
			| sql::Expr::FunctionCall(_)
			| sql::Expr::Closure(_)
			| sql::Expr::Mock(_) => "Eval",
			sql::Expr::Break => "Break",
			sql::Expr::Continue => "Continue",
			sql::Expr::Throw(_) => "Throw",
			sql::Expr::IfElse(_) => "If",
			sql::Expr::Block(_) => "Sequence",
			sql::Expr::Return(_) => "Return",
			sql::Expr::Select(_) => "Project",
			sql::Expr::Info(_) => "Info",
			sql::Expr::Foreach(_) => "Foreach",
			sql::Expr::Let(_) => "Let",
			sql::Expr::Sleep(_) => "Sleep",
			sql::Expr::Explain {
				..
			} => "Explain",
			sql::Expr::Create(_)
			| sql::Expr::Update(_)
			| sql::Expr::Delete(_)
			| sql::Expr::Relate(_)
			| sql::Expr::Insert(_)
			| sql::Expr::Upsert(_) => "Write",
			sql::Expr::Define(_)
			| sql::Expr::Remove(_)
			| sql::Expr::Rebuild(_)
			| sql::Expr::Alter(_) => "Ddl",
		}
	}

	fn variant_name(expr: &sql::Expr) -> &'static str {
		match expr {
			sql::Expr::Literal(_) => "Literal",
			sql::Expr::Param(_) => "Param",
			sql::Expr::Idiom(_) => "Idiom",
			sql::Expr::Table(_) => "Table",
			sql::Expr::Mock(_) => "Mock",
			sql::Expr::Block(_) => "Block",
			sql::Expr::Constant(_) => "Constant",
			sql::Expr::Prefix {
				..
			} => "Prefix",
			sql::Expr::Postfix {
				..
			} => "Postfix",
			sql::Expr::Binary {
				..
			} => "Binary",
			sql::Expr::FunctionCall(_) => "FunctionCall",
			sql::Expr::Closure(_) => "Closure",
			sql::Expr::Break => "Break",
			sql::Expr::Continue => "Continue",
			sql::Expr::Throw(_) => "Throw",
			sql::Expr::Return(_) => "Return",
			sql::Expr::IfElse(_) => "IfElse",
			sql::Expr::Select(_) => "Select",
			sql::Expr::Create(_) => "Create",
			sql::Expr::Update(_) => "Update",
			sql::Expr::Delete(_) => "Delete",
			sql::Expr::Relate(_) => "Relate",
			sql::Expr::Insert(_) => "Insert",
			sql::Expr::Define(_) => "Define",
			sql::Expr::Remove(_) => "Remove",
			sql::Expr::Rebuild(_) => "Rebuild",
			sql::Expr::Upsert(_) => "Upsert",
			sql::Expr::Alter(_) => "Alter",
			sql::Expr::Info(_) => "Info",
			sql::Expr::Foreach(_) => "Foreach",
			sql::Expr::Let(_) => "Let",
			sql::Expr::Sleep(_) => "Sleep",
			sql::Expr::Explain {
				..
			} => "Explain",
		}
	}

	/// One statement per surface variant. A bare table name only parses as
	/// one inside `FROM`, so that variant is built directly.
	fn corpus() -> Vec<sql::Expr> {
		let mut out: Vec<sql::Expr> = CORPUS.iter().map(|sql| parse(sql)).collect();
		out.push(sql::Expr::Table("t".into()));
		out
	}

	const CORPUS: &[&str] = &[
		"1",
		"$p",
		"a.b",
		"|t:3|",
		"{ 1; 2 }",
		"math::PI",
		"!true",
		"1..",
		"1 + 2",
		"count()",
		"|| 1",
		"BREAK",
		"CONTINUE",
		"THROW 'x'",
		"RETURN 1",
		"IF true THEN 1 ELSE 2 END",
		"SELECT * FROM t",
		"CREATE t",
		"UPDATE t SET a = 1",
		"DELETE t",
		"RELATE a:1->r->b:1",
		"INSERT INTO t { a: 1 }",
		"DEFINE TABLE t",
		"REMOVE TABLE t",
		"REBUILD INDEX i ON t",
		"UPSERT t",
		"ALTER TABLE t SCHEMAFULL",
		"INFO FOR DB",
		"FOR $x IN [1] { }",
		"LET $x = 1",
		"SLEEP 1ms",
		"EXPLAIN SELECT * FROM t",
	];

	#[test]
	fn lowering_classifies_every_surface_variant() {
		let mut seen = BTreeSet::new();
		for statement in corpus() {
			let sql = statement.to_sql();
			seen.insert(variant_name(&statement));
			let want = expected(&statement);
			let got = lower(statement).expect("every surface variant lowers");
			assert_eq!(got.name(), want, "`{sql}`");
		}
		// The corpus must exercise every variant `expected` classifies.
		let all: BTreeSet<_> = [
			"Literal",
			"Param",
			"Idiom",
			"Table",
			"Mock",
			"Block",
			"Constant",
			"Prefix",
			"Postfix",
			"Binary",
			"FunctionCall",
			"Closure",
			"Break",
			"Continue",
			"Throw",
			"Return",
			"IfElse",
			"Select",
			"Create",
			"Update",
			"Delete",
			"Relate",
			"Insert",
			"Define",
			"Remove",
			"Rebuild",
			"Upsert",
			"Alter",
			"Info",
			"Foreach",
			"Let",
			"Sleep",
			"Explain",
		]
		.into_iter()
		.collect();
		assert_eq!(seen, all, "the corpus must cover every surface variant");
	}

	/// A schema statement in scalar position lowers with the statement that
	/// holds it, as a mutation does, so the block around it keeps its logical
	/// form.
	#[test]
	fn a_ddl_nested_in_a_scalar_keeps_the_statements_logical_form() {
		let statement = parse("RETURN { DEFINE TABLE x; 1 }");
		let lowered = lower(statement).expect("a block holding a schema statement lowers");
		assert!(
			lowered.any(|node| matches!(node, LogicalOp::Ddl(_))),
			"the block should carry the schema statement"
		);
	}

	/// A mock record set lowers wherever it sits, not only in `FROM`. A
	/// decline refuses the whole statement, so a mock that declined outside
	/// `FROM` would fail a query with one `|t:n|` in a predicate.
	#[test]
	fn a_mock_lowers_outside_from() {
		for sql in [
			"SELECT * FROM t WHERE a IN |u:3|",
			"CREATE |u:3|",
			"UPSERT |u:1..4|",
			"LET $x = |u:3|",
			"RETURN [|u:3|]",
		] {
			lower(parse(sql)).unwrap_or_else(|err| panic!("`{sql}` lowers: {err}"));
		}
	}

	/// A schema statement is not read-only, wherever it sits. The node owns no
	/// scalars, so a classification that reached for them would answer `true`
	/// over an empty set and put a catalog write on a read-only transaction.
	#[test]
	fn a_ddl_plan_is_not_read_only() {
		for sql in ["DEFINE TABLE x", "REMOVE TABLE x", "ALTER TABLE x", "REBUILD INDEX i ON x"] {
			let lowered = lower(parse(sql)).expect("a schema statement lowers");
			assert!(!lowered.read_only(), "`{sql}` writes the catalog");
		}
		// And through the statement that holds it, which reads the answer off
		// the nested plan.
		let lowered = lower(parse("RETURN { DEFINE TABLE x; 1 }")).expect("the block lowers");
		assert!(!lowered.read_only(), "a block holding a schema statement writes");
	}

	/// The node carries the statement and hands out no expressions: its
	/// clauses are resolved where the catalog write is formed, not by a pass
	/// over the plan.
	#[test]
	fn a_ddl_node_owns_no_scalars_and_no_inputs() {
		let lowered = lower(parse("DEFINE INDEX i ON t FIELDS a, b")).expect("lowers");
		assert!(matches!(lowered, LogicalOp::Ddl(_)));
		assert!(lowered.scalars().is_empty());
		assert!(lowered.children().is_empty());
		assert_eq!(lowered.name(), "Ddl");
	}

	/// A mutation in scalar position lowers with the statement that holds it,
	/// rather than declining it. The plan it produces still writes, so the
	/// whole statement is a writing plan and the planner is what decides
	/// whether it can compile one.
	#[test]
	fn a_lowered_write_nested_in_a_scalar_keeps_the_statement_writing() {
		let op = lower(parse("SELECT * FROM t WHERE a IN (CREATE u).id"))
			.expect("a nested CREATE should lower with its statement");
		assert!(!op.read_only(), "a statement containing a mutation must not claim read-only");

		// The control: the same statement without the mutation. Without it
		// the assertion above would also hold for a plan that had simply lost
		// the nested statement, which is the failure worth excluding.
		let control = lower(parse("SELECT * FROM t WHERE a IN u.id")).expect("should lower");
		assert!(control.read_only(), "the control must be read-only for the pair to mean anything");
	}

	/// The [`LogicalWrite`] a mutation lowers to.
	fn write_of(sql: &str) -> LogicalWrite {
		let op = lower_sql(sql);
		let name = op.name();
		let LogicalOp::Write(write) = op else {
			panic!("`{sql}` should lower to a write, got {name}");
		};
		write
	}

	fn targets_of(sql: &str) -> Vec<LogicalExpr> {
		match write_of(sql).source {
			LogicalWriteSource::Targets(targets) => targets,
			other @ (LogicalWriteSource::Inserts(_) | LogicalWriteSource::Rows(_)) => panic!(
				"`{sql}` writes subjects that need not exist, so it must not read them: {other:?}"
			),
		}
	}

	fn inserts_of(sql: &str) -> LogicalInserts {
		match write_of(sql).source {
			LogicalWriteSource::Inserts(inserts) => inserts,
			other @ (LogicalWriteSource::Targets(_) | LogicalWriteSource::Rows(_)) => {
				panic!("`{sql}` writes rows of its own: {other:?}")
			}
		}
	}

	fn rows_of(sql: &str) -> LogicalOp {
		match write_of(sql).source {
			LogicalWriteSource::Rows(rows) => *rows,
			other @ (LogicalWriteSource::Targets(_) | LogicalWriteSource::Inserts(_)) => {
				panic!("`{sql}` writes subjects that already exist, so it reads them: {other:?}")
			}
		}
	}

	/// Every mutation form has a logical form, and the node names the kind it
	/// performs.
	#[test]
	fn every_mutation_form_lowers_to_a_write() {
		for (sql, kind) in [
			("CREATE person SET a = 1", "CREATE"),
			("UPDATE person SET a = 1", "UPDATE"),
			("UPSERT person SET a = 1", "UPSERT"),
			("DELETE person", "DELETE"),
			("INSERT INTO person { a: 1 }", "INSERT"),
			("RELATE person:a->likes->person:b", "RELATE"),
		] {
			assert_eq!(write_of(sql).statement.kind(), kind, "`{sql}`");
		}
	}

	/// Which source arm a form takes is a correctness property, not a
	/// preference. A reading source resolves a record id to its stored
	/// document and yields nothing when there is none, which for a subject
	/// being created is always, so a creating form given one would have its
	/// write silently dropped. An updating form's subjects exist and are read.
	#[test]
	fn a_creating_form_names_targets_and_an_updating_form_reads_rows() {
		for sql in ["CREATE person", "UPSERT person", "RELATE person:a->likes->person:b"] {
			let _ = targets_of(sql);
			let op = lower_sql(sql);
			assert!(op.children().is_empty(), "`{sql}` has no plan subtree to read");
		}
		// `INSERT` names its subjects a third way — as the rows themselves —
		// and reads no more than the other two do.
		let _ = inserts_of("INSERT INTO person { a: 1 }");
		assert!(
			lower_sql("INSERT INTO person { a: 1 }").children().is_empty(),
			"an INSERT has no plan subtree to read"
		);
		for sql in ["UPDATE person SET a = 1", "DELETE person"] {
			assert_eq!(shape(&rows_of(sql)), ["Source"], "`{sql}`");
		}
	}

	/// `ONLY` is read from the statement. `INSERT` has no such keyword in the
	/// grammar, so it never asks for a single record.
	#[test]
	fn only_is_read_from_the_statement_and_insert_has_none() {
		for sql in [
			"CREATE ONLY person",
			"UPDATE ONLY person:1 SET a = 1",
			"UPSERT ONLY person:1",
			"DELETE ONLY person:1",
			"RELATE ONLY person:a->likes->person:b",
		] {
			assert!(write_of(sql).statement.only(), "`{sql}`");
		}
		assert!(!write_of("DELETE person:1").statement.only());
		assert!(!write_of("INSERT INTO person { a: 1 }").statement.only());
	}

	/// An updating form's targets lower to the subtree the same targets take
	/// in a `FROM` clause, index hint included, rather than to a second
	/// lowering of their own.
	#[test]
	fn an_updating_forms_targets_lower_as_a_from_clause_does() {
		let rows = rows_of("UPDATE a, b:1 WITH NOINDEX SET x = 1");
		assert_eq!(rows.name(), "UnionAll");
		let leaves: Vec<_> = rows.children().iter().map(LogicalOp::name).collect();
		assert_eq!(leaves, ["Source", "Source"]);
		for leaf in rows.children() {
			let LogicalOp::Source(LogicalSource {
				with,
				for_update,
				..
			}) = leaf
			else {
				panic!("expected a source");
			};
			assert!(with.is_some(), "the hint reaches each stored source");
			assert!(!for_update, "the mutation's own write takes the lock");
		}
	}

	/// The per-record table permission gate runs ahead of the statement's
	/// condition on the updating shapes, so that a condition with side effects
	/// runs only for records the caller may write
	/// (`doc/adr/0001-streaming-write-path.md`). The row subtree runs beneath
	/// the write operator and so ahead of every one of its stages, which is
	/// why the condition must not be lowered into it; it stays a clause of the
	/// statement, for the write operator's condition stage to evaluate.
	#[test]
	fn an_updating_forms_condition_stays_out_of_its_row_subtree() {
		for sql in ["UPDATE person SET a = 1 WHERE b > 1", "DELETE person WHERE b > 1"] {
			let rows = rows_of(sql);
			assert!(
				!rows.any(|node| matches!(node, LogicalOp::Filter(_))),
				"`{sql}` must not evaluate its condition below the write: {rows}"
			);
			assert!(
				!rows.to_string().contains("b > 1"),
				"`{sql}` must not push its condition into the scan: {rows}"
			);
		}
		// The condition is kept, not dropped: the write operator's stage reads
		// it from the statement.
		let LogicalWriteStatement::Update(stmt) =
			write_of("UPDATE person SET a = 1 WHERE b > 1").statement
		else {
			panic!("expected an UPDATE");
		};
		assert!(stmt.cond.is_some());
		let LogicalWriteStatement::Delete(stmt) = write_of("DELETE person WHERE b > 1").statement
		else {
			panic!("expected a DELETE");
		};
		assert!(stmt.cond.is_some());
		// The control: the same target and condition in a `FROM` clause do
		// build the filter, so the assertions above are about where a
		// mutation's condition goes rather than about lowering being unable to
		// place one.
		assert_eq!(
			shape(&lower_sql("SELECT * FROM person WHERE b > 1")),
			["Project", "Filter", "Source"]
		);
	}

	/// An `UPSERT` subject may or may not exist, and the write operator
	/// reconciles the two cases by retrying a conflicting create as an update.
	/// A condition does not change that: a reading source would still drop the
	/// creating case.
	#[test]
	fn upsert_names_targets_whether_or_not_it_has_a_condition() {
		assert_eq!(targets_of("UPSERT person:1 SET a = 1").len(), 1);
		assert_eq!(targets_of("UPSERT person:1 SET a = 1 WHERE b > 1").len(), 1);
		assert_eq!(targets_of("UPSERT a, b:1").len(), 2, "one target per thing written");
	}

	/// `INSERT`'s rows are its subjects: they reach the write operator as its
	/// source, so the operator has a record to write for each of them. The
	/// `INTO` table rides beside them and is absent where the statement names
	/// none, in which case each row names its table through the id it carries.
	#[test]
	fn insert_carries_its_rows_and_its_into_table_as_its_source() {
		let single = inserts_of("INSERT INTO person { a: 1 }");
		assert!(single.into.is_some());
		assert!(matches!(single.rows, LogicalInsertRows::Value(_)));

		let bare = inserts_of("INSERT { id: person:1, a: 1 }");
		assert!(
			bare.into.is_none(),
			"without INTO each record names its table through the id it carries"
		);

		// A `VALUES` tuple keeps one `(place, expression)` pair per column, in
		// the order written: the pairs build the row, so losing their order or
		// their paths loses what the row holds.
		let LogicalInsertRows::Tuples(tuples) =
			inserts_of("INSERT INTO person (a, b) VALUES (1, 2), (3, 4)").rows
		else {
			panic!("a VALUES list must lower to tuples");
		};
		assert_eq!(tuples.len(), 2);
		let rendered: Vec<_> =
			tuples[0].iter().map(|(path, value)| (path.to_sql(), value.to_sql())).collect();
		assert_eq!(rendered, [("a".to_string(), "1".to_string()), ("b".into(), "2".into())]);
	}

	/// The `INTO` table and every row expression are the node's own scalars,
	/// so a pass that rewrites expressions reaches them — the same property
	/// the target list has, and what makes a parameter in a row resolvable.
	#[test]
	fn an_inserts_rows_are_the_nodes_own_scalars() {
		let single = lower_sql("INSERT INTO person { a: $x }");
		assert_eq!(single.scalars().len(), 2, "the INTO table and the one row expression");
		assert!(single.children().is_empty(), "rows are not a plan subtree");

		let tuples = lower_sql("INSERT INTO person (a, b) VALUES (1, 2), (3, 4)");
		assert_eq!(tuples.scalars().len(), 5, "the INTO table and four values");

		let bare = lower_sql("INSERT { id: person:1 }");
		assert_eq!(bare.scalars().len(), 1, "no INTO table, one row expression");
	}

	/// A data clause that names no rows is not `INSERT`'s source, and lowering
	/// declines rather than writing records the statement never described.
	#[test]
	fn an_insert_whose_data_names_no_rows_has_no_logical_form() {
		let stmt = sql::Expr::Insert(Box::new(sql::statements::InsertStatement {
			into: Some(sql::Expr::Table("person".into())),
			data: sql::Data::ContentExpression(sql::Expr::Literal(sql::Literal::Integer(1))),
			ignore: false,
			update: None,
			output: None,
			timeout: sql::Expr::Literal(sql::Literal::None),
			relation: false,
		}));
		assert!(matches!(lower(stmt), Err(NoLogicalForm::Unsupported(_))));
	}

	/// `RELATE`'s targets are `[from, through, to]`, the order
	/// [`LogicalWriteSource::Targets`] documents for this form. None of the
	/// three is read: the endpoints are used as the ids they evaluate to, and
	/// the edge record does not exist yet.
	#[test]
	fn relate_names_its_endpoints_and_the_table_between_them() {
		let rendered: Vec<_> =
			targets_of("RELATE person:a->likes->person:b").iter().map(ToSql::to_sql).collect();
		assert_eq!(rendered, ["person:a", "likes", "person:b"]);
		// They are the node's own scalars, so a pass that rewrites expressions
		// reaches all three.
		let op = lower_sql("RELATE person:a->likes->person:b");
		assert_eq!(op.scalars().len(), 3);
	}

	#[test]
	fn an_explain_statement_carries_its_format() {
		let LogicalOp::Explain(LogicalExplain {
			analyze,
			format,
			input,
		}) = lower_sql("EXPLAIN ANALYZE FORMAT JSON SELECT * FROM t")
		else {
			panic!("expected an Explain");
		};
		assert!(analyze);
		assert_eq!(format, crate::expr::ExplainFormat::Json);
		assert_eq!(input.name(), "Project");
		// The SELECT clause form answers with the structured plan.
		let LogicalOp::Explain(LogicalExplain {
			format,
			analyze,
			..
		}) = lower_sql("SELECT * FROM t EXPLAIN FULL")
		else {
			panic!("expected an Explain");
		};
		assert!(analyze);
		assert_eq!(format, crate::expr::ExplainFormat::Json);
	}

	#[test]
	fn a_geojson_object_lowers_to_a_geometry() {
		assert!(matches!(
			expr("{ type: 'Point', coordinates: [1.0, 2.0] }"),
			LogicalExpr::Literal(LogicalLiteral::Geometry(_))
		));
		assert!(matches!(expr("{ type: 'Point', other: 1 }"), LogicalExpr::Object(_)));
	}
}