spg-engine 7.37.22

Execution engine for SPG: glues spg-sql parsing to spg-storage. Foreign keys, joins, vectors, cold tier.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
//! Operator evaluation split out of `eval.rs` (cut 33): the pure
//! value-level unary / binary operator machinery that `eval_expr` (and
//! the compiled-expression stepper) dispatch into. Covers `apply_unary`,
//! `apply_binary` and its arithmetic / comparison / 3VL-logic / vector /
//! interval-calendar sub-evaluators (apply_binary_numeric,
//! apply_binary_calendar, apply_binary_interval, compare, and_3vl /
//! or_3vl, the pgvector distance ops, bit / shift arith, numeric rescale).
//! These are business-agnostic value math — no `EvalContext` or `Row`
//! entanglement. The calendar primitives they lean on (`civil_from_days`,
//! `add_months_to_civil`, `days_from_civil`) and the central
//! `value_to_text` renderer stay in `eval.rs` and are reached via
//! `use super::`.

use alloc::format;
use alloc::string::ToString;
use alloc::vec::Vec;

use spg_sql::ast::{BinOp, UnOp};
use spg_storage::{DataType, Value};

use super::{
    EvalError, add_months_to_civil, civil_from_days, days_from_civil, inet_op_bool_result,
    parse_date_literal, parse_timestamp_literal, ts_match, tsvector_concat, value_to_text,
};

/// v7.39 (round 508) — `?#`: do the two shapes meet?
///
/// PG defines it over box/box, line/box, line/line, lseg/box, lseg/line,
/// lseg/lseg and path/path. Every one reduces to the same two questions —
/// do two segments cross, and do two axis-aligned extents overlap — so the
/// shapes are reduced to segments (a box to its four sides, a path to its
/// edges) and tested pairwise.
fn geom_intersects(l: &Value<'_>, r: &Value<'_>) -> Result<Value<'static>, EvalError> {
    use spg_storage::Point2D;
    const EPS: f64 = 1.0e-6;

    fn cross(o: Point2D, a: Point2D, b: Point2D) -> f64 {
        (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
    }
    fn on_span(p: Point2D, a: Point2D, b: Point2D) -> bool {
        p.x >= a.x.min(b.x) - EPS
            && p.x <= a.x.max(b.x) + EPS
            && p.y >= a.y.min(b.y) - EPS
            && p.y <= a.y.max(b.y) + EPS
    }
    // Two closed segments meet when their endpoints straddle each other, or
    // when they are collinear and their spans overlap.
    fn segs_meet(p1: Point2D, p2: Point2D, q1: Point2D, q2: Point2D) -> bool {
        let (d1, d2) = (cross(q1, q2, p1), cross(q1, q2, p2));
        let (d3, d4) = (cross(p1, p2, q1), cross(p1, p2, q2));
        if ((d1 > EPS && d2 < -EPS) || (d1 < -EPS && d2 > EPS))
            && ((d3 > EPS && d4 < -EPS) || (d3 < -EPS && d4 > EPS))
        {
            return true;
        }
        (d1.abs() <= EPS && on_span(p1, q1, q2))
            || (d2.abs() <= EPS && on_span(p2, q1, q2))
            || (d3.abs() <= EPS && on_span(q1, p1, p2))
            || (d4.abs() <= EPS && on_span(q2, p1, p2))
    }

    // Every shape as the segments that bound it. A LINE is infinite, so it
    // is represented by a very long segment through its two known points —
    // enough to reach past any finite operand it is compared with.
    fn segments(v: &Value<'_>) -> Option<alloc::vec::Vec<(Point2D, Point2D)>> {
        match v {
            Value::Lseg(a, b) => Some(alloc::vec![(*a, *b)]),
            Value::PgBox(a, b) => {
                let (lo_x, hi_x) = (a.x.min(b.x), a.x.max(b.x));
                let (lo_y, hi_y) = (a.y.min(b.y), a.y.max(b.y));
                let c = |x, y| Point2D { x, y };
                Some(alloc::vec![
                    (c(lo_x, lo_y), c(hi_x, lo_y)),
                    (c(hi_x, lo_y), c(hi_x, hi_y)),
                    (c(hi_x, hi_y), c(lo_x, hi_y)),
                    (c(lo_x, hi_y), c(lo_x, lo_y)),
                ])
            }
            Value::Path { points, closed } => {
                if points.len() < 2 {
                    return Some(alloc::vec![]);
                }
                let mut out: alloc::vec::Vec<(Point2D, Point2D)> =
                    points.windows(2).map(|w| (w[0], w[1])).collect();
                if *closed {
                    out.push((points[points.len() - 1], points[0]));
                }
                Some(out)
            }
            // `Ax + By + C = 0`, sampled far enough out to behave as the
            // infinite line it is against any finite operand.
            Value::Line { a, b, c } => {
                const FAR: f64 = 1.0e9;
                if b.abs() > EPS {
                    let y = |x: f64| (-c - a * x) / b;
                    Some(alloc::vec![(
                        Point2D {
                            x: -FAR,
                            y: y(-FAR)
                        },
                        Point2D { x: FAR, y: y(FAR) },
                    )])
                } else if a.abs() > EPS {
                    let x = -c / a;
                    Some(alloc::vec![
                        (Point2D { x, y: -FAR }, Point2D { x, y: FAR },)
                    ])
                } else {
                    Some(alloc::vec![])
                }
            }
            _ => None,
        }
    }

    match (segments(l), segments(r)) {
        (Some(ls), Some(rs)) => {
            Ok(Value::Bool(ls.iter().any(|(p1, p2)| {
                rs.iter().any(|(q1, q2)| segs_meet(*p1, *p2, *q1, *q2))
            })))
        }
        _ => Err(EvalError::TypeMismatch {
            detail: alloc::format!(
                "operator ?# not supported for {:?} and {:?}",
                l.data_type(),
                r.data_type()
            ),
        }),
    }
}

pub(super) fn apply_unary(op: UnOp, v: Value<'static>) -> Result<Value<'static>, EvalError> {
    match (op, v) {
        (_, Value::Null) => Ok(Value::Null),
        (UnOp::Neg, Value::Int(n)) => {
            n.checked_neg()
                .map(Value::Int)
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "integer overflow on unary -".into(),
                })
        }
        (UnOp::Neg, Value::BigInt(n)) => {
            n.checked_neg()
                .map(Value::BigInt)
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "bigint out of range".into(),
                })
        }
        (UnOp::Neg, Value::Float(x)) => Ok(Value::Float(-x)),
        // v7.39 (read01 round 107) — unary minus on REAL (f32) was missing, so
        // `-3.5::real` errored "unary - applied to Real".
        (UnOp::Neg, Value::Real(x)) => Ok(Value::Real(-x)),
        (UnOp::Neg, Value::SmallInt(n)) => {
            n.checked_neg()
                .map(Value::SmallInt)
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "smallint overflow on unary -".into(),
                })
        }
        (
            UnOp::Neg,
            Value::Numeric {
                scaled,
                scale,
                kind,
            },
        ) => {
            use spg_storage::NumericKind as NK;
            match kind {
                NK::Finite => scaled
                    .checked_neg()
                    .map(|s| Value::Numeric {
                        scaled: s,
                        scale,
                        kind: NK::Finite,
                    })
                    .ok_or_else(|| EvalError::TypeMismatch {
                        detail: "numeric overflow on unary -".into(),
                    }),
                // v7.38 (read01, T6.P3) — -(NaN)=NaN, -(±Inf)=∓Inf.
                NK::NaN => Ok(Value::numeric_special(NK::NaN)),
                NK::PosInf => Ok(Value::numeric_special(NK::NegInf)),
                NK::NegInf => Ok(Value::numeric_special(NK::PosInf)),
            }
        }
        // NOTE: PG has no `- money` operator (unary minus on money is an
        // error there), so money is intentionally NOT handled here.
        (
            UnOp::Neg,
            Value::Interval {
                months,
                days,
                micros,
            },
        ) => {
            let overflow = || EvalError::TypeMismatch {
                detail: "INTERVAL overflows on unary -".into(),
            };
            Ok(Value::Interval {
                months: months.checked_neg().ok_or_else(overflow)?,
                days: days.checked_neg().ok_or_else(overflow)?,
                micros: micros.checked_neg().ok_or_else(overflow)?,
            })
        }
        // v7.39 (round 238) — PG's wording: "operator does not exist: - text".
        (UnOp::Neg, other) => Err(EvalError::TypeMismatch {
            detail: format!(
                "operator does not exist: - {}",
                super::strings::pg_typeof_name(&other)
            ),
        }),
        // v7.39 (round 507) — unary `+` is the identity on a number and
        // KEEPS its type: measured on PG18, `+1` is integer, `+1.5` is
        // numeric, `+'2'::bigint` is bigint. It is not a no-op the parser
        // could have dropped, because PG refuses every other operand —
        // "operator does not exist: + boolean", and the same for text and
        // interval, which is why interval is absent here even though unary
        // MINUS accepts one.
        (
            UnOp::Plus,
            v @ (Value::SmallInt(_)
            | Value::Int(_)
            | Value::BigInt(_)
            | Value::Real(_)
            | Value::Float(_)
            | Value::Numeric { .. }),
        ) => Ok(v),
        (UnOp::Plus, other) => Err(EvalError::TypeMismatch {
            detail: format!(
                "operator does not exist: + {}",
                super::strings::pg_typeof_name(&other)
            ),
        }),
        // v7.38 (read01) — PG `~ int2` is int2, not int4.
        (UnOp::BitNot, Value::SmallInt(n)) => Ok(Value::SmallInt(!n)),
        (UnOp::BitNot, Value::Int(n)) => Ok(Value::Int(!n)),
        (UnOp::BitNot, Value::BigInt(n)) => Ok(Value::BigInt(!n)),
        // v7.38 (read01) — PG `~ inet` flips every host-address bit, keeping the
        // operand's netmask (`~ 255.0.0.0/8` → `0.255.255.255/8`).
        (UnOp::BitNot, Value::Inet { family, bits, addr }) => {
            let mask = if family == 4 {
                u128::from(u32::MAX)
            } else {
                u128::MAX
            };
            let flipped = !inet_addr_u128(family, &addr) & mask;
            Ok(inet_from_u128(family, bits, flipped))
        }
        (UnOp::BitNot, Value::BitString { nbits, bytes }) => {
            // PG `~ bit(n)`: flip every bit, then re-zero the padding bits
            // past `nbits` so the value stays canonical (MSB-packed).
            let mut out: Vec<u8> = bytes.iter().map(|b| !b).collect();
            let used = nbits as usize;
            let full = used / 8;
            let rem = used % 8;
            if rem != 0 {
                if let Some(byte) = out.get_mut(full) {
                    *byte &= 0xffu8 << (8 - rem);
                }
            }
            Ok(Value::BitString {
                nbits,
                bytes: alloc::borrow::Cow::Owned(out),
            })
        }
        // PG `~ macaddr`: complement all six octets.
        (UnOp::BitNot, Value::Macaddr(a)) => {
            let mut out = [0u8; 6];
            for i in 0..6 {
                out[i] = !a[i];
            }
            Ok(Value::Macaddr(out))
        }
        // PG `~ macaddr8`: complement all eight EUI-64 octets.
        (UnOp::BitNot, Value::Macaddr8(a)) => {
            let mut out = [0u8; 8];
            for i in 0..8 {
                out[i] = !a[i];
            }
            Ok(Value::Macaddr8(out))
        }
        (UnOp::BitNot, other) => Err(EvalError::TypeMismatch {
            detail: format!("cannot apply ~ to {other:?}"),
        }),
        (UnOp::Not, Value::Bool(b)) => Ok(Value::Bool(!b)),
        // v7.39 (round 346, M1) — PG names the type it wanted, in its own
        // words; SPG printed `NOT applied to Some(Int)`, which is a Rust
        // Debug of an internal shape. MariaDB negates any truth value
        // (`NOT 5` is 0), and that reading is applied by the caller, which
        // is where the dialect is known.
        (UnOp::Not, other) => Err(EvalError::TypeMismatch {
            detail: alloc::format!(
                "argument of NOT must be type boolean, not type {}",
                super::strings::pg_typeof_name(&other)
            ),
        }),
    }
}

/// v7.9.27b — true when two values are "not distinct" per PG:
/// both NULL counts as equal; otherwise reduces to regular Eq.
fn values_not_distinct(l: &Value<'_>, r: &Value<'_>) -> bool {
    match (l, r) {
        (Value::Null, Value::Null) => true,
        (Value::Null, _) | (_, Value::Null) => false,
        // v7.38 (read01) — "not distinct" is the type's `=` semantics, not a
        // representation-exact match, so `1 IS NOT DISTINCT FROM 1.0` is true
        // (int and numeric compare equal) like PG. Fall back to structural
        // equality only for types `compare` cannot order.
        _ => match compare(BinOp::Eq, l, r) {
            Ok(Value::Bool(b)) => b,
            _ => l == r,
        },
    }
}

/// v7.37.9 T3 S4 — by-reference comparison/3VL fast path.
///
/// Lifts the operand contract from owned `Value<'static>` to borrowed
/// `&Value<'_>` for the **read-only** binary ops where the result is a
/// fresh `Value::Bool` / `Value::Null` and the operand bytes are never
/// stored in the result. Eliminates the per-pop `.into_owned()` that
/// the owning `apply_binary` requires (which String::clones every
/// Text/Bytes/Json/Vector value off the Step VM stack).
///
/// Returns `None` for ops that build a new owned result (Add / Sub /
/// Concat / Json* / arithmetic etc.) — the caller falls through to the
/// owning `apply_binary` path. This keeps the by-ref change a pure
/// optimization with no behaviour change for non-comparison ops.
pub(crate) fn apply_binary_by_ref(
    op: BinOp,
    l: &Value<'_>,
    r: &Value<'_>,
) -> Result<Option<Value<'static>>, EvalError> {
    // 3VL And/Or/IS [NOT] DISTINCT FROM — NULL handling without
    // moving operands. These mirror the owning path's pre-checks.
    if let BinOp::IsNotDistinctFrom = op {
        require_comparable(BinOp::Eq, l, r)?;
        return Ok(Some(Value::Bool(values_not_distinct(l, r))));
    }
    if let BinOp::IsDistinctFrom = op {
        require_comparable(BinOp::Eq, l, r)?;
        return Ok(Some(Value::Bool(!values_not_distinct(l, r))));
    }
    if let BinOp::And = op {
        require_boolean_argument("AND", l, r)?;
        return Ok(Some(and_3vl_by_ref(l, r)));
    }
    if let BinOp::Or = op {
        require_boolean_argument("OR", l, r)?;
        return Ok(Some(or_3vl_by_ref(l, r)));
    }
    // Any NULL operand → NULL for the remaining ops.
    if l.is_null() || r.is_null() {
        match op {
            BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq => {
                return Ok(Some(Value::Null));
            }
            _ => return Ok(None),
        }
    }
    // v7.39 (round 614) — integer arithmetic answered here rather than by
    // falling through to the owning path.
    //
    // `apply_binary_by_ref` used to decline every arithmetic operator, so
    // `id + 1` popped both operands owned and then walked `apply_binary`'s
    // whole guard chain — inet, pg_lsn, range, multirange, calendar, the
    // unknown-literal coercion — none of which can match two integers,
    // before reaching the `arith` call at the bottom. Measured over 500k
    // rows on a two-INT table: `WHERE id > 5`, which never enters this path
    // at all, cost 18.1 ms and `WHERE id + 1 > 5` cost 71.9 — for one
    // addition. The dispatch below is the SAME `arith` / `div_op` /
    // `mod_op` / `int_div_op` the bottom of `apply_binary` calls, with the
    // same arguments, so the answer (and every overflow and divide-by-zero
    // error) is identical by construction rather than by a second reading.
    let int_operand = |v: &Value<'_>| match v {
        Value::SmallInt(n) => Some(Value::SmallInt(*n)),
        Value::Int(n) => Some(Value::Int(*n)),
        Value::BigInt(n) => Some(Value::BigInt(*n)),
        _ => None,
    };
    if matches!(
        op,
        BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::IntDiv | BinOp::Mod
    ) && let (Some(a), Some(b)) = (int_operand(l), int_operand(r))
    {
        return match op {
            BinOp::Add => arith(a, b, i64::checked_add, |x, y| x + y, "+"),
            BinOp::Sub => arith(a, b, i64::checked_sub, |x, y| x - y, "-"),
            BinOp::Mul => arith(a, b, i64::checked_mul, |x, y| x * y, "*"),
            BinOp::Div => div_op(a, b),
            BinOp::IntDiv => int_div_op(&a, &b),
            BinOp::Mod => mod_op(a, b),
            _ => unreachable!("guarded by the matches! above"),
        }
        .map(Some);
    }

    match op {
        BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq => {
            Ok(Some(compare(op, l, r)?))
        }
        // Everything else needs owned semantics; caller falls back.
        _ => Ok(None),
    }
}

/// Read-only 3VL AND/OR mirror of `and_3vl` / `or_3vl` for the by-ref
/// path. Reuses the existing helpers via cheap `.clone()` only when
/// non-NULL branches need to be promoted to the owning code path; for
/// the common case (one side NULL) we resolve directly.
fn and_3vl_by_ref(l: &Value<'_>, r: &Value<'_>) -> Value<'static> {
    // 3VL truth table:
    //   FALSE AND _     = FALSE
    //   _ AND FALSE     = FALSE
    //   NULL AND TRUE/NULL = NULL
    //   TRUE AND TRUE   = TRUE
    match (l, r) {
        (Value::Bool(false), _) | (_, Value::Bool(false)) => Value::Bool(false),
        (Value::Bool(true), Value::Bool(true)) => Value::Bool(true),
        _ => Value::Null,
    }
}

fn or_3vl_by_ref(l: &Value<'_>, r: &Value<'_>) -> Value<'static> {
    // TRUE OR _   = TRUE
    // _ OR TRUE   = TRUE
    // NULL OR FALSE/NULL = NULL
    // FALSE OR FALSE = FALSE
    match (l, r) {
        (Value::Bool(true), _) | (_, Value::Bool(true)) => Value::Bool(true),
        (Value::Bool(false), Value::Bool(false)) => Value::Bool(false),
        _ => Value::Null,
    }
}

pub(crate) fn apply_binary(
    op: BinOp,
    l: Value<'static>,
    r: Value<'static>,
) -> Result<Value<'static>, EvalError> {
    // v7.39 (read01 round 70) — `tags @> '{b}'`. PG reads a bare string literal
    // beside an ARRAY operand as an array of that type (an "unknown" literal
    // takes the other side's type). SPG saw a TEXT and routed the operator to
    // its JSON reading — `@>` / `<@` errored, and `&&` went to the INET one. The
    // element-typed forms (`tags @> ARRAY['b']`) worked all along, so this was a
    // pure literal-coercion hole, not a missing operator.
    let (l, r) = coerce_array_literal_operands(op, l, r);
    // v7.39 (round 256) — PG treats a plain range as the one-element
    // multirange containing it, so the range↔multirange forms of the
    // CONTAINMENT / OVERLAP / POSITIONAL operators resolve. SPG had only
    // the multirange↔multirange arms wired: a mixed `range && multirange`
    // fell through to the INET reading and errored, and `range @>
    // multirange` / `multirange <@ range` answered a silent FALSE.
    // Promoting once here is the whole fix — the existing multirange arms
    // then handle every combination.
    //
    // The list is deliberately NOT every operator: PG declares no mixed
    // overload for the set algebra (`+` `-` `*`) or the comparisons, and
    // a blanket promotion made `multirange + range` silently answer where
    // PG raises `operator does not exist` (caught by probing the widened
    // surface, not by the cases this round set out to fix).
    let mixed_promotable = matches!(
        op,
        BinOp::JsonContains
            | BinOp::JsonContainedBy
            | BinOp::InetOverlap
            | BinOp::InetContainedBy
            | BinOp::InetContains
            | BinOp::OverLeft
            | BinOp::OverRight
    );
    let (l, r) = match (
        mixed_promotable && matches!(l, Value::Multirange { .. }),
        mixed_promotable && matches!(r, Value::Multirange { .. }),
    ) {
        (true, false) => match range_as_multirange(&r) {
            Some(m) => (l, m),
            None => (l, r),
        },
        (false, true) => match range_as_multirange(&l) {
            Some(m) => (m, r),
            None => (l, r),
        },
        _ => (l, r),
    };
    // SQL three-valued logic for AND / OR with NULL is special — handle before
    // the general NULL-propagation rule.
    if let BinOp::And = op {
        return and_3vl(l, r);
    }
    if let BinOp::Or = op {
        return or_3vl(l, r);
    }
    // v7.9.27b — IS [NOT] DISTINCT FROM. NULL-safe equality:
    // `NULL IS NOT DISTINCT FROM NULL` → true. mailrs pg_dump.
    if let BinOp::IsNotDistinctFrom = op {
        require_comparable(BinOp::Eq, &l, &r)?;
        return Ok(Value::Bool(values_not_distinct(&l, &r)));
    }
    if let BinOp::IsDistinctFrom = op {
        require_comparable(BinOp::Eq, &l, &r)?;
        return Ok(Value::Bool(!values_not_distinct(&l, &r)));
    }
    // v7.38 (read01) — PG's array `||` treats a NULL array operand as an empty
    // array (array_cat semantics), so `arr || NULL` and `NULL || arr` yield the
    // array itself, not NULL. (A scalar `text || NULL` still propagates NULL;
    // this only fires when the non-NULL side is an array.)
    if let BinOp::Concat = op {
        if l.is_null() && super::values::array_len(&r).is_some() {
            return Ok(r);
        }
        if r.is_null() && super::values::array_len(&l).is_some() {
            return Ok(l);
        }
    }
    // Everything else: any NULL operand → NULL.
    if l.is_null() || r.is_null() {
        return Ok(Value::Null);
    }
    // v7.38 (read01 sweep) — arithmetic against an unknown-type string literal
    // coerces the literal to the numeric operand's type (`5 + '3'`, `'10' - 2`,
    // `1.5 + '2'`), mirroring PG's implicit unknown → typed cast. Only the
    // arithmetic operators: `||` keeps its number→text rule and comparisons
    // coerce inside compare(). A literal that won't parse falls through to the
    // normal type-mismatch error.
    if matches!(
        op,
        BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod
    ) {
        let is_num = |v: &Value<'_>| {
            matches!(
                v.data_type(),
                Some(
                    DataType::Int
                        | DataType::BigInt
                        | DataType::SmallInt
                        | DataType::Float
                        | DataType::Numeric { .. }
                )
            )
        };
        if let Value::Text(s) = &l {
            if let Some(dt) = is_num(&r).then(|| r.data_type()).flatten() {
                if let Ok(c) = crate::conversions::coerce_value(Value::text(s.as_ref()), dt, "", 0)
                {
                    return apply_binary(op, c, r);
                }
            }
        }
        if let Value::Text(s) = &r {
            if let Some(dt) = is_num(&l).then(|| l.data_type()).flatten() {
                if let Ok(c) = crate::conversions::coerce_value(Value::text(s.as_ref()), dt, "", 0)
                {
                    return apply_binary(op, l, c);
                }
            }
        }
    }
    // NUMERIC arithmetic and comparisons run in fixed-point; promote
    // integers to a common NUMERIC scale and stay in i128 throughout.
    // A NUMERIC paired with an INTERVAL is interval scaling, not
    // numeric math — let the calendar path below take it.
    // `inet - inet` -> bigint: the count of addresses between them. Same
    // family required, matching PG.
    if op == BinOp::Sub {
        if let (
            Value::Inet {
                family: fa,
                addr: aa,
                ..
            },
            Value::Inet {
                family: fb,
                addr: ab,
                ..
            },
        ) = (&l, &r)
        {
            if fa != fb {
                return Err(EvalError::TypeMismatch {
                    detail: "cannot subtract addresses of different families".into(),
                });
            }
            let diff = inet_addr_u128(*fa, aa) as i128 - inet_addr_u128(*fb, ab) as i128;
            return i64::try_from(diff)
                .map(Value::BigInt)
                .map_err(|_| EvalError::TypeMismatch {
                    detail: "result out of range".into(),
                });
        }
    }
    // `inet ± bigint` -> inet: shift the address by N, keeping family + bits.
    // `bigint + inet` is commutative. PG18-verified: 192.168.1.5 + 10 =
    // 192.168.1.15/32.
    if matches!(op, BinOp::Add | BinOp::Sub) {
        let as_int = |v: &Value| -> Option<i128> {
            match v {
                Value::SmallInt(n) => Some(i128::from(*n)),
                Value::Int(n) => Some(i128::from(*n)),
                Value::BigInt(n) => Some(i128::from(*n)),
                _ => None,
            }
        };
        // A cidr operand behaves as PG's implicit cidr->inet cast: the result
        // is an inet shifted the same way.
        let arith = match (&l, &r) {
            (Value::Inet { family, bits, addr }, other)
            | (Value::Cidr { family, bits, addr }, other) => as_int(other)
                .map(|n| (*family, *bits, *addr, if op == BinOp::Sub { -n } else { n })),
            (other, Value::Inet { family, bits, addr })
            | (other, Value::Cidr { family, bits, addr })
                if op == BinOp::Add =>
            {
                as_int(other).map(|n| (*family, *bits, *addr, n))
            }
            _ => None,
        };
        if let Some((family, bits, addr, delta)) = arith {
            let cur = inet_addr_u128(family, &addr);
            let next = if delta >= 0 {
                cur.checked_add(delta as u128)
            } else {
                cur.checked_sub(delta.unsigned_abs())
            };
            let max = if family == 4 {
                u128::from(u32::MAX)
            } else {
                u128::MAX
            };
            let next = match next {
                Some(v) if v <= max => v,
                _ => {
                    return Err(EvalError::TypeMismatch {
                        detail: "result is out of range for the inet type".into(),
                    });
                }
            };
            let mut new_addr = [0u8; 16];
            if family == 4 {
                #[allow(clippy::cast_possible_truncation)]
                new_addr[0..4].copy_from_slice(&(next as u32).to_be_bytes());
            } else {
                new_addr.copy_from_slice(&next.to_be_bytes());
            }
            return Ok(Value::Inet {
                family,
                bits,
                addr: new_addr,
            });
        }
    }
    // v7.39 (read01 pg_lsn.c) — `pg_lsn ± numeric` shifts the WAL location
    // by N bytes (commutative for +); `pg_lsn - pg_lsn` is the byte
    // difference as numeric. Out-of-range shifts are PG's dedicated error.
    if matches!(op, BinOp::Add | BinOp::Sub) {
        let as_bytes = |v: &Value| -> Option<i128> {
            match v {
                Value::SmallInt(n) => Some(i128::from(*n)),
                Value::Int(n) => Some(i128::from(*n)),
                Value::BigInt(n) => Some(i128::from(*n)),
                Value::Numeric {
                    scaled,
                    scale: 0,
                    kind: spg_storage::NumericKind::Finite,
                } => Some(*scaled),
                _ => None,
            }
        };
        if let (Value::PgLsn(a), Value::PgLsn(b)) = (&l, &r) {
            if op == BinOp::Sub {
                return Ok(Value::Numeric {
                    scaled: i128::from(*a) - i128::from(*b),
                    scale: 0,
                    kind: spg_storage::NumericKind::Finite,
                });
            }
        }
        let shift = match (&l, &r) {
            (Value::PgLsn(a), other) => {
                as_bytes(other).map(|n| (*a, if op == BinOp::Sub { -n } else { n }))
            }
            (other, Value::PgLsn(a)) if op == BinOp::Add => as_bytes(other).map(|n| (*a, n)),
            _ => None,
        };
        if let Some((base, delta)) = shift {
            let next = i128::from(base) + delta;
            let Ok(next) = u64::try_from(next) else {
                return Err(EvalError::TypeMismatch {
                    detail: "pg_lsn out of range".into(),
                });
            };
            return Ok(Value::PgLsn(next));
        }
    }
    // PG `path + path` concatenates the two point lists (both taken open);
    // the result is an open path. Verified vs PG18.4.
    if op == BinOp::Add {
        if let (Value::Path { points: pa, .. }, Value::Path { points: pb, .. }) = (&l, &r) {
            let mut points = pa.clone();
            points.extend_from_slice(pb);
            return Ok(Value::Path {
                points,
                closed: false,
            });
        }
    }
    // PG `point ± point` translates a point by another's coordinates;
    // `point * point` / `point / point` are complex-number multiply/divide
    // (PG treats a point as the complex number x + yi).
    if let (Value::Point(a), Value::Point(b)) = (&l, &r) {
        match op {
            BinOp::Add => {
                return Ok(Value::Point(spg_storage::Point2D {
                    x: a.x + b.x,
                    y: a.y + b.y,
                }));
            }
            BinOp::Sub => {
                return Ok(Value::Point(spg_storage::Point2D {
                    x: a.x - b.x,
                    y: a.y - b.y,
                }));
            }
            BinOp::Mul => {
                // (a.x + a.y i)(b.x + b.y i)
                return Ok(Value::Point(spg_storage::Point2D {
                    x: a.x * b.x - a.y * b.y,
                    y: a.x * b.y + a.y * b.x,
                }));
            }
            BinOp::Div => {
                let denom = b.x * b.x + b.y * b.y;
                return Ok(Value::Point(spg_storage::Point2D {
                    x: (a.x * b.x + a.y * b.y) / denom,
                    y: (a.y * b.x - a.x * b.y) / denom,
                }));
            }
            _ => {}
        }
    }
    // v7.39 (read01 geo_ops.c part 2) — box / circle / path scale by a
    // point with complex-number multiply/divide, like `point * point`
    // (PG box_mul / circle_mul_pt / path_mul_pt and the _div twins).
    if matches!(op, BinOp::Mul | BinOp::Div)
        && matches!(&r, Value::Point(_))
        && matches!(
            &l,
            Value::PgBox(..) | Value::Circle { .. } | Value::Path { .. }
        )
    {
        let Value::Point(f) = &r else { unreachable!() };
        let cx = |p: &spg_storage::Point2D| -> Result<spg_storage::Point2D, EvalError> {
            Ok(if matches!(op, BinOp::Mul) {
                spg_storage::Point2D {
                    x: p.x * f.x - p.y * f.y,
                    y: p.x * f.y + p.y * f.x,
                }
            } else {
                let denom = f.x * f.x + f.y * f.y;
                if denom == 0.0 {
                    return Err(EvalError::DivisionByZero);
                }
                spg_storage::Point2D {
                    x: (p.x * f.x + p.y * f.y) / denom,
                    y: (p.y * f.x - p.x * f.y) / denom,
                }
            })
        };
        let mag = sqrt_newton(f.x * f.x + f.y * f.y);
        return match &l {
            Value::PgBox(a, b) => {
                let (na, nb) = (cx(a)?, cx(b)?);
                // Re-canonicalize the corners (high, low).
                let hi = spg_storage::Point2D {
                    x: na.x.max(nb.x),
                    y: na.y.max(nb.y),
                };
                let lo = spg_storage::Point2D {
                    x: na.x.min(nb.x),
                    y: na.y.min(nb.y),
                };
                Ok(Value::PgBox(hi, lo))
            }
            Value::Circle { center, radius } => Ok(Value::Circle {
                center: cx(center)?,
                radius: if matches!(op, BinOp::Mul) {
                    radius * mag
                } else {
                    if mag == 0.0 {
                        return Err(EvalError::DivisionByZero);
                    }
                    radius / mag
                },
            }),
            Value::Path { points, closed } => {
                let mut out = alloc::vec::Vec::with_capacity(points.len());
                for p in points {
                    out.push(cx(p)?);
                }
                Ok(Value::Path {
                    points: out,
                    closed: *closed,
                })
            }
            _ => unreachable!(),
        };
    }
    // v7.39 (read01 multirangetypes.c) — multirange set algebra:
    // + union, - difference, * intersection.
    if matches!(op, BinOp::Add | BinOp::Sub | BinOp::Mul)
        && matches!(l, Value::Multirange { .. })
        && matches!(r, Value::Multirange { .. })
    {
        let (Value::Multirange { kind, ranges: a }, Value::Multirange { ranges: b, .. }) = (&l, &r)
        else {
            unreachable!()
        };
        let ranges = match op {
            BinOp::Add => multirange_union(*kind, a, b),
            BinOp::Sub => multirange_difference(*kind, a, b),
            _ => multirange_intersection(*kind, a, b),
        };
        return Ok(Value::Multirange {
            kind: *kind,
            ranges,
        });
    }
    // MONEY arithmetic (integer cents) before the generic numeric path.
    if let Some(result) = money_arith(op, &l, &r) {
        return result;
    }
    // Range `*` intersection / `+` union / `-` difference claim Mul / Add / Sub
    // before numeric/date arithmetic.
    if op == BinOp::Mul || op == BinOp::Add || op == BinOp::Sub {
        if let (
            Value::Range {
                kind: ak,
                lower: al,
                upper: au,
                lower_inc: ali,
                upper_inc: aui,
                empty: ae,
            },
            Value::Range {
                kind: bk,
                lower: bl,
                upper: bu,
                lower_inc: bli,
                upper_inc: bui,
                empty: be,
            },
        ) = (&l, &r)
        {
            return match op {
                BinOp::Mul => Ok(range_intersect(
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                )),
                BinOp::Add => range_union(
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                ),
                _ => range_difference(
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                ),
            };
        }
    }
    if (matches!(l, Value::Numeric { .. } | Value::NumericBig(_))
        || matches!(r, Value::Numeric { .. } | Value::NumericBig(_))
        || matches!(l, Value::Real(_))
        || matches!(r, Value::Real(_)))
        && !matches!(l, Value::Interval { .. })
        && !matches!(r, Value::Interval { .. })
        // A range containment/overlap op (`range @> numeric`, `<@`, `&&`) keeps
        // a Numeric element — don't let the numeric fast-path claim it; the
        // range arms below handle it.
        && !matches!(l, Value::Range { .. })
        && !matches!(r, Value::Range { .. })
        // v7.37.15 — `||` is not arithmetic, whatever the operands are.
        //
        // This fast path keys on the OPERAND type and ignores the operator,
        // so once `Value::Real` joined it (T-float4, 7c36d30d) a REAL on
        // either side captured concatenation too: `'x' || score` reached
        // `apply_binary_numeric`, which tried to read the TEXT side as a
        // float and answered `cannot convert text to FLOAT`. The Concat arm
        // below, which formats the number and joins, was unreachable for
        // exactly the types that needed formatting.
        //
        // The asymmetry it left is what the drop-in panel saw: `|| MAX(d)`
        // (BIGINT) worked, `|| 1.5` (a Float literal) worked, and only
        // `|| score` (REAL) failed. Interval and Range already opt out of
        // this path the same way.
        && !matches!(op, BinOp::Concat)
    {
        return apply_binary_numeric(op, l, r);
    }
    // Date / Timestamp arithmetic. PG semantics:
    //   * date + int      → date  (int is days)
    //   * int + date      → date
    //   * date - int      → date
    //   * date - date     → int   (days, signed)
    //   * timestamp - timestamp → bigint (microseconds, signed)
    // Other date/time math (`timestamp + int`, INTERVAL) lands later.
    if let Some(result) = apply_binary_calendar(op, &l, &r)? {
        return Ok(result);
    }
    match op {
        BinOp::Add => arith(l, r, i64::checked_add, |a, b| a + b, "+"),
        // PG `jsonb - text` / `jsonb - int` / `jsonb - text[]` deletes an
        // object key, an array element, or a set of object keys. Routes
        // to the same code the `jsonb_delete` function uses.
        BinOp::Sub if matches!(l, Value::Json(_)) => crate::json::delete_key(&l, &r),
        BinOp::Sub => arith(l, r, i64::checked_sub, |a, b| a - b, "-"),
        BinOp::Mul => arith(l, r, i64::checked_mul, |a, b| a * b, "*"),
        BinOp::Div => div_op(l, r),
        // v7.39 (round 353, M9) — MySQL's `DIV`: integer division that
        // truncates TOWARD ZERO (`-7 DIV 2` is -3, `7 DIV -2` is -3) and
        // answers NULL on a zero divisor rather than raising. Measured on
        // MariaDB 11; a fractional or string operand is read as a number
        // first (`7.5 DIV 2` is 3, `'9' DIV 2` is 4).
        BinOp::IntDiv => int_div_op(&l, &r),
        BinOp::Mod => mod_op(l, r),
        // v7.39 (round 245) — `tsquery <-> tsquery` is PG's phrase
        // concatenation (tsquery_phrase, distance 1), a different operator
        // from the vector distance that shares the spelling.
        BinOp::L2Distance if matches!(l, Value::TsQuery(_)) && matches!(r, Value::TsQuery(_)) => {
            let (Value::TsQuery(a), Value::TsQuery(b)) = (&l, &r) else {
                unreachable!()
            };
            Ok(Value::TsQuery(spg_storage::TsQueryAst::Phrase {
                left: alloc::boxed::Box::new(a.clone()),
                right: alloc::boxed::Box::new(b.clone()),
                distance: 1,
            }))
        }
        BinOp::L2Distance => l2_distance(l, r),
        BinOp::InnerProduct => inner_product(l, r),
        BinOp::CosineDistance => cosine_distance(l, r),
        // PG `jsonb || jsonb` merges objects (right wins on dup keys) /
        // appends arrays. Text `||` stays text concatenation.
        // tsquery `||` is boolean OR (claim it before text concatenation).
        BinOp::Concat if matches!(l, Value::TsQuery(_)) && matches!(r, Value::TsQuery(_)) => {
            let (Value::TsQuery(a), Value::TsQuery(b)) = (&l, &r) else {
                unreachable!()
            };
            Ok(Value::TsQuery(spg_storage::TsQueryAst::Or(
                alloc::boxed::Box::new(a.clone()),
                alloc::boxed::Box::new(b.clone()),
            )))
        }
        BinOp::Concat if matches!(l, Value::Json(_)) || matches!(r, Value::Json(_)) => {
            crate::json::concat(&l, &r)
        }
        BinOp::Concat => Ok(text_concat(&l, &r)),
        // PG `jsonb #- text[]` deletes the value at a nested path.
        BinOp::JsonDeletePath => crate::json::delete_path(&[l, r]),
        BinOp::BitOr | BinOp::BitAnd
            if matches!(l, Value::Inet { .. }) && matches!(r, Value::Inet { .. }) =>
        {
            inet_bitwise(op, &l, &r)
        }
        BinOp::BitOr => bitop(l, r, |a, b| a | b, "|"),
        BinOp::BitAnd => bitop(l, r, |a, b| a & b, "&"),
        // v7.38 (read01) — PG `lseg # lseg`: the point where two segments cross,
        // or NULL when they don't. Distinct from the bit/integer `#` (XOR).
        BinOp::BitXor if matches!(l, Value::Lseg(..)) && matches!(r, Value::Lseg(..)) => {
            let (Value::Lseg(a1, a2), Value::Lseg(b1, b2)) = (&l, &r) else {
                unreachable!()
            };
            match lseg_intersection(*a1, *a2, *b1, *b2) {
                Some(p) => Ok(Value::Point(p)),
                None => Ok(Value::Null),
            }
        }
        // v7.39 (read01 geo_ops.c part 2) — `box # box` intersection box
        // (NULL when they don't overlap) and `line # line` intersection
        // point (NULL for parallel lines, PG line_interpt_line).
        BinOp::BitXor if matches!(l, Value::PgBox(..)) && matches!(r, Value::PgBox(..)) => {
            let (Value::PgBox(aur, all), Value::PgBox(bur, bll)) = (&l, &r) else {
                unreachable!()
            };
            if !(all.x <= bur.x && bll.x <= aur.x && all.y <= bur.y && bll.y <= aur.y) {
                return Ok(Value::Null);
            }
            Ok(Value::PgBox(
                spg_storage::Point2D {
                    x: aur.x.min(bur.x),
                    y: aur.y.min(bur.y),
                },
                spg_storage::Point2D {
                    x: all.x.max(bll.x),
                    y: all.y.max(bll.y),
                },
            ))
        }
        BinOp::BitXor if matches!(l, Value::Line { .. }) && matches!(r, Value::Line { .. }) => {
            let (
                Value::Line {
                    a: a1,
                    b: b1,
                    c: c1,
                },
                Value::Line {
                    a: a2,
                    b: b2,
                    c: c2,
                },
            ) = (&l, &r)
            else {
                unreachable!()
            };
            match line_line_interpt(*a1, *b1, *c1, *a2, *b2, *c2) {
                Some(p) => Ok(Value::Point(p)),
                None => Ok(Value::Null),
            }
        }
        BinOp::BitXor => bitop(l, r, |a, b| a ^ b, "#"),
        // v7.39 (read01 geo_ops.c part 2) — `##` closest point on the
        // right-hand object to the left-hand point (PG close_ps /
        // close_pb; the point is ITS OWN closest point when inside the
        // box).
        BinOp::ClosestPoint => match (&l, &r) {
            (Value::Point(pt), Value::Lseg(a, b)) => {
                Ok(Value::Point(lseg_closest_to_point(a, b, pt)))
            }
            (Value::Point(pt), Value::PgBox(ur, ll)) => {
                let (hx, hy) = (ll.x.max(ur.x), ll.y.max(ur.y));
                let (lx, ly) = (ll.x.min(ur.x), ll.y.min(ur.y));
                if pt.x >= lx && pt.x <= hx && pt.y >= ly && pt.y <= hy {
                    return Ok(Value::Point(*pt));
                }
                let c = [
                    spg_storage::Point2D { x: lx, y: ly },
                    spg_storage::Point2D { x: lx, y: hy },
                    spg_storage::Point2D { x: hx, y: hy },
                    spg_storage::Point2D { x: hx, y: ly },
                ];
                let mut best = spg_storage::Point2D { x: lx, y: ly };
                let mut bd = f64::INFINITY;
                for i in 0..4 {
                    let j = (i + 1) % 4;
                    let n = lseg_closest_to_point(&c[i], &c[j], pt);
                    let d = pt_dist(&n, pt);
                    if d < bd {
                        bd = d;
                        best = n;
                    }
                }
                Ok(Value::Point(best))
            }
            _ => Err(EvalError::TypeMismatch {
                detail: alloc::format!(
                    "operator ## not supported for {:?} and {:?}",
                    l.data_type(),
                    r.data_type()
                ),
            }),
        },
        // v7.39 (read01 geo_ops.c part 2) — `point ?- point`: horizontally
        // aligned (equal y under the geometric epsilon).
        BinOp::GeomHoriz => match (&l, &r) {
            (Value::Point(a), Value::Point(b)) => Ok(Value::Bool((a.y - b.y).abs() <= 1.0e-6)),
            _ => Err(EvalError::TypeMismatch {
                detail: alloc::format!(
                    "operator ?- not supported for {:?} and {:?}",
                    l.data_type(),
                    r.data_type()
                ),
            }),
        },
        // v7.39 (round 508) — `<^` / `>^`: strictly below / strictly above.
        // On points that is the y coordinate; on boxes PG compares the two
        // boxes' vertical extents, so a box is "below" another when its
        // HIGH y is under the other's LOW y. Measured: `box '((0,0),(1,1))'
        // <^ box '((0,2),(1,3))'` is true.
        BinOp::IsBelow | BinOp::IsAbove => {
            let below = matches!(op, BinOp::IsBelow);
            let extent = |v: &Value<'_>| -> Option<(f64, f64)> {
                match v {
                    Value::Point(p) => Some((p.y, p.y)),
                    Value::PgBox(a, b) => Some((a.y.min(b.y), a.y.max(b.y))),
                    _ => None,
                }
            };
            match (extent(&l), extent(&r)) {
                (Some((llo, lhi)), Some((rlo, rhi))) => {
                    Ok(Value::Bool(if below { lhi < rlo } else { llo > rhi }))
                }
                _ => Err(EvalError::TypeMismatch {
                    detail: alloc::format!(
                        "operator {} not supported for {:?} and {:?}",
                        if below { "<^" } else { ">^" },
                        l.data_type(),
                        r.data_type()
                    ),
                }),
            }
        }
        // v7.39 (round 508) — `?#` "do these intersect". PG defines it for
        // box/box, line/box, line/line, lseg/box, lseg/line, lseg/lseg and
        // path/path; the shared reading is "do the two point sets meet".
        BinOp::Intersects => geom_intersects(&l, &r),
        // v7.39 (round 508) — the `text_pattern_ops` comparisons. They
        // compare BYTES and ignore collation, which is exactly what makes
        // them usable for a LIKE-prefix index: measured on PG18,
        // `'A' ~<~ 'a'` is true while `'A' < 'a'` is false.
        BinOp::PatternLt | BinOp::PatternLtEq | BinOp::PatternGt | BinOp::PatternGtEq => {
            match (&l, &r) {
                (Value::Text(a), Value::Text(b)) => {
                    let ord = a.as_bytes().cmp(b.as_bytes());
                    Ok(Value::Bool(match op {
                        BinOp::PatternLt => ord.is_lt(),
                        BinOp::PatternLtEq => ord.is_le(),
                        BinOp::PatternGt => ord.is_gt(),
                        _ => ord.is_ge(),
                    }))
                }
                _ => Err(EvalError::TypeMismatch {
                    detail: alloc::format!(
                        "operator {op} not supported for {:?} and {:?}",
                        l.data_type(),
                        r.data_type()
                    ),
                }),
            }
        }
        // v7.39 (read01 geo_ops.c) — geometric predicates. Slopes compare
        // with PG's geometric EPSILON (1e-6): parallel = equal slopes,
        // perpendicular = vertical×horizontal or m1·m2 = -1.
        BinOp::GeomParallel | BinOp::GeomPerp => {
            const EPS: f64 = 1.0e-6;
            let slope_of = |v: &Value<'_>| -> Option<f64> {
                match v {
                    Value::Lseg(a, b) => Some(if (a.x - b.x).abs() <= EPS {
                        f64::INFINITY
                    } else if (a.y - b.y).abs() <= EPS {
                        0.0
                    } else {
                        (a.y - b.y) / (a.x - b.x)
                    }),
                    Value::Line { a, b, .. } => Some(if b.abs() <= EPS {
                        f64::INFINITY
                    } else {
                        -a / b
                    }),
                    _ => None,
                }
            };
            match (slope_of(&l), slope_of(&r)) {
                (Some(m1), Some(m2)) => {
                    let res = if matches!(op, BinOp::GeomParallel) {
                        (m1.is_infinite() && m2.is_infinite()) || (m1 - m2).abs() <= EPS
                    } else if m1.is_infinite() {
                        m2.abs() <= EPS
                    } else if m2.is_infinite() {
                        m1.abs() <= EPS
                    } else {
                        (m1 * m2 + 1.0).abs() <= EPS
                    };
                    Ok(Value::Bool(res))
                }
                _ => Err(EvalError::TypeMismatch {
                    detail: alloc::format!(
                        "operator {} needs lseg or line operands, got {:?} and {:?}",
                        if matches!(op, BinOp::GeomParallel) {
                            "?||"
                        } else {
                            "?-|"
                        },
                        l.data_type(),
                        r.data_type()
                    ),
                }),
            }
        }
        // v7.39 (read01 geo_ops.c) — `~=` "same as": component equality
        // under the geometric EPSILON, for point / box / circle / polygon.
        BinOp::GeomSameAs => {
            const EPS: f64 = 1.0e-6;
            let feq = |a: f64, b: f64| (a - b).abs() <= EPS;
            let res = match (&l, &r) {
                (Value::Point(a), Value::Point(b)) => Some(feq(a.x, b.x) && feq(a.y, b.y)),
                (Value::PgBox(a1, a2), Value::PgBox(b1, b2)) => {
                    // Canonical corners (high, low) compare pairwise.
                    let hx = |p: &spg_storage::Point2D, q: &spg_storage::Point2D| {
                        (p.x.max(q.x), p.y.max(q.y), p.x.min(q.x), p.y.min(q.y))
                    };
                    let a = hx(a1, a2);
                    let b = hx(b1, b2);
                    Some(feq(a.0, b.0) && feq(a.1, b.1) && feq(a.2, b.2) && feq(a.3, b.3))
                }
                (
                    Value::Circle {
                        center: c1,
                        radius: r1,
                    },
                    Value::Circle {
                        center: c2,
                        radius: r2,
                    },
                ) => Some(feq(c1.x, c2.x) && feq(c1.y, c2.y) && feq(*r1, *r2)),
                (Value::Polygon(a), Value::Polygon(b)) => Some(
                    a.len() == b.len()
                        && a.iter()
                            .zip(b.iter())
                            .all(|(p, q)| feq(p.x, q.x) && feq(p.y, q.y)),
                ),
                _ => None,
            };
            match res {
                Some(v) => Ok(Value::Bool(v)),
                None => Err(EvalError::TypeMismatch {
                    detail: alloc::format!(
                        "operator ~= needs matching geometric operands, got {:?} and {:?}",
                        l.data_type(),
                        r.data_type()
                    ),
                }),
            }
        }
        BinOp::JsonGet => crate::json::path_get(&l, &r, false),
        BinOp::JsonGetText => crate::json::path_get(&l, &r, true),
        BinOp::JsonGetPath => crate::json::path_walk(&l, &r, false),
        BinOp::JsonGetPathText => crate::json::path_walk(&l, &r, true),
        // v7.37 — RANGE operands claim @> / <@ / && ahead of the array /
        // JSON / inet interpretations. `range @> elem|range`, `<@` swapped,
        // `&&` overlap. Verified vs PG18.
        // v7.39 (read01 multirangetypes.c) — multirange containment.
        BinOp::JsonContains if matches!(l, Value::Multirange { .. }) => {
            let Value::Multirange { kind, ranges } = &l else {
                unreachable!()
            };
            match multirange_contains(*kind, ranges, &r) {
                Some(b) => Ok(Value::Bool(b)),
                None => Ok(Value::Null),
            }
        }
        BinOp::JsonContainedBy if matches!(r, Value::Multirange { .. }) => {
            let Value::Multirange { kind, ranges } = &r else {
                unreachable!()
            };
            match multirange_contains(*kind, ranges, &l) {
                Some(b) => Ok(Value::Bool(b)),
                None => Ok(Value::Null),
            }
        }
        // v7.39 (round 256) — the POSITIONAL operators read a multirange
        // as its outer hull (probed: `{[1,3),[9,11)} -|- {[3,5)}` is
        // FALSE, so it is not an any-element rule). An empty multirange
        // has no hull and answers false. Rewriting to the hull lets the
        // existing range arms below do the work.
        BinOp::InetContainedBy | BinOp::InetContains | BinOp::OverLeft | BinOp::OverRight
            if matches!(l, Value::Multirange { .. }) && matches!(r, Value::Multirange { .. }) =>
        {
            let (Value::Multirange { kind, ranges: a }, Value::Multirange { ranges: b, .. }) =
                (&l, &r)
            else {
                unreachable!()
            };
            if a.is_empty() || b.is_empty() {
                return Ok(Value::Bool(false));
            }
            let (hl, hr) = (multirange_hull(*kind, a), multirange_hull(*kind, b));
            apply_binary(op, hl, hr)
        }
        // Multirange overlap: non-empty intersection.
        BinOp::InetOverlap
            if matches!(l, Value::Multirange { .. }) && matches!(r, Value::Multirange { .. }) =>
        {
            let (Value::Multirange { kind, ranges: a }, Value::Multirange { ranges: b, .. }) =
                (&l, &r)
            else {
                unreachable!()
            };
            Ok(Value::Bool(
                !multirange_intersection(*kind, a, b).is_empty(),
            ))
        }
        BinOp::JsonContains if matches!(l, Value::Range { .. }) => {
            let Value::Range {
                kind: ak,
                lower: al,
                upper: au,
                lower_inc: ali,
                upper_inc: aui,
                empty: ae,
            } = &l
            else {
                unreachable!()
            };
            Ok(Value::Bool(match &r {
                Value::Range {
                    kind: bk,
                    lower: bl,
                    upper: bu,
                    lower_inc: bli,
                    upper_inc: bui,
                    empty: be,
                } => range_contains_range(
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                ),
                // v7.39 (round 256) — an ARRAY is not an element of any
                // range type: PG reports `operator does not exist:
                // int4range @> integer[]`, where SPG answered a silent
                // false through the element path.
                elem if super::values::array_len(elem).is_some() => {
                    return Err(EvalError::TypeMismatch {
                        detail: alloc::format!(
                            "operator does not exist: {} @> {}",
                            super::strings::pg_typeof_name(&l),
                            super::strings::pg_typeof_name(elem)
                        ),
                    });
                }
                elem => range_contains_elem(*ak, al, au, *ali, *aui, *ae, elem),
            }))
        }
        BinOp::JsonContainedBy if matches!(r, Value::Range { .. }) => {
            let Value::Range {
                kind: bk,
                lower: bl,
                upper: bu,
                lower_inc: bli,
                upper_inc: bui,
                empty: be,
            } = &r
            else {
                unreachable!()
            };
            Ok(Value::Bool(match &l {
                Value::Range {
                    kind: ak,
                    lower: al,
                    upper: au,
                    lower_inc: ali,
                    upper_inc: aui,
                    empty: ae,
                } => range_contains_range(
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                ),
                elem => range_contains_elem(*bk, bl, bu, *bli, *bui, *be, elem),
            }))
        }
        // tsquery `&&` is boolean AND (claim it before array / inet overlap).
        BinOp::InetOverlap if matches!(l, Value::TsQuery(_)) && matches!(r, Value::TsQuery(_)) => {
            let (Value::TsQuery(a), Value::TsQuery(b)) = (&l, &r) else {
                unreachable!()
            };
            Ok(Value::TsQuery(spg_storage::TsQueryAst::And(
                alloc::boxed::Box::new(a.clone()),
                alloc::boxed::Box::new(b.clone()),
            )))
        }
        // PG `tsquery @> tsquery` / `<@` — containment by lexeme set: `a @> b`
        // iff every lexeme in b appears in a (combining operators ignored).
        BinOp::JsonContains | BinOp::JsonContainedBy
            if matches!(l, Value::TsQuery(_)) && matches!(r, Value::TsQuery(_)) =>
        {
            let (Value::TsQuery(a), Value::TsQuery(b)) = (&l, &r) else {
                unreachable!()
            };
            let (container, contained) = if matches!(op, BinOp::JsonContains) {
                (a, b)
            } else {
                (b, a)
            };
            let mut cont_lex = alloc::collections::BTreeSet::new();
            tsquery_lexemes(container, &mut cont_lex);
            let mut sub_lex = alloc::collections::BTreeSet::new();
            tsquery_lexemes(contained, &mut sub_lex);
            Ok(Value::Bool(sub_lex.is_subset(&cont_lex)))
        }
        BinOp::InetOverlap
            if matches!(l, Value::Range { .. }) && matches!(r, Value::Range { .. }) =>
        {
            let (
                Value::Range {
                    kind: ak,
                    lower: al,
                    upper: au,
                    lower_inc: ali,
                    upper_inc: aui,
                    empty: ae,
                },
                Value::Range {
                    kind: bk,
                    lower: bl,
                    upper: bu,
                    lower_inc: bli,
                    upper_inc: bui,
                    empty: be,
                },
            ) = (&l, &r)
            else {
                unreachable!()
            };
            Ok(Value::Bool(range_overlaps(
                RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
            )))
        }
        // Geometric `container @> point` / `point <@ container` for
        // polygon / box / circle, ahead of the array & JSON interpretations.
        BinOp::JsonContains if geo_contains_point(&l, &r).is_some() => Ok(Value::Bool(
            geo_contains_point(&l, &r).expect("guard checked"),
        )),
        BinOp::JsonContainedBy if geo_contains_point(&r, &l).is_some() => Ok(Value::Bool(
            geo_contains_point(&r, &l).expect("guard checked"),
        )),
        // v7.39 (read01 geo_ops.c part 2) — polygon⊃polygon and
        // circle⊃circle containment (both directions).
        BinOp::JsonContains if geo_contains_geo(&l, &r).is_some() => Ok(Value::Bool(
            geo_contains_geo(&l, &r).expect("guard checked"),
        )),
        BinOp::JsonContainedBy if geo_contains_geo(&r, &l).is_some() => Ok(Value::Bool(
            geo_contains_geo(&r, &l).expect("guard checked"),
        )),
        // v7.39 (read01 geo_ops.c part 2) — `point <@ lseg` / `point <@
        // path` (the `@>` spellings do not exist in PG, so only the
        // contained-by direction hooks here).
        BinOp::JsonContainedBy if geo_pt_on_object(&r, &l).is_some() => Ok(Value::Bool(
            geo_pt_on_object(&r, &l).expect("guard checked"),
        )),
        BinOp::JsonContains if geo_contains_box(&l, &r).is_some() => Ok(Value::Bool(
            geo_contains_box(&l, &r).expect("guard checked"),
        )),
        BinOp::JsonContainedBy if geo_contains_box(&r, &l).is_some() => Ok(Value::Bool(
            geo_contains_box(&r, &l).expect("guard checked"),
        )),
        // Array operands claim && / @> / <@ before the inet / JSON
        // interpretations: ARRAY[1,2] && ARRAY[2,3] is the overlap
        // test, ARRAY[1,2,3] @> ARRAY[2] the containment test.
        BinOp::JsonContains
            if array_scalar_elems(&l).is_some() && array_scalar_elems(&r).is_some() =>
        {
            Ok(Value::Bool(array_contains_all(&l, &r)))
        }
        BinOp::JsonContainedBy
            if array_scalar_elems(&l).is_some() && array_scalar_elems(&r).is_some() =>
        {
            Ok(Value::Bool(array_contains_all(&r, &l)))
        }
        // Geometric `box && box` / `circle && circle` overlap, ahead of the
        // array-overlap interpretation.
        BinOp::InetOverlap if geo_overlaps(&l, &r).is_some() => {
            Ok(Value::Bool(geo_overlaps(&l, &r).expect("guard checked")))
        }
        BinOp::InetOverlap
            if array_scalar_elems(&l).is_some() && array_scalar_elems(&r).is_some() =>
        {
            let (a, _) = array_scalar_elems(&l).expect("guard checked");
            let (b, _) = array_scalar_elems(&r).expect("guard checked");
            Ok(Value::Bool(a.iter().any(|x| b.iter().any(|y| x == y))))
        }
        BinOp::JsonContains => crate::json::contains(&l, &r),
        // v7.37 — `jsonb @? jsonpath` = jsonb_path_exists: does the path
        // return any item? Reuses the jsonpath query engine.
        BinOp::JsonPathExists => {
            if matches!(l, Value::Null) || matches!(r, Value::Null) {
                Ok(Value::Null)
            } else {
                // v7.39 (round 235) — `@?` SUPPRESSES a strict-mode
                // refusal and answers NULL, where jsonb_path_exists()
                // raises it. Probed against PG18.4.
                match crate::json::path_query(&l, &r) {
                    Err(_) => Ok(Value::Null),
                    Ok(Value::TextArray(items)) => Ok(Value::Bool(!items.is_empty())),
                    Ok(Value::Null) => Ok(Value::Null),
                    Ok(_) => Ok(Value::Bool(true)),
                }
            }
        }
        // v7.37.6-A `<@` reuses `@>` with swapped args.
        BinOp::JsonContainedBy => crate::json::contains(&r, &l),
        BinOp::JsonKeyExists => crate::json::key_exists(&l, &r),
        // v7.39 (read01 geo_ops.c part 2) — `point ?| point`: vertically
        // aligned (equal x under the geometric epsilon), ahead of the
        // JSONB keys-any interpretation.
        BinOp::JsonKeysAny if matches!(l, Value::Point(_)) && matches!(r, Value::Point(_)) => {
            let (Value::Point(a), Value::Point(b)) = (&l, &r) else {
                unreachable!()
            };
            Ok(Value::Bool((a.x - b.x).abs() <= 1.0e-6))
        }
        BinOp::JsonKeysAny => crate::json::keys_any(&l, &r),
        BinOp::JsonKeysAll => crate::json::keys_all(&l, &r),
        // v7.38 (read01, T8) — `jsonb @@ jsonpath` evaluates a boolean
        // predicate (`'{"a":5}' @@ '$.a > 3'`), distinct from tsvector `@@`.
        BinOp::TsMatch if matches!(l, Value::Json(_)) => {
            if matches!(r, Value::Null) {
                return Ok(Value::Null);
            }
            // v7.39 (round 235) — `@@` suppresses a strict-mode refusal
            // too (same PG rule as `@?`).
            match match crate::json::path_predicate(&l, &r) {
                Err(_) => return Ok(Value::Null),
                Ok(v) => v,
            } {
                Some(b) => Ok(Value::Bool(b)),
                None => match crate::json::path_query(&l, &r) {
                    Err(_) => Ok(Value::Null),
                    Ok(Value::TextArray(items)) => Ok(Value::Bool(match items.first() {
                        Some(Some(s)) if s == "false" => false,
                        Some(_) => true,
                        None => false,
                    })),
                    Ok(Value::Null) => Ok(Value::Null),
                    Ok(_) => Ok(Value::Bool(true)),
                },
            }
        }
        // v7.12.2 — `@@` match. NULL on either side → NULL; PG
        // accepts both orderings so we normalise.
        BinOp::TsMatch => ts_match(l, r),
        // v7.39 (read01 rangetypes.c) — range `&<` / `&>`.
        BinOp::OverLeft | BinOp::OverRight
            if matches!(l, Value::Range { .. }) && matches!(r, Value::Range { .. }) =>
        {
            let (Some(a), Some(b)) = (RangeParts::from_value(&l), RangeParts::from_value(&r))
            else {
                unreachable!("guard checked both are ranges")
            };
            Ok(Value::Bool(if matches!(op, BinOp::OverLeft) {
                range_overleft(a, b)
            } else {
                range_overright(a, b)
            }))
        }
        BinOp::OverLeft | BinOp::OverRight => Err(EvalError::TypeMismatch {
            detail: format!(
                "operator {} requires range operands",
                if matches!(op, BinOp::OverLeft) {
                    "&<"
                } else {
                    "&>"
                }
            ),
        }),
        // range `<<` / `>>` — strictly left / right of. Claims the operator
        // ahead of the bitstring / integer-shift / inet interpretations when
        // both operands are ranges.
        BinOp::InetContainedBy | BinOp::InetContains
            if matches!(l, Value::Range { .. }) && matches!(r, Value::Range { .. }) =>
        {
            let (
                Value::Range {
                    kind: ak,
                    lower: al,
                    upper: au,
                    lower_inc: ali,
                    upper_inc: aui,
                    empty: ae,
                },
                Value::Range {
                    kind: bk,
                    lower: bl,
                    upper: bu,
                    lower_inc: bli,
                    upper_inc: bui,
                    empty: be,
                },
            ) = (&l, &r)
            else {
                unreachable!()
            };
            // `a >> b` is `b << a`.
            let strictly_left = if matches!(op, BinOp::InetContainedBy) {
                range_strictly_left(
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                )
            } else {
                range_strictly_left(
                    RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
                    RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                )
            };
            Ok(Value::Bool(strictly_left))
        }
        // bit(n) << / >> k: shift within the fixed-width bit string. Claims
        // the operator ahead of the integer-shift and inet interpretations.
        BinOp::InetContainedBy | BinOp::InetContains
            if matches!(l, Value::BitString { .. }) && int_operand(&r).is_some() =>
        {
            let Value::BitString { nbits, bytes } = &l else {
                unreachable!()
            };
            let k = int_operand(&r).expect("guard checked");
            let out = bitstring_shift(*nbits, bytes, k, matches!(op, BinOp::InetContainedBy));
            Ok(Value::BitString {
                nbits: *nbits,
                bytes: alloc::borrow::Cow::Owned(out),
            })
        }
        // Integer operands claim << / >> as bit shifts before the
        // inet containment interpretation (PG int4/int8 shift ops).
        BinOp::InetContainedBy | BinOp::InetContains
            if int_operand(&l).is_some() && int_operand(&r).is_some() =>
        {
            let a = int_operand(&l).expect("guard checked");
            let n = int_operand(&r).expect("guard checked");
            let shifted = if matches!(op, BinOp::InetContainedBy) {
                // PG masks the shift count to the operand width.
                a.wrapping_shl((n & 63) as u32)
            } else {
                a.wrapping_shr((n & 63) as u32)
            };
            if matches!(l, Value::BigInt(_)) {
                Ok(Value::BigInt(shifted))
            } else if matches!(l, Value::SmallInt(_)) {
                // v7.38 (read01) — PG `int2 << / >> k` stays int2.
                small_int_result(shifted)
            } else {
                #[allow(clippy::cast_possible_truncation)]
                Ok(Value::Int(shifted as i32))
            }
        }
        // v7.17.0 Phase 3.P0-47 — PG INET / CIDR containment + overlap.
        BinOp::InetContainedBy
        | BinOp::InetContainedByEq
        | BinOp::InetContains
        | BinOp::InetContainsEq
        | BinOp::InetOverlap => inet_op_bool_result(op, &l, &r),
        BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq => {
            compare(op, &l, &r)
        }
        BinOp::And
        | BinOp::Or
        | BinOp::LogicalXor
        | BinOp::IsDistinctFrom
        | BinOp::IsNotDistinctFrom => {
            unreachable!("handled above")
        }
    }
}

/// Calendar arithmetic. Returns `Some(value)` when the operand pair
/// is a date/time combo this function understands, `None` to let the
/// caller fall through to the regular numeric / text paths.
fn apply_binary_calendar(
    op: BinOp,
    l: &Value<'static>,
    r: &Value<'static>,
) -> Result<Option<Value<'static>>, EvalError> {
    let int_value = |v: &Value| -> Option<i64> {
        match v {
            Value::SmallInt(n) => Some(i64::from(*n)),
            Value::Int(n) => Some(i64::from(*n)),
            Value::BigInt(n) => Some(*n),
            _ => None,
        }
    };
    // PG resolves `<temporal> - <unknown-type string literal>` to the
    // same-type difference (date-date→int days, ts-ts/time-time/interval-
    // interval→interval; `date - '5 days'` is a date-date parse error in PG,
    // not date-interval). Coerce the Text operand to the temporal operand's
    // type before the arms below. Only `-` is unambiguous this way — `+`
    // stays a "not unique" error, matching PG.
    if op == BinOp::Sub {
        let temporal_dt = |v: &Value| match v.data_type() {
            Some(
                dt @ (DataType::Date | DataType::Timestamp | DataType::Time | DataType::Interval),
            ) => Some(dt),
            _ => None,
        };
        if let (Value::Text(s), Some(dt)) = (l, temporal_dt(r)) {
            if let Ok(c) = crate::conversions::coerce_value(Value::text(s.as_ref()), dt, "", 0) {
                return apply_binary_calendar(op, &c, r);
            }
        }
        if let (Some(dt), Value::Text(s)) = (temporal_dt(l), r) {
            if let Ok(c) = crate::conversions::coerce_value(Value::text(s.as_ref()), dt, "", 0) {
                return apply_binary_calendar(op, l, &c);
            }
        }
    }
    // PG resolves `<time|interval|timestamp> + <unknown-type string literal>`
    // to `… + interval` (the only `+` those types have), so the literal is
    // coerced to INTERVAL: `'10:00'::time + '30 minutes'` → 10:30:00. `date +`
    // stays a "not unique" error (date+int and date+interval both exist),
    // matching PG. Symmetric in operand order.
    if op == BinOp::Add {
        let plus_interval = |v: &Value| {
            matches!(
                v.data_type(),
                Some(DataType::Time | DataType::Interval | DataType::Timestamp)
            )
        };
        if let (Value::Text(s), true) = (l, plus_interval(r)) {
            if let Ok(c) =
                crate::conversions::coerce_value(Value::text(s.as_ref()), DataType::Interval, "", 0)
            {
                return apply_binary_calendar(op, &c, r);
            }
        }
        if let (true, Value::Text(s)) = (plus_interval(l), r) {
            if let Ok(c) =
                crate::conversions::coerce_value(Value::text(s.as_ref()), DataType::Interval, "", 0)
            {
                return apply_binary_calendar(op, l, &c);
            }
        }
    }
    // Most-specific cases first — DATE-DATE / TS-TS subtraction before
    // DATE-integer subtraction, otherwise the latter swallows the
    // former with an `int_value(Date) = None` no-op fall-through.
    match (l, r) {
        (Value::Date(a), Value::Date(b)) if op == BinOp::Sub => {
            // PG: date - date → integer (int4) day count, not bigint.
            let days = i64::from(*a) - i64::from(*b);
            return i32::try_from(days).map(Value::Int).map(Some).map_err(|_| {
                EvalError::TypeMismatch {
                    detail: "DATE - DATE day count out of integer range".into(),
                }
            });
        }
        (Value::Timestamp(a), Value::Timestamp(b)) if op == BinOp::Sub => {
            // v7.39 (read01 timestamp.c) — like-signed infinities cannot
            // subtract (PG "interval out of range"); mixed-sign needs an
            // interval infinity SPG's Interval can't represent yet
            // (recorded delta) so it errors the same way.
            let a_inf = *a == i64::MAX || *a == i64::MIN;
            let b_inf = *b == i64::MAX || *b == i64::MIN;
            if a_inf || b_inf {
                return Err(EvalError::TypeMismatch {
                    detail: "interval out of range".into(),
                });
            }
            // PG: timestamp - timestamp -> interval, justified to hours (every
            // 24h of the microsecond delta becomes one day). 30h -> `1 day
            // 06:00:00`, not a raw microsecond count.
            let delta = a.checked_sub(*b).ok_or_else(|| EvalError::TypeMismatch {
                detail: "interval out of range".into(),
            })?;
            const DAY_US: i64 = 86_400_000_000;
            let days = i32::try_from(delta / DAY_US).map_err(|_| EvalError::TypeMismatch {
                detail: "TIMESTAMP - TIMESTAMP day count overflows".into(),
            })?;
            return Ok(Some(Value::Interval {
                months: 0,
                days,
                micros: delta % DAY_US,
            }));
        }
        (Value::Time(a), Value::Time(b)) if op == BinOp::Sub => {
            // PG: time - time -> interval, the signed microsecond difference
            // (always within a day, so no day component). 10:30 - 08:15 ->
            // 02:15:00.
            return Ok(Some(Value::Interval {
                months: 0,
                days: 0,
                micros: a - b,
            }));
        }
        _ => {}
    }
    // INTERVAL arithmetic. PG: timestamp ± interval → timestamp,
    // date ± interval → date (if interval is pure days/months with no
    // sub-day component) else timestamp, interval ± interval → interval.
    if let Some(out) = apply_binary_interval(op, l, r)? {
        return Ok(Some(out));
    }
    match (l, r) {
        // v7.37 D.35 — `date + time` (and `time + date`) → timestamp at that
        // date's midnight plus the time-of-day. PG: date '2024-06-15' + time
        // '10:30' → 2024-06-15 10:30:00.
        (Value::Date(d), Value::Time(t)) | (Value::Time(t), Value::Date(d)) if op == BinOp::Add => {
            let micros = i64::from(*d)
                .checked_mul(86_400_000_000)
                .and_then(|day_us| day_us.checked_add(*t))
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "DATE + TIME overflows TIMESTAMP range".into(),
                })?;
            return Ok(Some(Value::Timestamp(micros)));
        }
        (Value::Date(d), other) if op == BinOp::Add => {
            if let Some(n) = int_value(other) {
                let days = i64::from(*d).saturating_add(n);
                let days32 = i32::try_from(days).map_err(|_| EvalError::TypeMismatch {
                    detail: "DATE + integer overflows DATE range".into(),
                })?;
                return Ok(Some(Value::Date(days32)));
            }
        }
        (other, Value::Date(d)) if op == BinOp::Add => {
            if let Some(n) = int_value(other) {
                let days = i64::from(*d).saturating_add(n);
                let days32 = i32::try_from(days).map_err(|_| EvalError::TypeMismatch {
                    detail: "integer + DATE overflows DATE range".into(),
                })?;
                return Ok(Some(Value::Date(days32)));
            }
        }
        (Value::Date(d), other) if op == BinOp::Sub => {
            if let Some(n) = int_value(other) {
                let days = i64::from(*d).saturating_sub(n);
                let days32 = i32::try_from(days).map_err(|_| EvalError::TypeMismatch {
                    detail: "DATE - integer overflows DATE range".into(),
                })?;
                return Ok(Some(Value::Date(days32)));
            }
        }
        _ => {}
    }

    Ok(None)
}

/// INTERVAL-aware binary ops. Recognises:
///   timestamp ± interval → timestamp
///   date ± interval      → date (if interval is integral days/months only)
///                       → timestamp (if interval has sub-day micros)
///   interval ± interval  → interval
/// Commutative for `+`. Returns `None` for unrecognised operand pairs so
/// the caller can fall through.
pub(crate) fn apply_binary_interval(
    op: BinOp,
    l: &Value<'static>,
    r: &Value<'static>,
) -> Result<Option<Value<'static>>, EvalError> {
    // interval * numeric (either order) and interval / numeric scale
    // the interval per PG's interval_mul: months and days truncate
    // toward zero and their fractional remainders spill downward
    // (fractional month → 30 days, fractional day → microseconds).
    match (l, r, op) {
        (Value::Interval { .. }, other, BinOp::Mul)
        | (other, Value::Interval { .. }, BinOp::Mul)
            if as_f64(other).is_ok() =>
        {
            let iv = if matches!(l, Value::Interval { .. }) {
                l
            } else {
                r
            };
            return scale_interval(iv, as_f64(other)?).map(Some);
        }
        (Value::Interval { .. }, other, BinOp::Div) if as_f64(other).is_ok() => {
            let divisor = as_f64(other)?;
            if divisor == 0.0 {
                return Err(EvalError::DivisionByZero);
            }
            return scale_interval(l, 1.0 / divisor).map(Some);
        }
        _ => {}
    }
    // Normalise so the interval (if any) is always on the right for Add;
    // Sub stays left-handed because it isn't commutative.
    let (lhs, rhs, sign): (&Value<'static>, &Value<'static>, i64) = match (l, r, op) {
        (Value::Interval { .. }, _, BinOp::Add) => (r, l, 1),
        (_, Value::Interval { .. }, BinOp::Add) => (l, r, 1),
        (_, Value::Interval { .. }, BinOp::Sub) => (l, r, -1),
        _ => return Ok(None),
    };
    let Value::Interval {
        months: rhs_months,
        days: rhs_days,
        micros: rhs_us,
    } = rhs
    else {
        unreachable!("rhs guaranteed to be Interval by the match above");
    };
    let signed_months = i64::from(*rhs_months) * sign;
    let signed_days = i64::from(*rhs_days) * sign;
    let signed_micros = rhs_us
        .checked_mul(sign)
        .ok_or_else(|| EvalError::TypeMismatch {
            detail: "INTERVAL micros overflows on negation".into(),
        })?;
    match lhs {
        // TIME ± INTERVAL wraps within the day (PG semantics): only
        // the sub-day microseconds apply, and the result is taken
        // modulo 24 hours so it stays a valid time of day.
        Value::Time(t) => {
            const DAY_US: i64 = 86_400_000_000;
            let shifted = t
                .checked_add(signed_micros)
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "TIME ± INTERVAL overflows i64 microseconds".into(),
                })?;
            Ok(Some(Value::Time(shifted.rem_euclid(DAY_US))))
        }
        Value::Timestamp(t) => Ok(Some(Value::Timestamp(add_interval_to_micros(
            *t,
            signed_months,
            signed_days,
            signed_micros,
        )?))),
        Value::Date(d) => {
            // PG: `date ± interval` ALWAYS yields TIMESTAMP (rendered at
            // midnight when the interval has no sub-day part), because the
            // interval may carry a time component. `date ± integer` stays a
            // date, but that is a different operator handled elsewhere.
            let base = i64::from(*d).checked_mul(86_400_000_000).ok_or_else(|| {
                EvalError::TypeMismatch {
                    detail: "DATE → TIMESTAMP lift overflows for INTERVAL math".into(),
                }
            })?;
            Ok(Some(Value::Timestamp(add_interval_to_micros(
                base,
                signed_months,
                signed_days,
                signed_micros,
            )?)))
        }
        Value::Interval {
            months: lhs_months,
            days: lhs_days,
            micros: lhs_us,
        } => {
            let new_months = i64::from(*lhs_months)
                .checked_add(signed_months)
                .and_then(|n| i32::try_from(n).ok())
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "INTERVAL ± INTERVAL months overflows i32".into(),
                })?;
            let raw_days = i64::from(*lhs_days)
                .checked_add(signed_days)
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "INTERVAL ± INTERVAL days overflows i64".into(),
                })?;
            let raw_micros =
                lhs_us
                    .checked_add(signed_micros)
                    .ok_or_else(|| EvalError::TypeMismatch {
                        detail: "INTERVAL ± INTERVAL micros overflows i64".into(),
                    })?;
            // v7.38 (read01) — PG interval arithmetic is PURELY component-wise:
            // it does NOT justify days ↔ micros, so `1 day - 2 hours` stays
            // `1 day -02:00:00` (mixed sign) and `1 day - 26 hours` stays
            // `1 day -26:00:00` (micros beyond a day). Months, days and micros
            // each subtract independently; only explicit justify_* reshuffles
            // them. (An earlier fix normalised at the day boundary, mistaking
            // PG's `1 day -12:00:00` for an artefact — it is PG's real output.)
            let new_days = i32::try_from(raw_days).map_err(|_| EvalError::TypeMismatch {
                detail: "INTERVAL ± INTERVAL day count exceeds i32".into(),
            })?;
            Ok(Some(Value::Interval {
                months: new_months,
                days: new_days,
                micros: raw_micros,
            }))
        }
        _ => Err(EvalError::TypeMismatch {
            detail: format!(
                "operator {op:?} not defined for {} and INTERVAL",
                crate::conversions::pg_type_name_for_error_opt(lhs.data_type())
            ),
        }),
    }
}

/// Scale an interval by a float factor per PG's `interval_mul`
/// (timestamp.c): months and days truncate toward zero; the
/// fractional month remainder spills into days at 30 days/month and
/// the fractional day remainder spills into microseconds.
fn scale_interval(iv: &Value<'static>, factor: f64) -> Result<Value<'static>, EvalError> {
    let Value::Interval {
        months,
        days,
        micros,
    } = iv
    else {
        unreachable!("caller guarantees Interval");
    };
    if !factor.is_finite() {
        return Err(EvalError::TypeMismatch {
            detail: "INTERVAL scale factor must be finite".into(),
        });
    }
    let m = f64::from(*months) * factor;
    let new_months = m.trunc();
    let d = f64::from(*days) * factor + (m - new_months) * 30.0;
    let new_days = d.trunc();
    let new_micros = (*micros as f64) * factor + (d - new_days) * 86_400_000_000.0;
    let months_ok = new_months >= f64::from(i32::MIN) && new_months <= f64::from(i32::MAX);
    let days_ok = new_days >= f64::from(i32::MIN) && new_days <= f64::from(i32::MAX);
    let micros_ok = (-9.3e18..=9.3e18).contains(&new_micros);
    if !(months_ok && days_ok && micros_ok) {
        return Err(EvalError::TypeMismatch {
            detail: "INTERVAL out of range after scaling".into(),
        });
    }
    #[allow(clippy::cast_possible_truncation)]
    Ok(Value::Interval {
        months: new_months as i32,
        days: new_days as i32,
        micros: libm::rint(new_micros) as i64,
    })
}

/// Shift a `Date` by a signed number of months using the PG clamp rule.
fn shift_date_by_months(d: i32, months: i64) -> Result<i32, EvalError> {
    let (y, m, day) = civil_from_days(d);
    let months_i32 = i32::try_from(months).map_err(|_| EvalError::TypeMismatch {
        detail: "INTERVAL months delta out of i32 range".into(),
    })?;
    let (ny, nm, nd) = add_months_to_civil(y, m, day, months_i32);
    Ok(days_from_civil(ny, nm, nd))
}

/// Add (months, days, micros) to a `Timestamp` (microseconds since
/// epoch). Months part is applied through civil calendar with
/// clamp-to-last-day; days part is a plain 86_400_000_000 micros each
/// (DST-naïve at the TIMESTAMP level, matching PG); micros part is
/// plain i64 addition with overflow guard. v7.37.5 β added the
/// `days` parameter.
pub(crate) fn add_interval_to_micros(
    t: i64,
    months: i64,
    days: i64,
    micros: i64,
) -> Result<i64, EvalError> {
    const MICROS_PER_DAY: i64 = 86_400_000_000;
    // v7.39 (read01 timestamp.c) — an infinite timestamp absorbs any
    // finite interval (PG: infinity + '1 day' = infinity).
    if t == i64::MAX || t == i64::MIN {
        return Ok(t);
    }
    let mut out = t;
    if months != 0 {
        let day_count = out.div_euclid(MICROS_PER_DAY);
        let day_micros = out.rem_euclid(MICROS_PER_DAY);
        let day_i32 = i32::try_from(day_count).map_err(|_| EvalError::TypeMismatch {
            detail: "TIMESTAMP day component out of i32 range for INTERVAL months math".into(),
        })?;
        let shifted_days = shift_date_by_months(day_i32, months)?;
        out = i64::from(shifted_days)
            .checked_mul(MICROS_PER_DAY)
            .and_then(|n| n.checked_add(day_micros))
            .ok_or_else(|| EvalError::TypeMismatch {
                detail: "TIMESTAMP ± INTERVAL months overflows i64 microseconds".into(),
            })?;
    }
    if days != 0 {
        let day_micros =
            days.checked_mul(MICROS_PER_DAY)
                .ok_or_else(|| EvalError::TypeMismatch {
                    detail: "INTERVAL days overflows i64 microseconds".into(),
                })?;
        out = out
            .checked_add(day_micros)
            .ok_or_else(|| EvalError::TypeMismatch {
                detail: "TIMESTAMP ± INTERVAL days overflows i64".into(),
            })?;
    }
    let out = out
        .checked_add(micros)
        .ok_or_else(|| EvalError::TypeMismatch {
            detail: "timestamp out of range".into(),
        })?;
    // v7.39 (read01 timestamp.c) — PG's lower bound (4714-11-24 BC,
    // Unix-epoch microseconds); arithmetic below it errors like PG.
    // The upper bound is i64 itself (checked adds above) — SPG's
    // 1970-based clock ends ~30 years before PG's 2000-based ceiling,
    // a recorded delta at the extreme fringe.
    const TS_MIN: i64 = -210_866_803_200_000_000;
    if out < TS_MIN {
        return Err(EvalError::TypeMismatch {
            detail: "timestamp out of range".into(),
        });
    }
    Ok(out)
}

/// Dispatch for any binary op when at least one operand is NUMERIC.
/// Other-side integers / floats are promoted to a NUMERIC at a common
/// scale; all add / sub / mul / div / compare paths stay in i128.
#[allow(clippy::needless_pass_by_value)] // mirrors `apply_binary`'s by-value calling convention
/// v7.38 (read01) — intersection point of two closed line segments, or `None`
/// when they are parallel or do not overlap within both segments' extents.
fn lseg_intersection(
    a1: spg_storage::Point2D,
    a2: spg_storage::Point2D,
    b1: spg_storage::Point2D,
    b2: spg_storage::Point2D,
) -> Option<spg_storage::Point2D> {
    let d1x = a2.x - a1.x;
    let d1y = a2.y - a1.y;
    let d2x = b2.x - b1.x;
    let d2y = b2.y - b1.y;
    let denom = d1x * d2y - d1y * d2x;
    if denom == 0.0 {
        return None; // parallel or degenerate
    }
    let t = ((b1.x - a1.x) * d2y - (b1.y - a1.y) * d2x) / denom;
    let s = ((b1.x - a1.x) * d1y - (b1.y - a1.y) * d1x) / denom;
    if (0.0..=1.0).contains(&t) && (0.0..=1.0).contains(&s) {
        Some(spg_storage::Point2D {
            x: a1.x + t * d1x,
            y: a1.y + t * d1y,
        })
    } else {
        None
    }
}

/// v7.38 (read01, T6.P3) — the NUMERIC kind of a numeric-family operand, or None
/// for a non-numeric value (which falls through to the ordinary type path).
fn numeric_kind_of(v: &Value) -> Option<spg_storage::NumericKind> {
    use spg_storage::NumericKind::Finite;
    match v {
        Value::Numeric { kind, .. } => Some(*kind),
        Value::Int(_) | Value::BigInt(_) | Value::SmallInt(_) => Some(Finite),
        _ => None,
    }
}

fn finite_sign_of(v: &Value) -> i32 {
    match v {
        Value::Numeric { scaled, .. } => (*scaled).signum() as i32,
        Value::Int(n) => n.signum(),
        Value::BigInt(n) => (*n).signum() as i32,
        Value::SmallInt(n) => i32::from(*n).signum(),
        _ => 0,
    }
}

/// v7.38 (read01, T6.P3) — PG's NUMERIC special-value arithmetic. Returns
/// `Some` when a NaN/±Infinity operand determines the result (verified vs PG:
/// NaN wins over everything incl. div-by-zero; `Inf/0` still errors; `Inf-Inf` /
/// `Inf*0` / `Inf/Inf` → NaN; `finite/Inf` → 0; `finite%Inf` → the dividend;
/// `Inf%x` → NaN; sign multiplies). `None` falls through to the finite path.
fn numeric_special_result(
    op: BinOp,
    l: &Value<'static>,
    r: &Value<'static>,
) -> Option<Result<Value<'static>, EvalError>> {
    use spg_storage::NumericKind as NK;
    let (lk, rk) = (numeric_kind_of(l)?, numeric_kind_of(r)?);
    if lk == NK::Finite && rk == NK::Finite {
        return None;
    }
    if !matches!(
        op,
        BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod
    ) {
        return None;
    }
    let nan = || Ok(Value::numeric_special(NK::NaN));
    // NaN wins over everything, including division by zero.
    if lk == NK::NaN || rk == NK::NaN {
        return Some(nan());
    }
    // After NaN: a zero divisor still errors (PG: `Infinity / 0` errors too).
    let r_is_zero = rk == NK::Finite && finite_sign_of(r) == 0;
    if matches!(op, BinOp::Div | BinOp::Mod) && r_is_zero {
        return Some(Err(EvalError::DivisionByZero));
    }
    let sign = |k: NK, v: &Value<'static>| -> i32 {
        match k {
            NK::PosInf => 1,
            NK::NegInf => -1,
            _ => finite_sign_of(v),
        }
    };
    let (ls, rs) = (sign(lk, l), sign(rk, r));
    let (l_inf, r_inf) = (
        matches!(lk, NK::PosInf | NK::NegInf),
        matches!(rk, NK::PosInf | NK::NegInf),
    );
    let inf = |s: i32| Value::numeric_special(if s < 0 { NK::NegInf } else { NK::PosInf });
    let l_zero = lk == NK::Finite && ls == 0;
    let res = match op {
        BinOp::Add => {
            if l_inf && r_inf {
                if ls == rs {
                    inf(ls)
                } else {
                    return Some(nan());
                }
            } else if l_inf {
                inf(ls)
            } else {
                inf(rs)
            }
        }
        BinOp::Sub => {
            let rs2 = -rs;
            if l_inf && r_inf {
                if ls == rs2 {
                    inf(ls)
                } else {
                    return Some(nan());
                }
            } else if l_inf {
                inf(ls)
            } else {
                inf(rs2)
            }
        }
        BinOp::Mul => {
            if (l_inf && r_is_zero) || (r_inf && l_zero) {
                return Some(nan());
            }
            inf(ls * rs)
        }
        BinOp::Div => {
            if l_inf && r_inf {
                return Some(nan());
            } else if l_inf {
                inf(ls * rs)
            } else {
                Value::numeric(0, 0) // finite / Inf → 0
            }
        }
        BinOp::Mod => {
            if l_inf {
                return Some(nan()); // Inf % x → NaN
            }
            l.clone().into_owned() // finite % Inf → the dividend
        }
        _ => unreachable!(),
    };
    Some(Ok(res))
}

/// v7.38 (read01, T3.C3) — convert a numeric-family value to `BigNumeric` for
/// the arbitrary-precision fallback. `None` for a non-numeric.
pub(crate) fn value_to_bignum(v: &Value) -> Option<spg_storage::bignum::BigNumeric> {
    use spg_storage::bignum::BigNumeric;
    Some(match v {
        Value::SmallInt(n) => BigNumeric::from_i128(i128::from(*n), 0),
        Value::Int(n) => BigNumeric::from_i128(i128::from(*n), 0),
        Value::BigInt(n) => BigNumeric::from_i128(i128::from(*n), 0),
        Value::Numeric {
            scaled,
            scale,
            kind,
        } if *kind == spg_storage::NumericKind::Finite => BigNumeric::from_i128(*scaled, *scale),
        Value::NumericBig(b) => (**b).clone(),
        _ => return None,
    })
}

/// Collapse a `BigNumeric` back to `Value::Numeric` when its mantissa fits
/// `i128`, else keep the big form.
pub(crate) fn bignum_to_value(b: spg_storage::bignum::BigNumeric) -> Value<'static> {
    match b.to_i128() {
        Some(scaled) => Value::Numeric {
            scaled,
            scale: b.scale(),
            kind: spg_storage::NumericKind::Finite,
        },
        None => Value::NumericBig(alloc::boxed::Box::new(b)),
    }
}

/// v7.38 (read01, T3.C3) — arbitrary-precision fallback for a numeric op whose
/// i128 fast path overflowed, or where an operand is already `NumericBig`.
fn numeric_big_op(op: BinOp, l: &Value, r: &Value) -> Option<Result<Value<'static>, EvalError>> {
    let (a, b) = (value_to_bignum(l)?, value_to_bignum(r)?);
    // Comparison honors the exact big values (sign + scale-aligned magnitude).
    if matches!(
        op,
        BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq
    ) {
        return Some(Ok(Value::Bool(cmp_to_bool(op, a.cmp(&b)))));
    }
    // Division carries PG's display scale (~16 significant digits, floored by
    // the dividend's scale); a zero divisor errors like the i128 path.
    if matches!(op, BinOp::Div) {
        if b.is_zero() {
            return Some(Err(EvalError::DivisionByZero));
        }
        let rscale = crate::numeric::division_display_scale_big(&a, &b);
        return Some(Ok({
            let q = a.div(&b, rscale)?;
            bignum_to_value(q)
        }));
    }
    // Modulo of two big integers (scale 0) via truncating integer div_rem;
    // a scaled big remainder is rare enough to leave to the type error.
    if matches!(op, BinOp::Mod) {
        if b.is_zero() {
            return Some(Err(EvalError::DivisionByZero));
        }
        if a.scale() == 0 && b.scale() == 0 {
            let (_, rem) = a.div_rem_int(&b);
            return Some(Ok(bignum_to_value(rem)));
        }
        return None;
    }
    let res = match op {
        BinOp::Add => a.add(&b),
        BinOp::Sub => a.sub(&b),
        BinOp::Mul => a.mul(&b),
        _ => return None,
    };
    Some(Ok(bignum_to_value(res)))
}

fn apply_binary_numeric(
    op: BinOp,
    l: Value<'static>,
    r: Value<'static>,
) -> Result<Value<'static>, EvalError> {
    // v7.39 (round 353, M9) — `DIV` reads a NUMERIC operand as a number
    // like every other one (`7.5 DIV 2` is 3, measured); it has no
    // fixed-point form of its own to compute here.
    if matches!(op, BinOp::IntDiv) {
        return int_div_op(&l, &r);
    }
    // v7.38 (read01, T3.C3) — an already-big operand skips the i128 fast path.
    if matches!(l, Value::NumericBig(_)) || matches!(r, Value::NumericBig(_)) {
        if let Some(res) = numeric_big_op(op, &l, &r) {
            return res;
        }
    }
    // v7.38 (read01) — an unknown-type string operand against NUMERIC coerces to
    // numeric (PG): `3.5::numeric = '3.5'`, `n <op> '2'`. Do it up front so both
    // the comparison and arithmetic paths see two numerics; a string that isn't
    // a valid numeric falls through to the type error (matching PG `n = 'abc'`).
    // Concat is the exception — `numeric || text` is text concatenation.
    if !matches!(op, BinOp::Concat) {
        let to_num = |v: &Value<'static>| -> Option<Value<'static>> {
            match v {
                Value::Text(s) => crate::conversions::coerce_value(
                    Value::text(s.as_ref()),
                    spg_storage::DataType::Numeric {
                        precision: 0,
                        scale: 0,
                    },
                    "",
                    0,
                )
                .ok(),
                _ => None,
            }
        };
        if let Some(nl) = to_num(&l) {
            return apply_binary_numeric(op, nl, r);
        }
        if let Some(nr) = to_num(&r) {
            return apply_binary_numeric(op, l, nr);
        }
    }
    // v7.38 (read01, T6.P3) — a NaN / ±Infinity operand determines the result
    // before the finite fixed-point path (arithmetic ops only; comparison of
    // specials is handled in the compare arm / cmp_numeric).
    if let Some(res) = numeric_special_result(op, &l, &r) {
        return res;
    }
    // Float still wins — Numeric + Float coerces both to f64 and runs
    // through the float path. PG demotes Numeric to float in this mix
    // too (the documented behaviour for `numeric + double precision`).
    // v7.38 (read01, T-float4) — `real op {real,int}` stays real (compute in
    // f64, narrow to f32); a float8 / numeric operand widens to the float path.
    let has_real = matches!(l, Value::Real(_)) || matches!(r, Value::Real(_));
    let has_float = matches!(l, Value::Float(_)) || matches!(r, Value::Float(_));
    let has_numeric = matches!(l, Value::Numeric { .. } | Value::NumericBig(_))
        || matches!(r, Value::Numeric { .. } | Value::NumericBig(_));
    let both_real = matches!(l, Value::Real(_)) && matches!(r, Value::Real(_));
    if both_real
        && matches!(
            op,
            BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod
        )
    {
        let af = as_f64(&l)?;
        let bf = as_f64(&r)?;
        return Ok(Value::Real(match op {
            BinOp::Add => (af + bf) as f32,
            BinOp::Sub => (af - bf) as f32,
            BinOp::Mul => (af * bf) as f32,
            BinOp::Div => {
                if bf == 0.0 {
                    return Err(EvalError::DivisionByZero);
                }
                (af / bf) as f32
            }
            BinOp::Mod => {
                if bf == 0.0 {
                    return Err(EvalError::DivisionByZero);
                }
                (af % bf) as f32
            }
            _ => unreachable!(),
        }));
    }
    let float_path = has_float || has_real;
    if float_path {
        let af = as_f64(&l)?;
        let bf = as_f64(&r)?;
        return match op {
            BinOp::Add => Ok(Value::Float(check_float8_range(af + bf, af, bf, "+")?)),
            BinOp::Sub => Ok(Value::Float(check_float8_range(af - bf, af, bf, "-")?)),
            BinOp::Mul => Ok(Value::Float(check_float8_range(af * bf, af, bf, "*")?)),
            BinOp::Div => {
                if bf == 0.0 {
                    Err(EvalError::DivisionByZero)
                } else {
                    Ok(Value::Float(check_float8_range(af / bf, af, bf, "/")?))
                }
            }
            BinOp::Mod => {
                if bf == 0.0 {
                    Err(EvalError::DivisionByZero)
                } else {
                    Ok(Value::Float(af % bf))
                }
            }
            BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq => {
                // PG's total float order: NaN == NaN and NaN > any number,
                // so the comparison is total (never errors on NaN).
                let ord = float_pg_cmp(af, bf);
                Ok(Value::Bool(cmp_to_bool(op, ord)))
            }
            BinOp::Concat => Ok(text_concat(&l, &r)),
            other => Err(EvalError::TypeMismatch {
                detail: format!("operator {other:?} not defined for NUMERIC and Float"),
            }),
        };
    }
    // v7.37 D.34 — `numeric || anything` / `anything || numeric` is text
    // concatenation (PG has no `||` operator on numeric); handle it before
    // numeric_or_widen, which rejects a non-numeric operand. Previously
    // `'x' || 1.5::numeric` errored "NUMERIC op against non-numeric Text"
    // because the numeric fast-path claimed the expression but couldn't widen
    // the text side. (The float path already special-cases Concat this way.)
    if matches!(op, BinOp::Concat) {
        return Ok(text_concat(&l, &r));
    }
    // v7.38 (read01, T6.P3) — comparison involving a NUMERIC special uses the
    // total order -Inf < finite < +Inf < NaN (NaN == NaN). Handled before the
    // finite widen, which would read a special's canonical 0 as the number 0.
    if matches!(
        op,
        BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq
    ) {
        use spg_storage::NumericKind as NK;
        if let (Some(lk), Some(rk)) = (numeric_kind_of(&l), numeric_kind_of(&r)) {
            if lk != NK::Finite || rk != NK::Finite {
                let rank = |k: NK| match k {
                    NK::NegInf => -2,
                    NK::Finite => 0,
                    NK::PosInf => 1,
                    NK::NaN => 2,
                };
                // At least one side is special, so the ranks alone order the pair
                // (two finites never reach here).
                let ord = rank(lk).cmp(&rank(rk));
                return Ok(Value::Bool(cmp_to_bool(op, ord)));
            }
        }
    }
    // Promote integer ↔ numeric to a shared scale (max of both sides).
    let (a, sa) = numeric_or_widen(&l).ok_or_else(|| EvalError::TypeMismatch {
        detail: format!(
            "NUMERIC op against non-numeric {}",
            crate::conversions::pg_type_name_for_error_opt(l.data_type())
        ),
    })?;
    let (b, sb) = numeric_or_widen(&r).ok_or_else(|| EvalError::TypeMismatch {
        detail: format!(
            "NUMERIC op against non-numeric {}",
            crate::conversions::pg_type_name_for_error_opt(r.data_type())
        ),
    })?;
    match op {
        BinOp::Add | BinOp::Sub => {
            let target_scale = sa.max(sb);
            // v7.38 (read01, T3.C3) — try the exact i128 path; any overflow
            // (rescale or the add/sub) promotes to arbitrary precision.
            let fast = rescale(a, sa, target_scale).and_then(|lhs| {
                rescale(b, sb, target_scale).and_then(|rhs| match op {
                    BinOp::Add => lhs.checked_add(rhs),
                    BinOp::Sub => lhs.checked_sub(rhs),
                    _ => unreachable!(),
                })
            });
            match fast {
                Some(r) => Ok(Value::Numeric {
                    scaled: r,
                    scale: target_scale,
                    kind: spg_storage::NumericKind::Finite,
                }),
                None => numeric_big_op(op, &l, &r).expect("numeric operands"),
            }
        }
        BinOp::Mod => {
            // PG `numeric % numeric`: rescale both to the shared scale, then
            // take the truncated remainder (sign of the dividend). Result keeps
            // that scale. 12.5 % 5 -> 125 % 50 = 25 @scale1 = 2.5.
            let target_scale = sa.max(sb);
            let lhs = rescale(a, sa, target_scale).ok_or_else(|| EvalError::TypeMismatch {
                detail: "NUMERIC overflow on rescale".into(),
            })?;
            let rhs = rescale(b, sb, target_scale).ok_or_else(|| EvalError::TypeMismatch {
                detail: "NUMERIC overflow on rescale".into(),
            })?;
            if rhs == 0 {
                return Err(EvalError::DivisionByZero);
            }
            Ok(Value::Numeric {
                scaled: lhs.wrapping_rem(rhs),
                scale: target_scale,
                kind: spg_storage::NumericKind::Finite,
            })
        }
        BinOp::Mul => {
            // v7.38 (read01, T3.C3) — i128 product overflow promotes to bignum.
            match a.checked_mul(b) {
                Some(scaled) => Ok(Value::Numeric {
                    scaled,
                    scale: sa.saturating_add(sb),
                    kind: spg_storage::NumericKind::Finite,
                }),
                None => numeric_big_op(op, &l, &r).expect("numeric operands"),
            }
        }
        BinOp::Div => {
            if b == 0 {
                return Err(EvalError::DivisionByZero);
            }
            // `numeric / numeric` picks the result scale from
            // `division_display_scale`, which keeps ~16 significant digits
            // (matching PG's observable division scale), so `10::numeric / 3`
            // yields 16 fractional digits (3.3333333333333333) rather than
            // truncating to the operands' scale-0 (which silently gave `3`).
            // v7.38 (read01, T3.C3) — when the scaled division overflows i128
            // (large dividend × 10^rscale), promote to the arbitrary-precision
            // path rather than erroring.
            match crate::numeric::numeric_div(a, sa, b, sb) {
                Some((scaled, scale)) => Ok(Value::Numeric {
                    scaled,
                    scale,
                    kind: spg_storage::NumericKind::Finite,
                }),
                None => numeric_big_op(op, &l, &r).expect("numeric operands"),
            }
        }
        BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq => {
            let target_scale = sa.max(sb);
            let lhs = rescale(a, sa, target_scale).ok_or_else(|| EvalError::TypeMismatch {
                detail: "NUMERIC overflow on rescale".into(),
            })?;
            let rhs = rescale(b, sb, target_scale).ok_or_else(|| EvalError::TypeMismatch {
                detail: "NUMERIC overflow on rescale".into(),
            })?;
            Ok(Value::Bool(cmp_to_bool(op, lhs.cmp(&rhs))))
        }
        BinOp::Concat => Ok(text_concat(&l, &r)),
        other => Err(EvalError::TypeMismatch {
            detail: format!("operator {other:?} not defined for NUMERIC"),
        }),
    }
}

/// Express `v` as a `(scaled_i128, scale)` pair. Plain integers come
/// back with `scale=0`; NUMERIC keeps its own scale. Anything else
/// returns `None` and the caller raises a type error.
fn numeric_or_widen(v: &Value<'static>) -> Option<(i128, u16)> {
    match v {
        Value::Numeric { scaled, scale, .. } => Some((*scaled, *scale)),
        Value::Int(n) => Some((i128::from(*n), 0)),
        Value::SmallInt(n) => Some((i128::from(*n), 0)),
        Value::BigInt(n) => Some((i128::from(*n), 0)),
        _ => None,
    }
}

fn rescale(scaled: i128, src: u16, dst: u16) -> Option<i128> {
    if src == dst {
        return Some(scaled);
    }
    // v7.39 (round 271) — with `scale` widened to u16 the power can
    // exceed i128. Returning None here drops the caller onto the
    // BigNumeric path it already has; the old unchecked `pow10_i128`
    // panicked and aborted the query.
    if dst > src {
        scaled.checked_mul(pow10_i128_checked(dst - src)?)
    } else {
        let drop = pow10_i128_checked(src - dst)?;
        let half = drop / 2;
        let r = if scaled >= 0 {
            scaled + half
        } else {
            scaled - half
        };
        Some(r / drop)
    }
}

pub(super) const fn pow10_i128(p: u16) -> i128 {
    match pow10_i128_checked(p) {
        Some(v) => v,
        None => i128::MAX,
    }
}

/// `10^p`, or None once it leaves the i128 range.
const fn pow10_i128_checked(p: u16) -> Option<i128> {
    let mut acc: i128 = 1;
    let mut i = 0;
    while i < p {
        match acc.checked_mul(10) {
            Some(v) => acc = v,
            None => return None,
        }
        i += 1;
    }
    Some(acc)
}

const fn cmp_to_bool(op: BinOp, ord: core::cmp::Ordering) -> bool {
    use core::cmp::Ordering::{Equal, Greater, Less};
    match op {
        BinOp::Eq => matches!(ord, Equal),
        BinOp::NotEq => !matches!(ord, Equal),
        BinOp::Lt => matches!(ord, Less),
        BinOp::LtEq => matches!(ord, Less | Equal),
        BinOp::Gt => matches!(ord, Greater),
        BinOp::GtEq => matches!(ord, Greater | Equal),
        _ => false,
    }
}

/// SQL `||` string concatenation. Operands are coerced to text via the same
/// rule as `::text` cast. NULL propagates (handled above; this function only
/// runs with non-NULL operands).
/// v7.24 (round-16 C) — `tsvector || tsvector`. PG semantics: the
/// right side's positions shift by the left side's max position;
/// lexemes present on both sides merge (positions concatenated,
/// the higher weight wins — SPG models weight per lexeme, PG per
/// position, so the stronger label is the faithful collapse).
/// Concatenate two MSB-packed bit strings (`bit || bit`): copy `a`'s
/// `an` bits, then append `b`'s `bn` bits starting at bit offset `an`.
/// Returns `(total_bits, packed_bytes)`.
fn bit_concat(an: u32, ab: &[u8], bn: u32, bb: &[u8]) -> (u32, alloc::vec::Vec<u8>) {
    let total = an + bn;
    let mut out = alloc::vec![0u8; ((total + 7) / 8) as usize];
    let get = |src: &[u8], i: u32| -> bool {
        src.get((i / 8) as usize)
            .is_some_and(|&byte| byte & (0x80 >> (i % 8)) != 0)
    };
    for i in 0..an {
        if get(ab, i) {
            out[(i / 8) as usize] |= 0x80 >> (i % 8);
        }
    }
    for i in 0..bn {
        if get(bb, i) {
            let p = an + i;
            out[(p / 8) as usize] |= 0x80 >> (p % 8);
        }
    }
    (total, out)
}

fn text_concat<'a>(l: &'a Value<'static>, r: &'a Value<'static>) -> Value<'static> {
    // v7.38 (read01, T11) — bpchar concatenates with its trailing blanks
    // removed (`'ab'::char(4) || 'x'` = `abx`).
    if matches!(l, Value::BpChar(_)) || matches!(r, Value::BpChar(_)) {
        let norm = |v: &Value<'static>| -> Value<'static> {
            match v {
                Value::BpChar(s) => Value::text(s.trim_end_matches(' ').to_string()),
                other => other.clone(),
            }
        };
        return text_concat(&norm(l), &norm(r));
    }
    if let (Value::TsVector(a), Value::TsVector(b)) = (l, r) {
        return tsvector_concat(a, b);
    }
    // PG `bit || bit` = a `bit varying` of length nbits(a)+nbits(b). Both
    // operands are MSB-packed and zero-padded, so the second string's bits
    // must be shifted to start at bit offset nbits(a), not byte-appended.
    if let (
        Value::BitString {
            nbits: an,
            bytes: ab,
        },
        Value::BitString {
            nbits: bn,
            bytes: bb,
        },
    ) = (l, r)
    {
        let (total, out) = bit_concat(*an, ab, *bn, bb);
        return Value::BitString {
            nbits: total,
            bytes: alloc::borrow::Cow::Owned(out),
        };
    }
    // v7.11.8 — PG `||` overloads: TEXT[] || TEXT[] = concatenated array;
    // TEXT[] || TEXT (or TEXT || TEXT[]) prepends/appends the single
    // element. NULL || anything = NULL (PG semantics for arrays;
    // text concat treats NULL the same way after value_to_text).
    match (l, r) {
        (Value::Null, _) | (_, Value::Null) => {
            // PG text concat: NULL || x = NULL. Array concat: NULL || x = NULL.
            // Keep the legacy text path (value_to_text handles Null as ""),
            // but for arrays we surface real NULL to match PG.
            if matches!(
                l,
                Value::TextArray(_) | Value::IntArray(_) | Value::BigIntArray(_) | Value::Bytes(_)
            ) || matches!(
                r,
                Value::TextArray(_) | Value::IntArray(_) | Value::BigIntArray(_) | Value::Bytes(_)
            ) {
                return Value::Null;
            }
        }
        (Value::TextArray(a), Value::TextArray(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().cloned());
            return Value::TextArray(out);
        }
        (Value::TextArray(a), Value::Text(s)) => {
            let mut out = a.clone();
            out.push(Some(s.to_string()));
            return Value::TextArray(out);
        }
        (Value::Text(s), Value::TextArray(b)) => {
            let mut out: alloc::vec::Vec<Option<alloc::string::String>> =
                alloc::vec::Vec::with_capacity(1 + b.len());
            out.push(Some(s.to_string()));
            out.extend(b.iter().cloned());
            return Value::TextArray(out);
        }
        // v7.11.13 — IntArray / BigIntArray `||` overloads. Same
        // PG semantics as TEXT[]: array||array concatenates, and
        // array||scalar appends/prepends. Mixed Int/BigInt widens
        // to BigIntArray.
        (Value::IntArray(a), Value::IntArray(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().copied());
            return Value::IntArray(out);
        }
        // v7.39 (read01 round 108) — 2-D array `||` appends the second matrix's
        // rows to the first (`ARRAY[[1,2]] || ARRAY[[3,4]]` → `{{1,2},{3,4}}`).
        // Without these arms two matrices fell through to text_concat, giving
        // the malformed `{{1,2}}{{3,4}}`.
        (Value::IntArray2D(a), Value::IntArray2D(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().cloned());
            return Value::IntArray2D(out);
        }
        (Value::BigIntArray2D(a), Value::BigIntArray2D(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().cloned());
            return Value::BigIntArray2D(out);
        }
        (Value::TextArray2D(a), Value::TextArray2D(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().cloned());
            return Value::TextArray2D(out);
        }
        (Value::BoolArray2D(a), Value::BoolArray2D(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().cloned());
            return Value::BoolArray2D(out);
        }
        (Value::IntArray(a), Value::Int(n)) => {
            let mut out = a.clone();
            out.push(Some(*n));
            return Value::IntArray(out);
        }
        (Value::IntArray(a), Value::SmallInt(n)) => {
            let mut out = a.clone();
            out.push(Some(i32::from(*n)));
            return Value::IntArray(out);
        }
        (Value::Int(n), Value::IntArray(b)) => {
            let mut out: alloc::vec::Vec<Option<i32>> = alloc::vec::Vec::with_capacity(1 + b.len());
            out.push(Some(*n));
            out.extend(b.iter().copied());
            return Value::IntArray(out);
        }
        (Value::SmallInt(n), Value::IntArray(b)) => {
            let mut out: alloc::vec::Vec<Option<i32>> = alloc::vec::Vec::with_capacity(1 + b.len());
            out.push(Some(i32::from(*n)));
            out.extend(b.iter().copied());
            return Value::IntArray(out);
        }
        (Value::BigIntArray(a), Value::BigIntArray(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().copied());
            return Value::BigIntArray(out);
        }
        (Value::BigIntArray(a), Value::IntArray(b)) => {
            let mut out = a.clone();
            out.extend(b.iter().map(|o| o.map(i64::from)));
            return Value::BigIntArray(out);
        }
        (Value::IntArray(a), Value::BigIntArray(b)) => {
            let mut out: alloc::vec::Vec<Option<i64>> =
                a.iter().map(|o| o.map(i64::from)).collect();
            out.extend(b.iter().copied());
            return Value::BigIntArray(out);
        }
        (Value::BigIntArray(a), Value::BigInt(n)) => {
            let mut out = a.clone();
            out.push(Some(*n));
            return Value::BigIntArray(out);
        }
        (Value::BigIntArray(a), Value::Int(n)) => {
            let mut out = a.clone();
            out.push(Some(i64::from(*n)));
            return Value::BigIntArray(out);
        }
        (Value::BigIntArray(a), Value::SmallInt(n)) => {
            let mut out = a.clone();
            out.push(Some(i64::from(*n)));
            return Value::BigIntArray(out);
        }
        (Value::BigInt(n), Value::BigIntArray(b)) => {
            let mut out: alloc::vec::Vec<Option<i64>> = alloc::vec::Vec::with_capacity(1 + b.len());
            out.push(Some(*n));
            out.extend(b.iter().copied());
            return Value::BigIntArray(out);
        }
        (Value::Int(n), Value::BigIntArray(b)) => {
            let mut out: alloc::vec::Vec<Option<i64>> = alloc::vec::Vec::with_capacity(1 + b.len());
            out.push(Some(i64::from(*n)));
            out.extend(b.iter().copied());
            return Value::BigIntArray(out);
        }
        (Value::SmallInt(n), Value::BigIntArray(b)) => {
            let mut out: alloc::vec::Vec<Option<i64>> = alloc::vec::Vec::with_capacity(1 + b.len());
            out.push(Some(i64::from(*n)));
            out.extend(b.iter().copied());
            return Value::BigIntArray(out);
        }
        // v7.11.15 — BYTEA `||` is byte concatenation.
        (Value::Bytes(a), Value::Bytes(b)) => {
            let mut out = a.clone().into_owned();
            out.extend_from_slice(b);
            return Value::bytes(out);
        }
        _ => {}
    }
    // v7.39 (round 608) — `s || 'x'` allocated five times a row over 200k
    // rows where `upper(s)`, which also builds a new string, allocated one.
    // Two of those were `value_to_text` copying operands that already WERE
    // their text, and a third was the concatenation growing into them.
    // Borrow what is already text, and size the result once.
    let borrow = |v: &'a Value<'static>| -> Option<&'a str> {
        match v {
            Value::Text(s) | Value::Json(s) => Some(s.as_ref()),
            _ => None,
        }
    };
    match (borrow(l), borrow(r)) {
        (Some(a), Some(b)) => {
            let mut out = alloc::string::String::with_capacity(a.len() + b.len());
            out.push_str(a);
            out.push_str(b);
            Value::text(out)
        }
        (Some(a), None) => {
            let b = value_to_text(r);
            let mut out = alloc::string::String::with_capacity(a.len() + b.len());
            out.push_str(a);
            out.push_str(&b);
            Value::text(out)
        }
        (None, Some(b)) => {
            let a = value_to_text(l);
            let mut out = alloc::string::String::with_capacity(a.len() + b.len());
            out.push_str(&a);
            out.push_str(b);
            Value::text(out)
        }
        (None, None) => {
            let a = value_to_text(l);
            let b = value_to_text(r);
            let mut out = alloc::string::String::with_capacity(a.len() + b.len());
            out.push_str(&a);
            out.push_str(&b);
            Value::text(out)
        }
    }
}

/// pgvector inner-product `<#>`. Returns the *negative* dot product so
/// smaller still means more similar — same convention as pgvector.
fn inner_product(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    let (a, b) = unwrap_vec_pair(l, r, "<#>")?;
    let mut dot: f64 = 0.0;
    for (x, y) in a.iter().zip(b.iter()) {
        dot += f64::from(*x) * f64::from(*y);
    }
    Ok(Value::Float(-dot))
}

/// pgvector cosine distance `<=>` — `1 - (a·b) / (‖a‖ ‖b‖)`. A zero-norm
/// operand produces NaN (matches pgvector).
fn cosine_distance(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    let (a, b) = unwrap_vec_pair(l, r, "<=>")?;
    let mut dot: f64 = 0.0;
    let mut na: f64 = 0.0;
    let mut nb: f64 = 0.0;
    for (x, y) in a.iter().zip(b.iter()) {
        let xf = f64::from(*x);
        let yf = f64::from(*y);
        dot += xf * yf;
        na += xf * xf;
        nb += yf * yf;
    }
    let denom = sqrt_newton(na) * sqrt_newton(nb);
    if denom == 0.0 {
        return Ok(Value::Float(f64::NAN));
    }
    Ok(Value::Float(1.0 - dot / denom))
}

fn unwrap_vec_pair(
    l: Value<'static>,
    r: Value<'static>,
    op: &str,
) -> Result<(Vec<f32>, Vec<f32>), EvalError> {
    // v6.0.1: SQ8 cells coming through the SQL evaluator are
    // dequantised to f32 here so the existing scalar distance
    // arithmetic stays intact. HNSW kNN search continues to use
    // the asymmetric ADC variant inside `cell_to_query_metric_
    // distance` — this path only runs when a vector expression
    // lands in the evaluator (full-scan ORDER BY, SELECT
    // projection of `v <-> $1`, etc.).
    let to_f32 = |v: Value| -> Option<Vec<f32>> {
        match v {
            Value::Vector(a) => Some(a.into_owned()),
            Value::Sq8Vector(q) => Some(spg_storage::quantize::dequantize(&q)),
            // v6.0.3: bit-exact dequant for halfvec cells.
            Value::HalfVector(h) => Some(h.to_f32_vec()),
            _ => None,
        }
    };
    let l_ty = l.data_type();
    let r_ty = r.data_type();
    match (to_f32(l), to_f32(r)) {
        (Some(a), Some(b)) => {
            if a.len() != b.len() {
                return Err(EvalError::TypeMismatch {
                    detail: format!("vector dim mismatch in {op}: {} vs {}", a.len(), b.len()),
                });
            }
            Ok((a, b))
        }
        _ => Err(EvalError::TypeMismatch {
            detail: format!("{op} requires two vectors, got {l_ty:?} and {r_ty:?}"),
        }),
    }
}

/// Numeric arithmetic with widening.
/// - both `Int` → `Int` (with overflow check)
/// - `Int` op `BigInt` (either side) → `BigInt`
/// - any `Float` involved → `Float`
/// Bitwise integer op (`|` / `&`). PG defines these for integer
/// types only — SmallInt widens to Int, Int x BigInt widens to
/// BigInt, anything else is a type error (mailrs embed round-12).
/// PG `bit(n) << k` / `>> k`: shift the MSB-packed bit sequence within its
/// fixed `nbits` window, zero-filling the vacated end (bits shifted past
/// either end are dropped). A negative count reverses direction.
fn bitstring_shift(nbits: u32, bytes: &[u8], k: i64, mut left: bool) -> alloc::vec::Vec<u8> {
    let n = nbits as usize;
    let k = if k < 0 {
        left = !left;
        k.unsigned_abs() as usize
    } else {
        k as usize
    };
    let get = |i: usize| -> bool { i < n && (bytes[i / 8] >> (7 - (i % 8))) & 1 == 1 };
    let mut out = alloc::vec![0u8; n.div_ceil(8)];
    for i in 0..n {
        // shift-left: out[i] = in[i+k]; shift-right: out[i] = in[i-k].
        let src = if left {
            i.checked_add(k)
        } else {
            i.checked_sub(k)
        };
        if src.is_some_and(get) {
            out[i / 8] |= 1 << (7 - (i % 8));
        }
    }
    out
}

fn bitop(
    l: Value<'static>,
    r: Value<'static>,
    f: impl Fn(i64, i64) -> i64,
    op_name: &str,
) -> Result<Value<'static>, EvalError> {
    // PG `bit(n) & / | bit(n)`: byte-wise over equal-length bit strings.
    // Operands are MSB-packed and zero-padded, so `&`/`|` keep the padding
    // zero. Differing lengths are an error, matching PG.
    if let (
        Value::BitString {
            nbits: an,
            bytes: ab,
        },
        Value::BitString {
            nbits: bn,
            bytes: bb,
        },
    ) = (&l, &r)
    {
        if an != bn {
            // v7.39 (read01 varbit.c) — PG spells the operator out.
            let word = match op_name {
                "&" => "AND",
                "|" => "OR",
                "#" => "XOR",
                other => other,
            };
            return Err(EvalError::TypeMismatch {
                detail: format!("cannot {word} bit strings of different sizes"),
            });
        }
        let out: alloc::vec::Vec<u8> = ab
            .iter()
            .zip(bb.iter())
            .map(|(&x, &y)| f(i64::from(x), i64::from(y)) as u8)
            .collect();
        return Ok(Value::BitString {
            nbits: *an,
            bytes: alloc::borrow::Cow::Owned(out),
        });
    }
    // PG `macaddr & / | macaddr`: byte-wise over the six address octets.
    if let (Value::Macaddr(a), Value::Macaddr(b)) = (&l, &r) {
        let mut out = [0u8; 6];
        for i in 0..6 {
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            {
                out[i] = f(i64::from(a[i]), i64::from(b[i])) as u8;
            }
        }
        return Ok(Value::Macaddr(out));
    }
    // PG `macaddr8 & / | macaddr8`: byte-wise over the eight EUI-64 octets.
    if let (Value::Macaddr8(a), Value::Macaddr8(b)) = (&l, &r) {
        let mut out = [0u8; 8];
        for i in 0..8 {
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            {
                out[i] = f(i64::from(a[i]), i64::from(b[i])) as u8;
            }
        }
        return Ok(Value::Macaddr8(out));
    }
    // v7.38 (read01) — PG `int2 & | # int2` stays int2 (a bitwise result of
    // two int2 always fits int2). Mixed widths widen as before.
    if let (Value::SmallInt(a), Value::SmallInt(b)) = (&l, &r) {
        return small_int_result(f(i64::from(*a), i64::from(*b)));
    }
    let widen = |v: Value<'static>| -> Value<'static> {
        match v {
            Value::SmallInt(n) => Value::Int(i32::from(n)),
            other => other,
        }
    };
    match (widen(l), widen(r)) {
        (Value::Int(a), Value::Int(b)) => {
            let result = f(i64::from(a), i64::from(b));
            // Two i32 inputs can't overflow i32 under | / &.
            Ok(Value::Int(result as i32))
        }
        (Value::Int(a), Value::BigInt(b)) | (Value::BigInt(b), Value::Int(a)) => {
            Ok(Value::BigInt(f(i64::from(a), b)))
        }
        (Value::BigInt(a), Value::BigInt(b)) => Ok(Value::BigInt(f(a, b))),
        (a, b) => Err(EvalError::TypeMismatch {
            detail: format!("cannot apply {op_name} to {a:?} and {b:?}"),
        }),
    }
}

/// The i64 value of an integer-family operand (SmallInt/Int/BigInt),
/// or `None` for anything else — used to claim bit-shift ops for
/// integers before the inet interpretation.
fn int_operand(v: &Value<'_>) -> Option<i64> {
    match v {
        Value::SmallInt(n) => Some(i64::from(*n)),
        Value::Int(n) => Some(i64::from(*n)),
        Value::BigInt(n) => Some(*n),
        _ => None,
    }
}

/// Elements of a scalar array Value normalised for equality
/// (integers widen to BigInt so int4[] and int8[] compare), plus a
/// flag for NULL elements. `None` for non-array operands.
fn array_scalar_elems(v: &Value<'_>) -> Option<(Vec<Value<'static>>, bool)> {
    fn collect<T: Clone>(
        items: &[Option<T>],
        f: impl Fn(T) -> Value<'static>,
    ) -> (Vec<Value<'static>>, bool) {
        let mut has_null = false;
        let mut out = Vec::with_capacity(items.len());
        for o in items {
            match o {
                Some(x) => out.push(f(x.clone())),
                None => has_null = true,
            }
        }
        (out, has_null)
    }
    Some(match v {
        Value::TextArray(i) => collect(i, Value::text),
        Value::IntArray(i) => collect(i, |x| Value::BigInt(i64::from(x))),
        Value::SmallIntArray(i) => collect(i, |x| Value::BigInt(i64::from(x))),
        Value::BigIntArray(i) => collect(i, Value::BigInt),
        Value::FloatArray(i) => collect(i, Value::Float),
        Value::BoolArray(i) => collect(i, Value::Bool),
        Value::DateArray(i) => collect(i, Value::Date),
        Value::TimestampArray(i) => collect(i, Value::Timestamp),
        _ => return None,
    })
}

/// `l @> r` for scalar arrays: every non-NULL element of `r` appears
/// in `l`; a NULL element on the right can never be contained (PG
/// semantics), and the empty array is contained in everything.
fn array_contains_all(l: &Value<'_>, r: &Value<'_>) -> bool {
    let (left, _) = array_scalar_elems(l).expect("caller guards array");
    let (right, right_nulls) = array_scalar_elems(r).expect("caller guards array");
    if right_nulls {
        return false;
    }
    right.iter().all(|b| left.contains(b))
}

/// PG's float8 range guard (utils/adt/float.c `check_float8_val`): a
/// finite ⊕ finite operation that overflows to ±Infinity is an error,
/// not a silent Inf; a multiply/divide whose non-zero operands underflow
/// to exactly 0 is an error too. Inf/NaN operands (whose Inf result is
/// legitimate) and additive cancellation to 0 pass through. Learned from
/// read01 float.c study — a silent Inf is exactly the kind of quiet
/// wrong answer SPG refuses to emit.
fn check_float8_range(result: f64, a: f64, b: f64, op_name: &str) -> Result<f64, EvalError> {
    if result.is_infinite() && !a.is_infinite() && !b.is_infinite() {
        return Err(EvalError::TypeMismatch {
            detail: "value out of range: overflow".into(),
        });
    }
    if (op_name == "*" || op_name == "/") && result == 0.0 && a != 0.0 && b != 0.0 {
        return Err(EvalError::TypeMismatch {
            detail: "value out of range: underflow".into(),
        });
    }
    Ok(result)
}

/// v7.38 (read01) — narrow an i64 result of two int2 operands back to int2,
/// erroring "smallint out of range" on overflow. PG keeps int2 arithmetic and
/// bitwise ops in int2; this is the shared narrowing all those sites use.
fn small_int_result(res: i64) -> Result<Value<'static>, EvalError> {
    i16::try_from(res)
        .map(Value::SmallInt)
        .map_err(|_| EvalError::TypeMismatch {
            detail: "smallint out of range".into(),
        })
}

fn arith(
    l: Value<'static>,
    r: Value<'static>,
    int_op: impl Fn(i64, i64) -> Option<i64>,
    float_op: impl Fn(f64, f64) -> f64,
    op_name: &str,
) -> Result<Value<'static>, EvalError> {
    // v7.38 (read01) — PG keeps int2 <op> int2 in int2 (widening only when
    // mixed with int4/int8), and a result outside int2 is "smallint out of
    // range", not a silent widen. Handle the both-small case before widening.
    if let (Value::SmallInt(a), Value::SmallInt(b)) = (&l, &r) {
        let res = int_op(i64::from(*a), i64::from(*b)).ok_or_else(|| EvalError::TypeMismatch {
            detail: format!("smallint overflow on {op_name}"),
        })?;
        return small_int_result(res);
    }
    // Widen SmallInt to Int up front so the rest of the arithmetic
    // table only deals with Int / BigInt / Float pairs.
    let widen = |v: Value<'static>| -> Value<'static> {
        match v {
            Value::SmallInt(n) => Value::Int(i32::from(n)),
            other => other,
        }
    };
    let l = widen(l);
    let r = widen(r);
    match (l, r) {
        (Value::Int(a), Value::Int(b)) => {
            // PG: int4 <op> int4 -> int4; a result that doesn't fit int4 is
            // "integer out of range", NOT a silent widening to bigint. This
            // matches SPG's own `::int` cast, which already errors on
            // overflow — the arithmetic path must agree.
            let result =
                int_op(i64::from(a), i64::from(b)).ok_or_else(|| EvalError::TypeMismatch {
                    detail: format!("integer overflow on {op_name}"),
                })?;
            let small = i32::try_from(result).map_err(|_| EvalError::TypeMismatch {
                detail: "integer out of range".into(),
            })?;
            Ok(Value::Int(small))
        }
        // Order-preserving: the `|`-merged form used to bind the Int as the
        // left operand regardless of position, so `bigint - int` computed
        // `int - bigint` (and `bigint / int` computed `int / bigint`). Split
        // so each side keeps its place for the non-commutative ops.
        (Value::Int(a), Value::BigInt(b)) => {
            let result = int_op(i64::from(a), b).ok_or_else(|| EvalError::TypeMismatch {
                detail: alloc::string::String::from("bigint out of range"),
            })?;
            Ok(Value::BigInt(result))
        }
        (Value::BigInt(a), Value::Int(b)) => {
            let result = int_op(a, i64::from(b)).ok_or_else(|| EvalError::TypeMismatch {
                detail: alloc::string::String::from("bigint out of range"),
            })?;
            Ok(Value::BigInt(result))
        }
        (Value::BigInt(a), Value::BigInt(b)) => {
            let result = int_op(a, b).ok_or_else(|| EvalError::TypeMismatch {
                detail: alloc::string::String::from("bigint out of range"),
            })?;
            Ok(Value::BigInt(result))
        }
        (a, b)
            if a.data_type() == Some(DataType::Float) || b.data_type() == Some(DataType::Float) =>
        {
            let af = as_f64(&a)?;
            let bf = as_f64(&b)?;
            Ok(Value::Float(check_float8_range(
                float_op(af, bf),
                af,
                bf,
                op_name,
            )?))
        }
        // v7.39 (round 238) — PG's wording: "operator does not exist:
        // integer + text". SPG printed `+ applied to non-numeric:
        // Some(Int) vs Some(Text)` — a Rust Debug dump of an internal enum,
        // and nothing a driver can match on.
        (a, b) => Err(EvalError::TypeMismatch {
            detail: format!(
                "operator does not exist: {} {op_name} {}",
                super::strings::pg_typeof_name(&a),
                super::strings::pg_typeof_name(&b)
            ),
        }),
    }
}

/// v7.39 (round 351, M11) — `apply_binary` with the session dialect.
///
/// It lives HERE, at the leaf, and not as an arm of `eval_expr`: that
/// function is the recursive frame the 768 KiB stack budget is tuned
/// against, and the guard test refused three separate shapes that added
/// locals to it (the round-305 frame cliff, twice in one round). A leaf
/// costs one frame, once.
pub(crate) fn apply_binary_in(
    op: BinOp,
    l: Value<'static>,
    r: Value<'static>,
    mysql: bool,
) -> Result<Value<'static>, EvalError> {
    if !mysql {
        return apply_binary(op, l, r);
    }
    // Date ± date-granular interval keeps a DATE — checked on the ORIGINAL
    // operands, before the operand-reading pair lifts a bare date string
    // to a midnight timestamp.
    if let Some(v) = mysql_date_plus_interval(op, &l, &r) {
        return Ok(v);
    }
    // v7.39 (round 393) — a STRING operand makes `/` a DOUBLE (`'10'/'4'`
    // is 2.5, not the DECIMAL 2.5000 that `10/4` is), checked on the
    // ORIGINAL operands before the reading pair lifts a string to a number.
    let div_text_operand =
        matches!(op, BinOp::Div) && (matches!(l, Value::Text(_)) || matches!(r, Value::Text(_)));
    let (l, r) = super::mysql_operand_reading_pair(op, l, r);
    if let Some(v) = super::mysql_true_division(op, &l, &r, div_text_operand) {
        return Ok(v);
    }
    // v7.39 (round 383) — `& | ^ << >>` are UNSIGNED 64-bit here, and
    // `^` reaches this path as BitXor (the parser maps `^` to XOR under
    // the MySQL dialect, not PG's `power`).
    if let Some(v) = super::mysql_bitwise(op, &l, &r) {
        return Ok(v);
    }
    apply_binary(op, l, r)
}

/// The DATE (days since the epoch) a `date ± INTERVAL` operand carries: a
/// `Value::Date`, or a bare date string with no time part.
fn date_operand_days(v: &Value<'_>) -> Option<i32> {
    match v {
        Value::Date(d) => Some(*d),
        Value::Text(s) if !s.contains(':') => crate::eval::parse_date_literal(s),
        // v7.39 (round 414) — only reached from `mysql_date_plus_interval`
        // (an out-of-line, MySQL-only step), so an integer here is MySQL's
        // YYYYMMDD / YYMMDD form. PG never reaches this arm.
        Value::SmallInt(n) => mysql_int_as_date_days(i64::from(*n)),
        Value::Int(n) => mysql_int_as_date_days(i64::from(*n)),
        Value::BigInt(n) => mysql_int_as_date_days(*n),
        _ => None,
    }
}

/// v7.39 (round 414) — read a MySQL YYYYMMDD (8) / YYMMDD (6) integer as
/// day offset. Bigger int shapes carry a time component and lift the result
/// to DATETIME, which is out of scope here — `mysql_date_plus_interval`
/// falls through and the operator errors, matching MariaDB's own refusal.
fn mysql_int_as_date_days(n: i64) -> Option<i32> {
    if n < 0 {
        return None;
    }
    let (y, mo, d) = match n {
        10_000_000..=99_999_999 => (
            (n / 10_000) as i32,
            ((n / 100) % 100) as u32,
            (n % 100) as u32,
        ),
        100_000..=999_999 => {
            let yy = (n / 10_000) as i32;
            let y = if yy < 70 { 2000 + yy } else { 1900 + yy };
            (y, ((n / 100) % 100) as u32, (n % 100) as u32)
        }
        _ => return None,
    };
    let text = alloc::format!("{y:04}-{mo:02}-{d:02}");
    crate::eval::parse_date_literal(&text)
}

/// v7.39 (round 373) — `date ± INTERVAL` keeps a DATE result under the
/// MySQL dialect when the interval has no time component (YEAR / MONTH /
/// DAY / WEEK): `DATE_ADD('2020-01-31', INTERVAL 1 MONTH)` is DATE
/// 2020-02-29 on MariaDB, not the midnight DATETIME PG produces. An
/// interval carrying a sub-day part (HOUR / MINUTE / SECOND) still lifts
/// to DATETIME, so this returns None for it and the PG path runs.
fn mysql_date_plus_interval(op: BinOp, l: &Value<'_>, r: &Value<'_>) -> Option<Value<'static>> {
    let iv = |v: &Value<'_>| match v {
        Value::Interval {
            months,
            days,
            micros,
        } => Some((*months, *days, *micros)),
        _ => None,
    };
    let (date, months, days, micros, sign) = match op {
        BinOp::Add => {
            if let (Some(d), Some((m, dd, us))) = (date_operand_days(l), iv(r)) {
                (d, m, dd, us, 1i64)
            } else if let (Some((m, dd, us)), Some(d)) = (iv(l), date_operand_days(r)) {
                (d, m, dd, us, 1i64)
            } else {
                return None;
            }
        }
        BinOp::Sub => {
            if let (Some(d), Some((m, dd, us))) = (date_operand_days(l), iv(r)) {
                (d, m, dd, us, -1i64)
            } else {
                return None;
            }
        }
        _ => return None,
    };
    if micros != 0 {
        return None; // a time component lifts the result to DATETIME
    }
    let base = i64::from(date).checked_mul(86_400_000_000)?;
    let res =
        add_interval_to_micros(base, i64::from(months) * sign, i64::from(days) * sign, 0).ok()?;
    let day = res.div_euclid(86_400_000_000);
    Some(Value::Date(i32::try_from(day).ok()?))
}

/// v7.39 (round 353, M9) — `a DIV b`.
fn int_div_op(l: &Value<'_>, r: &Value<'_>) -> Result<Value<'static>, EvalError> {
    let num = |v: &Value<'_>| -> Option<f64> {
        Some(match v {
            Value::SmallInt(n) => f64::from(*n),
            Value::Int(n) => f64::from(*n),
            #[allow(clippy::cast_precision_loss)]
            Value::BigInt(n) => *n as f64,
            Value::Float(f) => *f,
            Value::Real(f) => f64::from(*f),
            #[allow(clippy::cast_precision_loss)]
            Value::Numeric { scaled, scale, .. } => *scaled as f64 / 10_f64.powi(i32::from(*scale)),
            Value::Text(t) | Value::BpChar(t) => crate::eval::mysql_leading_number(t),
            Value::Null => return None,
            _ => return None,
        })
    };
    let (Some(a), Some(b)) = (num(l), num(r)) else {
        return Ok(Value::Null);
    };
    if b == 0.0 {
        return Ok(Value::Null);
    }
    #[allow(clippy::cast_possible_truncation)]
    Ok(Value::BigInt((a / b).trunc() as i64))
}

/// L2 (Euclidean) distance between two vectors of equal dimension.
/// Returned as `Value::Float(d)` so it composes with the existing
/// comparison / sort plumbing. Mismatched dims or non-vector operands
/// raise `TypeMismatch`.
#[allow(clippy::many_single_char_names)] // l, r, a, b, d are the natural names
fn l2_distance(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    // PG `point <-> point` is the Euclidean distance between two points.
    if let (Value::Point(a), Value::Point(b)) = (&l, &r) {
        let dx = a.x - b.x;
        let dy = a.y - b.y;
        return Ok(Value::Float(sqrt_newton(dx * dx + dy * dy)));
    }
    // PG `point <-> {lseg|box|circle}` (either order) — distance from the
    // point to the nearest point of the geometry.
    if let Some(d) = point_geo_distance(&l, &r).or_else(|| point_geo_distance(&r, &l)) {
        return Ok(Value::Float(d));
    }
    // PG `box <-> box` is the distance between the two box centres (verified
    // vs PG18.4: overlapping boxes still return the centre distance, not 0).
    if let (Value::PgBox(aur, all), Value::PgBox(bur, bll)) = (&l, &r) {
        let acx = (aur.x + all.x) / 2.0;
        let acy = (aur.y + all.y) / 2.0;
        let bcx = (bur.x + bll.x) / 2.0;
        let bcy = (bur.y + bll.y) / 2.0;
        let (dx, dy) = (acx - bcx, acy - bcy);
        return Ok(Value::Float(sqrt_newton(dx * dx + dy * dy)));
    }
    // PG `circle <-> circle` is the gap between the boundaries: the centre
    // distance minus both radii, clamped to 0 when they overlap.
    if let (
        Value::Circle {
            center: ac,
            radius: ar,
        },
        Value::Circle {
            center: bc,
            radius: br,
        },
    ) = (&l, &r)
    {
        let (dx, dy) = (ac.x - bc.x, ac.y - bc.y);
        let gap = sqrt_newton(dx * dx + dy * dy) - ar - br;
        return Ok(Value::Float(if gap < 0.0 { 0.0 } else { gap }));
    }
    // v7.39 (read01 geo_ops.c part 2) — the remaining geometric distance
    // pairs (lseg↔lseg, box↔lseg, point↔path, point↔polygon).
    if let Some(d) = geo_pair_distance(&l, &r) {
        return Ok(Value::Float(d));
    }
    // v6.0.1: route both operands through `unwrap_vec_pair` so SQ8
    // cells dequantise on the way in. Sub-f64 precision loss is
    // negligible vs the dequantisation noise the SQ8 path already
    // ships with.
    let (a, b) = unwrap_vec_pair(l, r, "<->")?;
    let mut sum: f64 = 0.0;
    for (x, y) in a.iter().zip(b.iter()) {
        let d = f64::from(*x) - f64::from(*y);
        sum += d * d;
    }
    Ok(Value::Float(sqrt_newton(sum)))
}

/// Self-built `sqrt` for `f64` — `std::f64::sqrt` lives in `std`, which the
/// engine's `no_std` constraint disallows. Newton-Raphson with a few rounds
/// reaches IEEE-754 precision for the inputs we'll see (sum of squares of
/// f32-derived distances, always non-negative, never NaN).
fn sqrt_newton(x: f64) -> f64 {
    if x <= 0.0 {
        return 0.0;
    }
    // libm's correctly-rounded sqrt matches PG's libc sqrt to the last ULP;
    // the former hand-rolled Newton iteration was one ULP low on irrational
    // results (e.g. box <-> box centre distance sqrt(2)).
    libm::sqrt(x)
}

/// v7.37.7 C.1.7 — PG integer modulo. PG's `%` (like C and the SQL
/// `mod()` function) is truncated-division remainder: the result takes
/// the sign of the DIVIDEND, so `-5 % 3 = -2`, not the Euclidean `1`.
/// That is exactly Rust's native `%`; `wrapping_rem` is used so the
/// `i64::MIN % -1` corner returns 0 instead of panicking. Division by
/// zero surfaces as `DivisionByZero` so callers see the same error
/// variant for `/` and `%`.
fn mod_op(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    // v7.39 (round 779 audit, F2) — the float arm below is an SPG
    // SUPERSET: PG resolves `%` over numeric / integer only (measured:
    // `5.5::float8 % 2::float8` is "operator does not exist" there).
    // §9-ledgered; the spelling MySQL callers expect.
    let any_float = matches!(l.data_type(), Some(DataType::Float))
        || matches!(r.data_type(), Some(DataType::Float));
    if any_float {
        let a = as_f64(&l)?;
        let b = as_f64(&r)?;
        if b == 0.0 {
            return Err(EvalError::DivisionByZero);
        }
        return Ok(Value::Float(a % b));
    }
    // `arith()` is integer-only when the float fast path above didn't
    // match; both closures take `i64`. `wrapping_rem` is truncated
    // remainder (sign of the dividend, matching PG / C / `mod()`) and
    // is panic-free including the `i64::MIN % -1` overflow corner.
    arith(
        l,
        r,
        |a, b| {
            if b == 0 {
                None
            } else {
                Some(a.wrapping_rem(b))
            }
        },
        // f64 fallback (when widening to Float happens inside arith);
        // f64's `%` is C `fmod` semantics, matching PG `%` on floats.
        |a, b| if b == 0.0 { 0.0 } else { a % b },
        "%",
    )
    .map_err(|e| match e {
        EvalError::TypeMismatch { detail } if detail.contains('%') => EvalError::DivisionByZero,
        other => other,
    })
}

fn div_op(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    let any_float = matches!(l.data_type(), Some(DataType::Float))
        || matches!(r.data_type(), Some(DataType::Float));
    if any_float {
        let a = as_f64(&l)?;
        let b = as_f64(&r)?;
        if b == 0.0 {
            return Err(EvalError::DivisionByZero);
        }
        return Ok(Value::Float(check_float8_range(a / b, a, b, "/")?));
    }
    // v7.38 (read01 P4.18) — a zero divisor is DivisionByZero; INT_MIN / -1
    // has no representable quotient and is a separate overflow (PG: "out of
    // range"), NOT a zero-divide. Check the divisor up front so the two stay
    // distinct instead of both collapsing to DivisionByZero.
    if matches!(r, Value::SmallInt(0) | Value::Int(0) | Value::BigInt(0)) {
        return Err(EvalError::DivisionByZero);
    }
    arith(
        l,
        r,
        // Divisor is non-zero here, so `checked_div` returns None only on the
        // INT_MIN / -1 overflow, which `arith` surfaces as an overflow error.
        |a, b| a.checked_div(b),
        |a, b| a / b,
        "/",
    )
}

fn as_f64(v: &Value<'_>) -> Result<f64, EvalError> {
    match v {
        Value::SmallInt(n) => Ok(f64::from(*n)),
        Value::Int(n) => Ok(f64::from(*n)),
        #[allow(clippy::cast_precision_loss)]
        Value::BigInt(n) => Ok(*n as f64),
        Value::Float(x) => Ok(*x),
        Value::Real(x) => Ok(f64::from(*x)),
        #[allow(clippy::cast_precision_loss)]
        // v7.39 (round 271) — parse the decimal text instead of
        // dividing by a power built with repeated multiplication,
        // which accumulated rounding error and ran to infinity once
        // `scale` could exceed 308.
        Value::Numeric { scaled, scale, .. } => Ok(crate::eval::format_numeric(*scaled, *scale)
            .parse()
            .unwrap_or(f64::NAN)),
        // v7.39 (read01 numeric.c) — a big NUMERIC in a mixed float op
        // approximates through its decimal text (same promotion as the
        // i128-mantissa arm above).
        Value::NumericBig(b) => b
            .to_decimal_str()
            .parse()
            .map_err(|_| EvalError::TypeMismatch {
                detail: "value out of range: overflow".into(),
            }),
        other => Err(EvalError::TypeMismatch {
            detail: format!(
                "cannot convert {} to FLOAT",
                crate::conversions::pg_type_name_for_error_opt(other.data_type())
            ),
        }),
    }
}

/// Element-wise ordering of one array element pair. PG's `array_cmp`
/// (the btree support routine backing `=` / `<` / `>` on arrays)
/// treats a NULL element as *greater* than any non-NULL, and two
/// NULLs as equal — a total order, unlike scalar `NULL = NULL`
/// which is unknown. This is why `ARRAY[1,NULL] = ARRAY[1,NULL]`
/// is `t` and `ARRAY[1,2] < ARRAY[1,NULL]` is `t`.
fn cmp_array_elem<T: Ord>(a: &Option<T>, b: &Option<T>) -> core::cmp::Ordering {
    use core::cmp::Ordering::{Equal, Greater, Less};
    match (a, b) {
        (None, None) => Equal,
        (None, Some(_)) => Greater,
        (Some(_), None) => Less,
        (Some(x), Some(y)) => x.cmp(y),
    }
}

/// Lexicographic array comparison: first differing element decides;
/// when one array is a prefix of the other, the shorter is less.
fn cmp_array<T: Ord>(a: &[Option<T>], b: &[Option<T>]) -> core::cmp::Ordering {
    for (x, y) in a.iter().zip(b.iter()) {
        let c = cmp_array_elem(x, y);
        if c != core::cmp::Ordering::Equal {
            return c;
        }
    }
    a.len().cmp(&b.len())
}

/// Element-wise array *value* equality (PG `array_eq`). Two arrays are
/// equal iff same length and every element pair is equal by `elem_eq`.
/// A NULL element is equal only to another NULL (PG `array_eq` treats
/// two NULLs as equal — `ARRAY[1,NULL] = ARRAY[1,NULL]` is `t`, not
/// NULL), and NULL vs non-NULL is unequal. This is used for the array
/// types whose element equality is *value-based* rather than the derived
/// `Ord` on the stored repr (NUMERIC[] scale-insensitive, FLOAT8[] NaN,
/// INTERVAL[] unit-equivalence), so `cmp_array`'s repr `Ord` is wrong.
/// Value comparison of two arrays under PG `array_cmp`: element by element
/// with the element comparator; the first non-equal pair decides. A NULL
/// element sorts GREATER than any non-NULL (two NULLs are equal). If every
/// common element is equal, the shorter array is less. Verified vs PG18:
/// `[1,NULL] > [1,2]`, `[1] < [1,2]`.
fn array_value_cmp<T>(
    a: &[Option<T>],
    b: &[Option<T>],
    elem_cmp: impl Fn(&T, &T) -> core::cmp::Ordering,
) -> core::cmp::Ordering {
    use core::cmp::Ordering;
    for (x, y) in a.iter().zip(b.iter()) {
        let o = match (x, y) {
            (None, None) => Ordering::Equal,
            (None, Some(_)) => Ordering::Greater,
            (Some(_), None) => Ordering::Less,
            (Some(p), Some(q)) => elem_cmp(p, q),
        };
        if o != Ordering::Equal {
            return o;
        }
    }
    a.len().cmp(&b.len())
}

/// Value comparison of two NUMERIC elements `(scaled, scale)`. Scale-
/// insensitive (`1.10` == `1.1`): rescale both up to the wider scale
/// (exact, no precision loss) and compare the i128. Mirrors the scalar
/// `numeric` compare (`apply_binary_numeric`). Rescale overflow
/// (astronomically different scales) falls back to the raw-scaled compare.
fn numeric_pair_cmp(a: (i128, u16), b: (i128, u16)) -> core::cmp::Ordering {
    let t = a.1.max(b.1);
    match (rescale(a.0, a.1, t), rescale(b.0, b.1, t)) {
        (Some(x), Some(y)) => x.cmp(&y),
        _ => a.0.cmp(&b.0),
    }
}

/// Value comparison of two FLOAT8 elements under PG's btree/array order:
/// `NaN` is the greatest value (`NaN == NaN`, `NaN >` any number) and
/// `+0.0 == -0.0`. `partial_cmp` gives the finite case (incl. signed-zero
/// equality); the NaN cases are special-cased.
fn float_pg_cmp(a: f64, b: f64) -> core::cmp::Ordering {
    use core::cmp::Ordering;
    match (a.is_nan(), b.is_nan()) {
        (true, true) => Ordering::Equal,
        (true, false) => Ordering::Greater,
        (false, true) => Ordering::Less,
        (false, false) => a.partial_cmp(&b).unwrap_or(Ordering::Equal),
    }
}

/// Value comparison of two INTERVAL elements by PG's canonical microsecond
/// span (months = 30 days, days = 24 hours), so `1 mon` == `30 days` and
/// `1 day` == `24:00:00`. i128 keeps the product from overflowing i64.
fn interval_span_cmp(
    a: &spg_storage::IntervalSpan,
    b: &spg_storage::IntervalSpan,
) -> core::cmp::Ordering {
    let span = |s: &spg_storage::IntervalSpan| -> i128 {
        (i128::from(s.months) * 30 + i128::from(s.days)) * 86_400_000_000 + i128::from(s.micros)
    };
    span(a).cmp(&span(b))
}

/// v7.38 (read01, T12.4) — PG's tsvector B-tree total order: lexeme count
/// first, then per-lexeme by word (length, then bytes), then positions, then
/// weight. (The position tiebreak direction for identical words is a niche
/// detail; the count + word ordering matches PG.)
fn tsvector_total_cmp(
    a: &[spg_storage::TsLexeme],
    b: &[spg_storage::TsLexeme],
) -> core::cmp::Ordering {
    use core::cmp::Ordering::Equal;
    if a.len() != b.len() {
        return a.len().cmp(&b.len());
    }
    for (la, lb) in a.iter().zip(b.iter()) {
        let c = la
            .word
            .len()
            .cmp(&lb.word.len())
            .then_with(|| la.word.cmp(&lb.word))
            .then_with(|| la.positions.cmp(&lb.positions))
            .then_with(|| la.weight.cmp(&lb.weight));
        if c != Equal {
            return c;
        }
    }
    Equal
}

/// v7.38 (read01, R2) — the "traditional" CRC-32 (reflected poly 0xEDB88320,
/// init/final 0xFFFFFFFF) PG's `silly_cmp_tsquery` uses to order tsquery
/// lexemes — so ordering is CRC-based, NOT alphabetical (`'b' > 'c'`).
fn crc32_traditional(bytes: &[u8]) -> u32 {
    let mut crc: u32 = 0xFFFF_FFFF;
    for &b in bytes {
        crc ^= u32::from(b);
        for _ in 0..8 {
            crc = if crc & 1 != 0 {
                (crc >> 1) ^ 0xEDB8_8320
            } else {
                crc >> 1
            };
        }
    }
    crc ^ 0xFFFF_FFFF
}

/// v7.38 (read01, R2) — one item of a tsquery flattened to prefix order.
enum QItem {
    Val {
        crc: u32,
        bytes: alloc::vec::Vec<u8>,
    },
    Phrase(u16),
    Or,
    And,
    Not,
}

fn tsquery_numnode(ast: &spg_storage::TsQueryAst) -> usize {
    use spg_storage::TsQueryAst as Q;
    match ast {
        Q::Term { .. } => 1,
        Q::Not(x) => 1 + tsquery_numnode(x),
        Q::And(l, r) | Q::Or(l, r) => 1 + tsquery_numnode(l) + tsquery_numnode(r),
        Q::Phrase { left, right, .. } => 1 + tsquery_numnode(left) + tsquery_numnode(right),
    }
}

fn tsquery_flatten(ast: &spg_storage::TsQueryAst, out: &mut alloc::vec::Vec<QItem>) {
    use spg_storage::TsQueryAst as Q;
    match ast {
        Q::Term { word, .. } => out.push(QItem::Val {
            crc: crc32_traditional(word.as_bytes()),
            bytes: word.as_bytes().to_vec(),
        }),
        Q::Not(x) => {
            out.push(QItem::Not);
            tsquery_flatten(x, out);
        }
        Q::And(l, r) => {
            out.push(QItem::And);
            tsquery_flatten(l, out);
            tsquery_flatten(r, out);
        }
        Q::Or(l, r) => {
            out.push(QItem::Or);
            tsquery_flatten(l, out);
            tsquery_flatten(r, out);
        }
        Q::Phrase {
            left,
            right,
            distance,
        } => {
            out.push(QItem::Phrase(*distance));
            tsquery_flatten(left, out);
            tsquery_flatten(right, out);
        }
    }
}

/// v7.38 (read01, R2) — PG's non-recursive tsquery total order: node count
/// first, then a prefix-item compare where an operand (VAL) sorts before any
/// operator, VALs compare by CRC-32 then length then bytes, operators sort
/// `PHRASE < OR < AND < NOT`, and two PHRASE nodes sort larger-distance-first.
fn tsquery_total_cmp(
    a: &spg_storage::TsQueryAst,
    b: &spg_storage::TsQueryAst,
) -> core::cmp::Ordering {
    use core::cmp::Ordering::{Equal, Greater, Less};
    let (na, nb) = (tsquery_numnode(a), tsquery_numnode(b));
    if na != nb {
        return na.cmp(&nb);
    }
    let opr_rank = |q: &QItem| -> u8 {
        match q {
            QItem::Phrase(_) => 0,
            QItem::Or => 1,
            QItem::And => 2,
            QItem::Not => 3,
            QItem::Val { .. } => unreachable!(),
        }
    };
    let item_cmp = |x: &QItem, y: &QItem| -> core::cmp::Ordering {
        match (x, y) {
            (QItem::Val { crc: cx, bytes: bx }, QItem::Val { crc: cy, bytes: by }) => {
                (*cx as i32) // PG's valcrc is int32 — the compare is SIGNED
                    .cmp(&(*cy as i32))
                    .then_with(|| bx.len().cmp(&by.len()))
                    .then_with(|| bx.cmp(by))
            }
            (QItem::Val { .. }, _) => Less,
            (_, QItem::Val { .. }) => Greater,
            (QItem::Phrase(dx), QItem::Phrase(dy)) => dy.cmp(dx), // larger distance first
            _ => opr_rank(x).cmp(&opr_rank(y)),
        }
    };
    let (mut ia, mut ib) = (alloc::vec::Vec::new(), alloc::vec::Vec::new());
    tsquery_flatten(a, &mut ia);
    tsquery_flatten(b, &mut ib);
    for (x, y) in ia.iter().zip(ib.iter()) {
        let c = item_cmp(x, y);
        if c != Equal {
            return c;
        }
    }
    Equal
}

/// Map a computed `Ordering` to the boolean result of a comparison op.
fn cmp_result(op: BinOp, ord: core::cmp::Ordering) -> Result<Value<'static>, EvalError> {
    match op {
        BinOp::Eq => Ok(Value::Bool(ord.is_eq())),
        BinOp::NotEq => Ok(Value::Bool(ord.is_ne())),
        BinOp::Lt => Ok(Value::Bool(ord.is_lt())),
        BinOp::LtEq => Ok(Value::Bool(ord.is_le())),
        BinOp::Gt => Ok(Value::Bool(ord.is_gt())),
        BinOp::GtEq => Ok(Value::Bool(ord.is_ge())),
        _ => Err(EvalError::TypeMismatch {
            detail: "array supports only comparison operators".into(),
        }),
    }
}

/// Compare the first `nbits` bits of two big-endian (MSB-first) byte
/// arrays, PG `bitncmp`. Full common bytes compared bytewise; the final
/// partial byte compared under a top-`rem`-bits mask.
fn bitncmp(a: &[u8; 16], b: &[u8; 16], nbits: u8) -> core::cmp::Ordering {
    let nbytes = (nbits / 8) as usize;
    let ord = a[..nbytes].cmp(&b[..nbytes]);
    if ord != core::cmp::Ordering::Equal {
        return ord;
    }
    let rem = nbits % 8;
    if rem == 0 {
        return core::cmp::Ordering::Equal;
    }
    let mask = 0xffu8 << (8 - rem);
    (a[nbytes] & mask).cmp(&(b[nbytes] & mask))
}

/// PG `network_cmp`: family first (IPv4 < IPv6), then the common netmask
/// prefix of the addresses, then the netmask length, then the full address.
fn network_cmp(
    af: u8,
    ab: u8,
    aa: &[u8; 16],
    bf: u8,
    bb: u8,
    ba: &[u8; 16],
) -> core::cmp::Ordering {
    if af != bf {
        return af.cmp(&bf);
    }
    let common = ab.min(bb);
    let ord = bitncmp(aa, ba, common);
    if ord != core::cmp::Ordering::Equal {
        return ord;
    }
    let ord = ab.cmp(&bb);
    if ord != core::cmp::Ordering::Equal {
        return ord;
    }
    let maxbits = if af == 4 { 32 } else { 128 };
    bitncmp(aa, ba, maxbits)
}

/// The successor of a discrete range bound (`+1` for int4/int8, `+1 day`
/// for date). `None` on overflow (PG errors; a comparison can just treat
/// the un-canonicalisable bound as-is). Continuous kinds return `None`.
fn range_step_up(kind: spg_storage::RangeKind, v: &Value<'_>) -> Option<Value<'static>> {
    use spg_storage::RangeKind as K;
    match (kind, v) {
        (K::Int4, Value::Int(n)) => n.checked_add(1).map(Value::Int),
        (K::Int8, Value::BigInt(n)) => n.checked_add(1).map(Value::BigInt),
        (K::Date, Value::Date(d)) => d.checked_add(1).map(Value::Date),
        _ => None,
    }
}

/// Canonicalise a range to PG's `[)` form for the DISCRETE kinds
/// (int4/int8/date): an exclusive lower becomes inclusive lower+1, an
/// inclusive upper becomes exclusive upper+1. Continuous kinds
/// (num/ts/tstz) are returned unchanged, so `[1,5)` != `[1,5]`.
fn range_canonical(
    kind: spg_storage::RangeKind,
    lower: &Option<alloc::boxed::Box<Value<'static>>>,
    upper: &Option<alloc::boxed::Box<Value<'static>>>,
    lower_inc: bool,
    upper_inc: bool,
) -> (Option<Value<'static>>, bool, Option<Value<'static>>, bool) {
    use spg_storage::RangeKind as K;
    let mut lo = lower.as_ref().map(|b| (**b).clone());
    let mut li = lower_inc;
    let mut up = upper.as_ref().map(|b| (**b).clone());
    let mut ui = upper_inc;
    if matches!(kind, K::Int4 | K::Int8 | K::Date) {
        if let (Some(l), false) = (lo.as_ref(), li) {
            if let Some(l2) = range_step_up(kind, l) {
                lo = Some(l2);
                li = true;
            }
        }
        if let (Some(u), true) = (up.as_ref(), ui) {
            if let Some(u2) = range_step_up(kind, u) {
                up = Some(u2);
                ui = false;
            }
        }
    }
    (lo, li, up, ui)
}

/// Sort key for range uppers: `None` (+∞) greatest, then by value, then an
/// inclusive upper after an exclusive one at the same value.
fn upper_cmp(
    au: &Option<Value<'static>>,
    aui: bool,
    bu: &Option<Value<'static>>,
    bui: bool,
) -> core::cmp::Ordering {
    use core::cmp::Ordering;
    match (au, bu) {
        (None, None) => Ordering::Equal,
        (None, Some(_)) => Ordering::Greater, // +∞ greatest
        (Some(_), None) => Ordering::Less,
        (Some(x), Some(y)) => bound_cmp(x, y).then(aui.cmp(&bui)), // inclusive(true) greater
    }
}
/// v7.38 — the six components of one range operand (kind, bounds, bound
/// inclusivity, emptiness), bundled so the `range_*` family below takes two
/// parameters instead of twelve.
#[derive(Clone, Copy)]
struct RangeParts<'v> {
    kind: spg_storage::RangeKind,
    lower: &'v Option<alloc::boxed::Box<Value<'static>>>,
    upper: &'v Option<alloc::boxed::Box<Value<'static>>>,
    lower_inc: bool,
    upper_inc: bool,
    empty: bool,
}

impl<'v> RangeParts<'v> {
    /// v7.39 (read01 rangetypes.c) — borrow the parts straight off a
    /// `Value::Range`; `None` for any other variant.
    fn from_value(v: &'v Value<'_>) -> Option<Self> {
        match v {
            Value::Range {
                kind,
                lower,
                upper,
                lower_inc,
                upper_inc,
                empty,
            } => Some(Self::new(
                *kind, lower, upper, *lower_inc, *upper_inc, *empty,
            )),
            _ => None,
        }
    }

    fn new(
        kind: spg_storage::RangeKind,
        lower: &'v Option<alloc::boxed::Box<Value<'static>>>,
        upper: &'v Option<alloc::boxed::Box<Value<'static>>>,
        lower_inc: bool,
        upper_inc: bool,
        empty: bool,
    ) -> Self {
        Self {
            kind,
            lower,
            upper,
            lower_inc,
            upper_inc,
            empty,
        }
    }
}

/// PG `range_cmp` total order: an empty range sorts first (two empties are
/// equal), otherwise compare canonical lower bounds, then upper bounds. The
/// `Equal` result subsumes range equality (canonical `[)` forms must match).
fn range_cmp(a: RangeParts<'_>, b: RangeParts<'_>) -> core::cmp::Ordering {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    use core::cmp::Ordering;
    if ae || be {
        return match (ae, be) {
            (true, true) => Ordering::Equal,
            (true, false) => Ordering::Less,
            _ => Ordering::Greater,
        };
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, bl, bu, bli, bui);
    lower_cmp(&al2, ali2, &bl2, bli2).then_with(|| upper_cmp(&au2, aui2, &bu2, bui2))
}

/// A canonicalised range span: (lower, lower_inc, upper, upper_inc).
type CanonSpan = (Option<Value<'static>>, bool, Option<Value<'static>>, bool);

/// Compare two range-element bound values of the same range kind.
fn bound_cmp(a: &Value<'_>, b: &Value<'_>) -> core::cmp::Ordering {
    use core::cmp::Ordering;
    match (a, b) {
        (Value::Int(x), Value::Int(y)) => x.cmp(y),
        (Value::BigInt(x), Value::BigInt(y)) => x.cmp(y),
        (Value::Date(x), Value::Date(y)) => x.cmp(y),
        (Value::Timestamp(x), Value::Timestamp(y)) => x.cmp(y),
        (
            Value::Numeric {
                scaled: xs,
                scale: xc,
                ..
            },
            Value::Numeric {
                scaled: ys,
                scale: yc,
                ..
            },
        ) => numeric_pair_cmp((*xs, *xc), (*ys, *yc)),
        // Mixed real-number bounds/elements (e.g. `numrange(1.5,3.5) @> 2.5`
        // where the literal lexes as float but the range bounds are numeric):
        // compare as f64 so containment doesn't silently fall through to Equal.
        _ => match (num_as_f64(a), num_as_f64(b)) {
            (Some(x), Some(y)) => x.partial_cmp(&y).unwrap_or(Ordering::Equal),
            _ => Ordering::Equal,
        },
    }
}

/// The f64 value of any real-number scalar, for cross-type bound comparison.
#[allow(clippy::cast_precision_loss)]
fn num_as_f64(v: &Value<'_>) -> Option<f64> {
    match v {
        Value::SmallInt(n) => Some(f64::from(*n)),
        Value::Int(n) => Some(f64::from(*n)),
        Value::BigInt(n) => Some(*n as f64),
        Value::Float(x) => Some(*x),
        Value::Numeric { scaled, scale, .. } => {
            Some(*scaled as f64 / 10i128.pow(u32::from(*scale)) as f64)
        }
        _ => None,
    }
}

/// Sort key for range lowers: `None` (−∞) first, then by value, then an
/// inclusive lower before an exclusive one at the same value.
fn lower_cmp(
    al: &Option<Value<'static>>,
    ali: bool,
    bl: &Option<Value<'static>>,
    bli: bool,
) -> core::cmp::Ordering {
    use core::cmp::Ordering;
    match (al, bl) {
        (None, None) => Ordering::Equal,
        (None, Some(_)) => Ordering::Less,
        (Some(_), None) => Ordering::Greater,
        (Some(x), Some(y)) => bound_cmp(x, y).then(bli.cmp(&ali)), // inclusive(true) first
    }
}

/// Does a span ending at `(au, aui)` reach a following span starting at
/// `(bl, bli)` — i.e. they overlap or are adjacent (no gap)?
fn upper_reaches_lower(
    au: &Option<Value<'static>>,
    aui: bool,
    bl: &Option<Value<'static>>,
    bli: bool,
) -> bool {
    use core::cmp::Ordering;
    match (au, bl) {
        (None, _) => true, // +∞ upper reaches anything
        (_, None) => true, // following span starts at −∞
        (Some(u), Some(l)) => match bound_cmp(u, l) {
            Ordering::Greater => true,     // overlap
            Ordering::Equal => aui || bli, // adjacent iff the touching point is covered
            Ordering::Less => false,       // gap
        },
    }
}

/// Is upper `(au, aui)` strictly greater than upper `(bu, bui)`?
fn upper_greater(
    au: &Option<Value<'static>>,
    aui: bool,
    bu: &Option<Value<'static>>,
    bui: bool,
) -> bool {
    use core::cmp::Ordering;
    match (au, bu) {
        (None, None) => false,
        (None, Some(_)) => true, // +∞ greatest
        (Some(_), None) => false,
        (Some(x), Some(y)) => match bound_cmp(x, y) {
            Ordering::Greater => true,
            Ordering::Less => false,
            Ordering::Equal => aui && !bui, // inclusive upper > exclusive upper
        },
    }
}

/// Normalise a multirange's spans to PG's canonical form: canonicalise each
/// (discrete `[)`), drop empties, sort by lower, then merge any overlapping
/// or adjacent spans. Two multiranges are equal iff their normal forms match.
fn normalize_multirange(
    kind: spg_storage::RangeKind,
    spans: &[spg_storage::RangeSpan],
) -> alloc::vec::Vec<CanonSpan> {
    let mut cs: alloc::vec::Vec<CanonSpan> = spans
        .iter()
        .filter(|s| !s.empty)
        .map(|s| range_canonical(kind, &s.lower, &s.upper, s.lower_inc, s.upper_inc))
        .collect();
    cs.sort_by(|a, b| lower_cmp(&a.0, a.1, &b.0, b.1));
    let mut out: alloc::vec::Vec<CanonSpan> = alloc::vec::Vec::new();
    for s in cs {
        if let Some(last) = out.last_mut() {
            if upper_reaches_lower(&last.2, last.3, &s.0, s.1) {
                if upper_greater(&s.2, s.3, &last.2, last.3) {
                    last.2 = s.2;
                    last.3 = s.3;
                }
                continue;
            }
        }
        out.push(s);
    }
    out
}

/// Normalize a multirange's member spans into PG-canonical form: drop
/// empty, canonicalize each (discrete `(a` → `[a+1`, `b]` → `b+1)`),
/// sort by lower bound, and merge overlapping/adjacent spans. This is
/// what `multirange` constructors must store — `int4multirange(int4range
/// (1,5), int4range(4,8))` is `{[1,8)}`, not two spans.
/// v7.39 (read01 multirangetypes.c) — span -> RangeParts view.
fn span_parts(kind: spg_storage::RangeKind, s: &spg_storage::RangeSpan) -> RangeParts<'_> {
    RangeParts::new(kind, &s.lower, &s.upper, s.lower_inc, s.upper_inc, s.empty)
}

/// Multirange contains: an element / a range is contained when SOME span
/// contains it; a multirange is contained when EVERY span of it is
/// contained in some span (canonical spans are disjoint + sorted, so the
/// per-span check is exact).
pub(crate) fn multirange_contains(
    kind: spg_storage::RangeKind,
    spans: &[spg_storage::RangeSpan],
    rhs: &Value<'_>,
) -> Option<bool> {
    match rhs {
        Value::Range {
            kind: rk,
            lower,
            upper,
            lower_inc,
            upper_inc,
            empty,
        } => {
            if *rk != kind {
                return None;
            }
            if *empty {
                return Some(true);
            }
            let b = RangeParts::new(*rk, lower, upper, *lower_inc, *upper_inc, *empty);
            Some(
                spans
                    .iter()
                    .any(|s| range_contains_range(span_parts(kind, s), b)),
            )
        }
        Value::Multirange { kind: rk, ranges } => {
            if *rk != kind {
                return None;
            }
            Some(ranges.iter().all(|r| {
                spans
                    .iter()
                    .any(|s| range_contains_range(span_parts(kind, s), span_parts(kind, r)))
            }))
        }
        Value::Null => None,
        elem => Some(spans.iter().any(|s| {
            range_contains_elem(
                kind,
                &s.lower,
                &s.upper,
                s.lower_inc,
                s.upper_inc,
                s.empty,
                elem,
            )
        })),
    }
}

/// Multirange set algebra: union / difference / intersection, all
/// returning canonical span lists.
/// v7.39 (round 256) — a multirange's outer hull as a plain range (the
/// same value `range_merge(multirange)` returns). PG's POSITIONAL
/// operators (`<<` `>>` `&<` `&>` `-|-`) treat a multirange as its hull:
/// probed live, `{[1,3),[9,11)} -|- {[3,5)}` is FALSE even though the
/// first element is adjacent to the operand — the hull `[1,11)`
/// overlaps it. An empty multirange has no hull, so those operators
/// answer false for it.
pub(crate) fn multirange_hull(
    kind: spg_storage::RangeKind,
    ranges: &[spg_storage::RangeSpan],
) -> Value<'static> {
    if ranges.is_empty() {
        return Value::Range {
            kind,
            lower: None,
            upper: None,
            lower_inc: false,
            upper_inc: false,
            empty: true,
        };
    }
    let first = &ranges[0];
    let last = &ranges[ranges.len() - 1];
    Value::Range {
        kind,
        lower: first.lower.clone(),
        upper: last.upper.clone(),
        lower_inc: first.lower_inc,
        upper_inc: last.upper_inc,
        empty: false,
    }
}

/// v7.39 (round 256) — promote a plain range to the one-element
/// multirange PG treats it as when the other operand is a multirange
/// (`range && multirange`, `range @> multirange`, `multirange <@ range`
/// …). Returns `None` for anything else so the caller leaves it alone.
pub(crate) fn range_as_multirange(v: &Value<'_>) -> Option<Value<'static>> {
    let Value::Range {
        kind,
        lower,
        upper,
        lower_inc,
        upper_inc,
        empty,
    } = v
    else {
        return None;
    };
    let ranges = if *empty {
        alloc::vec::Vec::new()
    } else {
        alloc::vec![spg_storage::RangeSpan {
            lower: lower.clone(),
            upper: upper.clone(),
            lower_inc: *lower_inc,
            upper_inc: *upper_inc,
            empty: false,
        }]
    };
    Some(Value::Multirange {
        kind: *kind,
        ranges,
    })
}

pub(crate) fn multirange_union(
    kind: spg_storage::RangeKind,
    a: &[spg_storage::RangeSpan],
    b: &[spg_storage::RangeSpan],
) -> alloc::vec::Vec<spg_storage::RangeSpan> {
    let mut all: alloc::vec::Vec<spg_storage::RangeSpan> = a.to_vec();
    all.extend_from_slice(b);
    normalize_multirange_spans(kind, &all)
}

pub(crate) fn multirange_intersection(
    kind: spg_storage::RangeKind,
    a: &[spg_storage::RangeSpan],
    b: &[spg_storage::RangeSpan],
) -> alloc::vec::Vec<spg_storage::RangeSpan> {
    let mut out: alloc::vec::Vec<spg_storage::RangeSpan> = alloc::vec::Vec::new();
    for x in a {
        for y in b {
            if let Value::Range {
                lower,
                upper,
                lower_inc,
                upper_inc,
                empty,
                ..
            } = range_intersect(span_parts(kind, x), span_parts(kind, y))
            {
                if !empty {
                    out.push(spg_storage::RangeSpan {
                        lower,
                        upper,
                        lower_inc,
                        upper_inc,
                        empty: false,
                    });
                }
            }
        }
    }
    normalize_multirange_spans(kind, &out)
}

pub(crate) fn multirange_difference(
    kind: spg_storage::RangeKind,
    a: &[spg_storage::RangeSpan],
    b: &[spg_storage::RangeSpan],
) -> alloc::vec::Vec<spg_storage::RangeSpan> {
    // Subtract every b-span from every surviving a-fragment. A cut can
    // split a fragment in two: [frag.lower, cut.lower) + (cut.upper,
    // frag.upper] — expressed through the bound flips.
    let mut frags: alloc::vec::Vec<spg_storage::RangeSpan> = a.to_vec();
    for cut in b {
        let mut next: alloc::vec::Vec<spg_storage::RangeSpan> = alloc::vec::Vec::new();
        for f in frags {
            // No overlap -> keep whole.
            let inter = range_intersect(span_parts(kind, &f), span_parts(kind, cut));
            let overlap = matches!(&inter, Value::Range { empty: false, .. });
            if !overlap {
                next.push(f);
                continue;
            }
            let Value::Range {
                lower: il,
                upper: iu,
                lower_inc: ili,
                upper_inc: iui,
                ..
            } = inter
            else {
                next.push(f);
                continue;
            };
            // Left remainder: [f.lower, inter.lower)
            let left = spg_storage::RangeSpan {
                lower: f.lower.clone(),
                upper: il.clone(),
                lower_inc: f.lower_inc,
                upper_inc: !ili,
                empty: false,
            };
            if span_nonempty(kind, &left) {
                next.push(left);
            }
            // Right remainder: (inter.upper, f.upper]
            let right = spg_storage::RangeSpan {
                lower: iu.clone(),
                upper: f.upper.clone(),
                lower_inc: !iui,
                upper_inc: f.upper_inc,
                empty: false,
            };
            if span_nonempty(kind, &right) {
                next.push(right);
            }
        }
        frags = next;
    }
    normalize_multirange_spans(kind, &frags)
}

/// A span is non-empty when lower < upper, or lower == upper with both
/// bounds inclusive; open bounds (None) are infinite.
fn span_nonempty(kind: spg_storage::RangeKind, s: &spg_storage::RangeSpan) -> bool {
    let _ = kind;
    match (&s.lower, &s.upper) {
        (Some(l), Some(u)) => match crate::orderby::value_cmp(l, u) {
            core::cmp::Ordering::Less => true,
            core::cmp::Ordering::Equal => s.lower_inc && s.upper_inc,
            core::cmp::Ordering::Greater => false,
        },
        _ => true,
    }
}

pub(crate) fn normalize_multirange_spans(
    kind: spg_storage::RangeKind,
    spans: &[spg_storage::RangeSpan],
) -> alloc::vec::Vec<spg_storage::RangeSpan> {
    normalize_multirange(kind, spans)
        .into_iter()
        .map(|(lo, li, up, ui)| spg_storage::RangeSpan {
            lower: lo.map(alloc::boxed::Box::new),
            upper: up.map(alloc::boxed::Box::new),
            lower_inc: li,
            upper_inc: ui,
            empty: false,
        })
        .collect()
}

/// Compare two already-canonicalised spans by lower then upper bound.
fn canon_span_cmp(a: &CanonSpan, b: &CanonSpan) -> core::cmp::Ordering {
    lower_cmp(&a.0, a.1, &b.0, b.1).then_with(|| upper_cmp(&a.2, a.3, &b.2, b.3))
}

/// PG multirange total order: compare the normalised span lists element by
/// element; a multirange that is a prefix of another sorts first (`{}` sorts
/// first). `Equal` subsumes multirange equality.
fn multirange_cmp(
    ak: spg_storage::RangeKind,
    aranges: &[spg_storage::RangeSpan],
    bk: spg_storage::RangeKind,
    branges: &[spg_storage::RangeSpan],
) -> core::cmp::Ordering {
    let _ = (ak, bk);
    let a = normalize_multirange(ak, aranges);
    let b = normalize_multirange(bk, branges);
    for (x, y) in a.iter().zip(b.iter()) {
        let o = canon_span_cmp(x, y);
        if o != core::cmp::Ordering::Equal {
            return o;
        }
    }
    a.len().cmp(&b.len())
}

/// Is the interval bounded below by `(l, l_inc)` and above by `(u, u_inc)`
/// non-empty? (`−∞` lower / `+∞` upper are always fine; equal finite bounds
/// need both inclusive.)
fn range_point_le(
    l: &Option<Value<'static>>,
    l_inc: bool,
    u: &Option<Value<'static>>,
    u_inc: bool,
) -> bool {
    match (l, u) {
        (None, _) | (_, None) => true,
        (Some(lv), Some(uv)) => match bound_cmp(lv, uv) {
            core::cmp::Ordering::Less => true,
            core::cmp::Ordering::Greater => false,
            core::cmp::Ordering::Equal => l_inc && u_inc,
        },
    }
}

/// Range `&&` overlap: the two ranges share at least one point. Empty
/// overlaps nothing.
fn range_overlaps(a: RangeParts<'_>, b: RangeParts<'_>) -> bool {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    if ae || be {
        return false;
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, bl, bu, bli, bui);
    range_point_le(&al2, ali2, &bu2, bui2) && range_point_le(&bl2, bli2, &au2, aui2)
}

/// Range `@>` range: `a` contains `b`. Empty is contained in everything;
/// only empty contains empty.
fn range_contains_range(a: RangeParts<'_>, b: RangeParts<'_>) -> bool {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    if be {
        return true;
    }
    if ae {
        return false;
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, bl, bu, bli, bui);
    lower_cmp(&al2, ali2, &bl2, bli2) != core::cmp::Ordering::Greater
        && upper_cmp(&au2, aui2, &bu2, bui2) != core::cmp::Ordering::Less
}

/// Range `@>` element: `a` contains the scalar `e`.
fn range_contains_elem(
    ak: spg_storage::RangeKind,
    al: &Option<alloc::boxed::Box<Value<'static>>>,
    au: &Option<alloc::boxed::Box<Value<'static>>>,
    ali: bool,
    aui: bool,
    ae: bool,
    e: &Value<'_>,
) -> bool {
    use core::cmp::Ordering;
    if ae {
        return false;
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let lower_ok = match &al2 {
        None => true,
        Some(lv) => match bound_cmp(e, lv) {
            Ordering::Greater => true,
            Ordering::Equal => ali2,
            Ordering::Less => false,
        },
    };
    let upper_ok = match &au2 {
        None => true,
        Some(uv) => match bound_cmp(e, uv) {
            Ordering::Less => true,
            Ordering::Equal => aui2,
            Ordering::Greater => false,
        },
    };
    lower_ok && upper_ok
}

/// Range `*` intersection: the overlapping sub-range (empty if disjoint).
fn range_intersect(a: RangeParts<'_>, b: RangeParts<'_>) -> Value<'static> {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    let _ = bk;
    let empty = Value::Range {
        kind: ak,
        lower: None,
        upper: None,
        lower_inc: false,
        upper_inc: false,
        empty: true,
    };
    if ae || be {
        return empty;
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, bl, bu, bli, bui);
    // intersection lower = the later start, upper = the earlier end.
    let (lo, lo_inc) = if lower_cmp(&al2, ali2, &bl2, bli2) == core::cmp::Ordering::Greater {
        (al2, ali2)
    } else {
        (bl2, bli2)
    };
    let (up, up_inc) = if upper_cmp(&au2, aui2, &bu2, bui2) == core::cmp::Ordering::Less {
        (au2, aui2)
    } else {
        (bu2, bui2)
    };
    if !range_point_le(&lo, lo_inc, &up, up_inc) {
        return empty;
    }
    Value::Range {
        kind: ak,
        lower: lo.map(alloc::boxed::Box::new),
        upper: up.map(alloc::boxed::Box::new),
        lower_inc: lo_inc,
        upper_inc: up_inc,
        empty: false,
    }
}

/// Do bound `(up, up_inc)` and `(low, low_inc)` touch with no gap and no
/// overlap? (equal values, exactly one side inclusive — the `[x) [x)` seam).
fn bounds_touch(
    up: &Option<Value<'static>>,
    up_inc: bool,
    low: &Option<Value<'static>>,
    low_inc: bool,
) -> bool {
    match (up, low) {
        (Some(u), Some(l)) => bound_cmp(u, l) == core::cmp::Ordering::Equal && (up_inc != low_inc),
        _ => false,
    }
}

/// Range `+` union: the two ranges must overlap or be adjacent, else PG
/// errors "result of range union would not be contiguous".
fn range_union(a: RangeParts<'_>, b: RangeParts<'_>) -> Result<Value<'static>, EvalError> {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    let mk = |k,
              lo: &Option<alloc::boxed::Box<Value<'static>>>,
              up: &Option<alloc::boxed::Box<Value<'static>>>,
              li,
              ui,
              e| Value::Range {
        kind: k,
        lower: lo.clone(),
        upper: up.clone(),
        lower_inc: li,
        upper_inc: ui,
        empty: e,
    };
    if ae {
        return Ok(mk(bk, bl, bu, bli, bui, be));
    }
    if be {
        return Ok(mk(ak, al, au, ali, aui, ae));
    }
    let overlap = range_overlaps(
        RangeParts::new(ak, al, au, ali, aui, ae),
        RangeParts::new(bk, bl, bu, bli, bui, be),
    );
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, bl, bu, bli, bui);
    let adjacent = bounds_touch(&au2, aui2, &bl2, bli2) || bounds_touch(&bu2, bui2, &al2, ali2);
    if !overlap && !adjacent {
        return Err(EvalError::TypeMismatch {
            detail: "result of range union would not be contiguous".into(),
        });
    }
    // union lower = the earlier start, upper = the later end.
    let (lo, lo_inc) = if lower_cmp(&al2, ali2, &bl2, bli2) == core::cmp::Ordering::Less {
        (al2, ali2)
    } else {
        (bl2, bli2)
    };
    let (up, up_inc) = if upper_cmp(&au2, aui2, &bu2, bui2) == core::cmp::Ordering::Greater {
        (au2, aui2)
    } else {
        (bu2, bui2)
    };
    Ok(Value::Range {
        kind: ak,
        lower: lo.map(alloc::boxed::Box::new),
        upper: up.map(alloc::boxed::Box::new),
        lower_inc: lo_inc,
        upper_inc: up_inc,
        empty: false,
    })
}

/// Range `-` difference: the part of `a` not covered by `b`. PG errors when
/// removing `b` would split `a` into two disjoint ranges.
fn range_difference(a: RangeParts<'_>, b: RangeParts<'_>) -> Result<Value<'static>, EvalError> {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    let empty = Value::Range {
        kind: ak,
        lower: None,
        upper: None,
        lower_inc: false,
        upper_inc: false,
        empty: true,
    };
    let mk = |lo: Option<alloc::boxed::Box<Value<'static>>>,
              up: Option<alloc::boxed::Box<Value<'static>>>,
              li,
              ui| Value::Range {
        kind: ak,
        lower: lo,
        upper: up,
        lower_inc: li,
        upper_inc: ui,
        empty: false,
    };
    if ae {
        return Ok(empty);
    }
    let a_unchanged = || Value::Range {
        kind: ak,
        lower: al.clone(),
        upper: au.clone(),
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    };
    if be
        || !range_overlaps(
            RangeParts::new(ak, al, au, ali, aui, ae),
            RangeParts::new(bk, bl, bu, bli, bui, be),
        )
    {
        return Ok(a_unchanged());
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, bl, bu, bli, bui);
    // `b` covers `a` entirely → empty.
    let b_starts_at_or_before_a = lower_cmp(&al2, ali2, &bl2, bli2) != core::cmp::Ordering::Less;
    let b_ends_at_or_after_a = upper_cmp(&au2, aui2, &bu2, bui2) != core::cmp::Ordering::Greater;
    if b_starts_at_or_before_a && b_ends_at_or_after_a {
        return Ok(empty);
    }
    let a_extends_left = lower_cmp(&al2, ali2, &bl2, bli2) == core::cmp::Ordering::Less;
    let a_extends_right = upper_cmp(&au2, aui2, &bu2, bui2) == core::cmp::Ordering::Greater;
    // `b` sits strictly inside `a` → the difference would be two ranges.
    if a_extends_left && a_extends_right {
        return Err(EvalError::TypeMismatch {
            detail: "result of range difference would not be contiguous".into(),
        });
    }
    if a_extends_left {
        // Keep `a`'s left part: [a.lower, b.lower).
        Ok(mk(
            al2.map(alloc::boxed::Box::new),
            bl2.map(alloc::boxed::Box::new),
            ali2,
            !bli2,
        ))
    } else {
        // Keep `a`'s right part: [b.upper, a.upper).
        Ok(mk(
            bu2.map(alloc::boxed::Box::new),
            au2.map(alloc::boxed::Box::new),
            !bui2,
            aui2,
        ))
    }
}

/// MONEY arithmetic on integer cents (PG semantics): `money ± money → money`,
/// `money × number → money`, `money ÷ number → money`, `money ÷ money → float8`
/// ratio. Returns `None` when the operator/operand shape is not money math (let
/// the caller fall through). Rounding is half-away-from-zero, no_std-friendly.
#[allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss
)]
fn money_arith(
    op: BinOp,
    l: &Value<'_>,
    r: &Value<'_>,
) -> Option<Result<Value<'static>, EvalError>> {
    if !matches!(l, Value::Money(_)) && !matches!(r, Value::Money(_)) {
        return None;
    }
    // A plain number that scales a money amount.
    let factor = |v: &Value<'_>| -> Option<f64> {
        match v {
            Value::SmallInt(n) => Some(f64::from(*n)),
            Value::Int(n) => Some(f64::from(*n)),
            Value::BigInt(n) => Some(*n as f64),
            Value::Float(x) => Some(*x),
            Value::Numeric { scaled, scale, .. } => {
                Some(*scaled as f64 / 10i128.pow(u32::from(*scale)) as f64)
            }
            _ => None,
        }
    };
    let round_cents = |v: f64| -> i64 {
        if v >= 0.0 {
            (v + 0.5) as i64
        } else {
            (v - 0.5) as i64
        }
    };
    match (op, l, r) {
        (BinOp::Add, Value::Money(a), Value::Money(b)) => {
            Some(Ok(Value::Money(a.saturating_add(*b))))
        }
        (BinOp::Sub, Value::Money(a), Value::Money(b)) => {
            Some(Ok(Value::Money(a.saturating_sub(*b))))
        }
        (BinOp::Div, Value::Money(a), Value::Money(b)) => Some(if *b == 0 {
            Err(EvalError::DivisionByZero)
        } else {
            Ok(Value::Float(*a as f64 / *b as f64))
        }),
        (BinOp::Mul, Value::Money(a), other) | (BinOp::Mul, other, Value::Money(a)) => {
            factor(other).map(|f| Ok(Value::Money(round_cents(*a as f64 * f))))
        }
        (BinOp::Div, Value::Money(a), other) => factor(other).map(|f| {
            if f == 0.0 {
                Err(EvalError::DivisionByZero)
            } else {
                Ok(Value::Money(round_cents(*a as f64 / f)))
            }
        }),
        _ => None,
    }
}

/// Range `<<` "strictly left of": every point of `a` is less than every point
/// of `b`. True when `a`'s upper bound value is below `b`'s lower bound value;
/// at an equal boundary they must not both be inclusive (else the shared point
/// overlaps). Unbounded sides on the touching edges make it false.
fn range_strictly_left(a: RangeParts<'_>, b: RangeParts<'_>) -> bool {
    let RangeParts {
        kind: ak,
        lower: al,
        upper: au,
        lower_inc: ali,
        upper_inc: aui,
        empty: ae,
    } = a;
    let RangeParts {
        kind: bk,
        lower: bl,
        upper: bu,
        lower_inc: bli,
        upper_inc: bui,
        empty: be,
    } = b;
    use core::cmp::Ordering;
    if ae || be {
        return false;
    }
    let (_, _, au2, aui2) = range_canonical(ak, al, au, ali, aui);
    let (bl2, bli2, _, _) = range_canonical(bk, bl, bu, bli, bui);
    match (&au2, &bl2) {
        (None, _) | (_, None) => false,
        (Some(u), Some(l)) => match bound_cmp(u, l) {
            Ordering::Less => true,
            Ordering::Equal => !(aui2 && bli2),
            Ordering::Greater => false,
        },
    }
}

/// v7.39 (read01 rangetypes.c) — range `&<` "does not extend to the right
/// of": a's upper bound position <= b's (empty on either side → false; at
/// equal values an inclusive upper sits right of an exclusive one).
fn range_overleft(a: RangeParts<'_>, b: RangeParts<'_>) -> bool {
    use core::cmp::Ordering;
    if a.empty || b.empty {
        return false;
    }
    let (_, _, au, aui) = range_canonical(a.kind, a.lower, a.upper, a.lower_inc, a.upper_inc);
    let (_, _, bu, bui) = range_canonical(b.kind, b.lower, b.upper, b.lower_inc, b.upper_inc);
    match (&au, &bu) {
        (None, None) => true,
        (None, Some(_)) => false,
        (Some(_), None) => true,
        (Some(x), Some(y)) => match bound_cmp(x, y) {
            Ordering::Less => true,
            Ordering::Greater => false,
            Ordering::Equal => !aui || bui,
        },
    }
}

/// v7.39 (read01 rangetypes.c) — range `&>` "does not extend to the left
/// of": a's lower bound position >= b's (at equal values an exclusive lower
/// sits right of an inclusive one).
fn range_overright(a: RangeParts<'_>, b: RangeParts<'_>) -> bool {
    use core::cmp::Ordering;
    if a.empty || b.empty {
        return false;
    }
    let (al, ali, _, _) = range_canonical(a.kind, a.lower, a.upper, a.lower_inc, a.upper_inc);
    let (bl, bli, _, _) = range_canonical(b.kind, b.lower, b.upper, b.lower_inc, b.upper_inc);
    match (&al, &bl) {
        (None, None) => true,
        (None, Some(_)) => false,
        (Some(_), None) => true,
        (Some(x), Some(y)) => match bound_cmp(x, y) {
            Ordering::Greater => true,
            Ordering::Less => false,
            Ordering::Equal => !ali || bli,
        },
    }
}

/// Distance from a point to a segment, box, or circle (`point <-> geo`).
/// `None` unless the first operand is a point and the second is one of those
/// geometries.
/// v7.39 (read01 geo_ops.c part 2) — nearest point on segment (a,b) to pt
/// (projection clamped to the segment).
fn lseg_closest_to_point(
    a: &spg_storage::Point2D,
    b: &spg_storage::Point2D,
    pt: &spg_storage::Point2D,
) -> spg_storage::Point2D {
    let (vx, vy) = (b.x - a.x, b.y - a.y);
    let len2 = vx * vx + vy * vy;
    let t = if len2 == 0.0 {
        0.0
    } else {
        (((pt.x - a.x) * vx + (pt.y - a.y) * vy) / len2).clamp(0.0, 1.0)
    };
    spg_storage::Point2D {
        x: a.x + t * vx,
        y: a.y + t * vy,
    }
}

/// v7.39 (read01 geo_ops.c part 2) — intersection of Ax+By+C=0 lines
/// (PG line_interpt_line): parallel (epsilon slope test) → None; -0 is
/// normalized to 0.
fn line_line_interpt(
    a1: f64,
    b1: f64,
    c1: f64,
    a2: f64,
    b2: f64,
    c2: f64,
) -> Option<spg_storage::Point2D> {
    const EPS: f64 = 1.0e-6;
    let (x, y);
    if b1.abs() > EPS {
        if (a2 - a1 * (b2 / b1)).abs() <= EPS {
            return None;
        }
        x = (b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1);
        y = -(a1 * x + c1) / b1;
    } else if b2.abs() > EPS {
        if (a1 - a2 * (b1 / b2)).abs() <= EPS {
            return None;
        }
        x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2);
        y = -(a2 * x + c2) / b2;
    } else {
        return None;
    }
    Some(spg_storage::Point2D {
        x: if x == 0.0 { 0.0 } else { x },
        y: if y == 0.0 { 0.0 } else { y },
    })
}

fn pt_dist(a: &spg_storage::Point2D, b: &spg_storage::Point2D) -> f64 {
    let (dx, dy) = (a.x - b.x, a.y - b.y);
    sqrt_newton(dx * dx + dy * dy)
}

/// Segment-to-segment distance: 0 when they intersect, else the minimum
/// endpoint-to-segment distance over the four combinations (PG's
/// lseg_closept_lseg shape).
fn lseg_lseg_distance(
    a1: &spg_storage::Point2D,
    a2: &spg_storage::Point2D,
    b1: &spg_storage::Point2D,
    b2: &spg_storage::Point2D,
) -> f64 {
    if lseg_intersection(*a1, *a2, *b1, *b2).is_some() {
        return 0.0;
    }
    let d1 = pt_dist(&lseg_closest_to_point(a1, a2, b1), b1);
    let d2 = pt_dist(&lseg_closest_to_point(a1, a2, b2), b2);
    let d3 = pt_dist(&lseg_closest_to_point(b1, b2, a1), a1);
    let d4 = pt_dist(&lseg_closest_to_point(b1, b2, a2), a2);
    d1.min(d2).min(d3).min(d4)
}

/// Even-odd ray-casting point-in-polygon (shared by containment and the
/// point-polygon distance).
fn point_in_polygon(pt: &spg_storage::Point2D, pts: &[spg_storage::Point2D]) -> bool {
    if pts.len() < 3 {
        return false;
    }
    let mut inside = false;
    let mut j = pts.len() - 1;
    for i in 0..pts.len() {
        let (xi, yi) = (pts[i].x, pts[i].y);
        let (xj, yj) = (pts[j].x, pts[j].y);
        if ((yi > pt.y) != (yj > pt.y)) && (pt.x < (xj - xi) * (pt.y - yi) / (yj - yi) + xi) {
            inside = !inside;
        }
        j = i;
    }
    inside
}

/// The polygon's edges including the closure segment.
fn poly_edges(
    pts: &[spg_storage::Point2D],
) -> impl Iterator<Item = (&spg_storage::Point2D, &spg_storage::Point2D)> {
    (0..pts.len()).map(move |i| {
        let j = if i == 0 { pts.len() - 1 } else { i - 1 };
        (&pts[j], &pts[i])
    })
}

/// v7.39 (read01 geo_ops.c part 2) — distances the generic point arm
/// doesn't cover: lseg↔lseg, box↔lseg, point↔path, point↔polygon
/// (either operand order for the mixed pairs).
fn geo_pair_distance(l: &Value<'_>, r: &Value<'_>) -> Option<f64> {
    let one = |a: &Value<'_>, b: &Value<'_>| -> Option<f64> {
        match (a, b) {
            (Value::Lseg(a1, a2), Value::Lseg(b1, b2)) => Some(lseg_lseg_distance(a1, a2, b1, b2)),
            (Value::PgBox(ur, ll), Value::Lseg(s1, s2)) => {
                // Inside or crossing the box is distance 0; otherwise the
                // minimum over the four box edges (PG box_closept_lseg).
                let inside = |p: &spg_storage::Point2D| {
                    p.x >= ll.x.min(ur.x)
                        && p.x <= ll.x.max(ur.x)
                        && p.y >= ll.y.min(ur.y)
                        && p.y <= ll.y.max(ur.y)
                };
                if inside(s1) || inside(s2) {
                    return Some(0.0);
                }
                let (hx, hy) = (ll.x.max(ur.x), ll.y.max(ur.y));
                let (lx, ly) = (ll.x.min(ur.x), ll.y.min(ur.y));
                let c = [
                    spg_storage::Point2D { x: lx, y: ly },
                    spg_storage::Point2D { x: lx, y: hy },
                    spg_storage::Point2D { x: hx, y: hy },
                    spg_storage::Point2D { x: hx, y: ly },
                ];
                let mut best = f64::INFINITY;
                for i in 0..4 {
                    let j = (i + 1) % 4;
                    best = best.min(lseg_lseg_distance(&c[i], &c[j], s1, s2));
                }
                Some(best)
            }
            (Value::Point(pt), Value::Path { points, closed }) => {
                if points.is_empty() {
                    return None;
                }
                if points.len() == 1 {
                    return Some(pt_dist(pt, &points[0]));
                }
                let mut best = f64::INFINITY;
                for i in 0..points.len() {
                    if i == 0 && !closed {
                        continue;
                    }
                    let j = if i == 0 { points.len() - 1 } else { i - 1 };
                    let n = lseg_closest_to_point(&points[j], &points[i], pt);
                    best = best.min(pt_dist(&n, pt));
                }
                Some(best)
            }
            (Value::Point(pt), Value::Polygon(pts)) => {
                if point_in_polygon(pt, pts) {
                    return Some(0.0);
                }
                let mut best = f64::INFINITY;
                for (a, b) in poly_edges(pts) {
                    let n = lseg_closest_to_point(a, b, pt);
                    best = best.min(pt_dist(&n, pt));
                }
                Some(best)
            }
            _ => None,
        }
    };
    one(l, r).or_else(|| one(r, l))
}

/// v7.39 (read01 geo_ops.c part 2) — direction-sensitive geometric
/// containments beyond point-in-shape: polygon⊃polygon (bbox gate +
/// every vertex and edge midpoint of the contained polygon inside the
/// container — a clean-room simplification of PG's lseg_inside_poly
/// recursion; concave edge-crossing corner cases are a recorded
/// residual), and circle⊃circle.
fn geo_contains_geo(container: &Value<'_>, contained: &Value<'_>) -> Option<bool> {
    const EPS: f64 = 1.0e-6;
    match (container, contained) {
        (Value::Polygon(a), Value::Polygon(b)) => {
            if a.len() < 3 || b.is_empty() {
                return Some(false);
            }
            let on_edge = |p: &spg_storage::Point2D| {
                poly_edges(a).any(|(e1, e2)| {
                    (pt_dist(p, e1) + pt_dist(p, e2) - pt_dist(e1, e2)).abs() <= EPS
                })
            };
            let inside = |p: &spg_storage::Point2D| point_in_polygon(p, a) || on_edge(p);
            if !b.iter().all(|p| inside(p)) {
                return Some(false);
            }
            let mids = (0..b.len()).all(|i| {
                let j = if i == 0 { b.len() - 1 } else { i - 1 };
                let m = spg_storage::Point2D {
                    x: (b[i].x + b[j].x) / 2.0,
                    y: (b[i].y + b[j].y) / 2.0,
                };
                inside(&m)
            });
            Some(mids)
        }
        (
            Value::Circle {
                center: c1,
                radius: r1,
            },
            Value::Circle {
                center: c2,
                radius: r2,
            },
        ) => Some(pt_dist(c1, c2) + r2 <= r1 + EPS),
        _ => None,
    }
}

/// v7.39 (read01 geo_ops.c part 2) — containers valid only on the `<@`
/// side (PG has point <@ lseg / point <@ path but no lseg @> point).
fn geo_pt_on_object(container: &Value<'_>, p: &Value<'_>) -> Option<bool> {
    const EPS: f64 = 1.0e-6;
    let Value::Point(pt) = p else { return None };
    match container {
        Value::Lseg(a, b) => Some((pt_dist(pt, a) + pt_dist(pt, b) - pt_dist(a, b)).abs() <= EPS),
        Value::Path { points, closed } => {
            if points.is_empty() {
                return Some(false);
            }
            if points.len() == 1 {
                return Some(pt_dist(pt, &points[0]) <= EPS);
            }
            let on_any = (0..points.len()).any(|i| {
                if i == 0 && !closed {
                    return false;
                }
                let j = if i == 0 { points.len() - 1 } else { i - 1 };
                (pt_dist(pt, &points[j]) + pt_dist(pt, &points[i])
                    - pt_dist(&points[j], &points[i]))
                .abs()
                    <= EPS
            });
            Some(on_any)
        }
        _ => None,
    }
}

fn point_geo_distance(p: &Value<'_>, geo: &Value<'_>) -> Option<f64> {
    let Value::Point(pt) = p else { return None };
    match geo {
        Value::Lseg(a, b) => {
            // Nearest point on the segment: project pt, clamp t to [0,1].
            let (vx, vy) = (b.x - a.x, b.y - a.y);
            let len2 = vx * vx + vy * vy;
            let t = if len2 == 0.0 {
                0.0
            } else {
                (((pt.x - a.x) * vx + (pt.y - a.y) * vy) / len2).clamp(0.0, 1.0)
            };
            let (nx, ny) = (a.x + t * vx, a.y + t * vy);
            let (dx, dy) = (pt.x - nx, pt.y - ny);
            Some(sqrt_newton(dx * dx + dy * dy))
        }
        Value::PgBox(ur, ll) => {
            // Clamp the point into the box; distance to the clamped point.
            let nx = pt.x.clamp(ll.x, ur.x);
            let ny = pt.y.clamp(ll.y, ur.y);
            let (dx, dy) = (pt.x - nx, pt.y - ny);
            Some(sqrt_newton(dx * dx + dy * dy))
        }
        Value::Circle { center, radius } => {
            let (dx, dy) = (pt.x - center.x, pt.y - center.y);
            let d = sqrt_newton(dx * dx + dy * dy) - radius;
            Some(if d < 0.0 { 0.0 } else { d })
        }
        // Point-to-line distance: PG's line_closept_point drops a
        // perpendicular through the point and measures to the
        // intersection — same math as |Ax+By+C|/√(A²+B²) but the
        // operation order matters to the last ULP, so mirror it.
        Value::Line { a, b, c } => {
            const EPS: f64 = 1.0e-6;
            // Perpendicular through pt: slope of the line is -a/b
            // (vertical when |b|≈0), the perpendicular inverts it.
            let (pa, pb, pc) = if b.abs() <= EPS {
                // Line is vertical → perpendicular is horizontal y = pt.y.
                (0.0, -1.0, pt.y)
            } else {
                let m = -a / b;
                let im = if m.abs() <= EPS {
                    // Horizontal line → vertical perpendicular x = pt.x.
                    return line_line_interpt(-1.0, 0.0, pt.x, *a, *b, *c)
                        .map(|ip| pt_dist(&ip, pt));
                } else {
                    -1.0 / m
                };
                (im, -1.0, pt.y - im * pt.x)
            };
            line_line_interpt(pa, pb, pc, *a, *b, *c).map(|ip| pt_dist(&ip, pt))
        }
        _ => None,
    }
}

/// Collect every lexeme (Term word) appearing anywhere in a tsquery tree,
/// ignoring the combining operators — the set used by `tsquery @> tsquery`.
fn tsquery_lexemes(
    q: &spg_storage::TsQueryAst,
    out: &mut alloc::collections::BTreeSet<alloc::string::String>,
) {
    use spg_storage::TsQueryAst;
    match q {
        TsQueryAst::Term { word, .. } => {
            out.insert(word.clone());
        }
        TsQueryAst::And(a, b) | TsQueryAst::Or(a, b) => {
            tsquery_lexemes(a, out);
            tsquery_lexemes(b, out);
        }
        TsQueryAst::Not(a) => tsquery_lexemes(a, out),
        TsQueryAst::Phrase { left, right, .. } => {
            tsquery_lexemes(left, out);
            tsquery_lexemes(right, out);
        }
    }
}

/// Geometric containment `container @> box` for a box container: the argument
/// box lies inside (or on the boundary of) the container box. `None` unless
/// both operands are boxes.
fn geo_contains_box(container: &Value<'_>, inner: &Value<'_>) -> Option<bool> {
    let (Value::PgBox(cur, cll), Value::PgBox(iur, ill)) = (container, inner) else {
        return None;
    };
    Some(ill.x >= cll.x && iur.x <= cur.x && ill.y >= cll.y && iur.y <= cur.y)
}

/// Geometric overlap `a && b` for same-type box or circle: boxes overlap when
/// their x- and y-projections both overlap; circles overlap when the distance
/// between centres is ≤ the sum of the radii. `None` for other operand pairs.
fn geo_overlaps(a: &Value<'_>, b: &Value<'_>) -> Option<bool> {
    match (a, b) {
        // v7.39 (read01 geo_ops.c part 2) — polygon overlap: any vertex of
        // one inside the other, or any pair of edges crossing.
        (Value::Polygon(pa), Value::Polygon(pb)) => {
            if pa.len() < 3 || pb.len() < 3 {
                return Some(false);
            }
            if pa.iter().any(|p| point_in_polygon(p, pb))
                || pb.iter().any(|p| point_in_polygon(p, pa))
            {
                return Some(true);
            }
            for (a1, a2) in poly_edges(pa) {
                for (b1, b2) in poly_edges(pb) {
                    if lseg_intersection(*a1, *a2, *b1, *b2).is_some() {
                        return Some(true);
                    }
                }
            }
            Some(false)
        }
        (Value::PgBox(aur, all), Value::PgBox(bur, bll)) => {
            Some(all.x <= bur.x && bll.x <= aur.x && all.y <= bur.y && bll.y <= aur.y)
        }
        (
            Value::Circle {
                center: c1,
                radius: r1,
            },
            Value::Circle {
                center: c2,
                radius: r2,
            },
        ) => {
            let (dx, dy) = (c1.x - c2.x, c1.y - c2.y);
            let rsum = r1 + r2;
            Some(dx * dx + dy * dy <= rsum * rsum)
        }
        _ => None,
    }
}

/// Geometric containment `container @> point`: whether the point lies inside
/// (or on the boundary of) a polygon (even-odd ray cast), box (bounding-box
/// test), or circle (distance ≤ radius). `None` if the operands are not a
/// geometry/point pair.
fn geo_contains_point(container: &Value<'_>, p: &Value<'_>) -> Option<bool> {
    let Value::Point(pt) = p else { return None };
    match container {
        Value::PgBox(ur, ll) => Some(pt.x >= ll.x && pt.x <= ur.x && pt.y >= ll.y && pt.y <= ur.y),
        Value::Circle { center, radius } => {
            let (dx, dy) = (pt.x - center.x, pt.y - center.y);
            Some(dx * dx + dy * dy <= radius * radius)
        }
        Value::Polygon(pts) => {
            if pts.len() < 3 {
                return Some(false);
            }
            // Even-odd ray-casting: count crossings of a ray going +x.
            let mut inside = false;
            let mut j = pts.len() - 1;
            for i in 0..pts.len() {
                let (xi, yi) = (pts[i].x, pts[i].y);
                let (xj, yj) = (pts[j].x, pts[j].y);
                if ((yi > pt.y) != (yj > pt.y)) && (pt.x < (xj - xi) * (pt.y - yi) / (yj - yi) + xi)
                {
                    inside = !inside;
                }
                j = i;
            }
            Some(inside)
        }
        _ => None,
    }
}

/// Range `-|-` "is adjacent to": `a` and `b` touch at exactly one bound with
/// no gap and no overlap — one range's upper bound value equals the other's
/// lower bound value, and exactly one of the two touching bounds is inclusive.
pub(crate) fn range_adjacent_pair(a: &Value<'_>, b: &Value<'_>) -> Option<bool> {
    use core::cmp::Ordering;
    // v7.39 (round 256) — `-|-` reads a multirange as its outer hull
    // (probed: `{[1,3),[9,11)} -|- {[3,5)}` is FALSE, so it is not an
    // any-element rule), and an empty multirange is never adjacent.
    if matches!(a, Value::Multirange { .. }) || matches!(b, Value::Multirange { .. }) {
        let hull = |v: &Value<'_>| -> Option<Value<'static>> {
            match v {
                Value::Multirange { kind, ranges } => {
                    if ranges.is_empty() {
                        return None;
                    }
                    Some(multirange_hull(*kind, ranges))
                }
                Value::Range { .. } => Some(v.clone().into_owned()),
                _ => None,
            }
        };
        let (Some(ha), Some(hb)) = (hull(a), hull(b)) else {
            return Some(false);
        };
        return range_adjacent_pair(&ha, &hb);
    }
    let (ak, al, au, ali, aui, ae) = match a {
        Value::Range {
            kind,
            lower,
            upper,
            lower_inc,
            upper_inc,
            empty,
        } => (
            *kind,
            lower.clone(),
            upper.clone(),
            *lower_inc,
            *upper_inc,
            *empty,
        ),
        _ => return None,
    };
    let (bk, bl, bu, bli, bui, be) = match b {
        Value::Range {
            kind,
            lower,
            upper,
            lower_inc,
            upper_inc,
            empty,
        } => (
            *kind,
            lower.clone(),
            upper.clone(),
            *lower_inc,
            *upper_inc,
            *empty,
        ),
        _ => return None,
    };
    if ae || be {
        return Some(false);
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, &al, &au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, &bl, &bu, bli, bui);
    // a's upper touches b's lower.
    let a_left_of_b = match (&au2, &bl2) {
        (Some(u), Some(l)) => bound_cmp(u, l) == Ordering::Equal && (aui2 != bli2),
        _ => false,
    };
    // b's upper touches a's lower.
    let b_left_of_a = match (&bu2, &al2) {
        (Some(u), Some(l)) => bound_cmp(u, l) == Ordering::Equal && (bui2 != ali2),
        _ => false,
    };
    Some(a_left_of_b || b_left_of_a)
}

/// `range_merge(a, b)` — the smallest range that contains both `a` and `b`.
/// Unlike `+` union it never errors on a gap: the result spans it (earliest
/// start to latest end). An empty operand contributes nothing. Returns `None`
/// if either value is not a range (caller falls back).
pub(crate) fn range_merge_pair(a: &Value<'_>, b: &Value<'_>) -> Option<Value<'static>> {
    let (ak, al, au, ali, aui, ae) = match a {
        Value::Range {
            kind,
            lower,
            upper,
            lower_inc,
            upper_inc,
            empty,
        } => (
            *kind,
            lower.clone(),
            upper.clone(),
            *lower_inc,
            *upper_inc,
            *empty,
        ),
        _ => return None,
    };
    let (bk, bl, bu, bli, bui, be) = match b {
        Value::Range {
            kind,
            lower,
            upper,
            lower_inc,
            upper_inc,
            empty,
        } => (
            *kind,
            lower.clone(),
            upper.clone(),
            *lower_inc,
            *upper_inc,
            *empty,
        ),
        _ => return None,
    };
    let mk = |k,
              lo: Option<alloc::boxed::Box<Value<'static>>>,
              up: Option<alloc::boxed::Box<Value<'static>>>,
              li,
              ui,
              e| {
        Value::Range {
            kind: k,
            lower: lo,
            upper: up,
            lower_inc: li,
            upper_inc: ui,
            empty: e,
        }
    };
    if ae {
        return Some(mk(bk, bl, bu, bli, bui, be));
    }
    if be {
        return Some(mk(ak, al, au, ali, aui, ae));
    }
    let (al2, ali2, au2, aui2) = range_canonical(ak, &al, &au, ali, aui);
    let (bl2, bli2, bu2, bui2) = range_canonical(bk, &bl, &bu, bli, bui);
    // merge lower = the earlier start, upper = the later end.
    let (lo, lo_inc) = if lower_cmp(&al2, ali2, &bl2, bli2) == core::cmp::Ordering::Less {
        (al2, ali2)
    } else {
        (bl2, bli2)
    };
    let (up, up_inc) = if upper_cmp(&au2, aui2, &bu2, bui2) == core::cmp::Ordering::Greater {
        (au2, aui2)
    } else {
        (bu2, bui2)
    };
    Some(mk(
        ak,
        lo.map(alloc::boxed::Box::new),
        up.map(alloc::boxed::Box::new),
        lo_inc,
        up_inc,
        false,
    ))
}

/// The numeric value of an inet/cidr address (IPv4 in the low 4 bytes,
/// IPv6 across all 16), MSB-first.
fn inet_addr_u128(family: u8, addr: &[u8; 16]) -> u128 {
    let slice: &[u8] = if family == 4 { &addr[0..4] } else { &addr[..] };
    slice
        .iter()
        .fold(0u128, |acc, &b| (acc << 8) | u128::from(b))
}

/// Rebuild a `Value::Inet` from a numeric address (inverse of
/// `inet_addr_u128`), MSB-packing the low 4 (IPv4) or 16 (IPv6) bytes.
fn inet_from_u128(family: u8, bits: u8, val: u128) -> Value<'static> {
    let mut addr = [0u8; 16];
    if family == 4 {
        #[allow(clippy::cast_possible_truncation)]
        addr[0..4].copy_from_slice(&(val as u32).to_be_bytes());
    } else {
        addr.copy_from_slice(&val.to_be_bytes());
    }
    Value::Inet { family, bits, addr }
}

/// v7.38 (read01) — PG `inet & inet` / `inet | inet`: both must be the same
/// family, the addresses combine bitwise, and the result netmask is the wider
/// of the two (`10.0.0.0/8 & 255.0.0.0` → `10.0.0.0/32`).
fn inet_bitwise(op: BinOp, l: &Value<'_>, r: &Value<'_>) -> Result<Value<'static>, EvalError> {
    let (
        Value::Inet {
            family: fa,
            bits: ba,
            addr: aa,
        },
        Value::Inet {
            family: fb,
            bits: bb,
            addr: ab,
        },
    ) = (l, r)
    else {
        return Err(EvalError::TypeMismatch {
            detail: "inet bitwise operator needs two inet values".into(),
        });
    };
    if fa != fb {
        return Err(EvalError::TypeMismatch {
            detail: "cannot AND/OR/XOR inet values of different sizes".into(),
        });
    }
    let x = inet_addr_u128(*fa, aa);
    let y = inet_addr_u128(*fb, ab);
    let val = match op {
        BinOp::BitAnd => x & y,
        BinOp::BitOr => x | y,
        _ => {
            return Err(EvalError::TypeMismatch {
                detail: "unsupported inet bitwise operator".into(),
            });
        }
    };
    Ok(inet_from_u128(*fa, (*ba).max(*bb), val))
}

/// v7.39 (read01 round 79) — PG's message for "no operator takes these two
/// types": `operator does not exist: text >= integer`. SPG used to print
/// `comparison between Some(Text) and Some(Int)` — a Rust `Option`/`Debug`
/// dump in a user-facing error, and nothing a driver could match on. The
/// SQLSTATE (42883) already keyed off PG's phrasing, so the wire class was
/// right while the text was not.
/// v7.39 (round 238) — the operator-resolution gate `=` already applies,
/// reusable by the constructs that compare WITHOUT going through `compare`:
/// `IS DISTINCT FROM`, `NULLIF` and `IN`. All three answered happily across
/// types PG has no operator for — `1 IS DISTINCT FROM 'a'::text` was `true`,
/// `nullif(1, 'a'::text)` was `1`, `1 IN (1, 'a'::text)` was `true` — so a
/// predicate that PG rejects outright silently decided a row's fate.
pub(super) fn require_comparable(op: BinOp, a: &Value<'_>, b: &Value<'_>) -> Result<(), EvalError> {
    if a.is_null() || b.is_null() {
        return Ok(());
    }
    // `compare` is the authority: if it can answer, the pair is comparable.
    match compare(op, a, b) {
        Ok(_) => Ok(()),
        Err(e) => Err(e),
    }
}

fn no_such_operator(op: BinOp, a: &Value<'_>, b: &Value<'_>) -> EvalError {
    // `pg_typeof_name` is the FULL value→type-name table (the one pg_typeof
    // itself answers from); `pg_typeof_name_for_datatype` covers only the
    // numeric/temporal subset and returns None for text, which is exactly the
    // type this error is most often about.
    EvalError::TypeMismatch {
        detail: alloc::format!(
            "operator does not exist: {} {op} {}",
            super::strings::pg_typeof_name(a),
            super::strings::pg_typeof_name(b)
        ),
    }
}

/// v7.39 (round 641) — a transaction id has EQUALITY and nothing else.
///
/// Measured on PG18: `xid = xid`, `xid <> xid`, `xid = integer` and
/// `xid <> integer` all answer; `xid < xid`, `<=`, `>`, `>=` and
/// `xid < integer` are all "operator does not exist". The counter wraps,
/// so `<` between two ids does not mean what it reads like — PG declines
/// rather than answer wrongly, and SPG answered all four.
///
/// The integer pair is asymmetric, deliberately: PG has `xideqint4` and
/// no commutator, so `1 = '1'::xid` is an error there too. Matching that
/// is not pedantry — a query written the reversed way works here and
/// breaks on the database SPG stands in for.
///
/// An unknown-typed literal reads through the type's input function on
/// either side: `'1'::xid = '1'` and `'1' = '1'::xid` both answer on PG,
/// so a Text operand coerces rather than being refused. SPG models an
/// unknown literal as Text and cannot tell it from a real `::text`,
/// which PG DOES refuse; that gap is the unknown-type model's, not this
/// rule's.
///
/// `#[cold]` and out of line on purpose — see the call site.
#[cold]
#[inline(never)]
fn xid_compare(op: BinOp, l: &Value<'_>, r: &Value<'_>) -> Result<Value<'static>, EvalError> {
    if l.is_null() || r.is_null() {
        return Ok(Value::Null);
    }
    let as_i64 = |v: &Value<'_>| -> Option<i64> {
        match v {
            Value::Xid(x) => Some(i64::from(*x)),
            Value::SmallInt(n) => Some(i64::from(*n)),
            Value::Int(n) => Some(i64::from(*n)),
            Value::BigInt(n) => Some(*n),
            Value::Text(s) => s.parse::<i64>().ok(),
            _ => None,
        }
    };
    let equality = matches!(op, BinOp::Eq | BinOp::NotEq);
    let integer_on_left = matches!(l, Value::SmallInt(_) | Value::Int(_) | Value::BigInt(_));
    match (equality, integer_on_left, as_i64(l), as_i64(r)) {
        (true, false, Some(a), Some(b)) => cmp_result(op, a.cmp(&b)),
        _ => Err(no_such_operator(op, l, r)),
    }
}

pub(super) fn compare(
    op: BinOp,
    l: &Value<'_>,
    r: &Value<'_>,
) -> Result<Value<'static>, EvalError> {
    // v7.39 (round 483) — same-variant scalars compare straight away.
    //
    // Round 482 removed the per-row `Value` churn and `compare` became the
    // dominant cost: 35.6 % of self time on `g = 5`, with `Value::data_type`
    // another 4.5 % — both spent on the pre-checks below before reaching
    // the ordering match at the bottom.
    //
    // None of those pre-checks can fire for these pairs. Each is gated on
    // one side being Text against a TYPED other (Text-vs-Text is explicitly
    // left alone), on `NumericBig`, or on `BpChar` — all different variants
    // from the ones matched here. So this is the same answer by the same
    // arms, reached without walking past four guards that cannot apply.
    match (l, r) {
        (Value::Int(a), Value::Int(b)) => {
            return cmp_result(op, a.cmp(b));
        }
        (Value::BigInt(a), Value::BigInt(b)) => {
            return cmp_result(op, a.cmp(b));
        }
        (Value::SmallInt(a), Value::SmallInt(b)) => {
            return cmp_result(op, a.cmp(b));
        }
        (Value::Int(a), Value::BigInt(b)) => {
            return cmp_result(op, i64::from(*a).cmp(b));
        }
        (Value::BigInt(a), Value::Int(b)) => {
            return cmp_result(op, a.cmp(&i64::from(*b)));
        }
        (Value::Text(a), Value::Text(b)) => {
            return cmp_result(op, a.cmp(b));
        }
        (Value::Bool(a), Value::Bool(b)) => {
            return cmp_result(op, a.cmp(b));
        }
        // v7.39 (round 641) — a transaction id has equality and no
        // ordering; `xid_compare` holds the rule and the reasons.
        //
        // It is dispatched from THIS match rather than a test of its
        // own below, and that is a measured decision, not tidiness.
        // Written as `if matches!(l, Xid) || matches!(r, Xid)` after
        // the match, `count(DISTINCT g)` — which has no xid anywhere —
        // went 6.9 ms to 8.7 ms: two extra discriminant tests on every
        // one of 500 000 rows. Here the switch is already happening
        // and the arm is free. An even earlier version put the whole
        // rule inline and cost `WHERE g BETWEEN 10 AND 20` 17x, by
        // growing `compare` past what the row loop can inline.
        (Value::Xid(_), _) | (_, Value::Xid(_)) => return xid_compare(op, l, r),
        _ => {}
    }
    // v7.39 (round 641) — a transaction id has EQUALITY and nothing else.
    // Measured on PG18: `xid = xid`, `xid <> xid`, `xid = integer` and
    // `xid <> integer` all answer; `xid < xid`, `<=`, `>`, `>=` and
    // `xid < integer` are all "operator does not exist". The reason is
    // that the counter wraps, so `<` between two ids does not mean what
    // it reads like — PG declines rather than answer wrongly, and SPG
    // answered all four.
    //
    // The pair is asymmetric, and deliberately: PG has `xideqint4` but
    // no commutator, so `1 = '1'::xid` is an error there too. Matching
    // that is not pedantry — a query that relies on the reversed
    // spelling works here and breaks on the database SPG stands in for.
    //
    // Placed AFTER the same-variant fast path above, which cannot match
    // an Xid, so an integer comparison never pays for this.
    // PG implicitly casts an unknown-type string literal to the other
    // operand's type (`i = '5'`, `1 = '1'`, `2 < '10'`). When one side is Text
    // and the other is a typed scalar, coerce the Text to that type first.
    // Text-vs-Text stays a string comparison, and Bool has its own arm, so
    // both are left alone.
    //
    // v7.39 (round 704) — an UNKNOWN numeric-family literal that won't parse
    // raises the type's input-function error in PG (`invalid input syntax
    // for type integer: "abc"`); an explicit `::text` operand raises the
    // OPERATOR error (`1 IS DISTINCT FROM 'a'::text`, measured). This
    // function sees two Values and cannot tell those apart — the first cut
    // of round 704 propagated the input error from HERE and broke the
    // typed-text shapes (the r238 pin and corpus 19 both caught it). The
    // distinction lives where the Expr is visible: the eval Binary arm
    // rewrites the fall-through error for an unknown string literal, and
    // the predicate compiler bails that shape to the tree evaluator.
    let needs_text_coerce = |other: &Value<'_>| -> Option<DataType> {
        match other.data_type() {
            // Text-vs-Text is a string comparison; an untyped operand can't
            // drive a coercion. Everything else (numerics, bool, date/time,
            // inet, uuid, …) coerces the Text side to it — e.g. `true = 't'`.
            Some(DataType::Text) | None => None,
            dt => dt,
        }
    };
    if let (Value::Text(s), Some(dt)) = (l, needs_text_coerce(r)) {
        if let Ok(c) = crate::conversions::coerce_value(Value::text(s.as_ref()), dt, "", 0) {
            return compare(op, &c, r);
        }
    }
    if let (Some(dt), Value::Text(s)) = (needs_text_coerce(l), r) {
        if let Ok(c) = crate::conversions::coerce_value(Value::text(s.as_ref()), dt, "", 0) {
            return compare(op, l, &c);
        }
    }
    // v7.38 (read01, T3.C3) — a NUMERIC beyond i128 compares via bignum (the
    // finite path below would mis-read its canonical form).
    if matches!(l, Value::NumericBig(_)) || matches!(r, Value::NumericBig(_)) {
        if let (Some(a), Some(b)) = (value_to_bignum(l), value_to_bignum(r)) {
            return Ok(Value::Bool(cmp_to_bool(op, a.cmp(&b))));
        }
    }
    // v7.38 (read01, T11) — bpchar compares blank-insensitively: when either
    // side is bpchar, both text-like operands are compared with trailing blanks
    // trimmed (`'ab'::char(4) = 'ab'` is true).
    if matches!(l, Value::BpChar(_)) || matches!(r, Value::BpChar(_)) {
        let trimmed = |v: &Value<'_>| -> Option<alloc::string::String> {
            match v {
                Value::BpChar(s) | Value::Text(s) => Some(s.trim_end_matches(' ').to_string()),
                _ => None,
            }
        };
        if let (Some(a), Some(b)) = (trimmed(l), trimmed(r)) {
            return cmp_result(op, a.cmp(&b));
        }
    }
    let ord = match (l, r) {
        (Value::Int(a), Value::Int(b)) => i64::from(*a).cmp(&i64::from(*b)),
        (Value::Int(a), Value::BigInt(b)) => i64::from(*a).cmp(b),
        (Value::BigInt(a), Value::Int(b)) => a.cmp(&i64::from(*b)),
        (Value::BigInt(a), Value::BigInt(b)) => a.cmp(b),
        // v7.38 (read01) — NUMERIC vs NUMERIC / integer compares exactly on a
        // shared scale (bare decimal literals are numeric now, so `n = 9.99`
        // lands here). A numeric-vs-float mix still falls to the float arm.
        (a, b)
            if (matches!(a, Value::Numeric { .. } | Value::NumericBig(_))
                || matches!(b, Value::Numeric { .. } | Value::NumericBig(_)))
                && !matches!(a.data_type(), Some(DataType::Float))
                && !matches!(b.data_type(), Some(DataType::Float)) =>
        {
            // Inline widen (numeric_or_widen wants `&Value<'static>`; these
            // operands are shorter-lived but only their Copy fields are read).
            let widen = |v: &Value<'_>| -> Option<(i128, u16)> {
                match v {
                    Value::Numeric { scaled, scale, .. } => Some((*scaled, *scale)),
                    Value::Int(n) => Some((i128::from(*n), 0)),
                    Value::SmallInt(n) => Some((i128::from(*n), 0)),
                    Value::BigInt(n) => Some((i128::from(*n), 0)),
                    _ => None,
                }
            };
            // v7.39 (read01 round 79) — PG's wording, and without leaking Rust's
            // `Some(Text)` debug form into a user-facing error:
            // "operator does not exist: text >= integer".
            let mismatch = || no_such_operator(op, a, b);
            let (na, sa) = widen(a).ok_or_else(mismatch)?;
            let (nb, sb) = widen(b).ok_or_else(mismatch)?;
            let target = sa.max(sb);
            let lhs = rescale(na, sa, target).ok_or_else(|| EvalError::TypeMismatch {
                detail: "NUMERIC overflow on rescale".into(),
            })?;
            let rhs = rescale(nb, sb, target).ok_or_else(|| EvalError::TypeMismatch {
                detail: "NUMERIC overflow on rescale".into(),
            })?;
            lhs.cmp(&rhs)
        }
        (a, b)
            if matches!(a.data_type(), Some(DataType::Float | DataType::Real))
                || matches!(b.data_type(), Some(DataType::Float | DataType::Real)) =>
        {
            let af = as_f64(a)?;
            let bf = as_f64(b)?;
            // PG total float order: NaN == NaN, NaN > any number.
            float_pg_cmp(af, bf)
        }
        (Value::Text(a), Value::Text(b)) => a.cmp(b),
        (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
        // Date / Timestamp compare on their integer storage repr.
        // Cross-domain (Date vs Timestamp) lifts the Date to the
        // matching midnight TIMESTAMP first.
        (Value::Date(a), Value::Date(b)) => a.cmp(b),
        (Value::Timestamp(a), Value::Timestamp(b)) => a.cmp(b),
        (Value::Date(a), Value::Timestamp(b)) => (i64::from(*a) * 86_400_000_000).cmp(b),
        (Value::Timestamp(a), Value::Date(b)) => a.cmp(&(i64::from(*b) * 86_400_000_000)),
        // INTERVAL compares by PG's canonical microsecond span:
        // months count as 30 days and days as 24 hours, so
        // `INTERVAL '1 month' = INTERVAL '30 days'` and
        // `INTERVAL '1 day' = INTERVAL '24 hours'` (i128 keeps the
        // months*30*86_400e6 product from overflowing i64).
        (
            Value::Interval {
                months: am,
                days: ad,
                micros: au,
            },
            Value::Interval {
                months: bm,
                days: bd,
                micros: bu,
            },
        ) => {
            let span = |m: i32, d: i32, u: i64| -> i128 {
                (i128::from(m) * 30 + i128::from(d)) * 86_400_000_000 + i128::from(u)
            };
            span(*am, *ad, *au).cmp(&span(*bm, *bd, *bu))
        }
        // PG-style implicit coercion: comparing a DATE / TIMESTAMP
        // column against a text literal lifts the literal into the
        // matching domain (e.g. `day >= '2024-01-01'`).
        //
        // v7.39 (round 704) — a literal that will not lift fails with the
        // TYPE's own input-function wording (`invalid input syntax for
        // type date: "notadate"`), measured off PG18. The old "cannot
        // parse … for comparison" described the same fact in SPG's words,
        // which nothing matching PG's shape recognises.
        (Value::Date(a), Value::Text(b)) => {
            let bd = parse_date_literal(b).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid input syntax for type date: \"{b}\""),
            })?;
            a.cmp(&bd)
        }
        (Value::Text(a), Value::Date(b)) => {
            let ad = parse_date_literal(a).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid input syntax for type date: \"{a}\""),
            })?;
            ad.cmp(b)
        }
        (Value::Timestamp(a), Value::Text(b)) => {
            let bt = parse_timestamp_literal(b).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid input syntax for type timestamp: \"{b}\""),
            })?;
            a.cmp(&bt)
        }
        (Value::Text(a), Value::Timestamp(b)) => {
            let at = parse_timestamp_literal(a).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid input syntax for type timestamp: \"{a}\""),
            })?;
            at.cmp(b)
        }
        // v7.17.0 — UUID byte-wise comparison; both sides UUID.
        (Value::Uuid(a), Value::Uuid(b)) => a.cmp(b),
        // v7.17.0 — PG promotes a `text` literal compared against a
        // `uuid` column into uuid (unknown-type literal inference).
        // Without this, `WHERE id = '550e...'` falls through to the
        // generic TypeMismatch — the application's literal becomes
        // an error rather than a comparison.
        (Value::Uuid(a), Value::Text(b)) => {
            let bu = spg_storage::parse_uuid_str(b).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid input syntax for type uuid: {b:?}"),
            })?;
            a.cmp(&bu)
        }
        (Value::Text(a), Value::Uuid(b)) => {
            let au = spg_storage::parse_uuid_str(a).ok_or_else(|| EvalError::TypeMismatch {
                detail: format!("invalid input syntax for type uuid: {a:?}"),
            })?;
            au.cmp(b)
        }
        // v7.37.16 — element-wise array ordering for `=` / `<>` /
        // `<` / `<=` / `>` / `>=`. PG compares same-type arrays with
        // `array_cmp`; see `cmp_array` for the NULL / length rules.
        (Value::IntArray(a), Value::IntArray(b)) => cmp_array(a, b),
        (Value::BigIntArray(a), Value::BigIntArray(b)) => cmp_array(a, b),
        (Value::SmallIntArray(a), Value::SmallIntArray(b)) => cmp_array(a, b),
        (Value::TextArray(a), Value::TextArray(b)) => cmp_array(a, b),
        (Value::VarcharArray(a), Value::VarcharArray(b)) => cmp_array(a, b),
        (Value::BoolArray(a), Value::BoolArray(b)) => cmp_array(a, b),
        (Value::DateArray(a), Value::DateArray(b)) => cmp_array(a, b),
        (Value::TimestampArray(a), Value::TimestampArray(b)) => cmp_array(a, b),
        // Int / BigInt arrays cross-compare after widening the
        // narrower side (mirrors the `||` widening rules).
        (Value::IntArray(a), Value::BigIntArray(b)) => {
            let aw: alloc::vec::Vec<Option<i64>> = a.iter().map(|o| o.map(i64::from)).collect();
            cmp_array(&aw, b)
        }
        (Value::BigIntArray(a), Value::IntArray(b)) => {
            let bw: alloc::vec::Vec<Option<i64>> = b.iter().map(|o| o.map(i64::from)).collect();
            cmp_array(a, &bw)
        }
        // v7.37.16 — `jsonb = jsonb` / `jsonb <> jsonb` structural
        // equality (PG18-compatible: object keys compared order-free,
        // array elements order-sensitive, numbers by value). Equality
        // returns early because it is not expressible as an `Ordering`
        // (two unequal jsonb values can still compare `Equal` under no
        // total order at all).
        //
        // v7.39 (read01 round 76) — the ordering operators (< <= > >=)
        // now route to the same total order ORDER BY already sorts by
        // (`json::jsonb_compare`: the PG type-class ladder Object >
        // Array > Boolean > Number > String > Null).
        (Value::Json(a), Value::Json(b)) => {
            if matches!(op, BinOp::Eq | BinOp::NotEq) {
                let eq = crate::json::equals(l, r)?;
                return Ok(Value::Bool(if matches!(op, BinOp::Eq) { eq } else { !eq }));
            }
            let pa = crate::json::parse(a).map_err(|e| EvalError::TypeMismatch {
                detail: alloc::format!("invalid jsonb operand: {e:?}"),
            })?;
            let pb = crate::json::parse(b).map_err(|e| EvalError::TypeMismatch {
                detail: alloc::format!("invalid jsonb operand: {e:?}"),
            })?;
            crate::json::jsonb_compare(&pa, &pb)
        }
        // v7.37.17 — SMALLINT integer comparison. SmallInt↔Float goes
        // through the Float branch above; SmallInt↔Numeric through
        // `apply_binary_numeric`; only the pure-integer pairs land here.
        // Widen to the common width and compare by value.
        (Value::SmallInt(a), Value::SmallInt(b)) => i32::from(*a).cmp(&i32::from(*b)),
        (Value::SmallInt(a), Value::Int(b)) => i32::from(*a).cmp(b),
        (Value::Int(a), Value::SmallInt(b)) => a.cmp(&i32::from(*b)),
        (Value::SmallInt(a), Value::BigInt(b)) => i64::from(*a).cmp(b),
        (Value::BigInt(a), Value::SmallInt(b)) => a.cmp(&i64::from(*b)),
        // v7.37.17 — TIME / MONEY compare on their i64 storage repr
        // (microseconds since midnight / integer cents). PG defines a
        // full btree ordering for both.
        (Value::Time(a), Value::Time(b)) => a.cmp(b),
        (Value::Money(a), Value::Money(b)) => a.cmp(b),
        // v7.37.17 — BYTEA bytewise unsigned comparison (PG `byteacmp`).
        (Value::Bytes(a), Value::Bytes(b)) => a.as_ref().cmp(b.as_ref()),
        // v7.37.17 — MACADDR / MACADDR8 bytewise comparison
        // (PG `macaddr_cmp` / `macaddr8_cmp`).
        (Value::Macaddr(a), Value::Macaddr(b)) => a.cmp(b),
        (Value::Macaddr8(a), Value::Macaddr8(b)) => a.cmp(b),
        // v7.39 (read01 pg_lsn.c) — LSN ordering is plain u64.
        (Value::PgLsn(a), Value::PgLsn(b)) => a.cmp(b),
        // v7.39 (read01 char.c) — "char" compares by byte value.
        (Value::Char1(a), Value::Char1(b)) => a.cmp(b),
        // v7.39 (read01 ruleutils.c) — regclass compares by oid, including
        // against the synth catalogs' plain integer oid columns.
        (Value::Tid(b1, o1), Value::Tid(b2, o2)) => b1.cmp(b2).then(o1.cmp(o2)),
        (Value::Xid(x), Value::Xid(y)) => x.cmp(y),
        (Value::Cid(x), Value::Cid(y)) => x.cmp(y),
        // v7.39 (round 342, V65 / round 648) — the reg family compares on
        // its oid half: `pg_proc.oid = 'f'::regproc` is the point of
        // carrying it, and regclass and regtype join the same way.
        //
        // Written as ONE arm per shape rather than one per type. Round
        // 648 first added seven arms of its own for regtype and measured
        // `stddev(id)` and `count(DISTINCT g)` 17-30 % slower — nothing
        // in either goes near a reg value. Collapsing all three types
        // into each shape leaves FEWER arms here than before regtype
        // existed. Rounds 641/643/644/646 met the same wall; this is the
        // fifth face of it, and the first where the cost came from arm
        // COUNT rather than from a statement or a branch.
        (
            Value::RegClass(a, _) | Value::RegProc(a, _) | Value::RegType(a, _),
            Value::RegClass(b, _) | Value::RegProc(b, _) | Value::RegType(b, _),
        ) => a.cmp(b),
        (Value::RegClass(a, _) | Value::RegProc(a, _) | Value::RegType(a, _), Value::BigInt(b)) => {
            a.cmp(b)
        }
        (Value::BigInt(a), Value::RegClass(b, _) | Value::RegProc(b, _) | Value::RegType(b, _)) => {
            a.cmp(b)
        }
        (Value::RegClass(a, _) | Value::RegProc(a, _) | Value::RegType(a, _), Value::Int(b)) => {
            a.cmp(&i64::from(*b))
        }
        (Value::Int(a), Value::RegClass(b, _) | Value::RegProc(b, _) | Value::RegType(b, _)) => {
            i64::from(*a).cmp(b)
        }
        // Text form compares by name (the pre-dual-shape contract).
        (Value::RegClass(_, a) | Value::RegProc(_, a) | Value::RegType(_, a), Value::Text(b)) => {
            a.as_ref().cmp(b.as_ref())
        }
        (Value::Text(a), Value::RegClass(_, b) | Value::RegProc(_, b) | Value::RegType(_, b)) => {
            a.as_ref().cmp(b.as_ref())
        }
        // v7.37.17 — same-type array `=` / `<>` / `<` / `<=` / `>` /
        // `>=` for the remaining Ord-element array variants. PG's
        // `array_cmp` total order = element-wise (`cmp_array`); uuid is
        // bytewise, bytea bytewise, money by cents — all match PG's
        // per-element btree ops.
        (Value::UuidArray(a), Value::UuidArray(b)) => cmp_array(a, b),
        (Value::BytesArray(a), Value::BytesArray(b)) => cmp_array(a, b),
        (Value::MoneyArray(a), Value::MoneyArray(b)) => cmp_array(a, b),
        // v7.37.18 — NUMERIC[] / FLOAT8[] / INTERVAL[] `=` / `<>` via
        // *value-based* element equality (PG `array_eq`), because the
        // stored repr's derived `Ord` is wrong for these element types:
        //   * NUMERIC (scaled, scale): `1.10` (110,2) vs `1.1` (11,1)
        //     differ in repr but are equal in value — must rescale.
        //   * FLOAT8: `NaN = NaN` is `t` and `+0.0 = -0.0` is `t` in PG.
        //   * INTERVAL: `1 day` = `24:00:00`, `1 mon` = `30 days` — equal
        //     by canonical microsecond span, not by (months, days, micros).
        // These use the SAME element comparators as the scalar `numeric =
        // numeric` / `interval = interval` paths so array and scalar
        // agree. Ordering (`<` etc.) is DEFERRED (`eq_only_result`).
        (Value::NumericArray(a), Value::NumericArray(b)) => {
            return cmp_result(op, array_value_cmp(a, b, |x, y| numeric_pair_cmp(*x, *y)));
        }
        (Value::FloatArray(a), Value::FloatArray(b)) => {
            return cmp_result(op, array_value_cmp(a, b, |x, y| float_pg_cmp(*x, *y)));
        }
        (Value::IntervalArray(a), Value::IntervalArray(b)) => {
            return cmp_result(op, array_value_cmp(a, b, interval_span_cmp));
        }
        // v7.37.17 — INET / CIDR comparison (=/<> and ordering). PG
        // `network_cmp`: family first (IPv4 < IPv6), then the common
        // netmask prefix of the addresses, then the netmask length, then
        // the full address. Live PG18.4-verified: 10.0.0.0/8 < 192.168/16,
        // 10.0.0.0/8 < 10.0.0.0/16, 1.2.3.4 < 1.2.3.5, IPv4 < IPv6.
        (
            Value::Inet {
                family: af,
                bits: ab,
                addr: aa,
            },
            Value::Inet {
                family: bf,
                bits: bb,
                addr: ba,
            },
        )
        | (
            Value::Cidr {
                family: af,
                bits: ab,
                addr: aa,
            },
            Value::Cidr {
                family: bf,
                bits: bb,
                addr: ba,
            },
        ) => {
            let ord = network_cmp(*af, *ab, aa, *bf, *bb, ba);
            return match op {
                BinOp::Eq => Ok(Value::Bool(ord.is_eq())),
                BinOp::NotEq => Ok(Value::Bool(ord.is_ne())),
                BinOp::Lt => Ok(Value::Bool(ord.is_lt())),
                BinOp::LtEq => Ok(Value::Bool(ord.is_le())),
                BinOp::Gt => Ok(Value::Bool(ord.is_gt())),
                BinOp::GtEq => Ok(Value::Bool(ord.is_ge())),
                _ => Err(EvalError::TypeMismatch {
                    detail: "inet/cidr supports only comparison operators".into(),
                }),
            };
        }
        // v7.37.17 — BIT / BIT VARYING comparison (=/<> and ordering).
        // Bytes are MSB-packed with the trailing sub-byte zero-padded at
        // construction, so a bytewise compare over the common byte prefix
        // reproduces PG `varbit_cmp`'s bit-lexicographic order; when the
        // common bytes tie, the string with MORE bits is greater (a shorter
        // bit string is a strict prefix of the longer, hence less). Verified
        // vs PG18: B'10'<B'11', B'1'<B'10', B'10'<B'100', B'101'<B'1010'.
        (
            Value::BitString {
                nbits: an,
                bytes: ab,
            },
            Value::BitString {
                nbits: bn,
                bytes: bb,
            },
        ) => {
            let av = ab.as_ref();
            let bv = bb.as_ref();
            let n = av.len().min(bv.len());
            let ord = av[..n].cmp(&bv[..n]).then_with(|| an.cmp(bn));
            return match op {
                BinOp::Eq => Ok(Value::Bool(ord.is_eq())),
                BinOp::NotEq => Ok(Value::Bool(ord.is_ne())),
                BinOp::Lt => Ok(Value::Bool(ord.is_lt())),
                BinOp::LtEq => Ok(Value::Bool(ord.is_le())),
                BinOp::Gt => Ok(Value::Bool(ord.is_gt())),
                BinOp::GtEq => Ok(Value::Bool(ord.is_ge())),
                _ => Err(EvalError::TypeMismatch {
                    detail: "bit/varbit supports only comparison operators".into(),
                }),
            };
        }
        // v7.37 — RANGE comparison (=/<> and ordering), PG `range_cmp`.
        // Discrete kinds (int4/int8/date) canonicalise to `[)` first, so
        // `int4range(1,5)` equals `'[1,4]'`; continuous kinds compare bounds
        // verbatim. An empty range sorts first. Verified vs PG18.
        (
            Value::Range {
                kind: ak,
                lower: al,
                upper: au,
                lower_inc: ali,
                upper_inc: aui,
                empty: ae,
            },
            Value::Range {
                kind: bk,
                lower: bl,
                upper: bu,
                lower_inc: bli,
                upper_inc: bui,
                empty: be,
            },
        ) => {
            let ord = range_cmp(
                RangeParts::new(*ak, al, au, *ali, *aui, *ae),
                RangeParts::new(*bk, bl, bu, *bli, *bui, *be),
            );
            return cmp_result(op, ord);
        }
        // v7.37 — MULTIRANGE comparison (=/<> and ordering). Normalise both
        // to PG's canonical form (canonicalise each span, drop empties, sort,
        // merge overlapping/adjacent) and compare the span lists lexically.
        (
            Value::Multirange {
                kind: ak,
                ranges: ar,
            },
            Value::Multirange {
                kind: bk,
                ranges: br,
            },
        ) => {
            return cmp_result(op, multirange_cmp(*ak, ar, *bk, br));
        }
        // v7.37 — TSVECTOR equality (=/<>). SPG stores lexemes sorted by
        // word + deduped with their (ascending) positions and weight, so a
        // structural compare matches PG (`'bar foo' = 'foo bar'`, position-
        // sensitive, deduped). Ordering (< etc.) is deferred.
        // v7.38 (read01, T12.4) — TSVECTOR total order. Equality stays
        // structural (position/weight sensitive); ordering follows PG: lexeme
        // count first, then per-lexeme by word (length, then bytes), then
        // positions, then weight.
        (Value::TsVector(a), Value::TsVector(b)) => {
            return match op {
                BinOp::Eq => Ok(Value::Bool(a == b)),
                BinOp::NotEq => Ok(Value::Bool(a != b)),
                _ => cmp_result(op, tsvector_total_cmp(a, b)),
            };
        }
        // v7.38 (read01, T12.4/R2) — TSQUERY total order. Equality stays
        // structural (PG does not normalise operand order: `'a & b' <> 'b & a'`);
        // ordering follows PG's non-recursive silly_cmp_tsquery.
        (Value::TsQuery(a), Value::TsQuery(b)) => {
            return match op {
                BinOp::Eq => Ok(Value::Bool(a == b)),
                BinOp::NotEq => Ok(Value::Bool(a != b)),
                _ => cmp_result(op, tsquery_total_cmp(a, b)),
            };
        }
        // v7.38 (read01, T21) — geometric comparison is by AREA: PG's box and
        // circle operators (=, <>, <, <=, >, >=) all compare areas, so
        // `<(0,0),5> = <(100,100),5>` is true (same radius → same area) and
        // `(0,0),(1,1) = (5,5),(6,6)` is true (same area, different position).
        (Value::Circle { radius: r1, .. }, Value::Circle { radius: r2, .. }) => {
            let area = |r: f64| core::f64::consts::PI * r * r;
            return cmp_result(op, float_pg_cmp(area(*r1), area(*r2)));
        }
        (Value::PgBox(aur, all), Value::PgBox(bur, bll)) => {
            let area = |ur: &spg_storage::Point2D, ll: &spg_storage::Point2D| {
                (ur.x - ll.x).abs() * (ur.y - ll.y).abs()
            };
            return cmp_result(op, float_pg_cmp(area(aur, all), area(bur, bll)));
        }
        // v7.39 (read01 rowtypes.c) — runtime composite comparison
        // (PG record_cmp: pairwise field order). The ROW-literal syntax
        // desugars earlier; this arm serves cast-produced composites.
        (Value::Composite(x), Value::Composite(y)) => {
            if x.len() != y.len() {
                return Err(EvalError::TypeMismatch {
                    detail: format!(
                        "cannot compare record types with different numbers of columns: {} vs {}",
                        x.len(),
                        y.len()
                    ),
                });
            }
            let mut ord = core::cmp::Ordering::Equal;
            for ((_, a), (_, b)) in x.iter().zip(y.iter()) {
                let c = crate::orderby::value_cmp(a, b);
                if c != core::cmp::Ordering::Equal {
                    ord = c;
                    break;
                }
            }
            ord
        }
        (a, b) => {
            return Err(no_such_operator(op, a, b));
        }
    };
    let result = match op {
        BinOp::Eq => ord.is_eq(),
        BinOp::NotEq => !ord.is_eq(),
        BinOp::Lt => ord.is_lt(),
        BinOp::LtEq => ord.is_le(),
        BinOp::Gt => ord.is_gt(),
        BinOp::GtEq => ord.is_ge(),
        BinOp::IntDiv
        | BinOp::And
        | BinOp::Or
        | BinOp::LogicalXor
        | BinOp::BitOr
        | BinOp::BitAnd
        | BinOp::BitXor
        | BinOp::GeomParallel
        | BinOp::OverLeft
        | BinOp::OverRight
        | BinOp::GeomPerp
        | BinOp::GeomSameAs
        | BinOp::ClosestPoint
        | BinOp::GeomHoriz
        | BinOp::Intersects
        | BinOp::IsBelow
        | BinOp::IsAbove
        | BinOp::PatternLt
        | BinOp::PatternLtEq
        | BinOp::PatternGt
        | BinOp::PatternGtEq
        | BinOp::Add
        | BinOp::Sub
        | BinOp::Mul
        | BinOp::Div
        | BinOp::Mod
        | BinOp::L2Distance
        | BinOp::InnerProduct
        | BinOp::CosineDistance
        | BinOp::Concat
        | BinOp::JsonGet
        | BinOp::JsonGetText
        | BinOp::JsonGetPath
        | BinOp::JsonGetPathText
        | BinOp::JsonContains
        | BinOp::JsonPathExists
        | BinOp::JsonContainedBy
        | BinOp::JsonKeyExists
        | BinOp::JsonKeysAny
        | BinOp::JsonKeysAll
        | BinOp::JsonDeletePath
        | BinOp::TsMatch
        | BinOp::IsDistinctFrom
        | BinOp::IsNotDistinctFrom
        | BinOp::InetContainedBy
        | BinOp::InetContainedByEq
        | BinOp::InetContains
        | BinOp::InetContainsEq
        | BinOp::InetOverlap => {
            unreachable!("compare() only called with comparison ops")
        }
    };
    Ok(Value::Bool(result))
}

/// v7.39 (round 238) — both arguments of a boolean connective must be
/// boolean, checked BEFORE the short-circuit. PG type-checks its arguments
/// up front, so `1 OR true` and `false AND 1` are refused; SPG decided the
/// answer from the other side first and let a non-boolean operand through.
fn require_boolean_argument(kw: &str, l: &Value<'_>, r: &Value<'_>) -> Result<(), EvalError> {
    for v in [l, r] {
        if !matches!(v, Value::Bool(_) | Value::Null) {
            return Err(EvalError::TypeMismatch {
                detail: alloc::format!(
                    "argument of {kw} must be type boolean, not type {}",
                    super::strings::pg_typeof_name(v)
                ),
            });
        }
    }
    Ok(())
}

// SQL three-valued AND / OR.
pub(crate) fn and_3vl(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    require_boolean_argument("AND", &l, &r)?;
    match (l, r) {
        (Value::Bool(false), _) | (_, Value::Bool(false)) => Ok(Value::Bool(false)),
        (Value::Bool(true), Value::Bool(true)) => Ok(Value::Bool(true)),
        (Value::Null, _) | (_, Value::Null) => Ok(Value::Null),
        // v7.39 (round 238) — PG names the offending side's type:
        // "argument of AND must be type boolean, not type integer".
        (a, b) => Err(EvalError::TypeMismatch {
            detail: format!(
                "argument of AND must be type boolean, not type {}",
                super::strings::pg_typeof_name(if matches!(a, Value::Bool(_)) { &b } else { &a })
            ),
        }),
    }
}

fn or_3vl(l: Value<'static>, r: Value<'static>) -> Result<Value<'static>, EvalError> {
    require_boolean_argument("OR", &l, &r)?;
    match (l, r) {
        (Value::Bool(true), _) | (_, Value::Bool(true)) => Ok(Value::Bool(true)),
        (Value::Bool(false), Value::Bool(false)) => Ok(Value::Bool(false)),
        (Value::Null, _) | (_, Value::Null) => Ok(Value::Null),
        (a, b) => Err(EvalError::TypeMismatch {
            detail: format!(
                "argument of OR must be type boolean, not type {}",
                super::strings::pg_typeof_name(if matches!(a, Value::Bool(_)) { &b } else { &a })
            ),
        }),
    }
}

/// v7.39 (read01 round 70) — for the three ARRAY operators (`@>`, `<@`, `&&`),
/// read a bare TEXT literal on either side as an array of the OTHER side's
/// type. PG's unknown-literal resolution, in the one place SPG needed it.
///
/// A text that does not parse as an array literal is left alone — it may well be
/// the JSON or inet reading of the same operator, which the dispatch below still
/// has to reach.
fn coerce_array_literal_operands(
    op: BinOp,
    l: Value<'static>,
    r: Value<'static>,
) -> (Value<'static>, Value<'static>) {
    // v7.39 (read01 round 71) — a RANGE beside a bare literal takes the same
    // rule: `r && '[4,11)'` is a range, not an inet operand. Same family as the
    // array case, found by the same sweep.
    if matches!(op, BinOp::InetOverlap) {
        if let (Value::Range { kind, .. }, Value::Text(_)) = (&l, &r) {
            let k = *kind;
            if let Ok(coerced) = crate::eval::cast::cast_value(
                r.clone(),
                spg_sql::ast::CastTarget::Named(range_type_name(k).into()),
            ) {
                return (l, coerced);
            }
        }
        if let (Value::Text(_), Value::Range { kind, .. }) = (&l, &r) {
            let k = *kind;
            if let Ok(coerced) = crate::eval::cast::cast_value(
                l.clone(),
                spg_sql::ast::CastTarget::Named(range_type_name(k).into()),
            ) {
                return (coerced, r);
            }
        }
    }
    if !matches!(
        op,
        BinOp::JsonContains | BinOp::JsonContainedBy | BinOp::InetOverlap
    ) {
        return (l, r);
    }
    let as_array_like = |v: &Value<'_>| array_scalar_elems(v).is_some();
    let cast_like = |text: Value<'static>, model: &Value<'_>| -> Option<Value<'static>> {
        let target = match model {
            Value::TextArray(_) => spg_sql::ast::CastTarget::TextArray,
            Value::IntArray(_) => spg_sql::ast::CastTarget::IntArray,
            Value::BigIntArray(_) => spg_sql::ast::CastTarget::BigIntArray,
            _ => return None,
        };
        crate::eval::cast::cast_value(text, target).ok()
    };
    match (&l, &r) {
        (_, Value::Text(_)) if as_array_like(&l) => match cast_like(r.clone(), &l) {
            Some(coerced) => (l, coerced),
            None => (l, r),
        },
        (Value::Text(_), _) if as_array_like(&r) => match cast_like(l.clone(), &r) {
            Some(coerced) => (coerced, r),
            None => (l, r),
        },
        _ => (l, r),
    }
}

/// v7.39 (read01 round 71) — the SQL name of a range kind, for coercing a bare
/// literal beside a range operand.
fn range_type_name(kind: spg_storage::RangeKind) -> &'static str {
    use spg_storage::RangeKind as K;
    match kind {
        K::Int4 => "int4range",
        K::Int8 => "int8range",
        K::Num => "numrange",
        K::Ts => "tsrange",
        K::TsTz => "tstzrange",
        K::Date => "daterange",
    }
}

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

    #[test]
    fn interval_add_to_timestamp_micros_part() {
        // 2024-01-01 00:00:00 + INTERVAL '1 hour' = 2024-01-01 01:00:00
        let ts = i64::from(days_from_civil(2024, 1, 1)) * 86_400_000_000;
        let r = add_interval_to_micros(ts, 0, 0, 3_600_000_000).unwrap();
        let expected = ts + 3_600_000_000;
        assert_eq!(r, expected);
    }

    /// v7.37.5 β — days dimension threads cleanly through
    /// `add_interval_to_micros`. INTERVAL '1 day' adds exactly one
    /// 86_400_000_000-µs chunk, independent of the months path.
    #[test]
    fn interval_add_to_timestamp_days_part() {
        let ts = i64::from(days_from_civil(2024, 1, 1)) * 86_400_000_000;
        let r = add_interval_to_micros(ts, 0, 1, 0).unwrap();
        let expected = i64::from(days_from_civil(2024, 1, 2)) * 86_400_000_000;
        assert_eq!(r, expected);
    }

    #[test]
    fn interval_clamp_month_end() {
        // 2024-01-31 + 1 month = 2024-02-29 (leap year).
        let d = days_from_civil(2024, 1, 31);
        let shifted = shift_date_by_months(d, 1).unwrap();
        let (y, m, day) = civil_from_days(shifted);
        assert_eq!((y, m, day), (2024, 2, 29));
        // 2023-01-31 + 1 month = 2023-02-28 (non-leap).
        let d = days_from_civil(2023, 1, 31);
        let shifted = shift_date_by_months(d, 1).unwrap();
        let (y, m, day) = civil_from_days(shifted);
        assert_eq!((y, m, day), (2023, 2, 28));
        // 2024-03-31 - 1 month = 2024-02-29.
        let d = days_from_civil(2024, 3, 31);
        let shifted = shift_date_by_months(d, -1).unwrap();
        let (y, m, day) = civil_from_days(shifted);
        assert_eq!((y, m, day), (2024, 2, 29));
    }

    #[test]
    fn interval_date_plus_pure_days_lifts_to_timestamp() {
        // PG: DATE + INTERVAL '7 days' yields TIMESTAMP at midnight, not DATE.
        let d = days_from_civil(2024, 6, 1);
        let lhs = Value::Date(d);
        let rhs = Value::Interval {
            months: 0,
            days: 7,
            micros: 0,
        };
        let v = apply_binary_interval(BinOp::Add, &lhs, &rhs)
            .unwrap()
            .unwrap();
        let expected = i64::from(days_from_civil(2024, 6, 8)) * 86_400_000_000;
        assert_eq!(v, Value::Timestamp(expected));
    }

    #[test]
    fn interval_date_plus_sub_day_lifts_to_timestamp() {
        // DATE + INTERVAL '1 hour' must lift to TIMESTAMP.
        let d = days_from_civil(2024, 6, 1);
        let lhs = Value::Date(d);
        let rhs = Value::Interval {
            months: 0,
            days: 0,
            micros: 3_600_000_000,
        };
        let v = apply_binary_interval(BinOp::Add, &lhs, &rhs)
            .unwrap()
            .unwrap();
        let expected = i64::from(d) * 86_400_000_000 + 3_600_000_000;
        assert_eq!(v, Value::Timestamp(expected));
    }
}