File: sql_planner.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 (4821 lines) | stat: -rw-r--r-- 199,612 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
/* Copyright (c) 2000, 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 */

/**
  @file

  @brief
  Create plan for a single select.


  @defgroup Query_Planner  Query Planner
  @{
*/

#include "sql/sql_planner.h"

#include "my_config.h"

#include <float.h>
#include <limits.h>
#include <string.h>
#include <algorithm>
#include <atomic>

#include "my_base.h"  // key_part_map
#include "my_bit.h"   // my_count_bits
#include "my_bitmap.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_double2ulonglong.h"
#include "my_macros.h"
#include "sql/enum_query_type.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/key.h"
#include "sql/merge_sort.h"  // merge_sort
#include "sql/nested_join.h"
#include "sql/opt_costmodel.h"
#include "sql/opt_hints.h"  // hint_table_state
#include "sql/opt_trace.h"  // Opt_trace_object
#include "sql/opt_trace_context.h"
#include "sql/query_options.h"
#include "sql/query_result.h"
#include "sql/range_optimizer/path_helpers.h"
#include "sql/range_optimizer/range_optimizer.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"  // THD
#include "sql/sql_const.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_opt_exec_shared.h"
#include "sql/sql_optimizer.h"  // JOIN
#include "sql/sql_select.h"     // JOIN_TAB
#include "sql/sql_test.h"       // print_plan
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/window.h"
#include "sql_string.h"

using std::max;
using std::min;

/**
  Number of rows in a reference table when refereed through a not unique key.
  This value is only used when we don't know anything about the key
  distribution.
*/
static constexpr const ha_rows MATCHING_ROWS_IN_OTHER_TABLE{10};

static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
static void trace_plan_prefix(JOIN *join, uint idx, table_map excluded_tables);

static uint max_part_bit(key_part_map bits) {
  uint found;
  for (found = 0; bits & 1; found++, bits >>= 1)
    ;
  return found;
}

static uint cache_record_length(JOIN *join, uint idx) {
  uint length = 0;
  JOIN_TAB **pos, **end;

  for (pos = join->best_ref + join->const_tables, end = join->best_ref + idx;
       pos != end; pos++) {
    JOIN_TAB *join_tab = *pos;
    if (!join_tab->used_fieldlength)  // Not calculated yet
    {
      /*
        (1) needs_rowid: we don't know if Duplicate Weedout may be
        used, length will thus be inaccurate, this is acceptable.
      */
      calc_used_field_length(join_tab->table(),
                             false,  // (1)
                             &join_tab->used_fieldlength);
    }
    length += join_tab->used_fieldlength;
  }
  return length;
}

Optimize_table_order::Optimize_table_order(THD *thd_arg, JOIN *join_arg,
                                           Table_ref *sjm_nest_arg)
    : thd(thd_arg),
      join(join_arg),
      search_depth(determine_search_depth(thd->variables.optimizer_search_depth,
                                          join->tables - join->const_tables)),
      prune_level(thd->variables.optimizer_prune_level),
      cur_embedding_map(0),
      emb_sjm_nest(sjm_nest_arg),
      excluded_tables(
          (emb_sjm_nest ? (join->all_table_map & ~emb_sjm_nest->sj_inner_tables)
                        : 0) |
          (join->allow_outer_refs ? 0 : OUTER_REF_TABLE_BIT)),
      has_sj(!(join->query_block->sj_nests.empty() || emb_sjm_nest)),
      test_all_ref_keys(false),
      found_plan_with_allowed_sj(false),
      got_final_plan(false) {}

double find_cost_for_ref(const THD *thd, TABLE *table, unsigned keyno,
                         double num_rows, double worst_seeks) {
  // Limit the number of matched rows
  num_rows = std::min(num_rows, double(thd->variables.max_seeks_for_key));
  //  The costs can be calculated only if the table is materialized.
  if (table->pos_in_table_list->is_derived_unfinished_materialization()) {
    return worst_seeks;
  }
  if (table->covering_keys.is_set(keyno)) {
    // We can use only index tree
    const Cost_estimate index_read_cost =
        table->file->index_scan_cost(keyno, 1, num_rows);
    return index_read_cost.total_cost();
  }
  if (keyno == table->s->primary_key &&
      table->file->primary_key_is_clustered()) {
    const Cost_estimate table_read_cost =
        table->file->read_cost(keyno, 1, num_rows);
    return table_read_cost.total_cost();
  }
  return min(table->file->page_read_cost(keyno, num_rows), worst_seeks);
}

/**
  Find the best index to do 'ref' access on for a table.

  The best index chosen using the following priority list
  1) A clustered primary key with equality predicates on all keyparts is
     always chosen.
  2) A non nullable unique index with equality predicates on
     all keyparts is preferred over a non-unique index,
     nullable unique index or unique index where there are some
     keyparts without equality predicates.
  3) Otherwise, the index with best cost estimate is chosen.

  As a side-effect, bound_keyparts/read_cost/fanout is set for the first
  Key_use of every considered key.

  @param tab                        the table to be joined by the function
  @param remaining_tables           set of tables not included in the
                                    partial plan yet.
  @param idx                        the index in join->position[] where 'tab'
                                    is added to the partial plan.
  @param prefix_rowcount            estimate for the number of records returned
                                    by the partial plan
  @param [out] found_condition      whether or not there exists a condition
                                    that filters away rows for this table.
                                    Always true when the function finds a
                                    usable 'ref' access, but also if it finds
                                    a condition that is not usable by 'ref'
                                    access, e.g. is there is an index covering
                                    (a,b) and there is a condition only on 'b'.
                                    Note that all dependent tables for the
                                    condition in question must be in the plan
                                    prefix for this to be 'true'. Unmodified
                                    if no relevant condition is found.
  @param [out] ref_depend_map       tables the best ref access depends on.
                                    Unmodified if no 'ref' access is found.
  @param [out] used_key_parts       Number of keyparts 'ref' access uses.
                                    Unmodified if no 'ref' access is found.

  @return pointer to Key_use for the index with best 'ref' access, NULL if
          no 'ref' access method is found.
*/
Key_use *Optimize_table_order::find_best_ref(
    const JOIN_TAB *tab, const table_map remaining_tables, const uint idx,
    const double prefix_rowcount, bool *found_condition,
    table_map *ref_depend_map, uint *used_key_parts) {
  // Skip finding best_ref if quick object is forced by hint.
  if (tab->range_scan() && get_forced_by_hint(tab->range_scan()))
    return nullptr;

  // Return value - will point to Key_use of the index with cheapest ref access
  Key_use *best_ref = nullptr;

  /*
    Cost of using best_ref; used to determine if ref access on another
    index is cheaper. Calculated as follows:

    (cost_ref_for_one_value + row_evaluate_cost(fanout_for_ref)) *
    prefix_rowcount
  */
  double best_ref_cost = DBL_MAX;

  // Index type, note that code below relies on this element definition order
  enum idx_type { CLUSTERED_PK, UNIQUE, NOT_UNIQUE, FULLTEXT };
  enum idx_type best_found_keytype = NOT_UNIQUE;

  TABLE *const table = tab->table();
  Opt_trace_context *const trace = &thd->opt_trace;

  /*
    Guessing the number of distinct values in the table; used to
    make "rec_per_key"-like estimates when no statistics is
    available.
  */
  ha_rows distinct_keys_est = tab->records() / MATCHING_ROWS_IN_OTHER_TABLE;

  // Test how we can use keys
  for (Key_use *keyuse = tab->keyuse(); keyuse->table_ref == tab->table_ref;) {
    // keyparts that are usable for this index given the current partial plan
    key_part_map found_part = 0;
    // Bitmap of keyparts where the ref access is over 'keypart=const'
    key_part_map const_part = 0;
    // Keyparts where ref access will not match on NULL values.
    // Used for unique indexes on nullable columns to decide whether
    // a specific key may match on (multiple) NULL valued rows.
    key_part_map null_rejecting_part = 0;
    /*
      Cost of ref access on current index. Calculated as follows:
      cost_ref_for_one_value * prefix_rowcount
    */
    double cur_read_cost;
    // Fanout for ref access using this index
    double cur_fanout;
    uint cur_used_keyparts = 0;  // number of used keyparts
    // tables 'ref' access on this index depends on
    table_map table_deps = 0;
    const uint key = keyuse->key;
    const KEY *const keyinfo = table->key_info + key;
    /*
      Bitmap of keyparts in this index that have a condition

        "WHERE col=... OR col IS NULL"

      If 'ref' access is to be used in such cases, the JT_REF_OR_NULL
      type will be used.
    */
    key_part_map ref_or_null_part = 0;

    DBUG_PRINT("info", ("Considering ref access on key %s", keyinfo->name));
    Opt_trace_object trace_access_idx(trace);

    enum idx_type cur_keytype =
        (keyuse->keypart == FT_KEYPART) ? FULLTEXT : NOT_UNIQUE;

    // Calculate how many key segments of the current key we can use
    Key_use *const start_key = keyuse;
    start_key->bound_keyparts = 0;  // Initially, no ref access is possible

    // For each keypart
    while (keyuse->table_ref == tab->table_ref && keyuse->key == key) {
      const uint keypart = keyuse->keypart;
      // tables the current keypart depends on
      table_map cur_keypart_table_deps = 0;
      double best_distinct_prefix_rowcount = DBL_MAX;

      /*
        Check all ways to access the keypart. There is one keyuse
        object for each equality predicate for the keypart, and this
        loop estimates which equality predicate is best. Example that
        would have two keyuse objects for a keypart covering
        t1.col_x: "WHERE t1.col_x=4 AND t1.col_x=t2.col_y"
      */
      for (; keyuse->table_ref == tab->table_ref && keyuse->key == key &&
             keyuse->keypart == keypart;
           ++keyuse) {
        /*
          This keyuse cannot be used if
          1) it is a key reference between a table inside a semijoin
             nest and one outside of it. The same applices to
             materialized subqueries
          2) it is a key reference to a table that is not in the plan
             prefix (i.e., a table that will be later in the join
             sequence)
          3) there will be two ref_or_null keyparts
             ("WHERE col=... OR col IS NULL"). Thus if
             a) the condition for an earlier keypart is of type
                ref_or_null, and
             b) the condition for the current keypart is ref_or_null
          4) The keyuse->value is a const-NULL-value and the key
             is not null_rejecting, while the index key will require a
             full TABLE_SCAN on NULL keys
             (Typically a NDB HASH index on a nullable column.)
        */
        if ((excluded_tables & keyuse->used_tables) ||        // 1)
            (remaining_tables & keyuse->used_tables) ||       // 2)
            (ref_or_null_part &&                              // 3a)
             (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)))  // 3b)
          continue;

        if (!(keyuse->used_tables & ~join->const_table_map)) {
          // keyuse can be const-evaluated, if needed, using the const tables.
          // Note that if the key is null_rejecting, it is better to still use
          // the key. RefIterator 'Late NULL filtering' eliminates the Read().
          if (!keyuse->null_rejecting &&  // 4)
              keyuse->val->is_null() &&
              (table->file->index_flags(key, 0, false) &
               HA_TABLE_SCAN_ON_NULL)) {
            continue;
          }
          const_part |= keyuse->keypart_map;
        }
        found_part |= keyuse->keypart_map;
        if (keypart != FT_KEYPART) {
          const bool keyinfo_maybe_null =
              keyinfo->key_part[keypart].field->is_nullable() ||
              tab->table()->is_nullable();
          if (keyuse->null_rejecting || !keyuse->val->is_nullable() ||
              !keyinfo_maybe_null)
            null_rejecting_part |= keyuse->keypart_map;
        }
        const double cur_distinct_prefix_rowcount =
            prev_record_reads(join, idx, (table_deps | keyuse->used_tables));
        if (cur_distinct_prefix_rowcount < best_distinct_prefix_rowcount) {
          /*
            We estimate that the currently considered usage of the
            keypart will have to lookup fewer distinct key
            combinations from the prefix tables.
          */
          cur_keypart_table_deps = keyuse->used_tables & ~join->const_table_map;
          best_distinct_prefix_rowcount = cur_distinct_prefix_rowcount;
        }
        if (distinct_keys_est > keyuse->ref_table_rows)
          distinct_keys_est = keyuse->ref_table_rows;
        /*
          If there is one 'key_column IS NULL' expression, we can
          use this ref_or_null optimisation of this field
        */
        if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
          ref_or_null_part |= keyuse->keypart_map;
      }
      table_deps |= cur_keypart_table_deps;
    }

    if (distinct_keys_est < MATCHING_ROWS_IN_OTHER_TABLE) {
      // Fix for small tables
      distinct_keys_est = MATCHING_ROWS_IN_OTHER_TABLE;
      if (tab->records() && tab->records() < distinct_keys_est)
        distinct_keys_est = tab->records();
    }

    // fulltext indexes require special treatment
    if (cur_keytype != FULLTEXT) {
      *found_condition |= (0 != found_part);

      const bool all_key_parts_covered =
          (found_part == LOWER_BITS(key_part_map, actual_key_parts(keyinfo)));
      const bool all_key_parts_non_null =
          (ref_or_null_part == 0 &&
           null_rejecting_part ==
               LOWER_BITS(key_part_map, actual_key_parts(keyinfo)));
      /*
        check for the current key type.
        If we find a key with all the keyparts having equality predicates and
        --> if it is a clustered primary key, current key type is set to
            CLUSTERED_PK.
        --> if it is non-nullable unique key, it is set as UNIQUE.
        --> If none of the specified key parts may result in NULL value(s)
            being matched, it is set as UNIQUE.
        --> otherwise its a NOT_UNIQUE keytype.
      */
      if (all_key_parts_covered && (keyinfo->flags & HA_NOSAME)) {
        if (key == table->s->primary_key &&
            table->file->primary_key_is_clustered())
          cur_keytype = CLUSTERED_PK;
        else if ((keyinfo->flags & HA_NULL_PART_KEY) == 0)
          cur_keytype = UNIQUE;
        else if (all_key_parts_non_null)
          cur_keytype = UNIQUE;
      }

      if (cur_keytype == UNIQUE || cur_keytype == CLUSTERED_PK)
        trace_access_idx.add_alnum("access_type", "eq_ref");
      else
        trace_access_idx.add_alnum("access_type", "ref");

      trace_access_idx.add_utf8("index", keyinfo->name);

      if (cur_keytype > best_found_keytype) {
        trace_access_idx.add("chosen", false)
            .add_alnum("cause", "heuristic_eqref_already_found");
        if (unlikely(!test_all_ref_keys))
          continue;
        else {
          /*
            key will be rejected further down, after we compute its
            bound_keyparts/read_cost/fanout.
          */
        }
      }

      // Check if we found full key
      if (all_key_parts_covered && !ref_or_null_part) /* use eq key */
      {
        cur_used_keyparts = (uint)~0;
        if (keyinfo->flags & HA_NOSAME &&
            ((keyinfo->flags & HA_NULL_PART_KEY) == 0 ||
             all_key_parts_non_null)) {
          cur_read_cost = prev_record_reads(join, idx, table_deps) *
                          table->cost_model()->page_read_cost(1.0);
          cur_fanout = 1.0;
        } else {
          if (!table_deps) { /* We found a const key */
            /*
              ReuseRangeEstimateForRef-1:
              We get here if we've found a ref(const) (c_i are constants):
              "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]

              If range optimizer was able to construct a "range"
              access on this index, then its condition "quick_cond" was
              equivalent to ref_const_cond (*), and we can re-use E(#rows)
              from the range optimizer.

              Proof of (*): By properties of range and ref optimizers
              quick_cond will be equal or tighter than ref_const_cond.
              ref_const_cond already covers "smallest" possible interval -
              a singlepoint interval over all keyparts. Therefore,
              quick_cond is equivalent to ref_const_cond (if it was an
              empty interval we wouldn't have got here).
            */
            if (table->quick_keys.is_set(key))
              cur_fanout = (double)table->quick_rows[key];
            else {
              // quick_range couldn't use key
              cur_fanout = (double)tab->records() / distinct_keys_est;
            }
          } else {
            // Use records per key statistics if available
            if (keyinfo->has_records_per_key(actual_key_parts(keyinfo) - 1)) {
              cur_fanout =
                  keyinfo->records_per_key(actual_key_parts(keyinfo) - 1);
            } else { /* Prefer longer keys */
              assert(table->s->max_key_length > 0);
              cur_fanout =
                  ((double)tab->records() / (double)distinct_keys_est *
                   (1.0 +
                    ((double)(table->s->max_key_length - keyinfo->key_length) /
                     (double)table->s->max_key_length)));
              if (cur_fanout < 2.0)
                cur_fanout = 2.0; /* Can't be as good as a unique */
            }

            /*
              ReuseRangeEstimateForRef-2:  We get here if we could not reuse
              E(#rows) from range optimizer. Make another try:

              If range optimizer produced E(#rows) for a prefix of the ref
              access we're considering, and that E(#rows) is lower then our
              current estimate, make an adjustment. The criteria of when we
              can make an adjustment is a special case of the criteria used
              in ReuseRangeEstimateForRef-3.
            */
            if (table->quick_keys.is_set(key) &&
                (const_part &
                 (((key_part_map)1 << table->quick_key_parts[key]) - 1)) ==
                    (((key_part_map)1 << table->quick_key_parts[key]) - 1) &&
                table->quick_n_ranges[key] == 1 &&
                cur_fanout > (double)table->quick_rows[key]) {
              cur_fanout = (double)table->quick_rows[key];
            }
          }
          cur_read_cost =
              prefix_rowcount *
              find_cost_for_ref(thd, table, key, cur_fanout, tab->worst_seeks);
        }
      } else if ((found_part & 1) &&
                 (!(table->file->index_flags(key, 0, false) &
                    HA_ONLY_WHOLE_INDEX) ||
                  all_key_parts_covered)) {
        /*
          Use as many key-parts as possible and a unique key is better
          than a not unique key.
          Set cur_fanout to (previous record count) * (records / combination)
        */

        cur_used_keyparts = max_part_bit(found_part);
        /*
          ReuseRangeEstimateForRef-3:
          We're now considering a ref[or_null] access via
          (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
          (same-as-above but with one cond replaced
          with "t.keypart_i IS NULL")]  (**)

          Try re-using E(#rows) from "range" optimizer:
          We can do so if "range" optimizer used the same intervals as
          in (**). The intervals used by range optimizer may be not
          available at this point (as "range" access might have chosen to
          create quick select over another index), so we can't compare
          them to (**). We'll make indirect judgements instead.
          The sufficient conditions for re-use are:
          (C1) All e_i in (**) are constants, i.e. table_deps==false. (if
          this is not satisfied we have no way to know which ranges
          will be actually scanned by 'ref' until we execute the
          join)
          (C2) max #key parts in 'range' access == K == max_key_part (this
          is apparently a necessary requirement)

          We also have a property that "range optimizer produces equal or
          tighter set of scan intervals than ref(const) optimizer". Each
          of the intervals in (**) are "tightest possible" intervals when
          one limits itself to using keyparts 1..K (which we do in #2).

          From here it follows that range access uses either one or
          both of the (I1) and (I2) intervals:

          (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
          (same-as-above but with one cond replaced
          with "t.keypart_i IS NULL")               (I2)

          The remaining part is to exclude the situation where range
          optimizer used one interval while we're considering
          ref-or-null and looking for estimate for two intervals. This
          is done by last limitation:

          (C3) "range optimizer used (have ref_or_null?2:1) intervals"
        */
        if (table->quick_keys.is_set(key) && !table_deps &&      //(C1)
            table->quick_key_parts[key] == cur_used_keyparts &&  //(C2)
            table->quick_n_ranges[key] ==
                1 + (ref_or_null_part ? 1 : 0))  //(C3)
        {
          cur_fanout = (double)table->quick_rows[key];
        } else {
          // Check if we have statistic about the distribution
          if (keyinfo->has_records_per_key(cur_used_keyparts - 1)) {
            cur_fanout = keyinfo->records_per_key(cur_used_keyparts - 1);

            /*
              Fix for the case where the index statistics is too
              optimistic:
              If
              (1) We're considering ref(const) and there is quick select
              on the same index,
              (2) and that quick select uses more keyparts (i.e. it will
              scan equal/smaller interval then this ref(const))
              Then use E(#rows) from quick select.

              One observation is that when there are multiple
              indexes with a common prefix (eg (b) and (b, c)) we
              are not always selecting (b, c) even when this can
              use more keyparts. Inaccuracies in statistics from
              the storage engines can cause the record estimate
              for the quick object for (b) to be lower than the
              record estimate for the quick object for (b,c).

              Q: Why do we choose to use 'ref'? Won't quick select be
              cheaper in some cases ?
              TODO: figure this out and adjust the plan choice if needed.
            */
            if (!table_deps && table->quick_keys.is_set(key) &&   // (1)
                table->quick_key_parts[key] > cur_used_keyparts)  // (2)
            {
              trace_access_idx.add("chosen", false)
                  .add_alnum("cause", "range_uses_more_keyparts");
              continue;
            }
          } else {
            /*
              Assume that the first key part matches 1% of the file
              and that the whole key matches 10 (duplicates) or 1
              (unique) records.
              Assume also that more key matches proportionally more
              records
              This gives the formula:
              records = (x * (b-a) + a*c-b)/(c-1)

              b = records matched by whole key
              a = records matched by first key part (1% of all records?)
              c = number of key parts in key
              x = used key parts (1 <= x <= c)
            */
            rec_per_key_t rec_per_key;
            if (keyinfo->has_records_per_key(keyinfo->user_defined_key_parts -
                                             1))
              rec_per_key =
                  keyinfo->records_per_key(keyinfo->user_defined_key_parts - 1);
            else
              rec_per_key =
                  rec_per_key_t(tab->records()) / distinct_keys_est + 1;

            double tmp_fanout;
            if (tab->records() == 0)
              tmp_fanout = 0.0;
            else if (rec_per_key / tab->records() >= 0.01)
              tmp_fanout = rec_per_key;
            else {
              const double a = tab->records() * 0.01;
              if (keyinfo->user_defined_key_parts > 1)
                tmp_fanout =
                    (cur_used_keyparts * (rec_per_key - a) +
                     a * keyinfo->user_defined_key_parts - rec_per_key) /
                    (keyinfo->user_defined_key_parts - 1);
              else
                tmp_fanout = a;
              tmp_fanout = std::max(tmp_fanout, 1.0);
            }
            cur_fanout = (ulong)tmp_fanout;
          }

          if (ref_or_null_part) {
            // We need to do two key searches to find key
            cur_fanout *= 2.0;
          }

          /*
            ReuseRangeEstimateForRef-4:  We get here if we could not reuse
            E(#rows) from range optimizer. Make another try:

            If range optimizer produced E(#rows) for a prefix of the ref
            access we're considering, and that E(#rows) is lower then our
            current estimate, make the adjustment.

            The decision whether we can re-use the estimate from the range
            optimizer is the same as in ReuseRangeEstimateForRef-3,
            applied to first table->quick_key_parts[key] key parts.
          */
          if (table->quick_keys.is_set(key) &&
              table->quick_key_parts[key] <= cur_used_keyparts &&
              const_part & ((key_part_map)1 << table->quick_key_parts[key]) &&
              table->quick_n_ranges[key] ==
                  1 + ((ref_or_null_part & const_part) ? 1 : 0) &&
              cur_fanout > (double)table->quick_rows[key]) {
            cur_fanout = (double)table->quick_rows[key];
          }
        }

        cur_read_cost =
            prefix_rowcount *
            find_cost_for_ref(thd, table, key, cur_fanout, tab->worst_seeks);

      } else {
        // No useful predicates on the first keypart; cannot use key
        trace_access_idx.add("usable", false).add("chosen", false);
        continue;
      }
    } else {
      // This is a full-text index

      trace_access_idx.add_alnum("access_type", "fulltext")
          .add_utf8("index", keyinfo->name);

      if (best_found_keytype < NOT_UNIQUE) {
        trace_access_idx.add("chosen", false)
            .add_alnum("cause", "heuristic_eqref_already_found");
        // Ignore test_all_ref_keys, semijoin loosescan never uses fulltext
        continue;
      }
      // Actually it should be cur_fanout=0.0 (yes!) but 1.0 is probably safer
      cur_read_cost = prev_record_reads(join, idx, table_deps) *
                      table->cost_model()->page_read_cost(1.0);
      cur_fanout = 1.0;
    }

    start_key->bound_keyparts = found_part;
    start_key->fanout = cur_fanout;
    start_key->read_cost = cur_read_cost;

    const double cur_ref_cost =
        cur_read_cost +
        prefix_rowcount * join->cost_model()->row_evaluate_cost(cur_fanout);
    trace_access_idx.add("rows", cur_fanout).add("cost", cur_ref_cost);

    /*
      The current index usage is better than the best index usage found
      so far if:

       1) The access type for the best index and the current index is
          FULLTEXT or REF, and the current index has a lower cost
       2) The access type is the same for the best index and the
          current index, and the current index has a lower cost
          (ie, both indexes are UNIQUE)
       3) The access type of the current index is better than
          that of the best index (EQ_REF better than REF, Clustered PK
          better than EQ_REF etc)
    */
    bool new_candidate = false;

    if (best_found_keytype >= NOT_UNIQUE && cur_keytype >= NOT_UNIQUE)
      new_candidate = cur_ref_cost < best_ref_cost;  // 1
    else if (best_found_keytype == cur_keytype)
      new_candidate = cur_ref_cost < best_ref_cost;  // 2
    else if (best_found_keytype > cur_keytype)
      new_candidate = true;  // 3

    if (new_candidate) {
      *ref_depend_map = table_deps;
      *used_key_parts = cur_used_keyparts;
      best_ref = start_key;
      best_ref_cost = cur_ref_cost;
      best_found_keytype = cur_keytype;
    }

    trace_access_idx.add("chosen", best_ref == start_key);

    if (best_found_keytype == CLUSTERED_PK) {
      trace_access_idx.add_alnum("cause", "clustered_pk_chosen_by_heuristics");
      if (unlikely(!test_all_ref_keys)) break;
    }
  }  // for each key

  return best_ref;
}

/**
  Calculate the cost of range/table/index scanning table 'tab'.

  Returns a hybrid scan cost number: the cost of fetching rows from
  the storage engine plus CPU cost during execution for evaluating the
  rows (estimate) that will be filtered out by predicates relevant to
  the table. The cost does not include the CPU cost during execution
  for rows that are not filtered out.

  This hybrid cost is needed because if join buffering is used to
  reduce the number of scans, then the final cost depends on how many
  times the join buffer had to be filled.

  @param tab                  the table to be joined by the function
  @param idx                  the index in join->position[] where 'tab'
                              is added to the partial plan.
  @param best_ref             description of the best ref access method
                              for 'tab'
  @param prefix_rowcount      estimate for the number of records returned
                              by the partial plan
  @param found_condition      whether or not there exists a condition
                              that filters away rows for this table.
                              @see find_best_ref()
  @param disable_jbuf         don't use join buffering if true
  @param[out] rows_after_filtering fanout of the access method after taking
                              condition filtering into account
  @param trace_access_scan    The optimizer trace object info is appended to

  @return                     Cost of fetching rows from the storage
                              engine plus CPU execution cost of the
                              rows that are estimated to be filtered out
                              by query conditions.
*/
double Optimize_table_order::calculate_scan_cost(
    const JOIN_TAB *tab, const uint idx, const Key_use *best_ref,
    const double prefix_rowcount, const bool found_condition,
    const bool disable_jbuf, double *rows_after_filtering,
    Opt_trace_object *trace_access_scan) {
  double scan_and_filter_cost;
  TABLE *const table = tab->table();
  const Cost_model_server *const cost_model = join->cost_model();
  *rows_after_filtering = static_cast<double>(tab->found_records);

  trace_access_scan->add("rows_to_scan", tab->found_records);

  /*
    This block should only affect the cost of scans using join
    buffering. Consider moving it to the if () block that handles join
    buffering.
  */
  if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_COND_FANOUT_FILTER)) {
    const float const_cond_filter = calculate_condition_filter(
        tab, nullptr, 0, static_cast<double>(tab->found_records), !disable_jbuf,
        true, *trace_access_scan);

    /*
      For high found_records values, multiplication by float may
      result in a higher value than the original for
      const_cond_filter=1.0. Cast to double to increase precision.
    */
    *rows_after_filtering = rows2double(tab->found_records) * const_cond_filter;
  } else if (table->quick_condition_rows != tab->found_records)
    *rows_after_filtering = static_cast<double>(table->quick_condition_rows);
  else if (found_condition) {
    /*
      If there is a filtering condition on the table (i.e. ref
      analyzer found at least one "table.keyXpartY= exprZ", where
      exprZ refers only to tables preceding this table in the join
      order we're now considering), and optimizer condition filtering
      is turned off, then assume that 25% of the rows will be filtered
      out by this condition.

      This heuristic is supposed to force tables used in exprZ to be
      before this table in join order.
    */

    *rows_after_filtering = tab->found_records * 0.75;
  }

  /*
    Range optimizer never proposes a RANGE if it isn't better
    than FULL: so if RANGE is present, it's always preferred to FULL.
    Here we estimate its cost.
  */
  if (tab->range_scan()) {
    trace_access_scan->add_alnum("access_type", "range");
    trace_quick_description(tab->range_scan(), &thd->opt_trace);
    /*
      For each record we:
      - read record range through 'quick'
      - skip rows which does not satisfy WHERE constraints
      TODO:
      We take into account possible use of join cache for ALL/index
      access (see first else-branch below), but we don't take it into
      account here for range/index_merge access. Find out why this is so.
    */
    scan_and_filter_cost =
        prefix_rowcount * (tab->range_scan()->cost +
                           cost_model->row_evaluate_cost(
                               tab->found_records - *rows_after_filtering));
  } else {
    trace_access_scan->add_alnum("access_type", "scan");

    // Cost of scanning the table once
    Cost_estimate scan_cost;
    if (table->force_index && !best_ref)  // index scan
      scan_cost = table->file->read_cost(tab->ref().key, 1,
                                         static_cast<double>(tab->records()));
    else
      scan_cost = table->file->table_scan_cost();  // table scan
    const double single_scan_read_cost = scan_cost.total_cost();

    /* Estimate total cost of reading table. */
    if (disable_jbuf) {
      /*
        For each record from the prefix we have to:
        - read the whole table
        - skip rows which does not satisfy join condition

        Note that there is also the cost of evaluating rows that DO
        satisfy the WHERE condition, but this is added
        a) temporarily in best_access_path(), before comparing this
           scan cost to the best 'ref' access method, and
        b) permanently by the caller of best_access_path() (@see e.g.
           best_extension_by_limited_search())
      */
      scan_and_filter_cost =
          prefix_rowcount *
          (single_scan_read_cost + cost_model->row_evaluate_cost(
                                       tab->records() - *rows_after_filtering));
    } else {
      /*
        IO cost: We read the table as many times as join buffer
        becomes full. (It would be more exact to round the result of
        the division with floor(), but that takes 5% of time in a
        20-table query plan search.)

        CPU cost: For every full join buffer, attached conditions are
        evaluated for each row in the scanned table. We assume that
        the conditions evaluate to 'true' for 'rows_after_filtering'
        number of rows. The rows that pass are then joined with the
        prefix rows.

        The CPU cost for the rows that do NOT satisfy the attached
        conditions is considered to be part of the read cost and is
        added below. The cost of joining the rows that DO satisfy the
        attached conditions with all prefix rows is added in
        greedy_search().
      */
      const double buffer_count =
          1.0 + ((double)cache_record_length(join, idx) * prefix_rowcount /
                 (double)thd->variables.join_buff_size);

      scan_and_filter_cost =
          buffer_count *
          (single_scan_read_cost + cost_model->row_evaluate_cost(
                                       tab->records() - *rows_after_filtering));

      trace_access_scan->add("using_join_cache", true);
      trace_access_scan->add(
          "buffers_needed",
          static_cast<ulonglong>(std::min(buffer_count, ULLONG_MAX_DOUBLE)));
    }
  }

  return scan_and_filter_cost;
}

/**
  If table is a lateral derived table, calculates the "cost of
  materialization", which is the cost of a single materialization (available
  in the DT's underlying JOIN final plan) multiplied by the number of rows
  output by the last-in-plan table which DT references (available in a
  POSITION structure). For example if plan is
  t1 (outputs 2 rows) - t2 (outputs 20 rows) - dt
  and dt's definition references only t1, we multiply by 2, not by 20.
  This cost is divided by the number of times the DT will be read (20, here),
  to provide a number which best_access_path() can add to best_read_cost.
*/
double Optimize_table_order::lateral_derived_cost(
    const JOIN_TAB *tab, const uint idx, const double prefix_rowcount,
    const Cost_model_server *cost_model) {
  assert(tab->table_ref->is_derived() &&
         tab->table_ref->derived_query_expression()->m_lateral_deps);
  if (prefix_rowcount == 0)  // no input rows: no materialization needed
    return 0;
  table_map deps = tab->table_ref->derived_query_expression()->m_lateral_deps;
  POSITION *positions = got_final_plan ? join->best_positions : join->positions;
  double derived_mat_cost = 0;
  for (int j = idx; j >= (int)join->const_tables; j--) {
    if (deps & join->best_ref[j]->table_ref->map()) {
      // We found the last table in plan, on which 'tab' depends.
      auto res = tab->table_ref->derived_query_expression()->query_result();
      double inner_query_cost = res->estimated_cost;
      double inner_query_rowcount = res->estimated_rowcount;
      // copied and simplified from calculate_materialization_costs()
      Cost_model_server::enum_tmptable_type tmp_table_type;
      if (tab->table()->s->reclength * inner_query_rowcount <
          thd->variables.max_heap_table_size)
        tmp_table_type = Cost_model_server::MEMORY_TMPTABLE;
      else
        tmp_table_type = Cost_model_server::DISK_TMPTABLE;
      double write_cost = cost_model->tmptable_readwrite_cost(
          tmp_table_type, inner_query_rowcount, 0.0);
      double mat_times = positions[j].prefix_rowcount;
      double total_mat_cost = mat_times * (inner_query_cost + write_cost);
      // average per read request:
      derived_mat_cost = total_mat_cost / prefix_rowcount;
      Opt_trace_context *const trace = &thd->opt_trace;
      Opt_trace_object trace_lateral(trace);
      Opt_trace_object trace_details(trace, "lateral_materialization");
      trace_details.add("cost_for_one_run_of_inner_query", inner_query_cost)
          .add("cost_for_writing_to_tmp_table", write_cost)
          .add("count_of_runs", mat_times)
          .add("total_cost", total_mat_cost)
          .add("cost_per_read", derived_mat_cost);
      break;
    }
  }
  return derived_mat_cost;
}

/**
  Find the best access path for an extension of a partial execution
  plan and add this path to the plan.

  The function finds the best access path to table 'tab' from the
  passed partial plan where an access path is the general term for any
  means to access the data in 'tab'. An access path may use either an
  index scan, a table scan, a range scan or ref access, whichever is
  cheaper. The input partial plan is passed via the array
  'join->positions' of length 'idx'. The chosen access method for
  'tab' and its cost is stored in 'join->positions[idx]'.

  @param tab               the table to be joined by the function
  @param remaining_tables  set of tables not included in the partial plan yet.
  @param idx               the index in join->position[] where 'tab' is added
                           to the partial plan.
  @param disable_jbuf      true<=> Don't use join buffering
  @param prefix_rowcount   estimate for the number of records returned by the
                           partial plan
  @param[out] pos          Table access plan
*/

void Optimize_table_order::best_access_path(JOIN_TAB *tab,
                                            const table_map remaining_tables,
                                            const uint idx, bool disable_jbuf,
                                            const double prefix_rowcount,
                                            POSITION *pos) {
  bool found_condition = false;
  bool best_uses_jbuf = false;
  Opt_trace_context *const trace = &thd->opt_trace;
  TABLE *const table = tab->table();
  const Cost_model_server *const cost_model = join->cost_model();

  float filter_effect = 1.0;

  thd->m_current_query_partial_plans++;

  /*
    Cannot use join buffering if either
     1. This is the first table in the join sequence, or
     2. Join buffering is not enabled
        (Only Block Nested Loop is considered in this context)
     3. If first-dependency-of-remaining-lateral-table < table-we-plan-for.
     Reason for 3: @see setup_join_buffering().
  */
  disable_jbuf =
      disable_jbuf || idx == join->const_tables ||  // 1
      !hint_table_state(join->thd, tab->table_ref,  // 2
                        BNL_HINT_ENUM, OPTIMIZER_SWITCH_BNL) ||
      join->deps_of_remaining_lateral_derived_tables & ~remaining_tables;  // 3

  DBUG_TRACE;

  Opt_trace_object trace_wrapper(trace, "best_access_path");
  Opt_trace_array trace_paths(trace, "considered_access_paths");

  // The 'ref' access method with lowest cost as found by find_best_ref()
  Key_use *best_ref = nullptr;

  table_map ref_depend_map = 0;
  uint used_key_parts = 0;

  // Look for the best ref access if the storage engine supports index access.
  if (tab->keyuse() != nullptr &&
      (table->file->ha_table_flags() & HA_NO_INDEX_ACCESS) == 0)
    best_ref =
        find_best_ref(tab, remaining_tables, idx, prefix_rowcount,
                      &found_condition, &ref_depend_map, &used_key_parts);

  double rows_fetched = best_ref ? best_ref->fanout : DBL_MAX;
  /*
    Cost of executing the best access method prefix_rowcount
    number of times
  */
  double best_read_cost = best_ref ? best_ref->read_cost : DBL_MAX;

  double derived_mat_cost =
      (tab->table_ref->is_derived() &&
       tab->table_ref->derived_query_expression()->m_lateral_deps)
          ? lateral_derived_cost(tab, idx, prefix_rowcount, cost_model)
          : 0;

  Opt_trace_object trace_access_scan(trace);
  /*
    Don't test table scan if it can't be better.
    Prefer key lookup if we would use the same key for scanning.

    Don't do a table scan on InnoDB tables, if we can read the used
    parts of the row from any of the used index.
    This is because table scans uses index and we would not win
    anything by using a table scan. The only exception is INDEX_MERGE
    quick select. We can not say for sure that INDEX_MERGE quick select
    is always faster than ref access. So it's necessary to check if
    ref access is more expensive.

    We do not consider index/table scan or range access if:

    1a) The best 'ref' access produces fewer records than a table scan
        (or index scan, or range access), and
    1b) The best 'ref' executed for all partial row combinations, is
        cheaper than a single scan. The rationale for comparing

        COST(ref_per_partial_row) * E(#partial_rows)
           vs
        COST(single_scan)

        is that if join buffering is used for the scan, then scan will
        not be performed E(#partial_rows) times, but
        E(#partial_rows)/E(#partial_rows_fit_in_buffer). At this point
        in best_access_path() we don't know this ratio, but it is
        somewhere between 1 and E(#partial_rows). To avoid
        overestimating the total cost of scanning, the heuristic used
        here has to assume that the ratio is 1. A more fine-grained
        cost comparison will be done later in this function.
    (2) The best way to perform table or index scan is to use 'range' access
        using index IDX. If it is a 'tight range' scan (i.e not a loose index
        scan' or 'index merge'), then ref access on the same index will
        perform equal or better if ref access can use the same or more number
        of key parts.
    (3) See above note about InnoDB.
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
             path, but there is no quick select)
        If the condition in the above brackets holds, then the only possible
        "table scan" access method is ALL/index (there is no quick select).
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
        choose it over ALL/index, there is no need to consider a full table
        scan.
  */
  if (rows_fetched < tab->found_records &&  // (1a)
      best_read_cost <= tab->read_time)     // (1b)
  {
    // "scan" means (full) index scan or (full) table scan.
    if (tab->range_scan()) {
      trace_access_scan.add_alnum("access_type", "range");
      trace_quick_description(tab->range_scan(), &thd->opt_trace);
    } else
      trace_access_scan.add_alnum("access_type", "scan");

    trace_access_scan
        .add("cost",
             tab->read_time + cost_model->row_evaluate_cost(
                                  static_cast<double>(tab->found_records)))
        .add("rows", tab->found_records)
        .add("chosen", false)
        .add_alnum("cause", "cost");
  } else if (tab->range_scan() && best_ref &&                   // (2)
             used_index(tab->range_scan()) == best_ref->key &&  // (2)
             best_ref->key != MAX_KEY &&
             used_key_parts >= table->quick_key_parts[best_ref->key] &&  // (2)
             tab->range_scan()->type != AccessPath::GROUP_INDEX_SKIP_SCAN &&
             tab->range_scan()->type != AccessPath::INDEX_SKIP_SCAN)  // (2)
  {
    trace_access_scan.add_alnum("access_type", "range");
    trace_quick_description(tab->range_scan(), &thd->opt_trace);
    trace_access_scan.add("chosen", false)
        .add_alnum("cause", "heuristic_index_cheaper");
  } else if ((table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&  //(3)
             !table->covering_keys.is_clear_all() && best_ref &&          //(3)
             (!tab->range_scan() ||                                       //(3)
              (tab->range_scan()->type ==
                   AccessPath::ROWID_INTERSECTION &&             //(3)
               best_ref->read_cost < tab->range_scan()->cost)))  //(3)
  {
    if (tab->range_scan()) {
      trace_access_scan.add_alnum("access_type", "range");
      trace_quick_description(tab->range_scan(), &thd->opt_trace);
    } else
      trace_access_scan.add_alnum("access_type", "scan");

    trace_access_scan.add("chosen", false)
        .add_alnum("cause", "covering_index_better_than_full_scan");
  } else if ((table->force_index && best_ref && !tab->range_scan()))  // (4)
  {
    trace_access_scan.add_alnum("access_type", "scan")
        .add("chosen", false)
        .add_alnum("cause", "force_index");
  } else {
    /*
      None of the heuristics found that table/index/range scan is
      obviously more expensive than 'ref' access. The 'ref' cost
      therefore has to be compared to the cost of scanning.
    */
    double rows_after_filtering;

    double scan_read_cost = calculate_scan_cost(
        tab, idx, best_ref, prefix_rowcount, found_condition, disable_jbuf,
        &rows_after_filtering, &trace_access_scan);

    /*
      We estimate the cost of evaluating WHERE clause for found
      records as row_evaluate_cost(prefix_rowcount * rows_after_filtering).
      This cost plus scan_cost gives us total cost of using
      TABLE/INDEX/RANGE SCAN.
    */
    const double scan_total_cost =
        scan_read_cost +
        cost_model->row_evaluate_cost(prefix_rowcount * rows_after_filtering);

    trace_access_scan.add("resulting_rows", rows_after_filtering);
    trace_access_scan.add("cost", scan_total_cost);

    if (best_ref == nullptr ||
        (scan_total_cost <
         best_read_cost +
             cost_model->row_evaluate_cost(prefix_rowcount * rows_fetched))) {
      /*
        If the table has a range (tab->quick is set) make_join_query_block()
        will ensure that this will be used
      */
      best_read_cost = scan_read_cost;
      rows_fetched = rows_after_filtering;

      if (tab->found_records) {
        /*
          Although join buffering may be used for this table, this
          filter calculation is not done to calculate the cost of join
          buffering itself (that is done inside
          calculate_scan_cost()). The is_join_buffering parameter is
          therefore 'false'.
        */
        const float full_filter = calculate_condition_filter(
            tab, nullptr, ~remaining_tables & ~excluded_tables,
            static_cast<double>(tab->found_records), false, false,
            trace_access_scan);
        filter_effect = static_cast<float>(std::min(
            1.0, tab->found_records * full_filter / rows_after_filtering));
      }
      best_ref = nullptr;
      best_uses_jbuf = !disable_jbuf;
      ref_depend_map = 0;
    }

    trace_access_scan.add("chosen", best_ref == nullptr);
  }

  /*
    Storage engines that track exact sizes may report an empty table
    as having row count equal to 0.
    If this table is an inner table of an outer join, adjust row count to 1,
    so that the join planner can make a better fanout calculation for
    the remaining tables of the join. (With size 0, the fanout would always
    become 0, meaning that the cost of adding one more table would also
    become 0, regardless of access method).
  */
  if (rows_fetched == 0.0 &&
      (join->query_block->outer_join & tab->table_ref->map()))
    rows_fetched = 1.0;

  /*
    Do not calculate condition filtering unless 'ref' access is
    chosen. The filtering effect for all the scan types of access
    (range/index scan/table scan) has already been calculated.
  */
  if (best_ref)
    filter_effect = calculate_condition_filter(
        tab, best_ref, ~remaining_tables & ~excluded_tables, rows_fetched,
        false, false, trace_access_scan);

  best_read_cost += derived_mat_cost;
  pos->filter_effect = filter_effect;
  pos->rows_fetched = rows_fetched;
  pos->read_cost = best_read_cost;
  pos->key = best_ref;
  pos->table = tab;
  pos->ref_depend_map = ref_depend_map;
  pos->loosescan_key = MAX_KEY;
  pos->use_join_buffer = best_uses_jbuf;

  if (!best_ref && idx == join->const_tables && table == join->sort_by_table &&
      join->query_expression()->select_limit_cnt >= rows_fetched) {
    trace_access_scan.add("use_tmp_table", true);
    join->sort_by_table = (TABLE *)1;  // Must use temporary table
  }

  if (!got_final_plan) {
    /*
      Since we have decided the table order up to 'tab', we store the
      lateral dependencies of the remaining tables so that we do not
      need to calculate it again.
    */
    pos->set_suffix_lateral_deps(
        join->deps_of_remaining_lateral_derived_tables);
  }
}

float calculate_condition_filter(const JOIN_TAB *const tab,
                                 const Key_use *const keyuse,
                                 table_map used_tables, double fanout,
                                 bool is_join_buffering, bool write_to_trace,
                                 Opt_trace_object &parent_trace) {
  /*
    Because calculating condition filtering has a cost, it should only
    be done if the filter is meaningful. It is meaningful if the query
    is an EXPLAIN, if a max_join_size has been specified, or if the
    filter may influence the QEP.

    Note that this means that EXPLAIN FOR CONNECTION will typically
    not find a calculated filtering value for the last table in a QEP
    (i.e., it will be 1.0).

    Calculate condition filter if
    1)  Condition filtering is enabled, and
    2a) Condition filtering is about to be calculated for a scan that
        might do join buffering. Rationale: When a table is scanned
        and joined with rows in a buffer, constant predicates are
        evaluated on rows in the joined table. Only rows that pass the
        constant predicates are attempted joined with the prefix rows
        in the buffer. The filtering effect is the estimate of how
        many rows pass the constant predicate evaluation.
    2b) 'tab' is not the last table that will be added to the plan.
        Rationale: filtering only reduces the number of rows sent to
        the next step in the join ordering and therefore has no effect
        on the last table in the join order, or
    2c) 'tab' is in a subselect. Rationale: for subqueries, view/table
        materializations, the filtering effect is needed to
        estimate the number of rows in the potentially materialized
        subquery, or
    2d) 'tab' is in a query_block with a semijoin nest. Rationale: the
        cost of some of the duplicate elimination strategies depends
        on the size of the output, or
    2e) The query has either an order by or group by clause and a limit clause.
        Rationale: some of the limit optimizations take the filtering effect
        on the last table into account.
    2f) Statement is EXPLAIN
    2g) max_join_size is in effect.

    Note: Even in the case of a single table query, the filtering
    effect may effect the QEP because the cost of sorting fewer rows
    is lower. This is currently ignored since single table
    optimization performance is so important.
  */
  const THD *thd = tab->join()->thd;
  TABLE *const table = tab->table();
  const table_map remaining_tables =
      ~used_tables & ~tab->table_ref->map() & tab->join()->all_table_map;
  if (!(thd->optimizer_switch_flag(
            OPTIMIZER_SWITCH_COND_FANOUT_FILTER) &&  // 1)
        (is_join_buffering ||                        // 2a
         remaining_tables != 0 ||                    // 2b
         tab->join()
                 ->query_block->master_query_expression()
                 ->outer_query_block() != nullptr ||     // 2c
         !tab->join()->query_block->sj_nests.empty() ||  // 2d
         ((!tab->join()->order.empty() || !tab->join()->group_list.empty()) &&
          tab->join()->query_expression()->select_limit_cnt !=
              HA_POS_ERROR) ||                                      // 2e
         thd->lex->is_explain() ||                                  // 2f
         !Overlaps(thd->variables.option_bits, OPTION_BIG_SELECTS)  // 2g
         )))
    return COND_FILTER_ALLPASS;

  // No filtering is calculated if we expect less than one row to be fetched
  if (fanout < 1.0 || tab->found_records < 1.0 || tab->records() < 1.0)
    return COND_FILTER_ALLPASS;

  /*
    cond_set has the column bit set for each column involved in a
    predicate. If no bits are set, there are no predicates on this
    table.
  */
  if (bitmap_is_clear_all(&table->cond_set)) return COND_FILTER_ALLPASS;

  /*
    Use TABLE::tmp_set to keep track of fields that should not
    contribute to filtering effect.
    First, verify it's not used.
  */
  assert(bitmap_is_clear_all(&table->tmp_set));

  float filter = COND_FILTER_ALLPASS;

  Opt_trace_context *const trace = &tab->join()->thd->opt_trace;

  Opt_trace_disable_I_S disable_trace(trace, !write_to_trace);
  Opt_trace_array filtering_effect_trace(trace, "filtering_effect");

  /*
    If ref/range access, the condition is already included in the
    record estimate. The fields used by the ref/range access method
    shall not contribute to the filtering estimate since 'filter' is
    percentage of fetched rows that are filtered away.
  */
  if (keyuse) {
    const KEY *key = table->key_info + keyuse->key;

    if (keyuse[0].keypart == FT_KEYPART) {
      /*
        Fulltext indexes are special because keyuse->keypart does not
        contain the keypart number but a constant (FT_KEYPART)
        defining that it is a fulltext index. However, since fulltext
        search demands that all indexed keyparts are used, iterating
        over the next 'actual_key_parts' works.
      */
      for (uint i = 0; i < key->actual_key_parts; i++)
        bitmap_set_bit(&table->tmp_set, key->key_part[i].field->field_index());
    } else {
      const Key_use *curr_ku = keyuse;

      /*
        'keyuse' describes the chosen ref access method for 'tab'. It
        is a pointer into JOIN::keyuse_array which describes all
        possible ways to perform ref access for all indexes of all
        tables. E.g., keyuse for the index "t1.idx(kp1, kp2)" and
        query condition

          "WHERE t1.kp1=1 AND t1.kp1=t2.col AND t1.kp2=2"
        will be
          [keyuse(t1.kp1,1),keyuse(t1.kp1,t2.col),keyuse(t1.kp2,2)]

        1) Since there may be multiple ways to ref-access any index it
        is not enough to look at keyuse[0..actual_key_parts-1].
        Instead, stop iterating when curr_ku no longer points to the
        specified index in 'tab'.

        2) In addition, there may be predicates that are relevant for
        an index but that will not be used by the 'ref' access (the
        keypart is not bound). This could e.g. be because the
        predicate depends on a value from a table later in the join
        sequence or because there is ref_or_null access:

          "WHERE t1.kp1=1 AND t1.kp2=t2.col"
             => t1.kp2 not used by ref since it depends on
                table later in join sequenc
          "WHERE (t1.kp1=1 OR t1.kp1 IS NULL) AND t1.kp2=2"
             => t1.kp2 not used by ref since kp1 is ref_or_null
      */
      while (curr_ku->table_ref == tab->table_ref &&         // 1)
             curr_ku->key == keyuse->key &&                  // 1)
             curr_ku->keypart_map & keyuse->bound_keyparts)  // 2)
      {
        bitmap_set_bit(&table->tmp_set,
                       key->key_part[curr_ku->keypart].field->field_index());
        curr_ku++;
      }
    }
  } else if (tab->range_scan())
    get_fields_used(tab->range_scan(), &table->tmp_set);

  /*
    Early exit if the only conditions for the table refers to columns
    used by the access method.
  */
  if (bitmap_is_subset(&table->cond_set, &table->tmp_set)) {
    assert(filter == COND_FILTER_ALLPASS);
    goto cleanup;
  }
  /*
    If the range optimizer has made row estimates for predicates that
    are not used by the chosen access method, the estimate from the
    range optimizer is used as filtering effect for those fields. We
    do this because the range optimizer is more accurate than index
    statistics.
  */
  if (!table->quick_keys.is_clear_all()) {
    char buf[MAX_FIELDS / 8];
    my_bitmap_map *bitbuf =
        static_cast<my_bitmap_map *>(static_cast<void *>(&buf));
    MY_BITMAP fields_current_quick;

    for (uint keyno = 0; keyno < table->s->keys; keyno++) {
      if (table->quick_keys.is_set(keyno)) {
        // The range optimizer made a row estimate for this index

        bitmap_init(&fields_current_quick, bitbuf, table->s->fields);

        const KEY *key = table->key_info + keyno;
        for (uint i = 0; i < table->quick_key_parts[keyno]; i++)
          bitmap_set_bit(&fields_current_quick,
                         key->key_part[i].field->field_index());

        /*
          If any of the fields used to get the rows estimate for this
          index were used to get a rows estimate for another index
          already contributing to 'filter', or by the access method we
          ignore it.
        */
        if (bitmap_is_overlapping(&table->tmp_set, &fields_current_quick))
          continue;

        bitmap_union(&table->tmp_set, &fields_current_quick);

        const float selectivity = static_cast<float>(table->quick_rows[keyno]) /
                                  static_cast<float>(tab->records());
        // Cannot possible access more rows than there are in the table
        filter *= std::min(selectivity, 1.0f);
      }
    }
  }

  /*
    Filtering effect for predicates that can be gathered from the
    range optimizer is now reflected in 'filter', and the fields of
    those predicates are set in 'tmp_set' to avoid that a
    single predicate contributes twice to 'filter'.

    Only calculate the filtering effect if
    1) There are query conditions, and
    2) At least one of the query conditions affect a field that is not
       going to be ignored in 'tab'. In other words, there has to
       exist a condition on a field that is not used by the ref/range
       access method.
  */
  if (tab->join()->where_cond &&                             // 1)
      !bitmap_is_subset(&table->cond_set, &table->tmp_set))  // 2)
  {
    /*
      Get filtering effect for predicates that are not already
      reflected in 'filter'. The below call gets this filtering effect
      based on index statistics and guesstimates.
    */
    filter *= tab->join()->where_cond->get_filtering_effect(
        tab->join()->thd, tab->table_ref->map(), used_tables, &table->tmp_set,
        static_cast<double>(tab->records()));
  }

  /*
    Cost calculations and picking the right join order assumes that a
    positive number of output rows from each joined table. We assume
    that at least one row in the table match the condition.  Not all
    code is able to cope with estimates of less than one row.  (For
    example, DupsWeedout may include extra tables in its
    duplicate-eliminating range in such cases.)
  */
  filter = max(filter, 1.0f / tab->records());

  /*
    For large tables, the restriction above may still give very small
    numbers when calculating fan-out.  The code below makes sure that
    there is a lower limit on fan-out.
    TODO: Should evaluate whether this restriction makes sense.  It
          can cause the estimated size of the result set to be
          different for different join orders. However, some unwanted
          effects on DBT-3 was observed when removing it, so keeping
          it for now.
  */
  if ((filter * fanout) < 0.05F) filter = 0.05F / static_cast<float>(fanout);

cleanup:
  filtering_effect_trace.end();
  parent_trace.add("final_filtering_effect", filter);

  // Clear tmp_set so it can be used elsewhere
  bitmap_clear_all(&table->tmp_set);
  assert(filter >= 0.0F && filter <= 1.0F);
  return filter;
}

/**
   Returns a bitmap of bound semi-join equalities.

   If we consider (oe1, .. oeN) IN (SELECT ie1, .. ieN) then ieK=oeK is
   called sj-equality. If ieK or oeK depends only on tables available before
   'tab' in this plan, then such equality is called "bound".

   @param tab                   table
   @param not_available_tables  bitmap of not-available tables.
*/
static ulonglong get_bound_sj_equalities(const JOIN_TAB *tab,
                                         table_map not_available_tables) {
  ulonglong bound_sj_equalities = 0;
  auto it_o = tab->emb_sj_nest->nested_join->sj_outer_exprs.begin();
  auto it_i = tab->emb_sj_nest->nested_join->sj_inner_exprs.begin();
  for (uint i = 0; it_o != tab->emb_sj_nest->nested_join->sj_outer_exprs.end();
       ++i, ++it_o, ++it_i) {
    Item *outer = *it_o, *inner = *it_i;
    if (!((not_available_tables)&outer->used_tables())) {
      bound_sj_equalities |= 1ULL << i;
      continue;
    }
    /*
      Now we look at equality propagation, to discover that a semi-join
      equality is bound, when the outer or inner expression is a field
      involved in some other non-semi-join equality.
      For example (propagation with inner field):
      select * from t2 where (b+0,a+0) in (select a,b from t1 where a=3);
      if the plan is t1-t2, 1st sj equality is bound, even though the
      corresponding outer expression t2.b+0 refers to 't2' which is not yet
      available.
      Other example (propagation with outer field):
      select * from t2 as t3, t2
      where t2.filler=t3.filler and
      (t2.b,t2.a,t2.filler) in (select a,b,a*3 from t1);
      if the plan is t3-t1-t2, 3rd sj equality is bound.

      We locate the relevant multiple equalities for the field. They are in
      the COND_EQUAL of the join nest which embeds the field's table. For
      example:
      select * from t1 left join t1 as t2
      on (t2.a= t1.a and (t2.a,t2.b) in (select a,b from t1 as t3))
      here we have:
      - a join nest (t2,t3) (called "wrap-nest"), which has a COND_EQUAL
      containing, among others: t2.a=t1.a
      - no COND_EQUAL for the WHERE clause.
      If the plan is t1-t3-t2, by looking at t2.a=t1.a we can deduce that
      the first semi join equality is bound.
    */
    Item *item;
    if (outer->type() == Item::FIELD_ITEM)
      item = outer;
    else if (inner->type() == Item::FIELD_ITEM)
      item = inner;
    else
      continue;
    Item_field *const item_field = static_cast<Item_field *>(item);
    Item_equal *item_equal = item_field->item_equal;
    if (!item_equal) {
      Table_ref *const nest = item_field->table_ref->outer_join_nest();
      item_equal = item_field->find_item_equal(nest ? nest->cond_equal
                                                    : tab->join()->cond_equal);
    }
    if (item_equal) {
      /*
        If the multiple equality {[optional_constant,] col1, col2...} contains
        (1) a constant
        (2) or a column from an available table
        then the semi-join equality is bound.
      */
      if (item_equal->const_arg() ||                            // (1)
          (item_equal->used_tables() & ~not_available_tables))  // (2)
        bound_sj_equalities |= 1ULL << i;
    }
  }
  return bound_sj_equalities;
}

/**
  Fills a POSITION object of the driving table of a semi-join LooseScan
  range, with the cheapest access path.

  This function was created by copying the code from best_access_path, and
  then eliminating everything which isn't related to semi-join LooseScan.

  Preconditions:
  1. Those checked by advance_sj_state(), ensuring that 'tab' is a valid
  LooseScan candidate.
  2. This function uses the members 'bound_keyparts', 'cost' and 'records' of
  each Key_use; thus best_access_path () must have been called, for this
  table, with the current join prefix, so that the members are up to date.

  @param tab               the driving table
  @param remaining_tables  set of tables not included in the partial plan yet.
  @param idx               the index in join->position[] where 'tab' is
                           added to the partial plan.
  @param prefix_rowcount   estimate for the number of records returned
                           by the partial plan
  @param[out] pos  If return code is 'true': table access path that uses
                   loosescan

  @returns true if it found a loosescan access path for this table.
*/

bool Optimize_table_order::semijoin_loosescan_fill_driving_table_position(
    const JOIN_TAB *tab, table_map remaining_tables, uint idx,
    double prefix_rowcount, POSITION *pos) {
  Opt_trace_context *const trace = &thd->opt_trace;
  Opt_trace_object trace_wrapper(trace);
  Opt_trace_object trace_ls(trace, "searching_loose_scan_index");

  TABLE *const table = tab->table();
  assert(remaining_tables & tab->table_ref->map());

  const ulonglong bound_sj_equalities =
      get_bound_sj_equalities(tab, excluded_tables | remaining_tables);

  // Use of quick select is a special case. Some of its properties:
  bool quick_uses_applicable_index = false;
  uint quick_max_keypart = 0;

  pos->read_cost = DBL_MAX;
  pos->use_join_buffer = false;
  /*
    No join buffer, so no need to manage any
    Table_map_restorer object.
    As this function calculates some read cost, we have to include any lateral
    materialization cost:
  */
  double derived_mat_cost =
      (tab->table_ref->is_derived() &&
       tab->table_ref->derived_query_expression()->m_lateral_deps)
          ? lateral_derived_cost(tab, idx, prefix_rowcount, join->cost_model())
          : 0;

  Opt_trace_array trace_all_idx(trace, "indexes");

  /*
    For each index, we calculate how many key segments of this index
    we can use.
  */
  for (Key_use *keyuse = tab->keyuse(); keyuse->table_ref == tab->table_ref;) {
    const uint key = keyuse->key;

    Key_use *const start_key = keyuse;
    Opt_trace_object trace_idx(trace);
    trace_idx.add_utf8("index", table->key_info[key].name);

    /*
      Equalities where one comparand is in index and other comparand is a
      not-yet-available expression.
    */
    ulonglong handled_sj_equalities = 0;
    key_part_map handled_keyparts = 0;
    /*
      Biggest index (starting at 0) of keyparts used for the "handled", not
      "bound", equalities.
    */
    uint max_keypart = 0;

    // For each keypart
    while (keyuse->table_ref == tab->table_ref && keyuse->key == key) {
      const uint keypart = keyuse->keypart;
      // For each way to access the keypart
      for (; keyuse->table_ref == tab->table_ref && keyuse->key == key &&
             keyuse->keypart == keypart;
           ++keyuse) {
        /*
          If this Key_use is not about a semi-join equality, or references an
          excluded table, or does not reference a not-yet-available table, or
          is for fulltext, or is over a prefix, then it is not a "handled sj
          equality".
        */
        if ((keyuse->sj_pred_no == UINT_MAX) ||
            (excluded_tables & keyuse->used_tables) ||
            !(remaining_tables & keyuse->used_tables) ||
            (keypart == FT_KEYPART) ||
            (table->key_info[key].key_part[keypart].key_part_flag &
             HA_PART_KEY_SEG))
          continue;
        handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
        handled_keyparts |= keyuse->keypart_map;
        assert(max_keypart <= keypart);  // see sort_keyuse()
        max_keypart = keypart;
      }
    }

    const key_part_map bound_keyparts = start_key->bound_keyparts;

    /*
      We can use semi-join LooseScan if duplicate elimination is going to work
      for all semi-join equalities. Duplicate elimination:
      - works for a bound semi-join equality, because this equality is tested
      before the nested loop leaves the last inner table of this semi-join
      nest.
      - works for a handled semi-join equality thanks to key comparison; key
      comparison works if:
        * the handled key parts are over a full field (not a prefix, otherwise
        two values, differing only after the prefix, would be treated as
        duplicates)
        * and any key part before the handled key parts, is bound (same
        justification as for "works for a bound semi-join equality" above).

      That gives us these requirements:
      1. All IN-equalities are either bound or handled.
      2. No hole in sequence of key parts.

      An example where (2) matters:
        SELECT * FROM ot1
        WHERE a IN (SELECT it1.b FROM it1 JOIN it2 ON it1.a = it2.a).
      Say the plan is it1-ot1-it2 and it1 has an index on (a,b). The semi-join
      equality is handled, by the second key part (it1.b). But the first key
      part is not bound (it2.a is not available). So there is a hole. If the
      rows of it1 are, in index order: (X,Z),(Y,Z), then the key comparison
      will let both rows pass; after joining with ot1 this will duplicate
      any row of ot1 having ot1.a=Z.

      We add this third requirement:
      3. At least one IN-equality is handled.
      In theory it is a superfluous restriction. Consider:
        select * from t2 as t3, t2
        where t2.b=t3.b and
              (t2.b) in (select b*3 from t1 where a=10);
      If the plan is t3-t1-t2, and we are looking at an index on t1.a:
      bound_sj_equalities==1 (because outer expression is equal to t3.b which
      is available), handled_sj_equalities==0 (no index on 'b*3'),
      handled_keyparts==0, bound_keyparts==1 (t1.a=10).
      We could set up 'ref' on t1.a (=10), with a "LooseScan key comparison
      length" (join_tab->loosescan_key_len) of size(t1.a), and a condition on
      t1 (t1->m_condition) of "t1.b*3=t3.b". After finding a match in t2
      (t2->m_condition="t2.b=t3.b"), the key comparison would skip all other
      rows of t1 returned by ref access. But this is a bit degenerate,
      FirstMatch-like.
    */
    if ((handled_sj_equalities | bound_sj_equalities) !=  // (1)
        LOWER_BITS(
            ulonglong,
            tab->emb_sj_nest->nested_join->sj_inner_exprs.size()))  // (1)
    {
      trace_idx.add("index_handles_needed_semijoin_equalities", false);
      continue;
    }
    if (handled_keyparts == 0)  // (3)
    {
      trace_idx.add("some_index_part_used", false);
      continue;
    }
    if ((LOWER_BITS(key_part_map, max_keypart + 1) &  // (2)
         ~(bound_keyparts | handled_keyparts)) != 0)  // (2)
    {
      trace_idx.add("index_can_remove_duplicates", false);
      continue;
    }

    // Ok, can use the strategy

    if (tab->range_scan() && used_index(tab->range_scan()) == key &&
        tab->range_scan()->type == AccessPath::INDEX_RANGE_SCAN) {
      quick_uses_applicable_index = true;
      quick_max_keypart = max_keypart;
    }

    if (bound_keyparts & 1) {
      Opt_trace_object trace_ref(trace, "ref");
      trace_ref.add("cost", start_key->read_cost);
      if (start_key->read_cost < pos->read_cost) {
        // @TODO use rec-per-key-based fanout calculations
        pos->loosescan_key = key;
        pos->read_cost = start_key->read_cost;
        pos->rows_fetched = start_key->fanout;
        pos->loosescan_parts = max_keypart + 1;
        pos->key = start_key;
        trace_ref.add("chosen", true);
      }
    } else if (tab->table()->covering_keys.is_set(key)) {
      /*
        There are no usable bound IN-equalities, e.g. we have

        outer_expr IN (SELECT innertbl.key FROM ...)

        and outer_expr cannot be evaluated yet, so it's actually full
        index scan and not a ref access
      */
      Opt_trace_object trace_cov_scan(trace, "covering_scan");

      // Calculate the cost of complete loose index scan.
      double rowcount = rows2double(tab->table()->file->stats.records);

      // The cost is entire index scan cost
      const double cost =
          tab->table()->file->index_scan_cost(key, 1, rowcount).total_cost();

      /*
        Now find out how many different keys we will get (for now we
        ignore the fact that we have "keypart_i=const" restriction for
        some key components, that may make us think that loose
        scan will produce more distinct records than it actually will)
      */
      if (tab->table()->key_info[key].has_records_per_key(max_keypart)) {
        const rec_per_key_t rpc =
            tab->table()->key_info[key].records_per_key(max_keypart);
        rowcount = rowcount / rpc;
      }

      trace_cov_scan.add("cost", cost);
      // @TODO: previous version also did /2
      if (cost < pos->read_cost) {
        pos->loosescan_key = key;
        pos->read_cost = cost;
        pos->rows_fetched = rowcount;
        pos->loosescan_parts = max_keypart + 1;
        pos->key = nullptr;
        trace_cov_scan.add("chosen", true);
      }
    } else
      trace_idx.add("ref_possible", false).add("covering_scan_possible", false);

  }  // ... for (Key_use *keyuse=tab->keyuse(); etc

  trace_all_idx.end();

  if (quick_uses_applicable_index && idx == join->const_tables) {
    Opt_trace_object trace_range(trace, "range_scan");
    trace_range.add("cost", tab->range_scan()->cost);
    // @TODO: this the right part restriction:
    if (tab->range_scan()->cost < pos->read_cost) {
      pos->loosescan_key = used_index(tab->range_scan());
      pos->read_cost = tab->range_scan()->cost;
      // this is ok because idx == join->const_tables
      pos->rows_fetched = tab->range_scan()->num_output_rows();
      pos->loosescan_parts = quick_max_keypart + 1;
      pos->key = nullptr;
      trace_range.add("chosen", true);
    }
  }

  if (pos->read_cost != DBL_MAX) {
    pos->read_cost += derived_mat_cost;
    pos->filter_effect = calculate_condition_filter(
        tab, pos->key, ~remaining_tables & ~excluded_tables, pos->rows_fetched,
        false, false, trace_ls);
    return true;
  }

  return false;

  // @todo need ref_depend_map ?
}

bool Join_tab_compare_default::operator()(const JOIN_TAB *jt1,
                                          const JOIN_TAB *jt2) const {
  // Sorting distinct tables, so a table should not be compared with itself
  assert(jt1 != jt2);

  if (jt1->dependent & jt2->table_ref->map()) return false;
  if (jt2->dependent & jt1->table_ref->map()) return true;

  const bool jt1_keydep_jt2 = jt1->key_dependent & jt2->table_ref->map();
  const bool jt2_keydep_jt1 = jt2->key_dependent & jt1->table_ref->map();

  if (jt1_keydep_jt2 && !jt2_keydep_jt1) return false;
  if (jt2_keydep_jt1 && !jt1_keydep_jt2) return true;

  if (jt1->found_records > jt2->found_records) return false;
  if (jt1->found_records < jt2->found_records) return true;

  return jt1 < jt2;
}

namespace {

/**
  "Less than" comparison function object used to compare two JOIN_TAB
  objects that are joined using STRAIGHT JOIN. For STRAIGHT JOINs,
  the join order is dictated by the relative order of the tables in the
  query which is reflected in JOIN_TAB::dependent. Table size and key
  dependencies are ignored here.
*/
class Join_tab_compare_straight {
 public:
  bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2) const {
    // Sorting distinct tables, so a table should not be compared with itself
    assert(jt1 != jt2);

    /*
      We don't do subquery flattening if the parent or child select has
      STRAIGHT_JOIN modifier. It is complicated to implement and the semantics
      is hardly useful.
    */
    assert(!jt1->emb_sj_nest);
    assert(!jt2->emb_sj_nest);

    if (jt1->dependent & jt2->table_ref->map()) return false;
    if (jt2->dependent & jt1->table_ref->map()) return true;

    return jt1 < jt2;
  }
};

/*
  Same as Join_tab_compare_default but tables from within the given
  semi-join nest go first. Used when optimizing semi-join
  materialization nests.
*/
class Join_tab_compare_embedded_first {
 private:
  const Table_ref *emb_nest;

 public:
  explicit Join_tab_compare_embedded_first(const Table_ref *nest)
      : emb_nest(nest) {}

  bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2) const {
    // Sorting distinct tables, so a table should not be compared with itself
    assert(jt1 != jt2);

    if (jt1->emb_sj_nest == emb_nest && jt2->emb_sj_nest != emb_nest)
      return true;
    if (jt1->emb_sj_nest != emb_nest && jt2->emb_sj_nest == emb_nest)
      return false;

    Join_tab_compare_default cmp;
    return cmp(jt1, jt2);
  }
};

}  // namespace

/**
  Selects and invokes a search strategy for an optimal query join order.

  The function checks user-configurable parameters that control the search
  strategy for an optimal plan, selects the search method and then invokes
  it. Each specific optimization procedure stores the final optimal plan in
  the array 'join->best_positions', and the cost of the plan in
  'join->best_read'.
  The function can be invoked to produce a plan for all tables in the query
  (in this case, the const tables are usually filtered out), or it can be
  invoked to produce a plan for a materialization of a semijoin nest.
  Set a non-NULL emb_sjm_nest pointer when producing a plan for a semijoin
  nest to be materialized and a NULL pointer when producing a full query plan.

  @return false if successful, true if error
*/

bool Optimize_table_order::choose_table_order() {
  DBUG_TRACE;

  got_final_plan = false;

  // Make consistent prefix cost estimates also for the const tables:
  for (uint i = 0; i < join->const_tables; i++)
    (join->positions + i)->set_prefix_cost(0.0, 1.0);

  /* Are there any tables to optimize? */
  if (join->const_tables == join->tables) {
    memcpy(join->best_positions, join->positions,
           sizeof(POSITION) * join->const_tables);
    join->best_read = 1.0;
    join->best_rowcount = 1;
    got_final_plan = true;
    return false;
  }

  join->query_block->reset_nj_counters();

  const bool straight_join =
      join->query_block->active_options() & SELECT_STRAIGHT_JOIN;
  table_map join_tables;  ///< The tables involved in order selection

  if (emb_sjm_nest) {
    /* We're optimizing semi-join materialization nest, so put the
       tables from this semi-join as first
    */
    merge_sort(join->best_ref + join->const_tables,
               join->best_ref + join->tables,
               Join_tab_compare_embedded_first(emb_sjm_nest));
    join_tables = emb_sjm_nest->sj_inner_tables;
  } else {
    /*
      if (SELECT_STRAIGHT_JOIN option is set)
        reorder tables so dependent tables come after tables they depend
        on, otherwise keep tables in the order they were specified in the query
      else
        Apply heuristic: pre-sort all access plans with respect to the number of
        records accessed.
    */
    if (straight_join)
      merge_sort(join->best_ref + join->const_tables,
                 join->best_ref + join->tables, Join_tab_compare_straight());
    else
      merge_sort(join->best_ref + join->const_tables,
                 join->best_ref + join->tables, Join_tab_compare_default());

    join_tables = join->all_table_map & ~join->const_table_map;
  }

  Opt_trace_object wrapper(&join->thd->opt_trace);
  Opt_trace_array trace_plan(&join->thd->opt_trace,
                             "considered_execution_plans",
                             Opt_trace_context::GREEDY_SEARCH);

  if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_COND_FANOUT_FILTER) &&
      join->where_cond) {
    for (uint idx = join->const_tables; idx < join->tables; ++idx)
      bitmap_clear_all(&join->best_ref[idx]->table()->cond_set);

    /*
      Set column bits for all columns involved in predicates in
      cond_set. Used to avoid calculating condition filtering in
      best_access_path() et al. when no filtering effect is possible.
    */
    join->where_cond->walk(&Item::add_field_to_cond_set_processor,
                           enum_walk::POSTFIX, nullptr);
  }

  recalculate_lateral_deps(join->const_tables);
  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);

  if (straight_join)
    optimize_straight_join(join_tables);
  else {
    if (greedy_search(join_tables)) return true;
  }

  deps_lateral.assert_unchanged();

  got_final_plan = true;

  // Remaining part of this function not needed when processing semi-join nests.
  if (emb_sjm_nest) return false;

  // Fix semi-join strategies and perform final cost calculation.
  if (fix_semijoin_strategies()) return true;

  return false;
}

/**
  Heuristic procedure to automatically guess a reasonable degree of
  exhaustiveness for the greedy search procedure.

  The procedure estimates the optimization time and selects a search depth
  big enough to result in a near-optimal QEP, that doesn't take too long to
  find. If the number of tables in the query exceeds some constant, then
  search_depth is set to this constant.

  @param search_depth Search depth value specified.
                      If zero, calculate a default value.
  @param table_count  Number of tables to be optimized (excludes const tables)

  @note
    This is an extremely simplistic implementation that serves as a stub for a
    more advanced analysis of the join. Ideally the search depth should be
    determined by learning from previous query optimizations, because it will
    depend on the CPU power (and other factors).

  @todo
    this value should be determined dynamically, based on statistics:
    uint max_tables_for_exhaustive_opt= 7;

  @todo
    this value could be determined by some mapping of the form:
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]

  @return
    A positive integer that specifies the search depth (and thus the
    exhaustiveness) of the depth-first search algorithm used by
    'greedy_search'.
*/

uint Optimize_table_order::determine_search_depth(uint search_depth,
                                                  uint table_count) {
  if (search_depth > 0) return search_depth;
  /* TODO: this value should be determined dynamically, based on statistics: */
  const uint max_tables_for_exhaustive_opt = 7;

  if (table_count <= max_tables_for_exhaustive_opt)
    search_depth =
        table_count + 1;  // use exhaustive for small number of tables
  else
    /*
      TODO: this value could be determined by some mapping of the form:
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
    */
    search_depth = max_tables_for_exhaustive_opt;  // use greedy search

  return search_depth;
}

/**
  Select the best ways to access the tables in a query without reordering them.

    Find the best access paths for each query table and compute their costs
    according to their order in the array 'join->best_ref' (thus without
    reordering the join tables). The function calls sequentially
    'best_access_path' for each table in the query to select the best table
    access method. The final optimal plan is stored in the array
    'join->best_positions', and the corresponding cost in 'join->best_read'.

  @param join_tables   set of the tables in the query

  @note
    This function can be applied to:
    - queries with STRAIGHT_JOIN
    - internally to compute the cost of an arbitrary QEP
  @par
    Thus 'optimize_straight_join' can be used at any stage of the query
    optimization process to finalize a QEP as it is.
*/

void Optimize_table_order::optimize_straight_join(table_map join_tables) {
  uint idx = join->const_tables;
  double rowcount = 1.0;
  double cost = 0.0;
  const Cost_model_server *const cost_model = join->cost_model();

  // resolve_subquery() disables semijoin if STRAIGHT_JOIN
  assert(join->query_block->sj_nests.empty());

  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);

  Opt_trace_context *const trace = &join->thd->opt_trace;
  for (JOIN_TAB **pos = join->best_ref + idx; *pos; idx++, pos++) {
    JOIN_TAB *const s = *pos;
    POSITION *const position = join->positions + idx;
    Opt_trace_object trace_table(trace);
    if (unlikely(trace->is_started())) {
      trace_plan_prefix(join, idx, excluded_tables);
      trace_table.add_utf8_table(s->table_ref);
    }
    /*
      Dependency computation (JOIN::make_join_plan()) and proper ordering
      based on them (join_tab_cmp*) guarantee that this order is compatible
      with execution, check it:
    */
    assert(!check_interleaving_with_nj(s));

    /* Find the best access method from 's' to the current partial plan */
    best_access_path(s, join_tables, idx, false, rowcount, position);

    // compute the cost of the new plan extended with 's'
    position->set_prefix_join_cost(idx, cost_model);

    position->no_semijoin();  // advance_sj_state() is not needed

    rowcount = position->prefix_rowcount;
    cost = position->prefix_cost;

    trace_table.add("condition_filtering_pct", position->filter_effect * 100)
        .add("rows_for_plan", rowcount)
        .add("cost_for_plan", cost);
    join_tables &= ~(s->table_ref->map());

    recalculate_lateral_deps_incrementally(idx + 1);
  }

  if (join->sort_by_table &&
      join->sort_by_table != join->positions[join->const_tables].table->table())
    cost += rowcount;  // We have to make a temp table

  memcpy(join->best_positions, join->positions, sizeof(POSITION) * idx);

  /**
   * If many plans have identical cost, which one will be used
   * depends on how compiler optimizes floating-point calculations.
   * this fix adds repeatability to the optimizer.
   * (Similar code in best_extension_by_li...)
   */
  join->best_read = cost - 0.001;
  join->best_rowcount = (ha_rows)rowcount;
}

/**
  Check whether a semijoin materialization strategy is allowed for
  the current (semi)join table order.

  @param join              Join object
  @param remaining_tables  Tables that have not yet been added to the join plan
  @param tab               Join_tab of the table being considered
  @param idx               Index in join->position[] with Join_tab "tab"

  @retval SJ_OPT_NONE               - Materialization not applicable
  @retval SJ_OPT_MATERIALIZE_LOOKUP - Materialization with lookup applicable
  @retval SJ_OPT_MATERIALIZE_SCAN   - Materialization with scan applicable

  @details
  The function checks applicability of both MaterializeLookup and
  MaterializeScan strategies.
  No checking is made until "tab" is pointing to the last inner table
  of a semijoin nest that can be executed using materialization -
  for all other cases SJ_OPT_NONE is returned.

  MaterializeLookup and MaterializeScan are both applicable in the following
  two cases:

   1. There are no correlated outer tables, or
   2. There are correlated outer tables within the prefix only.

  In this case, MaterializeLookup is returned based on a heuristic decision.
*/

static int semijoin_order_allows_materialization(const JOIN *join,
                                                 table_map remaining_tables,
                                                 const JOIN_TAB *tab,
                                                 uint idx) {
  assert(!(remaining_tables & tab->table_ref->map()));
  /*
   Check if
    1. We're in a semi-join nest that can be run with SJ-materialization
    2. All the tables from the subquery are in the prefix
  */
  const Table_ref *emb_sj_nest = tab->emb_sj_nest;
  if (!emb_sj_nest || !emb_sj_nest->nested_join->sjm.positions ||
      (remaining_tables & emb_sj_nest->sj_inner_tables))
    return SJ_OPT_NONE;

  /*
    Walk back and check if all immediately preceding tables are from
    this semi-join.
  */
  const uint n_tables = my_count_bits(emb_sj_nest->sj_inner_tables);
  for (uint i = 1; i < n_tables; i++) {
    if (join->positions[idx - i].table->emb_sj_nest != emb_sj_nest)
      return SJ_OPT_NONE;
  }

  /*
    Must use MaterializeScan strategy if there are outer correlated tables
    among the remaining tables, otherwise, if possible, use MaterializeLookup.
  */
  if ((remaining_tables & emb_sj_nest->nested_join->sj_depends_on) ||
      !emb_sj_nest->nested_join->sjm.lookup_allowed) {
    if (emb_sj_nest->nested_join->sjm.scan_allowed)
      return SJ_OPT_MATERIALIZE_SCAN;
    return SJ_OPT_NONE;
  }
  return SJ_OPT_MATERIALIZE_LOOKUP;
}

/**
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.

    The search procedure uses a hybrid greedy/exhaustive search with controlled
    exhaustiveness. The search is performed in N = card(remaining_tables)
    steps. Each step evaluates how promising is each of the unoptimized tables,
    selects the most promising table, and extends the current partial QEP with
    that table. Currently the most 'promising' table is the one with least
    expensive extension.\

    There are two extreme cases:
    -# When (card(remaining_tables) < search_depth), the estimate finds the
    best complete continuation of the partial QEP. This continuation can be
    used directly as a result of the search.
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
    considers the extension of the current QEP with each of the remaining
    unoptimized tables.

    All other cases are in-between these two extremes. Thus the parameter
    'search_depth' controls the exhaustiveness of the search. The higher the
    value, the longer the optimizaton time and possibly the better the
    resulting plan. The lower the value, the fewer alternative plans are
    estimated, but the more likely to get a bad QEP.

    All intermediate and final results of the procedure are stored in 'join':
    - join->positions     : modified for every partial QEP that is explored
    - join->best_positions: modified for the current best complete QEP
    - join->best_read     : modified for the current best complete QEP
    - join->best_ref      : might be partially reordered

    The final optimal plan is stored in 'join->best_positions', and its
    corresponding cost in 'join->best_read'.

  @note
    The following pseudocode describes the algorithm of 'greedy_search':

    @code
    procedure greedy_search
    input: remaining_tables
    output: pplan;
    {
      pplan = <>;
      do {
        (t, a) = best_extension(pplan, remaining_tables);
        pplan = concat(pplan, (t, a));
        remaining_tables = remaining_tables - t;
      } while (remaining_tables != {})
      return pplan;
    }

  @endcode
    where 'best_extension' is a placeholder for a procedure that selects the
    most "promising" of all tables in 'remaining_tables'.
    Currently this estimate is performed by calling
    'best_extension_by_limited_search' to evaluate all extensions of the
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
    mainly depends on that of 'best_extension_by_limited_search'.

  @par
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
    worst-case complexity of this algorithm is <=
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
    complexity of greedy_search is O(N!).
    'N' is the number of 'non eq_ref' tables + 'eq_ref groups' which normally
    are considerable less than total numbers of tables in the query.

  @par
    In the future, 'greedy_search' might be extended to support other
    implementations of 'best_extension'.

  @par
    @c search_depth from Optimize_table_order controls the exhaustiveness
    of the search, and @c prune_level controls the pruning heuristics that
    should be applied during search.

  @param remaining_tables set of tables not included into the partial plan yet

  @return false if successful, true if error
*/

bool Optimize_table_order::greedy_search(table_map remaining_tables) {
  uint idx = join->const_tables;  // index into 'join->best_ref'
  uint best_idx;
  POSITION best_pos;
  JOIN_TAB *best_table;  // the next plan node to be added to the curr QEP
  DBUG_TRACE;

  /* Number of tables that we are optimizing */
  const uint n_tables = my_count_bits(remaining_tables);

  /* Number of tables remaining to be optimized */
  uint size_remain = n_tables;
  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);
  // We should start with the lateral dependencies of all non-const JOIN_TABs.
  assert(!join->has_lateral ||
         (join->deps_of_remaining_lateral_derived_tables ==
          join->calculate_deps_of_remaining_lateral_derived_tables(
              ~excluded_tables, join->const_tables)));

  do {
    /* Find the extension of the current QEP with the lowest cost */
    join->best_read = DBL_MAX;
    join->best_rowcount = HA_POS_ERROR;
    found_plan_with_allowed_sj = false;
    if (best_extension_by_limited_search(remaining_tables, idx, search_depth))
      return true;
    /*
      'best_read < DBL_MAX' means that optimizer managed to find
      some plan and updated 'best_positions' array accordingly.
    */
    assert(join->best_read < DBL_MAX);

    if (size_remain <= search_depth || use_best_so_far) {
      /*
        'join->best_positions' contains a complete optimal extension of the
        current partial QEP.
      */
      DBUG_EXECUTE(
          "opt",
          print_plan(join, n_tables,
                     idx ? join->best_positions[idx - 1].prefix_rowcount : 1.0,
                     idx ? join->best_positions[idx - 1].prefix_cost : 0.0,
                     idx ? join->best_positions[idx - 1].prefix_cost : 0.0,
                     "optimal"););
      return false;
    }

    /* select the first table in the optimal extension as most promising */
    best_pos = join->best_positions[idx];
    best_table = best_pos.table;
    /*
      Each subsequent loop of 'best_extension_by_limited_search' uses
      'join->positions' for cost estimates, therefore we have to update its
      value.
    */
    join->positions[idx] = best_pos;

    /*
      Search depth is smaller than the number of remaining tables to join.
      - Update the interleaving state after extending the current partial plan
      with a new table. We are doing this here because
      best_extension_by_limited_search reverts the interleaving state to the
      one of the non-extended partial plan on exit.
      - The semi join state is entirely in POSITION, so it is transferred fine
      when we copy POSITION objects (no special handling needed).
      - After we have chosen the final plan covering all tables, the nested
      join state will not be reverted back to its initial state because we
      don't "pop" tables already present in the partial plan.
    */
    bool is_interleave_error [[maybe_unused]] =
        check_interleaving_with_nj(best_table);
    /* This has been already checked by best_extension_by_limited_search */
    assert(!is_interleave_error);

    /* find the position of 'best_table' in 'join->best_ref' */
    best_idx = idx;
    JOIN_TAB *pos = join->best_ref[best_idx];
    while (pos && best_table != pos) pos = join->best_ref[++best_idx];
    assert((pos != nullptr));  // should always find 'best_table'
    /*
      Maintain '#rows-sorted' order of 'best_ref[]':
       - Shift 'best_ref[]' to make first position free.
       - Insert 'best_table' at the first free position in the array of joins.
    */
    memmove(join->best_ref + idx + 1, join->best_ref + idx,
            sizeof(JOIN_TAB *) * (best_idx - idx));
    join->best_ref[idx] = best_table;

    remaining_tables &= ~(best_table->table_ref->map());

    DBUG_EXECUTE("opt",
                 print_plan(join, idx, join->positions[idx].prefix_rowcount,
                            join->positions[idx].prefix_cost,
                            join->positions[idx].prefix_cost, "extended"););

    recalculate_lateral_deps_incrementally(idx + 1);
    --size_remain;
    ++idx;
  } while (true);
}

/**
  Calculate a cost of given partial join order

  @param join               Join to use. @c positions holds the partial join
  order
  @param n_tables           Number of tables in the partial join order
  @param [out] cost_arg     Store read time here
  @param [out] rowcount_arg Store record count here

    This is needed for semi-join materialization code. The idea is that
    we detect sj-materialization after we've put all sj-inner tables into
    the join prefix

      prefix-tables semi-join-inner-tables  tN
                                             ^--we're here

    and we'll need to get the cost of prefix-tables prefix again.
*/

void get_partial_join_cost(JOIN *join, uint n_tables, double *cost_arg,
                           double *rowcount_arg) {
  double rowcount = 1.0;
  double cost = 0.0;
  const Cost_model_server *const cost_model = join->cost_model();

  for (uint i = join->const_tables; i < n_tables + join->const_tables; i++) {
    POSITION *const pos = join->best_positions + i;

    if (pos->rows_fetched > 0.0) {
      rowcount *= pos->rows_fetched;
      cost += pos->read_cost + cost_model->row_evaluate_cost(rowcount);
      rowcount *= pos->filter_effect;
    }
  }
  *cost_arg = cost;
  *rowcount_arg = rowcount;
}

/**
  Cost calculation of another (partial-)QEP has been completed.

  If this is our 'best' plan explored so far, we record this
  query plan and its cost.

  @param idx        length of the partial QEP in 'join->positions';
                    also corresponds to the current depth of the search tree;
                    also an index in the array 'join->best_ref';
  @param trace_obj  trace object where information is to be added

  @return false if successful, true if error
*/
bool Optimize_table_order::consider_plan(uint idx,
                                         Opt_trace_object *trace_obj) {
  double cost = join->positions[idx].prefix_cost;
  double sort_cost = 0;
  double windowing_cost = 0;
  /*
    We may have to make a temp table, note that this is only a
    heuristic since we cannot know for sure at this point.
    Hence it may be too pessimistic.

    @todo Windowing that uses sorting may force a sort cost both prior
    to windowing (i.e. GROUP BY) and after (i.e. ORDER BY or DISTINCT).
    In such cases we should add the cost twice here, but currently this is
    tweaked in Explain_join::shallow_explain. If would be preferable to do it
    here.
  */
  if (join->sort_by_table &&
      join->sort_by_table !=
          join->positions[join->const_tables].table->table()) {
    sort_cost = join->positions[idx].prefix_rowcount;
    cost += sort_cost;
    trace_obj->add("sort_cost", sort_cost).add("new_cost_for_plan", cost);
  }

  /*
    Check if the plan uses a disabled strategy.  (This may happen if this join
    order does not support any of the enabled strategies.)  Currently
    DuplicateWeedout is the only strategy for which this may happen.
    If we have found a previous plan with only allowed strategies,
    we only choose the current plan if it is both cheaper and does not use
    disabled strategies.  If all previous plans use a disabled strategy,
    we choose the current plan if it is either cheaper or does not use a
    disabled strategy.
  */
  bool plan_uses_allowed_sj = true;
  if (has_sj)
    for (uint i = join->const_tables; i <= idx && plan_uses_allowed_sj; i++)
      if (join->positions[i].sj_strategy == SJ_OPT_DUPS_WEEDOUT) {
        uint first = join->positions[i].first_dupsweedout_table;
        for (uint j = first; j <= i; j++) {
          Table_ref *emb_sj_nest = join->positions[j].table->emb_sj_nest;
          if (emb_sj_nest && !(emb_sj_nest->nested_join->sj_enabled_strategies &
                               OPTIMIZER_SWITCH_DUPSWEEDOUT))
            plan_uses_allowed_sj = false;
        }
      }

  bool cheaper = cost < join->best_read;
  bool chosen = found_plan_with_allowed_sj ? (plan_uses_allowed_sj && cheaper)
                                           : (plan_uses_allowed_sj || cheaper);

  /*
    If the statement is executed on a secondary engine, and the secondary engine
    has implemented a custom cost comparison function, ask the secondary engine
    to compare the cost. The secondary engine is only consulted when a complete
    join order is considered.
  */
  if (idx + 1 == join->tables) {  // this is a complete join order
    const handlerton *secondary_engine = SecondaryEngineHandlerton(thd);
    if (secondary_engine != nullptr &&
        secondary_engine->compare_secondary_engine_cost != nullptr) {
      double secondary_engine_cost;
      if (secondary_engine->compare_secondary_engine_cost(
              thd, *join, cost, &use_best_so_far, &cheaper,
              &secondary_engine_cost))
        return true;
      chosen = cheaper;
      trace_obj->add("secondary_engine_cost", secondary_engine_cost);

      // If this is the first plan seen, it must be chosen.
      assert(join->best_read != DBL_MAX || chosen);
    }
  }

  trace_obj->add("chosen", chosen);
  if (chosen) {
    if (!cheaper)
      trace_obj->add_alnum("cause", "previous_plan_used_disabled_strategy");

    memcpy((uchar *)join->best_positions, (uchar *)join->positions,
           sizeof(POSITION) * (idx + 1));

    if (join->m_windows_sort) {
      windowing_cost = Window::compute_cost(
          join->positions[idx].prefix_rowcount, join->m_windows);
      cost += windowing_cost;
      trace_obj->add("windowing_sort_cost", windowing_cost)
          .add("new_cost_for_plan", cost);
    }

    /*
      If many plans have identical cost, which one will be used
      depends on how compiler optimizes floating-point calculations.
      this fix adds repeatability to the optimizer.
      (Similar code in best_extension_by_li...)
    */
    join->best_read = cost - 0.001;
    join->best_rowcount = static_cast<ha_rows>(
        std::min(join->positions[idx].prefix_rowcount, ULLONG_MAX_DOUBLE));
    join->sort_cost = sort_cost;
    join->windowing_cost = windowing_cost;
    found_plan_with_allowed_sj = plan_uses_allowed_sj;
  } else if (cheaper)
    trace_obj->add_alnum("cause", "plan_uses_disabled_strategy");

  DBUG_EXECUTE("opt",
               print_plan(join, idx + 1, join->positions[idx].prefix_rowcount,
                          cost, cost, "full_plan"););

  return false;
}

/**
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
  exhaustive search.

    The procedure searches for the optimal ordering of the query tables in set
    'remaining_tables' of size N, and the corresponding optimal access paths to
    each table. The choice of a table order and an access path for each table
    constitutes a query execution plan (QEP) that fully specifies how to
    execute the query.

    The maximal size of the found plan is controlled by the parameter
    'search_depth'. When search_depth == N, the resulting plan is complete and
    can be used directly as a QEP. If search_depth < N, the found plan consists
    of only some of the query tables. Such "partial" optimal plans are useful
    only as input to query optimization procedures, and cannot be used directly
    to execute a query.

    The algorithm begins with an empty partial plan stored in 'join->positions'
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
    evaluates the cost of the partial plan extended by all access plans for
    each of the relations in 'remaining_tables', expands the current partial
    plan with the access plan that results in lowest cost of the expanded
    partial plan, and removes the corresponding relation from
    'remaining_tables'. The algorithm continues until it either constructs a
    complete optimal plan, or constructs an optimal plartial plan with size =
    search_depth.

    The final optimal plan is stored in 'join->best_positions'. The
    corresponding cost of the optimal plan is in 'join->best_read'.

  @note
    The procedure uses a recursive depth-first search where the depth of the
    recursion (and thus the exhaustiveness of the search) is controlled by the
    parameter 'search_depth'.

  @note
    The pseudocode below describes the algorithm of
    'best_extension_by_limited_search'. The worst-case complexity of this
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
    the complexity of greedy_search is O(N!).

  @note
    @c best_extension_by_limited_search() and @c
  eq_ref_extension_by_limited_search() are closely related to each other and
  intentionally implemented using the same pattern wherever possible. If a
  change/bug fix is done to either of these also consider if it is relevant for
  the other.

    @code
    procedure best_extension_by_limited_search(
      pplan in,             // in, partial plan of tables-joined-so-far
      pplan_cost,           // in, cost of pplan
      remaining_tables,     // in, set of tables not referenced in pplan
      best_plan_so_far,     // in/out, best plan found so far
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
      search_depth)         // in, maximum size of the plans being considered
    {
      for each table T from remaining_tables
      {
        // Calculate the cost of using table T as above
        cost = complex-series-of-calculations;

        // Add the cost to the cost so far.
        pplan_cost+= cost;

        if (pplan_cost >= best_plan_so_far_cost)
          // pplan_cost already too great, stop search
          continue;

        pplan= expand pplan by best_access_method;
        remaining_tables= remaining_tables - table T;
        if (remaining_tables is not an empty set
            and
            search_depth > 1)
        {
          if (table T is EQ_REF-joined)
            eq_ref_eq_ref_extension_by_limited_search(
                                             pplan, pplan_cost,
                                             remaining_tables,
                                             best_plan_so_far,
                                             best_plan_so_far_cost,
                                             search_depth - 1);

          else
            best_extension_by_limited_search(pplan, pplan_cost,
                                             remaining_tables,
                                             best_plan_so_far,
                                             best_plan_so_far_cost,
                                             search_depth - 1);
        }
        else
        {
          best_plan_so_far_cost= pplan_cost;
          best_plan_so_far= pplan;
        }
      }
    }
    @endcode

  @note
    The arguments pplan, plan_cost, best_plan_so_far and best_plan_so_far_cost
    are actually found in the POSITION object.

  @note
    When 'best_extension_by_limited_search' is called for the first time,
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
    The actual implementation provides a way to optionally use pruning
    heuristic (controlled by the parameter 'prune_level') to reduce the search
    space by skipping some partial plans.

  @note
    The parameter 'search_depth' provides control over the recursion
    depth, and thus the size of the resulting optimal plan.

  @param remaining_tables set of tables not included into the partial plan yet
  @param idx              length of the partial QEP in 'join->positions';
                          since a depth-first search is used, also corresponds
                          to the current depth of the search tree;
                          also an index in the array 'join->best_ref';
  @param current_search_depth  maximum depth of recursion and thus size of the
                          found optimal plan
                          (0 < current_search_depth <= join->tables+1).

  @return false if successful, true if error
*/

bool Optimize_table_order::best_extension_by_limited_search(
    table_map remaining_tables, uint idx, uint current_search_depth) {
  DBUG_TRACE;

  DBUG_EXECUTE_IF("bug13820776_2", thd->killed = THD::KILL_QUERY;);
  if (thd->killed)  // Abort
    return true;

  const Cost_model_server *const cost_model = join->cost_model();
  Opt_trace_context *const trace = &thd->opt_trace;

  /*
     'join' is a partial plan with lower cost than the best plan so far,
     so continue expanding it further with the tables in 'remaining_tables'.
  */
  double best_rowcount = DBL_MAX;
  double best_cost = DBL_MAX;

  DBUG_EXECUTE("opt",
               print_plan(join, idx,
                          idx ? join->positions[idx - 1].prefix_rowcount : 1.0,
                          idx ? join->positions[idx - 1].prefix_cost : 0.0,
                          idx ? join->positions[idx - 1].prefix_cost : 0.0,
                          "part_plan"););

  /*
    'eq_ref_extended' are the 'remaining_tables' which has already been
    involved in an partial query plan extension if this QEP. These
    will not be considered in further EQ_REF extensions based
    on current (partial) QEP.
  */
  table_map eq_ref_extended(0);

  JOIN_TAB *saved_refs[MAX_TABLES];
  // Save 'best_ref[]' as we has to restore before return.
  memcpy(saved_refs, join->best_ref + idx,
         sizeof(JOIN_TAB *) * (join->tables - idx));

  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);

  for (JOIN_TAB **pos = join->best_ref + idx; *pos && !use_best_so_far; pos++) {
    JOIN_TAB *const s = *pos;
    const table_map real_table_bit = s->table_ref->map();

    /*
      Don't move swap inside conditional code: All items should
      be uncond. swapped to maintain '#rows-ordered' best_ref[].
      This is critical for early pruning of bad plans.
    */
    std::swap(join->best_ref[idx], *pos);

    if ((remaining_tables & real_table_bit) &&
        !(eq_ref_extended & real_table_bit) &&
        !(remaining_tables & s->dependent) &&
        (!idx || !check_interleaving_with_nj(s))) {
      Opt_trace_object trace_one_table(trace);
      if (unlikely(trace->is_started())) {
        trace_plan_prefix(join, idx, excluded_tables);
        trace_one_table.add_utf8_table(s->table_ref);
      }
      POSITION *const position = join->positions + idx;

      // If optimizing a sj-mat nest, tables in this plan must be in nest:
      assert(emb_sjm_nest == nullptr || emb_sjm_nest == s->emb_sj_nest);

      deps_lateral.restore();  // as we "popped" the previously-tried table

      /* Find the best access method from 's' to the current partial plan */
      best_access_path(s, remaining_tables, idx, false,
                       idx ? (position - 1)->prefix_rowcount : 1.0, position);

      // Compute the cost of extending the plan with 's'
      position->set_prefix_join_cost(idx, cost_model);

      trace_one_table
          .add("condition_filtering_pct", position->filter_effect * 100)
          .add("rows_for_plan", position->prefix_rowcount)
          .add("cost_for_plan", position->prefix_cost);

      if (has_sj) {
        /*
          Even if there are no semijoins, advance_sj_state() has a significant
          cost (takes 9% of time in a 20-table plan search), hence the if()
          above, which is also more efficient than the same if() inside
          advance_sj_state() would be.
          Besides, never call advance_sj_state() when calculating the plan
          for a materialized semi-join nest.
        */
        advance_sj_state(remaining_tables, s, idx);
      } else
        position->no_semijoin();

      /*
        Expand only partial plans with lower cost than the best QEP so far.
        However, if the best plan so far uses a disabled semi-join strategy,
        we continue the search since this partial plan may support other
        semi-join strategies.
      */
      if (position->prefix_cost >= join->best_read &&
          found_plan_with_allowed_sj) {
        DBUG_EXECUTE("opt",
                     print_plan(join, idx + 1, position->prefix_rowcount,
                                position->read_cost, position->prefix_cost,
                                "prune_by_cost"););
        trace_one_table.add("pruned_by_cost", true);
        backout_nj_state(remaining_tables, s);
        continue;
      }

      /*
        Prune some less promising partial plans. This heuristic may miss
        the optimal QEPs, thus it results in a non-exhaustive search.
      */
      if (prune_level == 1) {
        if (best_rowcount > position->prefix_rowcount ||
            best_cost > position->prefix_cost ||
            (idx == join->const_tables &&  // 's' is the first table in the QEP
             s->table() == join->sort_by_table)) {
          if (best_rowcount >= position->prefix_rowcount &&
              best_cost >= position->prefix_cost &&
              /* TODO: What is the reasoning behind this condition? */
              (!(s->key_dependent & remaining_tables) ||
               position->rows_fetched < 2.0)) {
            best_rowcount = position->prefix_rowcount;
            best_cost = position->prefix_cost;
          }
        } else if (found_plan_with_allowed_sj) {
          DBUG_EXECUTE("opt",
                       print_plan(join, idx + 1, position->prefix_rowcount,
                                  position->read_cost, position->prefix_cost,
                                  "pruned_by_heuristic"););
          trace_one_table.add("pruned_by_heuristic", true);
          backout_nj_state(remaining_tables, s);
          continue;
        }
      }

      recalculate_lateral_deps_incrementally(idx + 1);

      const table_map remaining_tables_after =
          (remaining_tables & ~real_table_bit);
      if ((current_search_depth > 1) && remaining_tables_after) {
        /*
          Explore more extensions of plan:
          If possible, use heuristic to avoid a full expansion of partial QEP.
          Evaluate a simplified EQ_REF extension of QEP if:
            1) Pruning is enabled.
            2) and, There are tables joined by (EQ_)REF key.
            3) and, There is a 1::1 relation between those tables
        */
        if (prune_level == 1 &&             // 1)
            position->key != nullptr &&     // 2)
            position->rows_fetched <= 1.0)  // 3)
        {
          /*
            Join in this 'position' is an EQ_REF-joined table, append more
            EQ_REFs. We do this only for the first EQ_REF we encounter which
            will then include other EQ_REFs from 'remaining_tables' and inform
            about which tables was 'eq_ref_extended'. These are later 'pruned'
            as they was processed here.
          */
          if (eq_ref_extended == (table_map)0) {
            /* Try an EQ_REF-joined expansion of the partial plan */
            Opt_trace_array trace_rest(trace, "rest_of_plan");
            eq_ref_extended =
                real_table_bit |
                eq_ref_extension_by_limited_search(
                    remaining_tables_after, idx + 1, current_search_depth - 1);
            if (eq_ref_extended == ~(table_map)0) return true;  // Failed

            backout_nj_state(remaining_tables, s);

            if (eq_ref_extended == remaining_tables) goto done;

            continue;
          } else  // Skip, as described above
          {
            DBUG_EXECUTE("opt",
                         print_plan(join, idx + 1, position->prefix_rowcount,
                                    position->read_cost, position->prefix_cost,
                                    "pruned_by_eq_ref_heuristic"););
            trace_one_table.add("pruned_by_eq_ref_heuristic", true);
            backout_nj_state(remaining_tables, s);
            continue;
          }
        }  // if (prunable...)

        /* Fallthrough: Explore more best extensions of plan */
        Opt_trace_array trace_rest(trace, "rest_of_plan");
        if (best_extension_by_limited_search(remaining_tables_after, idx + 1,
                                             current_search_depth - 1))
          return true;
      } else  // if ((current_search_depth > 1) && ...
      {
        if (consider_plan(idx, &trace_one_table)) return true;
        /*
          If plan is complete, there should be no "open" outer join nest, and
          all semi join nests should be handled by a strategy:
        */
        assert((remaining_tables_after != 0) ||
               ((cur_embedding_map == 0) &&
                (join->positions[idx].dups_producing_tables == 0) &&
                (join->deps_of_remaining_lateral_derived_tables == 0)));
      }
      backout_nj_state(remaining_tables, s);
    }
  }

done:
  // Restore previous #rows sorted best_ref[]
  memcpy(join->best_ref + idx, saved_refs,
         sizeof(JOIN_TAB *) * (join->tables - idx));
  return false;
}

/**
  Helper function that compares two doubles and accept these as
  "almost equal" if they are within 10 percent of each other.

  Handling of exact 0.0 values: if one of the values are exactly 0.0, the
  other value must also be exactly 0.0 to be considered to be equal.

  @param left  First double number to compare
  @param right Second double number to compare

  @return true if the two numbers are almost equal, false otherwise.
*/

static inline bool almost_equal(double left, double right) {
  const double boundary = 0.1;  // 10 percent limit
  return ((left >= right * (1.0 - boundary)) &&
          (left <= right * (1.0 + boundary)));
}

/**
  Heuristic utility used by best_extension_by_limited_search().
  Adds EQ_REF-joined tables to the partial plan without
  extensive 'greedy' cost calculation.

  When a table is joined by an unique key there is a
  1::1 relation between the rows being joined. Assuming we
  have multiple such 1::1 (star-)joined relations in a
  sequence, without other join types in between. Then all of
  these 'eq_ref-joins' will be estimated to return the exact
  same number of rows and having identical 'cost' (or 'read_time').

  This leads to that we can append such a contiguous sequence
  of eq_ref-joins to a partial plan in any order without
  affecting the total cost of the query plan. Exploring the
  different permutations of these eq_refs in the 'greedy'
  optimizations will simply be a waste of precious CPU cycles.

  Once we have appended a single eq_ref-join to a partial
  plan, we may use eq_ref_extension_by_limited_search() to search
  'remaining_tables' for more eq_refs which will form a contiguous
  set of eq_refs in the QEP.

  Effectively, this chain of eq_refs will be handled as a single
  entity wrt. the full 'greedy' exploration of the possible
  join plans. This will reduce the 'N' in the O(N!) complexity
  of the full greedy search.

  The algorithm start by already having a eq_ref joined table
  in position[idx-1] when called. It then search for more
  eq_ref-joinable 'remaining_tables' which are added directly
  to the partial QEP without further cost analysis. The algorithm
  continues until it either has constructed a complete plan,
  constructed a partial plan with size = search_depth, or could not
  find more eq_refs to append.

  In the later case the algorithm continues into
  'best_extension_by_limited_search' which does a 'greedy'
  search for the next table to add - Possibly with later
  eq_ref_extensions.

  The final optimal plan is stored in 'join->best_positions'. The
  corresponding cost of the optimal plan is in 'join->best_read'.

  @note
    @c best_extension_by_limited_search() and @c
  eq_ref_extension_by_limited_search() are closely related to each other and
  intentionally implemented using the same pattern wherever possible. If a
  change/bug fix is done to either of these also consider if it is relevant for
  the other.

  @code
    procedure eq_ref_extension_by_limited_search(
      pplan in,             // in, partial plan of tables-joined-so-far
      pplan_cost,           // in, cost of pplan
      remaining_tables,     // in, set of tables not referenced in pplan
      best_plan_so_far,     // in/out, best plan found so far
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
      search_depth)         // in, maximum size of the plans being considered
    {
      if find 'eq_ref' table T from remaining_tables
      {
        // Calculate the cost of using table T as above
        cost = complex-series-of-calculations;

        // Add the cost to the cost so far.
        pplan_cost+= cost;

        if (pplan_cost >= best_plan_so_far_cost)
          // pplan_cost already too great, stop search
          continue;

        pplan= expand pplan by best_access_method;
        remaining_tables= remaining_tables - table T;
        eq_ref_extension_by_limited_search(pplan, pplan_cost,
                                           remaining_tables,
                                           best_plan_so_far,
                                           best_plan_so_far_cost,
                                           search_depth - 1);
      }
      else
      {
        best_extension_by_limited_search(pplan, pplan_cost,
                                         remaining_tables,
                                         best_plan_so_far,
                                         best_plan_so_far_cost,
                                         search_depth - 1);
      }
    }
    @endcode

  @note
    The parameter 'search_depth' provides control over the recursion
    depth, and thus the size of the resulting optimal plan.

  @param remaining_tables set of tables not included into the partial plan yet
  @param idx              length of the partial QEP in 'join->positions';
                          since a depth-first search is used, also corresponds
                          to the current depth of the search tree;
                          also an index in the array 'join->best_ref';
  @param current_search_depth
                          maximum depth of recursion and thus size of the
                          found optimal plan
                          (0 < current_search_depth <= join->tables+1).

  @retval
    'table_map'          Map of those tables appended to the EQ_REF-joined
  sequence
  @retval
    ~(table_map)0        Fatal error
*/

table_map Optimize_table_order::eq_ref_extension_by_limited_search(
    table_map remaining_tables, uint idx, uint current_search_depth) {
  DBUG_TRACE;

  if (remaining_tables == 0) return 0;

  /*
    The section below adds 'eq_ref' joinable tables to the QEP in the order
    they are found in the 'remaining_tables' set.
    See above description for why we can add these without greedy
    cost analysis.
  */
  Opt_trace_context *const trace = &thd->opt_trace;
  table_map eq_ref_ext(0);
  JOIN_TAB *s;
  JOIN_TAB *saved_refs[MAX_TABLES];
  // Save 'best_ref[]' as we has to restore before return.
  memcpy(saved_refs, join->best_ref + idx,
         sizeof(JOIN_TAB *) * (join->tables - idx));

  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);

  for (JOIN_TAB **pos = join->best_ref + idx; (s = *pos); pos++) {
    const table_map real_table_bit = s->table_ref->map();

    /*
      Don't move swap inside conditional code: All items
      should be swapped to maintain '#rows' ordered tables.
      This is critical for early pruning of bad plans.
    */
    std::swap(join->best_ref[idx], *pos);

    /*
      Consider table for 'eq_ref' heuristic if:
        1)      It might use a keyref for best_access_path
        2) and, Table remains to be handled.
        3) and, It is independent of those not yet in partial plan.
        4) and, It is key dependent on at least one already handled table
        5) and, It passed the interleaving check.
    */
    if (s->keyuse() &&                             // 1)
        (remaining_tables & real_table_bit) &&     // 2)
        !(remaining_tables & s->dependent) &&      // 3)
        (~remaining_tables & s->key_dependent) &&  // 4)
        (!idx || !check_interleaving_with_nj(s)))  // 5)
    {
      Opt_trace_object trace_one_table(trace);
      if (unlikely(trace->is_started())) {
        trace_plan_prefix(join, idx, excluded_tables);
        trace_one_table.add_utf8_table(s->table_ref);
      }
      POSITION *const position = join->positions + idx;

      assert(emb_sjm_nest == nullptr || emb_sjm_nest == s->emb_sj_nest);

      deps_lateral.restore();

      /* Find the best access method from 's' to the current partial plan */
      best_access_path(s, remaining_tables, idx, false,
                       idx ? (position - 1)->prefix_rowcount : 1.0, position);

      /*
        EQ_REF prune logic is based on that all joins
        in the ref_extension has the same #rows and cost.
        -> The total cost of the QEP is independent of the order
           of joins within this 'ref_extension'.
           Expand QEP with all 'identical' REFs in
          'join->positions' order.
        Note that due to index statistics from the storage engines
        is a floating point number and might not be exact, the
        rows and cost estimates for eq_ref on two tables might not
        be the exact same number.
        @todo This test could likely be re-implemented to use
        information about whether the index is unique or not.
      */
      const bool added_to_eq_ref_extension =
          position->key &&
          almost_equal(position->read_cost, (position - 1)->read_cost) &&
          almost_equal(position->rows_fetched, (position - 1)->rows_fetched);
      trace_one_table.add("added_to_eq_ref_extension",
                          added_to_eq_ref_extension);
      if (added_to_eq_ref_extension) {
        // Add the cost of extending the plan with 's'
        position->set_prefix_join_cost(idx, join->cost_model());

        trace_one_table
            .add("condition_filtering_pct", position->filter_effect * 100)
            .add("rows_for_plan", position->prefix_rowcount)
            .add("cost_for_plan", position->prefix_cost);

        if (has_sj) {
          /*
            Even if there are no semijoins, advance_sj_state() has a
            significant cost (takes 9% of time in a 20-table plan search),
            hence the if() above, which is also more efficient than the
            same if() inside advance_sj_state() would be.
          */
          advance_sj_state(remaining_tables, s, idx);
        } else
          position->no_semijoin();

        // Expand only partial plans with lower cost than the best QEP so far
        if (position->prefix_cost >= join->best_read) {
          DBUG_EXECUTE("opt",
                       print_plan(join, idx + 1, position->prefix_rowcount,
                                  position->read_cost, position->prefix_cost,
                                  "prune_by_cost"););
          trace_one_table.add("pruned_by_cost", true);
          backout_nj_state(remaining_tables, s);
          continue;
        }

        recalculate_lateral_deps_incrementally(idx + 1);

        eq_ref_ext = real_table_bit;
        const table_map remaining_tables_after =
            (remaining_tables & ~real_table_bit);
        if ((current_search_depth > 1) && remaining_tables_after) {
          DBUG_EXECUTE("opt",
                       print_plan(join, idx + 1, position->prefix_rowcount,
                                  position->read_cost, position->prefix_cost,
                                  "EQ_REF_extension"););

          /* Recursively EQ_REF-extend the current partial plan */
          Opt_trace_array trace_rest(trace, "rest_of_plan");
          eq_ref_ext |= eq_ref_extension_by_limited_search(
              remaining_tables_after, idx + 1, current_search_depth - 1);
        } else {
          if (consider_plan(idx, &trace_one_table)) return ~(table_map)0;
          assert((remaining_tables_after != 0) ||
                 ((cur_embedding_map == 0) &&
                  (join->positions[idx].dups_producing_tables == 0) &&
                  (join->deps_of_remaining_lateral_derived_tables == 0)));
        }
        backout_nj_state(remaining_tables, s);
        memcpy(join->best_ref + idx, saved_refs,
               sizeof(JOIN_TAB *) * (join->tables - idx));
        return eq_ref_ext;
      }  // if (added_to_eq_ref_extension)

      backout_nj_state(remaining_tables, s);
    }  // if (... !check_interleaving_with_nj() ...)
  }    // for (JOIN_TAB **pos= ...)

  memcpy(join->best_ref + idx, saved_refs,
         sizeof(JOIN_TAB *) * (join->tables - idx));
  deps_lateral.restore();
  /*
    'eq_ref' heuristic didn't find a table to be appended to
    the query plan. We need to use the greedy search
    for finding the next table to be added.
  */
  assert(!eq_ref_ext);
  if (best_extension_by_limited_search(remaining_tables, idx,
                                       current_search_depth))
    return ~(table_map)0;

  return eq_ref_ext;
}

/*
  Get the number of different row combinations for subset of partial join

  SYNOPSIS
    prev_record_reads()
      join       The join structure
      idx        Number of tables in the partial join order (i.e. the
                 partial join order is in join->positions[0..idx-1])
      found_ref  Bitmap of tables for which we need to find # of distinct
                 row combinations.

  DESCRIPTION
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
    tables within that join order (specified in found_ref), find out how many
    distinct row combinations of subset tables will be in the result of the
    partial join order.

    This is used as follows: Suppose we have a table accessed with a ref-based
    method. The ref access depends on current rows of tables in found_ref.
    We want to count # of different ref accesses. We assume two ref accesses
    will be different if at least one of access parameters is different.
    Example: consider a query

    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field

    and a join order:
      t1,  ref access on t1.key=c1
      t2,  ref access on t2.key=c2
      t3,  ref access on t3.key=t1.field

    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
    For t2: n_ref_scans = fanout(t1), n_distinct_ref_scans=1
    For t3: n_ref_scans = fanout(t1)*fanout(t2)
            n_distinct_ref_scans = #fanout(t1)

    Here "fanout(tx)" is the number of rows read by the access method
    of tx minus rows filtered out by condition filtering
    (pos->filter_effect).

    The reason for having this function (at least the latest version of it)
    is that we need to account for buffering in join execution.

    An edge-case example: if we have a non-first table in join accessed via
    ref(const) or ref(param) where there is a small number of different
    values of param, then the access will likely hit the disk cache and will
    not require any disk seeks.

    The proper solution would be to assume an LRU disk cache of some size,
    calculate probability of cache hits, etc. For now we just count
    identical ref accesses as one.

  RETURN
    Expected number of row combinations
*/

static double prev_record_reads(JOIN *join, uint idx, table_map found_ref) {
  double found = 1.0;
  POSITION *pos_end = join->positions - 1;
  for (POSITION *pos = join->positions + idx - 1; pos != pos_end; pos--) {
    const double fanout = pos->rows_fetched * pos->filter_effect;
    if (pos->table->table_ref->map() & found_ref) {
      found_ref |= pos->ref_depend_map;
      /*
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
        with no matching row we will get position[t2].rows_fetched==0.
        Actually the size of output is one null-complemented row, therefore
        we will use value of 1 whenever we get rows_fetched==0.

        Note
        - the above case can't occur if inner part of outer join has more
          than one table: table with no matches will not be marked as const.

        - Ideally we should add 1 to rows_fetched for every possible null-
          complemented row. We're not doing it because: 1. it will require
          non-trivial code and add overhead. 2. The value of rows_fetched
          is an inprecise estimate and adding 1 (or, in the worst case,
          #max_nested_outer_joins=64-1) will not make it any more precise.
      */
      if (pos->rows_fetched > DBL_EPSILON) found *= fanout;
    } else if (fanout < 1.0) {
      /*
        With condition filtering it is possible that a table has a
        lower fanout than 1.0. If so, calculate the fanout of this
        table into the found rows estimate so the produced number is
        not too pessimistic. Otherwise, the expected number of row
        combinations returned by this function may be higher than the
        prefix_rowcount for the table. See BUG#18352936
      */
      found *= fanout;
    }
  }
  return found;
}

/**
  @brief Fix semi-join strategies for the picked join order

  @return false if success, true if error

  @details
    Fix semi-join strategies for the picked join order. This is a step that
    needs to be done right after we have fixed the join order. What we do
    here is switch join's semi-join strategy description from backward-based
    to forwards based.

    When join optimization is in progress, we re-consider semi-join
    strategies after we've added another table. Here's an illustration.
    Suppose the join optimization is underway:

    1) ot1  it1  it2
                 sjX  -- looking at (ot1, it1, it2) join prefix, we decide
                         to use semi-join strategy sjX.

    2) ot1  it1  it2  ot2
                 sjX  sjY -- Having added table ot2, we now may consider
                             another semi-join strategy and decide to use a
                             different strategy sjY. Note that the record
                             of sjX has remained under it2. That is
                             necessary because we need to be able to get
                             back to (ot1, it1, it2) join prefix.
      what makes things even worse is that there are cases where the choice
      of sjY changes the way we should access it2.

    3) [ot1  it1  it2  ot2  ot3]
                  sjX  sjY  -- This means that after join optimization is
                               finished, semi-join info should be read
                               right-to-left (while nearly all plan refinement
                               functions, EXPLAIN, etc proceed from left to
                               right)

    This function does the needed reversal, making it possible to read the
    join and semi-join order from left to right.
*/

bool Optimize_table_order::fix_semijoin_strategies() {
  table_map remaining_tables = 0;
  table_map handled_tables = 0;

  DBUG_TRACE;

  if (join->query_block->sj_nests.empty()) return false;

  Opt_trace_context *const trace = &thd->opt_trace;

  for (uint tableno = join->tables - 1; tableno != join->const_tables - 1;
       tableno--) {
    POSITION *const pos = join->best_positions + tableno;

    if ((handled_tables & pos->table->table_ref->map()) ||
        pos->sj_strategy == SJ_OPT_NONE) {
      remaining_tables |= pos->table->table_ref->map();
      continue;
    }

    uint first = 0;
    if (pos->sj_strategy == SJ_OPT_MATERIALIZE_LOOKUP) {
      Table_ref *const sjm_nest = pos->table->emb_sj_nest;
      const uint table_count = my_count_bits(sjm_nest->sj_inner_tables);
      /*
        This memcpy() copies a partial QEP produced by
        optimize_semijoin_nests_for_materialization() (source) into the final
        top-level QEP (target), in order to re-use the source plan for
        to-be-materialized inner tables.
        It is however possible that the source QEP had picked
        some semijoin strategy (noted SJY), different from
        materialization. The target QEP rules (it has seen more tables), but
        this memcpy() is going to copy the source stale strategy SJY,
        wrongly. Which is why sj_strategy of each table of the
        duplicate-generating range then becomes temporarily unreliable. It is
        fixed for the first table of that range right after the memcpy(), and
        fixed for the rest of that range at the end of this iteration by
        setting it to SJ_OPT_NONE). But until then, pos->sj_strategy should
        not be read.
      */
      memcpy(pos - table_count + 1, sjm_nest->nested_join->sjm.positions,
             sizeof(POSITION) * table_count);
      first = tableno - table_count + 1;
      join->best_positions[first].n_sj_tables = table_count;
      join->best_positions[first].sj_strategy = SJ_OPT_MATERIALIZE_LOOKUP;

      Opt_trace_object trace_final_strategy(trace);
      trace_final_strategy.add_alnum("final_semijoin_strategy",
                                     "MaterializeLookup");
    } else if (pos->sj_strategy == SJ_OPT_MATERIALIZE_SCAN) {
      const uint last_inner = pos->sjm_scan_last_inner;
      Table_ref *const sjm_nest =
          (join->best_positions + last_inner)->table->emb_sj_nest;
      const uint table_count = my_count_bits(sjm_nest->sj_inner_tables);
      first = last_inner - table_count + 1;
      assert((join->best_positions + first)->table->emb_sj_nest == sjm_nest);
      memcpy(join->best_positions + first,  // stale semijoin strategy here too
             sjm_nest->nested_join->sjm.positions,
             sizeof(POSITION) * table_count);
      join->best_positions[first].sj_strategy = SJ_OPT_MATERIALIZE_SCAN;
      join->best_positions[first].n_sj_tables = table_count;

      Opt_trace_object trace_final_strategy(trace);
      trace_final_strategy.add_alnum("final_semijoin_strategy",
                                     "MaterializeScan");
      // Recalculate final access paths for this semi-join strategy
      double rowcount, cost;
      semijoin_mat_scan_access_paths(last_inner, tableno, remaining_tables,
                                     sjm_nest, &rowcount, &cost);

    } else if (pos->sj_strategy == SJ_OPT_FIRST_MATCH) {
      first = pos->first_firstmatch_table;

      Opt_trace_object trace_final_strategy(trace);
      trace_final_strategy.add_alnum("final_semijoin_strategy", "FirstMatch");

      // Recalculate final access paths for this semi-join strategy
      double rowcount, cost;
      (void)semijoin_firstmatch_loosescan_access_paths(
          first, tableno, remaining_tables, false, &rowcount, &cost);

      if (pos->table->emb_sj_nest->is_aj_nest()) {
        /*
          Antijoin doesn't use the execution logic of FirstMatch. So we
          won't set it up; and we won't either have the incompatibilities of
          FirstMatch with outer join. Declare that we don't use it:
        */
        pos->sj_strategy = SJ_OPT_NONE;
      } else {
        join->best_positions[first].sj_strategy = SJ_OPT_FIRST_MATCH;
        join->best_positions[first].n_sj_tables = tableno - first + 1;
      }
    } else if (pos->sj_strategy == SJ_OPT_LOOSE_SCAN) {
      first = pos->first_loosescan_table;

      Opt_trace_object trace_final_strategy(trace);
      trace_final_strategy.add_alnum("final_semijoin_strategy", "LooseScan");

      // Recalculate final access paths for this semi-join strategy
      double rowcount, cost;
      (void)semijoin_firstmatch_loosescan_access_paths(
          first, tableno, remaining_tables, true, &rowcount, &cost);

      POSITION *const first_pos = join->best_positions + first;
      first_pos->sj_strategy = SJ_OPT_LOOSE_SCAN;
      first_pos->n_sj_tables =
          my_count_bits(first_pos->table->emb_sj_nest->sj_inner_tables);
    } else if (pos->sj_strategy == SJ_OPT_DUPS_WEEDOUT) {
      /*
        Duplicate Weedout starting at pos->first_dupsweedout_table, ending at
        this table.
      */
      first = pos->first_dupsweedout_table;
      join->best_positions[first].sj_strategy = SJ_OPT_DUPS_WEEDOUT;
      join->best_positions[first].n_sj_tables = tableno - first + 1;

      Opt_trace_object trace_final_strategy(trace);
      trace_final_strategy.add_alnum("final_semijoin_strategy",
                                     "DuplicateWeedout");
    }

    for (uint i = first; i <= tableno; i++) {
      /*
        Eliminate stale strategies. See comment in the
        SJ_OPT_MATERIALIZE_LOOKUP case above.
      */
      if (i != first) join->best_positions[i].sj_strategy = SJ_OPT_NONE;
      handled_tables |= join->best_positions[i].table->table_ref->map();
    }

    remaining_tables |= pos->table->table_ref->map();
  }

  assert(remaining_tables == (join->all_table_map & ~join->const_table_map));

  return false;
}

/**
  Check interleaving with an inner tables of an outer join for
  extension table.

    Check if table tab can be added to current partial join order, and
    if yes, record that it has been added. This recording can be rolled back
    with backout_nj_state().

    The function assumes that both current partial join order and its
    extension with tab are valid wrt table dependencies.

  @verbatim
     IMPLEMENTATION
       LIMITATIONS ON JOIN ORDER
         The nested [outer] joins executioner algorithm imposes these
  limitations on join order:
         1. "Outer tables first" -  any "outer" table must be before any
             corresponding "inner" table.
         2. "No interleaving" - tables inside a nested join must form a
  continuous sequence in join order (i.e. the sequence must not be interrupted
  by tables that are outside of this nested join).

         #1 is checked elsewhere, this function checks #2 provided that #1 has
         been already checked.

       WHY NEED NON-INTERLEAVING
         Consider an example:

           select * from t0 join t1 left join (t2 join t3) on cond1

         The join order "t1 t2 t0 t3" is invalid:

         table t0 is outside of the nested join, so WHERE condition for t0 is
         attached directly to t0 (without triggers, and it may be used to access
         t0). Applying WHERE(t0) to (t2,t0,t3) record is invalid as we may miss
         combinations of (t1, t2, t3) that satisfy condition cond1, and produce
  a null-complemented (t1, t2.NULLs, t3.NULLs) row, which should not have been
  produced.

         If table t0 is not between t2 and t3, the problem doesn't exist:
          If t0 is located after (t2,t3), WHERE(t0) is applied after nested join
           processing has finished.
          If t0 is located before (t2,t3), predicates like WHERE_cond(t0, t2)
  are wrapped into condition triggers, which takes care of correct nested join
  processing.

       HOW IT IS IMPLEMENTED
         The limitations on join order can be rephrased as follows: for valid
         join order one must be able to:
           1. write down the used tables in the join order on one line.
           2. for each nested join, put one '(' and one ')' on the said line
           3. write "LEFT JOIN" and "ON (...)" where appropriate
           4. get a query equivalent to the query we're trying to execute.

         Calls to check_interleaving_with_nj() are equivalent to writing the
         above described line from left to right.
         A single check_interleaving_with_nj(A,B) call is equivalent to writing
         table B and appropriate brackets on condition that table A and
         appropriate brackets is the last what was written. Graphically the
         transition is as follows:

                              +---- current position
                              |
             ... last_tab ))) | ( tab )  )..) | ...
                                X     Y   Z   |
                                              +- need to move to this
                                                 position.

         Notes about the position:
           The caller guarantees that there is no more then one X-bracket by
           checking "!(remaining_tables & s->dependent)" before calling this
           function. X-bracket may have a pair in Y-bracket.

         When "writing" we store/update this auxiliary info about the current
         position:
          1. cur_embedding_map - bitmap of pairs of brackets (aka nested
             joins) we've opened but didn't close.
          2. {each NESTED_JOIN structure not simplified away}->counter - number
             of this nested join's children that have already been added to to
             the partial join order.
  @endverbatim

  @param tab   Table we're going to extend the current partial join with

  @retval
    false  Join order extended, nested joins info about current join
    order (see NOTE section) updated.
  @retval
    true   Requested join order extension not allowed.
*/

bool Optimize_table_order::check_interleaving_with_nj(JOIN_TAB *tab) {
  if (cur_embedding_map & ~tab->embedding_map) {
    /*
      tab is outside of the "pair of brackets" we're currently in.
      Cannot add it.
    */
    return true;
  }
  const Table_ref *next_emb = tab->table_ref->embedding;
  /*
    Do update counters for "pairs of brackets" that we've left (marked as
    X,Y,Z in the above picture)
  */
  for (; next_emb != emb_sjm_nest; next_emb = next_emb->embedding) {
    // Ignore join nests that are not outer joins.
    if (!next_emb->join_cond_optim()) continue;

    next_emb->nested_join->nj_counter++;
    cur_embedding_map |= next_emb->nested_join->nj_map;
    if (next_emb->nested_join->nj_total != next_emb->nested_join->nj_counter)
      break;

    /*
      We're currently at Y or Z-bracket as depicted in the above picture.
      Mark that we've left it and continue walking up the brackets hierarchy.
    */
    cur_embedding_map &= ~next_emb->nested_join->nj_map;
  }
  return false;
}

/**
  Find best access paths for semi-join FirstMatch or LooseScan strategy
  and calculate rowcount and cost based on these.

  @param first_tab        The first tab to calculate access paths for,
                          this is always a semi-join inner table.
  @param last_tab         The last tab to calculate access paths for,
                          always a semi-join inner table for FirstMatch,
                          may be inner or outer for LooseScan.
  @param remaining_tables Bitmap of tables that are not in the
                          [0...last_tab] join prefix
  @param loosescan        If true, use LooseScan strategy, otherwise FirstMatch
  @param[out] newcount    New output row count
  @param[out] newcost     New join prefix cost

  @return True if strategy selection successful, false otherwise.

  @details
    Calculate best access paths for the tables of a semi-join FirstMatch or
    LooseScan strategy, given the order of tables provided in join->positions
    (or join->best_positions when calculating the cost of a final plan).
    Calculate estimated cost and rowcount for this plan.
    Given a join prefix [0; ... first_tab-1], change the access to the tables
    in the range [first_tab; last_tab] according to the constraints set by the
    relevant semi-join strategy. Those constraints are:

    - For the LooseScan strategy, join buffering can be used for the outer
      tables following the last inner table.

    - For the FirstMatch strategy, join buffering can be used if there is a
      single inner table in the semi-join nest.

    For FirstMatch, the handled range of tables may be a mix of inner tables
    and non-dependent outer tables. The first and last table in the handled
    range are always inner tables.
    For LooseScan, the handled range can be a mix of inner tables and
    dependent and non-dependent outer tables. The first table is always an
    inner table.

    Depending on member 'got_final_plan', the function uses and updates access
    path data in join->best_positions, otherwise uses join->positions
    and updates a local buffer.
*/

bool Optimize_table_order::semijoin_firstmatch_loosescan_access_paths(
    uint first_tab, uint last_tab, table_map remaining_tables, bool loosescan,
    double *newcount, double *newcost) {
  DBUG_TRACE;
  double cost;                // Contains running estimate of calculated cost.
  double rowcount;            // Rowcount of join prefix (ie before first_tab).
  double outer_fanout = 1.0;  // Fanout contributed by outer tables in range.
  double inner_fanout = 1.0;  // Fanout contributed by inner tables in range.
  const Cost_model_server *const cost_model = join->cost_model();
  Opt_trace_context *const trace = &thd->opt_trace;
  Opt_trace_object recalculate(trace, "recalculate_access_paths_and_cost");
  Opt_trace_array trace_tables(trace, "tables");
  POSITION *const positions =
      got_final_plan ? join->best_positions : join->positions;

  if (first_tab == join->const_tables) {
    cost = 0.0;
    rowcount = 1.0;
  } else {
    cost = positions[first_tab - 1].prefix_cost;
    rowcount = positions[first_tab - 1].prefix_rowcount;
  }

  uint table_count = 0;
  uint no_jbuf_before;
  for (uint i = first_tab; i <= last_tab; i++) {
    remaining_tables |= positions[i].table->table_ref->map();
    if (positions[i].table->emb_sj_nest) table_count++;
  }
  // Join buffering is enabled/disabled based on how blocked nested loop (BNL)
  // worked. However, with hash joins replacing BNL in the executor now, we have
  // opportunity to enable join buffering for some more cases. For now, we
  // enable it only for secondary engine in case of FirstMatch (since secondary
  // engine is currently able to interpret plans generated using FirstMatch
  // strategy only). More details in setup_join_buffering().
  if (loosescan) {
    // LooseScan: May use join buffering for all tables after last inner table.
    for (no_jbuf_before = last_tab; no_jbuf_before > first_tab;
         no_jbuf_before--) {
      if (positions[no_jbuf_before].table->emb_sj_nest != nullptr)
        break;  // Encountered the last inner table.
    }
    no_jbuf_before++;
  } else {
    // FirstMatch: May use join buffering if there is only one inner table.
    // Restriction is lifted for secondary engine.
    if (table_count > 1 &&
        !(thd->lex->m_sql_cmd != nullptr &&
          thd->lex->m_sql_cmd->using_secondary_storage_engine())) {
      no_jbuf_before = last_tab + 1;
    } else {
      no_jbuf_before = first_tab;
    }
  }

  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);
  // recalculate, as we go back in the range of "unoptimized" tables:
  recalculate_lateral_deps(first_tab);

  for (uint i = first_tab; i <= last_tab; i++) {
    JOIN_TAB *const tab = positions[i].table;
    POSITION regular_pos;
    POSITION *const dst_pos = got_final_plan ? positions + i : &regular_pos;
    POSITION *pos;  // Position for later calculations
    /*
      We always need a new calculation for the first inner table in
      the LooseScan strategy.
    */
    const bool is_ls_driving_tab = (i == first_tab) && loosescan;
    if (is_ls_driving_tab || positions[i].use_join_buffer) {
      Opt_trace_object trace_one_table(trace);
      trace_one_table.add_utf8_table(tab->table_ref);

      /*
        Find the best access method with specified join buffering strategy.
        If this is a loosescan driving table,
        semijoin_loosescan_fill_driving_table_position will consider all keys,
        so best_access_path() should fill bound_keyparts/read_cost/fanout for
        all keys => test_all_ref_keys==true.
       */
      assert(!test_all_ref_keys);
      test_all_ref_keys = is_ls_driving_tab;
      double prefix_rowcount = rowcount * inner_fanout * outer_fanout;
      best_access_path(tab, remaining_tables, i, i < no_jbuf_before,
                       prefix_rowcount, dst_pos);
      test_all_ref_keys = false;
      if (is_ls_driving_tab)  // Use loose scan position
      {
        if (semijoin_loosescan_fill_driving_table_position(
                tab, remaining_tables, i, prefix_rowcount, dst_pos)) {
          dst_pos->table = tab;
          const double rows = rowcount * dst_pos->rows_fetched;
          dst_pos->set_prefix_cost(
              cost + dst_pos->read_cost + cost_model->row_evaluate_cost(rows),
              rows * dst_pos->filter_effect);
        } else {
          assert(!got_final_plan);
          return false;
        }
      }
      pos = dst_pos;
    } else
      pos = positions + i;  // Use result from prior calculation

    /*
      Terminate search if best_access_path found no possible plan.
      Otherwise we will be getting infinite cost when summing up below.
     */
    if (pos->read_cost == DBL_MAX) {
      assert(loosescan && !got_final_plan);
      return false;
    }

    remaining_tables &= ~tab->table_ref->map();

    cost += pos->read_cost +
            cost_model->row_evaluate_cost(rowcount * inner_fanout *
                                          outer_fanout * pos->rows_fetched);

    if (tab->emb_sj_nest)
      inner_fanout *= pos->rows_fetched * pos->filter_effect;
    else
      outer_fanout *= pos->rows_fetched * pos->filter_effect;

    recalculate_lateral_deps_incrementally(i + 1);
  }

  *newcount = rowcount * outer_fanout;
  *newcost = cost;

  return true;
}

/**
  Find best access paths for semi-join MaterializeScan strategy
  and calculate rowcount and cost based on these.

  @param last_inner_tab    The last tab in the set of inner tables
  @param last_outer_tab    The last tab in the set of outer tables
  @param remaining_tables  Bitmap of tables that are not in the join prefix
                           including the inner and outer tables processed here.
  @param sjm_nest          Pointer to semi-join nest for inner tables
  @param[out] newcount     New output row count
  @param[out] newcost      New join prefix cost

  @details
    Calculate best access paths for the outer tables of the MaterializeScan
    semi-join strategy. All outer tables may use join buffering.
    The prefix row count is adjusted with the estimated number of rows in
    the materialized tables, before taking into consideration the rows
    contributed by the outer tables.
*/

void Optimize_table_order::semijoin_mat_scan_access_paths(
    uint last_inner_tab, uint last_outer_tab, table_map remaining_tables,
    Table_ref *sjm_nest, double *newcount, double *newcost) {
  DBUG_TRACE;

  const Cost_model_server *const cost_model = join->cost_model();
  Opt_trace_context *const trace = &thd->opt_trace;
  Opt_trace_object recalculate(trace, "recalculate_access_paths_and_cost");
  Opt_trace_array trace_tables(trace, "tables");
  double cost;      // Calculated running cost of operation
  double rowcount;  // Rowcount of join prefix (ie before first_inner).

  POSITION *const positions =
      got_final_plan ? join->best_positions : join->positions;
  const uint inner_count = my_count_bits(sjm_nest->sj_inner_tables);

  // Get the prefix cost.
  const uint first_inner = last_inner_tab + 1 - inner_count;
  if (first_inner == join->const_tables) {
    rowcount = 1.0;
    cost = 0.0;
  } else {
    rowcount = positions[first_inner - 1].prefix_rowcount;
    cost = positions[first_inner - 1].prefix_cost;
  }

  // Add materialization cost.
  cost += sjm_nest->nested_join->sjm.materialization_cost.total_cost() +
          rowcount * sjm_nest->nested_join->sjm.scan_cost.total_cost();

  for (uint i = last_inner_tab + 1; i <= last_outer_tab; i++)
    remaining_tables |= positions[i].table->table_ref->map();
  /*
    Materialization removes duplicates from the materialized table, so
    number of rows to scan is probably less than the number of rows
    from a full join, on which the access paths of outer tables are currently
    based. Rerun best_access_path to adjust for reduced rowcount.
  */
  const double inner_fanout = sjm_nest->nested_join->sjm.expected_rowcount;
  double outer_fanout = 1.0;

  Table_map_restorer deps_lateral(
      &join->deps_of_remaining_lateral_derived_tables);
  // recalculate, as we go back in the range of "unoptimized" tables:
  recalculate_lateral_deps(last_inner_tab + 1);

  for (uint i = last_inner_tab + 1; i <= last_outer_tab; i++) {
    Opt_trace_object trace_one_table(trace);
    JOIN_TAB *const tab = positions[i].table;
    trace_one_table.add_utf8_table(tab->table_ref);
    POSITION regular_pos;
    POSITION *const dst_pos = got_final_plan ? positions + i : &regular_pos;
    best_access_path(tab, remaining_tables, i, false,
                     rowcount * inner_fanout * outer_fanout, dst_pos);
    remaining_tables &= ~tab->table_ref->map();
    outer_fanout *= dst_pos->rows_fetched;
    cost += dst_pos->read_cost + cost_model->row_evaluate_cost(
                                     rowcount * inner_fanout * outer_fanout);
    outer_fanout *= dst_pos->filter_effect;
    recalculate_lateral_deps_incrementally(i + 1);
  }

  *newcount = rowcount * outer_fanout;
  *newcost = cost;
}

/**
  Find best access paths for semi-join MaterializeLookup strategy.
  and calculate rowcount and cost based on these.

  @param last_inner        Index of the last inner table
  @param sjm_nest          Pointer to semi-join nest for inner tables
  @param[out] newcount     New output row count
  @param[out] newcost      New join prefix cost

  @details
    All outer tables may use join buffering, so there is no need to recalculate
    access paths nor costs for these.
    Add cost of materialization and scanning the materialized table to the
    costs of accessing the outer tables.
*/

void Optimize_table_order::semijoin_mat_lookup_access_paths(uint last_inner,
                                                            Table_ref *sjm_nest,
                                                            double *newcount,
                                                            double *newcost) {
  DBUG_TRACE;

  const uint inner_count = my_count_bits(sjm_nest->sj_inner_tables);
  double rowcount, cost;

  const uint first_inner = last_inner + 1 - inner_count;
  if (first_inner == join->const_tables) {
    cost = 0.0;
    rowcount = 1.0;
  } else {
    cost = join->positions[first_inner - 1].prefix_cost;
    rowcount = join->positions[first_inner - 1].prefix_rowcount;
  }

  cost += sjm_nest->nested_join->sjm.materialization_cost.total_cost() +
          rowcount * sjm_nest->nested_join->sjm.lookup_cost.total_cost();

  *newcount = rowcount;
  *newcost = cost;
}

/**
  Find best access paths for semi-join DuplicateWeedout strategy
  and calculate rowcount and cost based on these.

  @param first_tab        The first tab to calculate access paths for
  @param last_tab         The last tab to calculate access paths for
  @param[out] newcount    New output row count
  @param[out] newcost     New join prefix cost

  @details
    Notice that new best access paths need not be calculated.
    The proper access path information is already in join->positions,
    because DuplicateWeedout can handle any join buffering strategy.
    The only action performed by this function is to calculate
    output rowcount, and an updated cost estimate.

    The cost estimate is based on performing a join over the involved
    tables, but we must also add the cost of creating and populating
    the temporary table used for duplicate removal, and the cost of
    doing lookups against this table.
*/

void Optimize_table_order::semijoin_dupsweedout_access_paths(uint first_tab,
                                                             uint last_tab,
                                                             double *newcount,
                                                             double *newcost) {
  DBUG_TRACE;

  const Cost_model_server *const cost_model = join->cost_model();
  double cost, rowcount;
  double inner_fanout = 1.0;
  double outer_fanout = 1.0;
  double max_outer_fanout = 1.0;
  uint rowsize;  // Row size of the temporary table
  if (first_tab == join->const_tables) {
    cost = 0.0;
    rowcount = 1.0;
    rowsize = 0;
  } else {
    cost = join->positions[first_tab - 1].prefix_cost;
    rowcount = join->positions[first_tab - 1].prefix_rowcount;
    rowsize = 8;  // This is not true but we'll make it so
  }
  /**
    Some times, some outer fanout is "absorbed" into the inner fanout.
    In this case, we should make a better estimate for outer_fanout that
    is used to calculate the output rowcount.
    If we have inner table(s) before an outer table, there are
    dependencies between these tables. The fanout for the outer table is
    not a good estimate for the final number of rows from the weedout
    execution, therefore we convert some of the inner fanout into an outer
    fanout, limited to the number of possible rows in the outer table.
  */
  for (uint j = first_tab; j <= last_tab; j++) {
    const POSITION *const p = join->positions + j;
    cost += p->read_cost +
            cost_model->row_evaluate_cost(rowcount * inner_fanout *
                                          outer_fanout * p->rows_fetched);

    if (p->table->emb_sj_nest)
      inner_fanout *= p->rows_fetched * p->filter_effect;
    else {
      /*
        max_outer_fanout is the cardinality of the cross product
        of the outer tables.
        @note: We do not consider dependencies between these tables here.
      */
      double total_records = p->table->table()->file->stats.records;
      max_outer_fanout *= total_records * p->filter_effect;
      if (inner_fanout > 1.0) {
        // Absorb inner fanout into the outer fanout
        outer_fanout *= inner_fanout * p->rows_fetched * p->filter_effect;
        inner_fanout = 1;
      } else
        outer_fanout *= p->rows_fetched * p->filter_effect;
      rowsize += p->table->table()->file->ref_length;
    }
  }

  if (max_outer_fanout < outer_fanout) {
    /*
      The calculated fanout for the outer tables is bigger than
      the cardinality of the cross product of the outer tables.
      Adjust outer fanout to the max value, but also adjust
      inner fanout so that inner_fanout * outer_fanout is still
      the same (dups weedout runs a complete join internally).
    */
    if (max_outer_fanout > 0.0) inner_fanout *= outer_fanout / max_outer_fanout;
    outer_fanout = max_outer_fanout;
  }

  /*
    Add the cost of temptable use. The table will have outer_fanout rows,
    and we will make
    - rowcount * outer_fanout writes
    - rowcount * inner_fanout * outer_fanout lookups.
  */
  Cost_model_server::enum_tmptable_type tmp_table_type;
  if (outer_fanout * rowsize < thd->variables.max_heap_table_size)
    tmp_table_type = Cost_model_server::MEMORY_TMPTABLE;
  else
    tmp_table_type = Cost_model_server::DISK_TMPTABLE;

  cost += cost_model->tmptable_create_cost(tmp_table_type);
  cost += cost_model->tmptable_readwrite_cost(
      tmp_table_type, rowcount * outer_fanout,
      rowcount * inner_fanout * outer_fanout);

  *newcount = rowcount * outer_fanout;
  *newcost = cost;
}

/**
  Do semi-join optimization step after we've added a new tab to join prefix

  This function cannot work with nested SJ nests, for two reasons:
  (a) QEP_TAB::emb_sj_nest points to the most inner SJ nest, and this
  function looks only at it, so misses to do any SJ strategy choice for
  outer nests
  (b) POSITION has only one set of SJ-info (e.g. first_firstmatch_table): so
  planning for two nested nests would require more info than we have.
  And indeed, SJ nests cannot be nested, because:
  (c) a SJ nest is not nested in another SJ or anti SJ nest (it would have been
  dissolved into the outer nest by simplify_joins()).
  (d) an anti SJ nest is not nested inside another SJ or anti SJ nest (this case
  is blocked by resolve_subquery()).

  @param remaining_tables Tables not in the join prefix
  @param new_join_tab     Join tab that we are adding to the join prefix
  @param idx              Index in join->position storing this join tab
                          (i.e. number of tables in the prefix)

  @details
    Update semi-join optimization state after we've added another tab (table
    and access method) to the join prefix.

    The state is maintained in join->positions[#prefix_size]. Each of the
    available strategies has its own state variables.

    for each semi-join strategy
    {
      update strategy's state variables;

      if (join prefix has all the tables that are needed to consider
          using this strategy for the semi-join(s))
      {
        calculate cost of using the strategy
        if ((this is the first strategy to handle the semi-join nest(s)  ||
            the cost is less than other strategies))
        {
          // Pick this strategy
          pos->sj_strategy= ..
          ..
        }
      }
    }

    Most of the new state is saved in join->positions[idx] (and hence no undo
    is necessary).

    See setup_semijoin_dups_elimination() for a description of what kinds of
    join prefixes each strategy can handle.

    A note on access path, rowcount and cost estimates:
    - best_extension_by_limited_search() performs *initial calculations*
      of access paths, rowcount and cost based on the operation being
      an inner join or an outer join operation. These estimates are saved
      in join->positions.
    - advance_sj_state() performs *intermediate calculations* based on the
      same table information, but for the supported semi-join strategies.
      The access path part of these calculations are not saved anywhere,
      but the rowcount and cost of the best semi-join strategy are saved
      in join->positions.
    - Because the semi-join access path information was not saved previously,
      fix_semijoin_strategies() must perform *final calculations* of
      access paths, rowcount and cost when saving the selected table order
      in join->best_positions. The results of the final calculations will be
      the same as the results of the "best" intermediate calculations.
*/

void Optimize_table_order::advance_sj_state(table_map remaining_tables,
                                            const JOIN_TAB *new_join_tab,
                                            uint idx) {
  Opt_trace_context *const trace = &thd->opt_trace;
  Table_ref *const emb_sj_nest = new_join_tab->emb_sj_nest;
  POSITION *const pos = join->positions + idx;
  double best_cost = pos->prefix_cost;
  double best_rowcount = pos->prefix_rowcount;
  uint sj_strategy = SJ_OPT_NONE;  // Initially: No chosen strategy

  /*
    Semi-join nests cannot be nested, hence we never need to advance the
    semi-join state of a materialized semi-join query.
    In fact, doing this may cause undesirable effects because all tables
    within a semi-join nest have emb_sj_nest != NULL, which triggers several
    of the actions inside this function.
  */
  assert(emb_sjm_nest == nullptr);

  // remaining_tables include the current one:
  assert(remaining_tables & new_join_tab->table_ref->map());
  // Save it:
  const table_map remaining_tables_incl = remaining_tables;
  // And add the current table to the join prefix:
  remaining_tables &= ~new_join_tab->table_ref->map();

  DBUG_TRACE;

  Opt_trace_array trace_choices(trace, "semijoin_strategy_choice");

  /* Initialize the state or copy it from prev. tables */
  pos->cur_embedding_map = cur_embedding_map;
  if (idx == join->const_tables) {
    pos->dups_producing_tables = 0;
    pos->first_firstmatch_table = MAX_TABLES;
    pos->first_loosescan_table = MAX_TABLES;
    pos->dupsweedout_tables = 0;
    pos->sjm_scan_need_tables = 0;
    pos->sjm_scan_last_inner = 0;
  } else {
    pos->dups_producing_tables = pos[-1].dups_producing_tables;

    // FirstMatch
    pos->first_firstmatch_table = pos[-1].first_firstmatch_table;
    pos->first_firstmatch_rtbl = pos[-1].first_firstmatch_rtbl;
    pos->firstmatch_need_tables = pos[-1].firstmatch_need_tables;

    // LooseScan
    pos->first_loosescan_table = (pos[-1].sj_strategy == SJ_OPT_LOOSE_SCAN)
                                     ? MAX_TABLES
                                     : pos[-1].first_loosescan_table;
    pos->loosescan_need_tables = pos[-1].loosescan_need_tables;

    // MaterializeScan
    pos->sjm_scan_need_tables = (pos[-1].sj_strategy == SJ_OPT_MATERIALIZE_SCAN)
                                    ? 0
                                    : pos[-1].sjm_scan_need_tables;
    pos->sjm_scan_last_inner = pos[-1].sjm_scan_last_inner;

    // Duplicate Weedout
    pos->dupsweedout_tables = pos[-1].dupsweedout_tables;
    pos->first_dupsweedout_table = pos[-1].first_dupsweedout_table;
  }

  table_map handled_by_fm_or_ls = 0;
  /*
    FirstMatch Strategy
    ===================

    FirstMatch requires that all dependent outer tables are in the join prefix.
    (see "FirstMatch strategy" above setup_semijoin_dups_elimination()).
    The execution strategy will handle multiple semi-join nests correctly,
    and the optimizer will pick execution strategy according to these rules:
    - If tables from multiple semi-join nests are intertwined, they will
      be processed as one FirstMatch evaluation.
    - If tables from each semi-join nest are grouped together, each semi-join
      nest is processed as one FirstMatch evaluation.

    Example: Let's say we have an outer table ot and two semi-join nests with
    two tables each: it11 and it12, and it21 and it22.

    Intertwined tables: ot - FM(it11 - it21 - it12 - it22)
    Grouped tables: ot - FM(it11 - it12) - FM(it21 - it22)
  */

  if (pos->first_firstmatch_table != MAX_TABLES) {
    const Table_ref *first_emb_sj_nest =
        join->positions[pos->first_firstmatch_table].table->emb_sj_nest;
    if (emb_sj_nest != first_emb_sj_nest) {
      // Can't handle interleaving between tables from the
      // semi-join that FirstMatch is handling and any other tables.
      pos->first_firstmatch_table = MAX_TABLES;
    }
  }

  if (emb_sj_nest && emb_sj_nest->nested_join->sj_enabled_strategies &
                         OPTIMIZER_SWITCH_FIRSTMATCH) {
    const table_map outer_corr_tables = emb_sj_nest->nested_join->sj_depends_on;
    const table_map sj_inner_tables = emb_sj_nest->sj_inner_tables;
    /*
      Enter condition:
       1. The next join tab belongs to semi-join nest
          (verified for the encompassing code block above).
       2. We're not in a duplicate producer range yet
       3. All outer tables that
           - the subquery is correlated with, or
           - referred to from the outer_expr
          are in the join prefix
    */
    if (pos->dups_producing_tables == 0 &&        // (2)
        !(remaining_tables & outer_corr_tables))  // (3)
    {
      /* Start tracking potential FirstMatch range */
      pos->first_firstmatch_table = idx;
      pos->firstmatch_need_tables = 0;
      pos->first_firstmatch_rtbl = remaining_tables;
      // All inner tables should still be part of remaining_tables_inc
      assert(sj_inner_tables == (remaining_tables_incl & sj_inner_tables));
    }

    if (pos->first_firstmatch_table != MAX_TABLES) {
      /* Record that we need all of this semi-join's inner tables */
      pos->firstmatch_need_tables |= sj_inner_tables;

      if (outer_corr_tables & pos->first_firstmatch_rtbl) {
        /*
          Trying to add an sj-inner table whose sj-nest has an outer correlated
          table that was not in the prefix. This means FirstMatch can't be used.
        */
        pos->first_firstmatch_table = MAX_TABLES;
      } else if (!(pos->firstmatch_need_tables & remaining_tables)) {
        // Got a complete FirstMatch range.

        // We cannot FirstMatch to a different embedding nest,
        // e.g., for B LEFT JOIN (C SEMIJOIN D ON B.X=D.Y) and table order
        // B-D-C we cannot jump from D to B. This would cause non-hierarchical
        // joins. So we check that the jump won't leave from a still-open
        // nest: cur_embedding_map at the last table of this firstmatch range
        // must be included in cur_embedding_map at the target of the jump.
        nested_join_map cur_embedding_map_at_jump_target =
            pos->first_firstmatch_table > join->const_tables
                ? join->positions[pos->first_firstmatch_table - 1]
                      .cur_embedding_map
                : 0;
        if ((cur_embedding_map_at_jump_target & cur_embedding_map) !=
            cur_embedding_map) {
          pos->first_firstmatch_table = MAX_TABLES;
        } else {
          // Calculate access paths and cost
          double cost, rowcount;
          /* We use the same FirstLetterUpcase as in EXPLAIN */
          Opt_trace_object trace_one_strategy(trace);
          trace_one_strategy.add_alnum("strategy", "FirstMatch");
          (void)semijoin_firstmatch_loosescan_access_paths(
              pos->first_firstmatch_table, idx, remaining_tables, false,
              &rowcount, &cost);
          /*
            We don't yet know what are the other strategies, so pick FirstMatch.

            We ought to save the alternate POSITIONs produced by
            semijoin_firstmatch_loosescan_access_paths() but the problem is that
            providing save space uses too much space.
            Instead, we will re-calculate the alternate POSITIONs after we've
            picked the best QEP.
          */
          sj_strategy = SJ_OPT_FIRST_MATCH;
          best_cost = cost;
          best_rowcount = rowcount;
          trace_one_strategy.add("cost", best_cost).add("rows", best_rowcount);
          handled_by_fm_or_ls = pos->firstmatch_need_tables;

          trace_one_strategy.add("chosen", true);
        }
      }
    }
  }
  /*
    LooseScan Strategy
    ==================

    LooseScan requires that all dependent outer tables are not in the join
    prefix. (see "LooseScan strategy" above setup_semijoin_dups_elimination()).
    The tables must come in a rather strictly defined order:
    1. The LooseScan driving table (which is a subquery inner table).
    2. The remaining tables from the same semi-join nest as the above table.
    3. The outer dependent tables, possibly mixed with outer non-dependent
       tables.
    Notice that any other semi-joined tables must be outside this table range.
  */
  {
    if (pos->first_loosescan_table != MAX_TABLES) {
      const Table_ref *first_emb_sj_nest =
          join->positions[pos->first_loosescan_table].table->emb_sj_nest;
      if (first_emb_sj_nest->sj_inner_tables & remaining_tables_incl) {
        // Stage 2: Accept remaining tables from the semi-join nest:
        if (emb_sj_nest != first_emb_sj_nest) {
          /*
            LooseScan strategy can't handle interleaving between tables from
            the semi-join that LooseScan is handling and any other tables.
          */
          pos->first_loosescan_table = MAX_TABLES;
        } else {
          /*
            NestedLoopSemiJoinWithDuplicateRemovalIterator takes a
            single-table iterator as left argument, and inner-joins
            it with the set of other SJ-inner tables. E.g. it doesn't work for
            A SEMI JOIN (B LEFT JOIN C) with B as LooseScan table. So:
            - if we're now at the second SJ-inner table (1) , and
            - this table belongs to a join nest which is outer-joined to
            the first SJ-inner table (2), or is directly outer-joined to the
            first SJ-inner table (3),
            - then both tables are not inner-joined together and LooseScan is
            impossible.
          */
          if (idx == pos->first_loosescan_table + 1 &&  // (1)
              ((pos->table->table_ref->outer_join_nest() !=
                join->positions[pos->first_loosescan_table]
                    .table->table_ref->outer_join_nest())  // (2)
               || pos->table->table_ref->outer_join))      // (3)
            pos->first_loosescan_table = MAX_TABLES;
        }
      } else {
        // Stage 3: Accept outer dependent and non-dependent tables:
        assert(emb_sj_nest != first_emb_sj_nest);
        if (emb_sj_nest != nullptr) pos->first_loosescan_table = MAX_TABLES;
      }
    }

    /*
      We may consider the LooseScan strategy if
      1a. The next table is an SJ-inner table, and
      1b. LooseScan is enabled for this SJ nest, and
      2. We have no more than 64 IN expressions (must fit in bitmap), and
      3. It is the first table from that semijoin, and
      4. We're not within a semi-join range, except
      new_join_tab->emb_sj_nest (which we've just entered, see #3), and
      5. All non-IN-equality correlation references from this sj-nest are
      bound, and
      6. But some of the IN-equalities aren't (so this can't be handled by
      FirstMatch strategy), and
      7. There are equalities (including maybe semi-join ones) which can be
      handled with an index of this table, and
      8. Not a derived table/view. (a temporary restriction)
    */
    if (emb_sj_nest &&  // (1a)
        emb_sj_nest->nested_join->sj_enabled_strategies &
            OPTIMIZER_SWITCH_LOOSE_SCAN &&                          // (1b)
        emb_sj_nest->nested_join->sj_inner_exprs.size() <= 64 &&    // (2)
        ((remaining_tables_incl & emb_sj_nest->sj_inner_tables) ==  // (3)
         emb_sj_nest->sj_inner_tables) &&                           // (3)
        pos->dups_producing_tables == 0 &&                          // (4)
        !(remaining_tables_incl &
          emb_sj_nest->nested_join->sj_corr_tables) &&  // (5)
        (remaining_tables_incl &
         emb_sj_nest->nested_join->sj_depends_on) &&       // (6)
        new_join_tab->keyuse() != nullptr &&               // (7)
        !new_join_tab->table_ref->uses_materialization())  // (8)
    {
      // start considering using LooseScan strategy
      pos->first_loosescan_table = idx;
      pos->loosescan_need_tables = emb_sj_nest->sj_inner_tables |
                                   emb_sj_nest->nested_join->sj_depends_on;
    }

    if ((pos->first_loosescan_table != MAX_TABLES) &&
        !(remaining_tables & pos->loosescan_need_tables)) {
      /*
        Ok we have all LooseScan sj-nest's inner tables and outer correlated
        tables into the prefix.
      */

      // Got a complete LooseScan range. Calculate access paths and cost
      double cost, rowcount;
      Opt_trace_object trace_one_strategy(trace);
      trace_one_strategy.add_alnum("strategy", "LooseScan");
      /*
        The same problem as with FirstMatch - we need to save POSITIONs
        somewhere but reserving space for all cases would require too
        much space. We will re-calculate POSITION structures later on.
        If this function returns 'false', it means LS is impossible (didn't
        find a suitable index, etc).
      */
      if (semijoin_firstmatch_loosescan_access_paths(pos->first_loosescan_table,
                                                     idx, remaining_tables,
                                                     true, &rowcount, &cost)) {
        /*
          We don't yet have any other strategies that could handle this
          semi-join nest (the other options are Duplicate Elimination or
          Materialization, which need at least the same set of tables in
          the join prefix to be considered) so unconditionally pick the
          LooseScan.
        */
        sj_strategy = SJ_OPT_LOOSE_SCAN;
        best_cost = cost;
        best_rowcount = rowcount;
        trace_one_strategy.add("cost", best_cost).add("rows", best_rowcount);
        handled_by_fm_or_ls = join->positions[pos->first_loosescan_table]
                                  .table->emb_sj_nest->sj_inner_tables;
      }
      trace_one_strategy.add("chosen", sj_strategy == SJ_OPT_LOOSE_SCAN);
    }
  }

  if (emb_sj_nest) pos->dups_producing_tables |= emb_sj_nest->sj_inner_tables;

  pos->dups_producing_tables &= ~handled_by_fm_or_ls;

  /* MaterializeLookup and MaterializeScan strategy handler */
  const int sjm_strategy = semijoin_order_allows_materialization(
      join, remaining_tables, new_join_tab, idx);
  if (sjm_strategy == SJ_OPT_MATERIALIZE_SCAN) {
    /*
      We cannot evaluate this option now. This is because we cannot
      account for fanout of sj-inner tables yet:

        ntX  SJM-SCAN(it1 ... itN) | ot1 ... otN  |
                                   ^(1)           ^(2)

      we're now at position (1). SJM temptable in general has multiple
      records, so at point (1) we'll get the fanout from sj-inner tables (ie
      there will be multiple record combinations).

      The final join result will not contain any semi-join produced
      fanout, i.e. tables within SJM-SCAN(...) will not contribute to
      the cardinality of the join output.  Extra fanout produced by
      SJM-SCAN(...) will be 'absorbed' into fanout produced by ot1 ...  otN.

      The simple way to model this is to remove SJM-SCAN(...) fanout once
      we reach the point #2.
    */
    if (pos->sjm_scan_need_tables && emb_sj_nest != nullptr &&
        emb_sj_nest !=
            join->positions[pos->sjm_scan_last_inner].table->emb_sj_nest)
      /*
        Prevent that inner tables of different semijoin nests are
        interleaved for MatScan.
      */
      pos->sjm_scan_need_tables = 0;
    else {
      pos->sjm_scan_need_tables = emb_sj_nest->sj_inner_tables |
                                  emb_sj_nest->nested_join->sj_depends_on;
      pos->sjm_scan_last_inner = idx;
      Opt_trace_object(trace)
          .add_alnum("strategy", "MaterializeScan")
          .add_alnum("choice", "deferred");
    }
  } else if (sjm_strategy == SJ_OPT_MATERIALIZE_LOOKUP) {
    // Calculate access paths and cost for MaterializeLookup strategy
    double cost, rowcount;
    semijoin_mat_lookup_access_paths(idx, emb_sj_nest, &rowcount, &cost);

    Opt_trace_object trace_one_strategy(trace);
    trace_one_strategy.add_alnum("strategy", "MaterializeLookup")
        .add("cost", cost)
        .add("rows", rowcount)
        .add("duplicate_tables_left", pos->dups_producing_tables != 0);
    if (cost < best_cost || pos->dups_producing_tables) {
      /*
        NOTE: When we pick to use SJM[-Scan] we don't memcpy its POSITION
        elements to join->positions as that makes it hard to return things
        back when making one step back in join optimization. That's done
        after the QEP has been chosen.
      */
      sj_strategy = SJ_OPT_MATERIALIZE_LOOKUP;
      best_cost = cost;
      best_rowcount = rowcount;
      pos->dups_producing_tables &= ~emb_sj_nest->sj_inner_tables;
    }
    trace_one_strategy.add("chosen", sj_strategy == SJ_OPT_MATERIALIZE_LOOKUP);
  }

  /* MaterializeScan second phase check */
  /*
    The optimizer does not support that we have inner tables from more
    than one semi-join nest within the table range.
  */
  if (pos->sjm_scan_need_tables && emb_sj_nest != nullptr &&
      emb_sj_nest !=
          join->positions[pos->sjm_scan_last_inner].table->emb_sj_nest)
    pos->sjm_scan_need_tables = 0;

  if (pos->sjm_scan_need_tables && /* Have SJM-Scan prefix */
      !(pos->sjm_scan_need_tables & remaining_tables)) {
    Table_ref *const sjm_nest =
        join->positions[pos->sjm_scan_last_inner].table->emb_sj_nest;

    double cost, rowcount;

    Opt_trace_object trace_one_strategy(trace);
    trace_one_strategy.add_alnum("strategy", "MaterializeScan");

    semijoin_mat_scan_access_paths(pos->sjm_scan_last_inner, idx,
                                   remaining_tables, sjm_nest, &rowcount,
                                   &cost);
    trace_one_strategy.add("cost", cost)
        .add("rows", rowcount)
        .add("duplicate_tables_left", pos->dups_producing_tables != 0);
    /*
      Use the strategy if
       * it is cheaper then what we've had, or
       * we haven't picked any other semi-join strategy yet
      In the second case, we pick this strategy unconditionally because
      comparing cost without semi-join duplicate removal with cost with
      duplicate removal is not an apples-to-apples comparison.
    */
    if (cost < best_cost || pos->dups_producing_tables) {
      sj_strategy = SJ_OPT_MATERIALIZE_SCAN;
      best_cost = cost;
      best_rowcount = rowcount;
      pos->dups_producing_tables &= ~sjm_nest->sj_inner_tables;
    }
    trace_one_strategy.add("chosen", sj_strategy == SJ_OPT_MATERIALIZE_SCAN);
  }

  /* Duplicate Weedout strategy handler */
  {
    /*
       Duplicate weedout can be applied after all ON-correlated and
       correlated
    */
    if (emb_sj_nest) {
      if (!pos->dupsweedout_tables) pos->first_dupsweedout_table = idx;

      pos->dupsweedout_tables |= emb_sj_nest->sj_inner_tables |
                                 emb_sj_nest->nested_join->sj_depends_on;
    }

    if (pos->dupsweedout_tables &&
        !(remaining_tables & pos->dupsweedout_tables)) {
      Opt_trace_object trace_one_strategy(trace);
      trace_one_strategy.add_alnum("strategy", "DuplicatesWeedout");
      /*
        Ok, reached a state where we could put a dups weedout point.
        Walk back and calculate
          - the join cost (this is needed as the accumulated cost may assume
            some other duplicate elimination method)
          - extra fanout that will be removed by duplicate elimination
          - duplicate elimination cost
        There are two cases:
          1. We have other strategy/ies to remove all of the duplicates.
          2. We don't.

        We need to calculate the cost in case #2 also because we need to make
        choice between this join order and others.
      */
      double rowcount, cost;
      semijoin_dupsweedout_access_paths(pos->first_dupsweedout_table, idx,
                                        &rowcount, &cost);
      /*
        Use the strategy if
         * it is cheaper then what we've had, and strategy is enabled, or
         * we haven't picked any other semi-join strategy yet
        The second part is necessary because this strategy is the last one
        to consider (it needs "the most" tables in the prefix) and we can't
        leave duplicate-producing tables not handled by any strategy.
      */
      trace_one_strategy.add("cost", cost)
          .add("rows", rowcount)
          .add("duplicate_tables_left", pos->dups_producing_tables != 0);
      if ((cost < best_cost &&
           join->positions[pos->first_dupsweedout_table]
                   .table->emb_sj_nest->nested_join->sj_enabled_strategies &
               OPTIMIZER_SWITCH_DUPSWEEDOUT) ||
          pos->dups_producing_tables) {
        sj_strategy = SJ_OPT_DUPS_WEEDOUT;
        best_cost = cost;
        best_rowcount = rowcount;
        /*
          Note, dupsweedout_tables contains inner and outer tables, even though
          "dups_producing_tables" are always inner table. Ok for this use.
        */
        pos->dups_producing_tables &= ~pos->dupsweedout_tables;
      }
      trace_one_strategy.add("chosen", sj_strategy == SJ_OPT_DUPS_WEEDOUT);
    }
  }
  pos->sj_strategy = sj_strategy;
  /*
    If a semi-join strategy is chosen, update cost and rowcount in positions
    as well. These values may be used as prefix cost and rowcount for later
    semi-join calculations, e.g for plans like "ot1 - it1 - it2 - ot2",
    where we have two semi-join nests containing it1 and it2, respectively,
    and we have a dependency between ot1 and it1, and between ot2 and it2.
    When looking at a semi-join plan for "it2 - ot2", the correct prefix cost
   (located in the join_tab for it1) must be filled in properly.

    Tables in a semijoin range, except the last in range, won't have their
    prefix_costs changed below; this is normal: when we process them, this is
    a regular join so regular costs calculated in best_ext...() are ok;
    duplicates elimination happens only at the last table in range, so it
    makes sense to correct prefix_costs of that last table.
  */
  if (sj_strategy != SJ_OPT_NONE)
    pos->set_prefix_cost(best_cost, best_rowcount);
}

/**
  Nested joins perspective: Remove the last table from the join order.

  @details
  Remove the last table from the partial join order and update the nested
  joins counters and cur_embedding_map. It is ok to call this
  function for the first table in join order (for which
  check_interleaving_with_nj has not been called)

  This function rolls back changes done by:
   - check_interleaving_with_nj(): removes the last table from the partial join
     order and update the nested joins counters and cur_embedding_map. It
     is ok to call this for the first table in join order (for which
     check_interleaving_with_nj() has not been called).

  The algorithm is the reciprocal of check_interleaving_with_nj(), hence
  parent join nest nodes are updated only when the last table in its child
  node is removed. The ASCII graphic below will clarify.

  %A table nesting such as <tt> t1 x [ ( t2 x t3 ) x ( t4 x t5 ) ] </tt>is
  represented by the below join nest tree.

  @verbatim
                     NJ1
                  _/ /  \
                _/  /    NJ2
              _/   /     / \
             /    /     /   \
   t1 x [ (t2 x t3) x (t4 x t5) ]
  @endverbatim

  At the point in time when check_interleaving_with_nj() adds the table t5 to
  the query execution plan, QEP, it also directs the node named NJ2 to mark
  the table as covered. NJ2 does so by incrementing its @c counter
  member. Since all of NJ2's tables are now covered by the QEP, the algorithm
  proceeds up the tree to NJ1, incrementing its counter as well. All join
  nests are now completely covered by the QEP.

  backout_nj_state() does the above in reverse. As seen above, the node
  NJ1 contains the nodes t2, t3, and NJ2. Its counter being equal to 3 means
  that the plan covers t2, t3, and NJ2, @e and that the sub-plan (t4 x t5)
  completely covers NJ2. The removal of t5 from the partial plan will first
  decrement NJ2's counter to 1. It will then detect that NJ2 went from being
  completely to partially covered, and hence the algorithm must continue
  upwards to NJ1 and decrement its counter to 2. A subsequent removal of t4
  will however not influence NJ1 since it did not un-cover the last table in
  NJ2.

  @param remaining_tables remaining tables to optimize, must contain 'tab'
  @param tab              join table to remove, assumed to be the last in
                          current partial join order.
*/

void Optimize_table_order::backout_nj_state(const table_map remaining_tables
                                            [[maybe_unused]],
                                            const JOIN_TAB *tab) {
  assert(remaining_tables & tab->table_ref->map());

  /* Restore the nested join state */
  Table_ref *last_emb = tab->table_ref->embedding;

  for (; last_emb != emb_sjm_nest; last_emb = last_emb->embedding) {
    // Ignore join nests that are not outer joins.
    if (!last_emb->join_cond_optim()) continue;

    NESTED_JOIN *const nest = last_emb->nested_join;

    assert(nest->nj_counter > 0);

    cur_embedding_map |= nest->nj_map;

    bool was_fully_covered = nest->nj_total == nest->nj_counter;

    if (--nest->nj_counter == 0) cur_embedding_map &= ~nest->nj_map;

    if (!was_fully_covered) break;
  }
}

/**
   Calculate the lateral dependencies of the suffix of JOIN_TABs from tab_no
   to join->tables-1 in the final join plan, i.e. the plan contained in
   join->best_positions. This function is only called from asserts that verify
   the values cached in join->best_positions[table_no].m_suffix_lateral_deps.

   @param tab_no index of the first table in the suffix for which we calculate
                 the dependencies.
   @return the set of lateral dependencies.
   @see JOIN::calculate_deps_of_remaining_lateral_derived_tables()
*/
table_map Optimize_table_order::calculate_lateral_deps_of_final_plan(
    uint tab_no) const {
  assert(tab_no <= join->tables);
  assert(got_final_plan);
  assert(!plan_has_duplicate_tabs());
  table_map deps = 0;

  for (uint i = tab_no; i < join->tables; i++) {
    const JOIN_TAB &tab = *join->best_positions[i].table;
    if (tab.table_ref->map() & ~excluded_tables) {
      deps |= get_lateral_deps(tab);
    }
  }

  return deps;
}

/**
   Set join->deps_of_remaining_lateral_derived_tables to the
   set of lateral dependencies of the tables in the suffix
   of the join plan from 'tab_no' and on.
   @param first_tab_no index (in the join order) of the first JOIN_TAB
   in the suffix.
*/
void Optimize_table_order::recalculate_lateral_deps(uint first_tab_no) {
  assert(first_tab_no <= join->tables);
  assert(!plan_has_duplicate_tabs());

  if (join->has_lateral) {
    if (first_tab_no == join->tables) {
      join->deps_of_remaining_lateral_derived_tables = 0;
    } else if (got_final_plan) {
      join->deps_of_remaining_lateral_derived_tables =
          join->best_positions[first_tab_no].get_suffix_lateral_deps();
      assert(join->deps_of_remaining_lateral_derived_tables ==
             calculate_lateral_deps_of_final_plan(first_tab_no));
    } else {
      join->deps_of_remaining_lateral_derived_tables =
          join->calculate_deps_of_remaining_lateral_derived_tables(
              ~excluded_tables, first_tab_no);
    }
  }
}

/**
   Update join->deps_of_remaining_lateral_derived_tables after adding
   JOIN_TAB first_tab_no-1 to the plan.
   Precondition: deps_of_remaining_lateral_derived_tables must contain
   the dependencies of the plan suffix from first_tab_no-1 and on.
   @param first_tab_no index (in the join order) of the first JOIN_TAB
   in the suffix.

   This method intends to be faster than recalculate_lateral_deps(),
   as it only calculates the increment change of adding on more table.
   But it requires the precondition above to be fulfilled.
*/
void Optimize_table_order::recalculate_lateral_deps_incrementally(
    uint first_tab_no) {
  assert(first_tab_no > 0 && first_tab_no <= join->tables);
  assert(!plan_has_duplicate_tabs());

  if (join->has_lateral) {
    /*
      This function requires join->deps_of_remaining_lateral_derived_tables
      to contain the dependencies of the lateral derived tables from
      join->best_ref[next_idx-1] and on. The assert below checks that this
      precondition holds.
    */
    assert(got_final_plan ||
           join->deps_of_remaining_lateral_derived_tables ==
               join->calculate_deps_of_remaining_lateral_derived_tables(
                   ~excluded_tables, first_tab_no - 1));

    if (first_tab_no == join->tables) {
      join->deps_of_remaining_lateral_derived_tables = 0;

      /*
        We have just added join->best_ref[first_tab_no - 1] to the plan; if it
        is not lateral, the map doesn't change, no need to recalculate it.
      */
    } else if (got_final_plan ||
               get_lateral_deps(*join->best_ref[first_tab_no - 1]) != 0) {
      recalculate_lateral_deps(first_tab_no);
    }
  }
}

/**
   Check if any Table_ref appears twice in the plan (which is an error).
   @return 'true' if there are any duplicates.
*/
bool Optimize_table_order::plan_has_duplicate_tabs() const {
  table_map plan{0};
  for (uint i = 0; i < join->tables; i++) {
    Table_ref *const tab_ref = got_final_plan
                                   ? join->best_positions[i].table->table_ref
                                   : join->best_ref[i]->table_ref;

    if (tab_ref != nullptr) {
      if ((plan & tab_ref->map()) != 0) {
        return true;
      }
      plan |= tab_ref->map();
    }
  }
  return false;
}

/**
   Helper function to write the current plan's prefix to the optimizer trace.
*/
static void trace_plan_prefix(JOIN *join, uint idx, table_map excluded_tables) {
  THD *const thd = join->thd;
  Opt_trace_array plan_prefix(&thd->opt_trace, "plan_prefix");
  for (uint i = 0; i < idx; i++) {
    Table_ref *const tr = join->positions[i].table->table_ref;
    if (!(tr->map() & excluded_tables)) {
      StringBuffer<32> str;
      tr->print(
          thd, &str,
          enum_query_type(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER |
                          QT_NO_DEFAULT_DB | QT_DERIVED_TABLE_ONLY_ALIAS));
      plan_prefix.add_utf8(str.ptr(), str.length());
    }
  }
}

/**
  @} (end of group Query_Planner)
*/