File: join_optimizer.cc

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

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "sql/join_optimizer/join_optimizer.h"

#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include <algorithm>
#include <bitset>
#include <initializer_list>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

#include "ft_global.h"
#include "map_helpers.h"
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/udf_registration_types.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "scope_guard.h"
#include "sql/field.h"
#include "sql/filesort.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_sum.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/build_interesting_orders.h"
#include "sql/join_optimizer/compare_access_paths.h"
#include "sql/join_optimizer/cost_model.h"
#include "sql/join_optimizer/estimate_selectivity.h"
#include "sql/join_optimizer/explain_access_path.h"
#include "sql/join_optimizer/find_contained_subqueries.h"
#include "sql/join_optimizer/graph_simplification.h"
#include "sql/join_optimizer/hypergraph.h"
#include "sql/join_optimizer/interesting_orders.h"
#include "sql/join_optimizer/interesting_orders_defs.h"
#include "sql/join_optimizer/make_join_hypergraph.h"
#include "sql/join_optimizer/node_map.h"
#include "sql/join_optimizer/overflow_bitset.h"
#include "sql/join_optimizer/print_utils.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/join_optimizer/secondary_engine_costing_flags.h"
#include "sql/join_optimizer/subgraph_enumeration.h"
#include "sql/join_optimizer/walk_access_paths.h"
#include "sql/join_type.h"
#include "sql/key.h"
#include "sql/key_spec.h"
#include "sql/mem_root_array.h"
#include "sql/opt_costmodel.h"
#include "sql/parse_tree_node_base.h"
#include "sql/partition_info.h"
#include "sql/query_options.h"
#include "sql/range_optimizer/index_range_scan_plan.h"
#include "sql/range_optimizer/internal.h"
#include "sql/range_optimizer/path_helpers.h"
#include "sql/range_optimizer/range_analysis.h"
#include "sql/range_optimizer/range_opt_param.h"
#include "sql/range_optimizer/range_optimizer.h"
#include "sql/range_optimizer/tree.h"
#include "sql/sql_array.h"
#include "sql/sql_base.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_const.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_opt_exec_shared.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_partition.h"
#include "sql/sql_select.h"
#include "sql/sql_tmp_table.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/table_function.h"
#include "sql/temp_table_param.h"
#include "sql/uniques.h"
#include "sql/window.h"
#include "template_utils.h"

using hypergraph::Hyperedge;
using hypergraph::Node;
using hypergraph::NodeMap;
using std::find_if;
using std::min;
using std::pair;
using std::string;
using std::swap;
using std::vector;

namespace {

string PrintAccessPath(const AccessPath &path, const JoinHypergraph &graph,
                       const char *description_for_trace);
void PrintJoinOrder(const AccessPath *path, string *join_order);

AccessPath *CreateMaterializationPath(THD *thd, JOIN *join, AccessPath *path,
                                      TABLE *temp_table,
                                      Temp_table_param *temp_table_param,
                                      bool copy_items);

AccessPath *GetSafePathToSort(THD *thd, JOIN *join, AccessPath *path,
                              bool need_rowid);

/**
  CostingReceiver contains the main join planning logic, selecting access paths
  based on cost. It receives subplans from DPhyp (see enumerate_subgraph.h),
  assigns them costs based on a cost model, and keeps the ones that are
  cheapest. In the end, this means it will be left with a root access path that
  gives the lowest total cost for joining the tables in the query block, ie.,
  without ORDER BY etc.

  Currently, besides the expected number of produced rows (which is the same no
  matter how we access the table) we keep only a single value per subplan
  (total cost), and thus also only a single best access path. In the future,
  we will have more dimensions to worry about, such as initial cost versus total
  cost (relevant for LIMIT), ordering properties, and so on. At that point,
  there is not necessarily a single “best” access path anymore, and we will need
  to keep multiple ones around, and test all of them as candidates when building
  larger subplans.
 */
class CostingReceiver {
 public:
  CostingReceiver(
      THD *thd, Query_block *query_block, JoinHypergraph &graph,
      const LogicalOrderings *orderings,
      const Mem_root_array<SortAheadOrdering> *sort_ahead_orderings,
      const Mem_root_array<ActiveIndexInfo> *active_indexes,
      const Mem_root_array<FullTextIndexInfo> *fulltext_searches,
      NodeMap fulltext_tables, uint64_t sargable_fulltext_predicates,
      table_map update_delete_target_tables,
      table_map immediate_update_delete_candidates, bool need_rowid,
      SecondaryEngineFlags engine_flags, int subgraph_pair_limit,
      secondary_engine_modify_access_path_cost_t secondary_engine_cost_hook,
      string *trace)
      : m_thd(thd),
        m_query_block(query_block),
        m_access_paths(thd->mem_root),
        m_graph(&graph),
        m_orderings(orderings),
        m_sort_ahead_orderings(sort_ahead_orderings),
        m_active_indexes(active_indexes),
        m_fulltext_searches(fulltext_searches),
        m_fulltext_tables(fulltext_tables),
        m_sargable_fulltext_predicates(sargable_fulltext_predicates),
        m_update_delete_target_nodes(GetNodeMapFromTableMap(
            update_delete_target_tables, graph.table_num_to_node_num)),
        m_immediate_update_delete_candidates(GetNodeMapFromTableMap(
            immediate_update_delete_candidates, graph.table_num_to_node_num)),
        m_need_rowid(need_rowid),
        m_engine_flags(engine_flags),
        m_subgraph_pair_limit(subgraph_pair_limit),
        m_secondary_engine_cost_hook(secondary_engine_cost_hook),
        m_trace(trace) {
    // At least one join type must be supported.
    assert(Overlaps(engine_flags,
                    MakeSecondaryEngineFlags(
                        SecondaryEngineFlag::SUPPORTS_HASH_JOIN,
                        SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN)));
  }

  // Not copyable, but movable so that we can reset it after graph
  // simplification if needed.
  CostingReceiver &operator=(const CostingReceiver &) = delete;
  CostingReceiver &operator=(CostingReceiver &&) = default;
  CostingReceiver(const CostingReceiver &) = delete;
  CostingReceiver(CostingReceiver &&) = default;

  bool HasSeen(NodeMap subgraph) const {
    return m_access_paths.count(subgraph) != 0;
  }

  bool FoundSingleNode(int node_idx);

  // Called EmitCsgCmp() in the DPhyp paper.
  bool FoundSubgraphPair(NodeMap left, NodeMap right, int edge_idx);

  const Prealloced_array<AccessPath *, 4> &root_candidates() {
    const auto it =
        m_access_paths.find(TablesBetween(0, m_graph->nodes.size()));
    assert(it != m_access_paths.end());
    return it->second.paths;
  }

  FunctionalDependencySet active_fds_at_root() const {
    const auto it =
        m_access_paths.find(TablesBetween(0, m_graph->nodes.size()));
    assert(it != m_access_paths.end());
    return it->second.active_functional_dependencies;
  }

  size_t num_subplans() const { return m_access_paths.size(); }

  size_t num_access_paths() const {
    size_t access_paths = 0;
    for (const auto &[nodes, pathset] : m_access_paths) {
      access_paths += pathset.paths.size();
    }
    return access_paths;
  }

  /// True if the result of the join is found to be always empty, typically
  /// because of an impossible WHERE clause.
  bool always_empty() const {
    const auto it =
        m_access_paths.find(TablesBetween(0, m_graph->nodes.size()));
    return it != m_access_paths.end() && it->second.always_empty;
  }

  AccessPath *ProposeAccessPath(
      AccessPath *path, Prealloced_array<AccessPath *, 4> *existing_paths,
      OrderingSet obsolete_orderings, const char *description_for_trace) const;

  bool HasSecondaryEngineCostHook() const {
    return m_secondary_engine_cost_hook != nullptr;
  }

 private:
  THD *m_thd;

  /// The query block we are planning.
  Query_block *m_query_block;

  /**
    Besides the access paths for a set of nodes (see m_access_paths),
    AccessPathSet contains information that is common between all access
    paths for that set. One would believe num_output_rows would be such
    a member (a set of tables should produce the same number of output
    rows no matter the join order), but due to parameterized paths,
    different access paths could have different outputs. delayed_predicates
    is another, but currently, it's already efficiently hidden space-wise
    due to the use of a union.
   */
  struct AccessPathSet {
    Prealloced_array<AccessPath *, 4> paths;
    FunctionalDependencySet active_functional_dependencies{0};

    // Once-interesting orderings that we don't care about anymore,
    // e.g. because they were interesting for a semijoin but that semijoin
    // is now done (with or without using the ordering). This reduces
    // the number of access paths we have to keep in play, since they are
    // de-facto equivalent.
    //
    // Note that if orderings were merged, this could falsely prune out
    // orderings that we would actually need, but as long as all of the
    // relevant ones are semijoin orderings (which are never identical,
    // and never merged with the relevant-at-end orderings), this
    // should not happen.
    OrderingSet obsolete_orderings{0};

    // True if the join of the tables in this set has been found to be always
    // empty (typically because of an impossible WHERE clause).
    bool always_empty{false};
  };

  /**
    For each subset of tables that are connected in the join hypergraph,
    keeps the current best access paths for producing said subset.
    There can be several that are best in different ways; see comments
    on ProposeAccessPath().

    Also used for communicating connectivity information back to DPhyp
    (in HasSeen()); if there's an entry here, that subset will induce
    a connected subgraph of the join hypergraph.
   */
  mem_root_unordered_map<NodeMap, AccessPathSet> m_access_paths;

  /**
    How many subgraph pairs we've seen so far. Used to give up
    if we end up allocating too many resources (prompting us to
    create a simpler join graph and try again).
   */
  int m_num_seen_subgraph_pairs = 0;

  /// The graph we are running over.
  JoinHypergraph *m_graph;

  /// Whether we have applied clamping due to a multi-column EQ_REF at any
  /// point. There is a known issue (see bug #33550360) where this can cause
  /// row count estimates to be inconsistent between different access paths.
  /// Obviously, we should fix this bug by adjusting the selectivities
  /// (and we do for single-column indexes), but for multipart indexes,
  /// this is nontrivial. See the bug for details on some ideas, but the
  /// gist of it is that we probably will want a linear program to adjust
  /// multi-selectivities so that they are consistent, and not above 1/N
  /// (for N-row tables) if there are unique indexes on them.
  ///
  /// The only reason why we collect this information, like
  /// JoinHypergraph::has_reordered_left_joins, is to be able to assert
  /// on inconsistent row counts between APs, excluding this (known) issue.
  bool has_clamped_multipart_eq_ref = false;

  /// Whether we have a semijoin where the inner child is parameterized on the
  /// outer child, and the row estimate of the inner child is possibly clamped,
  /// for example because of some other semijoin. In this case, we may see
  /// inconsistent row count estimates between the ordinary semijoin plan and
  /// the rewrite_semi_to_inner plan, because it's hard to tell how much the
  /// already-applied-as-sargable selectivity affected the row count estimate of
  /// the child.
  ///
  /// The only reason why we collect this information, is to be able to assert
  /// on inconsistent row counts between access paths, excluding this known
  /// issue.
  bool has_semijoin_with_possibly_clamped_child = false;

  /// Keeps track of interesting orderings in this query block.
  /// See LogicalOrderings for more information.
  const LogicalOrderings *m_orderings;

  /// List of all orderings that are candidates for sort-ahead
  /// (because they are, or may eventually become, an interesting ordering).
  const Mem_root_array<SortAheadOrdering> *m_sort_ahead_orderings;

  /// List of all indexes that are active and that we can apply in this query.
  /// Indexes can be useful in several ways: We can use them for ref access,
  /// for index-only scans, or to get interesting orderings.
  const Mem_root_array<ActiveIndexInfo> *m_active_indexes;

  /// List of all active full-text indexes that we can apply in this query.
  const Mem_root_array<FullTextIndexInfo> *m_fulltext_searches;

  /// A map of tables that are referenced by a MATCH function (those tables that
  /// have Table_ref::is_fulltext_searched() == true). It is used for
  /// preventing hash joins involving tables that are full-text searched.
  NodeMap m_fulltext_tables = 0;

  /// The set of WHERE predicates which are on a form that can be satisfied by a
  /// full-text index scan. This includes calls to MATCH with no comparison
  /// operator, and predicates on the form MATCH > const or MATCH >= const
  /// (where const must be high enough to make the comparison return false for
  /// documents with zero score).
  uint64_t m_sargable_fulltext_predicates = 0;

  /// The target tables of an UPDATE or DELETE statement.
  NodeMap m_update_delete_target_nodes = 0;

  /// The set of tables that are candidates for immediate update or delete.
  /// Immediate update/delete means that the rows from the table are deleted
  /// while reading the rows from the topmost iterator. (As opposed to buffered
  /// update/delete, where the row IDs are stored in temporary tables, and only
  /// updated or deleted after the topmost iterator has been read to the end.)
  /// The candidates are those target tables that are only referenced once in
  /// the query. The requirement for immediate deletion is that the deleted row
  /// will not have to be read again later. Currently, at most one of the
  /// candidate tables is chosen, and it is always the outermost table in the
  /// join tree.
  NodeMap m_immediate_update_delete_candidates = 0;

  /// Whether we will be needing row IDs from our tables, typically for
  /// a later sort. If this happens, derived tables cannot use streaming,
  /// but need an actual materialization, since filesort expects to be
  /// able to go back and ask for a given row. (This is different from
  /// when we need row IDs for weedout, which doesn't preclude streaming.
  /// The hypergraph optimizer does not use weedout.)
  bool m_need_rowid;

  /// The flags declared by the secondary engine. In particular, it describes
  /// what kind of access path types should not be created.
  SecondaryEngineFlags m_engine_flags;

  /// The maximum number of pairs of subgraphs we are willing to accept,
  /// or -1 if no limit. If this limit gets hit, we stop traversing the graph
  /// and return an error; the caller will then have to modify the hypergraph
  /// (see GraphSimplifier) to make for a graph with fewer options, so that
  /// planning time will come under an acceptable limit.
  int m_subgraph_pair_limit;

  /// Pointer to a function that modifies the cost estimates of an access path
  /// for execution in a secondary storage engine, or nullptr otherwise.
  secondary_engine_modify_access_path_cost_t m_secondary_engine_cost_hook;

  /// If not nullptr, we store human-readable optimizer trace information here.
  string *m_trace;

  /// A map of tables that can never be on the right side of any join,
  /// ie., they have to be leftmost in the tree. This only affects recursive
  /// table references (ie., when WITH RECURSIVE is in use); they work by
  /// continuously tailing new records, which wouldn't work if we were to
  /// scan them multiple times or put them in a hash table. Naturally,
  /// there must be zero or one bit here; the common case is zero.
  NodeMap forced_leftmost_table = 0;

  /// A special MEM_ROOT for allocating OverflowBitsets that we might end up
  /// discarding, ie. for AccessPaths that do not yet live in m_access_paths.
  /// For any AccessPath that is to have a permanent life (ie., not be
  /// immediately discarded as inferior), the OverflowBitset _must_ be taken
  /// out of this MEM_ROOT and onto the regular one, as it is cleared often.
  /// (This significantly reduces the amount of memory used in situations
  /// where lots of AccessPaths are produced and discarded. Of course,
  /// it only matters for queries with >= 64 predicates.)
  ///
  /// The copying is using CommitBitsetsToHeap(). ProposeAccessPath() will
  /// automatically call CommitBitsetsToHeap() for accepted access paths,
  /// but it will not call it on any of their children. Thus, if you've
  /// added more than one AccessPath in the chain (e.g. if you add a join,
  /// then a sort of that join, and then propose the sort), you will need
  /// to make sure there are no stray bitsets left on this MEM_ROOT.
  ///
  /// Because this can be a source of subtle bugs, you should be conservative
  /// about what bitsets you put here; really, only the ones you could be
  /// allocating many of (like joins) should be candidates.
  MEM_ROOT m_overflow_bitset_mem_root;

  /// A special MEM_ROOT for temporary data for the range optimizer.
  /// It can be discarded immediately after we've decided on the range scans
  /// for a given table (but we reuse its memory as long as there are more
  /// tables left to scan).
  MEM_ROOT m_range_optimizer_mem_root;

  /// For trace use only.
  string PrintSet(NodeMap x) const {
    std::string ret = "{";
    bool first = true;
    for (size_t node_idx : BitsSetIn(x)) {
      if (!first) {
        ret += ",";
      }
      first = false;
      ret += m_graph->nodes[node_idx].table->alias;
    }
    return ret + "}";
  }

  /// For trace use only.
  string PrintSubgraphHeader(const JoinPredicate *edge,
                             const AccessPath &join_path, NodeMap left,
                             NodeMap right) const;

  /// Checks whether the given engine flag is active or not.
  bool SupportedEngineFlag(SecondaryEngineFlag flag) const {
    return Overlaps(m_engine_flags, MakeSecondaryEngineFlags(flag));
  }

  bool FindIndexRangeScans(int node_idx, bool *impossible,
                           double *num_output_rows_after_filter);
  void ProposeIndexMerge(TABLE *table, int node_idx, const SEL_IMERGE &imerge,
                         int pred_idx, bool inexact,
                         bool allow_clustered_primary_key_scan,
                         int num_where_predicates,
                         double num_output_rows_after_filter,
                         RANGE_OPT_PARAM *param,
                         bool *has_clustered_primary_key_scan);

  void TraceAccessPaths(NodeMap nodes);
  void ProposeAccessPathForBaseTable(int node_idx,
                                     double force_num_output_rows_after_filter,
                                     const char *description_for_trace,
                                     AccessPath *path);
  void ProposeAccessPathForIndex(int node_idx,
                                 OverflowBitset applied_predicates,
                                 OverflowBitset subsumed_predicates,
                                 double force_num_output_rows_after_filter,
                                 const char *description_for_trace,
                                 AccessPath *path);
  void ProposeAccessPathWithOrderings(NodeMap nodes,
                                      FunctionalDependencySet fd_set,
                                      OrderingSet obsolete_orderings,
                                      AccessPath *path,
                                      const char *description_for_trace);
  bool ProposeTableScan(TABLE *table, int node_idx,
                        double force_num_output_rows_after_filter);
  bool ProposeIndexScan(TABLE *table, int node_idx,
                        double force_num_output_rows_after_filter,
                        unsigned key_idx, bool reverse, int ordering_idx);
  bool ProposeRefAccess(TABLE *table, int node_idx, unsigned key_idx,
                        double force_num_output_rows_after_filter, bool reverse,
                        table_map allowed_parameter_tables, int ordering_idx);
  bool ProposeAllUniqueIndexLookupsWithConstantKey(int node_idx, bool *found);
  bool RedundantThroughSargable(
      OverflowBitset redundant_against_sargable_predicates,
      const AccessPath *left_path, const AccessPath *right_path, NodeMap left,
      NodeMap right);
  inline pair<bool, bool> AlreadyAppliedAsSargable(
      Item *condition, const AccessPath *left_path,
      const AccessPath *right_path);
  bool ProposeAllFullTextIndexScans(TABLE *table, int node_idx,
                                    double force_num_output_rows_after_filter);
  bool ProposeFullTextIndexScan(TABLE *table, int node_idx,
                                Item_func_match *match, int predicate_idx,
                                int ordering_idx,
                                double force_num_output_rows_after_filter);
  void ProposeNestedLoopJoin(NodeMap left, NodeMap right, AccessPath *left_path,
                             AccessPath *right_path, const JoinPredicate *edge,
                             bool rewrite_semi_to_inner,
                             FunctionalDependencySet new_fd_set,
                             OrderingSet new_obsolete_orderings,
                             bool *wrote_trace);
  void ProposeHashJoin(NodeMap left, NodeMap right, AccessPath *left_path,
                       AccessPath *right_path, const JoinPredicate *edge,
                       FunctionalDependencySet new_fd_set,
                       OrderingSet new_obsolete_orderings,
                       bool rewrite_semi_to_inner, bool *wrote_trace);
  void ApplyPredicatesForBaseTable(int node_idx,
                                   OverflowBitset applied_predicates,
                                   OverflowBitset subsumed_predicates,
                                   bool materialize_subqueries,
                                   AccessPath *path,
                                   FunctionalDependencySet *new_fd_set);
  void ApplyDelayedPredicatesAfterJoin(
      NodeMap left, NodeMap right, const AccessPath *left_path,
      const AccessPath *right_path, int join_predicate_first,
      int join_predicate_last, bool materialize_subqueries,
      AccessPath *join_path, FunctionalDependencySet *new_fd_set);
  double FindAlreadyAppliedSelectivity(const JoinPredicate *edge,
                                       const AccessPath *left_path,
                                       const AccessPath *right_path,
                                       NodeMap left, NodeMap right);

  void CommitBitsetsToHeap(AccessPath *path) const;
  bool BitsetsAreCommitted(AccessPath *path) const;
};

/// Lists the current secondary engine flags in use. If there is no secondary
/// engine, will use a default set of permissive flags suitable for
/// non-secondary engine use.
SecondaryEngineFlags EngineFlags(const THD *thd) {
  if (const handlerton *secondary_engine = SecondaryEngineHandlerton(thd);
      secondary_engine != nullptr) {
    return secondary_engine->secondary_engine_flags;
  }

  return MakeSecondaryEngineFlags(
      SecondaryEngineFlag::SUPPORTS_HASH_JOIN,
      SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN);
}

/// Gets the secondary storage engine cost modification function, if any.
secondary_engine_modify_access_path_cost_t SecondaryEngineCostHook(
    const THD *thd) {
  const handlerton *secondary_engine = SecondaryEngineHandlerton(thd);
  if (secondary_engine == nullptr) {
    return nullptr;
  } else {
    return secondary_engine->secondary_engine_modify_access_path_cost;
  }
}

/// Returns the MATCH function of a predicate that can be pushed down to a
/// full-text index. This can be done if the predicate is a MATCH function,
/// or in some cases (see IsSargableFullTextIndexPredicate() for details)
/// where the predicate is a comparison function which compares the result
/// of MATCH with a constant. For example, predicates on this form could be
/// pushed down to a full-text index:
///
///   WHERE MATCH (x) AGAINST ('search string') AND @<more predicates@>
///
///   WHERE MATCH (x) AGAINST ('search string') > 0.5 AND @<more predicates@>
///
/// Since full-text index scans return documents with positive scores only, an
/// index scan can only be used if the predicate excludes negative or zero
/// scores.
Item_func_match *GetSargableFullTextPredicate(const Predicate &predicate) {
  Item_func *func = down_cast<Item_func *>(predicate.condition);
  switch (func->functype()) {
    case Item_func::MATCH_FUNC:
      // The predicate is MATCH (x) AGAINST ('search string'), which can be
      // pushed to the index.
      return down_cast<Item_func_match *>(func->get_arg(0))->get_master();
    case Item_func::LT_FUNC:
    case Item_func::LE_FUNC:
      // The predicate is const < MATCH or const <= MATCH, with a constant value
      // which makes it pushable.
      assert(func->get_arg(0)->const_item());
      return down_cast<Item_func_match *>(func->get_arg(1))->get_master();
    case Item_func::GT_FUNC:
    case Item_func::GE_FUNC:
      // The predicate is MATCH > const or MATCH >= const, with a constant value
      // which makes it pushable.
      assert(func->get_arg(1)->const_item());
      return down_cast<Item_func_match *>(func->get_arg(0))->get_master();
    default:
      // The predicate is not on a form that can be pushed to a full-text index
      // scan. We should not get here.
      assert(false);
      return nullptr;
  }
}

/// Is the current statement a DELETE statement?
bool IsDeleteStatement(const THD *thd) {
  return thd->lex->sql_command == SQLCOM_DELETE ||
         thd->lex->sql_command == SQLCOM_DELETE_MULTI;
}

/// Is the current statement a DELETE statement?
bool IsUpdateStatement(const THD *thd) {
  return thd->lex->sql_command == SQLCOM_UPDATE ||
         thd->lex->sql_command == SQLCOM_UPDATE_MULTI;
}

void CostingReceiver::TraceAccessPaths(NodeMap nodes) {
  auto it = m_access_paths.find(nodes);
  if (it == m_access_paths.end()) {
    *m_trace += " - ";
    *m_trace += PrintSet(nodes);
    *m_trace += " has no access paths (this should not normally happen)\n";
    return;
  }

  *m_trace += " - current access paths for ";
  *m_trace += PrintSet(nodes);
  *m_trace += ": ";

  bool first = true;
  for (const AccessPath *path : it->second.paths) {
    if (!first) {
      *m_trace += ", ";
    }
    *m_trace += PrintAccessPath(*path, *m_graph, "");
    first = false;
  }
  *m_trace += ")\n";
}

/**
  Called for each table in the query block, at some arbitrary point before we
  start seeing subsets where it's joined to other tables.

  We support table scans and ref access, so we create access paths for both
  (where possible) and cost them. In this context, “tables” in a query block
  also includes virtual tables such as derived tables, so we need to figure out
  if there is a cost for materializing them.
 */
bool CostingReceiver::FoundSingleNode(int node_idx) {
  if (m_thd->is_error()) return true;

  m_graph->secondary_engine_costing_flags &=
      ~SecondaryEngineCostingFlag::HAS_MULTIPLE_BASE_TABLES;

  TABLE *table = m_graph->nodes[node_idx].table;
  Table_ref *tl = table->pos_in_table_list;

  if (m_trace != nullptr) {
    *m_trace += StringPrintf("\nFound node %s [rows=%llu]\n",
                             m_graph->nodes[node_idx].table->alias,
                             table->file->stats.records);
  }

  // First look for unique index lookups that use only constants.
  {
    bool found_eq_ref = false;
    if (ProposeAllUniqueIndexLookupsWithConstantKey(node_idx, &found_eq_ref)) {
      return true;
    }

    // If we found an unparameterized EQ_REF path, we can skip looking for
    // alternative access methods, like parameterized or non-unique index
    // lookups, index range scans or table scans, as they are unlikely to be any
    // better. Returning early to reduce time spent planning the query, which is
    // especially beneficial for point selects.
    if (found_eq_ref) {
      if (m_trace != nullptr) {
        TraceAccessPaths(TableBitmap(node_idx));
      }
      return false;
    }
  }

  // We run the range optimizer before anything else, because we can use
  // its estimates to adjust predicate selectivity, giving us consistent
  // row count estimation between the access paths. (It is also usually
  // more precise for complex range conditions than our default estimates.
  // This is also the reason why we run it even if HA_NO_INDEX_ACCESS is set.)
  double range_optimizer_row_estimate = -1.0;
  {
    auto cleanup_mem_root = create_scope_guard([this, node_idx] {
      if (node_idx == 0) {
        // We won't be calling the range optimizer anymore, so we don't need
        // to keep its temporary allocations around. Note that FoundSingleNode()
        // counts down from N-1 to 0, not up.
        m_range_optimizer_mem_root.Clear();
      } else {
        m_range_optimizer_mem_root.ClearForReuse();
      }
    });
    if (!tl->is_recursive_reference() && m_graph->num_where_predicates > 0) {
      // Note that true error returns in itself is not enough to fail the query;
      // the range optimizer could be out of RAM easily enough, which is
      // nonfatal. That just means we won't be using it for this table.
      bool impossible = false;
      if (FindIndexRangeScans(node_idx, &impossible,
                              &range_optimizer_row_estimate) &&
          m_thd->is_error()) {
        return true;
      }
      if (impossible) {
        const char *const cause = "WHERE condition is always false";
        if (!IsBitSet(tl->tableno(), m_graph->tables_inner_to_outer_or_anti)) {
          // The entire top-level join is going to be empty, so we can abort the
          // planning and return a zero rows plan.
          m_query_block->join->zero_result_cause = cause;
          return true;
        }
        AccessPath *table_path =
            NewTableScanAccessPath(m_thd, table, /*count_examined_rows=*/false);
        AccessPath *zero_path = NewZeroRowsAccessPath(m_thd, table_path, cause);

        // We need to get the set of functional dependencies right,
        // even though we don't need to actually apply any filters.
        FunctionalDependencySet new_fd_set;
        ApplyPredicatesForBaseTable(
            node_idx,
            /*applied_predicates=*/
            MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()},
            /*subsumed_predicates=*/
            MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()},
            /*materialize_subqueries=*/false, zero_path, &new_fd_set);
        zero_path->filter_predicates =
            MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()};
        zero_path->ordering_state =
            m_orderings->ApplyFDs(zero_path->ordering_state, new_fd_set);
        ProposeAccessPathWithOrderings(TableBitmap(node_idx), new_fd_set,
                                       /*obsolete_orderings=*/0, zero_path, "");
        if (m_trace != nullptr) {
          TraceAccessPaths(TableBitmap(node_idx));
        }
        return false;
      }
    }
  }

  if (ProposeTableScan(table, node_idx, range_optimizer_row_estimate)) {
    return true;
  }

  if (Overlaps(table->file->ha_table_flags(), HA_NO_INDEX_ACCESS) ||
      tl->is_recursive_reference()) {
    // We can't use any indexes, so end here.
    if (m_trace != nullptr) {
      TraceAccessPaths(TableBitmap(node_idx));
    }
    return false;
  }

  // Propose index scan (for getting interesting orderings).
  // We only consider those that are more interesting than a table scan;
  // for the others, we don't even need to create the access path and go
  // through the tournament.
  for (const ActiveIndexInfo &order_info : *m_active_indexes) {
    if (order_info.table != table) {
      continue;
    }

    const int forward_order =
        m_orderings->RemapOrderingIndex(order_info.forward_order);
    const int reverse_order =
        m_orderings->RemapOrderingIndex(order_info.reverse_order);
    for (bool reverse : {false, true}) {
      if (reverse && reverse_order == 0) {
        continue;
      }
      const int order = reverse ? reverse_order : forward_order;
      if (order != 0) {
        if (ProposeIndexScan(table, node_idx, range_optimizer_row_estimate,
                             order_info.key_idx, reverse, order)) {
          return true;
        }
      }

      // Propose ref access using only sargable predicates that reference no
      // other table.
      if (ProposeRefAccess(table, node_idx, order_info.key_idx,
                           range_optimizer_row_estimate, reverse,
                           /*allowed_parameter_tables=*/0, order)) {
        return true;
      }

      // Propose ref access using all sargable predicates that also refer to
      // other tables (e.g. t1.x = t2.x). Such access paths can only be used
      // on the inner side of a nested loop join, where all the other
      // referenced tables are among the outer tables of the join. Such path
      // is called a parameterized path.
      //
      // Since indexes can have multiple parts, the access path can also end
      // up being parameterized on multiple outer tables. However, since
      // parameterized paths are less flexible in joining than
      // non-parameterized ones, it can be advantageous to not use all parts
      // of the index; it's impossible to say locally. Thus, we enumerate all
      // possible subsets of table parameters that may be useful, to make sure
      // we don't miss any such paths.
      table_map want_parameter_tables = 0;
      for (const SargablePredicate &sp :
           m_graph->nodes[node_idx].sargable_predicates) {
        if (sp.field->table == table &&
            sp.field->part_of_key.is_set(order_info.key_idx) &&
            !Overlaps(sp.other_side->used_tables(),
                      PSEUDO_TABLE_BITS | table->pos_in_table_list->map())) {
          want_parameter_tables |= sp.other_side->used_tables();
        }
      }
      for (table_map allowed_parameter_tables :
           NonzeroSubsetsOf(want_parameter_tables)) {
        if (ProposeRefAccess(table, node_idx, order_info.key_idx,
                             range_optimizer_row_estimate, reverse,
                             allowed_parameter_tables, order)) {
          return true;
        }
      }
    }
  }

  if (tl->is_fulltext_searched()) {
    if (ProposeAllFullTextIndexScans(table, node_idx,
                                     range_optimizer_row_estimate)) {
      return true;
    }
  }

  if (m_trace != nullptr) {
    TraceAccessPaths(TableBitmap(node_idx));
  }
  return false;
}

// Figure out which predicates we have that are not applied/subsumed
// by scanning this specific index; we already did a check earlier,
// but that was on predicates applied by scanning _any_ index.
// The difference is those that
//
//   a) Use a field that's not part of this index.
//   b) Use a field that our index is only partial for; these are still
//      counted as applied for selectivity purposes (which is possibly
//      overcounting), but need to be rechecked (ie., not subsumed).
//   c) Use a field where we've been told by get_ranges_from_tree()
//      that it had to set up a range that was nonexact (because it was
//      part of a predicate that had a non-equality condition on an
//      earlier keypart). These are handled as for b).
//
// We use this information to build up sets of which fields an
// applied or subsumed predicate is allowed to reference,
// then check each predicate against those lists.
void FindAppliedAndSubsumedPredicatesForRangeScan(
    THD *thd, KEY *key, unsigned used_key_parts, unsigned num_exact_key_parts,
    TABLE *table, OverflowBitset tree_applied_predicates,
    OverflowBitset tree_subsumed_predicates, const JoinHypergraph &graph,
    OverflowBitset *applied_predicates_out,
    OverflowBitset *subsumed_predicates_out) {
  MutableOverflowBitset applied_fields{thd->mem_root, table->s->fields};
  MutableOverflowBitset subsumed_fields{thd->mem_root, table->s->fields};
  MutableOverflowBitset applied_predicates(thd->mem_root,
                                           graph.predicates.size());
  MutableOverflowBitset subsumed_predicates(thd->mem_root,
                                            graph.predicates.size());
  for (unsigned keypart_idx = 0; keypart_idx < used_key_parts; ++keypart_idx) {
    const KEY_PART_INFO &keyinfo = key->key_part[keypart_idx];
    applied_fields.SetBit(keyinfo.field->field_index());
    if (keypart_idx < num_exact_key_parts &&
        !Overlaps(keyinfo.key_part_flag, HA_PART_KEY_SEG)) {
      subsumed_fields.SetBit(keyinfo.field->field_index());
    }
  }
  for (int predicate_idx : BitsSetIn(tree_applied_predicates)) {
    Item *condition = graph.predicates[predicate_idx].condition;
    bool any_not_applied =
        WalkItem(condition, enum_walk::POSTFIX, [&applied_fields](Item *item) {
          return item->type() == Item::FIELD_ITEM &&
                 !IsBitSet(down_cast<Item_field *>(item)->field->field_index(),
                           applied_fields);
        });
    if (any_not_applied) {
      continue;
    }
    applied_predicates.SetBit(predicate_idx);
    if (IsBitSet(predicate_idx, tree_subsumed_predicates)) {
      bool any_not_subsumed = WalkItem(
          condition, enum_walk::POSTFIX, [&subsumed_fields](Item *item) {
            return item->type() == Item::FIELD_ITEM &&
                   !IsBitSet(
                       down_cast<Item_field *>(item)->field->field_index(),
                       subsumed_fields);
          });
      if (!any_not_subsumed) {
        subsumed_predicates.SetBit(predicate_idx);
      }
    }
  }
  *applied_predicates_out = std::move(applied_predicates);
  *subsumed_predicates_out = std::move(subsumed_predicates);
}

struct PossibleRangeScan {
  unsigned idx;
  unsigned mrr_flags;
  unsigned mrr_buf_size;
  unsigned used_key_parts;
  double cost;
  ha_rows num_rows;
  bool is_ror_scan;
  bool is_imerge_scan;
  OverflowBitset applied_predicates;
  OverflowBitset subsumed_predicates;
  Quick_ranges ranges;
};

bool CollectPossibleRangeScans(
    THD *thd, SEL_TREE *tree, RANGE_OPT_PARAM *param,
    OverflowBitset tree_applied_predicates,
    OverflowBitset tree_subsumed_predicates, const JoinHypergraph &graph,
    Mem_root_array<PossibleRangeScan> *possible_scans) {
  for (unsigned idx = 0; idx < param->keys; idx++) {
    SEL_ROOT *root = tree->keys[idx];
    if (root == nullptr || root->type == SEL_ROOT::Type::MAYBE_KEY ||
        root->root->maybe_flag) {
      continue;
    }

    const uint keynr = param->real_keynr[idx];
    const bool covering_index = param->table->covering_keys.is_set(keynr);
    unsigned mrr_flags, buf_size;
    Cost_estimate cost;
    bool is_ror_scan, is_imerge_scan;

    // NOTE: We give in ORDER_NOT_RELEVANT now, but will re-run with
    // ORDER_ASC/ORDER_DESC when actually proposing the index, if that
    // yields an interesting order.
    ha_rows num_rows = check_quick_select(
        thd, param, idx, covering_index, root, /*update_tbl_stats=*/true,
        ORDER_NOT_RELEVANT, /*skip_records_in_range=*/false, &mrr_flags,
        &buf_size, &cost, &is_ror_scan, &is_imerge_scan);
    if (num_rows == HA_POS_ERROR) {
      continue;
    }

    // TODO(sgunders): See if we could have had a pre-filtering mechanism
    // that allowed us to skip extracting these ranges if the path would
    // obviously too high cost. As it is, it's a bit hard to just propose
    // the path and see if it came in, since we need e.g. num_exact_key_parts
    // as an output from this call, and that in turn affects filter cost.
    Quick_ranges ranges(param->return_mem_root);
    unsigned used_key_parts, num_exact_key_parts;
    if (get_ranges_from_tree(param->return_mem_root, param->table,
                             param->key[idx], keynr, root, MAX_REF_PARTS,
                             &used_key_parts, &num_exact_key_parts, &ranges)) {
      return true;
    }

    KEY *key = &param->table->key_info[keynr];

    PossibleRangeScan scan;
    scan.idx = idx;
    scan.mrr_flags = mrr_flags;
    scan.mrr_buf_size = buf_size;
    scan.used_key_parts = used_key_parts;
    scan.cost = cost.total_cost();
    scan.num_rows = num_rows;
    scan.is_ror_scan = is_ror_scan;
    scan.is_imerge_scan = is_imerge_scan;
    scan.ranges = std::move(ranges);
    FindAppliedAndSubsumedPredicatesForRangeScan(
        thd, key, used_key_parts, num_exact_key_parts, param->table,
        tree_applied_predicates, tree_subsumed_predicates, graph,
        &scan.applied_predicates, &scan.subsumed_predicates);
    possible_scans->push_back(std::move(scan));
  }
  return false;
}

/**
  Based on estimates for all the different range scans (which cover different
  but potentially overlapping combinations of predicates), try to find an
  estimate for the number of rows scanning the given table, with all predicates
  applied.

  The #1 priority here is to get a single estimate for all (non-parameterized)
  scans over this table (including non-range scans), that we can reuse for all
  access paths. This makes sure they are fairly compared on cost (and ordering)
  alone; different estimates would be nonsensical, and cause those where we
  happen to have lower estimates to get preferred as they are joined higher up
  in the tree. Obviously, however, it is also attractive to get an estimate that
  is as good as possible. We only really care about the total selectivity of all
  predicates; we don't care to adjust each individual selectivity.

  [Mar07] describes an unbiased estimator that is exactly what we want,
  and [Hav20] demonstrates an efficient calculation method (up to about 20–25
  possible predicates) of this estimator. Long-term, implementing this would be
  our best choice. However, the implementation is not entirely trivial:

   - If the selectivity estimates are not consistent (e.g. S(a AND b) <
     S(a)S(b)), the algorithm will fail to converge. Extra steps are needed to
     correct for this.
   - The efficient algorithm (in [Hav20]) requires a linear algebra library
     (for performant matrix multiplication and Cholesky decomposition).
   - If we have a _lot_ of estimates, even the efficient algorithm fails to
     converge in time (just the answers require 2^n space), and we would need
     additional logic to partition the problem.

  Thus, for the time being, we use an ad-hoc algorithm instead. The estimate
  will not be as good, but it will hopefully be on the pessimistic side
  (overestimating the number of rows). It goes as follows:

    1. Pick the most-covering index (ie., the range scan that applies
       the most number of predicates) that does not cover any predicates we've
       already accounted for. If there are multiple ones, choose the least
       selective.
    2. Multiply in its selectivity, and mark all the predicates it covers
       as accounted for. Repeat #1 and #2 for as long as possible.
    3. For any remaining predicates, multiply by their existing estimate
       (ie., the one not coming from the range optimizer).

  The hope is that in #1, we will usually prefer using selectivity information
  from indexes with more keyparts; e.g., it's better to use an index on (a,b)
  than on (a) alone, since it will take into account the correlation between
  predicates on a and predicates on b.


  [Mar07]: Markl et al: “Consistent Selectivity Estimation Via Maximum Entropy”
  [Hav20]: Havenstein et al: “Fast Entropy Maximization for Selectivity
     Estimation of Conjunctive Predicates on CPUs and GPUs”
 */
double EstimateOutputRowsFromRangeTree(
    THD *thd, const RANGE_OPT_PARAM &param, ha_rows total_rows,
    const Mem_root_array<PossibleRangeScan> &possible_scans,
    const JoinHypergraph &graph, OverflowBitset predicates, string *trace) {
  MutableOverflowBitset remaining_predicates = predicates.Clone(thd->mem_root);
  double selectivity = 1.0;
  while (!IsEmpty(remaining_predicates)) {
    const PossibleRangeScan *best_scan = nullptr;
    int best_cover_size = 0;         // Just a cache, for convenience.
    double best_selectivity = -1.0;  // Same.

    for (const PossibleRangeScan &scan : possible_scans) {
      if (IsEmpty(scan.applied_predicates) ||
          !IsSubset(scan.applied_predicates, remaining_predicates)) {
        continue;
      }
      int cover_size = PopulationCount(scan.applied_predicates);
      // NOTE: The check for num_rows >= total_rows is because total_rows may be
      // outdated, and we wouldn't want to have selectivities above 1.0, or NaN
      // or Inf if total_rows is zero.
      const double scan_selectivity =
          scan.num_rows >= total_rows
              ? 1.0
              : scan.num_rows / static_cast<double>(total_rows);
      if (cover_size > best_cover_size ||
          (cover_size == best_cover_size &&
           scan_selectivity > best_selectivity)) {
        best_scan = &scan;
        best_cover_size = cover_size;
        best_selectivity = scan_selectivity;
      }
    }

    if (best_scan == nullptr) {
      // Couldn't use any more range scans (possibly because all have
      // been used).
      break;
    }

    selectivity *= best_selectivity;

    // Mark these predicates as being dealt with.
    for (int predicate_idx : BitsSetIn(best_scan->applied_predicates)) {
      remaining_predicates.ClearBit(predicate_idx);
    }

    if (trace != nullptr) {
      const unsigned keynr = param.real_keynr[best_scan->idx];
      KEY *key = &param.table->key_info[keynr];
      *trace += StringPrintf(
          " - using selectivity %.3f (%llu rows) from range scan on index %s "
          "to cover ",
          best_selectivity, best_scan->num_rows, key->name);
      bool first = true;
      for (int predicate_idx : BitsSetIn(best_scan->applied_predicates)) {
        if (!first) {
          *trace += " AND ";
        }
        first = false;
        *trace +=
            "(" + ItemToString(graph.predicates[predicate_idx].condition) + ")";
      }
      *trace += "\n";
    }
  }

  // Cover any remaining predicates by single-predicate estimates.
  for (int predicate_idx : BitsSetIn(std::move(remaining_predicates))) {
    if (trace != nullptr) {
      *trace += StringPrintf(
          " - using existing selectivity %.3f from outside range scan "
          "to cover %s\n",
          graph.predicates[predicate_idx].selectivity,
          ItemToString(graph.predicates[predicate_idx].condition).c_str());
    }
    selectivity *= graph.predicates[predicate_idx].selectivity;
  }
  return total_rows * selectivity;
}

/**
  From a collection of index scans, find the single cheapest one and generate an
  AccessPath for it. This is similar to CollectPossibleRangeScans(), except that
  this is for index merge, where we don't want to enumerate all possibilities;
  since we don't care about the ordering of the index (we're going to sort all
  of the rows to deduplicate them anyway), cost is the only interesting metric,
  so we only need to pick out and collect ranges for one of them.
  (This isn't strictly true; sometimes, it can be attractive to choose a
  clustered primary key, so we prefer one if we allow them. See the code about
  is_preferred_cpk below, and the comment on the caller. Also, see about
  exactness below.)

  This function can probably be extended to find ROR-capable scans later
  (just check is_ror_scan instead of is_imerge_scan).

  Note that all such scans are index-only (covering), which is reflected in
  the cost parameters we use.

  *inexact is set to true if and only if the chosen path does not reflect its
  predicate faithfully, and needs to be rechecked. We do not currently take
  into account that this may affect the cost higher up, as the difference
  should be small enough that we don't want the combinatorial explosion.
 */
AccessPath *FindCheapestIndexRangeScan(THD *thd, SEL_TREE *tree,
                                       RANGE_OPT_PARAM *param,
                                       bool prefer_clustered_primary_key_scan,
                                       bool *inexact) {
  double best_cost = DBL_MAX;
  int best_key = -1;
  int best_num_rows = -1;
  unsigned best_mrr_flags = 0, best_mrr_buf_size = 0;
  for (unsigned idx = 0; idx < param->keys; idx++) {
    SEL_ROOT *root = tree->keys[idx];
    if (root == nullptr || root->type == SEL_ROOT::Type::MAYBE_KEY ||
        root->root->maybe_flag) {
      continue;
    }

    unsigned mrr_flags, buf_size;
    Cost_estimate cost;
    bool is_ror_scan, is_imerge_scan;

    ha_rows num_rows =
        check_quick_select(thd, param, idx, /*index_only=*/true, root,
                           /*update_tbl_stats=*/true, ORDER_NOT_RELEVANT,
                           /*skip_records_in_range=*/false, &mrr_flags,
                           &buf_size, &cost, &is_ror_scan, &is_imerge_scan);
    if (num_rows == HA_POS_ERROR || !is_imerge_scan) {
      continue;
    }
    const bool is_preferred_cpk =
        prefer_clustered_primary_key_scan &&
        param->table->file->primary_key_is_clustered() &&
        param->real_keynr[idx] == param->table->s->primary_key;
    if (!is_preferred_cpk && cost.total_cost() > best_cost) {
      continue;
    }

    best_key = idx;
    best_cost = cost.total_cost();
    best_num_rows = num_rows;
    best_mrr_flags = mrr_flags;
    best_mrr_buf_size = buf_size;

    if (is_preferred_cpk) {
      break;
    }
  }
  if (best_key == -1) {
    return nullptr;
  }

  const uint keynr = param->real_keynr[best_key];
  SEL_ROOT *root = tree->keys[best_key];

  Quick_ranges ranges(param->return_mem_root);
  unsigned used_key_parts, num_exact_key_parts;
  if (get_ranges_from_tree(param->return_mem_root, param->table,
                           param->key[best_key], keynr, root, MAX_REF_PARTS,
                           &used_key_parts, &num_exact_key_parts, &ranges)) {
    return nullptr;
  }

  KEY *key = &param->table->key_info[keynr];

  AccessPath *path = new (param->return_mem_root) AccessPath;
  path->type = AccessPath::INDEX_RANGE_SCAN;
  path->init_cost = 0.0;
  path->cost = path->cost_before_filter = best_cost;
  path->set_num_output_rows(best_num_rows);
  path->num_output_rows_before_filter = best_num_rows;
  path->index_range_scan().index = keynr;
  path->index_range_scan().num_used_key_parts = used_key_parts;
  path->index_range_scan().used_key_part = param->key[best_key];
  path->index_range_scan().ranges = &ranges[0];
  path->index_range_scan().num_ranges = ranges.size();
  path->index_range_scan().mrr_flags = best_mrr_flags;
  path->index_range_scan().mrr_buf_size = best_mrr_buf_size;
  path->index_range_scan().can_be_used_for_ror =
      tree->ror_scans_map.is_set(best_key);
  path->index_range_scan().need_rows_in_rowid_order = false;
  path->index_range_scan().can_be_used_for_imerge = true;
  path->index_range_scan().reuse_handler = false;
  path->index_range_scan().geometry = Overlaps(key->flags, HA_SPATIAL);
  path->index_range_scan().reverse = false;
  path->index_range_scan().using_extended_key_parts = false;

  *inexact |= (num_exact_key_parts != used_key_parts);
  return path;
}

/**
  Represents a candidate index merge, ie. an OR expression of several
  range scans across different indexes (that can be reconciled by doing
  deduplication by sorting on row IDs).

  Each predicate (in our usual sense of “part of a top-level AND conjunction in
  WHERE”) can give rise to multiple index merges (if there are AND conjunctions
  within ORs), but one index merge arises from exactly one predicate.
  This is not an inherent limitation, but it is how tree_and() does it;
  if it takes two SEL_TREEs with index merges, it just combines their candidates
  wholesale; each will deal with one predicate, and the other one would just
  have to be applied as a filter.

  This is obviously suboptimal, as there are many cases where we could do
  better. Imagine something like (a = 3 OR b > 3) AND b <= 5, with separate
  indexes on a and b; obviously, we could have applied this as a single index
  merge between two range scans: (a = 3 AND b <= 5) OR (b > 3 AND b <= 5). But
  this is probably not a priority for us, so we follow the range optimizer's
  lead here and record each index merge as covering a separate, single
  predicate.
 */
struct PossibleIndexMerge {
  // The index merge itself (a list of range optimizer trees,
  // implicitly ORed together).
  SEL_IMERGE *imerge;

  // Which WHERE predicate it came from.
  size_t pred_idx;

  // If true, the index merge does not faithfully represent the entire
  // predicate (it could return more rows), and needs to be re-checked
  // with a filter.
  bool inexact;
};

bool CostingReceiver::FindIndexRangeScans(
    int node_idx, bool *impossible, double *num_output_rows_after_filter) {
  *impossible = false;
  *num_output_rows_after_filter = -1.0;
  TABLE *table = m_graph->nodes[node_idx].table;

  RANGE_OPT_PARAM param;
  if (setup_range_optimizer_param(
          m_thd, m_thd->mem_root, &m_range_optimizer_mem_root,
          table->keys_in_use_for_query, table, m_query_block, &param)) {
    return true;
  }
  m_thd->push_internal_handler(&param.error_handler);
  auto cleanup =
      create_scope_guard([thd{m_thd}] { thd->pop_internal_handler(); });

  // For each predicate touching this table only, try to include it into our
  // tree of ranges if we can.
  MutableOverflowBitset all_predicates{m_thd->mem_root,
                                       m_graph->predicates.size()};
  MutableOverflowBitset tree_applied_predicates{m_thd->mem_root,
                                                m_graph->predicates.size()};
  MutableOverflowBitset tree_subsumed_predicates{m_thd->mem_root,
                                                 m_graph->predicates.size()};
  Mem_root_array<PossibleIndexMerge> index_merges(&m_range_optimizer_mem_root);
  const NodeMap my_map = TableBitmap(node_idx);
  SEL_TREE *tree = nullptr;
  for (size_t i = 0; i < m_graph->num_where_predicates; ++i) {
    if (m_graph->predicates[i].total_eligibility_set != my_map) {
      // Only base predicates are eligible for being pushed into range scans.
      continue;
    }
    all_predicates.SetBit(i);

    SEL_TREE *new_tree = get_mm_tree(
        m_thd, &param, INNER_TABLE_BIT, INNER_TABLE_BIT,
        table->pos_in_table_list->map(),
        /*remove_jump_scans=*/true, m_graph->predicates[i].condition);
    if (param.has_errors()) {
      // Probably out of RAM; give up using the range optimizer.
      return true;
    }
    if (new_tree == nullptr || new_tree->type == SEL_TREE::ALWAYS) {
      // Nothing in this predicate could be used as range scans for any of
      // the indexes on this table. Skip the predicate for our purposes;
      // we'll be applying it as a normal one later.
      continue;
    }

    if (new_tree->keys_map.is_clear_all()) {
      // The predicate was not converted into a range scan, so it won't be
      // applied or subsumed by any index range scan.
    } else if (new_tree->inexact) {
      // The predicate was converted into a range scan, but there was some part
      // of it that couldn't be completely represented. We need to note that
      // it was converted, so that we don't double-count its
      // selectivity, but we also need to re-apply it as a filter afterwards,
      // so we cannot set it in subsumed_range_predicates.
      // Of course, we don't know the selectivity of the non-applied parts
      // of the predicate, but it's OK to overcount rows (much better than to
      // undercount them).
      tree_applied_predicates.SetBit(i);
    } else {
      // The predicate was completely represented as a range scan for at least
      // one index. This means we can mark it as subsumed for now, but note that
      // if we end up choosing some index that doesn't include the field as a
      // keypart, or one where (some of) its ranges have to be skipped, we could
      // revert this decision. See SEL_TREE::inexact and get_ranges_from_tree().
      tree_applied_predicates.SetBit(i);
      tree_subsumed_predicates.SetBit(i);
    }

    // Store any index merges this predicate gives rise to. The final ANDed tree
    // will also have a list of index merges, but it's only a combined list of
    // the ones from individual predicates, so we collect them here to know
    // which predicate they came from.
    for (SEL_IMERGE &imerge : new_tree->merges) {
      PossibleIndexMerge merge;
      merge.imerge = &imerge;
      merge.pred_idx = i;

      // If there is more than one candidate merge arising from this predicate,
      // it must be because we had an AND inside an OR (tree_and() is the only
      // case that creates multiple candidates). ANDs in index merges are pretty
      // much always handled nonexactly (see the comment on PossibleIndexMerge),
      // ie., we pick one part of the conjunction and have to check the other
      // by filter. So we need to note here that this has happened.
      merge.inexact = (new_tree->merges.size() > 1);

      // Similarly, if there is also range scan arising from this predicate
      // (again because of an AND inside an OR), we need to handle the index
      // merge nonexactly, as the index merge will need to have the range
      // predicate in a filter on top of it.
      merge.inexact |= !new_tree->keys_map.is_clear_all();

      index_merges.push_back(merge);
    }

    if (tree == nullptr) {
      tree = new_tree;
    } else {
      tree = tree_and(&param, tree, new_tree);
      if (param.has_errors()) {
        // Probably out of RAM; give up using the range optimizer.
        return true;
      }
    }
    if (tree->type == SEL_TREE::IMPOSSIBLE) {
      *impossible = true;
      return false;
    }
  }
  if (tree == nullptr) {
    // There were no range predicates on this table.
    return false;
  }
  assert(tree->type == SEL_TREE::KEY);

  Mem_root_array<PossibleRangeScan> possible_scans(&m_range_optimizer_mem_root);
  OverflowBitset tree_applied_predicates_fixed =
      std::move(tree_applied_predicates);
  OverflowBitset tree_subsumed_predicates_fixed =
      std::move(tree_subsumed_predicates);
  if (CollectPossibleRangeScans(
          m_thd, tree, &param, tree_applied_predicates_fixed,
          tree_subsumed_predicates_fixed, *m_graph, &possible_scans)) {
    return true;
  }
  *num_output_rows_after_filter = EstimateOutputRowsFromRangeTree(
      m_thd, param, table->file->stats.records, possible_scans, *m_graph,
      std::move(all_predicates), m_trace);
  if (Overlaps(table->file->ha_table_flags(), HA_NO_INDEX_ACCESS)) {
    // We only wanted to use the index for estimation, and now we've done that.
    return false;
  }

  // Propose all single-index index range scans.
  for (PossibleRangeScan &scan : possible_scans) {
    const uint keynr = param.real_keynr[scan.idx];
    KEY *key = &param.table->key_info[keynr];

    AccessPath path;
    path.type = AccessPath::INDEX_RANGE_SCAN;
    path.init_cost = 0.0;
    path.cost = path.cost_before_filter = scan.cost;
    path.num_output_rows_before_filter = scan.num_rows;
    path.index_range_scan().index = keynr;
    path.index_range_scan().num_used_key_parts = scan.used_key_parts;
    path.index_range_scan().used_key_part = param.key[scan.idx];
    path.index_range_scan().ranges = &scan.ranges[0];
    path.index_range_scan().num_ranges = scan.ranges.size();
    path.index_range_scan().mrr_flags = scan.mrr_flags;
    path.index_range_scan().mrr_buf_size = scan.mrr_buf_size;
    path.index_range_scan().can_be_used_for_ror =
        tree->ror_scans_map.is_set(scan.idx);
    path.index_range_scan().need_rows_in_rowid_order = false;
    path.index_range_scan().can_be_used_for_imerge = scan.is_imerge_scan;
    path.index_range_scan().reuse_handler = false;
    path.index_range_scan().geometry = Overlaps(key->flags, HA_SPATIAL);
    path.index_range_scan().reverse = false;
    path.index_range_scan().using_extended_key_parts = false;

    if (IsBitSet(node_idx, m_immediate_update_delete_candidates)) {
      path.immediate_update_delete_table = node_idx;
      // Don't allow immediate update of the key that is being scanned.
      if (IsUpdateStatement(m_thd) &&
          uses_index_on_fields(&path, table->write_set)) {
        path.immediate_update_delete_table = -1;
      }
    }

    bool contains_subqueries = false;  // Filled on the first iteration below.

    // First propose the unordered scan, optionally with sorting afterwards.
    for (bool materialize_subqueries : {false, true}) {
      AccessPath new_path = path;
      FunctionalDependencySet new_fd_set;
      ApplyPredicatesForBaseTable(
          node_idx, scan.applied_predicates, scan.subsumed_predicates,
          materialize_subqueries, &new_path, &new_fd_set);

      // Override the number of estimated rows, so that all paths get the same.
      new_path.set_num_output_rows(*num_output_rows_after_filter);

      string description_for_trace = string(key->name) + " range";
      ProposeAccessPathWithOrderings(
          TableBitmap(node_idx), new_fd_set,
          /*obsolete_orderings=*/0, &new_path,
          materialize_subqueries ? "mat. subq" : description_for_trace.c_str());

      if (!materialize_subqueries) {
        contains_subqueries = Overlaps(path.filter_predicates,
                                       m_graph->materializable_predicates);
        if (!contains_subqueries) {
          // Nothing to try to materialize.
          break;
        }
      }
    }

    // Now the ordered scans, if they are interesting.
    for (enum_order order_direction : {ORDER_ASC, ORDER_DESC}) {
      const auto it =
          find_if(m_active_indexes->begin(), m_active_indexes->end(),
                  [table, keynr](const ActiveIndexInfo &info) {
                    return info.table == table &&
                           info.key_idx == static_cast<int>(keynr);
                  });
      assert(it != m_active_indexes->end());
      const int ordering_idx = m_orderings->RemapOrderingIndex(
          order_direction == ORDER_ASC ? it->forward_order : it->reverse_order);
      if (ordering_idx == 0) {
        // Not an interesting order.
        continue;
      }

      // Rerun cost estimation, since sorting may have a cost.
      const bool covering_index = param.table->covering_keys.is_set(keynr);
      bool is_ror_scan, is_imerge_scan;
      Cost_estimate cost;
      ha_rows num_rows [[maybe_unused]] = check_quick_select(
          m_thd, &param, scan.idx, covering_index, tree->keys[scan.idx],
          /*update_tbl_stats=*/true, order_direction,
          /*skip_records_in_range=*/false, &path.index_range_scan().mrr_flags,
          &path.index_range_scan().mrr_buf_size, &cost, &is_ror_scan,
          &is_imerge_scan);
      // NOTE: num_rows may be different from scan.num_rows, if the statistics
      // changed in the meantime. If so, we keep the old estimate, in order to
      // get consistent values.
      path.cost = path.cost_before_filter = cost.total_cost();
      path.index_range_scan().can_be_used_for_imerge = is_imerge_scan;
      path.ordering_state = m_orderings->SetOrder(ordering_idx);
      path.index_range_scan().reverse = (order_direction == ORDER_DESC);

      // Reverse index range scans need to be told whether they should be using
      // extended key parts. If the requested scan ordering follows more
      // interesting orderings than a scan ordered by the user-defined key parts
      // only, it means the extended key parts are needed.
      path.index_range_scan().using_extended_key_parts =
          path.index_range_scan().reverse &&
          m_orderings->MoreOrderedThan(
              path.ordering_state,
              m_orderings->SetOrder(m_orderings->RemapOrderingIndex(
                  it->reverse_order_without_extended_key_parts)),
              /*obsolete_orderings=*/0);

      for (bool materialize_subqueries : {false, true}) {
        AccessPath new_path = path;
        FunctionalDependencySet new_fd_set;
        ApplyPredicatesForBaseTable(
            node_idx, scan.applied_predicates, scan.subsumed_predicates,
            materialize_subqueries, &new_path, &new_fd_set);

        // Override the number of estimated rows, so that all paths get the
        // same.
        new_path.set_num_output_rows(*num_output_rows_after_filter);

        string description_for_trace = string(key->name) + " ordered range";
        auto access_path_it = m_access_paths.find(TableBitmap(node_idx));
        assert(access_path_it != m_access_paths.end());
        ProposeAccessPath(&new_path, &access_path_it->second.paths,
                          /*obsolete_orderings=*/0,
                          materialize_subqueries
                              ? "mat. subq"
                              : description_for_trace.c_str());

        if (!contains_subqueries) {
          // Nothing to try to materialize.
          break;
        }
      }
    }
  }

  // Propose all index merges we have collected. Note that this is only
  // “sort-index” merges, ie., generally collect all the row IDs,
  // deduplicate them by sorting (in a Unique object) and then read all the
  // rows. If the indexes are “ROR compatible” (give out their rows in row ID
  // order directly, without any sort -- typically only for InnoDB indexes with
  // the primary key appended directly after the last key part), we can
  // union/intersect them directly without any sorts (“ROR scans”). However, we
  // do not support that yet; it will be for a future worklog.
  for (const PossibleIndexMerge &imerge : index_merges) {
    for (bool allow_clustered_primary_key_scan : {true, false}) {
      bool has_clustered_primary_key_scan;
      ProposeIndexMerge(table, node_idx, *imerge.imerge, imerge.pred_idx,
                        imerge.inexact, allow_clustered_primary_key_scan,
                        m_graph->num_where_predicates,
                        *num_output_rows_after_filter, &param,
                        &has_clustered_primary_key_scan);
      if (!has_clustered_primary_key_scan) {
        // No need to check scans with clustered key scans disallowed
        // if we didn't choose one to begin with.
        break;
      }
    }
  }
  return false;
}

void CostingReceiver::ProposeIndexMerge(
    TABLE *table, int node_idx, const SEL_IMERGE &imerge, int pred_idx,
    bool inexact, bool allow_clustered_primary_key_scan,
    int num_where_predicates, double num_output_rows_after_filter,
    RANGE_OPT_PARAM *param, bool *has_clustered_primary_key_scan) {
  double cost = 0.0;
  double num_output_rows = 0.0;

  // Clustered primary keys are special; we can deduplicate
  // against them cheaper than running through the Unique object,
  // so we want to keep track of its size to cost them.
  // However, they destroy ordering properties, and if there are
  // very few rows in the scan, it's probably better to avoid the
  // compare, so we need to try both with and without
  // (done in a for loop outside this function).
  *has_clustered_primary_key_scan = false;
  double non_cpk_cost = 0.0;
  double non_cpk_rows = 0.0;

  Mem_root_array<AccessPath *> paths(m_thd->mem_root);
  for (SEL_TREE *tree : imerge.trees) {
    inexact |= tree->inexact;

    // NOTE: If we allow clustered primary key scans, we prefer
    // them here even with a higher cost, in case they make the
    // entire query cheaper due to lower sort costs. (There can
    // only be one in any given index merge, since there is only
    // one primary key.) If we end up choosing it, we will be
    // called again with allow_clustered_primary_key_scan=false.
    AccessPath *path = FindCheapestIndexRangeScan(
        m_thd, tree, param,
        /*prefer_clustered_primary_key_scan=*/allow_clustered_primary_key_scan,
        &inexact);

    if (path == nullptr) {
      // Something failed; ignore.
      return;
    }
    paths.push_back(path);
    cost += path->cost;
    num_output_rows += path->num_output_rows();

    if (allow_clustered_primary_key_scan &&
        table->file->primary_key_is_clustered() &&
        path->index_range_scan().index == table->s->primary_key) {
      assert(!*has_clustered_primary_key_scan);
      *has_clustered_primary_key_scan = true;
    } else {
      non_cpk_cost += path->cost;
      non_cpk_rows = path->num_output_rows();
    }
  }

  double init_cost = non_cpk_cost;

  // If we have a clustered primary key scan, we scan it separately, without
  // going through the deduplication-by-sort. But that means we need to make
  // sure no other rows overlap with it; there's a special operation for this
  // (check if a given row ID falls inside a given primary key range),
  // but it's not free, so add it costs here.
  if (*has_clustered_primary_key_scan) {
    double compare_cost = table->cost_model()->key_compare_cost(non_cpk_rows);
    init_cost += compare_cost;
    cost += compare_cost;
  }

  // Add the cost for the Unique operations. Note that since we read
  // the clustered primary key _last_, we cannot get out a single row
  // before everything has been read and deduplicated. If we want
  // lower init_cost (i.e., for LIMIT), we should probably change this.
  const double rows_to_deduplicate =
      *has_clustered_primary_key_scan ? non_cpk_rows : num_output_rows;
  const double dup_removal_cost =
      Unique::get_use_cost(rows_to_deduplicate, table->file->ref_length,
                           m_thd->variables.sortbuff_size, table->cost_model());
  init_cost += dup_removal_cost;
  cost += dup_removal_cost;

  // Add the cost for converting the sorted row IDs into rows
  // (which is done for all rows, except for clustered primary keys).
  // This happens running for each row, so doesn't get added to init_cost.
  // NOTE: We always give is_interrupted = false, because we don't
  // really know where we will be in the join tree.
  Cost_estimate sweep_cost;
  get_sweep_read_cost(table, non_cpk_rows, /*interrupted=*/false, &sweep_cost);
  cost += sweep_cost.total_cost();

  AccessPath imerge_path;
  imerge_path.type = AccessPath::INDEX_MERGE;
  imerge_path.index_merge().table = table;
  imerge_path.index_merge().forced_by_hint = false;
  imerge_path.index_merge().allow_clustered_primary_key_scan =
      allow_clustered_primary_key_scan;
  imerge_path.index_merge().children = new (param->return_mem_root)
      Mem_root_array<AccessPath *>(std::move(paths));

  imerge_path.cost = imerge_path.cost_before_filter = cost;
  imerge_path.init_cost = init_cost;
  imerge_path.num_output_rows_before_filter =
      min<double>(num_output_rows, num_output_rows_after_filter);
  imerge_path.set_num_output_rows(imerge_path.num_output_rows_before_filter);

  if (IsBitSet(node_idx, m_immediate_update_delete_candidates)) {
    imerge_path.immediate_update_delete_table = node_idx;
    // Don't allow immediate update of any keys being scanned.
    if (IsUpdateStatement(m_thd) &&
        uses_index_on_fields(&imerge_path, table->write_set)) {
      imerge_path.immediate_update_delete_table = -1;
    }
  }

  // Find out which ordering we would follow, if any. We nominally sort
  // everything by row ID (which follows the primary key), but if we have a
  // clustered primary key scan, it is taken after everything else and thus
  // out-of-order (ironically enough).
  if (!*has_clustered_primary_key_scan &&
      table->file->primary_key_is_clustered()) {
    const auto it = find_if(
        m_active_indexes->begin(), m_active_indexes->end(),
        [table](const ActiveIndexInfo &info) {
          return info.table == table &&
                 info.key_idx == static_cast<int>(table->s->primary_key);
        });
    if (it != m_active_indexes->end()) {
      imerge_path.ordering_state = m_orderings->SetOrder(
          m_orderings->RemapOrderingIndex(it->forward_order));
    }
  }

  // An index merge corresponds to one predicate (see comment on
  // PossibleIndexMerge), and subsumes that predicate if and only if it is a
  // faithful representation of everything in it.
  MutableOverflowBitset this_predicate(param->temp_mem_root,
                                       num_where_predicates);
  this_predicate.SetBit(pred_idx);
  OverflowBitset applied_predicates(std::move(this_predicate));
  OverflowBitset subsumed_predicates =
      inexact ? OverflowBitset(MutableOverflowBitset(param->temp_mem_root,
                                                     num_where_predicates))
              : applied_predicates;
  const bool contains_subqueries = Overlaps(imerge_path.filter_predicates,
                                            m_graph->materializable_predicates);
  for (bool materialize_subqueries : {false, true}) {
    AccessPath new_path = imerge_path;
    FunctionalDependencySet new_fd_set;
    ApplyPredicatesForBaseTable(node_idx, applied_predicates,
                                subsumed_predicates, materialize_subqueries,
                                &new_path, &new_fd_set);

    // Override the number of estimated rows, so that all paths get the
    // same.
    new_path.set_num_output_rows(num_output_rows_after_filter);

    ProposeAccessPathWithOrderings(
        TableBitmap(node_idx), new_fd_set,
        /*obsolete_orderings=*/0, &new_path,
        materialize_subqueries ? "mat. subq" : "index merge");

    if (!contains_subqueries) {
      // Nothing to try to materialize.
      break;
    }
  }
}

// Specifies a mapping in an Index_lookup between an index keypart and a
// condition, with the intention to satisfy the condition with the index keypart
// (ref access). Roughly comparable to Key_use in the non-hypergraph optimizer.
struct KeypartForRef {
  // The condition we are pushing down (e.g. t1.f1 = 3).
  Item *condition;

  // The field that is to be matched (e.g. t1.f1).
  Field *field;

  // The value we are matching against (e.g. 3). Could be another field.
  Item *val;

  // Whether this condition would never match if either side is NULL.
  bool null_rejecting;

  // Tables used by the condition. Necessarily includes the table “field”
  // is part of.
  table_map used_tables;

  // Is it safe to evaluate "val" during optimization? It must be
  // const_for_execution() and contain no subqueries or stored procedures.
  bool can_evaluate;
};

int WasPushedDownToRef(Item *condition, const KeypartForRef *keyparts,
                       unsigned num_keyparts) {
  for (unsigned keypart_idx = 0; keypart_idx < num_keyparts; keypart_idx++) {
    if (condition->eq(keyparts[keypart_idx].condition,
                      /*binary_cmp=*/true)) {
      return keypart_idx;
    }
  }
  return -1;
}

bool ContainsSubqueries(Item *item_arg) {
  // Nearly the same as item_arg->has_subquery(), but different for
  // Item_func_not_all, which we currently do not support.
  return WalkItem(item_arg, enum_walk::POSTFIX, [](Item *item) {
    return item->type() == Item::SUBSELECT_ITEM;
  });
}

bool CostingReceiver::ProposeRefAccess(
    TABLE *table, int node_idx, unsigned key_idx,
    double force_num_output_rows_after_filter, bool reverse,
    table_map allowed_parameter_tables, int ordering_idx) {
  KEY *key = &table->key_info[key_idx];

  if (key->flags & HA_FULLTEXT) {
    return false;
  }

  // Go through each of the sargable predicates and see how many key parts
  // we can match.
  unsigned matched_keyparts = 0;
  unsigned length = 0;
  const unsigned usable_keyparts = actual_key_parts(key);
  KeypartForRef keyparts[MAX_REF_PARTS];
  table_map parameter_tables = 0;

  for (unsigned keypart_idx = 0;
       keypart_idx < usable_keyparts && keypart_idx < MAX_REF_PARTS;
       ++keypart_idx) {
    const KEY_PART_INFO &keyinfo = key->key_part[keypart_idx];
    bool matched_this_keypart = false;

    for (const SargablePredicate &sp :
         m_graph->nodes[node_idx].sargable_predicates) {
      if (!sp.field->part_of_key.is_set(key_idx)) {
        // Quick reject.
        continue;
      }
      Item_func_eq *item = down_cast<Item_func_eq *>(
          m_graph->predicates[sp.predicate_index].condition);
      if (sp.field->eq(keyinfo.field)) {
        const table_map other_side_tables =
            sp.other_side->used_tables() & ~PSEUDO_TABLE_BITS;
        if (IsSubset(other_side_tables, allowed_parameter_tables)) {
          parameter_tables |= other_side_tables;
          matched_this_keypart = true;
          keyparts[keypart_idx].field = sp.field;
          keyparts[keypart_idx].condition = item;
          keyparts[keypart_idx].val = sp.other_side;
          keyparts[keypart_idx].null_rejecting = true;
          keyparts[keypart_idx].used_tables = item->used_tables();
          keyparts[keypart_idx].can_evaluate = sp.can_evaluate;
          ++matched_keyparts;
          length += keyinfo.store_length;
          break;
        }
      }
    }
    if (!matched_this_keypart) {
      break;
    }
  }
  if (matched_keyparts == 0) {
    return false;
  }
  if (parameter_tables != allowed_parameter_tables) {
    // We've already seen this before, with a more lenient subset,
    // so don't try it again.
    return false;
  }

  if (matched_keyparts < usable_keyparts &&
      (table->file->index_flags(key_idx, 0, false) & HA_ONLY_WHOLE_INDEX)) {
    if (m_trace != nullptr) {
      *m_trace += StringPrintf(
          " - %s is whole-key only, and we could only match %d/%d "
          "key parts for ref access\n",
          key->name, matched_keyparts, usable_keyparts);
    }
    return false;
  }

  if (m_trace != nullptr) {
    if (matched_keyparts < usable_keyparts) {
      *m_trace += StringPrintf(
          " - %s is applicable for ref access (using %d/%d key parts only)\n",
          key->name, matched_keyparts, usable_keyparts);
    } else {
      *m_trace +=
          StringPrintf(" - %s is applicable for ref access\n", key->name);
    }
  }

  // Create Index_lookup for this ref, and set it up based on the chosen
  // keyparts.
  Index_lookup *ref = new (m_thd->mem_root) Index_lookup;
  if (init_ref(m_thd, matched_keyparts, length, key_idx, ref)) {
    return true;
  }

  uchar *key_buff = ref->key_buff;
  uchar *null_ref_key = nullptr;
  bool null_rejecting_key = true;
  for (unsigned keypart_idx = 0; keypart_idx < matched_keyparts;
       keypart_idx++) {
    KeypartForRef *keypart = &keyparts[keypart_idx];
    const KEY_PART_INFO *keyinfo = &key->key_part[keypart_idx];

    if (init_ref_part(m_thd, keypart_idx, keypart->val, /*cond_guard=*/nullptr,
                      keypart->null_rejecting, /*const_tables=*/0,
                      keypart->used_tables, keyinfo->null_bit, keyinfo,
                      key_buff, ref)) {
      return true;
    }
    // TODO(sgunders): When we get support for REF_OR_NULL,
    // set null_ref_key = key_buff here if appropriate.
    /*
      The selected key will reject matches on NULL values if:
       - the key field is nullable, and
       - predicate rejects NULL values (keypart->null_rejecting is true), or
       - JT_REF_OR_NULL is not effective.
    */
    if ((keyinfo->field->is_nullable() || table->is_nullable()) &&
        (!keypart->null_rejecting || null_ref_key != nullptr)) {
      null_rejecting_key = false;
    }
    key_buff += keyinfo->store_length;
  }

  double num_output_rows = table->file->stats.records;
  double join_condition_selectivity = 1.0;

  MutableOverflowBitset applied_predicates{m_thd->mem_root,
                                           m_graph->predicates.size()};
  MutableOverflowBitset subsumed_predicates{m_thd->mem_root,
                                            m_graph->predicates.size()};
  for (size_t i = 0; i < m_graph->predicates.size(); ++i) {
    const Predicate &pred = m_graph->predicates[i];
    int keypart_idx =
        WasPushedDownToRef(pred.condition, keyparts, matched_keyparts);
    if (keypart_idx == -1) {
      continue;
    }

    if (pred.was_join_condition) {
      // This predicate was promoted from a join condition to a WHERE predicate,
      // since it was part of a cycle. For purposes of sargable predicates,
      // we always see all relevant join conditions, so skip it this time
      // so that we don't double-count its selectivity.
      applied_predicates.SetBit(i);
      continue;
    }

    if (i < m_graph->num_where_predicates &&
        !IsSingleBitSet(pred.total_eligibility_set)) {
      // This is a WHERE condition that is either nondeterministic,
      // or after an outer join, so it is not sargable. (Having these
      // show up here is very rare, but will get more common when we
      // get to (x=... OR NULL) predicates.)
      continue;
    }

    if (!IsSubset(pred.condition->used_tables() & ~PSEUDO_TABLE_BITS,
                  table->pos_in_table_list->map())) {
      join_condition_selectivity *= pred.selectivity;
    }

    num_output_rows *= pred.selectivity;
    applied_predicates.SetBit(i);

    const KeypartForRef &keypart = keyparts[keypart_idx];
    bool subsumes;
    if (ref_lookup_subsumes_comparison(m_thd, keypart.field, keypart.val,
                                       keypart.can_evaluate, &subsumes)) {
      return true;
    }
    if (subsumes) {
      if (m_trace != nullptr) {
        *m_trace += StringPrintf(" - %s is subsumed by ref access on %s.%s\n",
                                 ItemToString(pred.condition).c_str(),
                                 table->alias, keypart.field->field_name);
      }
      subsumed_predicates.SetBit(i);
    } else {
      if (m_trace != nullptr) {
        *m_trace += StringPrintf(
            " - %s is not fully subsumed by ref access on %s.%s, keeping\n",
            ItemToString(pred.condition).c_str(), table->alias,
            keypart.field->field_name);
      }
    }
  }

  if (force_num_output_rows_after_filter >= 0.0) {
    // The range optimizer has given us an estimate for the number of
    // rows after all filters have been applied, that we should be
    // consistent with. However, that is only filters; not join conditions.
    // The join conditions we apply are completely independent of the
    // filters, so we make our usual independence assumption.
    force_num_output_rows_after_filter *= join_condition_selectivity;
  }

  // We are guaranteed to get a single row back if all of these hold:
  //
  //  - The index must be unique.
  //  - We can never query it with NULL (ie., no keyparts are nullable,
  //    or our condition is already NULL-rejecting), since NULL is
  //    an exception for unique indexes.
  //  - We use all key parts.
  //
  // This matches the logic in create_ref_for_key().
  const bool single_row = Overlaps(actual_key_flags(key), HA_NOSAME) &&
                          (!Overlaps(actual_key_flags(key), HA_NULL_PART_KEY) ||
                           null_rejecting_key) &&
                          matched_keyparts == usable_keyparts;
  if (single_row) {
    // FIXME: This can cause inconsistent row estimates between different access
    // paths doing the same thing, which is bad (it causes index lookups to be
    // unfairly preferred, especially as we add more tables to the join -- and
    // it also causes access path pruning to work less efficiently). See
    // comments in EstimateFieldSelectivity() and on has_clamped_eq_ref.
    if (num_output_rows > 1.0 && matched_keyparts >= 2) {
      has_clamped_multipart_eq_ref = true;
    }
    num_output_rows = std::min(num_output_rows, 1.0);
  }

  const double cost =
      EstimateCostForRefAccess(m_thd, table, key_idx, num_output_rows);

  AccessPath path;
  if (single_row) {
    path.type = AccessPath::EQ_REF;
    path.eq_ref().table = table;
    path.eq_ref().ref = ref;

    // We could set really any ordering here if we wanted to.
    // It's very rare that it should matter, though.
    path.ordering_state = m_orderings->SetOrder(ordering_idx);
  } else {
    path.type = AccessPath::REF;
    path.ref().table = table;
    path.ref().ref = ref;
    path.ref().reverse = reverse;

    // TODO(sgunders): Some storage engines, like NDB, can benefit from
    // use_order = false if we don't actually need the ordering later.
    // Consider adding a cost model for this, and then proposing both
    // with and without order.
    path.ordering_state = m_orderings->SetOrder(ordering_idx);
    path.ref().use_order = (path.ordering_state != 0);
  }

  path.num_output_rows_before_filter = num_output_rows;
  path.cost_before_filter = cost;
  path.init_cost = path.init_once_cost = 0.0;
  path.parameter_tables = GetNodeMapFromTableMap(
      parameter_tables & ~table->pos_in_table_list->map(),
      m_graph->table_num_to_node_num);

  if (IsBitSet(node_idx, m_immediate_update_delete_candidates)) {
    path.immediate_update_delete_table = node_idx;
    // Disallow immediate update on the key being looked up for REF_OR_NULL and
    // REF. It might be safe to update the key on which the REF lookup is
    // performed, but we follow the lead of the old optimizer and don't try it,
    // since we don't know how the engine behaves if doing an index lookup on a
    // changing index.
    //
    // EQ_REF should be safe, though. I has at most one matching row, with a
    // constant lookup value as this is the first table. So this row won't be
    // seen a second time; the iterator won't even try a second read.
    if (path.type != AccessPath::EQ_REF && IsUpdateStatement(m_thd) &&
        is_key_used(table, key_idx, table->write_set)) {
      path.immediate_update_delete_table = -1;
    }
  }

  ProposeAccessPathForIndex(
      node_idx, std::move(applied_predicates), std::move(subsumed_predicates),
      force_num_output_rows_after_filter, key->name, &path);
  return false;
}

/**
  Do we have a sargable predicate which checks if "field" is equal to a
  constant?
 */
bool HasConstantEqualityForField(
    const Mem_root_array<SargablePredicate> &sargable_predicates,
    const Field *field) {
  return std::any_of(sargable_predicates.begin(), sargable_predicates.end(),
                     [field](const SargablePredicate &sp) {
                       return sp.other_side->const_for_execution() &&
                              field->eq(sp.field);
                     });
}

/**
  Proposes all possible unique index lookups using only constants on the given
  table. This is done before exploring any other plans for the table, in order
  to allow early return for point selects, which do not benefit from using other
  access methods.

  @param node_idx    The table to propose index lookups for.
  @param[out] found  Set to true if a unique index lookup is proposed.
  @return True on error.
 */
bool CostingReceiver::ProposeAllUniqueIndexLookupsWithConstantKey(int node_idx,
                                                                  bool *found) {
  const Mem_root_array<SargablePredicate> &sargable_predicates =
      m_graph->nodes[node_idx].sargable_predicates;

  if (sargable_predicates.empty()) {
    return false;
  }

  TABLE *const table = m_graph->nodes[node_idx].table;
  assert(!table->pos_in_table_list->is_recursive_reference());
  assert(!Overlaps(table->file->ha_table_flags(), HA_NO_INDEX_ACCESS));

  for (const ActiveIndexInfo &index_info : *m_active_indexes) {
    if (index_info.table != table) {
      continue;
    }

    const KEY *const key = &table->key_info[index_info.key_idx];

    // EQ_REF is only possible on UNIQUE non-FULLTEXT indexes.
    if (!Overlaps(key->flags, HA_NOSAME) || Overlaps(key->flags, HA_FULLTEXT)) {
      continue;
    }

    const size_t num_key_parts = key->user_defined_key_parts;
    if (num_key_parts > sargable_predicates.size()) {
      // There are not enough predicates to satisfy this key with constants.
      continue;
    }

    if (std::all_of(key->key_part, key->key_part + num_key_parts,
                    [&sargable_predicates](const KEY_PART_INFO &key_part) {
                      return HasConstantEqualityForField(sargable_predicates,
                                                         key_part.field);
                    })) {
      *found = true;
      if (ProposeRefAccess(
              table, node_idx, index_info.key_idx,
              /*force_num_output_rows_after_filter=*/-1.0, /*reverse=*/false,
              /*allowed_parameter_tables=*/0,
              m_orderings->RemapOrderingIndex(index_info.forward_order))) {
        return true;
      }
    }
  }

  return false;
}

void CostingReceiver::ProposeAccessPathForIndex(
    int node_idx, OverflowBitset applied_predicates,
    OverflowBitset subsumed_predicates,
    double force_num_output_rows_after_filter,
    const char *description_for_trace, AccessPath *path) {
  MutableOverflowBitset applied_sargable_join_predicates_tmp =
      applied_predicates.Clone(m_thd->mem_root);
  applied_sargable_join_predicates_tmp.ClearBits(0,
                                                 m_graph->num_where_predicates);
  OverflowBitset applied_sargable_join_predicates =
      std::move(applied_sargable_join_predicates_tmp);

  MutableOverflowBitset subsumed_sargable_join_predicates_tmp =
      subsumed_predicates.Clone(m_thd->mem_root);
  subsumed_sargable_join_predicates_tmp.ClearBits(
      0, m_graph->num_where_predicates);
  OverflowBitset subsumed_sargable_join_predicates =
      std::move(subsumed_sargable_join_predicates_tmp);
  for (bool materialize_subqueries : {false, true}) {
    FunctionalDependencySet new_fd_set;
    ApplyPredicatesForBaseTable(node_idx, applied_predicates,
                                subsumed_predicates, materialize_subqueries,
                                path, &new_fd_set);

    if (force_num_output_rows_after_filter >= 0.0) {
      path->set_num_output_rows(force_num_output_rows_after_filter);
    }

    path->ordering_state =
        m_orderings->ApplyFDs(path->ordering_state, new_fd_set);
    path->applied_sargable_join_predicates() = OverflowBitset::Or(
        m_thd->mem_root, path->applied_sargable_join_predicates(),
        applied_sargable_join_predicates);
    path->subsumed_sargable_join_predicates() = OverflowBitset::Or(
        m_thd->mem_root, path->subsumed_sargable_join_predicates(),
        subsumed_sargable_join_predicates);
    ProposeAccessPathWithOrderings(
        TableBitmap(node_idx), new_fd_set, /*obsolete_orderings=*/0, path,
        materialize_subqueries ? "mat. subq" : description_for_trace);

    if (!Overlaps(path->filter_predicates,
                  m_graph->materializable_predicates)) {
      // Nothing to try to materialize.
      break;
    }
  }
}

bool CostingReceiver::ProposeTableScan(
    TABLE *table, int node_idx, double force_num_output_rows_after_filter) {
  Table_ref *tl = table->pos_in_table_list;
  AccessPath path;
  if (tl->is_recursive_reference()) {
    path.type = AccessPath::FOLLOW_TAIL;
    path.follow_tail().table = table;
    assert(forced_leftmost_table == 0);  // There can only be one, naturally.
    forced_leftmost_table = NodeMap{1} << node_idx;
  } else {
    path.type = AccessPath::TABLE_SCAN;
    path.table_scan().table = table;
  }
  path.count_examined_rows = true;
  path.ordering_state = 0;

  // Doing at least one table scan (this one), so mark the query as such.
  // TODO(sgunders): Move out when we get more types and this access path could
  // be replaced by something else.
  m_thd->set_status_no_index_used();

  const double num_output_rows = table->file->stats.records;
  const double cost = table->file->table_scan_cost().total_cost();

  path.num_output_rows_before_filter = num_output_rows;
  path.init_cost = path.init_once_cost = 0.0;
  path.cost_before_filter = path.cost = cost;
  if (IsBitSet(node_idx, m_immediate_update_delete_candidates)) {
    path.immediate_update_delete_table = node_idx;
    // This is a table scan, but it might be using the clustered key under the
    // cover. If so, don't allow immediate update if it's modifying the
    // primary key.
    if (IsUpdateStatement(m_thd) &&
        Overlaps(table->file->ha_table_flags(), HA_PRIMARY_KEY_IN_READ_INDEX) &&
        !table->s->is_missing_primary_key() &&
        is_key_used(table, table->s->primary_key, table->write_set)) {
      path.immediate_update_delete_table = -1;
    }
  }

  // See if this is an information schema table that must be filled in before
  // we scan.
  if (tl->schema_table != nullptr && tl->schema_table->fill_table) {
    // TODO(sgunders): We don't need to allocate materialize_path on the
    // MEM_ROOT.
    AccessPath *new_path = new (m_thd->mem_root) AccessPath(path);
    AccessPath *materialize_path =
        NewMaterializeInformationSchemaTableAccessPath(m_thd, new_path, tl,
                                                       /*condition=*/nullptr);
    materialize_path->num_output_rows_before_filter = num_output_rows;
    materialize_path->init_cost = path.cost;       // Rudimentary.
    materialize_path->init_once_cost = path.cost;  // Rudimentary.
    materialize_path->cost_before_filter = path.cost;
    materialize_path->cost = path.cost;
    materialize_path->filter_predicates = path.filter_predicates;
    materialize_path->delayed_predicates = path.delayed_predicates;
    new_path->filter_predicates.Clear();
    new_path->delayed_predicates.Clear();
    new_path->set_num_output_rows(num_output_rows);

    assert(!tl->uses_materialization());
    path = *materialize_path;
    assert(path.cost >= 0.0);
  } else if (tl->uses_materialization()) {
    // Move the path to stable storage, since we'll be referring to it.
    AccessPath *stable_path = new (m_thd->mem_root) AccessPath(path);

    // TODO(sgunders): We don't need to allocate materialize_path on the
    // MEM_ROOT.
    AccessPath *materialize_path;
    const char *always_empty_cause = nullptr;
    if (tl->is_table_function()) {
      materialize_path = NewMaterializedTableFunctionAccessPath(
          m_thd, table, tl->table_function, stable_path);
      CopyBasicProperties(*stable_path, materialize_path);
      materialize_path->cost_before_filter = materialize_path->init_cost =
          materialize_path->init_once_cost = materialize_path->cost;
      materialize_path->num_output_rows_before_filter = num_output_rows;

      materialize_path->parameter_tables = GetNodeMapFromTableMap(
          tl->table_function->used_tables() & ~PSEUDO_TABLE_BITS,
          m_graph->table_num_to_node_num);
      if (Overlaps(tl->table_function->used_tables(),
                   OUTER_REF_TABLE_BIT | RAND_TABLE_BIT)) {
        // Make sure the table function is never hashed, ever.
        materialize_path->parameter_tables |= RAND_TABLE_BIT;
      }
    } else {
      // If the derived table is known to be always empty, we may be able to
      // optimize away parts of the outer query block too.
      if (const AccessPath *derived_table_path =
              tl->derived_query_expression()->root_access_path();
          derived_table_path != nullptr &&
          derived_table_path->type == AccessPath::ZERO_ROWS) {
        always_empty_cause = derived_table_path->zero_rows().cause;
      }

      if (always_empty_cause != nullptr &&
          !IsBitSet(tl->tableno(), m_graph->tables_inner_to_outer_or_anti)) {
        // The entire query block can be optimized away. Stop planning.
        m_query_block->join->zero_result_cause = always_empty_cause;
        return true;
      }

      bool rematerialize = Overlaps(tl->derived_query_expression()->uncacheable,
                                    UNCACHEABLE_DEPENDENT);
      if (tl->common_table_expr()) {
        // Handled in clear_corr_derived_tmp_tables(), not here.
        rematerialize = false;
      }
      materialize_path = GetAccessPathForDerivedTable(
          m_thd, tl, table, rematerialize,
          /*invalidators=*/nullptr, m_need_rowid, stable_path);
      // Handle LATERAL.
      materialize_path->parameter_tables =
          GetNodeMapFromTableMap(tl->derived_query_expression()->m_lateral_deps,
                                 m_graph->table_num_to_node_num);

      // If we don't need row IDs, we also don't care about row ID safety.
      // This keeps us from retaining many extra unneeded paths.
      if (!m_need_rowid) {
        materialize_path->safe_for_rowid = AccessPath::SAFE;
      }
    }

    materialize_path->filter_predicates = path.filter_predicates;
    materialize_path->delayed_predicates = path.delayed_predicates;
    stable_path->filter_predicates.Clear();
    stable_path->delayed_predicates.Clear();
    path = *materialize_path;
    assert(path.cost >= 0.0);

    if (always_empty_cause != nullptr) {
      // The entire query block cannot be optimized away, only the inner block
      // for the derived table. But the materialization step is unnecessary, so
      // return a ZERO_ROWS path directly for the derived table. This also
      // allows subtrees of this query block to be removed (if the derived table
      // is inner-joined to some other tables).
      path = *NewZeroRowsAccessPath(
          m_thd, new (m_thd->mem_root) AccessPath(path), always_empty_cause);
    }
  }
  assert(path.cost >= 0.0);

  ProposeAccessPathForBaseTable(node_idx, force_num_output_rows_after_filter,
                                /*description_for_trace=*/"", &path);
  return false;
}

bool CostingReceiver::ProposeIndexScan(
    TABLE *table, int node_idx, double force_num_output_rows_after_filter,
    unsigned key_idx, bool reverse, int ordering_idx) {
  AccessPath path;
  path.type = AccessPath::INDEX_SCAN;
  path.index_scan().table = table;
  path.index_scan().idx = key_idx;
  path.index_scan().use_order = true;
  path.index_scan().reverse = reverse;
  path.count_examined_rows = true;
  path.ordering_state = m_orderings->SetOrder(ordering_idx);

  double num_output_rows = table->file->stats.records;
  double cost;

  // If a table scan and a primary key scan is the very same thing,
  // they should also have the same cost. However, read_cost()
  // is based on number of rows, and table_scan_cost() is based on
  // on-disk size, so it's complete potluck which one gives the
  // higher number. We force primary scan cost to be table scan cost
  // plus an arbitrary 0.1% factor, so that we will always prefer
  // table scans if we don't need the ordering (both for user experience,
  // and in case there _is_ a performance difference in the storage
  // engine), but primary index scans otherwise.
  //
  // Note that this will give somewhat more access paths than is
  // required in some cases.
  if (table->s->primary_key == key_idx &&
      table->file->primary_key_is_clustered()) {
    cost = table->file->table_scan_cost().total_cost() * 1.001;
  } else if (table->covering_keys.is_set(key_idx)) {
    // The index is covering, so we can do an index-only scan.
    cost =
        table->file->index_scan_cost(key_idx, /*ranges=*/1.0, num_output_rows)
            .total_cost();
  } else {
    cost = table->file->read_cost(key_idx, /*ranges=*/1.0, num_output_rows)
               .total_cost();
  }

  path.num_output_rows_before_filter = num_output_rows;
  path.init_cost = path.init_once_cost = 0.0;
  path.cost_before_filter = path.cost = cost;
  if (IsBitSet(node_idx, m_immediate_update_delete_candidates)) {
    path.immediate_update_delete_table = node_idx;
    // Don't allow immediate update of the key that is being scanned.
    if (IsUpdateStatement(m_thd) &&
        is_key_used(table, key_idx, table->write_set)) {
      path.immediate_update_delete_table = -1;
    }
  }

  ProposeAccessPathForBaseTable(node_idx, force_num_output_rows_after_filter,
                                table->key_info[key_idx].name, &path);
  return false;
}

// Checks if a given predicate can be subsumed by a full-text index. It can
// be subsumed if it returns TRUE for all documents returned by the full-text
// index, and FALSE for all other documents. Since a full-text index scan
// returns the documents with a positive score, predicates that are either a
// standalone call to MATCH, a comparison of MATCH > 0, or a comparison of
// 0 < MATCH, are considered subsumable.
//
// We assume that this function is only called on predicates for which
// IsSargableFullTextIndexPredicate() has returned true, so that we
// already know the predicate is a standalone MATCH function or a <, <=, >
// or >= comparing match to a constant.
bool IsSubsumableFullTextPredicate(Item_func *condition) {
  switch (condition->functype()) {
    case Item_func::MATCH_FUNC: {
      // WHERE MATCH (col) AGAINST ('search string') is subsumable.
      return true;
    }
    case Item_func::GT_FUNC: {
      // WHERE MATCH (col) AGAINST ('search string') > 0 is subsumable.
      assert(is_function_of_type(condition->get_arg(0), Item_func::FT_FUNC));
      assert(condition->get_arg(1)->const_item());
      const double value = condition->get_arg(1)->val_real();
      assert(!condition->get_arg(1)->null_value);
      return value == 0;
    }
    case Item_func::LT_FUNC: {
      // WHERE 0 < MATCH (col) AGAINST ('search string') subsumable.
      assert(condition->get_arg(0)->const_item());
      assert(is_function_of_type(condition->get_arg(1), Item_func::FT_FUNC));
      const double value = condition->get_arg(0)->val_real();
      assert(!condition->get_arg(0)->null_value);
      return value == 0;
    }
    case Item_func::GE_FUNC:
      // WHERE MATCH >= const is not subsumable, but assert the predicate is on
      // the expected form.
      assert(is_function_of_type(condition->get_arg(0), Item_func::FT_FUNC));
      assert(condition->get_arg(1)->const_item());
      return false;
    case Item_func::LE_FUNC:
      // WHERE const <= MATCH is not subsumable, but assert the predicate is on
      // the expected form.
      assert(condition->get_arg(0)->const_item());
      assert(is_function_of_type(condition->get_arg(1), Item_func::FT_FUNC));
      return false;
    default:
      // Not a sargable full-text predicate, so we don't expect to be called on
      // it.
      assert(false);
      return false;
  }
}

// Assuming that we have chosen a full-text index scan on the given predicate,
// can we pass the LIMIT of the query block as a hint to the storage engine?
//
// We can do this if we know that the number of rows seen before the LIMIT
// clause is processed, is the same number of rows as returned by the index
// scan. This is the case when:
//
// 1) It is a single-table query. No joins.
//
// 2) There is no aggregation or DISTINCT which could reduce the number of rows.
//
// 3) There is no filtering of the rows returned from the index. That is, there
// is no HAVING clause, and the WHERE clause contains no predicates apart from
// those that can be subsumed by the index.
bool IsLimitHintPushableToFullTextSearch(const Item_func_match *match,
                                         const JoinHypergraph &graph,
                                         uint64_t fulltext_predicates) {
  const Query_block *query_block = graph.query_block();
  assert(query_block->has_ft_funcs());

  // The query has a LIMIT clause.
  if (query_block->join->m_select_limit == HA_POS_ERROR) {
    return false;
  }

  // A single table, no joins.
  if (graph.nodes.size() != 1) {
    return false;
  }

  // No aggregation, DISTINCT or HAVING.
  if (query_block->is_grouped() || query_block->is_distinct() ||
      query_block->join->having_cond != nullptr) {
    return false;
  }

  // The WHERE clause contains full-text predicates only.
  if (fulltext_predicates != BitsBetween(0, graph.predicates.size())) {
    return false;
  }

  // And all the full-text predicates must be subsumed by the index scan.
  for (const Predicate &predicate : graph.predicates) {
    Item_func_match *cond = GetSargableFullTextPredicate(predicate);
    if (cond != match || !IsSubsumableFullTextPredicate(
                             down_cast<Item_func *>(predicate.condition))) {
      return false;
    }
  }

  return true;
}

// Propose full-text index scans for all full-text predicates found in the
// WHERE clause, if any. If an interesting order can be satisfied by an ordered
// full-text index scan using one of the predicates, propose an ordered scan.
// Otherwise, propose an unordered scan. (For completeness, we should have
// proposed both an ordered and an unordered scan when we have an interesting
// order. But we don't have a good estimate for the extra cost of making the
// scan ordered, so we only propose the ordered scan for simplicity. InnoDB, for
// example, uses an ordered scan regardless of whether we request it, so an
// explicitly ordered scan is no more expensive than an implicitly ordered scan,
// and it could potentially avoid a sort higher up in the query plan.)
bool CostingReceiver::ProposeAllFullTextIndexScans(
    TABLE *table, int node_idx, double force_num_output_rows_after_filter) {
  for (const FullTextIndexInfo &info : *m_fulltext_searches) {
    if (info.match->table_ref != table->pos_in_table_list) {
      continue;
    }

    // Propose a full-text index scan for each predicate that uses the MATCH
    // function given by info.match. Note that several predicates can use the
    // same MATCH function, due to Item_func_match's linking equivalent callers
    // to one canonical Item_func_match object (via set_master()/get_master()).
    //
    // For example, we may have:
    //
    //   WHERE MATCH (col) AGAINST ('string') AND
    //         MATCH (col) AGAINST ('string') > 0.3
    //
    // Both MATCH invocations have the same canonical Item_func_match object,
    // since they have the same set of columns and search for the same string.
    // In this case, we want to propose two index scans, and let the optimizer
    // pick the one that gives the plan with the lowest estimated cost.
    for (size_t i : BitsSetIn(m_sargable_fulltext_predicates)) {
      Item_func_match *match =
          GetSargableFullTextPredicate(m_graph->predicates[i]);
      assert(match != nullptr);
      if (match != info.match) continue;
      if (ProposeFullTextIndexScan(table, node_idx, match, i, info.order,
                                   force_num_output_rows_after_filter)) {
        return true;
      }
    }

    // Even if we have no predicates, we may use a full-text index scan if it is
    // possible to pass the LIMIT clause to the index scan, and the LIMIT is no
    // greater than the number of documents returned by the index scan. We only
    // do this if the index scan produces rows in an interesting order. And only
    // if the storage engine supports the extended full-text API, which is
    // required for counting the matches in the index.
    if (m_graph->predicates.empty() && info.order != 0 &&
        IsLimitHintPushableToFullTextSearch(info.match, *m_graph,
                                            m_sargable_fulltext_predicates) &&
        Overlaps(table->file->ha_table_flags(), HA_CAN_FULLTEXT_EXT)) {
      // The full-text function must be initialized before get_count() is
      // called. Even though we call init_search() on it again after the final
      // plan has been chosen, this does not mean the search is performed twice.
      if (info.match->init_search(m_thd)) {
        return true;
      }
      if (m_query_block->join->m_select_limit <= info.match->get_count()) {
        if (ProposeFullTextIndexScan(table, node_idx, info.match,
                                     /*predicate_idx=*/-1, info.order,
                                     force_num_output_rows_after_filter)) {
          return true;
        }
      }
    }
  }

  return false;
}

bool CostingReceiver::ProposeFullTextIndexScan(
    TABLE *table, int node_idx, Item_func_match *match, int predicate_idx,
    int ordering_idx, double force_num_output_rows_after_filter) {
  const unsigned key_idx = match->key;
  Index_lookup *ref = new (m_thd->mem_root) Index_lookup;
  if (init_ref(m_thd, /*keyparts=*/1, /*length=*/0, key_idx, ref)) {
    return true;
  }
  ref->items[0] = match->key_item();

  const Predicate *predicate =
      predicate_idx == -1 ? nullptr : &m_graph->predicates[predicate_idx];
  assert(predicate_idx == -1 ||
         match == GetSargableFullTextPredicate(*predicate));

  MutableOverflowBitset applied_predicates{m_thd->mem_root,
                                           m_graph->predicates.size()};
  MutableOverflowBitset subsumed_predicates{m_thd->mem_root,
                                            m_graph->predicates.size()};
  double num_output_rows;
  double num_output_rows_from_index;
  if (predicate == nullptr) {
    // We have no predicate. The index is used only for ordering. We only do
    // this if we have a limit. Note that we keep the full row number count
    // here, to get consistent results; we only apply the limit for cost
    // calculations.
    assert(m_query_block->join->m_select_limit != HA_POS_ERROR);
    num_output_rows = table->file->stats.records;
    num_output_rows_from_index =
        min(table->file->stats.records, m_query_block->join->m_select_limit);
  } else {
    num_output_rows_from_index =
        table->file->stats.records * predicate->selectivity;
    if (TableBitmap(node_idx) == predicate->total_eligibility_set) {
      applied_predicates.SetBit(predicate_idx);
      if (IsSubsumableFullTextPredicate(
              down_cast<Item_func *>(predicate->condition))) {
        // The predicate can be fully subsumed by the index. Apply the full
        // selectivity on the index scan and mark the predicate as subsumed.
        subsumed_predicates.SetBit(predicate_idx);
      }

      num_output_rows = num_output_rows_from_index;
    } else {
      // We have a MATCH() predicate pushed down to a table that is on the inner
      // side of an outer join. It needs to be re-checked later, so we don't set
      // applied_predicates (and thus, we also cannot set subsumed_predicates).
      // In reality, we've done all the filtering already, but if we said that,
      // we'd get an inconsistent row count. This is one of the few cases where
      // inconsistent row counts are actually possible to get, but given that
      // the situation is so rare (and would have been even rarer if MATCH()
      // conditions triggered outer-to-inner conversions through
      // not_null_tables(), which it cannot as long as MATCH() on NULL returns
      // 0.0 instead of NULL), we opt for the lesser evil and delay the
      // selectivity application to the point of the WHERE().
      num_output_rows = table->file->stats.records;
    }
  }

  const double cost = EstimateCostForRefAccess(m_thd, table, key_idx,
                                               num_output_rows_from_index);

  const LogicalOrderings::StateIndex ordering_state =
      m_orderings->SetOrder(ordering_idx);

  const bool use_order = ordering_state != 0;

  AccessPath *path = NewFullTextSearchAccessPath(
      m_thd, table, ref, match, use_order,
      IsLimitHintPushableToFullTextSearch(match, *m_graph,
                                          m_sargable_fulltext_predicates),
      /*count_examined_rows=*/true);
  path->set_num_output_rows(num_output_rows);
  path->num_output_rows_before_filter = num_output_rows;
  path->cost = path->cost_before_filter = cost;
  path->init_cost = path->init_once_cost = 0;
  path->ordering_state = ordering_state;
  if (IsBitSet(node_idx, m_immediate_update_delete_candidates)) {
    path->immediate_update_delete_table = node_idx;
    // Don't allow immediate update of the key that is being scanned.
    if (IsUpdateStatement(m_thd) &&
        is_key_used(table, key_idx, table->write_set)) {
      path->immediate_update_delete_table = -1;
    }
  }

  ProposeAccessPathForIndex(
      node_idx, std::move(applied_predicates), std::move(subsumed_predicates),
      force_num_output_rows_after_filter, table->key_info[key_idx].name, path);
  return false;
}

void CostingReceiver::ProposeAccessPathForBaseTable(
    int node_idx, double force_num_output_rows_after_filter,
    const char *description_for_trace, AccessPath *path) {
  for (bool materialize_subqueries : {false, true}) {
    FunctionalDependencySet new_fd_set;
    ApplyPredicatesForBaseTable(
        node_idx,
        /*applied_predicates=*/
        MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()},
        /*subsumed_predicates=*/
        MutableOverflowBitset{m_thd->mem_root, m_graph->predicates.size()},
        materialize_subqueries, path, &new_fd_set);
    path->ordering_state =
        m_orderings->ApplyFDs(path->ordering_state, new_fd_set);
    if (force_num_output_rows_after_filter >= 0.0) {
      path->set_num_output_rows(force_num_output_rows_after_filter);
    }
    ProposeAccessPathWithOrderings(
        TableBitmap(node_idx), new_fd_set, /*obsolete_orderings=*/0, path,
        materialize_subqueries ? "mat. subq" : description_for_trace);

    if (!Overlaps(path->filter_predicates,
                  m_graph->materializable_predicates)) {
      // Nothing to try to materialize.
      return;
    }
  }
}

/**
  See which predicates that apply to this table. Some can be applied
  right away, some require other tables first and must be delayed.

  @param node_idx Index of the base table in the nodes array.
  @param applied_predicates Bitmap of predicates that are already
    applied by means of ref access, and should not be recalculated selectivity
    for.
  @param subsumed_predicates Bitmap of predicates that are applied
    by means of ref access and do not need to rechecked. Overrides
    applied_predicates.
  @param materialize_subqueries If true, any subqueries in the
    predicate should be materialized. (If there are multiple ones,
    this is an all-or-nothing decision, for simplicity.)
  @param [in,out] path The access path to apply the predicates to.
    Note that if materialize_subqueries is true, a FILTER access path
    will be inserted (overwriting "path", although a copy of it will
    be set as a child), as AccessPath::filter_predicates always assumes
    non-materialized subqueries.
 */
void CostingReceiver::ApplyPredicatesForBaseTable(
    int node_idx, OverflowBitset applied_predicates,
    OverflowBitset subsumed_predicates, bool materialize_subqueries,
    AccessPath *path, FunctionalDependencySet *new_fd_set) {
  double materialize_cost = 0.0;

  const NodeMap my_map = TableBitmap(node_idx);
  path->set_num_output_rows(path->num_output_rows_before_filter);
  path->cost = path->cost_before_filter;
  MutableOverflowBitset filter_predicates{m_thd->mem_root,
                                          m_graph->predicates.size()};
  MutableOverflowBitset delayed_predicates{m_thd->mem_root,
                                           m_graph->predicates.size()};
  new_fd_set->reset();
  for (size_t i = 0; i < m_graph->num_where_predicates; ++i) {
    if (IsBitSet(i, subsumed_predicates)) {
      // Apply functional dependencies for the base table, but no others;
      // this ensures we get the same functional dependencies set no matter what
      // access path we choose. (The ones that refer to multiple tables,
      // which are fairly rare, are not really relevant before the other
      // table(s) have been joined in.)
      if (m_graph->predicates[i].total_eligibility_set == my_map) {
        *new_fd_set |= m_graph->predicates[i].functional_dependencies;
      } else {
        // We have a WHERE predicate that refers to multiple tables,
        // that we can subsume as if it were a join condition
        // (perhaps because it was identical to an actual join condition).
        // The other side of the join will mark it as delayed, so we
        // need to do so, too. Otherwise, we would never apply the
        // associated functional dependency at the right time.
        delayed_predicates.SetBit(i);
      }
      continue;
    }
    // TODO(sgunders): We should also allow conditions that depend on
    // parameterized tables (and also touch this table, of course). See bug
    // #33477822.
    if (m_graph->predicates[i].total_eligibility_set == my_map) {
      filter_predicates.SetBit(i);
      FilterCost cost =
          EstimateFilterCost(m_thd, path->num_output_rows(),
                             m_graph->predicates[i].contained_subqueries);
      if (materialize_subqueries) {
        path->cost += cost.cost_if_materialized;
        materialize_cost += cost.cost_to_materialize;
      } else {
        path->cost += cost.cost_if_not_materialized;
        path->init_cost += cost.init_cost_if_not_materialized;
      }
      if (IsBitSet(i, applied_predicates)) {
        // We already factored in this predicate when calculating
        // the selectivity of the ref access, so don't do it again.
      } else {
        path->set_num_output_rows(path->num_output_rows() *
                                  m_graph->predicates[i].selectivity);
      }
      *new_fd_set |= m_graph->predicates[i].functional_dependencies;
    } else if (Overlaps(m_graph->predicates[i].total_eligibility_set, my_map)) {
      delayed_predicates.SetBit(i);
    }
  }
  path->filter_predicates = std::move(filter_predicates);
  path->delayed_predicates = std::move(delayed_predicates);

  if (materialize_subqueries) {
    CommitBitsetsToHeap(path);
    ExpandSingleFilterAccessPath(m_thd, path, m_query_block->join,
                                 m_graph->predicates,
                                 m_graph->num_where_predicates);
    assert(path->type == AccessPath::FILTER);
    path->filter().materialize_subqueries = true;
    path->cost += materialize_cost;  // Will be subtracted back for rescans.
    path->init_cost += materialize_cost;
    path->init_once_cost += materialize_cost;
  }
}

/**
  Checks if the table given by "node_idx" has all its lateral dependencies
  satisfied by the set of tables given by "tables".
 */
bool LateralDependenciesAreSatisfied(int node_idx, NodeMap tables,
                                     const JoinHypergraph &graph) {
  const Table_ref *table_ref = graph.nodes[node_idx].table->pos_in_table_list;

  if (table_ref->is_derived()) {
    const NodeMap lateral_deps = GetNodeMapFromTableMap(
        table_ref->derived_query_expression()->m_lateral_deps,
        graph.table_num_to_node_num);
    return IsSubset(lateral_deps, tables);
  }

  // Not a lateral derived table, so there are no lateral dependencies, and
  // hence all lateral dependencies are satisfied.
  return true;
}

/**
  Find the set of tables we can join directly against, given that we have the
  given set of tables on one of the sides (effectively the same concept as
  DPhyp's “neighborhood”). Note that having false negatives here is fine
  (it will only make DisallowParameterizedJoinPath() slightly less effective),
  but false positives is not (it may disallow valid parameterized paths,
  ultimately even making LATERAL queries impossible to plan). Thus, we need
  to check conflict rules, and our handling of hyperedges with more than one
  table on the other side may also be a bit too strict (this may need
  adjustments when we get FULL OUTER JOIN).

  If this calculation turns out to be slow, we could probably cache it in
  AccessPathSet, or even try to build it incrementally.
 */
NodeMap FindReachableTablesFrom(NodeMap tables, const JoinHypergraph &graph) {
  const Mem_root_array<Node> &nodes = graph.graph.nodes;
  const Mem_root_array<Hyperedge> &edges = graph.graph.edges;

  NodeMap reachable = 0;
  for (int node_idx : BitsSetIn(tables)) {
    for (int neighbor_idx :
         BitsSetIn(nodes[node_idx].simple_neighborhood & ~reachable)) {
      if (LateralDependenciesAreSatisfied(neighbor_idx, tables, graph)) {
        reachable |= TableBitmap(neighbor_idx);
      }
    }
    for (int edge_idx : nodes[node_idx].complex_edges) {
      if (IsSubset(edges[edge_idx].left, tables)) {
        NodeMap others = edges[edge_idx].right & ~tables;
        if (IsSingleBitSet(others) && !Overlaps(others, reachable) &&
            PassesConflictRules(tables, graph.edges[edge_idx / 2].expr) &&
            LateralDependenciesAreSatisfied(FindLowestBitSet(others), tables,
                                            graph)) {
          reachable |= others;
        }
      }
    }
  }
  return reachable;
}

// Returns whether the given set of parameter tables is partially, but not
// fully, resolved by joining towards the other side.
bool PartiallyResolvedParameterization(NodeMap parameter_tables,
                                       NodeMap other_side) {
  return (parameter_tables & ~other_side) != 0 &&
         (parameter_tables & ~other_side) != parameter_tables;
}

/**
  Decide whether joining the two given paths would create a disallowed
  parameterized path. Parameterized paths are disallowed if they delay
  joining in their parameterizations without reason (ie., they could
  join in a parameterization right away, but don't). This is a trick
  borrowed from Postgres, which essentially forces inner-join ref-lookup
  plans to be left-deep (since such plans never gain anything from being
  bushy), reducing the search space significantly without compromising
  plan quality.
 */
bool DisallowParameterizedJoinPath(AccessPath *left_path,
                                   AccessPath *right_path, NodeMap left,
                                   NodeMap right, NodeMap left_reachable,
                                   NodeMap right_reachable) {
  const NodeMap left_parameters = left_path->parameter_tables & ~RAND_TABLE_BIT;
  const NodeMap right_parameters =
      right_path->parameter_tables & ~RAND_TABLE_BIT;

  if (IsSubset(left_parameters | right_parameters, left | right)) {
    // Not creating a parameterized path, so it's always fine.
    return false;
  }

  if (!Overlaps(right_parameters, right_reachable) &&
      !Overlaps(left_parameters, left_reachable)) {
    // Either left or right cannot resolve any of their parameterizations yet
    // (e.g., we're still on the inside of an outer join that we cannot
    // finish yet), so we cannot avoid keeping them if we want to use index
    // lookups here at all.
    return false;
  }

  // If the outer table partially, but not fully, resolves the inner table's
  // parameterization, we still allow it (otherwise, we could not have
  // multi-part index lookups where the keyparts come from different tables).
  // This is the so-called “star-schema exception”.
  //
  // We need to check both ways, in case we try to swap them for a hash join.
  // Only one of these will ever be true in any given join anyway (joins where
  // we try to resolve the outer path's parameterizations with the inner one
  // are disallowed), so we do not allow more than is required.
  if (PartiallyResolvedParameterization(left_parameters, right) ||
      PartiallyResolvedParameterization(right_parameters, left)) {
    return false;
  }

  // Disallow this join; left or right (or both) should resolve their
  // parameterizations before we try to combine them.
  return true;
}

/**
  Checks if the result of a join is empty, given that it is known that one or
  both of the join legs always produces an empty result.
 */
bool IsEmptyJoin(const RelationalExpression::Type join_type, bool left_is_empty,
                 bool right_is_empty) {
  switch (join_type) {
    case RelationalExpression::INNER_JOIN:
    case RelationalExpression::STRAIGHT_INNER_JOIN:
    case RelationalExpression::SEMIJOIN:
      // If either side of an inner join or a semijoin is empty, the result of
      // the join is also empty.
      return left_is_empty || right_is_empty;
    case RelationalExpression::LEFT_JOIN:
    case RelationalExpression::ANTIJOIN:
      // If the outer side of a left join or an antijoin is empty, the result of
      // the join is also empty.
      return left_is_empty;
    case RelationalExpression::FULL_OUTER_JOIN:
      // If both sides of a full outer join are empty, the result of the join is
      // also empty.
      return left_is_empty && right_is_empty;
    case RelationalExpression::TABLE:
    case RelationalExpression::MULTI_INNER_JOIN:
      break;
  }
  assert(false);
  return false;
}

/**
  If the ON clause of a left join only references tables on the right side of
  the join, pushing the condition into the right side is a valid thing to do. If
  such conditions are not pushed down for some reason, and are left in the ON
  clause, HeatWave might reject the query. This happens if the entire join
  condition is degenerate and only references the right side. Such conditions
  are most commonly seen in queries that have gone through subquery_to_derived
  transformation.

  This limitation is worked around here by moving the degenerate join condition
  from the join predicate to a filter path on top of the right path. This is
  only done for secondary storage engines.

  TODO(khatlen): If HeatWave gets capable of processing queries with such
  conditions, this workaround should be removed.
 */
void MoveDegenerateJoinConditionToFilter(THD *thd, Query_block *query_block,
                                         const JoinPredicate **edge,
                                         AccessPath **right_path) {
  assert(SecondaryEngineHandlerton(thd) != nullptr);
  const RelationalExpression *expr = (*edge)->expr;
  assert(expr->type == RelationalExpression::LEFT_JOIN);

  // If we have a degenerate join condition which references some tables on the
  // inner side of the join, and no tables on the outer side, we are allowed to
  // filter on that condition before the join. Do so
  if (expr->conditions_used_tables == 0 ||
      !IsSubset(expr->conditions_used_tables, expr->right->tables_in_subtree)) {
    return;
  }

  // If the join condition only references tables on one side of the join, there
  // cannot be any equijoin conditions, as they reference both sides.
  assert(expr->equijoin_conditions.empty());
  assert(!expr->join_conditions.empty());

  // Create a filter on top of right_path. This filter contains the entire
  // (degenerate) join condition.
  List<Item> conds;
  for (Item *cond : expr->join_conditions) {
    conds.push_back(cond);
  }
  Item *filter_cond = CreateConjunction(&conds);
  AccessPath *filter_path = NewFilterAccessPath(thd, *right_path, filter_cond);
  CopyBasicProperties(**right_path, filter_path);
  filter_path->set_num_output_rows(filter_path->num_output_rows() *
                                   (*edge)->selectivity);
  filter_path->cost += EstimateFilterCost(thd, (*right_path)->num_output_rows(),
                                          filter_cond, query_block)
                           .cost_if_not_materialized;

  // Build a new join predicate with no join condition.
  RelationalExpression *new_expr =
      new (thd->mem_root) RelationalExpression(thd);
  new_expr->type = expr->type;
  new_expr->tables_in_subtree = expr->tables_in_subtree;
  new_expr->nodes_in_subtree = expr->nodes_in_subtree;
  new_expr->left = expr->left;
  new_expr->right = expr->right;

  JoinPredicate *new_edge = new (thd->mem_root) JoinPredicate{
      new_expr, /*selectivity=*/1.0, (*edge)->estimated_bytes_per_row,
      (*edge)->functional_dependencies, /*functional_dependencies_idx=*/{}};

  // Use the filter path and the new join edge with no condition for creating
  // the hash join.
  *right_path = filter_path;
  *edge = new_edge;
}

/**
  Called to signal that it's possible to connect the non-overlapping
  table subsets “left” and “right” through the edge given by “edge_idx”
  (which corresponds to an index in m_graph->edges), ie., we have found
  a legal subplan for joining (left ∪ right). Assign it a cost based on
  the cost of the children and the join method we use. (Currently, there
  is only one -- hash join.)

  There may be multiple such calls for the same subplan; e.g. for
  inner-joining {t1,t2,t3}, we will get calls for both {t1}/{t2,t3}
  and {t1,t2}/{t3}, and need to assign costs to both and keep the
  cheapest one. However, we will not get calls with the two subsets
  in reversed order.
 */
bool CostingReceiver::FoundSubgraphPair(NodeMap left, NodeMap right,
                                        int edge_idx) {
  if (m_thd->is_error()) return true;

  m_graph->secondary_engine_costing_flags |=
      SecondaryEngineCostingFlag::HAS_MULTIPLE_BASE_TABLES;

  if (++m_num_seen_subgraph_pairs > m_subgraph_pair_limit &&
      m_subgraph_pair_limit >= 0) {
    // Bail out; we're going to be needing graph simplification,
    // which the caller will handle for us.
    return true;
  }

  assert(left != 0);
  assert(right != 0);
  assert((left & right) == 0);

  const JoinPredicate *edge = &m_graph->edges[edge_idx];
  if (!PassesConflictRules(left | right, edge->expr)) {
    return false;
  }

  bool is_commutative = OperatorIsCommutative(*edge->expr);

  // If we have an equi-semijoin, and the inner side is deduplicated
  // on the group given by the join predicates, we can rewrite it to an
  // inner join, which is commutative. This is a win in some cases
  // where we have an index on the outer side but not the inner side.
  // (It is rarely a significant win in hash join, especially as we
  // don't propagate orders through it, but we propose it anyway for
  // simplicity.)
  //
  // See the comment on OperatorsAreAssociative() for why we don't
  // also need to change the rules about associativity or l-asscom.
  bool can_rewrite_semi_to_inner =
      edge->expr->type == RelationalExpression::SEMIJOIN &&
      edge->ordering_idx_needed_for_semijoin_rewrite != -1;

  // Enforce that recursive references need to be leftmost.
  if (Overlaps(right, forced_leftmost_table)) {
    if (!is_commutative) {
      assert(IsSingleBitSet(forced_leftmost_table));
      const int node_idx = FindLowestBitSet(forced_leftmost_table);
      my_error(ER_CTE_RECURSIVE_FORBIDDEN_JOIN_ORDER, MYF(0),
               m_graph->nodes[node_idx].table->alias);
      return true;
    }
    swap(left, right);
  }
  if (Overlaps(left, forced_leftmost_table)) {
    is_commutative = false;
    can_rewrite_semi_to_inner = false;
  }

  auto left_it = m_access_paths.find(left);
  assert(left_it != m_access_paths.end());
  auto right_it = m_access_paths.find(right);
  assert(right_it != m_access_paths.end());

  const FunctionalDependencySet new_fd_set =
      left_it->second.active_functional_dependencies |
      right_it->second.active_functional_dependencies |
      edge->functional_dependencies;
  OrderingSet new_obsolete_orderings =
      left_it->second.obsolete_orderings | right_it->second.obsolete_orderings;
  if (edge->ordering_idx_needed_for_semijoin_rewrite >= 1 &&
      edge->ordering_idx_needed_for_semijoin_rewrite < kMaxSupportedOrderings) {
    // This ordering won't be needed anymore after the join is done,
    // so mark it as obsolete.
    new_obsolete_orderings.set(edge->ordering_idx_needed_for_semijoin_rewrite);
  }

  // Check if the join is known to produce an empty result. If so, we will
  // return a ZERO_ROWS path instead of a join path, but we cannot do that just
  // yet. We need to create the join path first and attach it to the ZERO_ROWS
  // path, in case a join higher up in the join tree needs to know which tables
  // are pruned away (typically for null-complementing in outer joins).
  const bool always_empty =
      IsEmptyJoin(edge->expr->type, left_it->second.always_empty,
                  right_it->second.always_empty);

  // If the join is known to produce an empty result, and will be replaced by a
  // ZERO_ROWS path further down, temporarily disable the secondary engine cost
  // hook. There's no point in asking the secondary engine to provide a cost
  // estimate for an access path we know will be discarded.
  const secondary_engine_modify_access_path_cost_t saved_cost_hook =
      m_secondary_engine_cost_hook;
  if (always_empty) {
    m_secondary_engine_cost_hook = nullptr;
  }

  bool wrote_trace = false;

  const NodeMap left_reachable = FindReachableTablesFrom(left, *m_graph);
  const NodeMap right_reachable = FindReachableTablesFrom(right, *m_graph);
  for (AccessPath *right_path : right_it->second.paths) {
    assert(BitsetsAreCommitted(right_path));
    if (edge->expr->join_conditions_reject_all_rows &&
        edge->expr->type != RelationalExpression::FULL_OUTER_JOIN) {
      // If the join condition can never be true, we also don't need to read the
      // right side. For inner joins and semijoins, we can actually just skip
      // reading the left side as well, but if so, the join condition would
      // normally be pulled up into a WHERE condition (or into the join
      // condition of the next higher non-inner join), so we'll never see that
      // in practice, and thus, don't care particularly about the case. We also
      // don't need to care much about the ordering, since we don't propagate
      // the right-hand ordering properties through joins.
      AccessPath *zero_path = NewZeroRowsAccessPath(
          m_thd, right_path, "Join condition rejects all rows");
      MutableOverflowBitset applied_sargable_join_predicates =
          right_path->applied_sargable_join_predicates().Clone(m_thd->mem_root);
      applied_sargable_join_predicates.ClearBits(0,
                                                 m_graph->num_where_predicates);
      zero_path->filter_predicates =
          std::move(applied_sargable_join_predicates);
      zero_path->delayed_predicates = right_path->delayed_predicates;
      right_path = zero_path;
    }
    for (AccessPath *left_path : left_it->second.paths) {
      if (DisallowParameterizedJoinPath(left_path, right_path, left, right,
                                        left_reachable, right_reachable)) {
        continue;
      }

      assert(BitsetsAreCommitted(left_path));
      // For inner joins and full outer joins, the order does not matter.
      // In lieu of a more precise cost model, always keep the one that hashes
      // the fewest amount of rows. (This has lower initial cost, and the same
      // cost.)
      //
      // Finally, if either of the sides are parameterized on something
      // external, flipping the order will not necessarily be allowed (and would
      // cause us to not give a hash join for these tables at all).
      if (is_commutative &&
          !Overlaps(left_path->parameter_tables | right_path->parameter_tables,
                    RAND_TABLE_BIT)) {
        if (left_path->num_output_rows() < right_path->num_output_rows()) {
          ProposeHashJoin(right, left, right_path, left_path, edge, new_fd_set,
                          new_obsolete_orderings,
                          /*rewrite_semi_to_inner=*/false, &wrote_trace);
        } else {
          ProposeHashJoin(left, right, left_path, right_path, edge, new_fd_set,
                          new_obsolete_orderings,
                          /*rewrite_semi_to_inner=*/false, &wrote_trace);
        }
      } else {
        ProposeHashJoin(left, right, left_path, right_path, edge, new_fd_set,
                        new_obsolete_orderings,
                        /*rewrite_semi_to_inner=*/false, &wrote_trace);
        if (is_commutative || can_rewrite_semi_to_inner) {
          ProposeHashJoin(right, left, right_path, left_path, edge, new_fd_set,
                          new_obsolete_orderings,
                          /*rewrite_semi_to_inner=*/can_rewrite_semi_to_inner,
                          &wrote_trace);
        }
      }

      ProposeNestedLoopJoin(left, right, left_path, right_path, edge,
                            /*rewrite_semi_to_inner=*/false, new_fd_set,
                            new_obsolete_orderings, &wrote_trace);
      if (is_commutative || can_rewrite_semi_to_inner) {
        ProposeNestedLoopJoin(
            right, left, right_path, left_path, edge,
            /*rewrite_semi_to_inner=*/can_rewrite_semi_to_inner, new_fd_set,
            new_obsolete_orderings, &wrote_trace);
      }
      m_overflow_bitset_mem_root.ClearForReuse();
    }
  }

  if (always_empty) {
    m_secondary_engine_cost_hook = saved_cost_hook;
    const auto it = m_access_paths.find(left | right);
    if (it != m_access_paths.end() && !it->second.paths.empty() &&
        !it->second.always_empty) {
      AccessPath *first_candidate = it->second.paths.front();
      AccessPath *zero_path =
          NewZeroRowsAccessPath(m_thd, first_candidate, "impossible WHERE");
      MutableOverflowBitset applied_sargable_join_predicates =
          first_candidate->applied_sargable_join_predicates().Clone(
              m_thd->mem_root);
      applied_sargable_join_predicates.ClearBits(0,
                                                 m_graph->num_where_predicates);
      zero_path->filter_predicates =
          std::move(applied_sargable_join_predicates);
      zero_path->delayed_predicates = first_candidate->delayed_predicates;
      zero_path->ordering_state = first_candidate->ordering_state;
      ProposeAccessPathWithOrderings(
          left | right, it->second.active_functional_dependencies,
          it->second.obsolete_orderings, zero_path, "empty join");
    }
  }

  if (m_trace != nullptr) {
    TraceAccessPaths(left | right);
  }
  return false;
}

/**
  Build an access path that deduplicates its input on a certain grouping.
  This is used for converting semijoins to inner joins. If the grouping is
  empty, all rows are the same, and we make a simple LIMIT 1 instead.
 */
AccessPath *DeduplicateForSemijoin(THD *thd, AccessPath *path,
                                   Item **semijoin_group,
                                   int semijoin_group_size) {
  AccessPath *dedup_path;
  if (semijoin_group_size == 0) {
    dedup_path = NewLimitOffsetAccessPath(thd, path, /*limit=*/1, /*offset=*/0,
                                          /*count_all_rows=*/false,
                                          /*reject_multiple_rows=*/false,
                                          /*send_records_override=*/nullptr);
  } else {
    dedup_path = NewRemoveDuplicatesAccessPath(thd, path, semijoin_group,
                                               semijoin_group_size);
    CopyBasicProperties(*path, dedup_path);
    // TODO(sgunders): Model the actual reduction in rows somehow.
    dedup_path->cost += kAggregateOneRowCost * path->num_output_rows();
  }
  return dedup_path;
}

string CostingReceiver::PrintSubgraphHeader(const JoinPredicate *edge,
                                            const AccessPath &join_path,
                                            NodeMap left, NodeMap right) const {
  string ret =
      StringPrintf("\nFound sets %s and %s, connected by condition %s\n",
                   PrintSet(left).c_str(), PrintSet(right).c_str(),
                   GenerateExpressionLabel(edge->expr).c_str());
  for (int pred_idx : BitsSetIn(join_path.filter_predicates)) {
    ret += StringPrintf(
        " - applied (delayed) predicate %s\n",
        ItemToString(m_graph->predicates[pred_idx].condition).c_str());
  }
  return ret;
}

void CostingReceiver::ProposeHashJoin(
    NodeMap left, NodeMap right, AccessPath *left_path, AccessPath *right_path,
    const JoinPredicate *edge, FunctionalDependencySet new_fd_set,
    OrderingSet new_obsolete_orderings, bool rewrite_semi_to_inner,
    bool *wrote_trace) {
  if (!SupportedEngineFlag(SecondaryEngineFlag::SUPPORTS_HASH_JOIN)) return;

  if (Overlaps(left_path->parameter_tables, right) ||
      Overlaps(right_path->parameter_tables, left | RAND_TABLE_BIT)) {
    // Parameterizations must be resolved by nested loop.
    // We can still have parameters from outside the join, though
    // (even in the hash table; but it must be cleared for each Init() then).
    return;
  }

  if (Overlaps(left | right, m_fulltext_tables)) {
    // Evaluation of a full-text function requires that the underlying scan is
    // positioned on the row that contains the value to be searched. It is not
    // enough that table->record[0] contains the row; the handler needs to be
    // actually positioned on the row. This does not work so well with hash
    // joins, since they may return rows in a different order than that of the
    // underlying scan.
    //
    // For now, be conservative and don't propose a hash join if either side of
    // the join contains a full-text searched table. It is possible to be more
    // lenient and allow hash joins if all the full-text search functions on the
    // accessed tables have been fully pushed down to the table/index scan and
    // don't need to be evaluated again outside of the join.
    return;
  }

  // A semijoin by definition should have a semijoin condition to work with and
  // also that the inner table of a semijoin should not be visible outside of
  // the semijoin. However, MySQL's semijoin transformation when combined with
  // outer joins might result in a transformation which might do just that. This
  // transformation cannot be interpreted as is, but instead needs some special
  // handling in optimizer to correctly do the semijoin and outer join. However,
  // this is a problem for hypergraph. For a pattern like:
  // t1 left join (t2 semijoin t3 on true) on t1.a = t2.a and t1.b = t3.a, where
  // a semijoin does not have any condition to work with, it is expected that
  // all joins including the outer join be performed before the duplicate
  // removal happens for semijoin (Complete details in WL#5561). This is not
  // possible with hash joins. Such a pattern is a result of having a subquery
  // in an ON condition like:
  // SELECT * FROM t1 LEFT JOIN t2 ON t1.a= t2.a AND t1.b IN (SELECT a FROM t3);
  // So we ban the transformation itself for hypergraph during resolving.
  //
  // However, this also bans the transformation for a query like this:
  // SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a AND t1.a IN (SELECT a FROM t3).
  // For the above query, because of the multiple equalities, we could have
  // t1 LEFT JOIN (t2 SEMIJOIN t3 ON t2.a=t3.a) ON t1.a=t2.a which could be
  // executed using hash joins. This is a problem for secondary engine, as
  // without the semijoin transformation, it needs to process subqueries which
  // it cannot at present. So we allow the transformation to go through during
  // resolving when secondary engine optimization is ON and recognize the
  // pattern when hash join is not possible and reject it here. This is not an
  // issue for secondary engine as it eventually rejects such a query because
  // it can only perform hash joins. However it's a problem if we allow for
  // primary engine as hypergraph can go ahead and produce a mix of NLJ and hash
  // joins which leads to wrong results.
  // TODO(Chaithra): It is possible that the various join nests are looked at
  // carefully when relational expressions are created and forcing only NLJ's
  // for such cases.
  if (edge->expr->type == RelationalExpression::LEFT_JOIN &&
      edge->expr->right->type == RelationalExpression::SEMIJOIN) {
    // Check if there is a condition connecting the left side of the outer
    // join and inner side of the semijoin. This is a deviation from the
    // definition of a semijoin which makes it not possible to execute such
    // a plan with hash joins.
    RelationalExpression *semijoin = edge->expr->right;
    const table_map disallowed_tables =
        semijoin->tables_in_subtree & ~GetVisibleTables(semijoin);
    if (disallowed_tables != 0) {
      for (Item *cond : edge->expr->equijoin_conditions) {
        if (Overlaps(disallowed_tables, cond->used_tables()) &&
            Overlaps(edge->expr->left->tables_in_subtree,
                     cond->used_tables())) {
          return;
        }
      }
      for (Item *cond : edge->expr->join_conditions) {
        if (Overlaps(disallowed_tables, cond->used_tables()) &&
            Overlaps(edge->expr->left->tables_in_subtree,
                     cond->used_tables())) {
          return;
        }
      }
    }
  }

  if (edge->expr->type == RelationalExpression::LEFT_JOIN &&
      SecondaryEngineHandlerton(m_thd) != nullptr) {
    MoveDegenerateJoinConditionToFilter(m_thd, m_query_block, &edge,
                                        &right_path);
  }

  assert(BitsetsAreCommitted(left_path));
  assert(BitsetsAreCommitted(right_path));

  AccessPath join_path;
  join_path.type = AccessPath::HASH_JOIN;
  join_path.parameter_tables =
      (left_path->parameter_tables | right_path->parameter_tables) &
      ~(left | right);
  join_path.hash_join().outer = left_path;
  join_path.hash_join().inner = right_path;
  join_path.hash_join().join_predicate = edge;
  join_path.hash_join().store_rowids = false;
  join_path.hash_join().rewrite_semi_to_inner = rewrite_semi_to_inner;
  join_path.hash_join().tables_to_get_rowid_for = 0;
  join_path.hash_join().allow_spill_to_disk = true;

  // The rows from the inner side of a hash join come in different order from
  // that of the underlying scan, so we need to store row IDs for any
  // update/delete target tables on the inner side, so that we know which rows
  // to update or delete. The same applies to rows from the outer side, if the
  // hash join spills to disk, so we need to store row IDs for both sides.
  if (Overlaps(m_update_delete_target_nodes, left | right)) {
    FindTablesToGetRowidFor(&join_path);
  }

  // See the equivalent code in ProposeNestedLoopJoin().
  if (rewrite_semi_to_inner) {
    int ordering_idx = edge->ordering_idx_needed_for_semijoin_rewrite;
    assert(ordering_idx != -1);
    if (ordering_idx != 0 && !m_orderings->DoesFollowOrder(
                                 left_path->ordering_state, ordering_idx)) {
      return;
    }
    assert(edge->expr->type == RelationalExpression::SEMIJOIN);

    // NOTE: We purposefully don't overwrite left_path here, so that we
    // don't have to worry about copying ordering_state etc.
    CommitBitsetsToHeap(left_path);
    join_path.hash_join().outer = DeduplicateForSemijoin(
        m_thd, left_path, edge->semijoin_group, edge->semijoin_group_size);
  }

  // TODO(sgunders): Consider removing redundant join conditions.
  // Normally, it's better to have more equijoin conditions than fewer,
  // but in this case, every row should fall into the same hash bucket anyway,
  // so they do not help.

  double num_output_rows;
  {
    double right_path_already_applied_selectivity =
        FindAlreadyAppliedSelectivity(edge, left_path, right_path, left, right);
    if (right_path_already_applied_selectivity < 0.0) {
      return;
    }
    double outer_input_rows = left_path->num_output_rows();
    double inner_input_rows =
        right_path->num_output_rows() / right_path_already_applied_selectivity;

    // If left and right are flipped for semijoins, we need to flip
    // them back for row calculation (or we'd clamp to the wrong value).
    if (rewrite_semi_to_inner) {
      swap(outer_input_rows, inner_input_rows);
    }

    num_output_rows =
        FindOutputRowsForJoin(outer_input_rows, inner_input_rows, edge);
  }

  // left_path and join_path.hash_join().outer are intentionally different if
  // rewrite_semi_to_inner is true. See comment where DeduplicateForSemijoin()
  // is called above. We want to calculate join cost based on the actual left
  // child, so use join_path.hash_join().outer in cost calculations for
  // join_path.
  const AccessPath *outer = join_path.hash_join().outer;

  // TODO(sgunders): Add estimates for spill-to-disk costs.
  // NOTE: Keep this in sync with SimulateJoin().
  const double build_cost =
      right_path->cost + right_path->num_output_rows() * kHashBuildOneRowCost;
  double cost = outer->cost + build_cost +
                outer->num_output_rows() * kHashProbeOneRowCost +
                num_output_rows * kHashReturnOneRowCost;

  // Note: This isn't strictly correct if the non-equijoin conditions
  // have selectivities far from 1.0; the cost should be calculated
  // on the number of rows after the equijoin conditions, but before
  // the non-equijoin conditions.
  cost += num_output_rows * edge->expr->join_conditions.size() *
          kApplyOneFilterCost;

  join_path.num_output_rows_before_filter = num_output_rows;
  join_path.cost_before_filter = cost;
  join_path.set_num_output_rows(num_output_rows);
  join_path.init_cost = build_cost + outer->init_cost;

  double estimated_bytes_per_row = edge->estimated_bytes_per_row;

  // If the edge is part of a cycle in the hypergraph, there may be other usable
  // join predicates in other edges. MoveFilterPredicatesIntoHashJoinCondition()
  // will widen the hash join predicate in that case, so account for that here.
  // Only relevant when joining more than two tables. Say {t1,t2} HJ {t3}, which
  // could be joined both along a t1-t3 edge and a t2-t3 edge.
  //
  // TODO(khatlen): The cost is still calculated as if the hash join only uses
  // "edge", and that the alternative edges are put in filters on top of the
  // join.
  if (edge->expr->join_predicate_first != edge->expr->join_predicate_last &&
      PopulationCount(left | right) > 2) {
    // Only inner joins are part of cycles.
    assert(edge->expr->type == RelationalExpression::INNER_JOIN);
    for (size_t edge_idx = 0; edge_idx < m_graph->graph.edges.size();
         ++edge_idx) {
      Hyperedge hyperedge = m_graph->graph.edges[edge_idx];
      if (IsSubset(hyperedge.left, left) && IsSubset(hyperedge.right, right)) {
        const JoinPredicate *other_edge = &m_graph->edges[edge_idx / 2];
        assert(other_edge->expr->type == RelationalExpression::INNER_JOIN);
        if (other_edge != edge &&
            PassesConflictRules(left | right, other_edge->expr)) {
          estimated_bytes_per_row += EstimateHashJoinKeyWidth(other_edge->expr);
        }
      }
    }
  }

  const double reuse_buffer_probability = [&]() {
    if (right_path->parameter_tables > 0) {
      // right_path has external dependencies, so the buffer cannot be reused.
      return 0.0;
    } else {
      /*
        If the full data set from right_path fits in the join buffer,
        we never need to rebuild the hash table. build_cost should
        then be counted as init_once_cost. Otherwise, build_cost will
        be incurred for each re-scan. To get a good estimate of
        init_once_cost we therefor need to estimate the chance of
        exceeding the join buffer size. We estimate this probability as:

        (expected_data_volume / join_buffer_size)^2

        for expected_data_volume < join_buffer_size and 1.0 otherwise.
      */
      const double buffer_usage = std::min(
          1.0, estimated_bytes_per_row * right_path->num_output_rows() /
                   m_thd->variables.join_buff_size);
      return 1.0 - buffer_usage * buffer_usage;
    }
  }();

  join_path.init_once_cost =
      outer->init_once_cost +
      (1.0 - reuse_buffer_probability) * right_path->init_once_cost +
      reuse_buffer_probability * build_cost;

  join_path.cost = cost;

  // For each scan, hash join will read the left side once and the right side
  // once, so we are as safe as the least safe of the two. (This isn't true
  // if we set spill_to_disk = false, but we never do that in the hypergraph
  // optimizer.) Note that if the right side fits entirely in RAM, we don't
  // scan it the second time (so we could make the operation _more_ safe
  // than the right side, and we should consider both ways of doing
  // an inner join), but we cannot know that when planning.
  join_path.safe_for_rowid =
      std::max(left_path->safe_for_rowid, right_path->safe_for_rowid);

  // Only trace once; the rest ought to be identical.
  if (m_trace != nullptr && !*wrote_trace) {
    *m_trace += PrintSubgraphHeader(edge, join_path, left, right);
    *wrote_trace = true;
  }

  for (bool materialize_subqueries : {false, true}) {
    AccessPath new_path = join_path;
    FunctionalDependencySet filter_fd_set;
    ApplyDelayedPredicatesAfterJoin(
        left, right, left_path, right_path, edge->expr->join_predicate_first,
        edge->expr->join_predicate_last, materialize_subqueries, &new_path,
        &filter_fd_set);
    // Hash join destroys all ordering information (even from the left side,
    // since we may have spill-to-disk).
    new_path.ordering_state = m_orderings->ApplyFDs(m_orderings->SetOrder(0),
                                                    new_fd_set | filter_fd_set);
    ProposeAccessPathWithOrderings(left | right, new_fd_set | filter_fd_set,
                                   new_obsolete_orderings, &new_path,
                                   materialize_subqueries ? "mat. subq." : "");

    if (!Overlaps(new_path.filter_predicates,
                  m_graph->materializable_predicates)) {
      break;
    }
  }
}

// Of all delayed predicates, see which ones we can apply now, and which
// ones that need to be delayed further.
void CostingReceiver::ApplyDelayedPredicatesAfterJoin(
    NodeMap left, NodeMap right, const AccessPath *left_path,
    const AccessPath *right_path, int join_predicate_first,
    int join_predicate_last, bool materialize_subqueries, AccessPath *join_path,
    FunctionalDependencySet *new_fd_set) {
  // We build up a new FD set each time; it should be the same for the same
  // left/right pair, so it is somewhat redundant, but it allows us to verify
  // that property through the assert in ProposeAccessPathWithOrderings().
  new_fd_set->reset();

  // Keep track of which multiple equalities we have created predicates for
  // so far. We use this to avoid applying redundant predicates, ie. predicates
  // that have already been checked. (This is not only to avoid unneeded work,
  // but to avoid double-counting the selectivity.)
  //
  // Avoiding redundant predicates for a multi-equality is equivalent to never
  // applying those that would cause loops in the subgraph induced by the tables
  // involved in the multi-equality. (In other words, we are building spanning
  // trees in the induced subgraph.) In general, every time we connect two
  // subgraphs, we must apply every relevant multi-equality exactly once,
  // and ignore the others. (This is vaguely reminiscent of Kruskal's algorithm
  // for constructing minimum spanning trees.)
  //
  // DPhyp only ever connects subgraphs that are not already connected
  // (ie., it already constructs spanning trees), so we know that the join
  // conditions applied earlier are never redundant wrt. the rest of the graph.
  // Thus, we only need to test the delayed predicates below; they _may_ contain
  // a multiple equality we haven't already applied, but they may also be new,
  // e.g. in this graph:
  //
  //     b
  //    /|\ .
  //   a | d
  //    \|/
  //     c
  //
  // If we have a multiple equality over {b,c,d}, and connect a-b and then a-c,
  // the edge b-c will come into play and contain a multi-equality that was not
  // applied before. We will need to apply that multi-equality (we will
  // only get one of d-b and d-c). However, if we instead connected d-b
  // and d-c, the edge b-c will now be redundant and must be ignored
  // (except for functional dependencies). We simply track which ones have been
  // applied this iteration by keeping a bitmap of them.
  uint64_t multiple_equality_bitmap = 0;
  for (int pred_idx = join_predicate_first; pred_idx < join_predicate_last;
       ++pred_idx) {
    const Predicate &pred = m_graph->predicates[pred_idx];
    if (pred.source_multiple_equality_idx != -1) {
      multiple_equality_bitmap |= uint64_t{1}
                                  << pred.source_multiple_equality_idx;
    }
  }

  double materialize_cost = 0.0;

  // filter_predicates holds both filter_predicates and
  // applied_sargable_join_predicates. Keep the information about the latter,
  // but reset the one pertaining to the former.
  MutableOverflowBitset filter_predicates =
      OverflowBitset::Or(&m_overflow_bitset_mem_root,
                         left_path->applied_sargable_join_predicates(),
                         right_path->applied_sargable_join_predicates());
  filter_predicates.ClearBits(0, m_graph->num_where_predicates);

  // Predicates we are still delaying.
  MutableOverflowBitset delayed_predicates = OverflowBitset::Xor(
      &m_overflow_bitset_mem_root, left_path->delayed_predicates,
      right_path->delayed_predicates);
  delayed_predicates.ClearBits(join_predicate_first, join_predicate_last);

  // Predicates that were delayed, but that we need to check now.
  // (We don't need to allocate a MutableOverflowBitset for this.)
  const NodeMap ready_tables = left | right;
  for (int pred_idx : BitsSetInBoth(left_path->delayed_predicates,
                                    right_path->delayed_predicates)) {
    if (pred_idx >= join_predicate_first && pred_idx < join_predicate_last) {
      continue;
    }
    const Predicate &pred = m_graph->predicates[pred_idx];
    if (IsSubset(pred.total_eligibility_set, ready_tables)) {
      const auto [already_applied_as_sargable, subsumed] =
          AlreadyAppliedAsSargable(pred.condition, left_path, right_path);
      if (pred.source_multiple_equality_idx == -1 ||
          !IsBitSet(pred.source_multiple_equality_idx,
                    multiple_equality_bitmap)) {
        if (!subsumed) {
          FilterCost cost = EstimateFilterCost(
              m_thd, join_path->num_output_rows(), pred.contained_subqueries);
          if (materialize_subqueries) {
            join_path->cost += cost.cost_if_materialized;
            materialize_cost += cost.cost_to_materialize;
          } else {
            join_path->cost += cost.cost_if_not_materialized;
          }
          if (!already_applied_as_sargable) {
            join_path->set_num_output_rows(join_path->num_output_rows() *
                                           pred.selectivity);
            filter_predicates.SetBit(pred_idx);
          }
        }
        if (pred.source_multiple_equality_idx != -1) {
          multiple_equality_bitmap |= uint64_t{1}
                                      << pred.source_multiple_equality_idx;
        }
      } else if (already_applied_as_sargable) {
        // The two subgraphs are joined by at least two (additional) edges
        // both belonging to the same multiple equality (of which this predicate
        // is one). One of them, not a sargable predicate, happened to be
        // earlier in the list, and was thus deemed to be the representative of
        // that multiple equality. However, we now see another one that is
        // already applied as sargable, and thus, its selectivity has already
        // been included. Thus, we need to remove that selectivity again to
        // avoid double-counting and row count inconsistencies.
        //
        // This is a bit of a hack, but it happens pretty rarely, and it's
        // fairly straightforward. An alternative would be to have a separate
        // scan over all the delayed predicates that were already applied as
        // sargable (predicates like the one we are considering right now),
        // in order to force them into being representative for their multiple
        // equality.
        if (pred.selectivity > 1e-6) {
          join_path->set_num_output_rows(join_path->num_output_rows() /
                                         pred.selectivity);
        }
      }
      *new_fd_set |= pred.functional_dependencies;
    } else {
      delayed_predicates.SetBit(pred_idx);
    }
  }
  join_path->filter_predicates = std::move(filter_predicates);
  join_path->delayed_predicates = std::move(delayed_predicates);

  if (materialize_subqueries) {
    CommitBitsetsToHeap(join_path);
    ExpandSingleFilterAccessPath(m_thd, join_path, m_query_block->join,
                                 m_graph->predicates,
                                 m_graph->num_where_predicates);
    assert(join_path->type == AccessPath::FILTER);
    join_path->filter().materialize_subqueries = true;
    join_path->cost +=
        materialize_cost;  // Will be subtracted back for rescans.
    join_path->init_cost += materialize_cost;
    join_path->init_once_cost += materialize_cost;
  }
}

/**
  Check if we're about to apply a join condition that would be redundant
  with regards to an already-applied sargable predicate, ie., whether our
  join condition and the sargable predicate applies the same multiple equality.
  E.g. if we try to join {t1,t2} and {t3} along t1=t3, but the access path
  for t3 already has applied the join condition t2=t3, and these are from the
  same multiple equality, return true.

  Even though this is totally _legal_, having such a situation is bad, because

    a) It double-counts the selectivity, causing the overall row estimate
       to become too low.
    b) It causes unneeded work by adding a redundant filter.

  b) would normally cause the path to be pruned out due to cost, except that
  the artificially low row count due to a) could make the path attractive as a
  subplan of a larger join. Thus, we simply reject these joins; we'll see a
  different alternative for this join at some point that is not redundant
  (e.g., in the given example, we'd see the t2=t3 join).
 */
bool CostingReceiver::RedundantThroughSargable(
    OverflowBitset redundant_against_sargable_predicates,
    const AccessPath *left_path, const AccessPath *right_path, NodeMap left,
    NodeMap right) {
  // For a join condition to be redundant against an already applied sargable
  // predicate, the applied predicate must somehow connect the left side and the
  // right side. This means either:
  //
  // - One of the paths must be parameterized on at least one of the tables in
  // the other path. In the example above, because t2=t3 is applied on the {t3}
  // path, and t2 is not included in the path, the {t3} path is parameterized on
  // t2. (It is only necessary to check if right_path is parameterized on
  // left_path, since parameterization is always resolved by nested-loop joining
  // in the parameter tables from the outer/left side into the parameterized
  // path on the inner/right side.)
  //
  // - Or both paths are parameterized on some common table that is not part of
  // either path. Say if {t1,t2} has sargable t1=t4 and {t3} has sargable t3=t4,
  // then both paths are parameterized on t4, and joining {t1,t2} with {t3}
  // along t1=t3 is redundant, given all three predicates (t1=t4, t3=t4, t1=t3)
  // are from the same multiple equality.
  //
  // If the parameterization is not like that, we don't need to check any
  // further.
  assert(!Overlaps(left_path->parameter_tables, right));
  if (!Overlaps(right_path->parameter_tables,
                left | left_path->parameter_tables)) {
    return false;
  }

  const auto redundant_and_applied = [](uint64_t redundant_sargable,
                                        uint64_t left_applied,
                                        uint64_t right_applied) {
    return redundant_sargable & (left_applied | right_applied);
  };
  bool redundant_against_something_in_left = false;
  bool redundant_against_something_in_right = false;
  for (size_t predicate_idx :
       OverflowBitsetBitsIn<3, decltype(redundant_and_applied)>(
           {redundant_against_sargable_predicates,
            left_path->applied_sargable_join_predicates(),
            right_path->applied_sargable_join_predicates()},
           redundant_and_applied)) {
    // The sargable condition must work as a join condition for this join
    // (not between tables we've already joined in). Note that the joining
    // could be through two different sargable predicates; they do not have
    // to be the same. E.g., if we have
    //
    //   - t1, with sargable t1.x = t3.x
    //   - t2, with sargable t2.x = t3.x
    //   - Join condition t1.x = t2.x
    //
    // then the join condition is redundant and should be refused,
    // even though neither sargable condition joins t1 and t2 directly.
    //
    // Note that there are more complicated situations, e.g. if t2 instead
    // had t2.x = t4.x in the example above, where we could reject non-redundant
    // join orderings. However, in nearly all such cases,
    // DisallowParameterizedJoinPath() would reject them anyway, and it is not
    // an issue for successfully planning the query, as there would always exist
    // a non-parameterized path that we could use instead.
    const Predicate &sargable_predicate = m_graph->predicates[predicate_idx];
    redundant_against_something_in_left |=
        Overlaps(sargable_predicate.used_nodes, left);
    redundant_against_something_in_right |=
        Overlaps(sargable_predicate.used_nodes, right);
    if (redundant_against_something_in_left &&
        redundant_against_something_in_right) {
      return true;
    }
  }
  return false;
}

/**
  Whether the given join condition is already applied as a sargable predicate
  earlier in the tree (presumably on the right side). This is different from
  RedundantThroughSargable() in that this checks whether we have already applied
  this exact join condition earlier, while the former checks whether we are
  trying to apply a different join condition that is redundant against something
  we've applied earlier.

  The first boolean is whether “condition” is a join condition we've applied
  earlier (as sargable; so we should not count its selectivity again),
  and the second argument is whether that sargable also subsumed the entire
  join condition (so we need not apply it as a filter).
 */
pair<bool, bool> CostingReceiver::AlreadyAppliedAsSargable(
    Item *condition, const AccessPath *left_path,
    const AccessPath *right_path) {
  const auto it = m_graph->sargable_join_predicates.find(condition);
  if (it == m_graph->sargable_join_predicates.end()) {
    return {false, false};
  }

  // NOTE: It is rare that join predicates already have been applied as
  // ref access on the outer side, but not impossible if conditions are
  // duplicated; see e.g. bug #33383388.
  const bool applied =
      IsBitSet(it->second, left_path->applied_sargable_join_predicates()) ||
      IsBitSet(it->second, right_path->applied_sargable_join_predicates());
  const bool subsumed =
      IsBitSet(it->second, left_path->subsumed_sargable_join_predicates()) ||
      IsBitSet(it->second, right_path->subsumed_sargable_join_predicates());
  if (subsumed) {
    assert(applied);
  }
  return {applied, subsumed};
}

void CostingReceiver::ProposeNestedLoopJoin(
    NodeMap left, NodeMap right, AccessPath *left_path, AccessPath *right_path,
    const JoinPredicate *edge, bool rewrite_semi_to_inner,
    FunctionalDependencySet new_fd_set, OrderingSet new_obsolete_orderings,
    bool *wrote_trace) {
  if (!SupportedEngineFlag(SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN))
    return;

  if (Overlaps(left_path->parameter_tables, right)) {
    // The outer table cannot pick up values from the inner,
    // only the other way around.
    return;
  }

  assert(BitsetsAreCommitted(left_path));
  assert(BitsetsAreCommitted(right_path));

  // FULL OUTER JOIN is not possible with nested-loop join.
  assert(edge->expr->type != RelationalExpression::FULL_OUTER_JOIN);

  AccessPath join_path;
  join_path.type = AccessPath::NESTED_LOOP_JOIN;
  join_path.parameter_tables =
      (left_path->parameter_tables | right_path->parameter_tables) &
      ~(left | right);
  join_path.nested_loop_join().pfs_batch_mode = false;
  join_path.nested_loop_join().already_expanded_predicates = false;
  join_path.nested_loop_join().outer = left_path;
  join_path.nested_loop_join().inner = right_path;
  if (rewrite_semi_to_inner) {
    // This join is a semijoin (which is non-commutative), but the caller wants
    // us to try to invert it anyway; or to be precise, it has already inverted
    // it for us, and wants us to make sure that's OK. This is only
    // allowed if we can remove the duplicates from the outer (originally inner)
    // side, so check that it is grouped correctly, and then deduplicate on it.
    //
    // Note that in many cases, the grouping/ordering here would be due to an
    // earlier sort-ahead inserted into the tree. (The other case is due to
    // scanning along an index, but then, we'd usually prefer to
    // use that index for lookups instead of inverting the join. It is possible,
    // though.) If so, it would have been nice to just do a deduplicating sort
    // instead, but it would require is to track deduplication information in
    // the access paths (possibly as part of the ordering state somehow) and
    // track them throughout the join tree, which we don't do at the moment.
    // Thus, there may be an inefficiency here.
    assert(edge->expr->type == RelationalExpression::SEMIJOIN);
    int ordering_idx = edge->ordering_idx_needed_for_semijoin_rewrite;
    assert(ordering_idx != -1);
    if (ordering_idx != 0 && !m_orderings->DoesFollowOrder(
                                 left_path->ordering_state, ordering_idx)) {
      return;
    }
    join_path.nested_loop_join().join_type = JoinType::INNER;

    // NOTE: We purposefully don't overwrite left_path here, so that we
    // don't have to worry about copying ordering_state etc.
    join_path.nested_loop_join().outer = DeduplicateForSemijoin(
        m_thd, left_path, edge->semijoin_group, edge->semijoin_group_size);
  } else if (edge->expr->type == RelationalExpression::STRAIGHT_INNER_JOIN) {
    join_path.nested_loop_join().join_type = JoinType::INNER;
  } else {
    join_path.nested_loop_join().join_type =
        static_cast<JoinType>(edge->expr->type);
  }
  join_path.nested_loop_join().join_predicate = edge;

  // Nested loop joins read the outer table exactly once, and the inner table
  // potentially many times, so we can only perform immediate update or delete
  // on the outer table.
  // TODO(khatlen): If left_path is guaranteed to return at most one row (like a
  // unique index lookup), it should be possible to perform immediate delete
  // from both sides of the nested loop join. The old optimizer already does
  // that for const tables.
  join_path.immediate_update_delete_table =
      left_path->immediate_update_delete_table;

  const AccessPath *inner = join_path.nested_loop_join().inner;
  double filter_cost = 0.0;

  double right_path_already_applied_selectivity = 1.0;
  join_path.nested_loop_join().equijoin_predicates = OverflowBitset{};
  if (edge->expr->join_conditions_reject_all_rows) {
    // We've already taken out all rows from the right-hand side
    // (by means of a ZeroRowsIterator), so no need to add filters;
    // they'd only clutter the EXPLAIN.
    //
    // Note that for obscure cases (inner joins where the join condition
    // was not pulled up due to a pass ordering issue), we might see
    // the left and right path be switched around due to commutativity.
    assert(left_path->type == AccessPath::ZERO_ROWS ||
           right_path->type == AccessPath::ZERO_ROWS);
  } else if (!edge->expr->equijoin_conditions.empty() ||
             !edge->expr->join_conditions.empty()) {
    // Apply join filters. Don't update num_output_rows, as the join's
    // selectivity will already be applied in FindOutputRowsForJoin().
    // NOTE(sgunders): We don't model the effect of short-circuiting filters on
    // the cost here.
    double rows_after_filtering = inner->num_output_rows();

    right_path_already_applied_selectivity =
        FindAlreadyAppliedSelectivity(edge, left_path, right_path, left, right);
    if (right_path_already_applied_selectivity < 0.0) {
      return;
    }

    // num_output_rows is only for cost calculation and display purposes;
    // we hard-code the use of edge->selectivity below, so that we're
    // seeing the same number of rows as for hash join. This might throw
    // the filtering cost off slightly.
    MutableOverflowBitset equijoin_predicates{
        m_thd->mem_root, edge->expr->equijoin_conditions.size()};
    for (size_t join_cond_idx = 0;
         join_cond_idx < edge->expr->equijoin_conditions.size();
         ++join_cond_idx) {
      Item_eq_base *condition = edge->expr->equijoin_conditions[join_cond_idx];
      const CachedPropertiesForPredicate &properties =
          edge->expr->properties_for_equijoin_conditions[join_cond_idx];

      const auto [already_applied_as_sargable, subsumed] =
          AlreadyAppliedAsSargable(condition, left_path, right_path);
      if (!subsumed) {
        equijoin_predicates.SetBit(join_cond_idx);
        filter_cost += EstimateFilterCost(m_thd, rows_after_filtering,
                                          properties.contained_subqueries)
                           .cost_if_not_materialized;
        rows_after_filtering *= properties.selectivity;
      }
    }
    for (const CachedPropertiesForPredicate &properties :
         edge->expr->properties_for_join_conditions) {
      filter_cost += EstimateFilterCost(m_thd, rows_after_filtering,
                                        properties.contained_subqueries)
                         .cost_if_not_materialized;
      rows_after_filtering *= properties.selectivity;
    }
    join_path.nested_loop_join().equijoin_predicates =
        std::move(equijoin_predicates);
  }

  // Ignores the row count from filter_path; see above.
  {
    assert(right_path_already_applied_selectivity >= 0.0);
    double outer_input_rows = left_path->num_output_rows();
    double inner_input_rows =
        right_path->num_output_rows() / right_path_already_applied_selectivity;

    // If left and right are flipped for semijoins, we need to flip
    // them back for row calculation (or we'd clamp to the wrong value).
    if (rewrite_semi_to_inner) {
      swap(outer_input_rows, inner_input_rows);

      if (right_path_already_applied_selectivity < 1.0 &&
          PopulationCount(right) > 1) {
        // If there are multiple inner tables, it is possible that the row count
        // of the inner child is clamped by FindOutputRowsForJoin() by a
        // semijoin nested inside the inner child, and it is therefore difficult
        // to tell whether the already applied selectivity needs to be accounted
        // for or not. Until we have found a way to ensure consistent row
        // estimates between semijoin and rewrite_semi_to_inner with already
        // applied sargable predicates, just set a flag to pacify the assert in
        // ProposeAccessPath().
        has_semijoin_with_possibly_clamped_child = true;
      }
    }

    join_path.num_output_rows_before_filter =
        FindOutputRowsForJoin(outer_input_rows, inner_input_rows, edge);
    join_path.set_num_output_rows(join_path.num_output_rows_before_filter);
  }

  // left_path and join_path.nested_loop_join().outer are intentionally
  // different if rewrite_semi_to_inner is true. See comment where
  // DeduplicateForSemijoin() is called above. We want to calculate join cost
  // based on the actual left child, so use join_path.nested_loop_join().outer
  // in cost calculations for join_path.
  const AccessPath *outer = join_path.nested_loop_join().outer;

  join_path.init_cost = outer->init_cost;

  // NOTE: The ceil() around the number of rows on the left side is a workaround
  // for an issue where we think the left side has a very low cardinality,
  // e.g. 1e-5 rows, and we believe that justifies having something hugely
  // expensive on the right side (e.g. a large table scan). Obviously, this is a
  // band-aid (we should “just” have better row estimation and/or braking
  // factors), but it should be fairly benign in general.
  const double first_loop_cost = (inner->cost + filter_cost) *
                                 std::min(1.0, ceil(outer->num_output_rows()));

  const double subsequent_loops_cost =
      (inner->rescan_cost() + filter_cost) *
      std::max(0.0, outer->num_output_rows() - 1.0);

  join_path.cost_before_filter = join_path.cost =
      outer->cost + first_loop_cost + subsequent_loops_cost;

  // Nested-loop preserves any ordering from the outer side. Note that actually,
  // the two orders are _concatenated_ (if you nested-loop join something
  // ordered on (a,b) with something joined on (c,d), the order will be
  // (a,b,c,d)), but the state machine has no way of representing that.
  join_path.ordering_state =
      m_orderings->ApplyFDs(left_path->ordering_state, new_fd_set);

  // We may scan the right side several times, but the left side maybe once.
  // So if the right side is not safe to scan for row IDs after multiple scans,
  // neither are we. But if it's safe, we're exactly as safe as the left side.
  if (right_path->safe_for_rowid != AccessPath::SAFE) {
    join_path.safe_for_rowid = AccessPath::UNSAFE;
  } else {
    join_path.safe_for_rowid = left_path->safe_for_rowid;
  }

  // Only trace once; the rest ought to be identical.
  if (m_trace != nullptr && !*wrote_trace) {
    *m_trace += PrintSubgraphHeader(edge, join_path, left, right);
    *wrote_trace = true;
  }

  for (bool materialize_subqueries : {false, true}) {
    AccessPath new_path = join_path;
    FunctionalDependencySet filter_fd_set;
    ApplyDelayedPredicatesAfterJoin(
        left, right, left_path, right_path, edge->expr->join_predicate_first,
        edge->expr->join_predicate_last, materialize_subqueries, &new_path,
        &filter_fd_set);
    new_path.ordering_state = m_orderings->ApplyFDs(new_path.ordering_state,
                                                    new_fd_set | filter_fd_set);

    const char *description_for_trace = "";
    if (m_trace != nullptr) {
      if (materialize_subqueries && rewrite_semi_to_inner) {
        description_for_trace = "dedup to inner nested loop, mat. subq";
      } else if (rewrite_semi_to_inner) {
        description_for_trace = "dedup to inner nested loop";
      } else if (materialize_subqueries) {
        description_for_trace = "mat. subq";
      }
    }

    ProposeAccessPathWithOrderings(left | right, new_fd_set | filter_fd_set,
                                   new_obsolete_orderings, &new_path,
                                   description_for_trace);

    if (!Overlaps(new_path.filter_predicates,
                  m_graph->materializable_predicates)) {
      break;
    }
  }
}

/**
  Go through all equijoin conditions for the given join, and find out how much
  of its selectivity that has already been applied as ref accesses (which should
  thus be divided away from the join's selectivity).

  Returns -1.0 if there is at least one sargable predicate that is entirely
  redundant, and that this subgraph pair should not be attempted joined at all.
 */
double CostingReceiver::FindAlreadyAppliedSelectivity(
    const JoinPredicate *edge, const AccessPath *left_path,
    const AccessPath *right_path, NodeMap left, NodeMap right) {
  double already_applied = 1.0;
  for (size_t join_cond_idx = 0;
       join_cond_idx < edge->expr->equijoin_conditions.size();
       ++join_cond_idx) {
    Item_eq_base *condition = edge->expr->equijoin_conditions[join_cond_idx];
    const CachedPropertiesForPredicate &properties =
        edge->expr->properties_for_equijoin_conditions[join_cond_idx];

    const auto [already_applied_as_sargable, subsumed] =
        AlreadyAppliedAsSargable(condition, left_path, right_path);
    if (already_applied_as_sargable) {
      // This predicate was already applied as a ref access earlier.
      // Make sure not to double-count its selectivity, and also
      // that we don't reapply it if it was subsumed by the ref access.
      const auto it = m_graph->sargable_join_predicates.find(condition);
      already_applied *= m_graph->predicates[it->second].selectivity;
    } else if (RedundantThroughSargable(
                   properties.redundant_against_sargable_predicates, left_path,
                   right_path, left, right)) {
      if (m_trace != nullptr) {
        *m_trace += " - " + PrintAccessPath(*right_path, *m_graph, "") +
                    " has a sargable predicate that is redundant with our join "
                    "predicate, skipping\n";
      }
      return -1.0;
    }
  }
  return already_applied;
}

uint32_t AddFlag(uint32_t flags, FuzzyComparisonResult flag) {
  return flags | static_cast<uint32_t>(flag);
}

bool HasFlag(uint32_t flags, FuzzyComparisonResult flag) {
  return (flags & static_cast<uint32_t>(flag));
}

}  // namespace

// See if one access path is better than the other across all cost dimensions
// (if so, we say it dominates the other one). If not, we return
// DIFFERENT_STRENGTHS so that both must be kept.
//
// TODO(sgunders): Support turning off certain cost dimensions; e.g., init_cost
// only matters if we have a LIMIT or nested loop semijoin somewhere in the
// query, and it might not matter for secondary engine.
PathComparisonResult CompareAccessPaths(const LogicalOrderings &orderings,
                                        const AccessPath &a,
                                        const AccessPath &b,
                                        OrderingSet obsolete_orderings) {
#ifndef NDEBUG
  // Manual preference overrides everything else.
  // If they're both preferred, tie-break by ordering.
  if (a.forced_by_dbug) {
    return PathComparisonResult::FIRST_DOMINATES;
  } else if (b.forced_by_dbug) {
    return PathComparisonResult::SECOND_DOMINATES;
  }
#endif

  uint32_t flags = 0;

  if (a.parameter_tables != b.parameter_tables) {
    if (!IsSubset(a.parameter_tables, b.parameter_tables)) {
      flags = AddFlag(flags, FuzzyComparisonResult::SECOND_BETTER);
    }
    if (!IsSubset(b.parameter_tables, a.parameter_tables)) {
      flags = AddFlag(flags, FuzzyComparisonResult::FIRST_BETTER);
    }
  }

  // If we have a parameterized path, this means that at some point, it _must_
  // be on the right side of a nested-loop join. This destroys ordering
  // information (at least in our implementation -- see comment in
  // NestedLoopJoin()), so in this situation, consider all orderings as equal.
  // (This is a trick borrowed from Postgres to keep the number of unique access
  // paths down in such situations.)
  const int a_ordering_state = (a.parameter_tables == 0) ? a.ordering_state : 0;
  const int b_ordering_state = (b.parameter_tables == 0) ? b.ordering_state : 0;
  if (orderings.MoreOrderedThan(a_ordering_state, b_ordering_state,
                                obsolete_orderings)) {
    flags = AddFlag(flags, FuzzyComparisonResult::FIRST_BETTER);
  }
  if (orderings.MoreOrderedThan(b_ordering_state, a_ordering_state,
                                obsolete_orderings)) {
    flags = AddFlag(flags, FuzzyComparisonResult::SECOND_BETTER);
  }

  // If one path is safe for row IDs and another one is not,
  // that is also something we need to take into account.
  // Safer values have lower numerical values, so we can compare them
  // as integers.
  if (a.safe_for_rowid < b.safe_for_rowid) {
    flags = AddFlag(flags, FuzzyComparisonResult::FIRST_BETTER);
  } else if (b.safe_for_rowid < a.safe_for_rowid) {
    flags = AddFlag(flags, FuzzyComparisonResult::SECOND_BETTER);
  }

  // A path that allows immediate update or delete of a table is better than
  // a path that allows none.
  if (a.immediate_update_delete_table != b.immediate_update_delete_table) {
    if (a.immediate_update_delete_table == -1) {
      flags = AddFlag(flags, FuzzyComparisonResult::SECOND_BETTER);
    } else if (b.immediate_update_delete_table == -1) {
      flags = AddFlag(flags, FuzzyComparisonResult::FIRST_BETTER);
    }
  }

  // Numerical cost dimensions are compared fuzzily in order to treat paths
  // with insignificant differences as identical.
  constexpr double fuzz_factor = 1.01;

  // Normally, two access paths for the same subplan should have the same
  // number of output rows. However, for parameterized paths, this need not
  // be the case; due to pushdown of sargable conditions into indexes;
  // some filters may be applied earlier, causing fewer rows to be
  // carried around temporarily (until the parameterization is resolved).
  // This can have an advantage in causing less work later even if it's
  // non-optimal now, e.g. by saving on filtering work, or having less work
  // done in other joins. Thus, we need to keep it around as an extra
  // cost dimension.
  flags = AddFlag(flags, FuzzyComparison(a.num_output_rows(),
                                         b.num_output_rows(), fuzz_factor));

  flags = AddFlag(flags, FuzzyComparison(a.cost, b.cost, fuzz_factor));
  flags =
      AddFlag(flags, FuzzyComparison(a.init_cost, b.init_cost, fuzz_factor));
  flags = AddFlag(
      flags, FuzzyComparison(a.rescan_cost(), b.rescan_cost(), fuzz_factor));

  bool a_is_better = HasFlag(flags, FuzzyComparisonResult::FIRST_BETTER);
  bool b_is_better = HasFlag(flags, FuzzyComparisonResult::SECOND_BETTER);
  if (a_is_better && b_is_better) {
    return PathComparisonResult::DIFFERENT_STRENGTHS;
  } else if (a_is_better && !b_is_better) {
    return PathComparisonResult::FIRST_DOMINATES;
  } else if (!a_is_better && b_is_better) {
    return PathComparisonResult::SECOND_DOMINATES;
  } else {  // Fuzzily identical
    bool a_is_slightly_better =
        HasFlag(flags, FuzzyComparisonResult::FIRST_SLIGHTLY_BETTER);
    bool b_is_slightly_better =
        HasFlag(flags, FuzzyComparisonResult::SECOND_SLIGHTLY_BETTER);
    // If one path is no worse in all dimensions and strictly better
    // in at least one dimension we identify it as dominant.
    if (a_is_slightly_better && !b_is_slightly_better) {
      return PathComparisonResult::FIRST_DOMINATES;
    } else if (!a_is_slightly_better && b_is_slightly_better) {
      return PathComparisonResult::SECOND_DOMINATES;
    }
    return PathComparisonResult::IDENTICAL;
  }
}

namespace {

string PrintAccessPath(const AccessPath &path, const JoinHypergraph &graph,
                       const char *description_for_trace) {
  string str = "{";
  string join_order;

  switch (path.type) {
    case AccessPath::TABLE_SCAN:
      str += "TABLE_SCAN";
      break;
    case AccessPath::INDEX_SCAN:
      str += "INDEX_SCAN";
      break;
    case AccessPath::REF:
      str += "REF";
      break;
    case AccessPath::REF_OR_NULL:
      str += "REF_OR_NULL";
      break;
    case AccessPath::EQ_REF:
      str += "EQ_REF";
      break;
    case AccessPath::PUSHED_JOIN_REF:
      str += "PUSHED_JOIN_REF";
      break;
    case AccessPath::FULL_TEXT_SEARCH:
      str += "FULL_TEXT_SEARCH";
      break;
    case AccessPath::CONST_TABLE:
      str += "CONST_TABLE";
      break;
    case AccessPath::MRR:
      str += "MRR";
      break;
    case AccessPath::FOLLOW_TAIL:
      str += "FOLLOW_TAIL";
      break;
    case AccessPath::INDEX_RANGE_SCAN:
      str += "INDEX_RANGE_SCAN";
      break;
    case AccessPath::INDEX_MERGE:
      str += "INDEX_MERGE";
      break;
    case AccessPath::ROWID_INTERSECTION:
      str += "ROWID_INTERSECTION";
      break;
    case AccessPath::ROWID_UNION:
      str += "ROWID_UNION";
      break;
    case AccessPath::INDEX_SKIP_SCAN:
      str += "INDEX_SKIP_SCAN";
      break;
    case AccessPath::GROUP_INDEX_SKIP_SCAN:
      str += "GROUP_INDEX_SKIP_SCAN";
      break;
    case AccessPath::DYNAMIC_INDEX_RANGE_SCAN:
      str += "DYNAMIC_INDEX_RANGE_SCAN";
      break;
    case AccessPath::TABLE_VALUE_CONSTRUCTOR:
      str += "TABLE_VALUE_CONSTRUCTOR";
      break;
    case AccessPath::FAKE_SINGLE_ROW:
      str += "FAKE_SINGLE_ROW";
      break;
    case AccessPath::ZERO_ROWS:
      str += "ZERO_ROWS";
      break;
    case AccessPath::ZERO_ROWS_AGGREGATED:
      str += "ZERO_ROWS_AGGREGATED";
      break;
    case AccessPath::MATERIALIZED_TABLE_FUNCTION:
      str += "MATERIALIZED_TABLE_FUNCTION";
      break;
    case AccessPath::UNQUALIFIED_COUNT:
      str += "UNQUALIFIED_COUNT";
      break;
    case AccessPath::NESTED_LOOP_JOIN:
      str += "NESTED_LOOP_JOIN";
      PrintJoinOrder(&path, &join_order);
      break;
    case AccessPath::NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL:
      str += "NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL";
      PrintJoinOrder(&path, &join_order);
      break;
    case AccessPath::BKA_JOIN:
      str += "BKA_JOIN";
      PrintJoinOrder(&path, &join_order);
      break;
    case AccessPath::HASH_JOIN:
      str += "HASH_JOIN";
      PrintJoinOrder(&path, &join_order);
      break;
    case AccessPath::FILTER:
      str += "FILTER";
      break;
    case AccessPath::SORT:
      str += "SORT";
      break;
    case AccessPath::AGGREGATE:
      str += "AGGREGATE";
      break;
    case AccessPath::TEMPTABLE_AGGREGATE:
      str += "TEMPTABLE_AGGREGATE";
      break;
    case AccessPath::LIMIT_OFFSET:
      str += "LIMIT_OFFSET";
      break;
    case AccessPath::STREAM:
      str += "STREAM";
      break;
    case AccessPath::MATERIALIZE:
      str += "MATERIALIZE";
      break;
    case AccessPath::MATERIALIZE_INFORMATION_SCHEMA_TABLE:
      str += "MATERIALIZE_INFORMATION_SCHEMA_TABLE";
      break;
    case AccessPath::APPEND:
      str += "APPEND";
      break;
    case AccessPath::WINDOW:
      str += "WINDOW";
      break;
    case AccessPath::WEEDOUT:
      str += "WEEDOUT";
      break;
    case AccessPath::REMOVE_DUPLICATES:
      str += "REMOVE_DUPLICATES";
      break;
    case AccessPath::REMOVE_DUPLICATES_ON_INDEX:
      str += "REMOVE_DUPLICATES_ON_INDEX";
      break;
    case AccessPath::ALTERNATIVE:
      str += "ALTERNATIVE";
      break;
    case AccessPath::CACHE_INVALIDATOR:
      str += "CACHE_INVALIDATOR";
      break;
    case AccessPath::DELETE_ROWS:
      str += "DELETE_ROWS";
      break;
    case AccessPath::UPDATE_ROWS:
      str += "UPDATE_ROWS";
      break;
  }

  str += StringPrintf(", cost=%.1f, init_cost=%.1f", path.cost, path.init_cost);
  if (path.init_once_cost != 0.0) {
    str += StringPrintf(", rescan_cost=%.1f", path.rescan_cost());
  }
  str += StringPrintf(", rows=%.1f", path.num_output_rows());

  if (!join_order.empty()) str += ", join_order=" + join_order;

  // Print parameter tables, if any.
  if (path.parameter_tables != 0) {
    str += ", parm={";
    bool first = true;
    for (size_t node_idx : BitsSetIn(path.parameter_tables)) {
      if (!first) {
        str += ", ";
      }
      if ((uint64_t{1} << node_idx) == RAND_TABLE_BIT) {
        str += "<random>";
      } else {
        str += graph.nodes[node_idx].table->alias;
      }
      first = false;
    }
    str += "}";
  }

  if (path.ordering_state != 0) {
    str += StringPrintf(", order=%d", path.ordering_state);
  }

  if (path.safe_for_rowid == AccessPath::SAFE_IF_SCANNED_ONCE) {
    str += StringPrintf(", safe_for_rowid_once");
  } else if (path.safe_for_rowid == AccessPath::UNSAFE) {
    str += StringPrintf(", unsafe_for_rowid");
  }

  DBUG_EXECUTE_IF("subplan_tokens", {
    str += ", token=";
    str += GetForceSubplanToken(const_cast<AccessPath *>(&path),
                                graph.query_block()->join);
  });

  if (strcmp(description_for_trace, "") == 0) {
    return str + "}";
  } else {
    return str + "} [" + description_for_trace + "]";
  }
}

/**
  Used by optimizer trace to print join order of join paths.
  Appends into 'join_order' a string that looks something like '(t1,(t2,t3))'
  where t1 is an alias of any kind of table including materialized table, and
  t1 is joined with (t2,t3) where (t2,t3) is another join.
 */
void PrintJoinOrder(const AccessPath *path, string *join_order) {
  assert(path != nullptr);

  auto func = [join_order](const AccessPath *subpath, const JOIN *) {
    // If it's a table, append its name.
    if (const TABLE *table = GetBasicTable(subpath); table != nullptr) {
      *join_order += table->alias;
      return true;
    }

    AccessPath *outer, *inner;
    switch (subpath->type) {
      case AccessPath::NESTED_LOOP_JOIN:
        outer = subpath->nested_loop_join().outer;
        inner = subpath->nested_loop_join().inner;
        break;
      case AccessPath::HASH_JOIN:
        outer = subpath->hash_join().outer;
        inner = subpath->hash_join().inner;
        break;
      case AccessPath::BKA_JOIN:
        outer = subpath->bka_join().outer;
        inner = subpath->bka_join().inner;
        break;
      case AccessPath::NESTED_LOOP_SEMIJOIN_WITH_DUPLICATE_REMOVAL:
        outer = subpath->nested_loop_semijoin_with_duplicate_removal().outer;
        inner = subpath->nested_loop_semijoin_with_duplicate_removal().inner;
        break;
      default:
        return false;  // Allow walker to continue.
    }

    // If we are here, we found a join path.
    join_order->push_back('(');
    PrintJoinOrder(outer, join_order);
    join_order->push_back(',');
    PrintJoinOrder(inner, join_order);
    join_order->push_back(')');

    return true;
  };

  // Fetch tables or joins at inner levels.
  WalkAccessPaths(path, /*join=*/nullptr,
                  WalkAccessPathPolicy::STOP_AT_MATERIALIZATION, func);
  return;
}

/// Commit OverflowBitsets in path (but not its children) to
/// stable storage (see m_overflow_bitset_mem_root).
void CostingReceiver::CommitBitsetsToHeap(AccessPath *path) const {
  if (path->filter_predicates.IsContainedIn(&m_overflow_bitset_mem_root)) {
    path->filter_predicates = path->filter_predicates.Clone(m_thd->mem_root);
  }
  if (path->delayed_predicates.IsContainedIn(&m_overflow_bitset_mem_root)) {
    path->delayed_predicates = path->delayed_predicates.Clone(m_thd->mem_root);
  }
}

/// Check if all bitsets under “path” are committed to stable storage
/// (see m_overflow_bitset_mem_root). Only relevant in debug mode,
/// as it is expensive.
[[maybe_unused]] bool CostingReceiver::BitsetsAreCommitted(
    AccessPath *path) const {
  // Verify that there are no uncommitted bitsets forgotten in children.
  bool all_ok = true;
  WalkAccessPaths(path, /*join=*/nullptr,
                  WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
                  [this, &all_ok](const AccessPath *subpath, const JOIN *) {
                    all_ok &= !subpath->filter_predicates.IsContainedIn(
                        &m_overflow_bitset_mem_root);
                    all_ok &= !subpath->delayed_predicates.IsContainedIn(
                        &m_overflow_bitset_mem_root);
                    return false;
                  });
  return all_ok;
}

/**
  Propose the given access path as an alternative to the existing access paths
  for the same task (assuming any exist at all), and hold a “tournament” to find
  whether it is better than the others. Only the best alternatives are kept,
  as defined by CompareAccessPaths(); a given access path is kept only if
  it is not dominated by any other path in the group (ie., the Pareto frontier
  is computed). This means that the following are all possible outcomes of the
  tournament:

   - The path is discarded, without ever being inserted in the list
     (dominated by at least one existing entry).
   - The path is inserted as a new alternative in the list (dominates none
     but it also not dominated by any -- or the list was empty), leaving it with
     N+1 entries.
   - The path is inserted as a new alternative in the list, but replaces one
     or more entries (dominates them).
   - The path replaces all existing alternatives, and becomes the sole entry
     in the list.

  “description_for_trace” is a short description of the inserted path
  to distinguish it in optimizer trace, if active. For instance, one might
  write “hash join” when proposing a hash join access path. It may be
  the empty string.

  If the access path is discarded, returns nullptr. Otherwise returns
  a pointer to where it was inserted. (This is useful if you need to
  call CommitBitsetsToHeap() on any of its children, or otherwise do
  work only for access paths that were kept.)
 */
AccessPath *CostingReceiver::ProposeAccessPath(
    AccessPath *path, Prealloced_array<AccessPath *, 4> *existing_paths,
    OrderingSet obsolete_orderings, const char *description_for_trace) const {
  if (m_secondary_engine_cost_hook != nullptr) {
    // If an error was raised by a previous invocation of the hook, reject all
    // paths.
    if (m_thd->is_error()) {
      return nullptr;
    }

    if (m_secondary_engine_cost_hook(m_thd, *m_graph, path)) {
      // Rejected by the secondary engine.
      return nullptr;
    }
    assert(!m_thd->is_error());
    assert(path->init_cost <= path->cost);
    if (!IsEmpty(path->filter_predicates)) {
      assert(path->num_output_rows() <= path->num_output_rows_before_filter);
      assert(path->cost_before_filter <= path->cost);
    }
  }

  DBUG_EXECUTE_IF("subplan_tokens", {
    string token =
        "force_subplan_" + GetForceSubplanToken(path, m_query_block->join);
    DBUG_EXECUTE_IF(token.c_str(), path->forced_by_dbug = true;);
  });

  if (existing_paths->empty()) {
    if (m_trace != nullptr) {
      *m_trace += " - " +
                  PrintAccessPath(*path, *m_graph, description_for_trace) +
                  " is first alternative, keeping\n";
    }
    AccessPath *insert_position = new (m_thd->mem_root) AccessPath(*path);
    existing_paths->push_back(insert_position);
    CommitBitsetsToHeap(insert_position);
    return insert_position;
  }

  // Verify that all row counts are consistent (if someone cares, ie. we are
  // either asserting they are, or tracing, so that a user can see it); we can
  // only do this for unparameterized tables (even though most such
  // inconsistencies probably originate further down the tree), since tables
  // with different parameterizations can have different sargable predicates.
  // (If we really wanted to, we could probably fix that as well, though.)
  // These should never happen, up to numerical issues, but they currently do;
  // see bug #33550360.
  const bool has_known_row_count_inconsistency_bugs =
      m_graph->has_reordered_left_joins || has_clamped_multipart_eq_ref ||
      has_semijoin_with_possibly_clamped_child;
  bool verify_consistency = (m_trace != nullptr);
#ifndef NDEBUG
  if (!has_known_row_count_inconsistency_bugs) {
    // Assert that we are consistent, even if we are not tracing.
    verify_consistency = true;
  }
#endif
  if (verify_consistency && path->parameter_tables == 0 &&
      path->num_output_rows() >= 1e-3) {
    for (const AccessPath *other_path : *existing_paths) {
      if (other_path->parameter_tables == 0 &&
          (other_path->num_output_rows() < path->num_output_rows() * 0.99 ||
           other_path->num_output_rows() > path->num_output_rows() * 1.01)) {
        if (m_trace != nullptr) {
          *m_trace += " - WARNING: " + PrintAccessPath(*path, *m_graph, "") +
                      " has inconsistent row counts with " +
                      PrintAccessPath(*other_path, *m_graph, "") + ".";
          if (has_known_row_count_inconsistency_bugs) {
            *m_trace += "\n   This is a bug, but probably a known one.\n";
          } else {
            *m_trace += " This is a bug.\n";
          }
        }
        if (!has_known_row_count_inconsistency_bugs) {
          assert(false);
        }
        break;
      }
    }
  }

  AccessPath *insert_position = nullptr;
  int num_dominated = 0;
  for (size_t i = 0; i < existing_paths->size(); ++i) {
    PathComparisonResult result = CompareAccessPaths(
        *m_orderings, *path, *((*existing_paths)[i]), obsolete_orderings);
    if (result == PathComparisonResult::DIFFERENT_STRENGTHS) {
      continue;
    }
    if (result == PathComparisonResult::IDENTICAL ||
        result == PathComparisonResult::SECOND_DOMINATES) {
      if (m_trace != nullptr) {
        *m_trace += " - " +
                    PrintAccessPath(*path, *m_graph, description_for_trace) +
                    " is not better than existing path " +
                    PrintAccessPath(*(*existing_paths)[i], *m_graph, "") +
                    ", discarding\n";
      }
      return nullptr;
    }
    if (result == PathComparisonResult::FIRST_DOMINATES) {
      ++num_dominated;
      if (insert_position == nullptr) {
        // Replace this path by the new, better one. We continue to search for
        // other paths to dominate. Note that we don't overwrite just yet,
        // because we might want to print out the old one in optimizer trace
        // below.
        insert_position = (*existing_paths)[i];
      } else {
        // The new path is better than the old one, but we don't need to insert
        // it again. Delete the old one by moving the last one into its place
        // (this may be a no-op) and then chopping one off the end.
        (*existing_paths)[i] = existing_paths->back();
        existing_paths->pop_back();
        --i;
      }
    }
  }

  if (insert_position == nullptr) {
    if (m_trace != nullptr) {
      *m_trace += " - " +
                  PrintAccessPath(*path, *m_graph, description_for_trace) +
                  " is potential alternative, keeping\n";
    }
    insert_position = new (m_thd->mem_root) AccessPath(*path);
    existing_paths->emplace_back(insert_position);
    CommitBitsetsToHeap(insert_position);
    return insert_position;
  }

  if (m_trace != nullptr) {
    if (existing_paths->size() == 1) {  // Only one left.
      if (num_dominated == 1) {
        *m_trace +=
            " - " + PrintAccessPath(*path, *m_graph, description_for_trace) +
            " is better than previous " +
            PrintAccessPath(*insert_position, *m_graph, "") + ", replacing\n";
      } else {
        *m_trace +=
            " - " + PrintAccessPath(*path, *m_graph, description_for_trace) +
            " is better than all previous alternatives, replacing all\n";
      }
    } else {
      assert(num_dominated > 0);
      *m_trace += StringPrintf(
          " - %s is better than %d others, replacing them\n",
          PrintAccessPath(*path, *m_graph, description_for_trace).c_str(),
          num_dominated);
    }
  }
  *insert_position = *path;
  CommitBitsetsToHeap(insert_position);
  return insert_position;
}

AccessPath MakeSortPathWithoutFilesort(THD *thd, AccessPath *child,
                                       ORDER *order, int ordering_state,
                                       int num_where_predicates) {
  assert(order != nullptr);
  AccessPath sort_path;
  sort_path.type = AccessPath::SORT;
  sort_path.ordering_state = ordering_state;
  if (!child->applied_sargable_join_predicates()
           .empty()) {  // Will be empty after grouping.
    MutableOverflowBitset applied_sargable_join_predicates =
        child->applied_sargable_join_predicates().Clone(thd->mem_root);
    applied_sargable_join_predicates.ClearBits(0, num_where_predicates);
    sort_path.applied_sargable_join_predicates() =
        std::move(applied_sargable_join_predicates);
  }
  sort_path.delayed_predicates = child->delayed_predicates;
  sort_path.count_examined_rows = false;
  sort_path.sort().child = child;
  sort_path.sort().filesort = nullptr;
  sort_path.sort().tables_to_get_rowid_for = 0;
  sort_path.sort().order = order;
  sort_path.sort().remove_duplicates = false;
  sort_path.sort().unwrap_rollup = true;
  sort_path.sort().limit = HA_POS_ERROR;
  sort_path.sort().force_sort_rowids = false;
  EstimateSortCost(&sort_path);
  return sort_path;
}

void CostingReceiver::ProposeAccessPathWithOrderings(
    NodeMap nodes, FunctionalDependencySet fd_set,
    OrderingSet obsolete_orderings, AccessPath *path,
    const char *description_for_trace) {
  AccessPathSet *path_set;
  // Insert an empty array if none exists.
  {
    const auto [it, inserted] = m_access_paths.emplace(
        nodes,
        AccessPathSet{Prealloced_array<AccessPath *, 4>{PSI_NOT_INSTRUMENTED},
                      fd_set, obsolete_orderings});
    path_set = &it->second;
    if (!inserted) {
      assert(fd_set == path_set->active_functional_dependencies);
      assert(obsolete_orderings == path_set->obsolete_orderings);
    }
  }

  if (path_set->always_empty) {
    // This subtree is already optimized away. Don't propose any alternative
    // plans, since we've already found the optimal one.
    return;
  }

  if (path->type == AccessPath::ZERO_ROWS) {
    // Clear the other candidates seen for this set of nodes, so that we prefer
    // a simple ZERO_ROWS path, even in the case where we have for example a
    // candidate NESTED_LOOP_JOIN path with zero cost.
    path_set->paths.clear();
    // Mark the subtree as optimized away.
    path_set->always_empty = true;
  }

  ProposeAccessPath(path, &path_set->paths, obsolete_orderings,
                    description_for_trace);

  // Don't bother trying sort-ahead if we are done joining;
  // there's no longer anything to be ahead of, so the regular
  // sort operations will take care of it.
  if (nodes == TablesBetween(0, m_graph->nodes.size())) {
    return;
  }

  if (!SupportedEngineFlag(SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN) &&
      SupportedEngineFlag(SecondaryEngineFlag::AGGREGATION_IS_UNORDERED)) {
    // If sortahead cannot propagate through joins to ORDER BY,
    // and also cannot propagate from anything to aggregation or
    // from aggregation to ORDER BY, it is pointless, so don't try.
    // Note that this also removes rewrite to semijoin via duplicate
    // removal, but that's fine, as it is rarely useful without having
    // nested loops against an index on the outer side.
    return;
  }

  // Don't try to sort-ahead parameterized paths; see the comment in
  // CompareAccessPaths for why.
  if (path->parameter_tables != 0) {
    return;
  }

  path = GetSafePathToSort(m_thd, m_query_block->join, path, m_need_rowid);

  // Try sort-ahead for all interesting orderings.
  // (For the final sort, this might not be so much _ahead_, but still
  // potentially useful, if there are multiple orderings where one is a
  // superset of the other.)
  bool path_is_on_heap = false;
  for (const SortAheadOrdering &sort_ahead_ordering : *m_sort_ahead_orderings) {
    if (!IsSubset(sort_ahead_ordering.required_nodes, nodes)) {
      continue;
    }
    if (sort_ahead_ordering.aggregates_required) {
      // For sort-ahead, we don't have any aggregates yet
      // (since we never group-ahead).
      continue;
    }

    LogicalOrderings::StateIndex new_state = m_orderings->ApplyFDs(
        m_orderings->SetOrder(sort_ahead_ordering.ordering_idx), fd_set);
    if (!m_orderings->MoreOrderedThan(new_state, path->ordering_state,
                                      obsolete_orderings)) {
      continue;
    }

    AccessPath sort_path =
        MakeSortPathWithoutFilesort(m_thd, path, sort_ahead_ordering.order,
                                    new_state, m_graph->num_where_predicates);

    char buf[256];
    if (m_trace != nullptr) {
      if (description_for_trace[0] == '\0') {
        snprintf(buf, sizeof(buf), "sort(%d)",
                 sort_ahead_ordering.ordering_idx);
      } else {
        snprintf(buf, sizeof(buf), "%s, sort(%d)", description_for_trace,
                 sort_ahead_ordering.ordering_idx);
      }
    }
    AccessPath *insert_position = ProposeAccessPath(
        &sort_path, &path_set->paths, obsolete_orderings, buf);
    if (insert_position != nullptr && !path_is_on_heap) {
      path = new (m_thd->mem_root) AccessPath(*path);
      CommitBitsetsToHeap(path);
      insert_position->sort().child = path;
      assert(BitsetsAreCommitted(insert_position));
      path_is_on_heap = true;
    }
  }
}

bool CheckSupportedQuery(THD *thd) {
  if (thd->lex->m_sql_cmd != nullptr &&
      thd->lex->m_sql_cmd->using_secondary_storage_engine() &&
      !Overlaps(EngineFlags(thd),
                MakeSecondaryEngineFlags(
                    SecondaryEngineFlag::SUPPORTS_HASH_JOIN,
                    SecondaryEngineFlag::SUPPORTS_NESTED_LOOP_JOIN))) {
    my_error(ER_HYPERGRAPH_NOT_SUPPORTED_YET, MYF(0),
             "the secondary engine in use");
    return true;
  }
  return false;
}

/**
  Set up an access path for streaming or materializing through a temporary
  table. If none is needed (because earlier iterators already materialize
  what needs to be done), returns the path itself.

  The actual temporary table will be created and filled out during finalization.
 */
AccessPath *CreateMaterializationOrStreamingPath(THD *thd, JOIN *join,
                                                 AccessPath *path,
                                                 bool need_rowid,
                                                 bool copy_items) {
  if (!IteratorsAreNeeded(thd, path)) {
    // Let external executors decide for themselves whether they need an
    // intermediate materialization or streaming step. Don't add it to the plan
    // for them.
    return path;
  }

  // See if later sorts will need row IDs from us or not.
  if (!need_rowid) {
    // The common case; we can use streaming.
    if (!copy_items) {
      // StreamingIterator exists only to copy items, so we don't need an
      // iterator here at all.
      return path;
    }
    AccessPath *stream_path = NewStreamingAccessPath(
        thd, path, join, /*temp_table_param=*/nullptr, /*table=*/nullptr,
        /*ref_slice=*/-1);
    EstimateStreamCost(stream_path);
    return stream_path;
  } else {
    // Filesort needs sort by row ID, possibly because large blobs are
    // involved, so we need to actually materialize. (If we wanted a
    // smaller temporary table at the expense of more seeks, we could
    // materialize only aggregate functions and do a multi-table sort
    // by docid, but this situation is rare, so we go for simplicity.)
    return CreateMaterializationPath(thd, join, path, /*temp_table=*/nullptr,
                                     /*temp_table_param=*/nullptr, copy_items);
  }
}

AccessPath *GetSafePathToSort(THD *thd, JOIN *join, AccessPath *path,
                              bool need_rowid) {
  if (need_rowid && path->safe_for_rowid == AccessPath::UNSAFE) {
    // We need to materialize this path before we can sort it,
    // since it might not give us stable row IDs.
    return CreateMaterializationOrStreamingPath(
        thd, join, new (thd->mem_root) AccessPath(*path), need_rowid,
        /*copy_items=*/true);
  } else {
    return path;
  }
}

/**
  Sets up an access path for materializing the results returned from a path in a
  temporary table.
 */
AccessPath *CreateMaterializationPath(THD *thd, JOIN *join, AccessPath *path,
                                      TABLE *temp_table,
                                      Temp_table_param *temp_table_param,
                                      bool copy_items) {
  AccessPath *table_path =
      NewTableScanAccessPath(thd, temp_table, /*count_examined_rows=*/false);
  AccessPath *materialize_path = NewMaterializeAccessPath(
      thd,
      SingleMaterializeQueryBlock(thd, path, /*select_number=*/-1, join,
                                  copy_items, temp_table_param),
      /*invalidators=*/nullptr, temp_table, table_path, /*cte=*/nullptr,
      /*unit=*/nullptr, /*ref_slice=*/-1, /*rematerialize=*/true,
      /*limit_rows=*/HA_POS_ERROR, /*reject_multiple_rows=*/false);

  EstimateMaterializeCost(thd, materialize_path);
  materialize_path->ordering_state = path->ordering_state;
  materialize_path->delayed_predicates = path->delayed_predicates;
  return materialize_path;
}

bool IsMaterializationPath(const AccessPath *path) {
  switch (path->type) {
    case AccessPath::MATERIALIZE:
    case AccessPath::MATERIALIZED_TABLE_FUNCTION:
    case AccessPath::MATERIALIZE_INFORMATION_SCHEMA_TABLE:
      return true;
    default:
      return false;
  }
}

/**
  Is this DELETE target table a candidate for being deleted from immediately,
  while scanning the result of the join? It only checks if it is a candidate for
  immediate delete. Whether it actually ends up being deleted from immediately,
  depends on the plan that is chosen.
 */
bool IsImmediateDeleteCandidate(const Table_ref *table_ref,
                                const Query_block *query_block) {
  assert(table_ref->is_deleted());

  // Cannot delete from the table immediately if it's joined with itself.
  if (unique_table(table_ref, query_block->leaf_tables,
                   /*check_alias=*/false) != nullptr) {
    return false;
  }

  return true;
}

/// Adds all fields of "table" that are referenced from "item" to
/// table->tmp_set.
void AddFieldsToTmpSet(Item *item, TABLE *table) {
  item->walk(&Item::add_field_to_set_processor, enum_walk::SUBQUERY_POSTFIX,
             pointer_cast<uchar *>(table));
}

/**
  Is this UPDATE target table a candidate for being updated immediately, while
  scanning the result of the join? It only checks if it is a candidate for
  immediate update. Whether it actually ends up being updated immediately,
  depends on the plan that is chosen.
 */
bool IsImmediateUpdateCandidate(const Table_ref *table_ref, int node_idx,
                                const JoinHypergraph &graph,
                                table_map target_tables) {
  assert(table_ref->is_updated());
  assert(Overlaps(table_ref->map(), target_tables));
  assert(table_ref->table == graph.nodes[node_idx].table);

  // Cannot update the table immediately if it's joined with itself.
  if (unique_table(table_ref, graph.query_block()->leaf_tables,
                   /*check_alias=*/false) != nullptr) {
    return false;
  }

  TABLE *const table = table_ref->table;

  // Cannot update the table immediately if it modifies a partitioning column,
  // as that could move the row to another partition so that it is seen more
  // than once.
  if (table->part_info != nullptr &&
      table->part_info->num_partitions_used() > 1 &&
      partition_key_modified(table, table->write_set)) {
    return false;
  }

  // If there are at least two tables to update, t1 and t2, t1 being before t2
  // in the plan, we need to collect all fields of t1 which influence the
  // selection of rows from t2. If those fields are also updated, it will not be
  // possible to update t1 on the fly.
  if (!IsSingleBitSet(target_tables)) {
    assert(bitmap_is_clear_all(&table->tmp_set));
    auto restore_tmp_set =
        create_scope_guard([table]() { bitmap_clear_all(&table->tmp_set); });

    // Mark referenced fields in the join conditions in all the simple edges
    // involving this table.
    for (unsigned edge_idx : graph.graph.nodes[node_idx].simple_edges) {
      const RelationalExpression *expr = graph.edges[edge_idx / 2].expr;
      for (Item *condition : expr->join_conditions) {
        AddFieldsToTmpSet(condition, table);
      }
      for (Item_eq_base *condition : expr->equijoin_conditions) {
        AddFieldsToTmpSet(condition, table);
      }
    }

    // Mark referenced fields in the join conditions in all the complex edges
    // involving this table.
    for (unsigned edge_idx : graph.graph.nodes[node_idx].complex_edges) {
      const RelationalExpression *expr = graph.edges[edge_idx / 2].expr;
      for (Item *condition : expr->join_conditions) {
        AddFieldsToTmpSet(condition, table);
      }
      for (Item_eq_base *condition : expr->equijoin_conditions) {
        AddFieldsToTmpSet(condition, table);
      }
    }

    // And mark referenced fields in join conditions that are left in the WHERE
    // clause (typically degenerate join conditions stemming from single-table
    // filters that can't be pushed down due to pseudo-table bits in
    // used_tables()).
    for (unsigned i = 0; i < graph.num_where_predicates; ++i) {
      const Predicate &predicate = graph.predicates[i];
      if (IsProperSubset(TableBitmap(node_idx), predicate.used_nodes)) {
        AddFieldsToTmpSet(predicate.condition, table);
      }
    }

    if (bitmap_is_overlapping(&table->tmp_set, table->write_set)) {
      return false;
    }
  }

  return true;
}

/**
  Finds all the target tables of an UPDATE or DELETE statement. It additionally
  disables covering index scans on the target tables, since ha_update_row() and
  ha_delete_row() can only be called on scans reading the full row.
 */
table_map FindUpdateDeleteTargetTables(const Query_block *query_block) {
  table_map target_tables = 0;
  for (Table_ref *tl = query_block->leaf_tables; tl != nullptr;
       tl = tl->next_leaf) {
    if (tl->is_updated() || tl->is_deleted()) {
      target_tables |= tl->map();
      // Target tables of DELETE and UPDATE need the full row, so disable
      // covering index scans.
      tl->table->no_keyread = true;
      tl->table->covering_keys.clear_all();
    }
  }
  assert(target_tables != 0);
  return target_tables;
}

/**
  Finds all of the target tables of an UPDATE or DELETE statement that are
  candidates from being updated or deleted from immediately while scanning the
  results of the join, without need to buffer the row IDs in a temporary table
  for delayed update/delete after the join has completed. These are candidates
  only; the actual tables to update while scanning, if any, will be chosen based
  on cost during planning.
 */
table_map FindImmediateUpdateDeleteCandidates(const JoinHypergraph &graph,
                                              table_map target_tables,
                                              bool is_delete) {
  table_map candidates = 0;
  for (unsigned node_idx = 0; node_idx < graph.nodes.size(); ++node_idx) {
    const JoinHypergraph::Node &node = graph.nodes[node_idx];
    const Table_ref *tl = node.table->pos_in_table_list;
    if (Overlaps(tl->map(), target_tables)) {
      if (is_delete ? IsImmediateDeleteCandidate(tl, graph.query_block())
                    : IsImmediateUpdateCandidate(tl, node_idx, graph,
                                                 target_tables)) {
        candidates |= tl->map();
      }
    }
  }
  return candidates;
}

// Returns a map containing the node indexes of all tables referenced by a
// full-text MATCH function.
NodeMap FindFullTextSearchedTables(const JoinHypergraph &graph) {
  NodeMap tables = 0;
  for (size_t i = 0; i < graph.nodes.size(); ++i) {
    if (graph.nodes[i].table->pos_in_table_list->is_fulltext_searched()) {
      tables |= TableBitmap(i);
    }
  }
  return tables;
}

// Checks if an item represents a full-text predicate which can be satisfied by
// a full-text index scan. This can be done if the predicate is on one of the
// following forms:
//
//    MATCH(col) AGAINST ('search string')
//    MATCH(col) AGAINST ('search string') > const, where const >= 0
//    MATCH(col) AGAINST ('search string') >= const, where const > 0
//    const < MATCH(col) AGAINST ('search string'), where const >= 0
//    const <= MATCH(col) AGAINST ('search string'), where const > 0
//
// That is, the predicate must return FALSE if MATCH returns zero. The predicate
// cannot be pushed to an index scan if it returns TRUE when MATCH returns zero,
// because a full-text index scan only returns documents with a positive score.
//
// If the item is sargable, the function returns true.
bool IsSargableFullTextIndexPredicate(Item *condition) {
  if (condition->type() != Item::FUNC_ITEM) {
    return false;
  }

  Item_func *func = down_cast<Item_func *>(condition);
  int const_arg_idx = -1;
  bool is_greater_than_op;
  switch (func->functype()) {
    case Item_func::MATCH_FUNC:
      // A standalone MATCH in WHERE is pushable to a full-text index.
      return true;
    case Item_func::GT_FUNC:
      // MATCH > const is pushable to a full-text index if const >= 0. Checked
      // after the switch.
      const_arg_idx = 1;
      is_greater_than_op = true;
      break;
    case Item_func::GE_FUNC:
      // MATCH >= const is pushable to a full-text index if const > 0. Checked
      // after the switch.
      const_arg_idx = 1;
      is_greater_than_op = false;
      break;
    case Item_func::LT_FUNC:
      // Normalize const < MATCH to MATCH > const.
      const_arg_idx = 0;
      is_greater_than_op = true;
      break;
    case Item_func::LE_FUNC:
      // Normalize const <= MATCH to MATCH >= const.
      const_arg_idx = 0;
      is_greater_than_op = false;
      break;
    default:
      // Other kinds of predicates are not pushable to a full-text index.
      return false;
  }

  assert(func->argument_count() == 2);
  assert(const_arg_idx == 0 || const_arg_idx == 1);

  // Only pushable if we have a MATCH function greater-than(-or-equal) a
  // constant value.
  Item *const_arg = func->get_arg(const_arg_idx);
  Item *match_arg = func->get_arg(1 - const_arg_idx);
  if (!is_function_of_type(match_arg, Item_func::FT_FUNC) ||
      !const_arg->const_item()) {
    return false;
  }

  // Evaluate the constant.
  const double value = const_arg->val_real();
  if (const_arg->null_value) {
    // MATCH <op> NULL cannot be pushed to a full-text index.
    return false;
  }

  // Check if the constant is high enough to exclude MATCH = 0, which is the1
  // requirement for being pushable to a full-text index.
  if (is_greater_than_op) {
    return value >= 0;
  } else {
    return value > 0;
  }
}

// Finds all the WHERE predicates that can be satisfied by a full-text index
// scan, and returns a bitmap of those predicates. See
// IsSargableFullTextIndexPredicate() for a description of which predicates are
// sargable.
uint64_t FindSargableFullTextPredicates(const JoinHypergraph &graph) {
  uint64_t fulltext_predicates = 0;
  for (size_t i = 0; i < graph.num_where_predicates; ++i) {
    const Predicate &predicate = graph.predicates[i];
    if (IsSargableFullTextIndexPredicate(predicate.condition)) {
      fulltext_predicates |= uint64_t{1} << i;

      // If the predicate is a standalone MATCH function, flag it as such. This
      // is used by Item_func_match::can_skip_ranking() to determine if ranking
      // is needed. (We could also have set other operation hints here, like
      // FT_OP_GT and FT_OP_GE. These hints are currently not used by any of the
      // storage engines, so we don't set them for now.)
      Item_func *predicate_func = down_cast<Item_func *>(predicate.condition);
      if (predicate_func->functype() == Item_func::MATCH_FUNC) {
        Item_func_match *parent =
            down_cast<Item_func_match *>(predicate_func->get_arg(0))
                ->get_master();
        List<Item_func_match> *funcs =
            parent->table_ref->query_block->ftfunc_list;
        // We only set the hint if this is the only reference to the MATCH
        // function. If it is used other places (for example in the SELECT list
        // or in other predicates) we may still need ranking.
        if (std::none_of(funcs->begin(), funcs->end(),
                         [parent](const Item_func_match &match) {
                           return match.master == parent;
                         })) {
          parent->set_hints_op(FT_OP_NO, 0.0);
        }
      }
    }
  }
  return fulltext_predicates;
}

// Inject casts into comparisons of expressions with incompatible types.
// For example, int_col = string_col is rewritten to
// CAST(int_col AS DOUBLE) = CAST(string_col AS DOUBLE)
bool InjectCastNodes(JoinHypergraph *graph) {
  // Inject cast nodes into the WHERE clause.
  for (Predicate &predicate :
       make_array(graph->predicates.data(), graph->num_where_predicates)) {
    if (predicate.condition->walk(&Item::cast_incompatible_args,
                                  enum_walk::POSTFIX, nullptr)) {
      return true;
    }
  }

  // Inject cast nodes into the join conditions.
  for (JoinPredicate &edge : graph->edges) {
    RelationalExpression *expr = edge.expr;
    if (expr->join_predicate_first != expr->join_predicate_last) {
      // The join predicates have been lifted to the WHERE clause, and casts are
      // already injected into the WHERE clause.
      continue;
    }
    for (Item_eq_base *item : expr->equijoin_conditions) {
      if (item->walk(&Item::cast_incompatible_args, enum_walk::POSTFIX,
                     nullptr)) {
        return true;
      }
    }
    for (Item *item : expr->join_conditions) {
      if (item->walk(&Item::cast_incompatible_args, enum_walk::POSTFIX,
                     nullptr)) {
        return true;
      }
    }
  }

  // Inject cast nodes to the expressions in the SELECT list.
  const JOIN *join = graph->join();
  for (Item *item : *join->fields) {
    if (item->walk(&Item::cast_incompatible_args, enum_walk::POSTFIX,
                   nullptr)) {
      return true;
    }
  }

  // Also GROUP BY expressions and HAVING, to be consistent everywhere.
  for (ORDER *ord = join->group_list.order; ord != nullptr; ord = ord->next) {
    if ((*ord->item)
            ->walk(&Item::cast_incompatible_args, enum_walk::POSTFIX,
                   nullptr)) {
      return true;
    }
  }
  if (join->having_cond != nullptr) {
    if (join->having_cond->walk(&Item::cast_incompatible_args,
                                enum_walk::POSTFIX, nullptr)) {
      return true;
    }
  }

  return false;
}

// Checks if any of the full-text indexes are covering for a table. If the query
// only needs the document ID and the rank, there is no need to access table
// rows. Index-only access can only be used if there is an FTS_DOC_ID column in
// the table, and no other columns must be accessed. All covering full-text
// indexes that are found, are added to TABLE::covering_keys.
void EnableFullTextCoveringIndexes(const Query_block *query_block) {
  for (Item_func_match &match : *query_block->ftfunc_list) {
    TABLE *table = match.table_ref->table;
    if (match.master == nullptr && match.key != NO_SUCH_KEY &&
        table->fts_doc_id_field != nullptr &&
        bitmap_is_set(table->read_set,
                      table->fts_doc_id_field->field_index()) &&
        bitmap_bits_set(table->read_set) == 1) {
      table->covering_keys.set_bit(match.key);
    }
  }
}

/**
  Creates a ZERO_ROWS access path for an always empty join result, or a
  ZERO_ROWS_AGGREGATED in case of an implicitly grouped query. The zero rows
  path is wrapped in FILTER (for HAVING) or LIMIT_OFFSET paths as needed, as
  well as UPDATE_ROWS/DELETE_ROWS paths for UPDATE/DELETE statements.
 */
AccessPath *CreateZeroRowsForEmptyJoin(JOIN *join, const char *cause) {
  join->zero_result_cause = cause;
  join->needs_finalize = true;
  join->create_access_paths_for_zero_rows();
  return join->root_access_path();
}

/**
  Creates an AGGREGATE AccessPath, possibly with an intermediary STREAM node if
  one is needed. The creation of the temporary table does not happen here, but
  is left for FinalizePlanForQueryBlock().

  @param thd The current thread.
  @param join The join to which 'path' belongs.
  @param rollup True for "GROUP BY ... WITH ROLLUP".
  @param row_estimate estimated number of output rows, so that we do not
         need to recalculate it, or kUnknownRowCount if unknown.
  @param trace Optimizer trace.
  @returns The AGGREGATE AccessPath.
 */
AccessPath CreateStreamingAggregationPath(THD *thd, AccessPath *path,
                                          JOIN *join, bool rollup,
                                          double row_estimate, string *trace) {
  AccessPath *child_path = path;
  const Query_block *query_block = join->query_block;

  // Create a streaming node, if one is needed. It is needed for aggregation of
  // some full-text queries, because AggregateIterator doesn't preserve the
  // position of the underlying scans.
  if (join->contains_non_aggregated_fts()) {
    child_path = NewStreamingAccessPath(
        thd, path, join, /*temp_table_param=*/nullptr, /*table=*/nullptr,
        /*ref_slice=*/-1);
    CopyBasicProperties(*path, child_path);
  }

  AccessPath aggregate_path;
  aggregate_path.type = AccessPath::AGGREGATE;
  aggregate_path.aggregate().child = child_path;
  aggregate_path.aggregate().rollup = rollup;
  aggregate_path.set_num_output_rows(row_estimate);
  EstimateAggregateCost(&aggregate_path, query_block, trace);
  return aggregate_path;
}

// If we are planned using in2exists, and our SELECT list has a window
// function, the HAVING condition may include parts that refer to window
// functions. (This cannot happen in standard SQL, but we add such conditions
// as part of in2exists processing.) Split them here.
void SplitHavingCondition(THD *thd, Item *cond, Item **having_cond,
                          Item **having_cond_wf) {
  if (cond == nullptr || !cond->has_wf()) {
    *having_cond = cond;
    *having_cond_wf = nullptr;
    return;
  }

  // If we have a IN-to-EXISTS with window functions and multiple columns,
  // we cannot safely push even the ones that are not dependent on the
  // window functions, as some of them would come before the window functions
  // and change their input data incorrectly. So if so, we need to delay
  // all of them.
  const bool delay_all_in2exists = cond->has_wf();

  Mem_root_array<Item *> cond_parts(thd->mem_root);
  ExtractConditions(cond, &cond_parts);

  List<Item> cond_parts_wf;
  List<Item> cond_parts_normal;
  for (Item *item : cond_parts) {
    if (item->has_wf() ||
        (delay_all_in2exists && item->created_by_in2exists())) {
      cond_parts_wf.push_back(item);
    } else {
      cond_parts_normal.push_back(item);
    }
  }
  *having_cond = CreateConjunction(&cond_parts_normal);
  *having_cond_wf = CreateConjunction(&cond_parts_wf);
}

void ApplyHavingCondition(THD *thd, Item *having_cond, Query_block *query_block,
                          const char *description_for_trace, string *trace,
                          Prealloced_array<AccessPath *, 4> *root_candidates,
                          CostingReceiver *receiver) {
  if (having_cond == nullptr) {
    return;
  }

  if (trace != nullptr) {
    *trace += description_for_trace;
  }

  Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
  for (AccessPath *root_path : *root_candidates) {
    AccessPath filter_path;
    filter_path.type = AccessPath::FILTER;
    filter_path.filter().child = root_path;
    filter_path.filter().condition = having_cond;
    // We don't currently bother with materializing subqueries
    // in HAVING, as they should be rare.
    filter_path.filter().materialize_subqueries = false;
    filter_path.set_num_output_rows(
        root_path->num_output_rows() *
        EstimateSelectivity(thd, having_cond, trace));

    const FilterCost filter_cost = EstimateFilterCost(
        thd, root_path->num_output_rows(), having_cond, query_block);

    filter_path.init_cost =
        root_path->init_cost + filter_cost.init_cost_if_not_materialized;

    filter_path.init_once_cost = root_path->init_once_cost;
    filter_path.cost = root_path->cost + filter_cost.cost_if_not_materialized;
    filter_path.num_output_rows_before_filter = filter_path.num_output_rows();
    filter_path.cost_before_filter = filter_path.cost;
    // TODO(sgunders): Collect and apply functional dependencies from
    // HAVING conditions.
    filter_path.ordering_state = root_path->ordering_state;
    receiver->ProposeAccessPath(&filter_path, &new_root_candidates,
                                /*obsolete_orderings=*/0, "");
  }
  *root_candidates = std::move(new_root_candidates);
}

AccessPath MakeSortPathForDistinct(
    THD *thd, AccessPath *root_path, int ordering_idx,
    bool aggregation_is_unordered, const LogicalOrderings &orderings,
    LogicalOrderings::StateIndex ordering_state) {
  AccessPath sort_path;
  sort_path.type = AccessPath::SORT;
  sort_path.count_examined_rows = false;
  sort_path.sort().child = root_path;
  sort_path.sort().filesort = nullptr;
  sort_path.sort().remove_duplicates = true;
  sort_path.sort().unwrap_rollup = false;
  sort_path.sort().limit = HA_POS_ERROR;
  sort_path.sort().force_sort_rowids = false;

  if (aggregation_is_unordered) {
    // Even though we create a sort node for the distinct operation,
    // the engine does not actually sort the rows. (The deduplication
    // flag is the hint in this case.)
    sort_path.ordering_state = 0;
  } else {
    sort_path.ordering_state = ordering_state;
  }

  // This sort is potentially after materialization, so we must make a
  // copy of the ordering so that ReplaceOrderItemsWithTempTableFields()
  // doesn't accidentally rewrite the items in a sort on the same
  // sort-ahead ordering before the materialization.
  ORDER *order_copy =
      BuildSortAheadOrdering(thd, &orderings, orderings.ordering(ordering_idx));
  sort_path.sort().order = order_copy;

  EstimateSortCost(&sort_path);
  return sort_path;
}

JoinHypergraph::Node *FindNodeWithTable(JoinHypergraph *graph, TABLE *table) {
  for (JoinHypergraph::Node &node : graph->nodes) {
    if (node.table == table) {
      return &node;
    }
  }
  return nullptr;
}

Prealloced_array<AccessPath *, 4> ApplyDistinctAndOrder(
    THD *thd, const CostingReceiver &receiver,
    const LogicalOrderings &orderings, bool aggregation_is_unordered,
    int order_by_ordering_idx, int distinct_ordering_idx,
    const Mem_root_array<SortAheadOrdering> &sort_ahead_orderings,
    FunctionalDependencySet fd_set, Query_block *query_block, bool need_rowid,
    bool force_sort_rowids, Prealloced_array<AccessPath *, 4> root_candidates,
    string *trace) {
  JOIN *join = query_block->join;
  assert(join->select_distinct || join->order.order != nullptr);

  if (root_candidates.empty()) {
    // Nothing to do if the secondary engine has rejected all candidates.
    assert(receiver.HasSecondaryEngineCostHook());
    return root_candidates;
  }

  // If we have both ORDER BY and GROUP BY, we need a materialization step
  // after the grouping (if windowing hasn't already given us one) -- although
  // in most cases, we only need to materialize one row at a time (streaming),
  // so the performance loss should be very slight. This is because when
  // filesort only really deals with fields, not values; when it is to “output”
  // a row, it puts back the contents of the sorted table's (or tables')
  // row buffer(s). For expressions that only depend on the current row, such as
  // (f1 + 1), this is fine, but aggregate functions (Item_sum) depend on
  // multiple rows, so we need a field where filesort can put back its value
  // (and of course, subsequent readers need to read from that field
  // instead of trying to evaluate the Item_sum). A temporary table provides
  // just that, so we create one based on the current field list;
  // StreamingIterator (or MaterializeIterator, if we actually need to
  // materialize) will evaluate all the Items in turn and put their values
  // into the temporary table's fields.
  //
  // For simplicity, we materialize all items in the SELECT list, even those
  // that are not aggregate functions. This is a tiny performance loss,
  // but makes things simpler.
  //
  // The test on join->sum_funcs is mainly to avoid having to create temporary
  // tables in unit tests; the rationale is that if there are no aggregate
  // functions, we also cannot sort on them, and thus, we don't get the
  // problem. Note that we can't do this if sorting by row IDs, as
  // AggregateIterator doesn't preserve them (doing so would probably not be
  // worth it for something that's fairly niche).
  //
  // NOTE: If we elide the sort due to interesting orderings, this might
  // be redundant. It is fairly harmless, though.
  if ((query_block->is_explicitly_grouped() &&
       (*join->sum_funcs != nullptr ||
        join->rollup_state != JOIN::RollupState::NONE || need_rowid)) &&
      join->m_windows.is_empty()) {
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      root_path =
          CreateMaterializationOrStreamingPath(thd, join, root_path, need_rowid,
                                               /*copy_items=*/true);
      receiver.ProposeAccessPath(root_path, &new_root_candidates,
                                 /*obsolete_orderings=*/0, "");
    }
    root_candidates = std::move(new_root_candidates);
  }

  // Now create iterators for DISTINCT, if applicable.
  if (join->select_distinct) {
    if (trace != nullptr) {
      *trace += "Applying sort for DISTINCT\n";
    }

    // Remove redundant elements from the grouping before it is applied.
    // Specifically, we want to remove elements that are constant after all
    // predicates have been applied.
    const Ordering grouping =
        ReduceFinalOrdering(thd, orderings, distinct_ordering_idx);

    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      if (grouping.GetElements().empty()) {
        // Only const fields.
        AccessPath *limit_path = NewLimitOffsetAccessPath(
            thd, root_path, /*limit=*/1, /*offset=*/0, join->calc_found_rows,
            /*reject_multiple_rows=*/false,
            /*send_records_override=*/nullptr);
        receiver.ProposeAccessPath(limit_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, "");
        continue;
      }
      if (!aggregation_is_unordered &&
          orderings.DoesFollowOrder(root_path->ordering_state,
                                    distinct_ordering_idx)) {
        // We don't need the sort, and can do with a simpler deduplication.
        // TODO(sgunders): In some cases, we could apply LIMIT 1,
        // which would be slightly more efficient; see e.g. the test for
        // bug #33148369.
        Item **group_items =
            thd->mem_root->ArrayAlloc<Item *>(grouping.GetElements().size());
        for (size_t i = 0; i < grouping.GetElements().size(); ++i) {
          group_items[i] = orderings.item(grouping.GetElements()[i].item);
        }
        AccessPath *dedup_path = NewRemoveDuplicatesAccessPath(
            thd, root_path, group_items, grouping.GetElements().size());
        CopyBasicProperties(*root_path, dedup_path);
        // TODO(sgunders): Model the actual reduction in rows somehow.
        dedup_path->cost += kAggregateOneRowCost * root_path->num_output_rows();
        receiver.ProposeAccessPath(dedup_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, "sort elided");
        continue;
      }

      root_path = GetSafePathToSort(thd, join, root_path, need_rowid);

      // We need to sort. Try all sort-ahead, not just the one directly
      // derived from DISTINCT clause, because the DISTINCT clause might
      // help us elide the sort for ORDER BY later, if the DISTINCT clause
      // is broader than the ORDER BY clause.
      for (const SortAheadOrdering &sort_ahead_ordering :
           sort_ahead_orderings) {
        if (sort_ahead_ordering.sort_ahead_only) {
          continue;
        }
        LogicalOrderings::StateIndex ordering_state = orderings.ApplyFDs(
            orderings.SetOrder(sort_ahead_ordering.ordering_idx), fd_set);
        // A broader DISTINCT could help elide ORDER BY. Not vice versa. Note
        // that ORDER BY would generally be subset of DISTINCT, but not always.
        // E.g. using ANY_VALUE() in ORDER BY would allow it to be not part of
        // DISTINCT.
        if (sort_ahead_ordering.ordering_idx == distinct_ordering_idx) {
          // The ordering derived from DISTINCT. Always propose this one,
          // regardless of whether it also satisfies the ORDER BY ordering.
        } else if (grouping.size() <
                   orderings.ordering(sort_ahead_ordering.ordering_idx)
                       .size()) {
          // This sort-ahead ordering is too wide and may cause duplicates to be
          // returned. Don't propose it.
          continue;
        } else if (order_by_ordering_idx == -1) {
          // There is no ORDER BY to satisfy later, so there is no point in
          // trying to find a sort that satisfies both DISTINCT and ORDER BY.
          continue;
        } else if (!orderings.DoesFollowOrder(ordering_state,
                                              distinct_ordering_idx) ||
                   !orderings.DoesFollowOrder(ordering_state,
                                              order_by_ordering_idx)) {
          // The ordering does not satisfy both of the orderings that are
          // interesting to us. So it's no better than the distinct_ordering_idx
          // one. Don't propose it.
          continue;
        }

        // The force_sort_rowids flag is only set for UPDATE and DELETE,
        // which don't have any syntax for specifying DISTINCT.
        assert(!force_sort_rowids);
        AccessPath sort_path = MakeSortPathForDistinct(
            thd, root_path, sort_ahead_ordering.ordering_idx,
            aggregation_is_unordered, orderings, ordering_state);
        receiver.ProposeAccessPath(&sort_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, "");
      }
    }
    root_candidates = std::move(new_root_candidates);
  }

  // Apply ORDER BY, if applicable.
  if (join->order.order != nullptr) {
    if (root_candidates.empty()) {
      // The secondary engine has rejected all candidates.
      assert(receiver.HasSecondaryEngineCostHook());
      return root_candidates;
    }
    Mem_root_array<TABLE *> tables = CollectTables(
        thd, root_candidates[0]);  // Should be same for all paths.
    if (trace != nullptr) {
      *trace += "Applying sort for ORDER BY\n";
    }

    // If we have LIMIT or OFFSET, we apply them here. This is done so that we
    // can push the LIMIT clause down to the SORT node in order to let Filesort
    // take advantage of it.
    const Query_expression *query_expression = join->query_expression();
    const ha_rows limit_rows = query_expression->select_limit_cnt;
    const ha_rows offset_rows = query_expression->offset_limit_cnt;

    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      // No sort is needed if the candidate already follows the
      // required ordering.
      if (orderings.DoesFollowOrder(root_path->ordering_state,
                                    order_by_ordering_idx)) {
        if (limit_rows != HA_POS_ERROR || offset_rows != 0) {
          root_path = NewLimitOffsetAccessPath(
              thd, root_path, limit_rows, offset_rows, join->calc_found_rows,
              /*reject_multiple_rows=*/false,
              /*send_records_override=*/nullptr);
        }
        receiver.ProposeAccessPath(root_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, "sort elided");
      } else {
        const bool push_limit_to_filesort =
            limit_rows != HA_POS_ERROR && !join->calc_found_rows;

        root_path = GetSafePathToSort(thd, join, root_path, need_rowid);

        AccessPath *sort_path = new (thd->mem_root) AccessPath;
        sort_path->type = AccessPath::SORT;
        sort_path->count_examined_rows = false;
        sort_path->immediate_update_delete_table =
            root_path->immediate_update_delete_table;
        sort_path->sort().child = root_path;
        sort_path->sort().filesort = nullptr;
        sort_path->sort().remove_duplicates = false;
        sort_path->sort().unwrap_rollup = false;
        sort_path->sort().limit =
            push_limit_to_filesort ? limit_rows : HA_POS_ERROR;
        sort_path->sort().order = join->order.order;
        EstimateSortCost(sort_path);

        // If this is a DELETE or UPDATE statement, row IDs must be preserved
        // through the ORDER BY clause, so that we know which rows to delete or
        // update.
        sort_path->sort().force_sort_rowids = force_sort_rowids;

        // If we have a LIMIT clause that is not pushed down to the filesort, or
        // if we have an OFFSET clause, we need to add a LIMIT_OFFSET path on
        // top of the SORT node.
        if ((limit_rows != HA_POS_ERROR && !push_limit_to_filesort) ||
            offset_rows != 0) {
          sort_path = NewLimitOffsetAccessPath(
              thd, sort_path, limit_rows, offset_rows, join->calc_found_rows,
              /*reject_multiple_rows=*/false,
              /*send_records_override=*/nullptr);
        }
        receiver.ProposeAccessPath(sort_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, "");
      }
    }
    root_candidates = std::move(new_root_candidates);
  }
  return root_candidates;
}

static AccessPath *ApplyWindow(THD *thd, AccessPath *root_path, Window *window,
                               JOIN *join, bool need_rowid_for_window) {
  AccessPath *window_path =
      NewWindowAccessPath(thd, root_path, window, /*temp_table_param=*/nullptr,
                          /*ref_slice=*/-1, window->needs_buffering());
  CopyBasicProperties(*root_path, window_path);
  window_path->cost += kWindowOneRowCost * window_path->num_output_rows();

  // NOTE: copy_items = false, because the window iterator does the copying
  // itself.
  return CreateMaterializationOrStreamingPath(thd, join, window_path,
                                              need_rowid_for_window,
                                              /*copy_items=*/false);
}

/**
  Find the ordering that allows us to process the most unprocessed windows.
  If specified, we can also demand that the ordering satisfies one or two
  later orderings (for DISTINCT and/or ORDER BY).

  Our priorities are, in strict order:

    1. Satisfying both DISTINCT and ORDER BY (if both are given;
       but see below).
    2. Satisfying the first operation after windowing
       (which is either DISTINCT or ORDER BY).
    3. Satisfying as many windows as possible.
    4. The shortest possible ordering (as a tie-breaker).

  If first_ordering_idx is given, #2 is mandatory. #4 is so that we don't
  get strange situations where the user specifies e.g. OVER (ORDER BY i)
  and we choose an ordering i,j,k,l,... because it happened to be given
  somewhere else.

  Note that normally, it is very hard to satisfy DISTINCT for a window
  function, because generally, it isn't constant for a given input
  (by nature, it also depends on other rows). But it can happen if the
  window frame is static; see main.window_functions_interesting_orders.

  @param join                    Contains the list of windows.
  @param orderings               Logical orderings in the query block.
  @param sort_ahead_orderings    Candidate orderings to consider.
  @param fd_set                  Active functional dependencies.
  @param finished_windows        Windows to ignore.
  @param tmp_buffer              Temporary space for keeping the best list
                                 of windows so far; must be as large as
                                 the number of values.
  @param first_ordering_idx      The first ordering after the query block
                                 that we need to satisfy (-1 if none).
  @param second_ordering_idx     The second ordering after the query block
                                 that we would like to satisfy (-1 if none).
  @param [out] included_windows  Which windows can be sorted using the given
                                 ordering.

  @return An index into sort_ahead_orderings, or -1 if no ordering could
    be found that sorts at least one window (plus, if first_ordering_idx
    is set, follows that ordering).
 */
static int FindBestOrderingForWindow(
    JOIN *join, const LogicalOrderings &orderings,
    FunctionalDependencySet fd_set,
    const Mem_root_array<SortAheadOrdering> &sort_ahead_orderings,
    Bounds_checked_array<bool> finished_windows,
    Bounds_checked_array<bool> tmp_buffer, int first_ordering_idx,
    int second_ordering_idx, Bounds_checked_array<bool> included_windows) {
  if (first_ordering_idx == -1) {
    assert(second_ordering_idx == -1);
  }

  int best_ordering_idx = -1;
  bool best_following_both_orders = false;
  int best_num_matching_windows = 0;
  for (size_t i = 0; i < sort_ahead_orderings.size(); ++i) {
    if (sort_ahead_orderings[i].sort_ahead_only) {
      continue;
    }
    const int ordering_idx = sort_ahead_orderings[i].ordering_idx;
    LogicalOrderings::StateIndex ordering_state =
        orderings.ApplyFDs(orderings.SetOrder(ordering_idx), fd_set);

    bool following_both_orders = false;
    if (first_ordering_idx != -1) {
      if (!orderings.DoesFollowOrder(ordering_state, first_ordering_idx)) {
        // Following one is mandatory.
        continue;
      }
      if (second_ordering_idx != -1) {
        if (orderings.DoesFollowOrder(ordering_state, second_ordering_idx)) {
          following_both_orders = true;
        } else if (best_following_both_orders) {
          continue;
        }
      }
    }

    // If we are doing sortahead for DISTINCT/ORDER BY:
    // Find windows that are referred to by DISTINCT/ORDER BY,
    // and disallow them. E.g., if we have
    //
    //   SELECT FOO() OVER w1 AS a ... ORDER BY a,
    //
    // we cannot put w1 in the group of windows that are to be sorted
    // together with ORDER BY.
    for (Window &window : join->m_windows) {
      window.m_mark = false;
    }
    Ordering ordering = orderings.ordering(ordering_idx);
    bool any_wf = false;
    for (OrderElement elem : ordering.GetElements()) {
      WalkItem(orderings.item(elem.item), enum_walk::PREFIX,
               [&any_wf](Item *item) {
                 if (item->m_is_window_function) {
                   down_cast<Item_sum *>(item)->window()->m_mark = true;
                   any_wf = true;
                 }
                 return false;
               });
      if (first_ordering_idx == -1 && any_wf) {
        break;
      }
    }

    // If we are doing sorts _before_ DISTINCT/ORDER BY, simply disallow
    // any sorts on window functions. There should be better options
    // available for us.
    if (first_ordering_idx == -1 && any_wf) {
      continue;
    }

    // Now find out which windows can be processed under this order.
    // We use tmp_buffer to hold which one we selected,
    // and then copy it into included_windows if we are the best so far.
    int num_matching_windows = 0;
    for (size_t window_idx = 0; window_idx < join->m_windows.size();
         ++window_idx) {
      Window *window = join->m_windows[window_idx];
      if (window->m_mark || finished_windows[window_idx] ||
          !orderings.DoesFollowOrder(ordering_state, window->m_ordering_idx)) {
        tmp_buffer[window_idx] = false;
        continue;
      }
      tmp_buffer[window_idx] = true;
      ++num_matching_windows;
    }
    if (num_matching_windows == 0) {
      continue;
    }

    bool is_best;
    if (best_ordering_idx == -1) {
      is_best = true;
    } else if (following_both_orders < best_following_both_orders) {
      is_best = false;
    } else if (following_both_orders > best_following_both_orders) {
      is_best = true;
    } else if (num_matching_windows < best_num_matching_windows) {
      is_best = false;
    } else if (num_matching_windows > best_num_matching_windows) {
      is_best = true;
    } else if (orderings.ordering(ordering_idx).GetElements().size() <
               orderings
                   .ordering(
                       sort_ahead_orderings[best_ordering_idx].ordering_idx)
                   .GetElements()
                   .size()) {
      is_best = true;
    } else {
      is_best = false;
    }
    if (is_best) {
      best_ordering_idx = i;
      best_following_both_orders = following_both_orders;
      best_num_matching_windows = num_matching_windows;
      memcpy(included_windows.array(), tmp_buffer.array(),
             sizeof(bool) * included_windows.size());
    }
  }
  return best_ordering_idx;
}

AccessPath *MakeSortPathAndApplyWindows(
    THD *thd, JOIN *join, AccessPath *root_path, int ordering_idx, ORDER *order,
    const LogicalOrderings &orderings,
    Bounds_checked_array<bool> windows_this_iteration,
    FunctionalDependencySet fd_set, int num_where_predicates,
    bool need_rowid_for_window, int single_window_idx,
    Bounds_checked_array<bool> finished_windows, int *num_windows_left) {
  AccessPath sort_path =
      MakeSortPathWithoutFilesort(thd, root_path, order,
                                  /*ordering_state=*/0, num_where_predicates);
  sort_path.ordering_state =
      orderings.ApplyFDs(orderings.SetOrder(ordering_idx), fd_set);
  root_path = new (thd->mem_root) AccessPath(sort_path);

  if (single_window_idx >= 0) {
    root_path = ApplyWindow(thd, root_path, join->m_windows[single_window_idx],
                            join, need_rowid_for_window);
    finished_windows[single_window_idx] = true;
    --(*num_windows_left);
    return root_path;
  }
  for (size_t window_idx = 0; window_idx < join->m_windows.size();
       ++window_idx) {
    if (!windows_this_iteration[window_idx]) {
      continue;
    }
    root_path = ApplyWindow(thd, root_path, join->m_windows[window_idx], join,
                            need_rowid_for_window);
    finished_windows[window_idx] = true;
    --(*num_windows_left);
  }
  return root_path;
}

}  // namespace

/**
  Apply window functions.

  Ordering of window functions is a tricky topic. We can apply window functions
  in any order that we'd like, but we would like to do as few sorts as possible.
  In its most general form, this would entail solving an instance of the
  traveling salesman problem (TSP), and although the number of windows is
  typically small (one or two in most queries), this can blow up for large
  numbers of windows.

  Thankfully, window functions never add or remove rows. We also assume that all
  sorts are equally expensive (which isn't really true, as ones on more columns
  take more processing time and buffer, but it's close enough in practice),
  and we also ignore the fact that as we compute more buffers, the temporary
  tables and sort buffers will get more columns. These assumptions, combined
  with some reasonable assumptions about ordering transitivity (if an ordering A
  is more sorted than an ordering B, and B > C, then also A > C -- the only
  thing that can disturb this is groupings, which we ignore for the sake of
  simplicity), mean that we need to care _only_ about the number of sorts, and
  can do them greedily. Thus, at any point, we pick the ordering that allows us
  to process the largest number of windows, process them, remove them from
  consideration, and repeat until there are none left.

  There is one more ordering complication; after windowing, we may have DISTINCT
  and/or ORDER BY, which may also benefit from groupings/orderings we leave
  after the last window. Thus, first of all, we see if there's an ordering that
  can satisfy them (ideally both if possible) _and_ at least one window; if so,
  we save that ordering and those windows for last.

  Temporary tables are set up in FinalizePlanForQueryBlock(). This is so that
  it is easier to have multiple different orderings for the temporary table
  parameters later.
 */
static Prealloced_array<AccessPath *, 4> ApplyWindowFunctions(
    THD *thd, const CostingReceiver &receiver,
    const LogicalOrderings &orderings, FunctionalDependencySet fd_set,
    bool aggregation_is_unordered, int order_by_ordering_idx,
    int distinct_ordering_idx, const JoinHypergraph &graph,
    const Mem_root_array<SortAheadOrdering> &sort_ahead_orderings,
    Query_block *query_block, int num_where_predicates, bool need_rowid,
    Prealloced_array<AccessPath *, 4> root_candidates, string *trace) {
  JOIN *join = query_block->join;

  // Figure out if windows need row IDs or not; we won't create
  // the temporary tables before later (since the optimal ordering
  // of windows is cost-based), so this is a conservative check.
  bool need_rowid_for_window = need_rowid;
  if (!need_rowid) {
    for (Item *item : *join->fields) {
      if (item->m_is_window_function && item->is_blob_field()) {
        need_rowid_for_window = true;
        break;
      }
    }
  }

  // Windows we're done processing, or have reserved for the last block.
  auto finished_windows =
      Bounds_checked_array<bool>::Alloc(thd->mem_root, join->m_windows.size());

  // Windows we've reserved for the last block (see function comment).
  auto reserved_windows =
      Bounds_checked_array<bool>::Alloc(thd->mem_root, join->m_windows.size());

  // Temporary space for FindBestOrderingForWindow().
  auto tmp =
      Bounds_checked_array<bool>::Alloc(thd->mem_root, join->m_windows.size());

  // Windows we're doing in this pass.
  auto included_windows =
      Bounds_checked_array<bool>::Alloc(thd->mem_root, join->m_windows.size());

  if (trace) {
    *trace += "\n";
  }
  Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
  for (AccessPath *root_path : root_candidates) {
    if (trace) {
      *trace += "Considering window order on top of " +
                PrintAccessPath(*root_path, graph, "") + "\n";
    }

    // First, go through and check which windows we can do without
    // any reordering, just based on the input ordering we get.
    int num_windows_left = join->m_windows.size();
    for (size_t window_idx = 0; window_idx < join->m_windows.size();
         ++window_idx) {
      Window *window = join->m_windows[window_idx];
      if (window->m_ordering_idx == -1 || join->implicit_grouping ||
          orderings.DoesFollowOrder(root_path->ordering_state,
                                    window->m_ordering_idx)) {
        if (trace) {
          *trace += std::string(" - window ") + window->printable_name() +
                    " does not need further sorting\n";
        }
        root_path =
            ApplyWindow(thd, root_path, window, join, need_rowid_for_window);
        finished_windows[window_idx] = true;
        --num_windows_left;
      } else {
        finished_windows[window_idx] = false;
      }
    }

    // Now, see if we can find an ordering that allows us to process
    // at least one window _and_ an operation after the windowing
    // (DISTINCT, ORDER BY). If so, that ordering will be our last.
    int final_sort_ahead_ordering_idx = -1;
    if ((!aggregation_is_unordered || distinct_ordering_idx == -1) &&
        (distinct_ordering_idx != -1 || order_by_ordering_idx != -1)) {
      int first_ordering_idx, second_ordering_idx;
      if (distinct_ordering_idx == -1) {
        first_ordering_idx = order_by_ordering_idx;
        second_ordering_idx = -1;
      } else {
        first_ordering_idx = distinct_ordering_idx;
        second_ordering_idx = order_by_ordering_idx;
      }
      final_sort_ahead_ordering_idx = FindBestOrderingForWindow(
          join, orderings, fd_set, sort_ahead_orderings, finished_windows, tmp,
          first_ordering_idx, second_ordering_idx, reserved_windows);
      for (size_t window_idx = 0; window_idx < join->m_windows.size();
           ++window_idx) {
        finished_windows[window_idx] |= reserved_windows[window_idx];
      }
    }

    // Now all the other orderings, eventually reaching all windows.
    while (num_windows_left > 0) {
      int sort_ahead_ordering_idx = FindBestOrderingForWindow(
          join, orderings, fd_set, sort_ahead_orderings, finished_windows, tmp,
          /*first_ordering_idx=*/-1,
          /*second_ordering_idx=*/-1, included_windows);
      Bounds_checked_array<bool> windows_this_iteration = included_windows;
      if (sort_ahead_ordering_idx == -1) {
        // None left, so take the one we've saved for last.
        sort_ahead_ordering_idx = final_sort_ahead_ordering_idx;
        windows_this_iteration = reserved_windows;
        final_sort_ahead_ordering_idx = -1;
      }

      if (sort_ahead_ordering_idx == -1) {
        // No sort-ahead orderings left, but some windows are left. The
        // remaining windows are handled after this loop.
        break;
      }

      root_path = MakeSortPathAndApplyWindows(
          thd, join, root_path,
          sort_ahead_orderings[sort_ahead_ordering_idx].ordering_idx,
          sort_ahead_orderings[sort_ahead_ordering_idx].order, orderings,
          windows_this_iteration, fd_set, num_where_predicates,
          need_rowid_for_window, /*single_window_idx*/ -1, finished_windows,
          &num_windows_left);
    }
    // The remaining windows (if any) have orderings which are not present in
    // the interesting orders bitmap, e.g. when the number of orders in the
    // query > kMaxSupportedOrderings. Create a sort path for each of
    // these windows using the window's own order instead of looking up an
    // order in the interesting-orders list.
    for (size_t window_idx = 0;
         window_idx < join->m_windows.size() && num_windows_left > 0;
         ++window_idx) {
      if (finished_windows[window_idx]) continue;

      Bounds_checked_array<bool> windows_this_iteration;
      root_path = MakeSortPathAndApplyWindows(
          thd, join, root_path, join->m_windows[window_idx]->m_ordering_idx,
          join->m_windows[window_idx]->sorting_order(thd), orderings,
          windows_this_iteration, fd_set, num_where_predicates,
          need_rowid_for_window, window_idx, finished_windows,
          &num_windows_left);
    }

    assert(num_windows_left == 0);
    receiver.ProposeAccessPath(root_path, &new_root_candidates,
                               /*obsolete_orderings=*/0, "");
  }
  if (trace) {
    *trace += "\n";
  }
  return new_root_candidates;
}

/**
  Find out if "value" has a type which is compatible with "field" so that it can
  be used for an index lookup if there is an index on "field".
 */
static bool CompatibleTypesForIndexLookup(Item_func_eq *eq_item, Field *field,
                                          Item *value) {
  if (!comparable_in_index(eq_item, field, Field::itRAW, eq_item->functype(),
                           value)) {
    // The types are not comparable in the index, so it's not sargable.
    return false;
  }

  if (field->cmp_type() == STRING_RESULT &&
      field->match_collation_to_optimize_range() &&
      field->charset() != eq_item->compare_collation()) {
    // The collations don't match, so it's not sargable.
    return false;
  }

  return true;
}

/**
  Find out whether “item” is a sargable condition; if so, add it to:

   - The list of sargable predicate for the tables (hypergraph nodes)
     the condition touches. For a regular condition, this will typically
     be one table; for a join condition, it will typically be two.
     If “force_table” is non-nullptr, only that table will be considered
     (this is used for join conditions, to ensure that we do not push
     down predicates that cannot, e.g. to the outer side of left joins).

   - The graph's global list of predicates, if it is not already present
     (predicate_index = -1). This will never happen for WHERE conditions,
     only for join conditions.
 */
static void PossiblyAddSargableCondition(THD *thd, Item *item,
                                         TABLE *force_table,
                                         int predicate_index,
                                         bool is_join_condition,
                                         JoinHypergraph *graph, string *trace) {
  if (!is_function_of_type(item, Item_func::EQ_FUNC)) {
    return;
  }
  Item_func_eq *eq_item = down_cast<Item_func_eq *>(item);
  if (eq_item->get_comparator()->get_child_comparator_count() >= 2) {
    return;
  }
  for (unsigned arg_idx = 0; arg_idx < 2; ++arg_idx) {
    Item **args = eq_item->arguments();
    Item *left = args[arg_idx];
    Item *right = args[1 - arg_idx];
    if (left->type() != Item::FIELD_ITEM) {
      continue;
    }
    Field *field = down_cast<Item_field *>(left)->field;
    TABLE *table = field->table;
    if (force_table != nullptr && force_table != table) {
      continue;
    }
    if (field->part_of_key.is_clear_all()) {
      // Not part of any key, so not sargable. (It could be part of a prefix
      // key, though, but we include them for now.)
      continue;
    }
    if (Overlaps(table->file->ha_table_flags(), HA_NO_INDEX_ACCESS)) {
      // Can't use index lookups on this table, so not sargable.
      continue;
    }
    JoinHypergraph::Node *node = FindNodeWithTable(graph, table);
    if (node == nullptr) {
      // A field in a different query block, so not sargable for us.
      continue;
    }

    // If the equality comes from a multiple equality, we have already verified
    // that the types of the arguments match exactly. For other equalities, we
    // need to check more thoroughly if the types are compatible.
    if (eq_item->source_multiple_equality != nullptr) {
      assert(CompatibleTypesForIndexLookup(eq_item, field, right));
    } else if (!CompatibleTypesForIndexLookup(eq_item, field, right)) {
      continue;
    }

    const table_map used_tables_left = table->pos_in_table_list->map();
    const table_map used_tables_right = right->used_tables();

    if (Overlaps(used_tables_left, used_tables_right)) {
      // Not sargable if the tables on the left and right side overlap, such as
      // t1.x = t1.y + t2.x. Will not be sargable in the opposite direction
      // either, so "break" instead of "continue".
      break;
    }

    if (Overlaps(used_tables_right, RAND_TABLE_BIT)) {
      // Non-deterministic predicates are not sargable. Will not be sargable in
      // the opposite direction either, so "break" instead of "continue".
      break;
    }

    if (trace != nullptr) {
      if (is_join_condition) {
        *trace += "Found sargable join condition " + ItemToString(item) +
                  " on " + node->table->alias + "\n";
      } else {
        *trace += "Found sargable condition " + ItemToString(item) + "\n";
      }
    }

    if (predicate_index == -1) {
      // This predicate is not already registered as a predicate
      // (which means in practice that it's a join predicate,
      // not a WHERE predicate), so add it so that we can refer
      // to it in bitmaps.
      Predicate p;
      p.condition = eq_item;
      p.selectivity = EstimateSelectivity(thd, eq_item, trace);
      p.used_nodes =
          GetNodeMapFromTableMap(eq_item->used_tables() & ~PSEUDO_TABLE_BITS,
                                 graph->table_num_to_node_num);
      p.total_eligibility_set =
          ~0;  // Should never be applied as a WHERE predicate.
      p.functional_dependencies_idx.init(thd->mem_root);
      p.contained_subqueries.init(thd->mem_root);  // Empty.
      graph->predicates.push_back(std::move(p));
      predicate_index = graph->predicates.size() - 1;
      graph->sargable_join_predicates.emplace(eq_item, predicate_index);
    }

    // Can we evaluate the right side of the predicate during optimization (in
    // ref_lookup_subsumes_comparison())? Don't consider items with subqueries
    // or stored procedures constant, as we don't want to execute them during
    // optimization.
    const bool can_evaluate = right->const_for_execution() &&
                              !right->has_subquery() && !right->is_expensive();

    node->sargable_predicates.push_back(
        {predicate_index, field, right, can_evaluate});

    // No need to check the opposite order. We have no indexes on constants.
    if (can_evaluate) break;
  }
}

// Find sargable predicates, ie., those that we can push down into indexes.
// See add_key_field().
//
// TODO(sgunders): Include x=y OR NULL predicates, <=> and IS NULL predicates,
// and the special case of COLLATION accepted in add_key_field().
//
// TODO(sgunders): Integrate with the range optimizer, or find some other way
// of accepting <, >, <= and >= predicates.
void FindSargablePredicates(THD *thd, string *trace, JoinHypergraph *graph) {
  if (trace != nullptr) {
    *trace += "\n";
  }
  for (unsigned i = 0; i < graph->num_where_predicates; ++i) {
    if (IsSingleBitSet(graph->predicates[i].total_eligibility_set)) {
      PossiblyAddSargableCondition(thd, graph->predicates[i].condition,
                                   /*force_table=*/nullptr, i,
                                   /*is_join_condition=*/false, graph, trace);
    }
  }
  for (JoinHypergraph::Node &node : graph->nodes) {
    for (Item *cond : node.join_conditions_pushable_to_this) {
      const auto it = graph->sargable_join_predicates.find(cond);
      int predicate_index =
          (it == graph->sargable_join_predicates.end()) ? -1 : it->second;
      PossiblyAddSargableCondition(thd, cond, node.table, predicate_index,
                                   /*is_join_condition=*/true, graph, trace);
    }
  }
}

static bool ComesFromSameMultiEquality(Item *cond1, Item_eq_base *cond2) {
  return cond2->source_multiple_equality != nullptr &&
         is_function_of_type(cond1, Item_func::EQ_FUNC) &&
         down_cast<Item_func_eq *>(cond1)->source_multiple_equality ==
             cond2->source_multiple_equality;
}

/**
  For each edge, cache some information for each of its join conditions.
  This reduces work when repeatedly applying these join conditions later on.
  In particular, FindContainedSubqueries() contains a large amount of
  virtual function calls that we would like to avoid doing every time
  we consider a given join.
 */
static void CacheCostInfoForJoinConditions(THD *thd,
                                           const Query_block *query_block,
                                           JoinHypergraph *graph,
                                           string *trace) {
  for (JoinPredicate &edge : graph->edges) {
    edge.expr->properties_for_equijoin_conditions.init(thd->mem_root);
    edge.expr->properties_for_join_conditions.init(thd->mem_root);
    for (Item_eq_base *cond : edge.expr->equijoin_conditions) {
      CachedPropertiesForPredicate properties;
      properties.selectivity = EstimateSelectivity(thd, cond, trace);
      properties.contained_subqueries.init(thd->mem_root);
      FindContainedSubqueries(
          cond, query_block, [&properties](const ContainedSubquery &subquery) {
            properties.contained_subqueries.push_back(subquery);
          });

      // Cache information about what sargable conditions this join condition
      // would be redundant against, for RedundantThroughSargable().
      // But don't deduplicate against ourselves (in case we're sargable).
      MutableOverflowBitset redundant(thd->mem_root, graph->predicates.size());
      for (unsigned sargable_pred_idx = graph->num_where_predicates;
           sargable_pred_idx < graph->predicates.size(); ++sargable_pred_idx) {
        Item *sargable_condition =
            graph->predicates[sargable_pred_idx].condition;
        if (sargable_condition != cond &&
            ComesFromSameMultiEquality(sargable_condition, cond)) {
          redundant.SetBit(sargable_pred_idx);
        }
      }
      properties.redundant_against_sargable_predicates = std::move(redundant);
      edge.expr->properties_for_equijoin_conditions.push_back(
          std::move(properties));
    }
    for (Item *cond : edge.expr->join_conditions) {
      CachedPropertiesForPredicate properties;
      properties.selectivity = EstimateSelectivity(thd, cond, trace);
      properties.contained_subqueries.init(thd->mem_root);
      FindContainedSubqueries(
          cond, query_block, [&properties](const ContainedSubquery &subquery) {
            properties.contained_subqueries.push_back(subquery);
          });
      edge.expr->properties_for_join_conditions.push_back(
          std::move(properties));
    }
  }
}

/**
  Find the lowest-cost plan (which hopefully is also the cheapest to execute)
  of all the legal ways to execute the query. The overall order of operations is
  largely dictated by the standard:

    1. All joined tables, including join predicates.
    2. WHERE predicates (we push these down into #1 where allowed)
    3. GROUP BY (it is sometimes possible to push this down into #1,
       but we don't have the functionality to do so).
    4. HAVING.
    5. Window functions.
    6. DISTINCT.
    7. ORDER BY.
    8. LIMIT.
    9. SQL_BUFFER_RESULT (a MySQL extension).

  The place where we have the most leeway by far is #1, which is why this
  part of the optimizer is generally called the join optimizer (there are
  potentially billions of different join orderings, whereas each of the
  other steps, except windowing, can only be done in one or two ways).
  But the various outputs of #1 can have different properties, that can make
  for higher or lower costs in the other steps. (For instance, LIMIT will
  affect candidates with different init_cost differently, and ordering
  properties can skip sorting in ORDER BY entirely.) Thus, we allow keeping
  multiple candidates in play at every step if they are meaningfully different,
  and only pick out the winning candidate based on cost at the very end.
 */
AccessPath *FindBestQueryPlan(THD *thd, Query_block *query_block,
                              string *trace) {
  JOIN *join = query_block->join;
  if (CheckSupportedQuery(thd)) return nullptr;

  // The hypergraph optimizer does not do const tables,
  // nor does it evaluate subqueries during optimization.
  assert(
      IsSubset(OPTION_NO_CONST_TABLES | OPTION_NO_SUBQUERY_DURING_OPTIMIZATION,
               query_block->active_options()));

  // In the case of rollup (only): After the base slice list was made, we may
  // have modified the field list to add rollup group items and sum switchers.
  // The resolver also takes care to update these in query_block->order_list.
  // However, even though the hypergraph join optimizer doesn't use slices,
  // setup_order() later modifies order->item to point into the base slice,
  // where the rollup group items are _not_ updated. Thus, we need to refresh
  // the base slice before we do anything.
  //
  // It would be better to have rollup resolving update the base slice directly,
  // but this would break HAVING in the old join optimizer (see the other call
  // to refresh_base_slice(), in JOIN::make_tmp_tables_info()).
  if (join->rollup_state != JOIN::RollupState::NONE) {
    join->refresh_base_slice();
  }

  // NOTE: Normally, we'd expect join->temp_tables and
  // join->filesorts_to_cleanup to be empty, but since we can get called twice
  // for materialized subqueries, there may already be data there that we must
  // keep.

  // Convert the join structures into a hypergraph.
  JoinHypergraph graph(thd->mem_root, query_block);
  bool where_is_always_false = false;
  if (MakeJoinHypergraph(thd, trace, &graph, &where_is_always_false)) {
    return nullptr;
  }

  if (where_is_always_false) {
    if (trace != nullptr) {
      *trace +=
          "Skipping join order optimization because an always false condition "
          "was found in the WHERE clause.\n";
    }
    return CreateZeroRowsForEmptyJoin(join, "WHERE condition is always false");
  }

  FindSargablePredicates(thd, trace, &graph);

  // Now that we have all join conditions, cache some properties
  // that we'd like to use many times.
  CacheCostInfoForJoinConditions(thd, query_block, &graph, trace);

  // Figure out if any later sort will need row IDs.
  bool need_rowid = false;
  if (query_block->is_explicitly_grouped() || join->order.order != nullptr ||
      join->select_distinct || !join->m_windows.is_empty()) {
    // NOTE: This is distinct from SortWillBeOnRowId(), as it also checks blob
    // fields arising from blob-generating functions on non-blob fields.
    for (Item *item : *join->fields) {
      if (item->is_blob_field()) {
        need_rowid = true;
        break;
      }
    }
    for (Table_ref *tl = query_block->leaf_tables; tl != nullptr && !need_rowid;
         tl = tl->next_leaf) {
      if (SortWillBeOnRowId(tl->table)) {
        need_rowid = true;
      }
    }
  }

  // Find out which predicates contain subqueries.
  MutableOverflowBitset materializable_predicates{thd->mem_root,
                                                  graph.predicates.size()};
  for (unsigned i = 0; i < graph.predicates.size(); ++i) {
    if (ContainsSubqueries(graph.predicates[i].condition)) {
      materializable_predicates.SetBit(i);
    }
  }
  graph.materializable_predicates = std::move(materializable_predicates);

  const bool is_topmost_query_block =
      query_block->outer_query_block() == nullptr;
  const bool is_delete = is_topmost_query_block && IsDeleteStatement(thd);
  const bool is_update = is_topmost_query_block && IsUpdateStatement(thd);

  table_map update_delete_target_tables = 0;
  table_map immediate_update_delete_candidates = 0;
  if (is_delete || is_update) {
    update_delete_target_tables = FindUpdateDeleteTargetTables(query_block);
    immediate_update_delete_candidates = FindImmediateUpdateDeleteCandidates(
        graph, update_delete_target_tables, is_delete);
  }

  NodeMap fulltext_tables = 0;
  uint64_t sargable_fulltext_predicates = 0;
  if (query_block->has_ft_funcs()) {
    fulltext_tables = FindFullTextSearchedTables(graph);

    // Check if we have full-text indexes that can be used.
    sargable_fulltext_predicates = FindSargableFullTextPredicates(graph);
    EnableFullTextCoveringIndexes(query_block);
  }

  // Collect interesting orders from ORDER BY, GROUP BY, semijoins and windows.
  // See BuildInterestingOrders() for more detailed information.
  SecondaryEngineFlags engine_flags = EngineFlags(thd);
  LogicalOrderings orderings(thd);
  Mem_root_array<SortAheadOrdering> sort_ahead_orderings(thd->mem_root);
  Mem_root_array<ActiveIndexInfo> active_indexes(thd->mem_root);
  Mem_root_array<FullTextIndexInfo> fulltext_searches(thd->mem_root);
  int order_by_ordering_idx = -1;
  int group_by_ordering_idx = -1;
  int distinct_ordering_idx = -1;
  BuildInterestingOrders(thd, &graph, query_block, &orderings,
                         &sort_ahead_orderings, &order_by_ordering_idx,
                         &group_by_ordering_idx, &distinct_ordering_idx,
                         &active_indexes, &fulltext_searches, trace);

  if (InjectCastNodes(&graph)) return nullptr;

  // Run the actual join optimizer algorithm. This creates an access path
  // for the join as a whole (with lowest possible cost, and thus also
  // hopefully optimal execution time), with all pushable predicates applied.
  if (trace != nullptr) {
    *trace += "\nEnumerating subplans:\n";
  }
  for (const JoinHypergraph::Node &node : graph.nodes) {
    node.table->init_cost_model(thd->cost_model());
  }
  const secondary_engine_modify_access_path_cost_t secondary_engine_cost_hook =
      SecondaryEngineCostHook(thd);
  CostingReceiver receiver(
      thd, query_block, graph, &orderings, &sort_ahead_orderings,
      &active_indexes, &fulltext_searches, fulltext_tables,
      sargable_fulltext_predicates, update_delete_target_tables,
      immediate_update_delete_candidates, need_rowid, EngineFlags(thd),
      thd->variables.optimizer_max_subgraph_pairs, secondary_engine_cost_hook,
      trace);
  if (graph.edges.empty()) {
    // Fast path for single-table queries. No need to run the join enumeration
    // when there is no join. Just visit the only node directly.
    assert(graph.nodes.size() == 1);
    if (receiver.FoundSingleNode(0) && thd->is_error()) {
      return nullptr;
    }
  } else if (EnumerateAllConnectedPartitions(graph.graph, &receiver) &&
             !thd->is_error() && join->zero_result_cause == nullptr) {
    SimplifyQueryGraph(thd, thd->variables.optimizer_max_subgraph_pairs, &graph,
                       trace);

    // Reset the receiver and run the query again, this time with
    // the simplified hypergraph (and no query limit, in case the
    // given limit was just inherently too low, e.g., one subgraph pair
    // and three tables).
    //
    // It's not given that we _must_ reset the receiver; we could
    // probably have reused its state (which could save time and
    // even lead to a better plan, if we have simplified away some
    // join orderings that have already been evaluated).
    // However, more subgraph pairs also often means we get more access
    // paths on the Pareto frontier for each subgraph, and given
    // that we don't currently have any heuristics to curb the
    // amount of those, it is probably good to get the second-order
    // effect as well and do a full reset.
    if (trace) {
      *trace += "Simplified hypergraph:\n";
      *trace += PrintDottyHypergraph(graph);
      *trace += "\nRestarting query planning with the new graph.\n";
    }
    receiver = CostingReceiver(
        thd, query_block, graph, &orderings, &sort_ahead_orderings,
        &active_indexes, &fulltext_searches, fulltext_tables,
        sargable_fulltext_predicates, update_delete_target_tables,
        immediate_update_delete_candidates, need_rowid, EngineFlags(thd),
        /*subgraph_pair_limit=*/-1, secondary_engine_cost_hook, trace);
    // Reset the secondary engine planning flags
    graph.secondary_engine_costing_flags = {};
    if (EnumerateAllConnectedPartitions(graph.graph, &receiver) &&
        thd->is_error()) {
      return nullptr;
    }
  }
  if (thd->is_error()) return nullptr;

  if (join->zero_result_cause != nullptr) {
    if (trace != nullptr) {
      *trace += "The join returns zero rows. Final cost is 0.0.\n";
    }
    return CreateZeroRowsForEmptyJoin(join, join->zero_result_cause);
  }

  // Get the root candidates. If there is a secondary engine cost hook, there
  // may be no candidates, as the hook may have rejected so many access paths
  // that we could not build a complete plan. Otherwise, expect at least one
  // candidate.
  if (secondary_engine_cost_hook != nullptr &&
      (!receiver.HasSeen(TablesBetween(0, graph.nodes.size())) ||
       receiver.root_candidates().empty())) {
    my_error(ER_SECONDARY_ENGINE, MYF(0),
             "All plans were rejected by the secondary storage engine");
    return nullptr;
  }
  Prealloced_array<AccessPath *, 4> root_candidates =
      receiver.root_candidates();
  assert(!root_candidates.empty());
  thd->m_current_query_partial_plans += receiver.num_subplans();
  if (trace != nullptr) {
    *trace += StringPrintf(
        "\nEnumerated %zu subplans keeping a total of %zu access paths, "
        "got %zu candidate(s) to finalize:\n",
        receiver.num_subplans(), receiver.num_access_paths(),
        root_candidates.size());
  }

  // If we know the result will be empty, there is no point in adding paths for
  // filters, aggregation, windowing and sorting on top of it further down. Just
  // return the empty result directly.
  if (receiver.always_empty()) {
    for (AccessPath *root_path : root_candidates) {
      if (root_path->type == AccessPath::ZERO_ROWS) {
        if (trace != nullptr) {
          *trace += "The join returns zero rows. Final cost is 0.0.\n";
        }
        return CreateZeroRowsForEmptyJoin(join, root_path->zero_rows().cause);
      }
    }
  }

  // Now we have one or more access paths representing joining all the tables
  // together. (There may be multiple ones because they can be better at
  // different metrics.) We apply the post-join operations to all of them in
  // turn, and then finally pick out the one with the lowest total cost,
  // because at the end, other metrics don't really matter any more.
  //
  // We could have stopped caring about e.g. init_cost after LIMIT
  // has been applied (after which it no longer matters), so that we'd get
  // fewer candidates in each step, but this part is so cheap that it's
  // unlikely to be worth it. We go through ProposeAccessPath() mainly
  // because it gives us better tracing.
  if (trace != nullptr) {
    *trace += "Adding final predicates\n";
  }
  FunctionalDependencySet fd_set = receiver.active_fds_at_root();
  bool has_final_predicates = false;
  for (size_t i = 0; i < graph.num_where_predicates; ++i) {
    // Apply any predicates that don't belong to any
    // specific table, or which are nondeterministic.
    if (!Overlaps(graph.predicates[i].total_eligibility_set,
                  TablesBetween(0, graph.nodes.size())) ||
        Overlaps(graph.predicates[i].total_eligibility_set, RAND_TABLE_BIT)) {
      fd_set |= graph.predicates[i].functional_dependencies;
      has_final_predicates = true;
    }
  }

  // Add the final predicates to the root candidates, and expand FILTER access
  // paths for all predicates (not only the final ones) in the entire access
  // path tree of the candidates.
  //
  // It is an unnecessary step if there are no FILTER access paths to expand.
  // It's not so expensive that it's worth spending a lot of effort to find out
  // if it can be skipped, but let's skip it if our only candidate is an EQ_REF
  // with no filter predicates, so that we don't waste time in point selects.
  if (has_final_predicates ||
      !(root_candidates.size() == 1 &&
        root_candidates[0]->type == AccessPath::EQ_REF &&
        IsEmpty(root_candidates[0]->filter_predicates))) {
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (const AccessPath *root_path : root_candidates) {
      for (bool materialize_subqueries : {false, true}) {
        AccessPath path = *root_path;
        double init_once_cost = 0.0;

        MutableOverflowBitset filter_predicates =
            path.filter_predicates.Clone(thd->mem_root);

        // Apply any predicates that don't belong to any
        // specific table, or which are nondeterministic.
        for (size_t i = 0; i < graph.num_where_predicates; ++i) {
          if (!Overlaps(graph.predicates[i].total_eligibility_set,
                        TablesBetween(0, graph.nodes.size())) ||
              Overlaps(graph.predicates[i].total_eligibility_set,
                       RAND_TABLE_BIT)) {
            filter_predicates.SetBit(i);
            FilterCost cost =
                EstimateFilterCost(thd, root_path->num_output_rows(),
                                   graph.predicates[i].contained_subqueries);
            if (materialize_subqueries) {
              path.cost += cost.cost_if_materialized;
              init_once_cost += cost.cost_to_materialize;
            } else {
              path.cost += cost.cost_if_not_materialized;
            }
            path.set_num_output_rows(path.num_output_rows() *
                                     graph.predicates[i].selectivity);
          }
        }
        path.ordering_state = orderings.ApplyFDs(path.ordering_state, fd_set);

        path.filter_predicates = std::move(filter_predicates);
        const bool contains_subqueries =
            Overlaps(path.filter_predicates, graph.materializable_predicates);

        // Now that we have decided on a full plan, expand all
        // the applied filter maps into proper FILTER nodes
        // for execution. This is a no-op in the second
        // iteration.
        ExpandFilterAccessPaths(thd, &path, join, graph.predicates,
                                graph.num_where_predicates);

        if (materialize_subqueries) {
          assert(path.type == AccessPath::FILTER);
          path.filter().materialize_subqueries = true;
          path.cost += init_once_cost;  // Will be subtracted
                                        // back for rescans.
          path.init_cost += init_once_cost;
          path.init_once_cost += init_once_cost;
        }

        receiver.ProposeAccessPath(&path, &new_root_candidates,
                                   /*obsolete_orderings=*/0,
                                   materialize_subqueries ? "mat. subq" : "");

        if (!contains_subqueries) {
          // Nothing to try to materialize.
          break;
        }
      }
    }
    root_candidates = std::move(new_root_candidates);
  }

  // Apply GROUP BY, if applicable. We currently always do this by sorting
  // first and then using streaming aggregation.
  const bool aggregation_is_unordered = Overlaps(
      engine_flags,
      MakeSecondaryEngineFlags(SecondaryEngineFlag::AGGREGATION_IS_UNORDERED));
  if (query_block->is_grouped()) {
    if (join->make_sum_func_list(*join->fields, /*before_group_by=*/true))
      return nullptr;

    graph.secondary_engine_costing_flags |=
        SecondaryEngineCostingFlag::CONTAINS_AGGREGATION_ACCESSPATH;

    if (trace != nullptr) {
      *trace += "Applying aggregation for GROUP BY\n";
    }

    // Reuse this, so that we do not have to recalculate it for each
    // alternative aggregate path.
    double aggregate_rows = kUnknownRowCount;
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      const bool rollup = (join->rollup_state != JOIN::RollupState::NONE);
      const bool group_needs_sort =
          query_block->is_explicitly_grouped() && !aggregation_is_unordered &&
          !orderings.DoesFollowOrder(root_path->ordering_state,
                                     group_by_ordering_idx);

      if (!group_needs_sort) {
        AccessPath aggregate_path = CreateStreamingAggregationPath(
            thd, root_path, join, rollup, aggregate_rows, trace);
        aggregate_rows = aggregate_path.num_output_rows();
        receiver.ProposeAccessPath(&aggregate_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, "sort elided");
        continue;
      }

      root_path = GetSafePathToSort(thd, join, root_path, need_rowid);

      // We need to sort. Try all sort-ahead, not just the one directly derived
      // from GROUP BY clause, because a broader one might help us elide ORDER
      // BY or DISTINCT later.
      for (const SortAheadOrdering &sort_ahead_ordering :
           sort_ahead_orderings) {
        LogicalOrderings::StateIndex ordering_state = orderings.ApplyFDs(
            orderings.SetOrder(sort_ahead_ordering.ordering_idx), fd_set);
        if (!orderings.DoesFollowOrder(ordering_state, group_by_ordering_idx)) {
          continue;
        }
        if (sort_ahead_ordering.aggregates_required) {
          // We can't sort by an aggregate before we've aggregated.
          continue;
        }

        Mem_root_array<TABLE *> tables = CollectTables(thd, root_path);
        AccessPath *sort_path = new (thd->mem_root) AccessPath;
        sort_path->type = AccessPath::SORT;
        sort_path->count_examined_rows = false;
        sort_path->sort().child = root_path;
        sort_path->sort().filesort = nullptr;
        sort_path->sort().remove_duplicates = false;
        sort_path->sort().unwrap_rollup = true;
        sort_path->sort().limit = HA_POS_ERROR;
        sort_path->sort().force_sort_rowids = false;
        sort_path->sort().order = sort_ahead_ordering.order;
        EstimateSortCost(sort_path);
        assert(!aggregation_is_unordered);
        sort_path->ordering_state = ordering_state;

        char description[256];
        if (trace != nullptr) {
          snprintf(description, sizeof(description), "sort(%d)",
                   sort_ahead_ordering.ordering_idx);
        }

        AccessPath aggregate_path = CreateStreamingAggregationPath(
            thd, sort_path, join, rollup, aggregate_rows, trace);
        aggregate_rows = aggregate_path.num_output_rows();
        receiver.ProposeAccessPath(&aggregate_path, &new_root_candidates,
                                   /*obsolete_orderings=*/0, description);
      }
    }
    root_candidates = std::move(new_root_candidates);

    if (make_group_fields(join, join)) {
      return nullptr;
    }

    // Final setup will be done in FinalizePlanForQueryBlock(),
    // when we have all materialization done.
  }

  // Before we apply the HAVING condition, make sure its used_tables() cache is
  // refreshed. The condition might have been rewritten by
  // FinalizePlanForQueryBlock() to point into a temporary table in a previous
  // execution. Even if that change was rolled back at the end of the previous
  // execution, used_tables() may still say it uses the temporary table.
  if (join->having_cond != nullptr) {
    join->having_cond->update_used_tables();
  }

  // Apply HAVING, if applicable (sans any window-related in2exists parts,
  // which we apply below).
  Item *having_cond;
  Item *having_cond_wf;
  SplitHavingCondition(thd, join->having_cond, &having_cond, &having_cond_wf);
  ApplyHavingCondition(thd, having_cond, query_block,
                       "Applying filter for HAVING\n", trace, &root_candidates,
                       &receiver);

  // If we have GROUP BY followed by a window function (which might include
  // ORDER BY), we might need to materialize before the first ordering -- see
  // the comment near the top of ApplyDistinctAndOrder() for why.
  if (query_block->is_explicitly_grouped() && !join->m_windows.is_empty()) {
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      root_path =
          CreateMaterializationOrStreamingPath(thd, join, root_path, need_rowid,
                                               /*copy_items=*/true);
      receiver.ProposeAccessPath(root_path, &new_root_candidates,
                                 /*obsolete_orderings=*/0, "");
    }
    root_candidates = std::move(new_root_candidates);
  }

  join->m_windowing_steps = !join->m_windows.is_empty();
  if (join->m_windowing_steps) {
    graph.secondary_engine_costing_flags |=
        SecondaryEngineCostingFlag::CONTAINS_WINDOW_ACCESSPATH;
    root_candidates = ApplyWindowFunctions(
        thd, receiver, orderings, fd_set, aggregation_is_unordered,
        order_by_ordering_idx, distinct_ordering_idx, graph,
        sort_ahead_orderings, query_block, graph.num_where_predicates,
        need_rowid, std::move(root_candidates), trace);
  }

  ApplyHavingCondition(
      thd, having_cond_wf, query_block,
      "Applying filter for window function in2exists conditions\n", trace,
      &root_candidates, &receiver);

  graph.secondary_engine_costing_flags |=
      SecondaryEngineCostingFlag::HANDLING_DISTINCT_ORDERBY_LIMITOFFSET;
  if (join->select_distinct || join->order.order != nullptr) {
    // UPDATE and DELETE must preserve row IDs through ORDER BY in order to keep
    // track of which rows to update or delete.
    const bool force_sort_rowids = update_delete_target_tables != 0;

    root_candidates = ApplyDistinctAndOrder(
        thd, receiver, orderings, aggregation_is_unordered,
        order_by_ordering_idx, distinct_ordering_idx, sort_ahead_orderings,
        fd_set, query_block, need_rowid, force_sort_rowids,
        std::move(root_candidates), trace);
  }

  // Apply LIMIT and OFFSET, if applicable. If the query block is ordered, they
  // are already applied by ApplyDistinctAndOrder().
  Query_expression *query_expression = join->query_expression();
  if (join->order.order == nullptr &&
      (query_expression->select_limit_cnt != HA_POS_ERROR ||
       query_expression->offset_limit_cnt != 0)) {
    if (trace != nullptr) {
      *trace += "Applying LIMIT\n";
    }
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      AccessPath *limit_path = NewLimitOffsetAccessPath(
          thd, root_path, query_expression->select_limit_cnt,
          query_expression->offset_limit_cnt, join->calc_found_rows,
          /*reject_multiple_rows=*/false,
          /*send_records_override=*/nullptr);
      receiver.ProposeAccessPath(limit_path, &new_root_candidates,
                                 /*obsolete_orderings=*/0, "");
    }
    root_candidates = std::move(new_root_candidates);
  }

  // Add a DELETE_ROWS or UPDATE_ROWS access path if this is the topmost query
  // block of a DELETE statement or an UPDATE statement.
  if (is_delete) {
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      table_map immediate_tables = 0;
      if (root_path->immediate_update_delete_table != -1) {
        immediate_tables = graph.nodes[root_path->immediate_update_delete_table]
                               .table->pos_in_table_list->map();
      }
      AccessPath *delete_path = NewDeleteRowsAccessPath(
          thd, root_path, update_delete_target_tables, immediate_tables);
      EstimateDeleteRowsCost(delete_path);
      receiver.ProposeAccessPath(delete_path, &new_root_candidates,
                                 /*obsolete_orderings=*/0, "");
    }
    root_candidates = std::move(new_root_candidates);
  } else if (is_update) {
    Prealloced_array<AccessPath *, 4> new_root_candidates(PSI_NOT_INSTRUMENTED);
    for (AccessPath *root_path : root_candidates) {
      table_map immediate_tables = 0;
      if (root_path->immediate_update_delete_table != -1) {
        immediate_tables = graph.nodes[root_path->immediate_update_delete_table]
                               .table->pos_in_table_list->map();
      }
      AccessPath *update_path = NewUpdateRowsAccessPath(
          thd, root_path, update_delete_target_tables, immediate_tables);
      EstimateUpdateRowsCost(update_path);
      receiver.ProposeAccessPath(update_path, &new_root_candidates,
                                 /*obsolete_orderings=*/0, "");
    }
    root_candidates = std::move(new_root_candidates);
  }

  if (thd->is_error()) return nullptr;

  if (root_candidates.empty()) {
    // The secondary engine has rejected so many of the post-processing paths
    // (e.g., sorting, limit, grouping) that we could not build a complete plan.
    assert(secondary_engine_cost_hook != nullptr);
    my_error(ER_SECONDARY_ENGINE, MYF(0),
             "All plans were rejected by the secondary storage engine");
    return nullptr;
  }

  // TODO(sgunders): If we are part of e.g. a derived table and are streamed,
  // we might want to keep multiple root paths around for future use, e.g.,
  // if there is a LIMIT higher up.
  AccessPath *root_path =
      *std::min_element(root_candidates.begin(), root_candidates.end(),
                        [](const AccessPath *a, const AccessPath *b) {
                          return a->cost < b->cost;
                        });

  // Materialize the result if a top-level query block has the SQL_BUFFER_RESULT
  // option, and the chosen root path isn't already a materialization path. Skip
  // the materialization path when using an external executor, since it will
  // have to decide for itself whether and how to do the materialization.
  if (query_block->active_options() & OPTION_BUFFER_RESULT &&
      is_topmost_query_block && !IsMaterializationPath(root_path) &&
      IteratorsAreNeeded(thd, root_path)) {
    if (trace != nullptr) {
      *trace += "Adding temporary table for SQL_BUFFER_RESULT.\n";
    }

    // If we have windows, we may need to add a materialization for the last
    // window here, or create_tmp_table() will not create fields for its window
    // functions. (All other windows have already been materialized.)
    bool copy_items = join->m_windows.is_empty();
    root_path =
        CreateMaterializationPath(thd, join, root_path, /*temp_table=*/nullptr,
                                  /*temp_table_param=*/nullptr, copy_items);
  }

  if (trace != nullptr) {
    *trace += StringPrintf("Final cost is %.1f.\n", root_path->cost);
  }

#ifndef NDEBUG
  WalkAccessPaths(root_path, join, WalkAccessPathPolicy::ENTIRE_QUERY_BLOCK,
                  [&](const AccessPath *path, const JOIN *) {
                    assert(path->cost >= path->init_cost);
                    assert(path->init_cost >= path->init_once_cost);
                    return false;
                  });
#endif

  join->needs_finalize = true;
  join->best_rowcount = lrint(root_path->num_output_rows());
  join->best_read = root_path->cost;

  // 0 or 1 rows has a special meaning; it means a _guarantee_ we have no more
  // than one (so-called “const tables”). Make sure we don't give that
  // guarantee unless we have a LIMIT.
  if (join->best_rowcount <= 1 &&
      query_expression->select_limit_cnt - query_expression->offset_limit_cnt >
          1) {
    join->best_rowcount = PLACEHOLDER_TABLE_ROW_ESTIMATE;
  }

  return root_path;
}