summaryrefslogtreecommitdiff
path: root/sql/opt_range.cc
blob: 40184d7a421d3e5afe1c7ceb3ac1edbf5888a22c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
/* Copyright (c) 2000, 2015, Oracle and/or its affiliates.
   Copyright (c) 2008, 2015, MariaDB

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

   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 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 */

/*
  TODO:
  Fix that MAYBE_KEY are stored in the tree so that we can detect use
  of full hash keys for queries like:

  select s.id, kws.keyword_id from sites as s,kws where s.id=kws.site_id and kws.keyword_id in (204,205);

*/

/*
  This file contains:

  RangeAnalysisModule  
    A module that accepts a condition, index (or partitioning) description, 
    and builds lists of intervals (in index/partitioning space), such that 
    all possible records that match the condition are contained within the 
    intervals.
    The entry point for the range analysis module is get_mm_tree() function.
    
    The lists are returned in form of complicated structure of interlinked
    SEL_TREE/SEL_IMERGE/SEL_ARG objects.
    See quick_range_seq_next, find_used_partitions for examples of how to walk 
    this structure.
    All direct "users" of this module are located within this file, too.


  PartitionPruningModule
    A module that accepts a partitioned table, condition, and finds which
    partitions we will need to use in query execution. Search down for
    "PartitionPruningModule" for description.
    The module has single entry point - prune_partitions() function.


  Range/index_merge/groupby-minmax optimizer module  
    A module that accepts a table, condition, and returns 
     - a QUICK_*_SELECT object that can be used to retrieve rows that match
       the specified condition, or a "no records will match the condition" 
       statement.

    The module entry points are
      test_quick_select()
      get_quick_select_for_ref()


  Record retrieval code for range/index_merge/groupby-min-max.
    Implementations of QUICK_*_SELECT classes.

  KeyTupleFormat
  ~~~~~~~~~~~~~~
  The code in this file (and elsewhere) makes operations on key value tuples.
  Those tuples are stored in the following format:
  
  The tuple is a sequence of key part values. The length of key part value
  depends only on its type (and not depends on the what value is stored)
  
    KeyTuple: keypart1-data, keypart2-data, ...
  
  The value of each keypart is stored in the following format:
  
    keypart_data: [isnull_byte] keypart-value-bytes

  If a keypart may have a NULL value (key_part->field->real_maybe_null() can
  be used to check this), then the first byte is a NULL indicator with the 
  following valid values:
    1  - keypart has NULL value.
    0  - keypart has non-NULL value.

  <questionable-statement> If isnull_byte==1 (NULL value), then the following
  keypart->length bytes must be 0.
  </questionable-statement>

  keypart-value-bytes holds the value. Its format depends on the field type.
  The length of keypart-value-bytes may or may not depend on the value being
  stored. The default is that length is static and equal to 
  KEY_PART_INFO::length.
  
  Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the 
  value:
  
     keypart-value-bytes: value_length value_bytes

  The value_length part itself occupies HA_KEY_BLOB_LENGTH=2 bytes.

  See key_copy() and key_restore() for code to move data between index tuple
  and table record

  CAUTION: the above description is only sergefp's understanding of the 
           subject and may omit some details.
*/

#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation				// gcc: Class implementation
#endif

#include <my_global.h>
#include "sql_priv.h"
#include "key.h"        // is_key_used, key_copy, key_cmp, key_restore
#include "sql_parse.h"                          // check_stack_overrun
#include "sql_partition.h"    // get_part_id_func, PARTITION_ITERATOR,
                              // struct partition_info, NOT_A_PARTITION_ID
#include "records.h"          // init_read_record, end_read_record
#include <m_ctype.h>
#include "sql_select.h"
#include "sql_statistics.h"
#include "uniques.h"

#ifndef EXTRA_DEBUG
#define test_rb_tree(A,B) {}
#define test_use_count(A) {}
#endif

/*
  Convert double value to #rows. Currently this does floor(), and we
  might consider using round() instead.
*/
#define double2rows(x) ((ha_rows)(x))

/*
  this should be long enough so that any memcmp with a string that
  starts from '\0' won't cross is_null_string boundaries, even
  if the memcmp is optimized to compare 4- 8- or 16- bytes at once
*/
static uchar is_null_string[20]= {1,0};

/**
  Helper function to compare two SEL_ARG's.
*/
static bool all_same(const SEL_ARG *sa1, const SEL_ARG *sa2)
{
  if (sa1 == NULL && sa2 == NULL)
    return true;
  if ((sa1 != NULL && sa2 == NULL) || (sa1 == NULL && sa2 != NULL))
    return false;
  return sa1->all_same(sa2);
}

class SEL_IMERGE;

#define CLONE_KEY1_MAYBE 1
#define CLONE_KEY2_MAYBE 2
#define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)


/*
  While objects of the class SEL_ARG represent ranges for indexes or
  index infixes (including ranges for index prefixes and index suffixes),
  objects of the class SEL_TREE represent AND/OR formulas of such ranges.
  Currently an AND/OR formula represented by a SEL_TREE object can have
  at most three levels: 

    <SEL_TREE formula> ::= 
      [ <SEL_RANGE_TREE formula> AND ]
      [ <SEL_IMERGE formula> [ AND <SEL_IMERGE formula> ...] ]

    <SEL_RANGE_TREE formula> ::=
      <SEL_ARG formula> [ AND  <SEL_ARG_formula> ... ]

    <SEL_IMERGE formula> ::=  
      <SEL_RANGE_TREE formula> [ OR <SEL_RANGE_TREE formula> ]

  As we can see from the above definitions:
   - SEL_RANGE_TREE formula is a conjunction of SEL_ARG formulas
   - SEL_IMERGE formula is a disjunction of SEL_RANGE_TREE formulas
   - SEL_TREE formula is a conjunction of a SEL_RANGE_TREE formula
     and SEL_IMERGE formulas. 
  It's required above that a SEL_TREE formula has at least one conjunct.

  Usually we will consider normalized SEL_RANGE_TREE formulas where we use
  TRUE as conjunct members for those indexes whose SEL_ARG trees are empty.
  
  We will call an SEL_TREE object simply 'tree'. 
  The part of a tree that represents SEL_RANGE_TREE formula is called
  'range part' of the tree while the remaining part is called 'imerge part'. 
  If a tree contains only a range part then we call such a tree 'range tree'.
  Components of a range tree that represent SEL_ARG formulas are called ranges.
  If a tree does not contain any range part we call such a tree 'imerge tree'.
  Components of the imerge part of a tree that represent SEL_IMERGE formula
  are called imerges.

  Usually we'll designate:
    SEL_TREE formulas         by T_1,...,T_k
    SEL_ARG formulas          by R_1,...,R_k
    SEL_RANGE_TREE formulas   by RT_1,...,RT_k
    SEL_IMERGE formulas       by M_1,...,M_k
  Accordingly we'll use:
    t_1,...,t_k - to designate trees representing T_1,...,T_k
    r_1,...,r_k - to designate ranges representing R_1,...,R_k 
    rt_1,...,r_tk - to designate range trees representing RT_1,...,RT_k
    m_1,...,m_k - to designate imerges representing M_1,...,M_k

  SEL_TREE objects are usually built from WHERE conditions or
  ON expressions.
  A SEL_TREE object always represents an inference of the condition it is
  built from. Therefore, if a row satisfies a SEL_TREE formula it also
  satisfies the condition it is built from.

  The following transformations of tree t representing SEL_TREE formula T 
  yield a new tree t1 thar represents an inference of T: T=>T1.  
    (1) remove any of SEL_ARG tree from the range part of t
    (2) remove any imerge from the tree t 
    (3) remove any of SEL_ARG tree from any range tree contained
        in any imerge of tree   
 
  Since the basic blocks of any SEL_TREE objects are ranges, SEL_TREE
  objects in many cases can be effectively used to filter out a big part
  of table rows that do not satisfy WHERE/IN conditions utilizing
  only single or multiple range index scans.

  A single range index scan is constructed for a range tree that contains
  only one SEL_ARG object for an index or an index prefix.
  An index intersection scan can be constructed for a range tree
  that contains several SEL_ARG objects. Currently index intersection
  scans are constructed only for single-point ranges.
  An index merge scan is constructed for a imerge tree that contains only
  one imerge. If range trees of this imerge contain only single-point merges
  than a union of index intersections can be built.

  Usually the tree built by the range optimizer for a query table contains
  more than one range in the range part, and additionally may contain some
  imerges in the imerge part. The range optimizer evaluates all of them one
  by one and chooses the range or the imerge that provides the cheapest
  single or multiple range index scan of the table.  According to rules 
  (1)-(3) this scan always filter out only those rows that do not satisfy
  the query conditions. 

  For any condition the SEL_TREE object for it is built in a bottom up
  manner starting from the range trees for the predicates. The tree_and
  function builds a tree for any conjunction of formulas from the trees
  for its conjuncts. The tree_or function builds a tree for any disjunction
  of formulas from the trees for its disjuncts.    
*/ 
  
class SEL_TREE :public Sql_alloc
{
public:
  /*
    Starting an effort to document this field:
    (for some i, keys[i]->type == SEL_ARG::IMPOSSIBLE) => 
       (type == SEL_TREE::IMPOSSIBLE)
  */
  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;

  SEL_TREE(enum Type type_arg, MEM_ROOT *root, size_t num_keys)
    : type(type_arg), keys(root, num_keys), n_ror_scans(0)
  {
    keys_map.clear_all();
  }

  SEL_TREE(MEM_ROOT *root, size_t num_keys) :
    type(KEY), keys(root, num_keys), n_ror_scans(0)
  { 
    keys_map.clear_all();
  }
   
  SEL_TREE(SEL_TREE *arg, bool without_merges, RANGE_OPT_PARAM *param);
  /*
    Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
    keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
    merit in range analyzer functions (e.g. get_mm_parts) returning a
    pointer to such SEL_TREE instead of NULL)
  */
  Mem_root_array<SEL_ARG *, true> keys;

  key_map keys_map;        /* bitmask of non-NULL elements in keys */

  /*
    Possible ways to read rows using index_merge. The list is non-empty only
    if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
  */
  List<SEL_IMERGE> merges;

  /* The members below are filled/used only after get_mm_tree is done */
  key_map ror_scans_map;   /* bitmask of ROR scan-able elements in keys */
  uint    n_ror_scans;     /* number of set bits in ror_scans_map */

  struct st_index_scan_info **index_scans;     /* list of index scans */
  struct st_index_scan_info **index_scans_end; /* last index scan */

  struct st_ror_scan_info **ror_scans;     /* list of ROR key scans */
  struct st_ror_scan_info **ror_scans_end; /* last ROR scan */
  /* Note that #records for each key scan is stored in table->quick_rows */

  bool without_ranges() { return keys_map.is_clear_all(); }
  bool without_imerges() { return merges.is_empty(); }
};


class PARAM : public RANGE_OPT_PARAM
{
public:
  ha_rows quick_rows[MAX_KEY];

  /*
    This will collect 'possible keys' based on the range optimization.
    
    Queries with a JOIN object actually use ref optimizer (see add_key_field)
    to collect possible_keys. This is used by single table UPDATE/DELETE.
  */
  key_map possible_keys;
  longlong baseflag;
  uint max_key_part, range_count;

  bool quick;				// Don't calulate possible keys

  uint fields_bitmap_size;
  MY_BITMAP needed_fields;    /* bitmask of fields needed by the query */
  MY_BITMAP tmp_covered_fields;

  key_map *needed_reg;        /* ptr to SQL_SELECT::needed_reg */

  uint *imerge_cost_buff;     /* buffer for index_merge cost estimates */
  uint imerge_cost_buff_size; /* size of the buffer */

  /* TRUE if last checked tree->key can be used for ROR-scan */
  bool is_ror_scan;
  /* Number of ranges in the last checked tree->key */
  uint n_ranges;
  uint8 first_null_comp; /* first null component if any, 0 - otherwise */
};


class TABLE_READ_PLAN;
  class TRP_RANGE;
  class TRP_ROR_INTERSECT;
  class TRP_ROR_UNION;
  class TRP_INDEX_INTERSECT;
  class TRP_INDEX_MERGE;
  class TRP_GROUP_MIN_MAX;

struct st_index_scan_info;
struct st_ror_scan_info;

static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts);
static ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
                                  SEL_ARG *tree, bool update_tbl_stats, 
                                  uint *mrr_flags, uint *bufsize,
                                  Cost_estimate *cost);

QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint index,
                                     SEL_ARG *key_tree, uint mrr_flags, 
                                     uint mrr_buf_size, MEM_ROOT *alloc);
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
                                       bool index_read_must_be_used,
                                       bool update_tbl_stats,
                                       double read_time);
static
TRP_INDEX_INTERSECT *get_best_index_intersect(PARAM *param, SEL_TREE *tree,
                                              double read_time);
static
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
                                          double read_time,
                                          bool *are_all_covering);
static
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
                                                   SEL_TREE *tree,
                                                   double read_time);
static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
                                         double read_time);
static
TABLE_READ_PLAN *merge_same_index_scans(PARAM *param, SEL_IMERGE *imerge,
                                        TRP_INDEX_MERGE *imerge_trp,
                                        double read_time);
static
TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree,
                                          double read_time);

#ifndef DBUG_OFF
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
                           const char *msg);
static void print_ror_scans_arr(TABLE *table, const char *msg,
                                struct st_ror_scan_info **start,
                                struct st_ror_scan_info **end);
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
#endif

static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,
                          SEL_TREE *tree1, SEL_TREE *tree2);
static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,
                         SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
static SEL_ARG *key_or(RANGE_OPT_PARAM *param,
                       SEL_ARG *key1, SEL_ARG *key2);
static SEL_ARG *key_and(RANGE_OPT_PARAM *param,
                        SEL_ARG *key1, SEL_ARG *key2,
                        uint clone_flag);
static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
                    SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
                    uchar *max_key,uint max_key_flag);
static bool eq_tree(SEL_ARG* a,SEL_ARG *b);

static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
static bool null_part_in_key(KEY_PART *key_part, const uchar *key,
                             uint length);
static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts);

#include "opt_range_mrr.cc"

static bool sel_trees_have_common_keys(SEL_TREE *tree1, SEL_TREE *tree2, 
                                       key_map *common_keys);
static void eliminate_single_tree_imerges(RANGE_OPT_PARAM *param,
                                          SEL_TREE *tree);

static bool sel_trees_can_be_ored(RANGE_OPT_PARAM* param,
                                  SEL_TREE *tree1, SEL_TREE *tree2, 
                                  key_map *common_keys);
static bool sel_trees_must_be_ored(RANGE_OPT_PARAM* param,
                                   SEL_TREE *tree1, SEL_TREE *tree2,
                                   key_map common_keys);
static int and_range_trees(RANGE_OPT_PARAM *param,
                           SEL_TREE *tree1, SEL_TREE *tree2,
                           SEL_TREE *result);
static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree);


/*
  SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
  a condition in the following form:
   (t_1||t_2||...||t_N) && (next)

  where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
  (t_i,t_j) contains SEL_ARGS for the same index.

  SEL_TREE contained in SEL_IMERGE always has merges=NULL.

  This class relies on memory manager to do the cleanup.
*/

class SEL_IMERGE : public Sql_alloc
{
  enum { PREALLOCED_TREES= 10};
public:
  SEL_TREE *trees_prealloced[PREALLOCED_TREES];
  SEL_TREE **trees;             /* trees used to do index_merge   */
  SEL_TREE **trees_next;        /* last of these trees            */
  SEL_TREE **trees_end;         /* end of allocated space         */

  SEL_ARG  ***best_keys;        /* best keys to read in SEL_TREEs */

  SEL_IMERGE() :
    trees(&trees_prealloced[0]),
    trees_next(trees),
    trees_end(trees + PREALLOCED_TREES)
  {}
  SEL_IMERGE (SEL_IMERGE *arg, uint cnt, RANGE_OPT_PARAM *param);
  int or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree);
  bool have_common_keys(RANGE_OPT_PARAM *param, SEL_TREE *tree);
  int and_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree, 
                   SEL_IMERGE *new_imerge);
  int or_sel_tree_with_checks(RANGE_OPT_PARAM *param,
                              uint n_init_trees, 
                              SEL_TREE *new_tree,
                              bool is_first_check_pass,
                              bool *is_last_check_pass);
  int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param,
                                uint n_init_trees,
                                SEL_IMERGE* imerge,
                                bool is_first_check_pass,
                                bool *is_last_check_pass);
};


/*
  Add a range tree to the range trees of this imerge 

  SYNOPSIS
    or_sel_tree()
      param                  Context info for the operation         
      tree                   SEL_TREE to add to this imerge 

  DESCRIPTION 
    The function just adds the range tree 'tree' to the range trees
    of this imerge.

  RETURN
     0   if the operation is success
    -1   if the function runs out memory
*/

int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
  if (trees_next == trees_end)
  {
    const int realloc_ratio= 2;		/* Double size for next round */
    uint old_elements= (trees_end - trees);
    uint old_size= sizeof(SEL_TREE**) * old_elements;
    uint new_size= old_size * realloc_ratio;
    SEL_TREE **new_trees;
    if (!(new_trees= (SEL_TREE**)alloc_root(param->mem_root, new_size)))
      return -1;
    memcpy(new_trees, trees, old_size);
    trees=      new_trees;
    trees_next= trees + old_elements;
    trees_end=  trees + old_elements * realloc_ratio;
  }
  *(trees_next++)= tree;
  return 0;
}


/*
  Check if any of the range trees of this imerge intersects with a given tree 

  SYNOPSIS
    have_common_keys()
      param    Context info for the function
      tree     SEL_TREE intersection with the imerge range trees is checked for 

  DESCRIPTION
    The function checks whether there is any range tree rt_i in this imerge
    such that there are some indexes for which ranges are defined in both
    rt_i and the range part of the SEL_TREE tree.  
    To check this the function calls the function sel_trees_have_common_keys.

  RETURN 
    TRUE    if there are such range trees in this imerge
    FALSE   otherwise
*/

bool SEL_IMERGE::have_common_keys(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
  for (SEL_TREE** or_tree= trees, **bound= trees_next;
       or_tree != bound; or_tree++)
  {
    key_map common_keys;
    if (sel_trees_have_common_keys(*or_tree, tree, &common_keys))
      return TRUE;
  }
  return FALSE;
}


/* 
  Perform AND operation for this imerge and the range part of a tree

  SYNOPSIS
    and_sel_tree()
      param           Context info for the operation
      tree            SEL_TREE for the second operand of the operation
      new_imerge  OUT imerge for the result of the operation

  DESCRIPTION
    This function performs AND operation for this imerge m and the
    range part of the SEL_TREE tree rt. In other words the function
    pushes rt into this imerge. The resulting imerge is returned in
    the parameter new_imerge.
    If this imerge m represent the formula
      RT_1 OR ... OR RT_k
    then the resulting imerge of the function represents the formula
      (RT_1 AND RT) OR ... OR (RT_k AND RT)
    The function calls the function and_range_trees to construct the
    range tree representing (RT_i AND RT).
    
  NOTE
    The function may return an empty imerge without any range trees.
    This happens when each call of and_range_trees returns an 
    impossible range tree (SEL_TREE::IMPOSSIBLE).
    Example: (key1 < 2 AND key2 > 10) AND (key1 > 4 OR key2 < 6).
         
  RETURN
     0  if the operation is a success
    -1  otherwise: there is not enough memory to perform the operation
*/

int SEL_IMERGE::and_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree,
                             SEL_IMERGE *new_imerge)
{
  for (SEL_TREE** or_tree= trees; or_tree != trees_next; or_tree++) 
  {
    SEL_TREE *res_or_tree= 0;
    SEL_TREE *and_tree= 0;
    if (!(res_or_tree= new SEL_TREE(param->mem_root, param->keys)) ||
        !(and_tree= new SEL_TREE(tree, TRUE, param)))
      return (-1);
    if (!and_range_trees(param, *or_tree, and_tree, res_or_tree))
    {
      if (new_imerge->or_sel_tree(param, res_or_tree))
        return (-1);
    }        
  }
  return 0;
}      


/*
  Perform OR operation on this imerge and the range part of a tree

  SYNOPSIS
    or_sel_tree_with_checks()
      param                  Context info for the operation 
      n_trees                Number of trees in this imerge to check for oring        
      tree                   SEL_TREE whose range part is to be ored 
      is_first_check_pass    <=> the first call of the function for this imerge  
      is_last_check_pass OUT <=> no more calls of the function for this imerge

  DESCRIPTION
    The function performs OR operation on this imerge m and the range part
    of the SEL_TREE tree rt. It always replaces this imerge with the result
    of the operation.
 
    The operation can be performed in two different modes: with
    is_first_check_pass==TRUE and is_first_check_pass==FALSE, transforming
    this imerge differently.

    Given this imerge represents the formula
      RT_1 OR ... OR RT_k:

    1. In the first mode, when is_first_check_pass==TRUE :
      1.1. If rt must be ored(see the function sel_trees_must_be_ored) with
           some rt_j (there may be only one such range tree in the imerge)
           then the function produces an imerge representing the formula
             RT_1 OR ... OR (RT_j OR RT) OR ... OR RT_k,
           where the tree for (RT_j OR RT) is built by oring the pairs
           of SEL_ARG trees for the corresponding indexes
      1.2. Otherwise the function produces the imerge representing the formula:
           RT_1 OR ... OR RT_k OR RT.

    2. In the second mode, when is_first_check_pass==FALSE :
      2.1. For each rt_j in the imerge that can be ored (see the function
           sel_trees_can_be_ored) with rt the function replaces rt_j for a
           range tree such that for each index for which ranges are defined
           in both in rt_j and rt  the tree contains the  result of oring of
           these ranges.
      2.2. In other cases the function does not produce any imerge.

    When is_first_check==TRUE the function returns FALSE in the parameter
    is_last_check_pass if there is no rt_j such that rt_j can be ored with rt,
    but, at the same time, it's not true that rt_j must be ored with rt.
    When is_first_check==FALSE the function always returns FALSE in the
    parameter is_last_check_pass.    
          
  RETURN
    1  The result of oring of rt_j and rt that must be ored returns the
       the range tree with type==SEL_TREE::ALWAYS
       (in this case the imerge m should be discarded)
   -1  The function runs out of memory
    0  in all other cases 
*/

int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param,
                                        uint n_trees,
                                        SEL_TREE *tree,
                                        bool is_first_check_pass,
                                        bool *is_last_check_pass)
{
  bool was_ored= FALSE;
  *is_last_check_pass= is_first_check_pass;
  SEL_TREE** or_tree = trees;
  for (uint i= 0; i < n_trees; i++, or_tree++)
  {
    SEL_TREE *result= 0;
    key_map result_keys;
    key_map ored_keys;
    if (sel_trees_can_be_ored(param, *or_tree, tree, &ored_keys))
    {
      bool must_be_ored= sel_trees_must_be_ored(param, *or_tree, tree,
                                                ored_keys);
      if (must_be_ored || !is_first_check_pass)
      {
        result_keys.clear_all();
        result= *or_tree;
        for (uint key_no= 0; key_no < param->keys; key_no++)
        {
          if (!ored_keys.is_set(key_no))
	  {
            result->keys[key_no]= 0;
	    continue;
          }
          SEL_ARG *key1= (*or_tree)->keys[key_no];
          SEL_ARG *key2= tree->keys[key_no];
          key2->incr_refs();
          if ((result->keys[key_no]= key_or(param, key1, key2)))
          {
            
            result_keys.set_bit(key_no);
#ifdef EXTRA_DEBUG
            if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
	    {
              key1= result->keys[key_no]; 
              (key1)->test_use_count(key1);
            }
#endif
          }       
        }
      }
      else if(is_first_check_pass) 
        *is_last_check_pass= FALSE;
    } 

    if (result)
    {
      result->keys_map= result_keys;
      if (result_keys.is_clear_all())
        result->type= SEL_TREE::ALWAYS;
      if ((result->type == SEL_TREE::MAYBE) ||
          (result->type == SEL_TREE::ALWAYS))
        return 1;
      /* SEL_TREE::IMPOSSIBLE is impossible here */
      *or_tree= result;
      was_ored= TRUE;
    }
  }
  if (was_ored)
    return 0;

  if (is_first_check_pass && !*is_last_check_pass &&
      !(tree= new SEL_TREE(tree, FALSE, param)))
    return (-1);
  return or_sel_tree(param, tree);
}


/*
  Perform OR operation on this imerge and and another imerge

  SYNOPSIS
    or_sel_imerge_with_checks()
      param                  Context info for the operation 
      n_trees           Number of trees in this imerge to check for oring        
      imerge                 The second operand of the operation 
      is_first_check_pass    <=> the first call of the function for this imerge  
      is_last_check_pass OUT <=> no more calls of the function for this imerge

  DESCRIPTION
    For each range tree rt from 'imerge' the function calls the method
    SEL_IMERGE::or_sel_tree_with_checks that performs OR operation on this
    SEL_IMERGE object m and the tree rt. The mode of the operation is
    specified by the parameter is_first_check_pass. Each call of
    SEL_IMERGE::or_sel_tree_with_checks transforms this SEL_IMERGE object m.
    The function returns FALSE in the prameter is_last_check_pass if
    at least one of the calls of SEL_IMERGE::or_sel_tree_with_checks
    returns FALSE as the value of its last parameter. 
    
  RETURN
    1  One of the calls of SEL_IMERGE::or_sel_tree_with_checks returns 1.
       (in this case the imerge m should be discarded)
   -1  The function runs out of memory
    0  in all other cases 
*/

int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param,
                                          uint n_trees,
                                          SEL_IMERGE* imerge,
                                          bool is_first_check_pass,
                                          bool *is_last_check_pass)
{
  *is_last_check_pass= TRUE;
  SEL_TREE** tree= imerge->trees;
  SEL_TREE** tree_end= imerge->trees_next;
  for ( ; tree < tree_end; tree++)
  {
    uint rc;
    bool is_last= TRUE; 
    rc= or_sel_tree_with_checks(param, n_trees, *tree, 
                               is_first_check_pass, &is_last);
    if (!is_last)
      *is_last_check_pass= FALSE;
    if (rc)
      return rc;
  }
  return 0;
}


/*
  Copy constructor for SEL_TREE objects

  SYNOPSIS
    SEL_TREE
      arg            The source tree for the constructor
      without_merges <=> only the range part of the tree arg is copied
      param          Context info for the operation

  DESCRIPTION
    The constructor creates a full copy of the SEL_TREE arg if
    the prameter without_merges==FALSE. Otherwise a tree is created
    that contains the copy only of the range part of the tree arg. 
*/ 

SEL_TREE::SEL_TREE(SEL_TREE *arg, bool without_merges,
                   RANGE_OPT_PARAM *param) 
  : Sql_alloc(),
    keys(param->mem_root, param->keys),
    n_ror_scans(0)
{
  keys_map= arg->keys_map;
  type= arg->type;
  MEM_ROOT *mem_root;

  for (uint idx= 0; idx < param->keys; idx++)
  {
    if ((keys[idx]= arg->keys[idx]))
      keys[idx]->incr_refs_all();
  }

  if (without_merges)
    return;

  mem_root= current_thd->mem_root;
  List_iterator<SEL_IMERGE> it(arg->merges);
  for (SEL_IMERGE *el= it++; el; el= it++)
  {
    SEL_IMERGE *merge= new (mem_root) SEL_IMERGE(el, 0, param);
    if (!merge || merge->trees == merge->trees_next)
    {
      merges.empty();
      return;
    }
    merges.push_back(merge, mem_root);
  }
}


/*
  Copy constructor for SEL_IMERGE objects

  SYNOPSIS
    SEL_IMERGE
      arg         The source imerge for the constructor
      cnt         How many trees from arg are to be copied
      param       Context info for the operation

  DESCRIPTION
    The cnt==0 then the constructor creates a full copy of the 
    imerge arg. Otherwise only the first cnt trees of the imerge
    are copied.
*/ 

SEL_IMERGE::SEL_IMERGE(SEL_IMERGE *arg, uint cnt,
                       RANGE_OPT_PARAM *param) : Sql_alloc()
{
  uint elements= (arg->trees_end - arg->trees);
  if (elements > PREALLOCED_TREES)
  {
    uint size= elements * sizeof (SEL_TREE **);
    if (!(trees= (SEL_TREE **)alloc_root(param->mem_root, size)))
      goto mem_err;
  }
  else
    trees= &trees_prealloced[0];

  trees_next= trees + (cnt ? cnt : arg->trees_next-arg->trees);
  trees_end= trees + elements;

  for (SEL_TREE **tree = trees, **arg_tree= arg->trees; tree < trees_next; 
       tree++, arg_tree++)
  {
    if (!(*tree= new SEL_TREE(*arg_tree, TRUE, param)))
      goto mem_err;
  }

  return;

mem_err:
  trees= &trees_prealloced[0];
  trees_next= trees;
  trees_end= trees;
}


/*
  Perform AND operation on two imerge lists

  SYNOPSIS
    imerge_list_and_list()
      param             Context info for the operation         
      im1               The first imerge list for the operation
      im2               The second imerge list for the operation

  DESCRIPTION
    The function just appends the imerge list im2 to the imerge list im1  
    
  RETURN VALUE
    none
*/

inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
{
  im1->append(im2);
}


/*
  Perform OR operation on two imerge lists

  SYNOPSIS
    imerge_list_or_list()
      param             Context info for the operation         
      im1               The first imerge list for the operation
      im2               The second imerge list for the operation
     
  DESCRIPTION
    Assuming that the first imerge list represents the formula
      F1= M1_1 AND ... AND M1_k1 
    while the second imerge list represents the formula 
      F2= M2_1 AND ... AND M2_k2,
    where M1_i= RT1_i_1 OR ... OR RT1_i_l1i (i in [1..k1])
    and M2_i = RT2_i_1 OR ... OR RT2_i_l2i (i in [1..k2]),
    the function builds a list of imerges for some formula that can be 
    inferred from the formula (F1 OR F2).

    More exactly the function builds imerges for the formula (M1_1 OR M2_1).
    Note that
      (F1 OR F2) = (M1_1 AND ... AND M1_k1) OR (M2_1 AND ... AND M2_k2) =
      AND (M1_i OR M2_j) (i in [1..k1], j in [1..k2]) =>
      M1_1 OR M2_1.
    So (M1_1 OR M2_1) is indeed an inference formula for (F1 OR F2).

    To build imerges for the formula (M1_1 OR M2_1) the function invokes,
    possibly twice, the method SEL_IMERGE::or_sel_imerge_with_checks
    for the imerge m1_1.
    At its first invocation the method SEL_IMERGE::or_sel_imerge_with_checks
    performs OR operation on the imerge m1_1 and the range tree rt2_1_1 by
    calling SEL_IMERGE::or_sel_tree_with_checks with is_first_pass_check==TRUE.
    The resulting imerge of the operation is ored with the next range tree of
    the imerge m2_1. This oring continues until the last range tree from
    m2_1 has been ored. 
    At its second invocation the method SEL_IMERGE::or_sel_imerge_with_checks
    performs the same sequence of OR operations, but now calling
    SEL_IMERGE::or_sel_tree_with_checks with is_first_pass_check==FALSE.

    The imerges that the operation produces replace those in the list im1   
       
  RETURN
    0     if the operation is a success 
   -1     if the function has run out of memory 
*/

int imerge_list_or_list(RANGE_OPT_PARAM *param,
                        List<SEL_IMERGE> *im1,
                        List<SEL_IMERGE> *im2)
{

  uint rc;
  bool is_last_check_pass= FALSE;
  SEL_IMERGE *imerge= im1->head();
  uint elems= imerge->trees_next-imerge->trees;
  MEM_ROOT *mem_root= current_thd->mem_root;

  im1->empty();
  im1->push_back(imerge, mem_root);

  rc= imerge->or_sel_imerge_with_checks(param, elems, im2->head(),
                                        TRUE, &is_last_check_pass);
  if (rc)
  {
    if (rc == 1)
    {
      im1->empty();
      rc= 0;
    }
    return rc;
  }

  if (!is_last_check_pass)
  {
    SEL_IMERGE* new_imerge= new (mem_root) SEL_IMERGE(imerge, elems, param);
    if (new_imerge)
    {
      is_last_check_pass= TRUE;
      rc= new_imerge->or_sel_imerge_with_checks(param, elems, im2->head(),
                                                 FALSE, &is_last_check_pass);
      if (!rc)
        im1->push_back(new_imerge, mem_root); 
    }
  }
  return rc;  
}


/*
  Perform OR operation for each imerge from a list and the range part of a tree

  SYNOPSIS
    imerge_list_or_tree()
      param       Context info for the operation
      merges      The list of imerges to be ored with the range part of tree          
      tree        SEL_TREE whose range part is to be ored with the imerges

  DESCRIPTION
    For each imerge mi from the list 'merges' the function performes OR
    operation with mi and the range part of 'tree' rt, producing one or
    two imerges.

    Given the merge mi represent the formula RTi_1 OR ... OR RTi_k, 
    the function forms the merges by the following rules:
 
    1. If rt cannot be ored with any of the trees rti the function just
       produces an imerge that represents the formula
         RTi_1 OR ... RTi_k OR RT.
    2. If there exist a tree rtj that must be ored with rt the function
       produces an imerge the represents the formula
         RTi_1 OR ... OR (RTi_j OR RT) OR ... OR RTi_k,
       where the range tree for (RTi_j OR RT) is constructed by oring the
       SEL_ARG trees that must be ored.
    3. For each rti_j that can be ored with rt the function produces
       the new tree rti_j' and substitutes rti_j for this new range tree.

    In any case the function removes mi from the list and then adds all
    produced imerges.

    To build imerges by rules 1-3 the function calls the method
    SEL_IMERGE::or_sel_tree_with_checks, possibly twice. With the first
    call it passes TRUE for the third parameter of the function.
    At this first call imerges by rules 1-2 are built. If the call
    returns FALSE as the return value of its fourth parameter then the
    function are called for the second time. At this call the imerge
    of rule 3 is produced.

    If a call of SEL_IMERGE::or_sel_tree_with_checks returns 1 then
    then it means that the produced tree contains an always true
    range tree and the whole imerge can be discarded.
    
  RETURN
    1     if no imerges are produced
    0     otherwise
*/

static
int imerge_list_or_tree(RANGE_OPT_PARAM *param,
                        List<SEL_IMERGE> *merges,
                        SEL_TREE *tree)
{
  SEL_IMERGE *imerge;
  List<SEL_IMERGE> additional_merges;
  List_iterator<SEL_IMERGE> it(*merges);
  MEM_ROOT *mem_root= current_thd->mem_root;
  
  while ((imerge= it++))
  {
    bool is_last_check_pass;
    int rc= 0;
    int rc1= 0;
    SEL_TREE *or_tree= new (mem_root) SEL_TREE (tree, FALSE, param);
    if (or_tree)
    {
      uint elems= imerge->trees_next-imerge->trees;
      rc= imerge->or_sel_tree_with_checks(param, elems, or_tree,
                                          TRUE, &is_last_check_pass);
      if (!is_last_check_pass)
      {
        SEL_IMERGE *new_imerge= new (mem_root) SEL_IMERGE(imerge, elems,
                                                          param);
        if (new_imerge)
	{ 
          rc1= new_imerge->or_sel_tree_with_checks(param, elems, or_tree,
                                                   FALSE, &is_last_check_pass);
          if (!rc1)
            additional_merges.push_back(new_imerge, mem_root);
        }
      }
    }
    if (rc || rc1 || !or_tree)
      it.remove();
  }

  merges->append(&additional_merges);
  return merges->is_empty();
}


/*
  Perform pushdown operation of the range part of a tree into given imerges 

  SYNOPSIS
    imerge_list_and_tree()
      param           Context info for the operation
      merges   IN/OUT List of imerges to push the range part of 'tree' into
      tree            SEL_TREE whose range part is to be pushed into imerges
      replace         if the pushdow operation for a imerge is a success
                      then the original imerge is replaced for the result
                      of the pushdown 

  DESCRIPTION
    For each imerge from the list merges the function pushes the range part
    rt of 'tree' into the imerge. 
    More exactly if the imerge mi from the list represents the formula
      RTi_1 OR ... OR RTi_k 
    the function bulds a new imerge that represents the formula
      (RTi_1 AND RT) OR ... OR (RTi_k AND RT)
    and adds this imerge to the list merges.
    To perform this pushdown operation the function calls the method
    SEL_IMERGE::and_sel_tree. 
    For any imerge mi the new imerge is not created if for each pair of
    trees rti_j and rt the intersection of the indexes with defined ranges
    is empty.
    If the result of the pushdown operation for the imerge mi returns an
    imerge with no trees then then not only nothing is added to the list 
    merges but mi itself is removed from the list. 

  TODO
    Optimize the code in order to not create new SEL_IMERGE and new SER_TREE
    objects when 'replace' is TRUE. (Currently this function is called always
    with this parameter equal to TRUE.)
    
  RETURN
    1    if no imerges are left in the list merges             
    0    otherwise
*/

static
int imerge_list_and_tree(RANGE_OPT_PARAM *param,
                         List<SEL_IMERGE> *merges,
                         SEL_TREE *tree, 
                         bool replace)
{
  SEL_IMERGE *imerge;
  SEL_IMERGE *new_imerge= NULL;
  List<SEL_IMERGE> new_merges;
  List_iterator<SEL_IMERGE> it(*merges);
  MEM_ROOT *mem_root= current_thd->mem_root;
  
  while ((imerge= it++))
  {
    if (!new_imerge)
      new_imerge= new (mem_root) SEL_IMERGE();
    if (imerge->have_common_keys(param, tree) && 
        new_imerge && !imerge->and_sel_tree(param, tree, new_imerge))
    {
      if (new_imerge->trees == new_imerge->trees_next)
        it.remove();
      else
      { 
        if (replace)
          it.replace(new_imerge);
        else        
          new_merges.push_back(new_imerge, mem_root);
        new_imerge= NULL;
      }
    }
  }
  imerge_list_and_list(&new_merges, merges);
  *merges= new_merges;
  return merges->is_empty();
}


/***************************************************************************
** Basic functions for SQL_SELECT and QUICK_RANGE_SELECT
***************************************************************************/

	/* make a select from mysql info
	   Error is set as following:
	   0 = ok
	   1 = Got some error (out of memory?)
	   */

SQL_SELECT *make_select(TABLE *head, table_map const_tables,
			table_map read_tables, COND *conds,
                        SORT_INFO *filesort,
                        bool allow_null_cond,
                        int *error)
{
  SQL_SELECT *select;
  DBUG_ENTER("make_select");

  *error=0;

  if (!conds && !allow_null_cond)
    DBUG_RETURN(0);
  if (!(select= new (head->in_use->mem_root) SQL_SELECT))
  {
    *error= 1;			// out of memory
    DBUG_RETURN(0);		/* purecov: inspected */
  }
  select->read_tables=read_tables;
  select->const_tables=const_tables;
  select->head=head;
  select->cond= conds;

  if (filesort && my_b_inited(&filesort->io_cache))
  {
    /*
      Hijack the filesort io_cache for make_select
      SQL_SELECT will be responsible for ensuring that it's properly freed.
    */
    select->file= filesort->io_cache;
    select->records=(ha_rows) (select->file.end_of_file/
			       head->file->ref_length);
    my_b_clear(&filesort->io_cache);
  }
  DBUG_RETURN(select);
}


SQL_SELECT::SQL_SELECT() :quick(0),cond(0),pre_idx_push_select_cond(NULL),free_cond(0)
{
  quick_keys.clear_all(); needed_reg.clear_all();
  my_b_clear(&file);
}


void SQL_SELECT::cleanup()
{
  delete quick;
  quick= 0;
  if (free_cond)
  {
    free_cond=0;
    delete cond;
    cond= 0;
  }
  close_cached_file(&file);
}


SQL_SELECT::~SQL_SELECT()
{
  cleanup();
}

#undef index					// Fix for Unixware 7

QUICK_SELECT_I::QUICK_SELECT_I()
  :max_used_key_length(0),
   used_key_parts(0)
{}

QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr,
                                       bool no_alloc, MEM_ROOT *parent_alloc,
                                       bool *create_error)
  :free_file(0),cur_range(NULL),last_range(0),dont_free(0)
{
  my_bitmap_map *bitmap;
  DBUG_ENTER("QUICK_RANGE_SELECT::QUICK_RANGE_SELECT");

  in_ror_merged_scan= 0;
  index= key_nr;
  head=  table;
  key_part_info= head->key_info[index].key_part;

  /* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */
  mrr_buf_size= thd->variables.mrr_buff_size;
  mrr_buf_desc= NULL;

  if (!no_alloc && !parent_alloc)
  {
    // Allocates everything through the internal memroot
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                   MYF(MY_THREAD_SPECIFIC));
    thd->mem_root= &alloc;
  }
  else
    bzero((char*) &alloc,sizeof(alloc));
  file= head->file;
  record= head->record[0];

  my_init_dynamic_array2(&ranges, sizeof(QUICK_RANGE*),
                         thd->alloc(sizeof(QUICK_RANGE*) * 16), 16, 16,
                         MYF(MY_THREAD_SPECIFIC));

  /* Allocate a bitmap for used columns */
  if (!(bitmap= (my_bitmap_map*) thd->alloc(head->s->column_bitmap_size)))
  {
    column_bitmap.bitmap= 0;
    *create_error= 1;
  }
  else
    my_bitmap_init(&column_bitmap, bitmap, head->s->fields, FALSE);
  DBUG_VOID_RETURN;
}


void QUICK_RANGE_SELECT::need_sorted_output()
{
  if (!(mrr_flags & HA_MRR_SORTED))
  {
    /*
      Native implementation can't produce sorted output. We'll have to
      switch to default
    */
    mrr_flags |= HA_MRR_USE_DEFAULT_IMPL; 
  }
  mrr_flags |= HA_MRR_SORTED;
}


int QUICK_RANGE_SELECT::init()
{
  DBUG_ENTER("QUICK_RANGE_SELECT::init");

  if (file->inited != handler::NONE)
    file->ha_index_or_rnd_end();
  DBUG_RETURN(FALSE);
}


void QUICK_RANGE_SELECT::range_end()
{
  if (file->inited != handler::NONE)
    file->ha_index_or_rnd_end();
}


QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
{
  DBUG_ENTER("QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT");
  if (!dont_free)
  {
    /* file is NULL for CPK scan on covering ROR-intersection */
    if (file) 
    {
      range_end();
      file->ha_end_keyread();
      if (free_file)
      {
        DBUG_PRINT("info", ("Freeing separate handler 0x%lx (free: %d)", (long) file,
                            free_file));
        file->ha_external_lock(current_thd, F_UNLCK);
        file->ha_close();
        delete file;
      }
    }
    delete_dynamic(&ranges); /* ranges are allocated in alloc */
    free_root(&alloc,MYF(0));
  }
  my_free(mrr_buf_desc);
  DBUG_VOID_RETURN;
}

/*
  QUICK_INDEX_SORT_SELECT works as follows:
  - Do index scans, accumulate rowids in the Unique object 
    (Unique will also sort and de-duplicate rowids)
  - Use rowids from unique to run a disk-ordered sweep
*/

QUICK_INDEX_SORT_SELECT::QUICK_INDEX_SORT_SELECT(THD *thd_param,
                                                 TABLE *table)
  :unique(NULL), pk_quick_select(NULL), thd(thd_param)
{
  DBUG_ENTER("QUICK_INDEX_SORT_SELECT::QUICK_INDEX_SORT_SELECT");
  index= MAX_KEY;
  head= table;
  bzero(&read_record, sizeof(read_record));
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                 MYF(MY_THREAD_SPECIFIC));
  DBUG_VOID_RETURN;
}

int QUICK_INDEX_SORT_SELECT::init()
{
  DBUG_ENTER("QUICK_INDEX_SORT_SELECT::init");
  DBUG_RETURN(0);
}

int QUICK_INDEX_SORT_SELECT::reset()
{
  DBUG_ENTER("QUICK_INDEX_SORT_SELECT::reset");
  const int retval= read_keys_and_merge();
  DBUG_RETURN(retval);
}

bool
QUICK_INDEX_SORT_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick_sel_range)
{
  DBUG_ENTER("QUICK_INDEX_SORT_SELECT::push_quick_back");
  if (head->file->primary_key_is_clustered() &&
      quick_sel_range->index == head->s->primary_key)
  {
   /*
     A quick_select over a clustered primary key is handled specifically
     Here we assume:
     - PK columns are included in any other merged index
     - Scan on the PK is disk-ordered.
       (not meeting #2 will only cause performance degradation)

       We could treat clustered PK as any other index, but that would
       be inefficient. There is no point in doing scan on
       CPK, remembering the rowid, then making rnd_pos() call with
       that rowid.
    */
    pk_quick_select= quick_sel_range;
    DBUG_RETURN(0);
  }
  DBUG_RETURN(quick_selects.push_back(quick_sel_range, thd->mem_root));
}

QUICK_INDEX_SORT_SELECT::~QUICK_INDEX_SORT_SELECT()
{
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
  QUICK_RANGE_SELECT* quick;
  DBUG_ENTER("QUICK_INDEX_SORT_SELECT::~QUICK_INDEX_SORT_SELECT");
  delete unique;
  quick_it.rewind();
  while ((quick= quick_it++))
    quick->file= NULL;
  quick_selects.delete_elements();
  delete pk_quick_select;
  /* It's ok to call the next two even if they are already deinitialized */
  end_read_record(&read_record);
  free_root(&alloc,MYF(0));
  DBUG_VOID_RETURN;
}

QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
                                                       TABLE *table,
                                                       bool retrieve_full_rows,
                                                       MEM_ROOT *parent_alloc)
  : cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows),
    scans_inited(FALSE)
{
  index= MAX_KEY;
  head= table;
  record= head->record[0];
  if (!parent_alloc)
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                   MYF(MY_THREAD_SPECIFIC));
  else
    bzero(&alloc, sizeof(MEM_ROOT));
  last_rowid= (uchar*) alloc_root(parent_alloc? parent_alloc : &alloc,
                                  head->file->ref_length);
}


/*
  Do post-constructor initialization.
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::init()

  RETURN
    0      OK
    other  Error code
*/

int QUICK_ROR_INTERSECT_SELECT::init()
{
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init");
 /* Check if last_rowid was successfully allocated in ctor */
  DBUG_RETURN(!last_rowid);
}


/*
  Initialize this quick select to be a ROR-merged scan.

  SYNOPSIS
    QUICK_RANGE_SELECT::init_ror_merged_scan()
      reuse_handler If TRUE, use head->file, otherwise create a separate
                    handler object

  NOTES
    This function creates and prepares for subsequent use a separate handler
    object if it can't reuse head->file. The reason for this is that during
    ROR-merge several key scans are performed simultaneously, and a single
    handler is only capable of preserving context of a single key scan.

    In ROR-merge the quick select doing merge does full records retrieval,
    merged quick selects read only keys.

  RETURN
    0  ROR child scan initialized, ok to use.
    1  error
*/

int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler,
                                             MEM_ROOT *local_alloc)
{
  handler *save_file= file, *org_file;
  THD *thd= head->in_use;
  MY_BITMAP * const save_vcol_set= head->vcol_set;
  MY_BITMAP * const save_read_set= head->read_set;
  MY_BITMAP * const save_write_set= head->write_set;
  DBUG_ENTER("QUICK_RANGE_SELECT::init_ror_merged_scan");

  in_ror_merged_scan= 1;
  if (reuse_handler)
  {
    DBUG_PRINT("info", ("Reusing handler 0x%lx", (long) file));
    if (init())
    {
      DBUG_RETURN(1);
    }
    goto end;
  }

  /* Create a separate handler object for this quick select */
  if (free_file)
  {
    /* already have own 'handler' object. */
    DBUG_RETURN(0);
  }

  if (!(file= head->file->clone(head->s->normalized_path.str, local_alloc)))
  {
    /* 
      Manually set the error flag. Note: there seems to be quite a few
      places where a failure could cause the server to "hang" the client by
      sending no response to a query. ATM those are not real errors because 
      the storage engine calls in question happen to never fail with the 
      existing storage engines. 
    */
    my_error(ER_OUT_OF_RESOURCES, MYF(0)); /* purecov: inspected */
    /* Caller will free the memory */
    goto failure;  /* purecov: inspected */
  }

  if (file->ha_external_lock(thd, F_RDLCK))
    goto failure;

  if (init())
  {
    file->ha_external_lock(thd, F_UNLCK);
    file->ha_close();
    goto failure;
  }
  free_file= TRUE;
  last_rowid= file->ref;

end:
  /*
    We are only going to read key fields and call position() on 'file'
    The following sets head->read_set (== column_bitmap) to only use this
    key. The 'column_bitmap' is used in ::get_next()
  */
  org_file= head->file;
  head->file= file;

  head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap, &column_bitmap);
  head->prepare_for_keyread(index, &column_bitmap);
  head->prepare_for_position();

  head->file= org_file;

  /* Restore head->read_set (and write_set) to what they had before the call */
  head->column_bitmaps_set(save_read_set, save_write_set);
 
  if (reset())
  {
    if (!reuse_handler)
    {
      file->ha_external_lock(thd, F_UNLCK);
      file->ha_close();
      goto failure;
    }
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);

failure:
  head->column_bitmaps_set(save_read_set, save_write_set, save_vcol_set);
  delete file;
  file= save_file;
  DBUG_RETURN(1);
}


/*
  Initialize this quick select to be a part of a ROR-merged scan.
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan()
      reuse_handler If TRUE, use head->file, otherwise create separate
                    handler object.
  RETURN
    0     OK
    other error code
*/
int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler, 
                                                     MEM_ROOT *local_alloc)
{
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> quick_it(quick_selects);
  QUICK_SELECT_WITH_RECORD *cur;
  QUICK_RANGE_SELECT *quick;
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan");

  /* Initialize all merged "children" quick selects */
  DBUG_ASSERT(!need_to_fetch_row || reuse_handler);
  if (!need_to_fetch_row && reuse_handler)
  {
    cur= quick_it++;
    quick= cur->quick;
    /*
      There is no use of this->file. Use it for the first of merged range
      selects.
    */
    int error= quick->init_ror_merged_scan(TRUE, local_alloc);
    if (error)
      DBUG_RETURN(error);
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
  }
  while ((cur= quick_it++))
  {
    quick= cur->quick;
#ifndef DBUG_OFF
    const MY_BITMAP * const save_read_set= quick->head->read_set;
    const MY_BITMAP * const save_write_set= quick->head->write_set;
#endif
    if (quick->init_ror_merged_scan(FALSE, local_alloc))
      DBUG_RETURN(1);
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);

    // Sets are shared by all members of "quick_selects" so must not change
#ifndef DBUG_OFF
    DBUG_ASSERT(quick->head->read_set == save_read_set);
    DBUG_ASSERT(quick->head->write_set == save_write_set);
#endif
    /* All merged scans share the same record buffer in intersection. */
    quick->record= head->record[0];
  }

  if (need_to_fetch_row && head->file->ha_rnd_init_with_error(false))
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


/*
  Initialize quick select for row retrieval.
  SYNOPSIS
    reset()
  RETURN
    0      OK
    other  Error code
*/

int QUICK_ROR_INTERSECT_SELECT::reset()
{
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::reset");
  if (!scans_inited && init_ror_merged_scan(TRUE, &alloc))
    DBUG_RETURN(1);
  scans_inited= TRUE;
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> it(quick_selects);
  QUICK_SELECT_WITH_RECORD *qr;
  while ((qr= it++))
    qr->quick->reset();
  DBUG_RETURN(0);
}


/*
  Add a merged quick select to this ROR-intersection quick select.

  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::push_quick_back()
      alloc Mem root to create auxiliary structures on
      quick Quick select to be added. The quick select must return
            rows in rowid order.
  NOTES
    This call can only be made before init() is called.

  RETURN
    FALSE OK
    TRUE  Out of memory.
*/

bool
QUICK_ROR_INTERSECT_SELECT::push_quick_back(MEM_ROOT *local_alloc,
                                            QUICK_RANGE_SELECT *quick)
{
  QUICK_SELECT_WITH_RECORD *qr;
  if (!(qr= new QUICK_SELECT_WITH_RECORD) || 
      !(qr->key_tuple= (uchar*)alloc_root(local_alloc,
                                          quick->max_used_key_length)))
    return TRUE;
  qr->quick= quick;
  return quick_selects.push_back(qr);
}


QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT()
{
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT");
  quick_selects.delete_elements();
  delete cpk_quick;
  free_root(&alloc,MYF(0));
  if (need_to_fetch_row && head->file->inited != handler::NONE)
    head->file->ha_rnd_end();
  DBUG_VOID_RETURN;
}


QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
                                               TABLE *table)
  : thd(thd_param), scans_inited(FALSE)
{
  index= MAX_KEY;
  head= table;
  rowid_length= table->file->ref_length;
  record= head->record[0];
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                 MYF(MY_THREAD_SPECIFIC));
  thd_param->mem_root= &alloc;
}


/*
  Comparison function to be used QUICK_ROR_UNION_SELECT::queue priority
  queue.

  SYNPOSIS
    QUICK_ROR_UNION_SELECT_queue_cmp()
      arg   Pointer to QUICK_ROR_UNION_SELECT
      val1  First merged select
      val2  Second merged select
*/

C_MODE_START

static int QUICK_ROR_UNION_SELECT_queue_cmp(void *arg, uchar *val1, uchar *val2)
{
  QUICK_ROR_UNION_SELECT *self= (QUICK_ROR_UNION_SELECT*)arg;
  return self->head->file->cmp_ref(((QUICK_SELECT_I*)val1)->last_rowid,
                                   ((QUICK_SELECT_I*)val2)->last_rowid);
}

C_MODE_END


/*
  Do post-constructor initialization.
  SYNOPSIS
    QUICK_ROR_UNION_SELECT::init()

  RETURN
    0      OK
    other  Error code
*/

int QUICK_ROR_UNION_SELECT::init()
{
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::init");
  if (init_queue(&queue, quick_selects.elements, 0,
                 FALSE , QUICK_ROR_UNION_SELECT_queue_cmp,
                 (void*) this, 0, 0))
  {
    bzero(&queue, sizeof(QUEUE));
    DBUG_RETURN(1);
  }

  if (!(cur_rowid= (uchar*) alloc_root(&alloc, 2*head->file->ref_length)))
    DBUG_RETURN(1);
  prev_rowid= cur_rowid + head->file->ref_length;
  DBUG_RETURN(0);
}


/*
  Initialize quick select for row retrieval.
  SYNOPSIS
    reset()

  RETURN
    0      OK
    other  Error code
*/

int QUICK_ROR_UNION_SELECT::reset()
{
  QUICK_SELECT_I *quick;
  int error;
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::reset");
  have_prev_rowid= FALSE;
  if (!scans_inited)
  {
    List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
    while ((quick= it++))
    {
      if (quick->init_ror_merged_scan(FALSE, &alloc))
        DBUG_RETURN(1);
    }
    scans_inited= TRUE;
  }
  queue_remove_all(&queue);
  /*
    Initialize scans for merged quick selects and put all merged quick
    selects into the queue.
  */
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
    if ((error= quick->reset()))
      DBUG_RETURN(error);
    if ((error= quick->get_next()))
    {
      if (error == HA_ERR_END_OF_FILE)
        continue;
      DBUG_RETURN(error);
    }
    quick->save_last_pos();
    queue_insert(&queue, (uchar*)quick);
  }
  /* Prepare for ha_rnd_pos calls. */
  if (head->file->inited && (error= head->file->ha_rnd_end()))
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_end call failed"));
    DBUG_RETURN(error);
  }
  if ((error= head->file->ha_rnd_init(false)))
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
    DBUG_RETURN(error);
  }

  DBUG_RETURN(0);
}


bool
QUICK_ROR_UNION_SELECT::push_quick_back(QUICK_SELECT_I *quick_sel_range)
{
  return quick_selects.push_back(quick_sel_range);
}

QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT()
{
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT");
  delete_queue(&queue);
  quick_selects.delete_elements();
  if (head->file->inited != handler::NONE)
    head->file->ha_rnd_end();
  free_root(&alloc,MYF(0));
  DBUG_VOID_RETURN;
}


QUICK_RANGE::QUICK_RANGE()
  :min_key(0),max_key(0),min_length(0),max_length(0),
   flag(NO_MIN_RANGE | NO_MAX_RANGE),
  min_keypart_map(0), max_keypart_map(0)
{}

SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
{
  type=arg.type;
  min_flag=arg.min_flag;
  max_flag=arg.max_flag;
  maybe_flag=arg.maybe_flag;
  maybe_null=arg.maybe_null;
  part=arg.part;
  field=arg.field;
  min_value=arg.min_value;
  max_value=arg.max_value;
  next_key_part=arg.next_key_part;
  max_part_no= arg.max_part_no;
  use_count=1; elements=1;
}


inline void SEL_ARG::make_root()
{
  left=right= &null_element;
  color=BLACK;
  next=prev=0;
  use_count=0; elements=1;
}

SEL_ARG::SEL_ARG(Field *f,const uchar *min_value_arg,
                 const uchar *max_value_arg)
  :min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
   elements(1), use_count(1), field(f), min_value((uchar*) min_value_arg),
   max_value((uchar*) max_value_arg), next(0),prev(0),
   next_key_part(0), color(BLACK), type(KEY_RANGE)
{
  left=right= &null_element;
  max_part_no= 1;
}

SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
                 uchar *min_value_, uchar *max_value_,
		 uint8 min_flag_,uint8 max_flag_,uint8 maybe_flag_)
  :min_flag(min_flag_),max_flag(max_flag_),maybe_flag(maybe_flag_),
   part(part_),maybe_null(field_->real_maybe_null()), elements(1),use_count(1),
   field(field_), min_value(min_value_), max_value(max_value_),
   next(0),prev(0),next_key_part(0),color(BLACK),type(KEY_RANGE)
{
  max_part_no= part+1;
  left=right= &null_element;
}

SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, 
                        SEL_ARG **next_arg)
{
  SEL_ARG *tmp;

  /* Bail out if we have already generated too many SEL_ARGs */
  if (++param->alloced_sel_args > MAX_SEL_ARGS)
    return 0;

  if (type != KEY_RANGE)
  {
    if (!(tmp= new (param->mem_root) SEL_ARG(type)))
      return 0;					// out of memory
    tmp->prev= *next_arg;			// Link into next/prev chain
    (*next_arg)->next=tmp;
    (*next_arg)= tmp;
    tmp->part= this->part;
  }
  else
  {
    if (!(tmp= new (param->mem_root) SEL_ARG(field,part, min_value,max_value,
                                             min_flag, max_flag, maybe_flag)))
      return 0;					// OOM
    tmp->parent=new_parent;
    tmp->next_key_part=next_key_part;
    if (left != &null_element)
      if (!(tmp->left=left->clone(param, tmp, next_arg)))
	return 0;				// OOM

    tmp->prev= *next_arg;			// Link into next/prev chain
    (*next_arg)->next=tmp;
    (*next_arg)= tmp;

    if (right != &null_element)
      if (!(tmp->right= right->clone(param, tmp, next_arg)))
	return 0;				// OOM
  }
  increment_use_count(1);
  tmp->color= color;
  tmp->elements= this->elements;
  tmp->max_part_no= max_part_no;
  return tmp;
}

/**
  This gives the first SEL_ARG in the interval list, and the minimal element
  in the red-black tree

  @return
  SEL_ARG   first SEL_ARG in the interval list
*/
SEL_ARG *SEL_ARG::first()
{
  SEL_ARG *next_arg=this;
  if (!next_arg->left)
    return 0;					// MAYBE_KEY
  while (next_arg->left != &null_element)
    next_arg=next_arg->left;
  return next_arg;
}

const SEL_ARG *SEL_ARG::first() const
{
  return const_cast<SEL_ARG*>(this)->first();
}

SEL_ARG *SEL_ARG::last()
{
  SEL_ARG *next_arg=this;
  if (!next_arg->right)
    return 0;					// MAYBE_KEY
  while (next_arg->right != &null_element)
    next_arg=next_arg->right;
  return next_arg;
}


/*
  Check if a compare is ok, when one takes ranges in account
  Returns -2 or 2 if the ranges where 'joined' like  < 2 and >= 2
*/

int SEL_ARG::sel_cmp(Field *field, uchar *a, uchar *b, uint8 a_flag,
                     uint8 b_flag)
{
  int cmp;
  /* First check if there was a compare to a min or max element */
  if (a_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
  {
    if ((a_flag & (NO_MIN_RANGE | NO_MAX_RANGE)) ==
	(b_flag & (NO_MIN_RANGE | NO_MAX_RANGE)))
      return 0;
    return (a_flag & NO_MIN_RANGE) ? -1 : 1;
  }
  if (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
    return (b_flag & NO_MIN_RANGE) ? 1 : -1;

  if (field->real_maybe_null())			// If null is part of key
  {
    if (*a != *b)
    {
      return *a ? -1 : 1;
    }
    if (*a)
      goto end;					// NULL where equal
    a++; b++;					// Skip NULL marker
  }
  cmp=field->key_cmp(a , b);
  if (cmp) return cmp < 0 ? -1 : 1;		// The values differed

  // Check if the compared equal arguments was defined with open/closed range
 end:
  if (a_flag & (NEAR_MIN | NEAR_MAX))
  {
    if ((a_flag & (NEAR_MIN | NEAR_MAX)) == (b_flag & (NEAR_MIN | NEAR_MAX)))
      return 0;
    if (!(b_flag & (NEAR_MIN | NEAR_MAX)))
      return (a_flag & NEAR_MIN) ? 2 : -2;
    return (a_flag & NEAR_MIN) ? 1 : -1;
  }
  if (b_flag & (NEAR_MIN | NEAR_MAX))
    return (b_flag & NEAR_MIN) ? -2 : 2;
  return 0;					// The elements where equal
}


SEL_ARG *SEL_ARG::clone_tree(RANGE_OPT_PARAM *param)
{
  SEL_ARG tmp_link,*next_arg,*root;
  next_arg= &tmp_link;
  if (!(root= clone(param, (SEL_ARG *) 0, &next_arg)))
    return 0;
  next_arg->next=0;				// Fix last link
  tmp_link.next->prev=0;			// Fix first link
  if (root)					// If not OOM
    root->use_count= 0;
  return root;
}


/*
  Table rows retrieval plan. Range optimizer creates QUICK_SELECT_I-derived
  objects from table read plans.
*/
class TABLE_READ_PLAN
{
public:
  /*
    Plan read cost, with or without cost of full row retrieval, depending
    on plan creation parameters.
  */
  double read_cost;
  ha_rows records; /* estimate of #rows to be examined */

  /*
    If TRUE, the scan returns rows in rowid order. This is used only for
    scans that can be both ROR and non-ROR.
  */
  bool is_ror;

  /*
    Create quick select for this plan.
    SYNOPSIS
     make_quick()
       param               Parameter from test_quick_select
       retrieve_full_rows  If TRUE, created quick select will do full record
                           retrieval.
       parent_alloc        Memory pool to use, if any.

    NOTES
      retrieve_full_rows is ignored by some implementations.

    RETURN
      created quick select
      NULL on any error.
  */
  virtual QUICK_SELECT_I *make_quick(PARAM *param,
                                     bool retrieve_full_rows,
                                     MEM_ROOT *parent_alloc=NULL) = 0;

  /* Table read plans are allocated on MEM_ROOT and are never deleted */
  static void *operator new(size_t size, MEM_ROOT *mem_root)
  { return (void*) alloc_root(mem_root, (uint) size); }
  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
  static void operator delete(void *ptr, MEM_ROOT *mem_root) { /* Never called */ }
  virtual ~TABLE_READ_PLAN() {}               /* Remove gcc warning */

};

class TRP_ROR_INTERSECT;
class TRP_ROR_UNION;
class TRP_INDEX_MERGE;


/*
  Plan for a QUICK_RANGE_SELECT scan.
  TRP_RANGE::make_quick ignores retrieve_full_rows parameter because
  QUICK_RANGE_SELECT doesn't distinguish between 'index only' scans and full
  record retrieval scans.
*/

class TRP_RANGE : public TABLE_READ_PLAN
{
public:
  SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
  uint     key_idx; /* key number in PARAM::key */
  uint     mrr_flags; 
  uint     mrr_buf_size;

  TRP_RANGE(SEL_ARG *key_arg, uint idx_arg, uint mrr_flags_arg)
   : key(key_arg), key_idx(idx_arg), mrr_flags(mrr_flags_arg)
  {}
  virtual ~TRP_RANGE() {}                     /* Remove gcc warning */

  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc)
  {
    DBUG_ENTER("TRP_RANGE::make_quick");
    QUICK_RANGE_SELECT *quick;
    if ((quick= get_quick_select(param, key_idx, key,  mrr_flags, 
                                 mrr_buf_size, parent_alloc)))
    {
      quick->records= records;
      quick->read_time= read_cost;
    }
    DBUG_RETURN(quick);
  }
};


/* Plan for QUICK_ROR_INTERSECT_SELECT scan. */

class TRP_ROR_INTERSECT : public TABLE_READ_PLAN
{
public:
  TRP_ROR_INTERSECT() {}                      /* Remove gcc warning */
  virtual ~TRP_ROR_INTERSECT() {}             /* Remove gcc warning */
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);

  /* Array of pointers to ROR range scans used in this intersection */
  struct st_ror_scan_info **first_scan;
  struct st_ror_scan_info **last_scan; /* End of the above array */
  struct st_ror_scan_info *cpk_scan;  /* Clustered PK scan, if there is one */
  bool is_covering; /* TRUE if no row retrieval phase is necessary */
  double index_scan_costs; /* SUM(cost(index_scan)) */
};


/*
  Plan for QUICK_ROR_UNION_SELECT scan.
  QUICK_ROR_UNION_SELECT always retrieves full rows, so retrieve_full_rows
  is ignored by make_quick.
*/

class TRP_ROR_UNION : public TABLE_READ_PLAN
{
public:
  TRP_ROR_UNION() {}                          /* Remove gcc warning */
  virtual ~TRP_ROR_UNION() {}                 /* Remove gcc warning */
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);
  TABLE_READ_PLAN **first_ror; /* array of ptrs to plans for merged scans */
  TABLE_READ_PLAN **last_ror;  /* end of the above array */
};


/*
  Plan for QUICK_INDEX_INTERSECT_SELECT scan.
  QUICK_INDEX_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
  is ignored by make_quick.
*/

class TRP_INDEX_INTERSECT : public TABLE_READ_PLAN
{
public:
  TRP_INDEX_INTERSECT() {}                        /* Remove gcc warning */
  virtual ~TRP_INDEX_INTERSECT() {}               /* Remove gcc warning */
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);
  TRP_RANGE **range_scans; /* array of ptrs to plans of intersected scans */
  TRP_RANGE **range_scans_end; /* end of the array */
  /* keys whose scans are to be filtered by cpk conditions */
  key_map filtered_scans;  
};


/*
  Plan for QUICK_INDEX_MERGE_SELECT scan.
  QUICK_ROR_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
  is ignored by make_quick.
*/

class TRP_INDEX_MERGE : public TABLE_READ_PLAN
{
public:
  TRP_INDEX_MERGE() {}                        /* Remove gcc warning */
  virtual ~TRP_INDEX_MERGE() {}               /* Remove gcc warning */
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);
  TRP_RANGE **range_scans; /* array of ptrs to plans of merged scans */
  TRP_RANGE **range_scans_end; /* end of the array */
};


/*
  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan. 
*/

class TRP_GROUP_MIN_MAX : public TABLE_READ_PLAN
{
private:
  bool have_min, have_max, have_agg_distinct;
  KEY_PART_INFO *min_max_arg_part;
  uint group_prefix_len;
  uint used_key_parts;
  uint group_key_parts;
  KEY *index_info;
  uint index;
  uint key_infix_len;
  uchar key_infix[MAX_KEY_LENGTH];
  SEL_TREE *range_tree; /* Represents all range predicates in the query. */
  SEL_ARG  *index_tree; /* The SEL_ARG sub-tree corresponding to index_info. */
  uint param_idx; /* Index of used key in param->key. */
  bool is_index_scan; /* Use index_next() instead of random read */ 
public:
  /* Number of records selected by the ranges in index_tree. */
  ha_rows quick_prefix_records;
public:
  TRP_GROUP_MIN_MAX(bool have_min_arg, bool have_max_arg, 
                    bool have_agg_distinct_arg,
                    KEY_PART_INFO *min_max_arg_part_arg,
                    uint group_prefix_len_arg, uint used_key_parts_arg,
                    uint group_key_parts_arg, KEY *index_info_arg,
                    uint index_arg, uint key_infix_len_arg,
                    uchar *key_infix_arg,
                    SEL_TREE *tree_arg, SEL_ARG *index_tree_arg,
                    uint param_idx_arg, ha_rows quick_prefix_records_arg)
  : have_min(have_min_arg), have_max(have_max_arg),
    have_agg_distinct(have_agg_distinct_arg),
    min_max_arg_part(min_max_arg_part_arg),
    group_prefix_len(group_prefix_len_arg), used_key_parts(used_key_parts_arg),
    group_key_parts(group_key_parts_arg), index_info(index_info_arg),
    index(index_arg), key_infix_len(key_infix_len_arg), range_tree(tree_arg),
    index_tree(index_tree_arg), param_idx(param_idx_arg), is_index_scan(FALSE),
    quick_prefix_records(quick_prefix_records_arg)
    {
      if (key_infix_len)
        memcpy(this->key_infix, key_infix_arg, key_infix_len);
    }
  virtual ~TRP_GROUP_MIN_MAX() {}             /* Remove gcc warning */

  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);
  void use_index_scan() { is_index_scan= TRUE; }
};


typedef struct st_index_scan_info
{
  uint      idx;      /* # of used key in param->keys */
  uint      keynr;    /* # of used key in table */
  uint      range_count;
  ha_rows   records;  /* estimate of # records this scan will return */

  /* Set of intervals over key fields that will be used for row retrieval. */
  SEL_ARG   *sel_arg;

  KEY *key_info;
  uint used_key_parts;

  /* Estimate of # records filtered out by intersection with cpk */
  ha_rows   filtered_out;
  /* Bitmap of fields used in index intersection */ 
  MY_BITMAP used_fields;

  /* Fields used in the query and covered by ROR scan. */
  MY_BITMAP covered_fields;
  uint      used_fields_covered; /* # of set bits in covered_fields */
  int       key_rec_length; /* length of key record (including rowid) */

  /*
    Cost of reading all index records with values in sel_arg intervals set
    (assuming there is no need to access full table records)
  */
  double    index_read_cost;
  uint      first_uncovered_field; /* first unused bit in covered_fields */
  uint      key_components; /* # of parts in the key */
} INDEX_SCAN_INFO;

/*
  Fill param->needed_fields with bitmap of fields used in the query.
  SYNOPSIS
    fill_used_fields_bitmap()
      param Parameter from test_quick_select function.

  NOTES
    Clustered PK members are not put into the bitmap as they are implicitly
    present in all keys (and it is impossible to avoid reading them).
  RETURN
    0  Ok
    1  Out of memory.
*/

static int fill_used_fields_bitmap(PARAM *param)
{
  TABLE *table= param->table;
  my_bitmap_map *tmp;
  uint pk;
  param->tmp_covered_fields.bitmap= 0;
  param->fields_bitmap_size= table->s->column_bitmap_size;
  if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
                                  param->fields_bitmap_size)) ||
      my_bitmap_init(&param->needed_fields, tmp, table->s->fields, FALSE))
    return 1;

  bitmap_copy(&param->needed_fields, table->read_set);
  bitmap_union(&param->needed_fields, table->write_set);

  pk= param->table->s->primary_key;
  if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
  {
    /* The table uses clustered PK and it is not internally generated */
    KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
    KEY_PART_INFO *key_part_end= key_part +
                                 param->table->key_info[pk].user_defined_key_parts;
    for (;key_part != key_part_end; ++key_part)
      bitmap_clear_bit(&param->needed_fields, key_part->fieldnr-1);
  }
  return 0;
}


/*
  Test if a key can be used in different ranges

  SYNOPSIS
    SQL_SELECT::test_quick_select()
      thd               Current thread
      keys_to_use       Keys to use for range retrieval
      prev_tables       Tables assumed to be already read when the scan is
                        performed (but not read at the moment of this call)
      limit             Query limit
      force_quick_range Prefer to use range (instead of full table scan) even
                        if it is more expensive.

  NOTES
    Updates the following in the select parameter:
      needed_reg - Bits for keys with may be used if all prev regs are read
      quick      - Parameter to use when reading records.

    In the table struct the following information is updated:
      quick_keys           - Which keys can be used
      quick_rows           - How many rows the key matches
      quick_condition_rows - E(# rows that will satisfy the table condition)

  IMPLEMENTATION
    quick_condition_rows value is obtained as follows:
      
      It is a minimum of E(#output rows) for all considered table access
      methods (range and index_merge accesses over various indexes).
    
    The obtained value is not a true E(#rows that satisfy table condition)
    but rather a pessimistic estimate. To obtain a true E(#...) one would
    need to combine estimates of various access methods, taking into account
    correlations between sets of rows they will return.
    
    For example, if values of tbl.key1 and tbl.key2 are independent (a right
    assumption if we have no information about their correlation) then the
    correct estimate will be:
    
      E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) = 
      = E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)

    which is smaller than 
      
       MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))

    which is currently produced.

  TODO
   * Change the value returned in quick_condition_rows from a pessimistic
     estimate to true E(#rows that satisfy table condition). 
     (we can re-use some of E(#rows) calcuation code from index_merge/intersection 
      for this)
   
   * Check if this function really needs to modify keys_to_use, and change the
     code to pass it by reference if it doesn't.

   * In addition to force_quick_range other means can be (an usually are) used
     to make this function prefer range over full table scan. Figure out if
     force_quick_range is really needed.

  RETURN
   -1 if impossible select (i.e. certainly no rows will be selected)
    0 if can't use quick_select
    1 if found usable ranges and quick select has been successfully created.
*/

int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
				  table_map prev_tables,
				  ha_rows limit, bool force_quick_range, 
                                  bool ordered_output,
                                  bool remove_false_parts_of_where)
{
  uint idx;
  double scan_time;
  DBUG_ENTER("SQL_SELECT::test_quick_select");
  DBUG_PRINT("enter",("keys_to_use: %lu  prev_tables: %lu  const_tables: %lu",
		      (ulong) keys_to_use.to_ulonglong(), (ulong) prev_tables,
		      (ulong) const_tables));
  DBUG_PRINT("info", ("records: %lu", (ulong) head->stat_records()));
  delete quick;
  quick=0;
  needed_reg.clear_all();
  quick_keys.clear_all();
  DBUG_ASSERT(!head->is_filled_at_execution());
  if (keys_to_use.is_clear_all() || head->is_filled_at_execution())
    DBUG_RETURN(0);
  records= head->stat_records();
  if (!records)
    records++;					/* purecov: inspected */
  scan_time= (double) records / TIME_FOR_COMPARE + 1;
  read_time= (double) head->file->scan_time() + scan_time + 1.1;
  if (head->force_index)
    scan_time= read_time= DBL_MAX;
  if (limit < records)
    read_time= (double) records + scan_time + 1; // Force to use index
  
  possible_keys.clear_all();

  DBUG_PRINT("info",("Time to scan table: %g", read_time));

  keys_to_use.intersect(head->keys_in_use_for_query);
  if (!keys_to_use.is_clear_all())
  {
    uchar buff[STACK_BUFF_ALLOC];
    MEM_ROOT alloc;
    SEL_TREE *tree= NULL;
    KEY_PART *key_parts;
    KEY *key_info;
    PARAM param;

    if (check_stack_overrun(thd, 2*STACK_MIN_SIZE + sizeof(PARAM), buff))
      DBUG_RETURN(0);                           // Fatal error flag is set

    /* set up parameter that is passed to all functions */
    param.thd= thd;
    param.baseflag= head->file->ha_table_flags();
    param.prev_tables=prev_tables | const_tables;
    param.read_tables=read_tables;
    param.current_table= head->map;
    param.table=head;
    param.keys=0;
    param.mem_root= &alloc;
    param.old_root= thd->mem_root;
    param.needed_reg= &needed_reg;
    param.imerge_cost_buff_size= 0;
    param.using_real_indexes= TRUE;
    param.remove_jump_scans= TRUE;
    param.remove_false_where_parts= remove_false_parts_of_where;
    param.force_default_mrr= ordered_output;
    param.possible_keys.clear_all();

    thd->no_errors=1;				// Don't warn about NULL
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                   MYF(MY_THREAD_SPECIFIC));
    if (!(param.key_parts=
           (KEY_PART*) alloc_root(&alloc,
                                  sizeof(KEY_PART) *
	                          head->s->actual_n_key_parts(thd))) ||
        fill_used_fields_bitmap(&param))
    {
      thd->no_errors=0;
      free_root(&alloc,MYF(0));			// Return memory & allocator
      DBUG_RETURN(0);				// Can't use range
    }
    key_parts= param.key_parts;

    /*
      Make an array with description of all key parts of all table keys.
      This is used in get_mm_parts function.
    */
    key_info= head->key_info;
    uint max_key_len= 0;
    for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
    {
      KEY_PART_INFO *key_part_info;
      uint n_key_parts= head->actual_n_key_parts(key_info);

      if (!keys_to_use.is_set(idx))
	continue;
      if (key_info->flags & HA_FULLTEXT)
	continue;    // ToDo: ft-keys in non-ft ranges, if possible   SerG

      param.key[param.keys]=key_parts;
      key_part_info= key_info->key_part;
      uint cur_key_len= 0;
      for (uint part= 0 ; part < n_key_parts ; 
           part++, key_parts++, key_part_info++)
     {
	key_parts->key=		 param.keys;
	key_parts->part=	 part;
	key_parts->length=       key_part_info->length;
	key_parts->store_length= key_part_info->store_length;
        cur_key_len += key_part_info->store_length;
	key_parts->field=	 key_part_info->field;
	key_parts->null_bit=	 key_part_info->null_bit;
        key_parts->image_type =
          (key_info->flags & HA_SPATIAL) ? Field::itMBR : Field::itRAW;
        /* Only HA_PART_KEY_SEG is used */
        key_parts->flag=         (uint8) key_part_info->key_part_flag;
      }
      param.real_keynr[param.keys++]=idx;
      if (cur_key_len > max_key_len)
        max_key_len= cur_key_len;
    }
    param.key_parts_end=key_parts;
    param.alloced_sel_args= 0;

    max_key_len++; /* Take into account the "+1" in QUICK_RANGE::QUICK_RANGE */
    if (!(param.min_key= (uchar*)alloc_root(&alloc,max_key_len)) ||
        !(param.max_key= (uchar*)alloc_root(&alloc,max_key_len)))
    {
      thd->no_errors=0;
      free_root(&alloc,MYF(0));			// Return memory & allocator
      DBUG_RETURN(0);				// Can't use range
    }

    thd->mem_root= &alloc;
    /* Calculate cost of full index read for the shortest covering index */
    if (!force_quick_range && !head->covering_keys.is_clear_all())
    {
      int key_for_use= find_shortest_key(head, &head->covering_keys);
      double key_read_time= head->file->keyread_time(key_for_use, 1, records) +
                            (double) records / TIME_FOR_COMPARE;
      DBUG_PRINT("info",  ("'all'+'using index' scan will be using key %d, "
                           "read time %g", key_for_use, key_read_time));
      if (key_read_time < read_time)
        read_time= key_read_time;
    }

    TABLE_READ_PLAN *best_trp= NULL;
    TRP_GROUP_MIN_MAX *group_trp;
    double best_read_time= read_time;

    if (cond)
    {
      if ((tree= cond->get_mm_tree(&param, &cond)))
      {
        if (tree->type == SEL_TREE::IMPOSSIBLE)
        {
          records=0L;                      /* Return -1 from this function. */
          read_time= (double) HA_POS_ERROR;
          goto free_mem;
        }
        /*
          If the tree can't be used for range scans, proceed anyway, as we
          can construct a group-min-max quick select
        */
        if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
          tree= NULL;
      }
    }

    /*
      Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
      Notice that it can be constructed no matter if there is a range tree.
    */
    group_trp= get_best_group_min_max(&param, tree, best_read_time);
    if (group_trp)
    {
      param.table->quick_condition_rows= MY_MIN(group_trp->records,
                                             head->stat_records());
      if (group_trp->read_cost < best_read_time)
      {
        best_trp= group_trp;
        best_read_time= best_trp->read_cost;
      }
    }

    if (tree)
    {
      /*
        It is possible to use a range-based quick select (but it might be
        slower than 'all' table scan).
      */
      TRP_RANGE         *range_trp;
      TRP_ROR_INTERSECT *rori_trp;
      TRP_INDEX_INTERSECT *intersect_trp;
      bool can_build_covering= FALSE;
      
      remove_nonrange_trees(&param, tree);

      /* Get best 'range' plan and prepare data for making other plans */
      if ((range_trp= get_key_scans_params(&param, tree, FALSE, TRUE,
                                           best_read_time)))
      {
        best_trp= range_trp;
        best_read_time= best_trp->read_cost;
      }

      /*
        Simultaneous key scans and row deletes on several handler
        objects are not allowed so don't use ROR-intersection for
        table deletes.
      */
      if ((thd->lex->sql_command != SQLCOM_DELETE) && 
           optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE))
      {
        /*
          Get best non-covering ROR-intersection plan and prepare data for
          building covering ROR-intersection.
        */
        if ((rori_trp= get_best_ror_intersect(&param, tree, best_read_time,
                                              &can_build_covering)))
        {
          best_trp= rori_trp;
          best_read_time= best_trp->read_cost;
          /*
            Try constructing covering ROR-intersect only if it looks possible
            and worth doing.
          */
          if (!rori_trp->is_covering && can_build_covering &&
              (rori_trp= get_best_covering_ror_intersect(&param, tree,
                                                         best_read_time)))
            best_trp= rori_trp;
        }
      }
      /*
        Do not look for an index intersection  plan if there is a covering
        index. The scan by this covering index will be always cheaper than
        any index intersection.
      */
      if (param.table->covering_keys.is_clear_all() &&
          optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE) &&
          optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE_SORT_INTERSECT))
      {
        if ((intersect_trp= get_best_index_intersect(&param, tree,
                                                    best_read_time)))
        {
          best_trp= intersect_trp;
          best_read_time= best_trp->read_cost; 
          set_if_smaller(param.table->quick_condition_rows, 
                         intersect_trp->records);
        }
      }

      if (optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE) &&
          head->stat_records() != 0)
      {
        /* Try creating index_merge/ROR-union scan. */
        SEL_IMERGE *imerge;
        TABLE_READ_PLAN *best_conj_trp= NULL,
          *UNINIT_VAR(new_conj_trp); /* no empty index_merge lists possible */
        DBUG_PRINT("info",("No range reads possible,"
                           " trying to construct index_merge"));
        List_iterator_fast<SEL_IMERGE> it(tree->merges);
        while ((imerge= it++))
        {
          new_conj_trp= get_best_disjunct_quick(&param, imerge, best_read_time);
          if (new_conj_trp)
            set_if_smaller(param.table->quick_condition_rows, 
                           new_conj_trp->records);
          if (new_conj_trp &&
              (!best_conj_trp || 
               new_conj_trp->read_cost < best_conj_trp->read_cost))
          {
            best_conj_trp= new_conj_trp;
            best_read_time= best_conj_trp->read_cost;
          }
        }
        if (best_conj_trp)
          best_trp= best_conj_trp;
      }
    }

    thd->mem_root= param.old_root;

    /* If we got a read plan, create a quick select from it. */
    if (best_trp)
    {
      records= best_trp->records;
      if (!(quick= best_trp->make_quick(&param, TRUE)) || quick->init())
      {
        delete quick;
        quick= NULL;
      }
    }
    possible_keys= param.possible_keys;

  free_mem:
    free_root(&alloc,MYF(0));			// Return memory & allocator
    thd->mem_root= param.old_root;
    thd->no_errors=0;
  }

  DBUG_EXECUTE("info", print_quick(quick, &needed_reg););

  /*
    Assume that if the user is using 'limit' we will only need to scan
    limit rows if we are using a key
  */
  DBUG_RETURN(records ? MY_TEST(quick) : -1);
}

/****************************************************************************
 * Condition selectivity module
 ****************************************************************************/


/*
  Build descriptors of pseudo-indexes over columns to perform range analysis

  SYNOPSIS
    create_key_parts_for_pseudo_indexes()
      param       IN/OUT data structure for the descriptors to be built 
      used_fields bitmap of columns for which the descriptors are to be built          

  DESCRIPTION
    For each column marked in the bitmap used_fields the function builds
    a descriptor of a single-component pseudo-index over this column that
    can be used for the range analysis of the predicates over this columns. 
    The descriptors are created in the memory of param->mem_root. 
   
  RETURN
    FALSE  in the case of success
    TRUE   otherwise
*/

static
bool create_key_parts_for_pseudo_indexes(RANGE_OPT_PARAM *param,
                                         MY_BITMAP *used_fields)
{
  Field **field_ptr;
  TABLE *table= param->table;
  uint parts= 0;

  for (field_ptr= table->field; *field_ptr; field_ptr++)
  {
    if (bitmap_is_set(used_fields, (*field_ptr)->field_index))
      parts++;
  }

  KEY_PART *key_part;
  uint keys= 0;

  if (!(key_part= (KEY_PART *)  alloc_root(param->mem_root,
                                           sizeof(KEY_PART) * parts)))
    return TRUE;

  param->key_parts= key_part;
  uint max_key_len= 0;
  for (field_ptr= table->field; *field_ptr; field_ptr++)
  {
    if (bitmap_is_set(used_fields, (*field_ptr)->field_index))
    {
      Field *field= *field_ptr;
      uint16 store_length;
      uint16 max_key_part_length= (uint16) table->file->max_key_part_length();
      key_part->key= keys;
      key_part->part= 0;
      if (field->flags & BLOB_FLAG)
        key_part->length= max_key_part_length;
      else
      {
        key_part->length= (uint16) field->key_length();
        set_if_smaller(key_part->length, max_key_part_length);
      }
      store_length= key_part->length;
      if (field->real_maybe_null())
        store_length+= HA_KEY_NULL_LENGTH;
      if (field->real_type() == MYSQL_TYPE_VARCHAR)
        store_length+= HA_KEY_BLOB_LENGTH;
      if (max_key_len < store_length)
        max_key_len= store_length;
      key_part->store_length= store_length; 
      key_part->field= field; 
      key_part->image_type= Field::itRAW;
      key_part->flag= 0;
      param->key[keys]= key_part;
      keys++;
      key_part++;
    }
  }

  max_key_len++; /* Take into account the "+1" in QUICK_RANGE::QUICK_RANGE */
  if (!(param->min_key= (uchar*)alloc_root(param->mem_root, max_key_len)) ||
      !(param->max_key= (uchar*)alloc_root(param->mem_root, max_key_len)))
  {
    return true;
  }
  param->keys= keys;
  param->key_parts_end= key_part;

  return FALSE;
}


/*
  Estimate the number of rows in all ranges built for a column
  by the range optimizer  

  SYNOPSIS
    records_in_column_ranges()
      param      the data structure to access descriptors of pseudo indexes
                 built over columns used in the condition of the processed query
      idx        the index of the descriptor of interest in param
      tree       the tree representing ranges built for the interesting column         

  DESCRIPTION
    This function retrieves the ranges represented by the SEL_ARG 'tree' and
    for each of them r it calls the function get_column_range_cardinality()
    that estimates the number of expected rows in r. It is assumed that param
    is the data structure containing the descriptors of pseudo-indexes that
    has been built to perform range analysis of the range conditions imposed
    on the columns used in the processed query, while idx is the index of the
    descriptor created in 'param' exactly for the column for which 'tree'
    has been built by the range optimizer.    

  RETURN
    the number of rows in the retrieved ranges  
*/

static
double records_in_column_ranges(PARAM *param, uint idx, 
                                SEL_ARG *tree)
{
  SEL_ARG_RANGE_SEQ seq;
  KEY_MULTI_RANGE range;
  range_seq_t seq_it;
  double rows;
  Field *field;
  uint flags= 0;
  double total_rows= 0;
  RANGE_SEQ_IF seq_if = {NULL, sel_arg_range_seq_init, 
                         sel_arg_range_seq_next, 0, 0};
  
  /* Handle cases when we don't have a valid non-empty list of range */
  if (!tree)
    return HA_POS_ERROR;
  if (tree->type == SEL_ARG::IMPOSSIBLE)
    return (0L);

  field= tree->field;

  seq.keyno= idx;
  seq.real_keyno= MAX_KEY;
  seq.param= param;
  seq.start= tree;

  seq_it= seq_if.init((void *) &seq, 0, flags);

  while (!seq_if.next(seq_it, &range))
  {
    key_range *min_endp, *max_endp;
    min_endp= range.start_key.length? &range.start_key : NULL;
    max_endp= range.end_key.length? &range.end_key : NULL;
    rows= get_column_range_cardinality(field, min_endp, max_endp,
                                       range.range_flag);
    if (HA_POS_ERROR == rows)
    {
      total_rows= HA_POS_ERROR;
      break;
    }
    total_rows += rows;
  }    
  return total_rows;
} 


/*
  Calculate the selectivity of the condition imposed on the rows of a table

  SYNOPSIS
    calculate_cond_selectivity_for_table()
      thd        the context handle 
      table      the table of interest
      cond       conditions imposed on the rows of the table        

  DESCRIPTION
    This function calculates the selectivity of range conditions cond imposed
    on the rows of 'table' in the processed query.
    The calculated selectivity is assigned to the field table->cond_selectivity.
    
    Selectivity is calculated as a product of selectivities imposed by:

    1. possible range accesses. (if multiple range accesses use the same
       restrictions on the same field, we make adjustments for that)
    2. Sargable conditions on fields for which we have column statistics (if 
       a field is used in a possible range access, we assume that selectivity
       is already provided by the range access' estimates)
    3. Reading a few records from the table pages and checking the condition
       selectivity (this is used for conditions like "column LIKE '%val%'" 
       where approaches #1 and #2 do not provide selectivity data).

  NOTE
    Currently the selectivities of range conditions over different columns are
    considered independent. 

  RETURN
    FALSE  on success
    TRUE   otherwise 
*/

bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
{
  uint keynr;
  uint max_quick_key_parts= 0;
  MY_BITMAP *used_fields= &table->cond_set;
  double table_records= table->stat_records(); 
  DBUG_ENTER("calculate_cond_selectivity_for_table");

  table->cond_selectivity= 1.0;

  if (!*cond || table_records == 0)
    DBUG_RETURN(FALSE);

  if (table->pos_in_table_list->schema_table)
    DBUG_RETURN(FALSE);
  
  MY_BITMAP handled_columns;
  my_bitmap_map* buf;
  if (!(buf= (my_bitmap_map*)thd->alloc(table->s->column_bitmap_size)))
    DBUG_RETURN(TRUE);
  my_bitmap_init(&handled_columns, buf, table->s->fields, FALSE);

  /*
    Calculate the selectivity of the range conditions supported by indexes.

    First, take into account possible range accesses. 
    range access estimates are the most precise, we prefer them to any other
    estimate sources.
  */

  for (keynr= 0;  keynr < table->s->keys; keynr++)
  {
    if (table->quick_keys.is_set(keynr))
      set_if_bigger(max_quick_key_parts, table->quick_key_parts[keynr]);
  }

  /* 
    Walk through all indexes, indexes where range access uses more keyparts 
    go first.
  */
  for (uint quick_key_parts= max_quick_key_parts;
       quick_key_parts; quick_key_parts--)
  {
    for (keynr= 0;  keynr < table->s->keys; keynr++)
    {
      if (table->quick_keys.is_set(keynr) &&
          table->quick_key_parts[keynr] == quick_key_parts)
      {
        uint i;
        uint used_key_parts= table->quick_key_parts[keynr];
        double quick_cond_selectivity= table->quick_rows[keynr] / 
	                               table_records;
        KEY *key_info= table->key_info + keynr;
        KEY_PART_INFO* key_part= key_info->key_part;
        /*
          Suppose, there are range conditions on two keys
            KEY1 (col1, col2)
            KEY2 (col3, col2)
          
          we don't want to count selectivity of condition on col2 twice.
          
          First, find the longest key prefix that's made of columns whose
          selectivity wasn't already accounted for.
        */
        for (i= 0; i < used_key_parts; i++, key_part++)
        {
          if (bitmap_is_set(&handled_columns, key_part->fieldnr-1))
	    break; 
          bitmap_set_bit(&handled_columns, key_part->fieldnr-1);
        }
        if (i)
        {
          double UNINIT_VAR(selectivity_mult);

          /* 
            There is at least 1-column prefix of columns whose selectivity has
            not yet been accounted for.
          */
          table->cond_selectivity*= quick_cond_selectivity;
          if (i != used_key_parts)
	  {
            /*
              Range access got us estimate for #used_key_parts.
              We need estimate for #(i-1) key parts.
            */
            double f1= key_info->actual_rec_per_key(i-1);
            double f2= key_info->actual_rec_per_key(i);
            if (f1 > 0 && f2 > 0)
              selectivity_mult= f1 / f2;
            else
            {
              /* 
                No statistics available, assume the selectivity is proportional
                to the number of key parts.
                (i=0 means 1 keypart, i=1 means 2 keyparts, so use i+1)
              */
              selectivity_mult= ((double)(i+1)) / i;
            }
            table->cond_selectivity*= selectivity_mult;            
          }
          /*
            We need to set selectivity for fields supported by indexes.
            For single-component indexes and for some first components
            of other indexes we do it here. For the remaining fields
            we do it later in this function, in the same way as for the
            fields not used in any indexes.
	  */
	  if (i == 1)
	  {
            uint fieldnr= key_info->key_part[0].fieldnr;
            table->field[fieldnr-1]->cond_selectivity= quick_cond_selectivity;
            if (i != used_key_parts)
	      table->field[fieldnr-1]->cond_selectivity*= selectivity_mult;
            bitmap_clear_bit(used_fields, fieldnr-1);
	  }
        }
      }
    }
  }
   
  /* 
    Second step: calculate the selectivity of the range conditions not 
    supported by any index and selectivity of the range condition
    over the fields whose selectivity has not been set yet.
  */

  if (thd->variables.optimizer_use_condition_selectivity > 2 &&
      !bitmap_is_clear_all(used_fields))
  {
    PARAM param;
    MEM_ROOT alloc;
    SEL_TREE *tree;
    double rows;
  
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                   MYF(MY_THREAD_SPECIFIC));
    param.thd= thd;
    param.mem_root= &alloc;
    param.old_root= thd->mem_root;
    param.table= table;
    param.is_ror_scan= FALSE;
    param.remove_false_where_parts= true;

    if (create_key_parts_for_pseudo_indexes(&param, used_fields))
      goto free_alloc;

    param.prev_tables= param.read_tables= 0;
    param.current_table= table->map;
    param.using_real_indexes= FALSE;
    param.real_keynr[0]= 0;
    param.alloced_sel_args= 0;

    thd->no_errors=1;		    

    tree= cond[0]->get_mm_tree(&param, cond);

    if (!tree)
      goto free_alloc;
    
    table->reginfo.impossible_range= 0;
    if (tree->type == SEL_TREE::IMPOSSIBLE)
    {
      rows= 0;
      table->reginfo.impossible_range= 1;
      goto free_alloc;
    }  
    else if (tree->type == SEL_TREE::ALWAYS)
    {
      rows= table_records;
      goto free_alloc;
    }        
    else if (tree->type == SEL_TREE::MAYBE)
    {
      rows= table_records;
      goto free_alloc;
    }        

    for (uint idx= 0; idx < param.keys; idx++)
    {
      SEL_ARG *key= tree->keys[idx];
      if (key)
      {
        if (key->type == SEL_ARG::IMPOSSIBLE)
	{
          rows= 0;
          table->reginfo.impossible_range= 1;
          goto free_alloc;
        }          
        else
        {
          rows= records_in_column_ranges(&param, idx, key);
          if (rows != HA_POS_ERROR)
            key->field->cond_selectivity= rows/table_records;
        }
      }
    }

    for (Field **field_ptr= table->field; *field_ptr; field_ptr++)
    {
      Field *table_field= *field_ptr;   
      if (bitmap_is_set(used_fields, table_field->field_index) &&
          table_field->cond_selectivity < 1.0)
      {
        if (!bitmap_is_set(&handled_columns, table_field->field_index))
          table->cond_selectivity*= table_field->cond_selectivity;
      }
    }

  free_alloc:
    thd->no_errors= 0;
    thd->mem_root= param.old_root;
    free_root(&alloc, MYF(0));

  }

  bitmap_union(used_fields, &handled_columns);

  /* Check if we can improve selectivity estimates by using sampling */
  ulong check_rows=
    MY_MIN(thd->variables.optimizer_selectivity_sampling_limit,
        (ulong) (table_records * SELECTIVITY_SAMPLING_SHARE));
  if (*cond && check_rows > SELECTIVITY_SAMPLING_THRESHOLD &&
      thd->variables.optimizer_use_condition_selectivity > 4)
  {
    find_selective_predicates_list_processor_data *dt=
      (find_selective_predicates_list_processor_data *)
      alloc_root(thd->mem_root,
                 sizeof(find_selective_predicates_list_processor_data));
    if (!dt)
      DBUG_RETURN(TRUE);
    dt->list.empty();
    dt->table= table;
    if ((*cond)->walk(&Item::find_selective_predicates_list_processor, 0, dt))
      DBUG_RETURN(TRUE);
    if (dt->list.elements > 0)
    {
      check_rows= check_selectivity(thd, check_rows, table, &dt->list);
      if (check_rows > SELECTIVITY_SAMPLING_THRESHOLD)
      {
        COND_STATISTIC *stat;
        List_iterator_fast<COND_STATISTIC> it(dt->list);
        double examined_rows= check_rows;
        while ((stat= it++))
        {
          if (!stat->positive)
          {
            DBUG_PRINT("info", ("To avoid 0 assigned 1 to the counter"));
            stat->positive= 1; // avoid 0
          }
          DBUG_PRINT("info", ("The predicate selectivity : %g",
                              (double)stat->positive / examined_rows));
          double selectivity= ((double)stat->positive) / examined_rows;
          table->cond_selectivity*= selectivity;
          /*
            If a field is involved then we register its selectivity in case
            there in an equality with the field.
            For example in case
            t1.a LIKE "%bla%" and t1.a = t2.b
            the selectivity we have found could be used also for t2.
          */
          if (stat->field_arg)
          {
            stat->field_arg->cond_selectivity*= selectivity;

            if (stat->field_arg->next_equal_field)
            {
              for (Field *next_field= stat->field_arg->next_equal_field;
                   next_field != stat->field_arg;
                   next_field= next_field->next_equal_field)
              {
                next_field->cond_selectivity*= selectivity;
                next_field->table->cond_selectivity*= selectivity;
              }
            }
          }
        }

      }
      /* This list and its elements put to mem_root so should not be freed */
      table->cond_selectivity_sampling_explain= &dt->list;
    }
  }

  DBUG_RETURN(FALSE);
}

/****************************************************************************
 * Condition selectivity code ends
 ****************************************************************************/

/****************************************************************************
 * Partition pruning module
 ****************************************************************************/

/*
  Store field key image to table record

  SYNOPSIS
    store_key_image_to_rec()
      field  Field which key image should be stored
      ptr    Field value in key format
      len    Length of the value, in bytes
  
  ATTENTION
    len is the length of the value not counting the NULL-byte (at the same
    time, ptr points to the key image, which starts with NULL-byte for 
    nullable columns)

  DESCRIPTION
    Copy the field value from its key image to the table record. The source
    is the value in key image format, occupying len bytes in buffer pointed
    by ptr. The destination is table record, in "field value in table record"
    format.
*/

void store_key_image_to_rec(Field *field, uchar *ptr, uint len)
{
  /* Do the same as print_key() does */ 
  my_bitmap_map *old_map;

  if (field->real_maybe_null())
  {
    if (*ptr)
    {
      field->set_null();
      return;
    }
    field->set_notnull();
    ptr++;
  }    
  old_map= dbug_tmp_use_all_columns(field->table,
                                    field->table->write_set);
  field->set_key_image(ptr, len); 
  dbug_tmp_restore_column_map(field->table->write_set, old_map);
}

#ifdef WITH_PARTITION_STORAGE_ENGINE

/*
  PartitionPruningModule

  This part of the code does partition pruning. Partition pruning solves the
  following problem: given a query over partitioned tables, find partitions
  that we will not need to access (i.e. partitions that we can assume to be
  empty) when executing the query.
  The set of partitions to prune doesn't depend on which query execution
  plan will be used to execute the query.
  
  HOW IT WORKS
  
  Partition pruning module makes use of RangeAnalysisModule. The following
  examples show how the problem of partition pruning can be reduced to the 
  range analysis problem:
  
  EXAMPLE 1
    Consider a query:
    
      SELECT * FROM t1 WHERE (t1.a < 5 OR t1.a = 10) AND t1.a > 3 AND t1.b='z'
    
    where table t1 is partitioned using PARTITION BY RANGE(t1.a).  An apparent
    way to find the used (i.e. not pruned away) partitions is as follows:
    
    1. analyze the WHERE clause and extract the list of intervals over t1.a
       for the above query we will get this list: {(3 < t1.a < 5), (t1.a=10)}

    2. for each interval I
       {
         find partitions that have non-empty intersection with I;
         mark them as used;
       }
       
  EXAMPLE 2
    Suppose the table is partitioned by HASH(part_func(t1.a, t1.b)). Then
    we need to:

    1. Analyze the WHERE clause and get a list of intervals over (t1.a, t1.b).
       The list of intervals we'll obtain will look like this:
       ((t1.a, t1.b) = (1,'foo')),
       ((t1.a, t1.b) = (2,'bar')), 
       ((t1,a, t1.b) > (10,'zz'))
       
    2. for each interval I 
       {
         if (the interval has form "(t1.a, t1.b) = (const1, const2)" )
         {
           calculate HASH(part_func(t1.a, t1.b));
           find which partition has records with this hash value and mark
             it as used;
         }
         else
         {
           mark all partitions as used; 
           break;
         }
       }

   For both examples the step #1 is exactly what RangeAnalysisModule could
   be used to do, if it was provided with appropriate index description
   (array of KEY_PART structures). 
   In example #1, we need to provide it with description of index(t1.a), 
   in example #2, we need to provide it with description of index(t1.a, t1.b).
   
   These index descriptions are further called "partitioning index
   descriptions". Note that it doesn't matter if such indexes really exist,
   as range analysis module only uses the description.
   
   Putting it all together, partitioning module works as follows:
   
   prune_partitions() {
     call create_partition_index_description();

     call get_mm_tree(); // invoke the RangeAnalysisModule
     
     // analyze the obtained interval list and get used partitions 
     call find_used_partitions();
  }

*/

struct st_part_prune_param;
struct st_part_opt_info;

typedef void (*mark_full_part_func)(partition_info*, uint32);

/*
  Partition pruning operation context
*/
typedef struct st_part_prune_param
{
  RANGE_OPT_PARAM range_param; /* Range analyzer parameters */

  /***************************************************************
   Following fields are filled in based solely on partitioning 
   definition and not modified after that:
   **************************************************************/
  partition_info *part_info; /* Copy of table->part_info */
  /* Function to get partition id from partitioning fields only */
  get_part_id_func get_top_partition_id_func;
  /* Function to mark a partition as used (w/all subpartitions if they exist)*/
  mark_full_part_func mark_full_partition_used;
 
  /* Partitioning 'index' description, array of key parts */
  KEY_PART *key;
  
  /*
    Number of fields in partitioning 'index' definition created for
    partitioning (0 if partitioning 'index' doesn't include partitioning
    fields)
  */
  uint part_fields;
  uint subpart_fields; /* Same as above for subpartitioning */
  
  /* 
    Number of the last partitioning field keypart in the index, or -1 if
    partitioning index definition doesn't include partitioning fields.
  */
  int last_part_partno;
  int last_subpart_partno; /* Same as above for supartitioning */

  /*
    is_part_keypart[i] == MY_TEST(keypart #i in partitioning index is a member
                                  used in partitioning)
    Used to maintain current values of cur_part_fields and cur_subpart_fields
  */
  my_bool *is_part_keypart;
  /* Same as above for subpartitioning */
  my_bool *is_subpart_keypart;

  my_bool ignore_part_fields; /* Ignore rest of partioning fields */

  /***************************************************************
   Following fields form find_used_partitions() recursion context:
   **************************************************************/
  SEL_ARG **arg_stack;     /* "Stack" of SEL_ARGs */
  SEL_ARG **arg_stack_end; /* Top of the stack    */
  /* Number of partitioning fields for which we have a SEL_ARG* in arg_stack */
  uint cur_part_fields;
  /* Same as cur_part_fields, but for subpartitioning */
  uint cur_subpart_fields;

  /* Iterator to be used to obtain the "current" set of used partitions */
  PARTITION_ITERATOR part_iter;

  /* Initialized bitmap of num_subparts size */
  MY_BITMAP subparts_bitmap;

  uchar *cur_min_key;
  uchar *cur_max_key;

  uint cur_min_flag, cur_max_flag;
} PART_PRUNE_PARAM;

static bool create_partition_index_description(PART_PRUNE_PARAM *prune_par);
static int find_used_partitions(PART_PRUNE_PARAM *ppar, SEL_ARG *key_tree);
static int find_used_partitions_imerge(PART_PRUNE_PARAM *ppar,
                                       SEL_IMERGE *imerge);
static int find_used_partitions_imerge_list(PART_PRUNE_PARAM *ppar,
                                            List<SEL_IMERGE> &merges);
static void mark_all_partitions_as_used(partition_info *part_info);

#ifndef DBUG_OFF
static void print_partitioning_index(KEY_PART *parts, KEY_PART *parts_end);
static void dbug_print_field(Field *field);
static void dbug_print_segment_range(SEL_ARG *arg, KEY_PART *part);
static void dbug_print_singlepoint_range(SEL_ARG **start, uint num);
#endif


/**
  Perform partition pruning for a given table and condition.

  @param      thd            Thread handle
  @param      table          Table to perform partition pruning for
  @param      pprune_cond    Condition to use for partition pruning
  
  @note This function assumes that lock_partitions are setup when it
  is invoked. The function analyzes the condition, finds partitions that
  need to be used to retrieve the records that match the condition, and 
  marks them as used by setting appropriate bit in part_info->read_partitions
  In the worst case all partitions are marked as used. If the table is not
  yet locked, it will also unset bits in part_info->lock_partitions that is
  not set in read_partitions.

  This function returns promptly if called for non-partitioned table.

  @return Operation status
    @retval true  Failure
    @retval false Success
*/

bool prune_partitions(THD *thd, TABLE *table, Item *pprune_cond)
{
  bool retval= FALSE;
  partition_info *part_info = table->part_info;
  DBUG_ENTER("prune_partitions");

  if (!part_info)
    DBUG_RETURN(FALSE); /* not a partitioned table */
  
  if (!pprune_cond)
  {
    mark_all_partitions_as_used(part_info);
    DBUG_RETURN(FALSE);
  }
  
  PART_PRUNE_PARAM prune_param;
  MEM_ROOT alloc;
  RANGE_OPT_PARAM  *range_par= &prune_param.range_param;
  my_bitmap_map *old_sets[2];

  prune_param.part_info= part_info;
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
                 MYF(MY_THREAD_SPECIFIC));
  range_par->mem_root= &alloc;
  range_par->old_root= thd->mem_root;

  if (create_partition_index_description(&prune_param))
  {
    mark_all_partitions_as_used(part_info);
    free_root(&alloc,MYF(0));		// Return memory & allocator
    DBUG_RETURN(FALSE);
  }
  
  dbug_tmp_use_all_columns(table, old_sets, 
                           table->read_set, table->write_set);
  range_par->thd= thd;
  range_par->table= table;
  /* range_par->cond doesn't need initialization */
  range_par->prev_tables= range_par->read_tables= 0;
  range_par->current_table= table->map;
  /* It should be possible to switch the following ON: */
  range_par->remove_false_where_parts= false;

  range_par->keys= 1; // one index
  range_par->using_real_indexes= FALSE;
  range_par->remove_jump_scans= FALSE;
  range_par->real_keynr[0]= 0;
  range_par->alloced_sel_args= 0;

  thd->no_errors=1;				// Don't warn about NULL
  thd->mem_root=&alloc;

  bitmap_clear_all(&part_info->read_partitions);

  prune_param.key= prune_param.range_param.key_parts;
  SEL_TREE *tree;
  int res;

  tree= pprune_cond->get_mm_tree(range_par, &pprune_cond);
  if (!tree)
    goto all_used;

  if (tree->type == SEL_TREE::IMPOSSIBLE)
  {
    retval= TRUE;
    goto end;
  }

  if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
    goto all_used;

  if (tree->merges.is_empty())
  {
    /* Range analysis has produced a single list of intervals. */
    prune_param.arg_stack_end= prune_param.arg_stack;
    prune_param.cur_part_fields= 0;
    prune_param.cur_subpart_fields= 0;
    
    prune_param.cur_min_key= prune_param.range_param.min_key;
    prune_param.cur_max_key= prune_param.range_param.max_key;
    prune_param.cur_min_flag= prune_param.cur_max_flag= 0;

    init_all_partitions_iterator(part_info, &prune_param.part_iter);
    if (!tree->keys[0] || (-1 == (res= find_used_partitions(&prune_param,
                                                            tree->keys[0]))))
      goto all_used;
  }
  else
  {
    if (tree->merges.elements == 1)
    {
      /* 
        Range analysis has produced a "merge" of several intervals lists, a 
        SEL_TREE that represents an expression in form         
          sel_imerge = (tree1 OR tree2 OR ... OR treeN)
        that cannot be reduced to one tree. This can only happen when 
        partitioning index has several keyparts and the condition is OR of
        conditions that refer to different key parts. For example, we'll get
        here for "partitioning_field=const1 OR subpartitioning_field=const2"
      */
      if (-1 == (res= find_used_partitions_imerge(&prune_param,
                                                  tree->merges.head())))
        goto all_used;
    }
    else
    {
      /* 
        Range analysis has produced a list of several imerges, i.e. a
        structure that represents a condition in form 
        imerge_list= (sel_imerge1 AND sel_imerge2 AND ... AND sel_imergeN)
        This is produced for complicated WHERE clauses that range analyzer
        can't really analyze properly.
      */
      if (-1 == (res= find_used_partitions_imerge_list(&prune_param,
                                                       tree->merges)))
        goto all_used;
    }
  }
  
  /*
    res == 0 => no used partitions => retval=TRUE
    res == 1 => some used partitions => retval=FALSE
    res == -1 - we jump over this line to all_used:
  */
  retval= MY_TEST(!res);
  goto end;

all_used:
  retval= FALSE; // some partitions are used
  mark_all_partitions_as_used(prune_param.part_info);
end:
  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
  thd->no_errors=0;
  thd->mem_root= range_par->old_root;
  free_root(&alloc,MYF(0));			// Return memory & allocator
  /*
    Must be a subset of the locked partitions.
    lock_partitions contains the partitions marked by explicit partition
    selection (... t PARTITION (pX) ...) and we must only use partitions
    within that set.
  */
  bitmap_intersect(&prune_param.part_info->read_partitions,
                   &prune_param.part_info->lock_partitions);
  /*
    If not yet locked, also prune partitions to lock if not UPDATEing
    partition key fields. This will also prune lock_partitions if we are under
    LOCK TABLES, so prune away calls to start_stmt().
    TODO: enhance this prune locking to also allow pruning of
    'UPDATE t SET part_key = const WHERE cond_is_prunable' so it adds
    a lock for part_key partition.
  */
  if (table->file->get_lock_type() == F_UNLCK &&
      !partition_key_modified(table, table->write_set))
  {
    bitmap_copy(&prune_param.part_info->lock_partitions,
                &prune_param.part_info->read_partitions);
  }
  if (bitmap_is_clear_all(&(prune_param.part_info->read_partitions)))
  {
    table->all_partitions_pruned_away= true;
    retval= TRUE;
  }
  DBUG_RETURN(retval);
}


/*
  For SEL_ARG* array, store sel_arg->min values into table record buffer

  SYNOPSIS
    store_selargs_to_rec()
      ppar   Partition pruning context
      start  Array of SEL_ARG* for which the minimum values should be stored
      num    Number of elements in the array

  DESCRIPTION
    For each SEL_ARG* interval in the specified array, store the left edge
    field value (sel_arg->min, key image format) into the table record.
*/

static void store_selargs_to_rec(PART_PRUNE_PARAM *ppar, SEL_ARG **start,
                                 int num)
{
  KEY_PART *parts= ppar->range_param.key_parts;
  for (SEL_ARG **end= start + num; start != end; start++)
  {
    SEL_ARG *sel_arg= (*start);
    store_key_image_to_rec(sel_arg->field, sel_arg->min_value,
                           parts[sel_arg->part].length);
  }
}


/* Mark a partition as used in the case when there are no subpartitions */
static void mark_full_partition_used_no_parts(partition_info* part_info,
                                              uint32 part_id)
{
  DBUG_ENTER("mark_full_partition_used_no_parts");
  DBUG_PRINT("enter", ("Mark partition %u as used", part_id));
  bitmap_set_bit(&part_info->read_partitions, part_id);
  DBUG_VOID_RETURN;
}


/* Mark a partition as used in the case when there are subpartitions */
static void mark_full_partition_used_with_parts(partition_info *part_info,
                                                uint32 part_id)
{
  uint32 start= part_id * part_info->num_subparts;
  uint32 end=   start + part_info->num_subparts; 
  DBUG_ENTER("mark_full_partition_used_with_parts");

  for (; start != end; start++)
  {
    DBUG_PRINT("info", ("1:Mark subpartition %u as used", start));
    bitmap_set_bit(&part_info->read_partitions, start);
  }
  DBUG_VOID_RETURN;
}

/*
  Find the set of used partitions for List<SEL_IMERGE>
  SYNOPSIS
    find_used_partitions_imerge_list
      ppar      Partition pruning context.
      key_tree  Intervals tree to perform pruning for.
      
  DESCRIPTION
    List<SEL_IMERGE> represents "imerge1 AND imerge2 AND ...". 
    The set of used partitions is an intersection of used partitions sets
    for imerge_{i}.
    We accumulate this intersection in a separate bitmap.
 
  RETURN 
    See find_used_partitions()
*/

static int find_used_partitions_imerge_list(PART_PRUNE_PARAM *ppar,
                                            List<SEL_IMERGE> &merges)
{
  MY_BITMAP all_merges;
  uint bitmap_bytes;
  my_bitmap_map *bitmap_buf;
  uint n_bits= ppar->part_info->read_partitions.n_bits;
  bitmap_bytes= bitmap_buffer_size(n_bits);
  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(ppar->range_param.mem_root,
                                                bitmap_bytes)))
  {
    /*
      Fallback, process just the first SEL_IMERGE. This can leave us with more
      partitions marked as used then actually needed.
    */
    return find_used_partitions_imerge(ppar, merges.head());
  }
  my_bitmap_init(&all_merges, bitmap_buf, n_bits, FALSE);
  bitmap_set_prefix(&all_merges, n_bits);

  List_iterator<SEL_IMERGE> it(merges);
  SEL_IMERGE *imerge;
  while ((imerge=it++))
  {
    int res= find_used_partitions_imerge(ppar, imerge);
    if (!res)
    {
      /* no used partitions on one ANDed imerge => no used partitions at all */
      return 0;
    }

    if (res != -1)
      bitmap_intersect(&all_merges, &ppar->part_info->read_partitions);


    if (bitmap_is_clear_all(&all_merges))
      return 0;

    bitmap_clear_all(&ppar->part_info->read_partitions);
  }
  memcpy(ppar->part_info->read_partitions.bitmap, all_merges.bitmap,
         bitmap_bytes);
  return 1;
}


/*
  Find the set of used partitions for SEL_IMERGE structure
  SYNOPSIS
    find_used_partitions_imerge()
      ppar      Partition pruning context.
      key_tree  Intervals tree to perform pruning for.
      
  DESCRIPTION
    SEL_IMERGE represents "tree1 OR tree2 OR ...". The implementation is
    trivial - just use mark used partitions for each tree and bail out early
    if for some tree_{i} all partitions are used.
 
  RETURN 
    See find_used_partitions().
*/

static
int find_used_partitions_imerge(PART_PRUNE_PARAM *ppar, SEL_IMERGE *imerge)
{
  int res= 0;
  for (SEL_TREE **ptree= imerge->trees; ptree < imerge->trees_next; ptree++)
  {
    ppar->arg_stack_end= ppar->arg_stack;
    ppar->cur_part_fields= 0;
    ppar->cur_subpart_fields= 0;
    
    ppar->cur_min_key= ppar->range_param.min_key;
    ppar->cur_max_key= ppar->range_param.max_key;
    ppar->cur_min_flag= ppar->cur_max_flag= 0;

    init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
    SEL_ARG *key_tree= (*ptree)->keys[0];
    if (!key_tree || (-1 == (res |= find_used_partitions(ppar, key_tree))))
      return -1;
  }
  return res;
}


/*
  Collect partitioning ranges for the SEL_ARG tree and mark partitions as used

  SYNOPSIS
    find_used_partitions()
      ppar      Partition pruning context.
      key_tree  SEL_ARG range tree to perform pruning for

  DESCRIPTION
    This function 
      * recursively walks the SEL_ARG* tree collecting partitioning "intervals"
      * finds the partitions one needs to use to get rows in these intervals
      * marks these partitions as used.
    The next session desribes the process in greater detail.
 
  IMPLEMENTATION
    TYPES OF RESTRICTIONS THAT WE CAN OBTAIN PARTITIONS FOR    
    We can find out which [sub]partitions to use if we obtain restrictions on 
    [sub]partitioning fields in the following form:
    1.  "partition_field1=const1 AND ... AND partition_fieldN=constN"
    1.1  Same as (1) but for subpartition fields

    If partitioning supports interval analysis (i.e. partitioning is a
    function of a single table field, and partition_info::
    get_part_iter_for_interval != NULL), then we can also use condition in
    this form:
    2.  "const1 <=? partition_field <=? const2"
    2.1  Same as (2) but for subpartition_field

    INFERRING THE RESTRICTIONS FROM SEL_ARG TREE
    
    The below is an example of what SEL_ARG tree may represent:
    
    (start)
     |                           $
     |   Partitioning keyparts   $  subpartitioning keyparts
     |                           $
     |     ...          ...      $
     |      |            |       $
     | +---------+  +---------+  $  +-----------+  +-----------+
     \-| par1=c1 |--| par2=c2 |-----| subpar1=c3|--| subpar2=c5|
       +---------+  +---------+  $  +-----------+  +-----------+
            |                    $        |             |
            |                    $        |        +-----------+ 
            |                    $        |        | subpar2=c6|
            |                    $        |        +-----------+ 
            |                    $        |
            |                    $  +-----------+  +-----------+
            |                    $  | subpar1=c4|--| subpar2=c8|
            |                    $  +-----------+  +-----------+
            |                    $         
            |                    $
       +---------+               $  +------------+  +------------+
       | par1=c2 |------------------| subpar1=c10|--| subpar2=c12|
       +---------+               $  +------------+  +------------+
            |                    $
           ...                   $

    The up-down connections are connections via SEL_ARG::left and
    SEL_ARG::right. A horizontal connection to the right is the
    SEL_ARG::next_key_part connection.
    
    find_used_partitions() traverses the entire tree via recursion on
     * SEL_ARG::next_key_part (from left to right on the picture)
     * SEL_ARG::left|right (up/down on the pic). Left-right recursion is
       performed for each depth level.
    
    Recursion descent on SEL_ARG::next_key_part is used to accumulate (in
    ppar->arg_stack) constraints on partitioning and subpartitioning fields.
    For the example in the above picture, one of stack states is:
      in find_used_partitions(key_tree = "subpar2=c5") (***)
      in find_used_partitions(key_tree = "subpar1=c3")
      in find_used_partitions(key_tree = "par2=c2")   (**)
      in find_used_partitions(key_tree = "par1=c1")
      in prune_partitions(...)
    We apply partitioning limits as soon as possible, e.g. when we reach the
    depth (**), we find which partition(s) correspond to "par1=c1 AND par2=c2",
    and save them in ppar->part_iter.
    When we reach the depth (***), we find which subpartition(s) correspond to
    "subpar1=c3 AND subpar2=c5", and then mark appropriate subpartitions in
    appropriate subpartitions as used.
    
    It is possible that constraints on some partitioning fields are missing.
    For the above example, consider this stack state:
      in find_used_partitions(key_tree = "subpar2=c12") (***)
      in find_used_partitions(key_tree = "subpar1=c10")
      in find_used_partitions(key_tree = "par1=c2")
      in prune_partitions(...)
    Here we don't have constraints for all partitioning fields. Since we've
    never set the ppar->part_iter to contain used set of partitions, we use
    its default "all partitions" value.  We get  subpartition id for 
    "subpar1=c3 AND subpar2=c5", and mark that subpartition as used in every
    partition.

    The inverse is also possible: we may get constraints on partitioning
    fields, but not constraints on subpartitioning fields. In that case,
    calls to find_used_partitions() with depth below (**) will return -1,
    and we will mark entire partition as used.

  TODO
    Replace recursion on SEL_ARG::left and SEL_ARG::right with a loop

  RETURN
    1   OK, one or more [sub]partitions are marked as used.
    0   The passed condition doesn't match any partitions
   -1   Couldn't infer any partition pruning "intervals" from the passed 
        SEL_ARG* tree (which means that all partitions should be marked as
        used) Marking partitions as used is the responsibility of the caller.
*/

static 
int find_used_partitions(PART_PRUNE_PARAM *ppar, SEL_ARG *key_tree)
{
  int res, left_res=0, right_res=0;
  int key_tree_part= (int)key_tree->part;
  bool set_full_part_if_bad_ret= FALSE;
  bool ignore_part_fields= ppar->ignore_part_fields;
  bool did_set_ignore_part_fields= FALSE;
  RANGE_OPT_PARAM *range_par= &(ppar->range_param);

  if (check_stack_overrun(range_par->thd, 3*STACK_MIN_SIZE, NULL))
    return -1;

  if (key_tree->left != &null_element)
  {
    if (-1 == (left_res= find_used_partitions(ppar,key_tree->left)))
      return -1;
  }

  /* Push SEL_ARG's to stack to enable looking backwards as well */
  ppar->cur_part_fields+= ppar->is_part_keypart[key_tree_part];
  ppar->cur_subpart_fields+= ppar->is_subpart_keypart[key_tree_part];
  *(ppar->arg_stack_end++)= key_tree;

  if (ignore_part_fields)
  {
    /*
      We come here when a condition on the first partitioning
      fields led to evaluating the partitioning condition
      (due to finding a condition of the type a < const or
      b > const). Thus we must ignore the rest of the
      partitioning fields but we still want to analyse the
      subpartitioning fields.
    */
    if (key_tree->next_key_part)
      res= find_used_partitions(ppar, key_tree->next_key_part);
    else
      res= -1;
    goto pop_and_go_right;
  }

  if (key_tree->type == SEL_ARG::KEY_RANGE)
  {
    if (ppar->part_info->get_part_iter_for_interval && 
        key_tree->part <= ppar->last_part_partno)
    {
      /* Collect left and right bound, their lengths and flags */
      uchar *min_key= ppar->cur_min_key;
      uchar *max_key= ppar->cur_max_key;
      uchar *tmp_min_key= min_key;
      uchar *tmp_max_key= max_key;
      key_tree->store_min(ppar->key[key_tree->part].store_length,
                          &tmp_min_key, ppar->cur_min_flag);
      key_tree->store_max(ppar->key[key_tree->part].store_length,
                          &tmp_max_key, ppar->cur_max_flag);
      uint flag;
      if (key_tree->next_key_part &&
          key_tree->next_key_part->part == key_tree->part+1 &&
          key_tree->next_key_part->part <= ppar->last_part_partno &&
          key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
      {
        /*
          There are more key parts for partition pruning to handle
          This mainly happens when the condition is an equality
          condition.
        */
        if ((tmp_min_key - min_key) == (tmp_max_key - max_key) && 
            (memcmp(min_key, max_key, (uint)(tmp_max_key - max_key)) == 0) &&
            !key_tree->min_flag && !key_tree->max_flag)
        {
          /* Set 'parameters' */
          ppar->cur_min_key= tmp_min_key;
          ppar->cur_max_key= tmp_max_key;
          uint save_min_flag= ppar->cur_min_flag;
          uint save_max_flag= ppar->cur_max_flag;

          ppar->cur_min_flag|= key_tree->min_flag;
          ppar->cur_max_flag|= key_tree->max_flag;
          
          res= find_used_partitions(ppar, key_tree->next_key_part);
           
          /* Restore 'parameters' back */
          ppar->cur_min_key= min_key;
          ppar->cur_max_key= max_key;

          ppar->cur_min_flag= save_min_flag;
          ppar->cur_max_flag= save_max_flag;
          goto pop_and_go_right;
        }
        /* We have arrived at the last field in the partition pruning */
        uint tmp_min_flag= key_tree->min_flag,
             tmp_max_flag= key_tree->max_flag;
        if (!tmp_min_flag)
          key_tree->next_key_part->store_min_key(ppar->key,
                                                 &tmp_min_key,
                                                 &tmp_min_flag,
                                                 ppar->last_part_partno);
        if (!tmp_max_flag)
          key_tree->next_key_part->store_max_key(ppar->key,
                                                 &tmp_max_key,
                                                 &tmp_max_flag,
                                                 ppar->last_part_partno);
        flag= tmp_min_flag | tmp_max_flag;
      }
      else
        flag= key_tree->min_flag | key_tree->max_flag;
      
      if (tmp_min_key != range_par->min_key)
        flag&= ~NO_MIN_RANGE;
      else
        flag|= NO_MIN_RANGE;
      if (tmp_max_key != range_par->max_key)
        flag&= ~NO_MAX_RANGE;
      else
        flag|= NO_MAX_RANGE;

      /*
        We need to call the interval mapper if we have a condition which
        makes sense to prune on. In the example of COLUMNS on a and
        b it makes sense if we have a condition on a, or conditions on
        both a and b. If we only have conditions on b it might make sense
        but this is a harder case we will solve later. For the harder case
        this clause then turns into use of all partitions and thus we
        simply set res= -1 as if the mapper had returned that.
        TODO: What to do here is defined in WL#4065.
      */
      if (ppar->arg_stack[0]->part == 0)
      {
        uint32 i;
        uint32 store_length_array[MAX_KEY];
        uint32 num_keys= ppar->part_fields;

        for (i= 0; i < num_keys; i++)
          store_length_array[i]= ppar->key[i].store_length;
        res= ppar->part_info->
             get_part_iter_for_interval(ppar->part_info,
                                        FALSE,
                                        store_length_array,
                                        range_par->min_key,
                                        range_par->max_key,
                                        tmp_min_key - range_par->min_key,
                                        tmp_max_key - range_par->max_key,
                                        flag,
                                        &ppar->part_iter);
        if (!res)
          goto pop_and_go_right; /* res==0 --> no satisfying partitions */
      }
      else
        res= -1;

      if (res == -1)
      {
        /* get a full range iterator */
        init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
      }
      /* 
        Save our intent to mark full partition as used if we will not be able
        to obtain further limits on subpartitions
      */
      if (key_tree_part < ppar->last_part_partno)
      {
        /*
          We need to ignore the rest of the partitioning fields in all
          evaluations after this
        */
        did_set_ignore_part_fields= TRUE;
        ppar->ignore_part_fields= TRUE;
      }
      set_full_part_if_bad_ret= TRUE;
      goto process_next_key_part;
    }

    if (key_tree_part == ppar->last_subpart_partno && 
        (NULL != ppar->part_info->get_subpart_iter_for_interval))
    {
      PARTITION_ITERATOR subpart_iter;
      DBUG_EXECUTE("info", dbug_print_segment_range(key_tree,
                                                    range_par->key_parts););
      res= ppar->part_info->
           get_subpart_iter_for_interval(ppar->part_info,
                                         TRUE,
                                         NULL, /* Currently not used here */
                                         key_tree->min_value, 
                                         key_tree->max_value,
                                         0, 0, /* Those are ignored here */
                                         key_tree->min_flag |
                                           key_tree->max_flag,
                                         &subpart_iter);
      if (res == 0)
      {
        /*
           The only case where we can get "no satisfying subpartitions"
           returned from the above call is when an error has occurred.
        */
        DBUG_ASSERT(range_par->thd->is_error());
        return 0;
      }

      if (res == -1)
        goto pop_and_go_right; /* all subpartitions satisfy */

      uint32 subpart_id;
      bitmap_clear_all(&ppar->subparts_bitmap);
      while ((subpart_id= subpart_iter.get_next(&subpart_iter)) !=
             NOT_A_PARTITION_ID)
        bitmap_set_bit(&ppar->subparts_bitmap, subpart_id);

      /* Mark each partition as used in each subpartition.  */
      uint32 part_id;
      while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
              NOT_A_PARTITION_ID)
      {
        for (uint i= 0; i < ppar->part_info->num_subparts; i++)
          if (bitmap_is_set(&ppar->subparts_bitmap, i))
            bitmap_set_bit(&ppar->part_info->read_partitions,
                           part_id * ppar->part_info->num_subparts + i);
      }
      goto pop_and_go_right;
    }

    if (key_tree->is_singlepoint())
    {
      if (key_tree_part == ppar->last_part_partno &&
          ppar->cur_part_fields == ppar->part_fields &&
          ppar->part_info->get_part_iter_for_interval == NULL)
      {
        /* 
          Ok, we've got "fieldN<=>constN"-type SEL_ARGs for all partitioning
          fields. Save all constN constants into table record buffer.
        */
        store_selargs_to_rec(ppar, ppar->arg_stack, ppar->part_fields);
        DBUG_EXECUTE("info", dbug_print_singlepoint_range(ppar->arg_stack,
                                                       ppar->part_fields););
        uint32 part_id;
        longlong func_value;
        /* Find in which partition the {const1, ...,constN} tuple goes */
        if (ppar->get_top_partition_id_func(ppar->part_info, &part_id,
                                            &func_value))
        {
          res= 0; /* No satisfying partitions */
          goto pop_and_go_right;
        }
        /* Rembember the limit we got - single partition #part_id */
        init_single_partition_iterator(part_id, &ppar->part_iter);
        
        /*
          If there are no subpartitions/we fail to get any limit for them, 
          then we'll mark full partition as used. 
        */
        set_full_part_if_bad_ret= TRUE;
        goto process_next_key_part;
      }

      if (key_tree_part == ppar->last_subpart_partno &&
          ppar->cur_subpart_fields == ppar->subpart_fields)
      {
        /* 
          Ok, we've got "fieldN<=>constN"-type SEL_ARGs for all subpartitioning
          fields. Save all constN constants into table record buffer.
        */
        store_selargs_to_rec(ppar, ppar->arg_stack_end - ppar->subpart_fields,
                             ppar->subpart_fields);
        DBUG_EXECUTE("info", dbug_print_singlepoint_range(ppar->arg_stack_end- 
                                                       ppar->subpart_fields,
                                                       ppar->subpart_fields););
        /* Find the subpartition (it's HASH/KEY so we always have one) */
        partition_info *part_info= ppar->part_info;
        uint32 part_id, subpart_id;
                 
        if (part_info->get_subpartition_id(part_info, &subpart_id))
          return 0;

        /* Mark this partition as used in each subpartition. */
        while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
                NOT_A_PARTITION_ID)
        {
          bitmap_set_bit(&part_info->read_partitions,
                         part_id * part_info->num_subparts + subpart_id);
        }
        res= 1; /* Some partitions were marked as used */
        goto pop_and_go_right;
      }
    }
    else
    {
      /* 
        Can't handle condition on current key part. If we're that deep that 
        we're processing subpartititoning's key parts, this means we'll not be
        able to infer any suitable condition, so bail out.
      */
      if (key_tree_part >= ppar->last_part_partno)
      {
        res= -1;
        goto pop_and_go_right;
      }
      /*
        No meaning in continuing with rest of partitioning key parts.
        Will try to continue with subpartitioning key parts.
      */
      ppar->ignore_part_fields= true;
      did_set_ignore_part_fields= true;
      goto process_next_key_part;
    }
  }

process_next_key_part:
  if (key_tree->next_key_part)
    res= find_used_partitions(ppar, key_tree->next_key_part);
  else
    res= -1;

  if (did_set_ignore_part_fields)
  {
    /*
      We have returned from processing all key trees linked to our next
      key part. We are ready to be moving down (using right pointers) and
      this tree is a new evaluation requiring its own decision on whether
      to ignore partitioning fields.
    */
    ppar->ignore_part_fields= FALSE;
  }
  if (set_full_part_if_bad_ret)
  {
    if (res == -1)
    {
      /* Got "full range" for subpartitioning fields */
      uint32 part_id;
      bool found= FALSE;
      while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
             NOT_A_PARTITION_ID)
      {
        ppar->mark_full_partition_used(ppar->part_info, part_id);
        found= TRUE;
      }
      res= MY_TEST(found);
    }
    /*
      Restore the "used partitions iterator" to the default setting that
      specifies iteration over all partitions.
    */
    init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
  }

pop_and_go_right:
  /* Pop this key part info off the "stack" */
  ppar->arg_stack_end--;
  ppar->cur_part_fields-=    ppar->is_part_keypart[key_tree_part];
  ppar->cur_subpart_fields-= ppar->is_subpart_keypart[key_tree_part];

  if (res == -1)
    return -1;
  if (key_tree->right != &null_element)
  {
    if (-1 == (right_res= find_used_partitions(ppar,key_tree->right)))
      return -1;
  }
  return (left_res || right_res || res);
}
 

static void mark_all_partitions_as_used(partition_info *part_info)
{
  bitmap_copy(&(part_info->read_partitions),
              &(part_info->lock_partitions));
}


/*
  Check if field types allow to construct partitioning index description
 
  SYNOPSIS
    fields_ok_for_partition_index()
      pfield  NULL-terminated array of pointers to fields.

  DESCRIPTION
    For an array of fields, check if we can use all of the fields to create
    partitioning index description.
    
    We can't process GEOMETRY fields - for these fields singlepoint intervals
    cant be generated, and non-singlepoint are "special" kinds of intervals
    to which our processing logic can't be applied.

    It is not known if we could process ENUM fields, so they are disabled to be
    on the safe side.

  RETURN 
    TRUE   Yes, fields can be used in partitioning index
    FALSE  Otherwise
*/

static bool fields_ok_for_partition_index(Field **pfield)
{
  if (!pfield)
    return FALSE;
  for (; (*pfield); pfield++)
  {
    enum_field_types ftype= (*pfield)->real_type();
    if (ftype == MYSQL_TYPE_ENUM || ftype == MYSQL_TYPE_GEOMETRY)
      return FALSE;
  }
  return TRUE;
}


/*
  Create partition index description and fill related info in the context
  struct

  SYNOPSIS
    create_partition_index_description()
      prune_par  INOUT Partition pruning context

  DESCRIPTION
    Create partition index description. Partition index description is:

      part_index(used_fields_list(part_expr), used_fields_list(subpart_expr))

    If partitioning/sub-partitioning uses BLOB or Geometry fields, then
    corresponding fields_list(...) is not included into index description
    and we don't perform partition pruning for partitions/subpartitions.

  RETURN
    TRUE   Out of memory or can't do partition pruning at all
    FALSE  OK
*/

static bool create_partition_index_description(PART_PRUNE_PARAM *ppar)
{
  RANGE_OPT_PARAM *range_par= &(ppar->range_param);
  partition_info *part_info= ppar->part_info;
  uint used_part_fields, used_subpart_fields;

  used_part_fields= fields_ok_for_partition_index(part_info->part_field_array) ?
                      part_info->num_part_fields : 0;
  used_subpart_fields= 
    fields_ok_for_partition_index(part_info->subpart_field_array)? 
      part_info->num_subpart_fields : 0;
  
  uint total_parts= used_part_fields + used_subpart_fields;

  ppar->ignore_part_fields= FALSE;
  ppar->part_fields=      used_part_fields;
  ppar->last_part_partno= (int)used_part_fields - 1;

  ppar->subpart_fields= used_subpart_fields;
  ppar->last_subpart_partno= 
    used_subpart_fields?(int)(used_part_fields + used_subpart_fields - 1): -1;

  if (part_info->is_sub_partitioned())
  {
    ppar->mark_full_partition_used=  mark_full_partition_used_with_parts;
    ppar->get_top_partition_id_func= part_info->get_part_partition_id;
  }
  else
  {
    ppar->mark_full_partition_used=  mark_full_partition_used_no_parts;
    ppar->get_top_partition_id_func= part_info->get_partition_id;
  }

  KEY_PART *key_part;
  MEM_ROOT *alloc= range_par->mem_root;
  if (!total_parts || 
      !(key_part= (KEY_PART*)alloc_root(alloc, sizeof(KEY_PART)*
                                               total_parts)) ||
      !(ppar->arg_stack= (SEL_ARG**)alloc_root(alloc, sizeof(SEL_ARG*)* 
                                                      total_parts)) ||
      !(ppar->is_part_keypart= (my_bool*)alloc_root(alloc, sizeof(my_bool)*
                                                           total_parts)) ||
      !(ppar->is_subpart_keypart= (my_bool*)alloc_root(alloc, sizeof(my_bool)*
                                                           total_parts)))
    return TRUE;
 
  if (ppar->subpart_fields)
  {
    my_bitmap_map *buf;
    uint32 bufsize= bitmap_buffer_size(ppar->part_info->num_subparts);
    if (!(buf= (my_bitmap_map*) alloc_root(alloc, bufsize)))
      return TRUE;
    my_bitmap_init(&ppar->subparts_bitmap, buf, ppar->part_info->num_subparts,
                FALSE);
  }
  range_par->key_parts= key_part;
  Field **field= (ppar->part_fields)? part_info->part_field_array :
                                           part_info->subpart_field_array;
  bool in_subpart_fields= FALSE;
  uint total_key_len= 0;
  for (uint part= 0; part < total_parts; part++, key_part++)
  {
    key_part->key=          0;
    key_part->part=	    part;
    key_part->length= (uint16)(*field)->key_length();
    key_part->store_length= (uint16)get_partition_field_store_length(*field);
    total_key_len += key_part->store_length;

    DBUG_PRINT("info", ("part %u length %u store_length %u", part,
                         key_part->length, key_part->store_length));

    key_part->field=        (*field);
    key_part->image_type =  Field::itRAW;
    /* 
      We set keypart flag to 0 here as the only HA_PART_KEY_SEG is checked
      in the RangeAnalysisModule.
    */
    key_part->flag=         0;
    /* We don't set key_parts->null_bit as it will not be used */

    ppar->is_part_keypart[part]= !in_subpart_fields;
    ppar->is_subpart_keypart[part]= in_subpart_fields;

    /*
      Check if this was last field in this array, in this case we
      switch to subpartitioning fields. (This will only happens if
      there are subpartitioning fields to cater for).
    */
    if (!*(++field))
    {
      field= part_info->subpart_field_array;
      in_subpart_fields= TRUE;
    }
  }
  range_par->key_parts_end= key_part;

  total_key_len++; /* Take into account the "+1" in QUICK_RANGE::QUICK_RANGE */
  if (!(range_par->min_key= (uchar*)alloc_root(alloc,total_key_len)) ||
      !(range_par->max_key= (uchar*)alloc_root(alloc,total_key_len)))
  {
    return true;
  }

  DBUG_EXECUTE("info", print_partitioning_index(range_par->key_parts,
                                                range_par->key_parts_end););
  return FALSE;
}


#ifndef DBUG_OFF

static void print_partitioning_index(KEY_PART *parts, KEY_PART *parts_end)
{
  DBUG_ENTER("print_partitioning_index");
  DBUG_LOCK_FILE;
  fprintf(DBUG_FILE, "partitioning INDEX(");
  for (KEY_PART *p=parts; p != parts_end; p++)
  {
    fprintf(DBUG_FILE, "%s%s", p==parts?"":" ,", p->field->field_name.str);
  }
  fputs(");\n", DBUG_FILE);
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}

/* Print field value into debug trace, in NULL-aware way. */
static void dbug_print_field(Field *field)
{
  if (field->is_real_null())
    fprintf(DBUG_FILE, "NULL");
  else
  {
    char buf[256];
    String str(buf, sizeof(buf), &my_charset_bin);
    str.length(0);
    String *pstr;
    pstr= field->val_str(&str);
    fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
  }
}


/* Print a "c1 < keypartX < c2" - type interval into debug trace. */
static void dbug_print_segment_range(SEL_ARG *arg, KEY_PART *part)
{
  DBUG_ENTER("dbug_print_segment_range");
  DBUG_LOCK_FILE;
  if (!(arg->min_flag & NO_MIN_RANGE))
  {
    store_key_image_to_rec(part->field, arg->min_value, part->length);
    dbug_print_field(part->field);
    if (arg->min_flag & NEAR_MIN)
      fputs(" < ", DBUG_FILE);
    else
      fputs(" <= ", DBUG_FILE);
  }

  fprintf(DBUG_FILE, "%s", part->field->field_name.str);

  if (!(arg->max_flag & NO_MAX_RANGE))
  {
    if (arg->max_flag & NEAR_MAX)
      fputs(" < ", DBUG_FILE);
    else
      fputs(" <= ", DBUG_FILE);
    store_key_image_to_rec(part->field, arg->max_value, part->length);
    dbug_print_field(part->field);
  }
  fputs("\n", DBUG_FILE);
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}


/*
  Print a singlepoint multi-keypart range interval to debug trace
 
  SYNOPSIS
    dbug_print_singlepoint_range()
      start  Array of SEL_ARG* ptrs representing conditions on key parts
      num    Number of elements in the array.

  DESCRIPTION
    This function prints a "keypartN=constN AND ... AND keypartK=constK"-type 
    interval to debug trace.
*/

static void dbug_print_singlepoint_range(SEL_ARG **start, uint num)
{
  DBUG_ENTER("dbug_print_singlepoint_range");
  DBUG_LOCK_FILE;
  SEL_ARG **end= start + num;

  for (SEL_ARG **arg= start; arg != end; arg++)
  {
    Field *field= (*arg)->field;
    fprintf(DBUG_FILE, "%s%s=", (arg==start)?"":", ", field->field_name.str);
    dbug_print_field(field);
  }
  fputs("\n", DBUG_FILE);
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}
#endif

/****************************************************************************
 * Partition pruning code ends
 ****************************************************************************/
#endif


/*
  Get cost of 'sweep' full records retrieval.
  SYNOPSIS
    get_sweep_read_cost()
      param            Parameter from test_quick_select
      records          # of records to be retrieved
  RETURN
    cost of sweep
*/

double get_sweep_read_cost(const PARAM *param, ha_rows records)
{
  double result;
  DBUG_ENTER("get_sweep_read_cost");
  if (param->table->file->primary_key_is_clustered())
  {
    /*
      We are using the primary key to find the rows.
      Calculate the cost for this.
    */
    result= param->table->file->read_time(param->table->s->primary_key,
                                          (uint)records, records);
  }
  else
  {
    /*
      Rows will be retreived with rnd_pos(). Caluclate the expected
      cost for this.
    */
    double n_blocks=
      ceil(ulonglong2double(param->table->file->stats.data_file_length) /
           IO_SIZE);
    double busy_blocks=
      n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, rows2double(records)));
    if (busy_blocks < 1.0)
      busy_blocks= 1.0;
    DBUG_PRINT("info",("sweep: nblocks: %g, busy_blocks: %g", n_blocks,
                       busy_blocks));
    /*
      Disabled: Bail out if # of blocks to read is bigger than # of blocks in
      table data file.
    if (max_cost != DBL_MAX  && (busy_blocks+index_reads_cost) >= n_blocks)
      return 1;
    */
    JOIN *join= param->thd->lex->select_lex.join;
    if (!join || join->table_count == 1)
    {
      /* No join, assume reading is done in one 'sweep' */
      result= busy_blocks*(DISK_SEEK_BASE_COST +
                          DISK_SEEK_PROP_COST*n_blocks/busy_blocks);
    }
    else
    {
      /*
        Possibly this is a join with source table being non-last table, so
        assume that disk seeks are random here.
      */
      result= busy_blocks;
    }
  }
  DBUG_PRINT("return",("cost: %g", result));
  DBUG_RETURN(result);
}


/*
  Get best plan for a SEL_IMERGE disjunctive expression.
  SYNOPSIS
    get_best_disjunct_quick()
      param     Parameter from check_quick_select function
      imerge    Expression to use
      read_time Don't create scans with cost > read_time

  NOTES
    index_merge cost is calculated as follows:
    index_merge_cost =
      cost(index_reads) +         (see #1)
      cost(rowid_to_row_scan) +   (see #2)
      cost(unique_use)            (see #3)

    1. cost(index_reads) =SUM_i(cost(index_read_i))
       For non-CPK scans,
         cost(index_read_i) = {cost of ordinary 'index only' scan}
       For CPK scan,
         cost(index_read_i) = {cost of non-'index only' scan}

    2. cost(rowid_to_row_scan)
      If table PK is clustered then
        cost(rowid_to_row_scan) =
          {cost of ordinary clustered PK scan with n_ranges=n_rows}

      Otherwise, we use the following model to calculate costs:
      We need to retrieve n_rows rows from file that occupies n_blocks blocks.
      We assume that offsets of rows we need are independent variates with
      uniform distribution in [0..max_file_offset] range.

      We'll denote block as "busy" if it contains row(s) we need to retrieve
      and "empty" if doesn't contain rows we need.

      Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
      applies to any block in file). Let x_i be a variate taking value 1 if
      block #i is empty and 0 otherwise.

      Then E(x_i) = (1 - 1/n_blocks)^n_rows;

      E(n_empty_blocks) = E(sum(x_i)) = sum(E(x_i)) =
        = n_blocks * ((1 - 1/n_blocks)^n_rows) =
       ~= n_blocks * exp(-n_rows/n_blocks).

      E(n_busy_blocks) = n_blocks*(1 - (1 - 1/n_blocks)^n_rows) =
       ~= n_blocks * (1 - exp(-n_rows/n_blocks)).

      Average size of "hole" between neighbor non-empty blocks is
           E(hole_size) = n_blocks/E(n_busy_blocks).

      The total cost of reading all needed blocks in one "sweep" is:

      E(n_busy_blocks)*
       (DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*n_blocks/E(n_busy_blocks)).

    3. Cost of Unique use is calculated in Unique::get_use_cost function.

  ROR-union cost is calculated in the same way index_merge, but instead of
  Unique a priority queue is used.

  RETURN
    Created read plan
    NULL - Out of memory or no read scan could be built.
*/

static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
                                         double read_time)
{
  SEL_TREE **ptree;
  TRP_INDEX_MERGE *imerge_trp= NULL;
  TRP_RANGE **range_scans;
  TRP_RANGE **cur_child;
  TRP_RANGE **cpk_scan= NULL;
  bool imerge_too_expensive= FALSE;
  double imerge_cost= 0.0;
  ha_rows cpk_scan_records= 0;
  ha_rows non_cpk_scan_records= 0;
  bool pk_is_clustered= param->table->file->primary_key_is_clustered();
  bool all_scans_ror_able= TRUE;
  bool all_scans_rors= TRUE;
  uint unique_calc_buff_size;
  TABLE_READ_PLAN **roru_read_plans;
  TABLE_READ_PLAN **cur_roru_plan;
  double roru_index_costs;
  ha_rows roru_total_records;
  double roru_intersect_part= 1.0;
  DBUG_ENTER("get_best_disjunct_quick");
  DBUG_PRINT("info", ("Full table scan cost: %g", read_time));

  /*
    In every tree of imerge remove SEL_ARG trees that do not make ranges.
    If after this removal some SEL_ARG tree becomes empty discard imerge.  
  */
  for (ptree= imerge->trees; ptree != imerge->trees_next; ptree++)
  {
    if (remove_nonrange_trees(param, *ptree))
    {
      imerge->trees_next= imerge->trees;
      break;
    }
  }

  uint n_child_scans= imerge->trees_next - imerge->trees;
  
  if (!n_child_scans)
    DBUG_RETURN(NULL);

  if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
                                             sizeof(TRP_RANGE*)*
                                             n_child_scans)))
    DBUG_RETURN(NULL);
  /*
    Collect best 'range' scan for each of disjuncts, and, while doing so,
    analyze possibility of ROR scans. Also calculate some values needed by
    other parts of the code.
  */
  for (ptree= imerge->trees, cur_child= range_scans;
       ptree != imerge->trees_next;
       ptree++, cur_child++)
  {
    DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
                                        "tree in SEL_IMERGE"););
    if (!(*cur_child= get_key_scans_params(param, *ptree, TRUE, FALSE, read_time)))
    {
      /*
        One of index scans in this index_merge is more expensive than entire
        table read for another available option. The entire index_merge (and
        any possible ROR-union) will be more expensive then, too. We continue
        here only to update SQL_SELECT members.
      */
      imerge_too_expensive= TRUE;
    }
    if (imerge_too_expensive)
      continue;

    imerge_cost += (*cur_child)->read_cost;
    all_scans_ror_able &= ((*ptree)->n_ror_scans > 0);
    all_scans_rors &= (*cur_child)->is_ror;
    if (pk_is_clustered &&
        param->real_keynr[(*cur_child)->key_idx] ==
        param->table->s->primary_key)
    {
      cpk_scan= cur_child;
      cpk_scan_records= (*cur_child)->records;
    }
    else
      non_cpk_scan_records += (*cur_child)->records;
  }

  DBUG_PRINT("info", ("index_merge scans cost %g", imerge_cost));
  if (imerge_too_expensive || (imerge_cost > read_time) ||
      ((non_cpk_scan_records+cpk_scan_records >=
        param->table->stat_records()) &&
       read_time != DBL_MAX))
  {
    /*
      Bail out if it is obvious that both index_merge and ROR-union will be
      more expensive
    */
    DBUG_PRINT("info", ("Sum of index_merge scans is more expensive than "
                        "full table scan, bailing out"));
    DBUG_RETURN(NULL);
  }

  /* 
    If all scans happen to be ROR, proceed to generate a ROR-union plan (it's 
    guaranteed to be cheaper than non-ROR union), unless ROR-unions are
    disabled in @@optimizer_switch
  */
  if (all_scans_rors && 
      optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_UNION))
  {
    roru_read_plans= (TABLE_READ_PLAN**)range_scans;
    goto skip_to_ror_scan;
  }

  if (cpk_scan)
  {
    /*
      Add one ROWID comparison for each row retrieved on non-CPK scan.  (it
      is done in QUICK_RANGE_SELECT::row_in_ranges)
     */
    imerge_cost += non_cpk_scan_records / TIME_FOR_COMPARE_ROWID;
  }

  /* Calculate cost(rowid_to_row_scan) */
  imerge_cost += get_sweep_read_cost(param, non_cpk_scan_records);
  DBUG_PRINT("info",("index_merge cost with rowid-to-row scan: %g",
                     imerge_cost));
  if (imerge_cost > read_time || 
      !optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION))
  {
    goto build_ror_index_merge;
  }

  /* Add Unique operations cost */
  unique_calc_buff_size=
    Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
                                    param->table->file->ref_length,
                                    param->thd->variables.sortbuff_size);
  if (param->imerge_cost_buff_size < unique_calc_buff_size)
  {
    if (!(param->imerge_cost_buff= (uint*)alloc_root(param->mem_root,
                                                     unique_calc_buff_size)))
      DBUG_RETURN(NULL);
    param->imerge_cost_buff_size= unique_calc_buff_size;
  }

  imerge_cost +=
    Unique::get_use_cost(param->imerge_cost_buff, (uint)non_cpk_scan_records,
                         param->table->file->ref_length,
                         param->thd->variables.sortbuff_size,
                         TIME_FOR_COMPARE_ROWID,
                         FALSE, NULL);
  DBUG_PRINT("info",("index_merge total cost: %g (wanted: less then %g)",
                     imerge_cost, read_time));
  if (imerge_cost < read_time)
  {
    if ((imerge_trp= new (param->mem_root)TRP_INDEX_MERGE))
    {
      imerge_trp->read_cost= imerge_cost;
      imerge_trp->records= non_cpk_scan_records + cpk_scan_records;
      imerge_trp->records= MY_MIN(imerge_trp->records,
                               param->table->stat_records());
      imerge_trp->range_scans= range_scans;
      imerge_trp->range_scans_end= range_scans + n_child_scans;
      read_time= imerge_cost;
    }
    if (imerge_trp)
    {
      TABLE_READ_PLAN *trp= merge_same_index_scans(param, imerge, imerge_trp,
                                                   read_time);
      if (trp != imerge_trp)
        DBUG_RETURN(trp);
    }
  }

build_ror_index_merge:
  if (!all_scans_ror_able || 
      param->thd->lex->sql_command == SQLCOM_DELETE ||
      !optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_UNION))
    DBUG_RETURN(imerge_trp);

  /* Ok, it is possible to build a ROR-union, try it. */
  bool dummy;
  if (!(roru_read_plans=
          (TABLE_READ_PLAN**)alloc_root(param->mem_root,
                                        sizeof(TABLE_READ_PLAN*)*
                                        n_child_scans)))
    DBUG_RETURN(imerge_trp);

skip_to_ror_scan:
  roru_index_costs= 0.0;
  roru_total_records= 0;
  cur_roru_plan= roru_read_plans;

  /* Find 'best' ROR scan for each of trees in disjunction */
  for (ptree= imerge->trees, cur_child= range_scans;
       ptree != imerge->trees_next;
       ptree++, cur_child++, cur_roru_plan++)
  {
    /*
      Assume the best ROR scan is the one that has cheapest full-row-retrieval
      scan cost.
      Also accumulate index_only scan costs as we'll need them to calculate
      overall index_intersection cost.
    */
    double cost;
    if ((*cur_child)->is_ror)
    {
      /* Ok, we have index_only cost, now get full rows scan cost */
      cost= param->table->file->
              read_time(param->real_keynr[(*cur_child)->key_idx], 1,
                        (*cur_child)->records) +
              rows2double((*cur_child)->records) / TIME_FOR_COMPARE;
    }
    else
      cost= read_time;

    TABLE_READ_PLAN *prev_plan= *cur_child;
    if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost,
                                                 &dummy)))
    {
      if (prev_plan->is_ror)
        *cur_roru_plan= prev_plan;
      else
        DBUG_RETURN(imerge_trp);
      roru_index_costs += (*cur_roru_plan)->read_cost;
    }
    else
      roru_index_costs +=
        ((TRP_ROR_INTERSECT*)(*cur_roru_plan))->index_scan_costs;
    roru_total_records += (*cur_roru_plan)->records;
    roru_intersect_part *= (*cur_roru_plan)->records /
                           param->table->stat_records();
  }

  /*
    rows to retrieve=
      SUM(rows_in_scan_i) - table_rows * PROD(rows_in_scan_i / table_rows).
    This is valid because index_merge construction guarantees that conditions
    in disjunction do not share key parts.
  */
  roru_total_records -= (ha_rows)(roru_intersect_part*
                                  param->table->stat_records());
  /* ok, got a ROR read plan for each of the disjuncts
    Calculate cost:
    cost(index_union_scan(scan_1, ... scan_n)) =
      SUM_i(cost_of_index_only_scan(scan_i)) +
      queue_use_cost(rowid_len, n) +
      cost_of_row_retrieval
    See get_merge_buffers_cost function for queue_use_cost formula derivation.
  */

  double roru_total_cost;
  roru_total_cost= roru_index_costs +
                   rows2double(roru_total_records)*log((double)n_child_scans) /
                   (TIME_FOR_COMPARE_ROWID * M_LN2) +
                   get_sweep_read_cost(param, roru_total_records);

  DBUG_PRINT("info", ("ROR-union: cost %g, %d members", roru_total_cost,
                      n_child_scans));
  TRP_ROR_UNION* roru;
  if (roru_total_cost < read_time)
  {
    if ((roru= new (param->mem_root) TRP_ROR_UNION))
    {
      roru->first_ror= roru_read_plans;
      roru->last_ror= roru_read_plans + n_child_scans;
      roru->read_cost= roru_total_cost;
      roru->records= roru_total_records;
      DBUG_RETURN(roru);
    }
  }
    DBUG_RETURN(imerge_trp);
}


/*
  Merge index scans for the same indexes in an index merge plan

  SYNOPSIS
    merge_same_index_scans()
      param           Context info for the operation
      imerge   IN/OUT SEL_IMERGE from which imerge_trp has been extracted          
      imerge_trp      The index merge plan where index scans for the same
                      indexes are to be merges
      read_time       The upper bound for the cost of the plan to be evaluated

  DESRIPTION
    For the given index merge plan imerge_trp extracted from the SEL_MERGE
    imerge the function looks for range scans with the same indexes and merges
    them into SEL_ARG trees. Then for each such SEL_ARG tree r_i the function
    creates a range tree rt_i that contains only r_i. All rt_i are joined
    into one index merge that replaces the original index merge imerge.
    The function calls get_best_disjunct_quick for the new index merge to
    get a new index merge plan that contains index scans only for different
    indexes.
    If there are no index scans for the same index in the original index
    merge plan the function does not change the original imerge and returns
    imerge_trp as its result.

  RETURN
    The original or or improved index merge plan                        
*/

static
TABLE_READ_PLAN *merge_same_index_scans(PARAM *param, SEL_IMERGE *imerge,
                                        TRP_INDEX_MERGE *imerge_trp,
                                        double read_time)
{
  uint16 first_scan_tree_idx[MAX_KEY];
  SEL_TREE **tree;
  TRP_RANGE **cur_child;
  uint removed_cnt= 0;

  DBUG_ENTER("merge_same_index_scans");

  bzero(first_scan_tree_idx, sizeof(first_scan_tree_idx[0])*param->keys);

  for (tree= imerge->trees, cur_child= imerge_trp->range_scans;
       tree != imerge->trees_next;
       tree++, cur_child++)
  {
    DBUG_ASSERT(tree);
    uint key_idx= (*cur_child)->key_idx;
    uint16 *tree_idx_ptr= &first_scan_tree_idx[key_idx];
    if (!*tree_idx_ptr)
      *tree_idx_ptr= (uint16) (tree-imerge->trees+1);
    else
    {
      SEL_TREE **changed_tree= imerge->trees+(*tree_idx_ptr-1);
      SEL_ARG *key= (*changed_tree)->keys[key_idx];
      for (uint i= 0; i < param->keys; i++)
        (*changed_tree)->keys[i]= NULL;
      (*changed_tree)->keys_map.clear_all();
      if (key) 
        key->incr_refs(); 
      if ((*tree)->keys[key_idx]) 
        (*tree)->keys[key_idx]->incr_refs(); 
      if (((*changed_tree)->keys[key_idx]=
             key_or(param, key, (*tree)->keys[key_idx])))
        (*changed_tree)->keys_map.set_bit(key_idx);
      *tree= NULL;
      removed_cnt++;
    }
  }
  if (!removed_cnt)
    DBUG_RETURN(imerge_trp);

  TABLE_READ_PLAN *trp= NULL;
  SEL_TREE **new_trees_next= imerge->trees;
  for (tree= new_trees_next; tree != imerge->trees_next; tree++)
  {
    if (!*tree)
      continue;
    if (tree > new_trees_next)
      *new_trees_next= *tree;
    new_trees_next++;
  }
  imerge->trees_next= new_trees_next;

  DBUG_ASSERT(imerge->trees_next>imerge->trees);

  if (imerge->trees_next-imerge->trees > 1)
    trp= get_best_disjunct_quick(param, imerge, read_time);
  else
  {
    /*
      This alternative theoretically can be reached when the cost
      of the index merge for such a formula as
        (key1 BETWEEN c1_1 AND c1_2) AND key2 > c2 OR
        (key1 BETWEEN c1_3 AND c1_4) AND key3 > c3
      is estimated as being cheaper than the cost of index scan for
      the formula
        (key1 BETWEEN c1_1 AND c1_2) OR (key1 BETWEEN c1_3 AND c1_4)
      
      In the current code this may happen for two reasons:
      1. for a single index range scan data records are accessed in
         a random order
      2. the functions that estimate the cost of a range scan and an
         index merge retrievals are not well calibrated
    */
    trp= get_key_scans_params(param, *imerge->trees, FALSE, TRUE,
                              read_time);
  }

  DBUG_RETURN(trp); 
}


/*
  This structure contains the info common for all steps of a partial
  index intersection plan. Morever it contains also the info common
  for index intersect plans. This info is filled in by the function
  prepare_search_best just before searching for the best index
  intersection plan.
*/  

typedef struct st_common_index_intersect_info
{
  PARAM *param;           /* context info for range optimizations            */
  uint key_size;          /* size of a ROWID element stored in Unique object */
  uint compare_factor;         /* 1/compare - cost to compare two ROWIDs     */
  ulonglong max_memory_size;   /* maximum space allowed for Unique objects   */   
  ha_rows table_cardinality;   /* estimate of the number of records in table */
  double cutoff_cost;        /* discard index intersects with greater costs  */ 
  INDEX_SCAN_INFO *cpk_scan;  /* clustered primary key used in intersection  */

  bool in_memory;  /* unique object for intersection is completely in memory */

  INDEX_SCAN_INFO **search_scans;    /* scans possibly included in intersect */ 
  uint n_search_scans;               /* number of elements in search_scans   */

  bool best_uses_cpk;   /* current best intersect uses clustered primary key */
  double best_cost;       /* cost of the current best index intersection     */
  /* estimate of the number of records in the current best intersection      */
  ha_rows best_records;
  uint best_length;    /* number of indexes in the current best intersection */
  INDEX_SCAN_INFO **best_intersect;  /* the current best index intersection  */
  /* scans from the best intersect to be filtrered by cpk conditions         */
  key_map filtered_scans; 

  uint *buff_elems;        /* buffer to calculate cost of index intersection */
  
} COMMON_INDEX_INTERSECT_INFO;


/*
  This structure contains the info specific for one step of an index
  intersection plan. The structure is filled in by the function 
   check_index_intersect_extension.
*/

typedef struct st_partial_index_intersect_info
{
  COMMON_INDEX_INTERSECT_INFO *common_info;    /* shared by index intersects */
  uint length;         /* number of index scans in the partial intersection  */
  ha_rows records;     /* estimate of the number of records in intersection  */
  double cost;         /* cost of the partial index intersection             */

  /* estimate of total number of records of all scans of the partial index
     intersect sent to the Unique object used for the intersection  */
  ha_rows records_sent_to_unique;

  /* total cost of the scans of indexes from the partial index intersection  */
  double index_read_cost; 

  bool use_cpk_filter;      /* cpk filter is to be used for this       scan  */  
  bool in_memory;            /* uses unique object in memory                 */
  double in_memory_cost;     /* cost of using unique object in memory        */

  key_map filtered_scans;    /* scans to be filtered by cpk conditions       */
         
  MY_BITMAP *intersect_fields;     /* bitmap of fields used in intersection  */
} PARTIAL_INDEX_INTERSECT_INFO;


/* Check whether two indexes have the same first n components */

static
bool same_index_prefix(KEY *key1, KEY *key2, uint used_parts)
{
  KEY_PART_INFO *part1= key1->key_part;
  KEY_PART_INFO *part2= key2->key_part;
  for(uint i= 0; i < used_parts; i++, part1++, part2++)
  {
    if (part1->fieldnr != part2->fieldnr)
      return FALSE;
  }
  return TRUE;
}


/* Create a bitmap for all fields of a table */

static
bool create_fields_bitmap(PARAM *param, MY_BITMAP *fields_bitmap)
{
  my_bitmap_map *bitmap_buf;

  if (!(bitmap_buf= (my_bitmap_map *) alloc_root(param->mem_root,
                                                 param->fields_bitmap_size)))
    return TRUE;
  if (my_bitmap_init(fields_bitmap, bitmap_buf, param->table->s->fields, FALSE))
    return TRUE;
  
  return FALSE;
}

/* Compare two indexes scans for sort before search for the best intersection */

static
int cmp_intersect_index_scan(INDEX_SCAN_INFO **a, INDEX_SCAN_INFO **b)
{
  return (*a)->records < (*b)->records ?
          -1 : (*a)->records == (*b)->records ? 0 : 1;
}


static inline
void set_field_bitmap_for_index_prefix(MY_BITMAP *field_bitmap,
                                       KEY_PART_INFO *key_part,
                                       uint used_key_parts)
{
  bitmap_clear_all(field_bitmap);
  for (KEY_PART_INFO *key_part_end= key_part+used_key_parts;
       key_part < key_part_end; key_part++)
  {
    bitmap_set_bit(field_bitmap, key_part->fieldnr-1);
  }
}


/*
  Round up table cardinality read from statistics provided by engine.
  This function should go away when mysql test will allow to handle
  more or less easily in the test suites deviations of InnoDB 
  statistical data.
*/
 
static inline
ha_rows get_table_cardinality_for_index_intersect(TABLE *table)
{
  if (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT)
    return table->stat_records();
  else
  {
    ha_rows d;
    double q;
    for (q= (double)table->stat_records(), d= 1 ; q >= 10; q/= 10, d*= 10 ) ;
    return (ha_rows) (floor(q+0.5) * d);
  } 
}

  
static
ha_rows records_in_index_intersect_extension(PARTIAL_INDEX_INTERSECT_INFO *curr,
                                             INDEX_SCAN_INFO *ext_index_scan);

/*
  Prepare to search for the best index intersection

  SYNOPSIS
    prepare_search_best_index_intersect()
      param         common info about index ranges
      tree          tree of ranges for indexes than can be intersected
      common    OUT info needed for search to be filled by the function 
      init      OUT info for an initial pseudo step of the intersection plans
      cutoff_cost   cut off cost of the interesting index intersection 

  DESCRIPTION
    The function initializes all fields of the structure 'common' to be used
    when searching for the best intersection plan. It also allocates
    memory to store the most cheap index intersection.

  NOTES
    When selecting candidates for index intersection we always take only
    one representative out of any set of indexes that share the same range
    conditions. These indexes always have the same prefixes and the
    components of this prefixes are exactly those used in these range
    conditions.
    Range conditions over clustered primary key (cpk) is always used only
    as the condition that filters out some rowids retrieved by the scans
    for secondary indexes. The cpk index will be handled in special way by
    the function that search for the best index intersection. 

  RETURN
    FALSE  in the case of success
    TRUE   otherwise
*/

static
bool prepare_search_best_index_intersect(PARAM *param, 
                                         SEL_TREE *tree,
                                         COMMON_INDEX_INTERSECT_INFO *common,
                                         PARTIAL_INDEX_INTERSECT_INFO *init,
                                         double cutoff_cost)
{
  uint i;
  uint n_search_scans;
  double cost;
  INDEX_SCAN_INFO **index_scan;
  INDEX_SCAN_INFO **scan_ptr;
  INDEX_SCAN_INFO *cpk_scan= NULL;
  TABLE *table= param->table;
  uint n_index_scans= tree->index_scans_end - tree->index_scans;

  if (!n_index_scans)
    return 1;

  bzero(init, sizeof(*init));
  init->filtered_scans.init();
  init->common_info= common;
  init->cost= cutoff_cost;

  common->param= param;
  common->key_size= table->file->ref_length;
  common->compare_factor= TIME_FOR_COMPARE_ROWID;
  common->max_memory_size= param->thd->variables.sortbuff_size;
  common->cutoff_cost= cutoff_cost;
  common->cpk_scan= NULL;
  common->table_cardinality= 
    get_table_cardinality_for_index_intersect(table);

  if (n_index_scans <= 1)
    return TRUE;

  if (table->file->primary_key_is_clustered())
  {
    INDEX_SCAN_INFO **index_scan_end;
    index_scan= tree->index_scans;
    index_scan_end= index_scan+n_index_scans;
    for ( ; index_scan < index_scan_end; index_scan++)
    {  
      if ((*index_scan)->keynr == table->s->primary_key)
      {
        common->cpk_scan= cpk_scan= *index_scan;
        break;
      }
    }
  }

  i= n_index_scans - MY_TEST(cpk_scan != NULL) + 1;

  if (!(common->search_scans =
	(INDEX_SCAN_INFO **) alloc_root (param->mem_root,
                                         sizeof(INDEX_SCAN_INFO *) * i)))
    return TRUE;
  bzero(common->search_scans, sizeof(INDEX_SCAN_INFO *) * i);

  INDEX_SCAN_INFO **selected_index_scans= common->search_scans;
    
  for (i=0, index_scan= tree->index_scans; i < n_index_scans; i++, index_scan++)
  {
    uint used_key_parts= (*index_scan)->used_key_parts;
    KEY *key_info= (*index_scan)->key_info;

    if (*index_scan == cpk_scan)
      continue;
    if (cpk_scan && cpk_scan->used_key_parts >= used_key_parts &&
        same_index_prefix(cpk_scan->key_info, key_info, used_key_parts))
      continue;

    cost= table->file->keyread_time((*index_scan)->keynr,
                                    (*index_scan)->range_count,
                                    (*index_scan)->records);
    if (cost >= cutoff_cost)
      continue;
   
    for (scan_ptr= selected_index_scans; *scan_ptr ; scan_ptr++)
    {
      /*
        When we have range conditions for two different indexes with the same
        beginning it does not make sense to consider both of them for index 
        intersection if the range conditions are covered by common initial
        components of the indexes. Actually in this case the indexes are
        guaranteed to have the same range conditions.
      */
      if ((*scan_ptr)->used_key_parts == used_key_parts &&
          same_index_prefix((*scan_ptr)->key_info, key_info, used_key_parts))
        break;
    }
    if (!*scan_ptr || cost < (*scan_ptr)->index_read_cost)
    {
      *scan_ptr= *index_scan;
      (*scan_ptr)->index_read_cost= cost;
    }
  } 

  ha_rows records_in_scans= 0;

  for (scan_ptr=selected_index_scans, i= 0; *scan_ptr; scan_ptr++, i++)
  {
    if (create_fields_bitmap(param, &(*scan_ptr)->used_fields))
      return TRUE;
    records_in_scans+= (*scan_ptr)->records;
  }
  n_search_scans= i;

  if (cpk_scan && create_fields_bitmap(param, &cpk_scan->used_fields))
    return TRUE;
  
  if (!(common->n_search_scans= n_search_scans))
    return TRUE;
    
  common->best_uses_cpk= FALSE;
  common->best_cost= cutoff_cost + COST_EPS;
  common->best_length= 0;

  if (!(common->best_intersect=
	(INDEX_SCAN_INFO **) alloc_root (param->mem_root,
                                         sizeof(INDEX_SCAN_INFO *) *
                                         (i + MY_TEST(cpk_scan != NULL)))))
    return TRUE;

  size_t calc_cost_buff_size=
         Unique::get_cost_calc_buff_size((size_t)records_in_scans,
                                         common->key_size,
				         common->max_memory_size);
  if (!(common->buff_elems= (uint *) alloc_root(param->mem_root,
                                                calc_cost_buff_size)))
    return TRUE;

  my_qsort(selected_index_scans, n_search_scans, sizeof(INDEX_SCAN_INFO *),
           (qsort_cmp) cmp_intersect_index_scan);

  if (cpk_scan)
  {
    PARTIAL_INDEX_INTERSECT_INFO curr;
    set_field_bitmap_for_index_prefix(&cpk_scan->used_fields,
                                      cpk_scan->key_info->key_part,
                                      cpk_scan->used_key_parts);
    curr.common_info= common;
    curr.intersect_fields= &cpk_scan->used_fields;
    curr.records= cpk_scan->records;
    curr.length= 1;
    for (scan_ptr=selected_index_scans; *scan_ptr; scan_ptr++)
    {
      ha_rows scan_records= (*scan_ptr)->records;
      ha_rows records= records_in_index_intersect_extension(&curr, *scan_ptr);
      (*scan_ptr)->filtered_out= records >= scan_records ?
                                   0 : scan_records-records; 
    }
  } 
  else
  {
    for (scan_ptr=selected_index_scans; *scan_ptr; scan_ptr++)
      (*scan_ptr)->filtered_out= 0;
  }

  return FALSE;
}


/*
  On Estimation of the Number of Records in an Index Intersection 
  ===============================================================

  Consider query Q over table t. Let C be the WHERE condition of  this query,
  and, idx1(a1_1,...,a1_k1) and idx2(a2_1,...,a2_k2) be some indexes defined
  on table t.
  Let rt1 and rt2 be the range trees extracted by the range optimizer from C
  for idx1 and idx2 respectively.
  Let #t be the estimate of the number of records in table t provided for the
  optimizer. 
  Let #r1 and #r2 be the estimates of the number of records in the range trees
  rt1 and rt2, respectively, obtained by the range optimizer.

  We need to get an estimate for the number of records in the index 
  intersection of rt1 and rt2. In other words, we need to estimate the
  cardinality of the set of records that are in both trees. Let's designate
  this number by #r.

  If we do not make any assumptions then we can only state that
     #r<=MY_MIN(#r1,#r2).
  With this estimate we can't say that the index intersection scan will be 
  cheaper than the cheapest index scan.

  Let Rt1 and Rt2 be AND/OR conditions representing rt and rt2 respectively.
  The probability that a record belongs to rt1 is sel(Rt1)=#r1/#t.
  The probability that a record belongs to rt2 is sel(Rt2)=#r2/#t.

  If we assume that the values in columns of idx1 and idx2 are independent
  then #r/#t=sel(Rt1&Rt2)=sel(Rt1)*sel(Rt2)=(#r1/#t)*(#r2/#t).
  So in this case we have: #r=#r1*#r2/#t.

  The above assumption of independence of the columns in idx1 and idx2 means
  that:
  - all columns are different
  - values from one column do not correlate with values from any other column.

  We can't help with the case when column correlate with each other.
  Yet, if they are assumed to be uncorrelated the value of #r theoretically can
  be evaluated . Unfortunately this evaluation, in general, is rather complex.

  Let's consider two indexes idx1:(dept, manager),  idx2:(dept, building)
  over table 'employee' and two range conditions over these indexes:
    Rt1: dept=10 AND manager LIKE 'S%'
    Rt2: dept=10 AND building LIKE 'L%'.
  We can state that:
    sel(Rt1&Rt2)=sel(dept=10)*sel(manager LIKE 'S%')*sel(building LIKE 'L%')
    =sel(Rt1)*sel(Rt2)/sel(dept=10).
  sel(Rt1/2_0:dept=10) can be estimated if we know the cardinality #r1_0 of
  the range for sub-index idx1_0 (dept) of the index idx1 or the cardinality
  #rt2_0 of the same range for sub-index idx2_0(dept) of the index idx2.
  The current code does not make an estimate either for #rt1_0, or for #rt2_0,
  but it can be adjusted to provide those numbers.
  Alternatively, MY_MIN(rec_per_key) for (dept) could be used to get an upper 
  bound for the value of sel(Rt1&Rt2). Yet this statistics is not provided
  now.  
 
  Let's consider two other indexes idx1:(dept, last_name), 
  idx2:(first_name, last_name) and two range conditions over these indexes:
    Rt1: dept=5 AND last_name='Sm%'
    Rt2: first_name='Robert' AND last_name='Sm%'.

  sel(Rt1&Rt2)=sel(dept=5)*sel(last_name='Sm5')*sel(first_name='Robert')
  =sel(Rt2)*sel(dept=5)
  Here MY_MAX(rec_per_key) for (dept) could be used to get an upper bound for
  the value of sel(Rt1&Rt2).
  
  When the intersected indexes have different major columns, but some
  minor column are common the picture may be more complicated.

  Let's consider the following range conditions for the same indexes as in
  the previous example:
    Rt1: (Rt11: dept=5 AND last_name='So%') 
         OR 
         (Rt12: dept=7 AND last_name='Saw%')
    Rt2: (Rt21: first_name='Robert' AND last_name='Saw%')
         OR
         (Rt22: first_name='Bob' AND last_name='So%')
  Here we have:
  sel(Rt1&Rt2)= sel(Rt11)*sel(Rt21)+sel(Rt22)*sel(dept=5) +
                sel(Rt21)*sel(dept=7)+sel(Rt12)*sel(Rt22)
  Now consider the range condition:
    Rt1_0: (dept=5 OR dept=7)
  For this condition we can state that:
  sel(Rt1_0&Rt2)=(sel(dept=5)+sel(dept=7))*(sel(Rt21)+sel(Rt22))=
  sel(dept=5)*sel(Rt21)+sel(dept=7)*sel(Rt21)+
  sel(dept=5)*sel(Rt22)+sel(dept=7)*sel(Rt22)=
  sel(dept=5)*sel(Rt21)+sel(Rt21)*sel(dept=7)+
  sel(Rt22)*sel(dept=5)+sel(dept=7)*sel(Rt22) >
  sel(Rt11)*sel(Rt21)+sel(Rt22)*sel(dept=5)+
  sel(Rt21)*sel(dept=7)+sel(Rt12)*sel(Rt22) >
  sel(Rt1 & Rt2) 

 We've just demonstrated for an example what is intuitively almost obvious
 in general. We can  remove the ending parts fromrange trees getting less
 selective range conditions for sub-indexes.
 So if not a most major component with the number k of an index idx is
 encountered in the index with which we intersect we can use the sub-index
 idx_k-1 that includes the components of idx up to the i-th component and
 the range tree for idx_k-1 to make an upper bound estimate for the number
  of records in the index intersection.
 The range tree for idx_k-1 we use here is the subtree of the original range
  tree for idx that contains only parts from the first k-1 components.

  As it was mentioned above the range optimizer currently does not provide
  an estimate for the number of records in the ranges for sub-indexes.
  However, some reasonable upper bound estimate can be obtained.

  Let's consider the following range tree:
    Rt: (first_name='Robert' AND last_name='Saw%')
        OR
        (first_name='Bob' AND last_name='So%')
  Let #r be the number of records in Rt. Let f_1 be the fan-out of column
  last_name:
    f_1 = rec_per_key[first_name]/rec_per_key[last_name].
  The the number of records in the range tree:
    Rt_0:  (first_name='Robert' OR first_name='Bob')
  for the sub-index (first_name) is not greater than MY_MAX(#r*f_1, #t).
  Strictly speaking, we can state only that it's not greater than 
  MY_MAX(#r*max_f_1, #t), where
    max_f_1= max_rec_per_key[first_name]/min_rec_per_key[last_name].
  Yet, if #r/#t is big enough (and this is the case of an index intersection,
  because using this index range with a single index scan is cheaper than
  the cost of the intersection when #r/#t is small) then almost safely we
  can use here f_1 instead of max_f_1.

  The above considerations can be used in future development. Now, they are
  used partly in the function that provides a rough upper bound estimate for
  the number of records in an index intersection that follow below.
*/

/*
  Estimate the number of records selected by an extension a partial intersection

  SYNOPSIS
    records_in_index_intersect_extension()
     curr            partial intersection plan to be extended
     ext_index_scan  the evaluated extension of this partial plan

  DESCRIPTION
    The function provides an estimate for the number of records in the
    intersection of the partial index intersection curr with the index
    ext_index_scan. If all intersected indexes does not have common columns
    then  the function returns an exact estimate (assuming there are no
    correlations between values in the columns). If the intersected indexes
    have common  columns the function returns an upper bound for the number
    of records in the intersection provided that the intersection of curr
    with ext_index_scan can is expected to have less records than the expected
    number of records in the partial intersection curr. In this case the
    function also assigns the bitmap of the columns in the extended 
    intersection to ext_index_scan->used_fields.
    If the function cannot expect that the number of records in the extended
    intersection is less that the expected number of records #r in curr then
    the function returns a number bigger than #r.

  NOTES
   See the comment before the desription of the function that explains the
   reasoning used  by this function.
    
  RETURN
    The expected number of rows in the extended index intersection
*/

static
ha_rows records_in_index_intersect_extension(PARTIAL_INDEX_INTERSECT_INFO *curr,
                                             INDEX_SCAN_INFO *ext_index_scan)
{
  KEY *key_info= ext_index_scan->key_info;
  KEY_PART_INFO* key_part= key_info->key_part;
  uint used_key_parts= ext_index_scan->used_key_parts;
  MY_BITMAP *used_fields= &ext_index_scan->used_fields;
  
  if (!curr->length)
  {
    /* 
      If this the first index in the intersection just mark the
      fields in the used_fields bitmap and return the expected
      number of records in the range scan for the index provided
      by the range optimizer.
    */ 
    set_field_bitmap_for_index_prefix(used_fields, key_part, used_key_parts);
    return ext_index_scan->records;
  }

  uint i;
  bool better_selectivity= FALSE;
  ha_rows records= curr->records;
  MY_BITMAP *curr_intersect_fields= curr->intersect_fields; 
  for (i= 0; i < used_key_parts; i++, key_part++)
  {
    if (bitmap_is_set(curr_intersect_fields, key_part->fieldnr-1))
      break;
  }
  if (i)
  {
    ha_rows table_cardinality= curr->common_info->table_cardinality;
    ha_rows ext_records= ext_index_scan->records;
    if (i < used_key_parts)
    {
      double f1= key_info->actual_rec_per_key(i-1);
      double f2= key_info->actual_rec_per_key(i);
      ext_records= (ha_rows) ((double) ext_records / f2 * f1);
    }
    if (ext_records < table_cardinality)
    {
      better_selectivity= TRUE;
      records= (ha_rows) ((double) records / table_cardinality *
			  ext_records);
      bitmap_copy(used_fields, curr_intersect_fields);
      key_part= key_info->key_part;
      for (uint j= 0; j < used_key_parts; j++, key_part++)
        bitmap_set_bit(used_fields, key_part->fieldnr-1);
    }
  }
  return !better_selectivity ? records+1 :
                               !records ? 1 : records;
}


/* 
  Estimate the cost a binary search within disjoint cpk range intervals

  Number of comparisons to check whether a cpk value satisfies
  the cpk range condition = log2(cpk_scan->range_count).
*/ 

static inline
double get_cpk_filter_cost(ha_rows filtered_records, 
                           INDEX_SCAN_INFO *cpk_scan,
                           double compare_factor)
{
  return log((double) (cpk_scan->range_count+1)) / (compare_factor * M_LN2) *
           filtered_records;
}


/*
  Check whether a patial index intersection plan can be extended 

  SYNOPSIS
    check_index_intersect_extension()
     curr            partial intersection plan to be extended
     ext_index_scan  a possible extension of this plan to be checked
     next       OUT  the structure to be filled for the extended plan 

  DESCRIPTION
    The function checks whether it makes sense to extend the index
    intersection plan adding the index ext_index_scan, and, if this
    the case, the function fills in the structure for the extended plan.

  RETURN
    TRUE      if it makes sense to extend the given plan 
    FALSE     otherwise
*/

static
bool check_index_intersect_extension(PARTIAL_INDEX_INTERSECT_INFO *curr,
                                     INDEX_SCAN_INFO *ext_index_scan,
                                     PARTIAL_INDEX_INTERSECT_INFO *next)
{
  ha_rows records;
  ha_rows records_sent_to_unique;
  double cost;
  ha_rows ext_index_scan_records= ext_index_scan->records;
  ha_rows records_filtered_out_by_cpk= ext_index_scan->filtered_out;
  COMMON_INDEX_INTERSECT_INFO *common_info= curr->common_info;
  double cutoff_cost= common_info->cutoff_cost;
  uint idx= curr->length;
  next->index_read_cost= curr->index_read_cost+ext_index_scan->index_read_cost;
  if (next->index_read_cost > cutoff_cost)
    return FALSE; 

  if ((next->in_memory= curr->in_memory))
    next->in_memory_cost= curr->in_memory_cost;

  next->intersect_fields= &ext_index_scan->used_fields;
  next->filtered_scans= curr->filtered_scans;

  records_sent_to_unique= curr->records_sent_to_unique;

  next->use_cpk_filter= FALSE;

  /* Calculate the cost of using a Unique object for index intersection */
  if (idx && next->in_memory)
  { 
    /* 
      All rowids received from the first scan are expected in one unique tree
    */
    ha_rows elems_in_tree= common_info->search_scans[0]->records-
                           common_info->search_scans[0]->filtered_out ;
    next->in_memory_cost+= Unique::get_search_cost(elems_in_tree,
                                                   common_info->compare_factor)* 
                             ext_index_scan_records;
    cost= next->in_memory_cost;
  }
  else
  {
    uint *buff_elems= common_info->buff_elems;
    uint key_size= common_info->key_size;
    uint compare_factor= common_info->compare_factor;         
    ulonglong max_memory_size= common_info->max_memory_size; 
    
    records_sent_to_unique+= ext_index_scan_records;
    cost= Unique::get_use_cost(buff_elems, (size_t) records_sent_to_unique, key_size,
                               max_memory_size, compare_factor, TRUE,
                               &next->in_memory);
    if (records_filtered_out_by_cpk)
    {
      /* Check whether using cpk filter for this scan is beneficial */

      double cost2;
      bool in_memory2;
      ha_rows records2= records_sent_to_unique-records_filtered_out_by_cpk;
      cost2=  Unique::get_use_cost(buff_elems, (size_t) records2, key_size,
                                   max_memory_size, compare_factor, TRUE,
                                   &in_memory2);
      cost2+= get_cpk_filter_cost(ext_index_scan_records, common_info->cpk_scan,
                                  compare_factor);
      if (cost > cost2 + COST_EPS)
      {
        cost= cost2;
        next->in_memory= in_memory2;
        next->use_cpk_filter= TRUE;
        records_sent_to_unique= records2;
      }

    }   
    if (next->in_memory)
      next->in_memory_cost= cost;
  }

  if (next->use_cpk_filter)
  {
    next->filtered_scans.set_bit(ext_index_scan->keynr);
    bitmap_union(&ext_index_scan->used_fields,
                 &common_info->cpk_scan->used_fields);
  }
  next->records_sent_to_unique= records_sent_to_unique;
       
  records= records_in_index_intersect_extension(curr, ext_index_scan);
  if (idx && records > curr->records)
    return FALSE;
  if (next->use_cpk_filter && curr->filtered_scans.is_clear_all())
    records-= records_filtered_out_by_cpk;
  next->records= records;

  cost+= next->index_read_cost;
  if (cost >= cutoff_cost)
    return FALSE;

  cost+= get_sweep_read_cost(common_info->param, records);

  next->cost= cost;
  next->length= curr->length+1;

  return TRUE;
}


/*
  Search for the cheapest extensions of range scans used to access a table    

  SYNOPSIS
    find_index_intersect_best_extension()
      curr        partial intersection to evaluate all possible extension for 

  DESCRIPTION
    The function tries to extend the partial plan curr in all possible ways
    to look for a cheapest index intersection whose cost less than the 
    cut off value set in curr->common_info.cutoff_cost. 
*/

static 
void find_index_intersect_best_extension(PARTIAL_INDEX_INTERSECT_INFO *curr)
{
  PARTIAL_INDEX_INTERSECT_INFO next;
  COMMON_INDEX_INTERSECT_INFO *common_info= curr->common_info;
  INDEX_SCAN_INFO **index_scans= common_info->search_scans;
  uint idx= curr->length;
  INDEX_SCAN_INFO **rem_first_index_scan_ptr= &index_scans[idx];
  double cost= curr->cost;

  if (cost + COST_EPS < common_info->best_cost)
  {
    common_info->best_cost= cost;
    common_info->best_length= curr->length;
    common_info->best_records= curr->records;
    common_info->filtered_scans= curr->filtered_scans;
    /* common_info->best_uses_cpk <=> at least one scan uses a cpk filter */
    common_info->best_uses_cpk= !curr->filtered_scans.is_clear_all();
    uint sz= sizeof(INDEX_SCAN_INFO *) * curr->length;
    memcpy(common_info->best_intersect, common_info->search_scans, sz);
    common_info->cutoff_cost= cost;
  }   

  if (!(*rem_first_index_scan_ptr))
    return;  

  next.common_info= common_info;
 
  INDEX_SCAN_INFO *rem_first_index_scan= *rem_first_index_scan_ptr;
  for (INDEX_SCAN_INFO **index_scan_ptr= rem_first_index_scan_ptr;
       *index_scan_ptr; index_scan_ptr++)
  {
    *rem_first_index_scan_ptr= *index_scan_ptr;
    *index_scan_ptr= rem_first_index_scan;
    if (check_index_intersect_extension(curr, *rem_first_index_scan_ptr, &next))
      find_index_intersect_best_extension(&next);
    *index_scan_ptr= *rem_first_index_scan_ptr;
    *rem_first_index_scan_ptr= rem_first_index_scan;
  }
}


/*
  Get the plan of the best intersection of range scans used to access a table    

  SYNOPSIS
    get_best_index_intersect()
      param         common info about index ranges
      tree          tree of ranges for indexes than can be intersected
      read_time     cut off value for the evaluated plans 

  DESCRIPTION
    The function looks for the cheapest index intersection of the range
    scans to access a table. The info about the ranges for all indexes
    is provided by the range optimizer and is passed through the
    parameters param and tree. Any plan whose cost is greater than read_time
    is rejected. 
    After the best index intersection is found the function constructs
    the structure that manages the execution by the chosen plan.

  RETURN
    Pointer to the generated execution structure if a success,
    0 - otherwise.
*/

static
TRP_INDEX_INTERSECT *get_best_index_intersect(PARAM *param, SEL_TREE *tree,
                                              double read_time)
{
  uint i;
  uint count;
  TRP_RANGE **cur_range;
  TRP_RANGE **range_scans;
  INDEX_SCAN_INFO *index_scan;
  COMMON_INDEX_INTERSECT_INFO common;
  PARTIAL_INDEX_INTERSECT_INFO init;
  TRP_INDEX_INTERSECT *intersect_trp= NULL;
  TABLE *table= param->table;
  
  
  DBUG_ENTER("get_best_index_intersect");

  if (prepare_search_best_index_intersect(param, tree, &common, &init,
                                          read_time))
    DBUG_RETURN(NULL);

  find_index_intersect_best_extension(&init);

  if (common.best_length <= 1 && !common.best_uses_cpk)
    DBUG_RETURN(NULL);

  if (common.best_uses_cpk)
  {
    memmove((char *) (common.best_intersect+1), (char *) common.best_intersect,
            sizeof(INDEX_SCAN_INFO *) * common.best_length);
    common.best_intersect[0]= common.cpk_scan;
    common.best_length++;
  }

  count= common.best_length;

  if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
                                            sizeof(TRP_RANGE *)*
                                            count)))
    DBUG_RETURN(NULL);

  for (i= 0, cur_range= range_scans; i < count; i++)
  {
    index_scan= common.best_intersect[i];
    if ((*cur_range= new (param->mem_root) TRP_RANGE(index_scan->sel_arg,
                                                     index_scan->idx, 0)))
    {  
      TRP_RANGE *trp= *cur_range;  
      trp->read_cost= index_scan->index_read_cost;  
      trp->records= index_scan->records;        
      trp->is_ror= FALSE;
      trp->mrr_buf_size= 0;
      table->intersect_keys.set_bit(index_scan->keynr);
      cur_range++;
    }
  }
  
  count= tree->index_scans_end - tree->index_scans;
  for (i= 0; i < count; i++)
  {
    index_scan= tree->index_scans[i]; 
    if (!table->intersect_keys.is_set(index_scan->keynr))
    {
      for (uint j= 0; j < common.best_length; j++)
      {
	INDEX_SCAN_INFO *scan= common.best_intersect[j];
        if (same_index_prefix(index_scan->key_info, scan->key_info,
                              scan->used_key_parts))
	{
          table->intersect_keys.set_bit(index_scan->keynr);
          break;
        } 
      }
    }
  }
      
  if ((intersect_trp= new (param->mem_root)TRP_INDEX_INTERSECT))
  {
    intersect_trp->read_cost= common.best_cost;
    intersect_trp->records= common.best_records;
    intersect_trp->range_scans= range_scans;
    intersect_trp->range_scans_end= cur_range;
    intersect_trp->filtered_scans= common.filtered_scans;
  }
  DBUG_RETURN(intersect_trp);
}


typedef struct st_ror_scan_info : INDEX_SCAN_INFO
{ 
} ROR_SCAN_INFO;


/*
  Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
  sel_arg set of intervals.

  SYNOPSIS
    make_ror_scan()
      param    Parameter from test_quick_select function
      idx      Index of key in param->keys
      sel_arg  Set of intervals for a given key

  RETURN
    NULL - out of memory
    ROR scan structure containing a scan for {idx, sel_arg}
*/

static
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
{
  ROR_SCAN_INFO *ror_scan;
  my_bitmap_map *bitmap_buf;
  uint keynr;
  DBUG_ENTER("make_ror_scan");

  if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
                                             sizeof(ROR_SCAN_INFO))))
    DBUG_RETURN(NULL);

  ror_scan->idx= idx;
  ror_scan->keynr= keynr= param->real_keynr[idx];
  ror_scan->key_rec_length= (param->table->key_info[keynr].key_length +
                             param->table->file->ref_length);
  ror_scan->sel_arg= sel_arg;
  ror_scan->records= param->quick_rows[keynr];

  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
                                                param->fields_bitmap_size)))
    DBUG_RETURN(NULL);

  if (my_bitmap_init(&ror_scan->covered_fields, bitmap_buf,
                  param->table->s->fields, FALSE))
    DBUG_RETURN(NULL);
  bitmap_clear_all(&ror_scan->covered_fields);

  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
  KEY_PART_INFO *key_part_end= key_part +
                               param->table->key_info[keynr].user_defined_key_parts;
  for (;key_part != key_part_end; ++key_part)
  {
    if (bitmap_is_set(&param->needed_fields, key_part->fieldnr-1))
      bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
  }
  ror_scan->index_read_cost=
    param->table->file->keyread_time(ror_scan->keynr, 1, ror_scan->records);
  DBUG_RETURN(ror_scan);
}


/*
  Compare two ROR_SCAN_INFO** by  E(#records_matched) * key_record_length.
  SYNOPSIS
    cmp_ror_scan_info()
      a ptr to first compared value
      b ptr to second compared value

  RETURN
   -1 a < b
    0 a = b
    1 a > b
*/

static int cmp_ror_scan_info(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
{
  double val1= rows2double((*a)->records) * (*a)->key_rec_length;
  double val2= rows2double((*b)->records) * (*b)->key_rec_length;
  return (val1 < val2)? -1: (val1 == val2)? 0 : 1;
}

/*
  Compare two ROR_SCAN_INFO** by
   (#covered fields in F desc,
    #components asc,
    number of first not covered component asc)

  SYNOPSIS
    cmp_ror_scan_info_covering()
      a ptr to first compared value
      b ptr to second compared value

  RETURN
   -1 a < b
    0 a = b
    1 a > b
*/

static int cmp_ror_scan_info_covering(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
{
  if ((*a)->used_fields_covered > (*b)->used_fields_covered)
    return -1;
  if ((*a)->used_fields_covered < (*b)->used_fields_covered)
    return 1;
  if ((*a)->key_components < (*b)->key_components)
    return -1;
  if ((*a)->key_components > (*b)->key_components)
    return 1;
  if ((*a)->first_uncovered_field < (*b)->first_uncovered_field)
    return -1;
  if ((*a)->first_uncovered_field > (*b)->first_uncovered_field)
    return 1;
  return 0;
}


/* Auxiliary structure for incremental ROR-intersection creation */
typedef struct
{
  const PARAM *param;
  MY_BITMAP covered_fields; /* union of fields covered by all scans */
  /*
    Fraction of table records that satisfies conditions of all scans.
    This is the number of full records that will be retrieved if a
    non-index_only index intersection will be employed.
  */
  double out_rows;
  /* TRUE if covered_fields is a superset of needed_fields */
  bool is_covering;

  ha_rows index_records; /* sum(#records to look in indexes) */
  double index_scan_costs; /* SUM(cost of 'index-only' scans) */
  double total_cost;
} ROR_INTERSECT_INFO;


/*
  Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.

  SYNOPSIS
    ror_intersect_init()
      param         Parameter from test_quick_select

  RETURN
    allocated structure
    NULL on error
*/

static
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
{
  ROR_INTERSECT_INFO *info;
  my_bitmap_map* buf;
  if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
                                              sizeof(ROR_INTERSECT_INFO))))
    return NULL;
  info->param= param;
  if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
                                         param->fields_bitmap_size)))
    return NULL;
  if (my_bitmap_init(&info->covered_fields, buf, param->table->s->fields,
                  FALSE))
    return NULL;
  info->is_covering= FALSE;
  info->index_scan_costs= 0.0;
  info->index_records= 0;
  info->out_rows= (double) param->table->stat_records();
  bitmap_clear_all(&info->covered_fields);
  return info;
}

void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
{
  dst->param= src->param;
  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap, 
         no_bytes_in_map(&src->covered_fields));
  dst->out_rows= src->out_rows;
  dst->is_covering= src->is_covering;
  dst->index_records= src->index_records;
  dst->index_scan_costs= src->index_scan_costs;
  dst->total_cost= src->total_cost;
}


/*
  Get selectivity of a ROR scan wrt ROR-intersection.

  SYNOPSIS
    ror_scan_selectivity()
      info  ROR-interection 
      scan  ROR scan
      
  NOTES
    Suppose we have a condition on several keys
    cond=k_11=c_11 AND k_12=c_12 AND ...  // parts of first key
         k_21=c_21 AND k_22=c_22 AND ...  // parts of second key
          ...
         k_n1=c_n1 AND k_n3=c_n3 AND ...  (1) //parts of the key used by *scan

    where k_ij may be the same as any k_pq (i.e. keys may have common parts).

    A full row is retrieved if entire condition holds.

    The recursive procedure for finding P(cond) is as follows:

    First step:
    Pick 1st part of 1st key and break conjunction (1) into two parts:
      cond= (k_11=c_11 AND R)

    Here R may still contain condition(s) equivalent to k_11=c_11.
    Nevertheless, the following holds:

      P(k_11=c_11 AND R) = P(k_11=c_11) * P(R | k_11=c_11).

    Mark k_11 as fixed field (and satisfied condition) F, save P(F),
    save R to be cond and proceed to recursion step.

    Recursion step:
    We have a set of fixed fields/satisfied conditions) F, probability P(F),
    and remaining conjunction R
    Pick next key part on current key and its condition "k_ij=c_ij".
    We will add "k_ij=c_ij" into F and update P(F).
    Lets denote k_ij as t,  R = t AND R1, where R1 may still contain t. Then

     P((t AND R1)|F) = P(t|F) * P(R1|t|F) = P(t|F) * P(R1|(t AND F)) (2)

    (where '|' mean conditional probability, not "or")

    Consider the first multiplier in (2). One of the following holds:
    a) F contains condition on field used in t (i.e. t AND F = F).
      Then P(t|F) = 1

    b) F doesn't contain condition on field used in t. Then F and t are
     considered independent.

     P(t|F) = P(t|(fields_before_t_in_key AND other_fields)) =
          = P(t|fields_before_t_in_key).

     P(t|fields_before_t_in_key) = #records(fields_before_t_in_key) /
                                   #records(fields_before_t_in_key, t)

    The second multiplier is calculated by applying this step recursively.

  IMPLEMENTATION
    This function calculates the result of application of the "recursion step"
    described above for all fixed key members of a single key, accumulating set
    of covered fields, selectivity, etc.

    The calculation is conducted as follows:
    Lets denote #records(keypart1, ... keypartK) as n_k. We need to calculate

     n_{k1}      n_{k2}
    --------- * ---------  * .... (3)
     n_{k1-1}    n_{k2-1}

    where k1,k2,... are key parts which fields were not yet marked as fixed
    ( this is result of application of option b) of the recursion step for
      parts of a single key).
    Since it is reasonable to expect that most of the fields are not marked
    as fixed, we calculate (3) as

                                  n_{i1}      n_{i2}
    (3) = n_{max_key_part}  / (   --------- * ---------  * ....  )
                                  n_{i1-1}    n_{i2-1}

    where i1,i2, .. are key parts that were already marked as fixed.

    In order to minimize number of expensive records_in_range calls we group
    and reduce adjacent fractions.

  RETURN
    Selectivity of given ROR scan.
*/

static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info, 
                                   const ROR_SCAN_INFO *scan)
{
  double selectivity_mult= 1.0;
  KEY_PART_INFO *key_part= info->param->table->key_info[scan->keynr].key_part;
  uchar key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
  uchar *key_ptr= key_val;
  SEL_ARG *sel_arg, *tuple_arg= NULL;
  key_part_map keypart_map= 0;
  bool cur_covered;
  bool prev_covered= MY_TEST(bitmap_is_set(&info->covered_fields,
                                           key_part->fieldnr - 1));
  key_range min_range;
  key_range max_range;
  min_range.key= key_val;
  min_range.flag= HA_READ_KEY_EXACT;
  max_range.key= key_val;
  max_range.flag= HA_READ_AFTER_KEY;
  ha_rows prev_records= info->param->table->stat_records();
  DBUG_ENTER("ror_scan_selectivity");

  for (sel_arg= scan->sel_arg; sel_arg;
       sel_arg= sel_arg->next_key_part)
  {
    DBUG_PRINT("info",("sel_arg step"));
    cur_covered= MY_TEST(bitmap_is_set(&info->covered_fields,
                                       key_part[sel_arg->part].fieldnr - 1));
    if (cur_covered != prev_covered)
    {
      /* create (part1val, ..., part{n-1}val) tuple. */
      ha_rows records;
      if (!tuple_arg)
      {
        tuple_arg= scan->sel_arg;
        /* Here we use the length of the first key part */
        tuple_arg->store_min(key_part->store_length, &key_ptr, 0);
        keypart_map= 1;
      }
      while (tuple_arg->next_key_part != sel_arg)
      {
        tuple_arg= tuple_arg->next_key_part;
        tuple_arg->store_min(key_part[tuple_arg->part].store_length,
                             &key_ptr, 0);
        keypart_map= (keypart_map << 1) | 1;
      }
      min_range.length= max_range.length= (size_t) (key_ptr - key_val);
      min_range.keypart_map= max_range.keypart_map= keypart_map;
      records= (info->param->table->file->
                records_in_range(scan->keynr, &min_range, &max_range));
      if (cur_covered)
      {
        /* uncovered -> covered */
        double tmp= rows2double(records)/rows2double(prev_records);
        DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
        selectivity_mult *= tmp;
        prev_records= HA_POS_ERROR;
      }
      else
      {
        /* covered -> uncovered */
        prev_records= records;
      }
    }
    prev_covered= cur_covered;
  }
  if (!prev_covered)
  {
    double tmp= rows2double(info->param->quick_rows[scan->keynr]) /
                rows2double(prev_records);
    DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
    selectivity_mult *= tmp;
  }
  DBUG_PRINT("info", ("Returning multiplier: %g", selectivity_mult));
  DBUG_RETURN(selectivity_mult);
}


/*
  Check if adding a ROR scan to a ROR-intersection reduces its cost of
  ROR-intersection and if yes, update parameters of ROR-intersection,
  including its cost.

  SYNOPSIS
    ror_intersect_add()
      param        Parameter from test_quick_select
      info         ROR-intersection structure to add the scan to.
      ror_scan     ROR scan info to add.
      is_cpk_scan  If TRUE, add the scan as CPK scan (this can be inferred
                   from other parameters and is passed separately only to
                   avoid duplicating the inference code)

  NOTES
    Adding a ROR scan to ROR-intersect "makes sense" iff the cost of ROR-
    intersection decreases. The cost of ROR-intersection is calculated as
    follows:

    cost= SUM_i(key_scan_cost_i) + cost_of_full_rows_retrieval

    When we add a scan the first increases and the second decreases.

    cost_of_full_rows_retrieval=
      (union of indexes used covers all needed fields) ?
        cost_of_sweep_read(E(rows_to_retrieve), rows_in_table) :
        0

    E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
                           ror_scan_selectivity({scan1}, scan2) * ... *
                           ror_scan_selectivity({scan1,...}, scanN). 
  RETURN
    TRUE   ROR scan added to ROR-intersection, cost updated.
    FALSE  It doesn't make sense to add this ROR scan to this ROR-intersection.
*/

static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
                              ROR_SCAN_INFO* ror_scan, bool is_cpk_scan)
{
  double selectivity_mult= 1.0;

  DBUG_ENTER("ror_intersect_add");
  DBUG_PRINT("info", ("Current out_rows= %g", info->out_rows));
  DBUG_PRINT("info", ("Adding scan on %s",
                      info->param->table->key_info[ror_scan->keynr].name));
  DBUG_PRINT("info", ("is_cpk_scan: %d",is_cpk_scan));

  selectivity_mult = ror_scan_selectivity(info, ror_scan);
  if (selectivity_mult == 1.0)
  {
    /* Don't add this scan if it doesn't improve selectivity. */
    DBUG_PRINT("info", ("The scan doesn't improve selectivity."));
    DBUG_RETURN(FALSE);
  }
  
  info->out_rows *= selectivity_mult;
  
  if (is_cpk_scan)
  {
    /*
      CPK scan is used to filter out rows. We apply filtering for 
      each record of every scan. Assuming 1/TIME_FOR_COMPARE_ROWID
      per check this gives us:
    */
    info->index_scan_costs += rows2double(info->index_records) / 
                              TIME_FOR_COMPARE_ROWID;
  }
  else
  {
    info->index_records += info->param->quick_rows[ror_scan->keynr];
    info->index_scan_costs += ror_scan->index_read_cost;
    bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
    if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
                                               &info->covered_fields))
    {
      DBUG_PRINT("info", ("ROR-intersect is covering now"));
      info->is_covering= TRUE;
    }
  }

  info->total_cost= info->index_scan_costs;
  DBUG_PRINT("info", ("info->total_cost: %g", info->total_cost));
  if (!info->is_covering)
  {
    info->total_cost += 
      get_sweep_read_cost(info->param, double2rows(info->out_rows));
    DBUG_PRINT("info", ("info->total_cost= %g", info->total_cost));
  }
  DBUG_PRINT("info", ("New out_rows: %g", info->out_rows));
  DBUG_PRINT("info", ("New cost: %g, %scovering", info->total_cost,
                      info->is_covering?"" : "non-"));
  DBUG_RETURN(TRUE);
}


/*
  Get best ROR-intersection plan using non-covering ROR-intersection search
  algorithm. The returned plan may be covering.

  SYNOPSIS
    get_best_ror_intersect()
      param            Parameter from test_quick_select function.
      tree             Transformed restriction condition to be used to look
                       for ROR scans.
      read_time        Do not return read plans with cost > read_time.
      are_all_covering [out] set to TRUE if union of all scans covers all
                       fields needed by the query (and it is possible to build
                       a covering ROR-intersection)

  NOTES
    get_key_scans_params must be called before this function can be called.
    
    When this function is called by ROR-union construction algorithm it
    assumes it is building an uncovered ROR-intersection (and thus # of full
    records to be retrieved is wrong here). This is a hack.

  IMPLEMENTATION
    The approximate best non-covering plan search algorithm is as follows:

    find_min_ror_intersection_scan()
    {
      R= select all ROR scans;
      order R by (E(#records_matched) * key_record_length).

      S= first(R); -- set of scans that will be used for ROR-intersection
      R= R-first(S);
      min_cost= cost(S);
      min_scan= make_scan(S);
      while (R is not empty)
      {
        firstR= R - first(R);
        if (!selectivity(S + firstR < selectivity(S)))
          continue;
          
        S= S + first(R);
        if (cost(S) < min_cost)
        {
          min_cost= cost(S);
          min_scan= make_scan(S);
        }
      }
      return min_scan;
    }

    See ror_intersect_add function for ROR intersection costs.

    Special handling for Clustered PK scans
    Clustered PK contains all table fields, so using it as a regular scan in
    index intersection doesn't make sense: a range scan on CPK will be less
    expensive in this case.
    Clustered PK scan has special handling in ROR-intersection: it is not used
    to retrieve rows, instead its condition is used to filter row references
    we get from scans on other keys.

  RETURN
    ROR-intersection table read plan
    NULL if out of memory or no suitable plan found.
*/

static
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
                                          double read_time,
                                          bool *are_all_covering)
{
  uint idx;
  double min_cost= DBL_MAX;
  DBUG_ENTER("get_best_ror_intersect");

  if ((tree->n_ror_scans < 2) || !param->table->stat_records() ||
      !optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT))
    DBUG_RETURN(NULL);

  /*
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of 
    them. Also find and save clustered PK scan if there is one.
  */
  ROR_SCAN_INFO **cur_ror_scan;
  ROR_SCAN_INFO *cpk_scan= NULL;
  uint cpk_no;

  if (!(tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     param->keys)))
    return NULL;
  cpk_no= ((param->table->file->primary_key_is_clustered()) ?
           param->table->s->primary_key : MAX_KEY);

  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
  {
    ROR_SCAN_INFO *scan;
    uint key_no;
    if (!tree->ror_scans_map.is_set(idx))
      continue;
    key_no= param->real_keynr[idx];
    if (key_no != cpk_no &&
        param->table->file->index_flags(key_no,0,0) & HA_CLUSTERED_INDEX)
    {
      /* Ignore clustering keys */
      tree->n_ror_scans--;
      continue;
    }
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
      return NULL;
    if (key_no == cpk_no)
    {
      cpk_scan= scan;
      tree->n_ror_scans--;
    }
    else
      *(cur_ror_scan++)= scan;
  }

  tree->ror_scans_end= cur_ror_scan;
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "original",
                                          tree->ror_scans,
                                          tree->ror_scans_end););
  /*
    Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
    ROR_SCAN_INFO's.
    Step 2: Get best ROR-intersection using an approximate algorithm.
  */
  my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
           (qsort_cmp)cmp_ror_scan_info);
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "ordered",
                                          tree->ror_scans,
                                          tree->ror_scans_end););

  ROR_SCAN_INFO **intersect_scans; /* ROR scans used in index intersection */
  ROR_SCAN_INFO **intersect_scans_end;
  if (!(intersect_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     tree->n_ror_scans)))
    return NULL;
  intersect_scans_end= intersect_scans;

  /* Create and incrementally update ROR intersection. */
  ROR_INTERSECT_INFO *intersect, *intersect_best;
  if (!(intersect= ror_intersect_init(param)) || 
      !(intersect_best= ror_intersect_init(param)))
    return NULL;

  /* [intersect_scans,intersect_scans_best) will hold the best intersection */
  ROR_SCAN_INFO **intersect_scans_best;
  cur_ror_scan= tree->ror_scans;
  intersect_scans_best= intersect_scans;
  while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
  {
    /* S= S + first(R);  R= R - first(R); */
    if (!ror_intersect_add(intersect, *cur_ror_scan, FALSE))
    {
      cur_ror_scan++;
      continue;
    }
    
    *(intersect_scans_end++)= *(cur_ror_scan++);

    if (intersect->total_cost < min_cost)
    {
      /* Local minimum found, save it */
      ror_intersect_cpy(intersect_best, intersect);
      intersect_scans_best= intersect_scans_end;
      min_cost = intersect->total_cost;
    }
  }

  if (intersect_scans_best == intersect_scans)
  {
    DBUG_PRINT("info", ("None of scans increase selectivity"));
    DBUG_RETURN(NULL);
  }
    
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table,
                                          "best ROR-intersection",
                                          intersect_scans,
                                          intersect_scans_best););

  *are_all_covering= intersect->is_covering;
  uint best_num= intersect_scans_best - intersect_scans;
  ror_intersect_cpy(intersect, intersect_best);

  /*
    Ok, found the best ROR-intersection of non-CPK key scans.
    Check if we should add a CPK scan. If the obtained ROR-intersection is 
    covering, it doesn't make sense to add CPK scan.
  */
  if (cpk_scan && !intersect->is_covering)
  {
    if (ror_intersect_add(intersect, cpk_scan, TRUE) && 
        (intersect->total_cost < min_cost))
      intersect_best= intersect; //just set pointer here
  }
  else
    cpk_scan= 0;                                // Don't use cpk_scan

  /* Ok, return ROR-intersect plan if we have found one */
  TRP_ROR_INTERSECT *trp= NULL;
  if (min_cost < read_time && (cpk_scan || best_num > 1))
  {
    if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
      DBUG_RETURN(trp);
    if (!(trp->first_scan=
           (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                       sizeof(ROR_SCAN_INFO*)*best_num)))
      DBUG_RETURN(NULL);
    memcpy(trp->first_scan, intersect_scans, best_num*sizeof(ROR_SCAN_INFO*));
    trp->last_scan=  trp->first_scan + best_num;
    trp->is_covering= intersect_best->is_covering;
    trp->read_cost= intersect_best->total_cost;
    /* Prevent divisons by zero */
    ha_rows best_rows = double2rows(intersect_best->out_rows);
    if (!best_rows)
      best_rows= 1;
    set_if_smaller(param->table->quick_condition_rows, best_rows);
    trp->records= best_rows;
    trp->index_scan_costs= intersect_best->index_scan_costs;
    trp->cpk_scan= cpk_scan;
    DBUG_PRINT("info", ("Returning non-covering ROR-intersect plan:"
                        "cost %g, records %lu",
                        trp->read_cost, (ulong) trp->records));
  }
  DBUG_RETURN(trp);
}


/*
  Get best covering ROR-intersection.
  SYNOPSIS
    get_best_ntersectcovering_ror_intersect()
      param     Parameter from test_quick_select function.
      tree      SEL_TREE with sets of intervals for different keys.
      read_time Don't return table read plans with cost > read_time.

  RETURN
    Best covering ROR-intersection plan
    NULL if no plan found.

  NOTES
    get_best_ror_intersect must be called for a tree before calling this
    function for it.
    This function invalidates tree->ror_scans member values.

  The following approximate algorithm is used:
    I=set of all covering indexes
    F=set of all fields to cover
    S={}

    do
    {
      Order I by (#covered fields in F desc,
                  #components asc,
                  number of first not covered component asc);
      F=F-covered by first(I);
      S=S+first(I);
      I=I-first(I);
    } while F is not empty.
*/

static
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
                                                   SEL_TREE *tree,
                                                   double read_time)
{
  ROR_SCAN_INFO **ror_scan_mark;
  ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
  DBUG_ENTER("get_best_covering_ror_intersect");

  if (!optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT))
    DBUG_RETURN(NULL);

  for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
    (*scan)->key_components=
      param->table->key_info[(*scan)->keynr].user_defined_key_parts;

  /*
    Run covering-ROR-search algorithm.
    Assume set I is [ror_scan .. ror_scans_end)
  */

  /*I=set of all covering indexes */
  ror_scan_mark= tree->ror_scans;

  MY_BITMAP *covered_fields= &param->tmp_covered_fields;
  if (!covered_fields->bitmap) 
    covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
                                               param->fields_bitmap_size);
  if (!covered_fields->bitmap ||
      my_bitmap_init(covered_fields, covered_fields->bitmap,
                  param->table->s->fields, FALSE))
    DBUG_RETURN(0);
  bitmap_clear_all(covered_fields);

  double total_cost= 0.0f;
  ha_rows records=0;
  bool all_covered;

  DBUG_PRINT("info", ("Building covering ROR-intersection"));
  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                           "building covering ROR-I",
                                           ror_scan_mark, ror_scans_end););
  do
  {
    /*
      Update changed sorting info:
        #covered fields,
	number of first not covered component
      Calculate and save these values for each of remaining scans.
    */
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
    {
      bitmap_subtract(&(*scan)->covered_fields, covered_fields);
      (*scan)->used_fields_covered=
        bitmap_bits_set(&(*scan)->covered_fields);
      (*scan)->first_uncovered_field=
        bitmap_get_first(&(*scan)->covered_fields);
    }

    my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
             (qsort_cmp)cmp_ror_scan_info_covering);

    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                             "remaining scans",
                                             ror_scan_mark, ror_scans_end););

    /* I=I-first(I) */
    total_cost += (*ror_scan_mark)->index_read_cost;
    records += (*ror_scan_mark)->records;
    DBUG_PRINT("info", ("Adding scan on %s",
                        param->table->key_info[(*ror_scan_mark)->keynr].name));
    if (total_cost > read_time)
      DBUG_RETURN(NULL);
    /* F=F-covered by first(I) */
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
  
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
    DBUG_RETURN(NULL);

  /*
    Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
    cost total_cost.
  */
  DBUG_PRINT("info", ("Covering ROR-intersect scans cost: %g", total_cost));
  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                           "creating covering ROR-intersect",
                                           tree->ror_scans, ror_scan_mark););

  /* Add priority queue use cost. */
  total_cost += rows2double(records)*
                log((double)(ror_scan_mark - tree->ror_scans)) /
                (TIME_FOR_COMPARE_ROWID * M_LN2);
  DBUG_PRINT("info", ("Covering ROR-intersect full cost: %g", total_cost));

  if (total_cost > read_time)
    DBUG_RETURN(NULL);

  TRP_ROR_INTERSECT *trp;
  if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
    DBUG_RETURN(trp);
  uint best_num= (ror_scan_mark - tree->ror_scans);
  if (!(trp->first_scan= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     best_num)))
    DBUG_RETURN(NULL);
  memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(ROR_SCAN_INFO*));
  trp->last_scan=  trp->first_scan + best_num;
  trp->is_covering= TRUE;
  trp->read_cost= total_cost;
  trp->records= records;
  trp->cpk_scan= NULL;
  set_if_smaller(param->table->quick_condition_rows, records); 

  DBUG_PRINT("info",
             ("Returning covering ROR-intersect plan: cost %g, records %lu",
              trp->read_cost, (ulong) trp->records));
  DBUG_RETURN(trp);
}


/*
  Get best "range" table read plan for given SEL_TREE.
  Also update PARAM members and store ROR scans info in the SEL_TREE.
  SYNOPSIS
    get_key_scans_params
      param        parameters from test_quick_select
      tree         make range select for this SEL_TREE
      index_read_must_be_used if TRUE, assume 'index only' option will be set
                             (except for clustered PK indexes)
      read_time    don't create read plans with cost > read_time.
  RETURN
    Best range read plan
    NULL if no plan found or error occurred
*/

static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
                                       bool index_read_must_be_used, 
                                       bool update_tbl_stats,
                                       double read_time)
{
  uint idx, best_idx;
  SEL_ARG *key_to_read= NULL;
  ha_rows UNINIT_VAR(best_records);              /* protected by key_to_read */
  uint    UNINIT_VAR(best_mrr_flags),            /* protected by key_to_read */
          UNINIT_VAR(best_buf_size);             /* protected by key_to_read */
  TRP_RANGE* read_plan= NULL;
  DBUG_ENTER("get_key_scans_params");
  /*
    Note that there may be trees that have type SEL_TREE::KEY but contain no
    key reads at all, e.g. tree for expression "key1 is not null" where key1
    is defined as "not null".
  */
  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->keys_map,
                                      "tree scans"););
  tree->ror_scans_map.clear_all();
  tree->n_ror_scans= 0;
  tree->index_scans= 0;
  if (!tree->keys_map.is_clear_all())
  {
    tree->index_scans=
      (INDEX_SCAN_INFO **) alloc_root(param->mem_root,
                                      sizeof(INDEX_SCAN_INFO *) * param->keys);
  }
  tree->index_scans_end= tree->index_scans;                                                  
  for (idx= 0; idx < param->keys; idx++)
  {
    SEL_ARG *key= tree->keys[idx];
    if (key)
    {
      ha_rows found_records;
      Cost_estimate cost;
      double found_read_time;
      uint mrr_flags, buf_size;
      INDEX_SCAN_INFO *index_scan;
      uint keynr= param->real_keynr[idx];
      if (key->type == SEL_ARG::MAYBE_KEY ||
          key->maybe_flag)
        param->needed_reg->set_bit(keynr);

      bool read_index_only= index_read_must_be_used ? TRUE :
                            (bool) param->table->covering_keys.is_set(keynr);

      found_records= check_quick_select(param, idx, read_index_only, key,
                                        update_tbl_stats, &mrr_flags,
                                        &buf_size, &cost);

      if (found_records != HA_POS_ERROR && tree->index_scans &&
          (index_scan= (INDEX_SCAN_INFO *)alloc_root(param->mem_root,
						     sizeof(INDEX_SCAN_INFO))))
      {
        index_scan->idx= idx;
        index_scan->keynr= keynr;
        index_scan->key_info= &param->table->key_info[keynr];
        index_scan->used_key_parts= param->max_key_part+1;
        index_scan->range_count= param->range_count;
        index_scan->records= found_records;
        index_scan->sel_arg= key;
        *tree->index_scans_end++= index_scan;
      }        
      if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
      {
        tree->n_ror_scans++;
        tree->ror_scans_map.set_bit(idx);
      }
      if (found_records != HA_POS_ERROR &&
          read_time > (found_read_time= cost.total_cost()))
      {
        read_time=    found_read_time;
        best_records= found_records;
        key_to_read=  key;
        best_idx= idx;
        best_mrr_flags= mrr_flags;
        best_buf_size=  buf_size;
      }
    }
  }

  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->ror_scans_map,
                                      "ROR scans"););
  if (key_to_read)
  {
    if ((read_plan= new (param->mem_root) TRP_RANGE(key_to_read, best_idx,
                                                    best_mrr_flags)))
    {
      read_plan->records= best_records;
      read_plan->is_ror= tree->ror_scans_map.is_set(best_idx);
      read_plan->read_cost= read_time;
      read_plan->mrr_buf_size= best_buf_size;
      DBUG_PRINT("info",
                 ("Returning range plan for key %s, cost %g, records %lu",
                  param->table->key_info[param->real_keynr[best_idx]].name,
                  read_plan->read_cost, (ulong) read_plan->records));
    }
  }
  else
    DBUG_PRINT("info", ("No 'range' table read plan found"));

  DBUG_RETURN(read_plan);
}


QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
                                            bool retrieve_full_rows,
                                            MEM_ROOT *parent_alloc)
{
  QUICK_INDEX_MERGE_SELECT *quick_imerge;
  QUICK_RANGE_SELECT *quick;
  /* index_merge always retrieves full rows, ignore retrieve_full_rows */
  if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->thd, param->table)))
    return NULL;

  quick_imerge->records= records;
  quick_imerge->read_time= read_cost;
  for (TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
       range_scan++)
  {
    if (!(quick= (QUICK_RANGE_SELECT*)
          ((*range_scan)->make_quick(param, FALSE, &quick_imerge->alloc)))||
        quick_imerge->push_quick_back(quick))
    {
      delete quick;
      delete quick_imerge;
      return NULL;
    }
  }
  return quick_imerge;
}


QUICK_SELECT_I *TRP_INDEX_INTERSECT::make_quick(PARAM *param,
                                                bool retrieve_full_rows,
                                                MEM_ROOT *parent_alloc)
{
  QUICK_INDEX_INTERSECT_SELECT *quick_intersect;
  QUICK_RANGE_SELECT *quick;
  /* index_merge always retrieves full rows, ignore retrieve_full_rows */
  if (!(quick_intersect= new QUICK_INDEX_INTERSECT_SELECT(param->thd, param->table)))
    return NULL;

  quick_intersect->records= records;
  quick_intersect->read_time= read_cost;
  quick_intersect->filtered_scans= filtered_scans;
  for (TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
       range_scan++)
  {
    if (!(quick= (QUICK_RANGE_SELECT*)
          ((*range_scan)->make_quick(param, FALSE, &quick_intersect->alloc)))||
        quick_intersect->push_quick_back(quick))
    {
      delete quick;
      delete quick_intersect;
      return NULL;
    }
  }
  return quick_intersect;
}


QUICK_SELECT_I *TRP_ROR_INTERSECT::make_quick(PARAM *param,
                                              bool retrieve_full_rows,
                                              MEM_ROOT *parent_alloc)
{
  QUICK_ROR_INTERSECT_SELECT *quick_intrsect;
  QUICK_RANGE_SELECT *quick;
  DBUG_ENTER("TRP_ROR_INTERSECT::make_quick");
  MEM_ROOT *alloc;

  if ((quick_intrsect=
         new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
                                        (retrieve_full_rows? (!is_covering) :
                                         FALSE),
                                        parent_alloc)))
  {
    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                             "creating ROR-intersect",
                                             first_scan, last_scan););
    alloc= parent_alloc? parent_alloc: &quick_intrsect->alloc;
    for (; first_scan != last_scan;++first_scan)
    {
      if (!(quick= get_quick_select(param, (*first_scan)->idx,
                                    (*first_scan)->sel_arg,
                                    HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
                                    0, alloc)) ||
          quick_intrsect->push_quick_back(alloc, quick))
      {
        delete quick_intrsect;
        DBUG_RETURN(NULL);
      }
    }
    if (cpk_scan)
    {
      if (!(quick= get_quick_select(param, cpk_scan->idx,
                                    cpk_scan->sel_arg,
                                    HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
                                    0, alloc)))
      {
        delete quick_intrsect;
        DBUG_RETURN(NULL);
      }
      quick->file= NULL; 
      quick_intrsect->cpk_quick= quick;
    }
    quick_intrsect->records= records;
    quick_intrsect->read_time= read_cost;
  }
  DBUG_RETURN(quick_intrsect);
}


QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
                                          bool retrieve_full_rows,
                                          MEM_ROOT *parent_alloc)
{
  QUICK_ROR_UNION_SELECT *quick_roru;
  TABLE_READ_PLAN **scan;
  QUICK_SELECT_I *quick;
  DBUG_ENTER("TRP_ROR_UNION::make_quick");
  /*
    It is impossible to construct a ROR-union that will not retrieve full
    rows, ignore retrieve_full_rows parameter.
  */
  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
  {
    for (scan= first_ror; scan != last_ror; scan++)
    {
      if (!(quick= (*scan)->make_quick(param, FALSE, &quick_roru->alloc)) ||
          quick_roru->push_quick_back(quick))
        DBUG_RETURN(NULL);
    }
    quick_roru->records= records;
    quick_roru->read_time= read_cost;
  }
  DBUG_RETURN(quick_roru);
}


/*
  Build a SEL_TREE for <> or NOT BETWEEN predicate
 
  SYNOPSIS
    get_ne_mm_tree()
      param       PARAM from SQL_SELECT::test_quick_select
      cond_func   item for the predicate
      field       field in the predicate
      lt_value    constant that field should be smaller
      gt_value    constant that field should be greaterr

  RETURN 
    #  Pointer to tree built tree
    0  on error
*/

SEL_TREE *Item_bool_func::get_ne_mm_tree(RANGE_OPT_PARAM *param,
                                         Field *field,
                                         Item *lt_value, Item *gt_value)
{
  SEL_TREE *tree;
  tree= get_mm_parts(param, field, Item_func::LT_FUNC, lt_value);
  if (tree)
    tree= tree_or(param, tree, get_mm_parts(param, field, Item_func::GT_FUNC,
					    gt_value));
  return tree;
}


SEL_TREE *Item_func_between::get_func_mm_tree(RANGE_OPT_PARAM *param,
                                              Field *field, Item *value)
{
  SEL_TREE *tree;
  DBUG_ENTER("Item_func_between::get_func_mm_tree");
  if (!value)
  {
    if (negated)
    {
      tree= get_ne_mm_tree(param, field, args[1], args[2]);
    }
    else
    {
      tree= get_mm_parts(param, field, Item_func::GE_FUNC, args[1]);
      if (tree)
      {
        tree= tree_and(param, tree, get_mm_parts(param, field,
                                                 Item_func::LE_FUNC,
                                                 args[2]));
      }
    }
  }
  else
  {
    tree= get_mm_parts(param, field,
                       (negated ?
                        (value == (Item*)1 ? Item_func::GT_FUNC :
                                             Item_func::LT_FUNC):
                        (value == (Item*)1 ? Item_func::LE_FUNC :
                                             Item_func::GE_FUNC)),
                       args[0]);
  }
  DBUG_RETURN(tree);
}


SEL_TREE *Item_func_in::get_func_mm_tree(RANGE_OPT_PARAM *param,
                                         Field *field, Item *value)
{
  SEL_TREE *tree= 0;
  DBUG_ENTER("Item_func_in::get_func_mm_tree");
  /*
    Array for IN() is constructed when all values have the same result
    type. Tree won't be built for values with different result types,
    so we check it here to avoid unnecessary work.
  */
  if (!arg_types_compatible)
    DBUG_RETURN(0);

  if (negated)
  {
    if (array && array->result_type() != ROW_RESULT)
    {
      /*
        We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
        where c{i} are constants. Our goal is to produce a SEL_TREE that
        represents intervals:

        ($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ...    (*)

        where $MIN is either "-inf" or NULL.

        The most straightforward way to produce it is to convert NOT IN
        into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
        analyzer to build SEL_TREE from that. The problem is that the
        range analyzer will use O(N^2) memory (which is probably a bug),
        and people do use big NOT IN lists (e.g. see BUG#15872, BUG#21282),
        will run out of memory.

        Another problem with big lists like (*) is that a big list is
        unlikely to produce a good "range" access, while considering that
        range access will require expensive CPU calculations (and for
        MyISAM even index accesses). In short, big NOT IN lists are rarely
        worth analyzing.

        Considering the above, we'll handle NOT IN as follows:
        * if the number of entries in the NOT IN list is less than
          NOT_IN_IGNORE_THRESHOLD, construct the SEL_TREE (*) manually.
        * Otherwise, don't produce a SEL_TREE.
      */
#define NOT_IN_IGNORE_THRESHOLD 1000
      MEM_ROOT *tmp_root= param->mem_root;
      param->thd->mem_root= param->old_root;
      /* 
        Create one Item_type constant object. We'll need it as
        get_mm_parts only accepts constant values wrapped in Item_Type
        objects.
        We create the Item on param->mem_root which points to
        per-statement mem_root (while thd->mem_root is currently pointing
        to mem_root local to range optimizer).
      */
      Item *value_item= array->create_item(param->thd);
      param->thd->mem_root= tmp_root;

      if (array->count > NOT_IN_IGNORE_THRESHOLD || !value_item)
        DBUG_RETURN(0);

      /* Get a SEL_TREE for "(-inf|NULL) < X < c_0" interval.  */
      uint i=0;
      do
      {
        array->value_to_item(i, value_item);
        tree= get_mm_parts(param, field, Item_func::LT_FUNC, value_item);
        if (!tree)
          break;
        i++;
      } while (i < array->count && tree->type == SEL_TREE::IMPOSSIBLE);

      if (!tree || tree->type == SEL_TREE::IMPOSSIBLE)
      {
        /* We get here in cases like "t.unsigned NOT IN (-1,-2,-3) */
        DBUG_RETURN(NULL);
      }
      SEL_TREE *tree2;
      for (; i < array->used_count; i++)
      {
        if (array->compare_elems(i, i-1))
        {
          /* Get a SEL_TREE for "-inf < X < c_i" interval */
          array->value_to_item(i, value_item);
          tree2= get_mm_parts(param, field, Item_func::LT_FUNC, value_item);
          if (!tree2)
          {
            tree= NULL;
            break;
          }

          /* Change all intervals to be "c_{i-1} < X < c_i" */
          for (uint idx= 0; idx < param->keys; idx++)
          {
            SEL_ARG *new_interval, *last_val;
            if (((new_interval= tree2->keys[idx])) &&
                (tree->keys[idx]) &&
                ((last_val= tree->keys[idx]->last())))
            {
              new_interval->min_value= last_val->max_value;
              new_interval->min_flag= NEAR_MIN;

              /*
                If the interval is over a partial keypart, the
                interval must be "c_{i-1} <= X < c_i" instead of
                "c_{i-1} < X < c_i". Reason:

                Consider a table with a column "my_col VARCHAR(3)",
                and an index with definition
                "INDEX my_idx my_col(1)". If the table contains rows
                with my_col values "f" and "foo", the index will not
                distinguish the two rows.

                Note that tree_or() below will effectively merge
                this range with the range created for c_{i-1} and
                we'll eventually end up with only one range:
                "NULL < X".

                Partitioning indexes are never partial.
              */
              if (param->using_real_indexes)
              {
                const KEY key=
                  param->table->key_info[param->real_keynr[idx]];
                const KEY_PART_INFO *kpi= key.key_part + new_interval->part;

                if (kpi->key_part_flag & HA_PART_KEY_SEG)
                  new_interval->min_flag= 0;
              }
            }
          }
          /* 
            The following doesn't try to allocate memory so no need to
            check for NULL.
          */
          tree= tree_or(param, tree, tree2);
        }
      }

      if (tree && tree->type != SEL_TREE::IMPOSSIBLE)
      {
        /*
          Get the SEL_TREE for the last "c_last < X < +inf" interval
          (value_item cotains c_last already)
        */
        tree2= get_mm_parts(param, field, Item_func::GT_FUNC, value_item);
        tree= tree_or(param, tree, tree2);
      }
    }
    else
    {
      tree= get_ne_mm_tree(param, field, args[1], args[1]);
      if (tree)
      {
        Item **arg, **end;
        for (arg= args + 2, end= arg + arg_count - 2; arg < end ; arg++)
        {
          tree=  tree_and(param, tree, get_ne_mm_tree(param, field,
                                                      *arg, *arg));
        }
      }
    }
  }
  else
  {
    tree= get_mm_parts(param, field, Item_func::EQ_FUNC, args[1]);
    if (tree)
    {
      Item **arg, **end;
      for (arg= args + 2, end= arg + arg_count - 2;
           arg < end ; arg++)
      {
        tree= tree_or(param, tree, get_mm_parts(param, field,
                                                Item_func::EQ_FUNC, *arg));
      }
    }
  }
  DBUG_RETURN(tree);
}


/*
  The structure Key_col_info is purely  auxiliary and is used
  only in the method Item_func_in::get_func_row_mm_tree
*/ 
struct Key_col_info {
  Field *field;         /* If != NULL the column can be used for keys */
  cmp_item *comparator; /* If != 0 the column can be evaluated        */
};

/**
    Build SEL_TREE for the IN predicate whose arguments are rows 

    @param param          PARAM from SQL_SELECT::test_quick_select
    @param key_row        First operand of the IN predicate

  @note
    The function builds a SEL_TREE for in IN predicate in the case
    when the predicate uses row arguments. First the function
    detects among the components of the key_row (c[1],...,c[n]) taken 
    from in the left part the predicate those that can be usable
    for building SEL_TREE (c[i1],...,c[ik]).  They have to contain
    items whose real items are  field items referring to the current
    table or equal to the items referring to the current table.
    For the remaining components of the row it checks whether they
    can be evaluated. The result of the analysis is put into the
    array of structures of the type Key_row_col_info.

    After this the function builds the SEL_TREE for the following 
    formula that can be inferred from the given IN predicate:
      c[i11]=a[1][i11] AND ... AND c[i1k1]=a[1][i1k1]
      OR   
      ...
      OR
      c[im1]=a[m][im1] AND ... AND c[imkm]=a[m][imkm].
    Here a[1],...,a[m] are all arguments of the IN predicate from
    the right part and for each j ij1,...,ijkj is a subset of
    i1,...,ik such that a[j][ij1],...,a[j][ijkj] can be evaluated.

    If for some j there no a[j][i1],...,a[j][ik] can be evaluated
    then no SEL_TREE can be built for this predicate and the
    function immediately returns 0.

    If for some j by using evaluated values of key_row it can be
    proven that c[ij1]=a[j][ij1] AND ... AND c[ijkj]=a[j][ijkj]
    is always FALSE then this disjunct is omitted. 

  @returns
    the built SEL_TREE if it can be constructed
    0 - otherwise.
*/

SEL_TREE *Item_func_in::get_func_row_mm_tree(RANGE_OPT_PARAM *param,
                                             Item_row *key_row)
{
  DBUG_ENTER("Item_func_in::get_func_row_mm_tree");

  if (negated)
    DBUG_RETURN(0);

  SEL_TREE *res_tree= 0;
  uint used_key_cols= 0;
  uint col_comparators= 0;
  table_map param_comp= ~(param->prev_tables | param->read_tables |
                          param->current_table);
  uint row_cols= key_row->cols();
  Dynamic_array <Key_col_info> key_cols_info(row_cols);
  cmp_item_row *row_cmp_item;

  if (array)
  {
    in_row *row= static_cast<in_row*>(array);
    row_cmp_item= static_cast<cmp_item_row*>(row->get_cmp_item());
  }
  else
  {
    DBUG_ASSERT(get_comparator_type_handler(0) == &type_handler_row);
    row_cmp_item= static_cast<cmp_item_row*>(get_comparator_cmp_item(0));
  }
  DBUG_ASSERT(row_cmp_item);

  Item **key_col_ptr= key_row->addr(0);
  for(uint i= 0; i < row_cols;  i++, key_col_ptr++)
  {
    Key_col_info key_col_info= {0, NULL};
    Item *key_col= *key_col_ptr;
    if (key_col->real_item()->type() == Item::FIELD_ITEM)
    {
      /*
        The i-th component of key_row can be used for key access if 
        key_col->real_item() points to a field of the current table or
        if it is equal to a field item pointing to such a field.
      */
      Item_field *col_field_item= (Item_field *) (key_col->real_item());
      Field *key_col_field= col_field_item->field;
      if (key_col_field->table->map != param->current_table)
      {
        Item_equal *item_equal= col_field_item->item_equal;
        if (item_equal)
        {
          Item_equal_fields_iterator it(*item_equal);
          while (it++)
	  {
            key_col_field= it.get_curr_field();
            if (key_col_field->table->map == param->current_table)
              break;
          }
        }
      }
      if (key_col_field->table->map == param->current_table)
      {
        key_col_info.field= key_col_field;
        used_key_cols++;
      }
    }
    else if (!(key_col->used_tables() & (param_comp | param->current_table))
             && !key_col->is_expensive())
    {
      /* The i-th component of key_row can be evaluated */

      /* See the comment in Item::get_mm_tree_for_const */
      MEM_ROOT *tmp_root= param->mem_root;
      param->thd->mem_root= param->old_root;

      key_col->bring_value();
      key_col_info.comparator= row_cmp_item->get_comparator(i);
      key_col_info.comparator->store_value(key_col);
      col_comparators++;

      param->thd->mem_root= tmp_root;
    }
    key_cols_info.push(key_col_info);
  }

  if (!used_key_cols)
    DBUG_RETURN(0);

  uint omitted_tuples= 0;
  Item **arg_start= arguments() + 1;
  Item **arg_end= arg_start + argument_count() - 1;
  for (Item **arg= arg_start ; arg < arg_end; arg++)
  {
    uint i;
   
    /* 
      First check whether the disjunct constructed for *arg
      is really needed
    */  
    Item_row *arg_tuple= (Item_row *) (*arg);
    if (col_comparators)
    {
      MEM_ROOT *tmp_root= param->mem_root;
      param->thd->mem_root= param->old_root;
      for (i= 0; i < row_cols; i++)
      {
        Key_col_info *key_col_info= &key_cols_info.at(i);
        if (key_col_info->comparator)
	{
          Item *arg_col= arg_tuple->element_index(i);
          if (!(arg_col->used_tables() & (param_comp | param->current_table)) &&
	      !arg_col->is_expensive() &&
              key_col_info->comparator->cmp(arg_col))
	  {
            omitted_tuples++;
            break;
          }
        }
      }
      param->thd->mem_root= tmp_root;
      if (i < row_cols)
        continue;
    }
    
    /* The disjunct for *arg is needed: build it. */     
    SEL_TREE *and_tree= 0;
    Item **arg_col_ptr= arg_tuple->addr(0);
    for (uint i= 0; i < row_cols; i++, arg_col_ptr++)
    { 
      Key_col_info *key_col_info= &key_cols_info.at(i);
      if (!key_col_info->field)
        continue;
      Item *arg_col= *arg_col_ptr;
      if (!(arg_col->used_tables() & (param_comp | param->current_table)) &&
	  !arg_col->is_expensive())
      {
        and_tree= tree_and(param, and_tree, 
                           get_mm_parts(param,
                                        key_col_info->field, 
                                        Item_func::EQ_FUNC,
                                        arg_col->real_item()));
      }
    }
    if (!and_tree)
    {
      res_tree= 0;
      break;
    }
    /* Join the disjunct the the OR tree that is being constructed */
    res_tree= !res_tree ? and_tree : tree_or(param, res_tree, and_tree);
  }
  if (omitted_tuples == argument_count() - 1)
  {
    /* It's turned out that all disjuncts are always FALSE */
    res_tree= new (param->mem_root) SEL_TREE(SEL_TREE::IMPOSSIBLE,
                                             param->mem_root, param->keys);
  }
  DBUG_RETURN(res_tree);
}


/*
  Build conjunction of all SEL_TREEs for a simple predicate applying equalities
 
  SYNOPSIS
    get_full_func_mm_tree()
      param       PARAM from SQL_SELECT::test_quick_select
      field_item  field in the predicate
      value       constant in the predicate (or a field already read from 
                  a table in the case of dynamic range access)
                  (for BETWEEN it contains the number of the field argument,
                   for IN it's always 0) 
      inv         TRUE <> NOT cond_func is considered
                  (makes sense only when cond_func is BETWEEN or IN)

  DESCRIPTION
    For a simple SARGable predicate of the form (f op c), where f is a field and
    c is a constant, the function builds a conjunction of all SEL_TREES that can
    be obtained by the substitution of f for all different fields equal to f.

  NOTES  
    If the WHERE condition contains a predicate (fi op c),
    then not only SELL_TREE for this predicate is built, but
    the trees for the results of substitution of fi for
    each fj belonging to the same multiple equality as fi
    are built as well.
    E.g. for WHERE t1.a=t2.a AND t2.a > 10 
    a SEL_TREE for t2.a > 10 will be built for quick select from t2
    and   
    a SEL_TREE for t1.a > 10 will be built for quick select from t1.

    A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
    in a similar way: we build a conjuction of trees for the results
    of all substitutions of fi for equal fj.
    Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
    differently. It is considered as a conjuction of two SARGable
    predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
    is called for each of them separately producing trees for 
       AND j (f1j <=c ) and AND j (f2j <= c) 
    After this these two trees are united in one conjunctive tree.
    It's easy to see that the same tree is obtained for
       AND j,k (f1j <=c AND f2k<=c)
    which is equivalent to 
       AND j,k (c BETWEEN f1j AND f2k).
    The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
    which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
    function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
    producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
    trees are united in one OR-tree. The expression 
      (AND j (f1j > c) OR AND j (f2j < c)
    is equivalent to the expression
      AND j,k (f1j > c OR f2k < c) 
    which is just a translation of 
      AND j,k (c NOT BETWEEN f1j AND f2k)

    In the cases when one of the items f1, f2 is a constant c1 we do not create
    a tree for it at all. It works for BETWEEN predicates but does not
    work for NOT BETWEEN predicates as we have to evaluate the expression
    with it. If it is TRUE then the other tree can be completely ignored.
    We do not do it now and no trees are built in these cases for
    NOT BETWEEN predicates.

    As to IN predicates only ones of the form (f IN (c1,...,cn)),
    where f1 is a field and c1,...,cn are constant, are considered as
    SARGable. We never try to narrow the index scan using predicates of
    the form (c IN (c1,...,f,...,cn)). 
      
  RETURN 
    Pointer to the tree representing the built conjunction of SEL_TREEs
*/

SEL_TREE *Item_bool_func::get_full_func_mm_tree(RANGE_OPT_PARAM *param,
                                                Item_field *field_item,
                                                Item *value)
{
  DBUG_ENTER("Item_bool_func::get_full_func_mm_tree");
  SEL_TREE *tree= 0;
  SEL_TREE *ftree= 0;
  table_map ref_tables= 0;
  table_map param_comp= ~(param->prev_tables | param->read_tables |
		          param->current_table);
#ifdef HAVE_SPATIAL
  Field::geometry_type sav_geom_type;
  LINT_INIT_STRUCT(sav_geom_type);

  if (field_item->field->type() == MYSQL_TYPE_GEOMETRY)
  {
    sav_geom_type= ((Field_geom*) field_item->field)->geom_type;
    /* We have to be able to store all sorts of spatial features here */
    ((Field_geom*) field_item->field)->geom_type= Field::GEOM_GEOMETRY;
  }
#endif /*HAVE_SPATIAL*/

  for (uint i= 0; i < arg_count; i++)
  {
    Item *arg= arguments()[i]->real_item();
    if (arg != field_item)
      ref_tables|= arg->used_tables();
  }
  Field *field= field_item->field;
  if (!((ref_tables | field->table->map) & param_comp))
    ftree= get_func_mm_tree(param, field, value);
  Item_equal *item_equal= field_item->item_equal;
  if (item_equal)
  {
    Item_equal_fields_iterator it(*item_equal);
    while (it++)
    {
      Field *f= it.get_curr_field();
      if (field->eq(f))
        continue;
      if (!((ref_tables | f->table->map) & param_comp))
      {
        tree= get_func_mm_tree(param, f, value);
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
      }
    }
  }

#ifdef HAVE_SPATIAL
  if (field_item->field->type() == MYSQL_TYPE_GEOMETRY)
  {
    ((Field_geom*) field_item->field)->geom_type= sav_geom_type;
  }
#endif /*HAVE_SPATIAL*/
  DBUG_RETURN(ftree);
}


/* 
  make a select tree of all keys in condition 
  
  @param  param  Context
  @param  cond  INOUT condition to perform range analysis on.

  @detail
    Range analysis may infer that some conditions are never true. 
    - If the condition is never true, SEL_TREE(type=IMPOSSIBLE) is returned
    - if parts of condition are never true, the function may remove these parts
      from the condition 'cond'.  Sometimes, this will cause the condition to
      be substituted for something else.


  @return 
    NULL     - Could not infer anything from condition cond.
    SEL_TREE with type=IMPOSSIBLE - condition can never be true.
*/
SEL_TREE *Item_cond_and::get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
{
  DBUG_ENTER("Item_cond_and::get_mm_tree");
  SEL_TREE *tree= NULL;
  List_iterator<Item> li(*argument_list());
  Item *item;
  while ((item= li++))
  {
    SEL_TREE *new_tree= li.ref()[0]->get_mm_tree(param,li.ref());
    if (param->statement_should_be_aborted())
      DBUG_RETURN(NULL);
    tree= tree_and(param, tree, new_tree);
    if (tree && tree->type == SEL_TREE::IMPOSSIBLE)
    {
      /*
        Do not remove 'item' from 'cond'. We return a SEL_TREE::IMPOSSIBLE
        and that is sufficient for the caller to see that the whole
        condition is never true.
      */
      break;
    }
  }
  DBUG_RETURN(tree);
}


SEL_TREE *Item_cond::get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
{
  DBUG_ENTER("Item_cond::get_mm_tree");
  List_iterator<Item> li(*argument_list());
  bool replace_cond= false;
  Item *replacement_item= li++;
  SEL_TREE *tree= li.ref()[0]->get_mm_tree(param, li.ref());
  if (param->statement_should_be_aborted())
    DBUG_RETURN(NULL);
  if (tree)
  {
    if (tree->type == SEL_TREE::IMPOSSIBLE &&
        param->remove_false_where_parts)
    {
      /* See the other li.remove() call below */
      li.remove();
      if (argument_list()->elements <= 1)
        replace_cond= true;
    }

    Item *item;
    while ((item= li++))
    {
      SEL_TREE *new_tree= li.ref()[0]->get_mm_tree(param, li.ref());
      if (new_tree == NULL || param->statement_should_be_aborted())
        DBUG_RETURN(NULL);
      tree= tree_or(param, tree, new_tree);
      if (tree == NULL || tree->type == SEL_TREE::ALWAYS)
      {
        replacement_item= *li.ref();
        break;
      }

      if (new_tree && new_tree->type == SEL_TREE::IMPOSSIBLE &&
          param->remove_false_where_parts)
      {
        /*
          This is a condition in form

            cond = item1 OR ... OR item_i OR ... itemN

          and item_i produces SEL_TREE(IMPOSSIBLE). We should remove item_i
          from cond.  This may cause 'cond' to become a degenerate,
          one-way OR. In that case, we replace 'cond' with the remaining
          item_i.
        */
        li.remove();
        if (argument_list()->elements <= 1)
          replace_cond= true;
      }
      else
        replacement_item= *li.ref();
    }

    if (replace_cond)
      *cond_ptr= replacement_item;
  }
  DBUG_RETURN(tree);
}


SEL_TREE *Item::get_mm_tree_for_const(RANGE_OPT_PARAM *param)
{
  DBUG_ENTER("get_mm_tree_for_const");
  if (is_expensive())
    DBUG_RETURN(0);
  /*
    During the cond->val_int() evaluation we can come across a subselect
    item which may allocate memory on the thd->mem_root and assumes
    all the memory allocated has the same life span as the subselect
    item itself. So we have to restore the thread's mem_root here.
  */
  MEM_ROOT *tmp_root= param->mem_root;
  param->thd->mem_root= param->old_root;
  SEL_TREE *tree;

  const SEL_TREE::Type type= val_int()? SEL_TREE::ALWAYS: SEL_TREE::IMPOSSIBLE;
  param->thd->mem_root= tmp_root;

  tree= new (tmp_root) SEL_TREE(type, tmp_root, param->keys);
  DBUG_RETURN(tree);
}


SEL_TREE *Item::get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
{
  DBUG_ENTER("Item::get_mm_tree");
  if (const_item())
    DBUG_RETURN(get_mm_tree_for_const(param));

  /*
    Here we have a not-constant non-function Item.

    Item_field should not appear, as normalize_cond() replaces
    "WHERE field" to "WHERE field<>0".

    Item_exists_subselect is possible, e.g. in this query:
    SELECT id, st FROM t1
    WHERE st IN ('GA','FL') AND EXISTS (SELECT 1 FROM t2 WHERE t2.id=t1.id)
    GROUP BY id;
  */
  table_map ref_tables= used_tables();
  if ((ref_tables & param->current_table) ||
      (ref_tables & ~(param->prev_tables | param->read_tables)))
    DBUG_RETURN(0);
  DBUG_RETURN(new (param->mem_root) SEL_TREE(SEL_TREE::MAYBE, param->mem_root, 
                                             param->keys));
}


SEL_TREE *
Item_func_between::get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
{
  DBUG_ENTER("Item_func_between::get_mm_tree");
  if (const_item())
    DBUG_RETURN(get_mm_tree_for_const(param));

  SEL_TREE *tree= 0;
  SEL_TREE *ftree= 0;

  if (arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
  {
    Item_field *field_item= (Item_field*) (arguments()[0]->real_item());
    ftree= get_full_func_mm_tree(param, field_item, NULL);
  }

  /*
    Concerning the code below see the NOTES section in
    the comments for the function get_full_func_mm_tree()
  */
  for (uint i= 1 ; i < arg_count ; i++)
  {
    if (arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
    {
      Item_field *field_item= (Item_field*) (arguments()[i]->real_item());
      SEL_TREE *tmp= get_full_func_mm_tree(param, field_item,
                                           (Item*)(intptr) i);
      if (negated)
      {
        tree= !tree ? tmp : tree_or(param, tree, tmp);
        if (tree == NULL)
          break;
      }
      else
        tree= tree_and(param, tree, tmp);
    }
    else if (negated)
    {
      tree= 0;
      break;
    }
  }

  ftree= tree_and(param, ftree, tree);
  DBUG_RETURN(ftree);
}


SEL_TREE *Item_func_in::get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
{
  DBUG_ENTER("Item_func_in::get_mm_tree");
  if (const_item())
    DBUG_RETURN(get_mm_tree_for_const(param));

  SEL_TREE *tree= 0;
  switch (key_item()->real_item()->type()) {
  case Item::FIELD_ITEM:
    tree= get_full_func_mm_tree(param,
                                (Item_field*) (key_item()->real_item()),
                                NULL);
    break;
  case Item::ROW_ITEM:
    tree= get_func_row_mm_tree(param,
			       (Item_row *) (key_item()->real_item()));
    break;
  default:
    DBUG_RETURN(0);
  } 
  DBUG_RETURN(tree);
} 


SEL_TREE *Item_equal::get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
{
  DBUG_ENTER("Item_equal::get_mm_tree");
  if (const_item())
    DBUG_RETURN(get_mm_tree_for_const(param));

  SEL_TREE *tree= 0;
  SEL_TREE *ftree= 0;

  Item *value;
  if (!(value= get_const()) || value->is_expensive())
    DBUG_RETURN(0);

  Item_equal_fields_iterator it(*this);
  table_map ref_tables= value->used_tables();
  table_map param_comp= ~(param->prev_tables | param->read_tables |
		          param->current_table);
  while (it++)
  {
    Field *field= it.get_curr_field();
    if (!((ref_tables | field->table->map) & param_comp))
    {
      tree= get_mm_parts(param, field, Item_func::EQ_FUNC, value);
      ftree= !ftree ? tree : tree_and(param, ftree, tree);
    }
  }

  DBUG_RETURN(ftree);
}


SEL_TREE *
Item_bool_func::get_mm_parts(RANGE_OPT_PARAM *param, Field *field,
	                     Item_func::Functype type, Item *value)
{
  DBUG_ENTER("get_mm_parts");
  if (field->table != param->table)
    DBUG_RETURN(0);

  KEY_PART *key_part = param->key_parts;
  KEY_PART *end = param->key_parts_end;
  SEL_TREE *tree=0;
  table_map value_used_tables= 0;
  if (value &&
      (value_used_tables= value->used_tables()) &
      ~(param->prev_tables | param->read_tables))
    DBUG_RETURN(0);
  for (; key_part != end ; key_part++)
  {
    if (field->eq(key_part->field))
    {
      SEL_ARG *sel_arg=0;
      if (!tree && !(tree=new (param->thd->mem_root) SEL_TREE(param->mem_root,
                                                              param->keys)))
	DBUG_RETURN(0);				// OOM
      if (!value || !(value_used_tables & ~param->read_tables))
      {
        /*
          We need to restore the runtime mem_root of the thread in this
          function because it evaluates the value of its argument, while
          the argument can be any, e.g. a subselect. The subselect
          items, in turn, assume that all the memory allocated during
          the evaluation has the same life span as the item itself.
          TODO: opt_range.cc should not reset thd->mem_root at all.
        */
        MEM_ROOT *tmp_root= param->mem_root;
        param->thd->mem_root= param->old_root;
        sel_arg= get_mm_leaf(param, key_part->field, key_part, type, value);
        param->thd->mem_root= tmp_root;

	if (!sel_arg)
	  continue;
	if (sel_arg->type == SEL_ARG::IMPOSSIBLE)
	{
	  tree->type=SEL_TREE::IMPOSSIBLE;
	  DBUG_RETURN(tree);
	}
      }
      else
      {
	// This key may be used later
	if (!(sel_arg= new SEL_ARG(SEL_ARG::MAYBE_KEY)))
	  DBUG_RETURN(0);			// OOM
      }
      sel_arg->part=(uchar) key_part->part;
      sel_arg->max_part_no= sel_arg->part+1;
      tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
      tree->keys_map.set_bit(key_part->key);
    }
  }

  if (tree && tree->merges.is_empty() && tree->keys_map.is_clear_all())
    tree= NULL;
  DBUG_RETURN(tree);
}


SEL_ARG *
Item_func_null_predicate::get_mm_leaf(RANGE_OPT_PARAM *param,
                                      Field *field, KEY_PART *key_part,
                                      Item_func::Functype type,
                                      Item *value)
{
  MEM_ROOT *alloc= param->mem_root;
  DBUG_ENTER("Item_func_null_predicate::get_mm_leaf");
  DBUG_ASSERT(!value);
  /*
    No check for field->table->maybe_null. It's perfecly fine to use range
    access for cases like

      SELECT * FROM t1 LEFT JOIN t2 ON t2.key IS [NOT] NULL

    ON expression is evaluated before considering NULL-complemented rows, so
    IS [NOT] NULL has regular semantics.
  */
  if (!field->real_maybe_null())
    DBUG_RETURN(type == ISNULL_FUNC ? &null_element : NULL);
  SEL_ARG *tree;
  if (!(tree= new (alloc) SEL_ARG(field, is_null_string, is_null_string)))
    DBUG_RETURN(0);
  if (type == Item_func::ISNOTNULL_FUNC)
  {
    tree->min_flag=NEAR_MIN;		    /* IS NOT NULL ->  X > NULL */
    tree->max_flag=NO_MAX_RANGE;
  }
  DBUG_RETURN(tree);
}


SEL_ARG *
Item_func_like::get_mm_leaf(RANGE_OPT_PARAM *param,
                            Field *field, KEY_PART *key_part,
                            Item_func::Functype type, Item *value)
{
  DBUG_ENTER("Item_func_like::get_mm_leaf");
  DBUG_ASSERT(value);

  if (key_part->image_type != Field::itRAW)
    DBUG_RETURN(0);

  if (param->using_real_indexes &&
      !field->optimize_range(param->real_keynr[key_part->key],
                             key_part->part))
    DBUG_RETURN(0);

  if (field->result_type() == STRING_RESULT &&
      field->charset() != compare_collation())
    DBUG_RETURN(0);

  StringBuffer<MAX_FIELD_WIDTH> tmp(value->collation.collation);
  String *res;

  if (!(res= value->val_str(&tmp)))
    DBUG_RETURN(&null_element);

  if (field->cmp_type() != STRING_RESULT ||
      field->type_handler() == &type_handler_enum ||
      field->type_handler() == &type_handler_set)
    DBUG_RETURN(0);

  /*
    TODO:
    Check if this was a function. This should have be optimized away
    in the sql_select.cc
  */
  if (res != &tmp)
  {
    tmp.copy(*res);				// Get own copy
    res= &tmp;
  }

  uint maybe_null= (uint) field->real_maybe_null();
  uint field_length= field->pack_length() + maybe_null;
  size_t offset= maybe_null;
  size_t length= key_part->store_length;

  if (length != key_part->length + maybe_null)
  {
    /* key packed with length prefix */
    offset+= HA_KEY_BLOB_LENGTH;
    field_length= length - HA_KEY_BLOB_LENGTH;
  }
  else
  {
    if (unlikely(length < field_length))
    {
      /*
        This can only happen in a table created with UNIREG where one key
        overlaps many fields
      */
      length= field_length;
    }
    else
      field_length= length;
  }
  length+= offset;
  uchar *min_str,*max_str;
  if (!(min_str= (uchar*) alloc_root(param->mem_root, length*2)))
    DBUG_RETURN(0);
  max_str= min_str + length;
  if (maybe_null)
    max_str[0]= min_str[0]=0;

  size_t min_length, max_length;
  field_length-= maybe_null;
  if (my_like_range(field->charset(),
                                 res->ptr(), res->length(),
                                 escape, wild_one, wild_many,
                                 field_length,
                                 (char*) min_str + offset,
                                 (char*) max_str + offset,
                                 &min_length, &max_length))
    DBUG_RETURN(0);              // Can't optimize with LIKE

  if (offset != maybe_null)			// BLOB or VARCHAR
  {
    int2store(min_str + maybe_null, min_length);
    int2store(max_str + maybe_null, max_length);
  }
  SEL_ARG *tree= new (param->mem_root) SEL_ARG(field, min_str, max_str);
  DBUG_RETURN(tree);
}


SEL_ARG *
Item_bool_func::get_mm_leaf(RANGE_OPT_PARAM *param,
                            Field *field, KEY_PART *key_part,
                            Item_func::Functype type, Item *value)
{
  uint maybe_null=(uint) field->real_maybe_null();
  SEL_ARG *tree= 0;
  MEM_ROOT *alloc= param->mem_root;
  uchar *str;
  int err;
  DBUG_ENTER("Item_bool_func::get_mm_leaf");

  DBUG_ASSERT(value); // IS NULL and IS NOT NULL are handled separately

  if (key_part->image_type != Field::itRAW)
    DBUG_RETURN(0);   // e.g. SPATIAL index

  if (param->using_real_indexes &&
      !field->optimize_range(param->real_keynr[key_part->key],
                             key_part->part) &&
      type != EQ_FUNC &&
      type != EQUAL_FUNC)
    goto end;                                   // Can't optimize this

  if (!field->can_optimize_range(this, value,
                                 type == EQUAL_FUNC || type == EQ_FUNC))
    goto end;

  err= value->save_in_field_no_warnings(field, 1);
  if (err > 0)
  {
    if (field->type_handler() == &type_handler_enum ||
        field->type_handler() == &type_handler_set)
    {
      if (type == EQ_FUNC || type == EQUAL_FUNC)
        tree= new (alloc) SEL_ARG_IMPOSSIBLE(field);
      goto end;
    }

    if (err == 2 && field->cmp_type() == STRING_RESULT)
    {
      if (type == EQ_FUNC || type == EQUAL_FUNC)
        tree= new (alloc) SEL_ARG_IMPOSSIBLE(field);
      else
        tree= NULL; /*  Cannot infer anything */
      goto end;
    }

    if (err == 3 && field->type() == FIELD_TYPE_DATE)
    {
      /*
        We were saving DATETIME into a DATE column, the conversion went ok
        but a non-zero time part was cut off.

        In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
        values. Index over a DATE column uses DATE comparison. Changing
        from one comparison to the other is possible:

        datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
        datetime(date_col)<='2007-12-10 12:34:55' -> date_col<='2007-12-10'

        datetime(date_col)> '2007-12-10 12:34:55' -> date_col>='2007-12-10'
        datetime(date_col)>='2007-12-10 12:34:55' -> date_col>='2007-12-10'

        but we'll need to convert '>' to '>=' and '<' to '<='. This will
        be done together with other types at the end of this function
        (grep for stored_field_cmp_to_item)
      */
      if (type == EQ_FUNC || type == EQUAL_FUNC)
      {
        tree= new (alloc) SEL_ARG_IMPOSSIBLE(field);
        goto end;
      }
      // Continue with processing non-equality ranges
    }
    else if (field->cmp_type() != value->result_type())
    {
      if ((type == EQ_FUNC || type == EQUAL_FUNC) &&
          value->result_type() == item_cmp_type(field->result_type(),
                                                value->result_type()))
      {
        tree= new (alloc) SEL_ARG_IMPOSSIBLE(field);
        goto end;
      }
      else
      {
        /*
          TODO: We should return trees of the type SEL_ARG::IMPOSSIBLE
          for the cases like int_field > 999999999999999999999999 as well.
        */
        tree= 0;
        goto end;
      }
    }

    /*
      guaranteed at this point:  err > 0; field and const of same type
      If an integer got bounded (e.g. to within 0..255 / -128..127)
      for < or >, set flags as for <= or >= (no NEAR_MAX / NEAR_MIN)
    */
    else if (err == 1 && field->result_type() == INT_RESULT)
    {
      if (type == LT_FUNC && (value->val_int() > 0))
        type= LE_FUNC;
      else if (type == GT_FUNC &&
               (field->type() != FIELD_TYPE_BIT) &&
               !((Field_num*)field)->unsigned_flag &&
               !((Item_int*)value)->unsigned_flag &&
               (value->val_int() < 0))
        type= GE_FUNC;
    }
  }
  else if (err < 0)
  {
    /* This happens when we try to insert a NULL field in a not null column */
    tree= &null_element;                        // cmp with NULL is never TRUE
    goto end;
  }

  /*
    Any sargable predicate except "<=>" involving NULL as a constant is always
    FALSE
  */
  if (type != EQUAL_FUNC && field->is_real_null())
  {
    tree= &null_element;
    goto end;
  }
  
  str= (uchar*) alloc_root(alloc, key_part->store_length+1);
  if (!str)
    goto end;
  if (maybe_null)
    *str= (uchar) field->is_real_null();        // Set to 1 if null
  field->get_key_image(str+maybe_null, key_part->length,
                       key_part->image_type);
  if (!(tree= new (alloc) SEL_ARG(field, str, str)))
    goto end;                                   // out of memory

  /*
    Check if we are comparing an UNSIGNED integer with a negative constant.
    In this case we know that:
    (a) (unsigned_int [< | <=] negative_constant) == FALSE
    (b) (unsigned_int [> | >=] negative_constant) == TRUE
    In case (a) the condition is false for all values, and in case (b) it
    is true for all values, so we can avoid unnecessary retrieval and condition
    testing, and we also get correct comparison of unsinged integers with
    negative integers (which otherwise fails because at query execution time
    negative integers are cast to unsigned if compared with unsigned).
   */
  if (field->result_type() == INT_RESULT &&
      value->result_type() == INT_RESULT &&
      ((field->type() == FIELD_TYPE_BIT || 
       ((Field_num *) field)->unsigned_flag) && 
       !((Item_int*) value)->unsigned_flag))
  {
    longlong item_val= value->val_int();
    if (item_val < 0)
    {
      if (type == LT_FUNC || type == LE_FUNC)
      {
        tree->type= SEL_ARG::IMPOSSIBLE;
        goto end;
      }
      if (type == GT_FUNC || type == GE_FUNC)
      {
        tree= 0;
        goto end;
      }
    }
  }

  switch (type) {
  case LT_FUNC:
    if (stored_field_cmp_to_item(param->thd, field, value) == 0)
      tree->max_flag=NEAR_MAX;
    /* fall through */
  case LE_FUNC:
    if (!maybe_null)
      tree->min_flag=NO_MIN_RANGE;		/* From start */
    else
    {						// > NULL
      tree->min_value=is_null_string;
      tree->min_flag=NEAR_MIN;
    }
    break;
  case GT_FUNC:
    /* Don't use open ranges for partial key_segments */
    if ((!(key_part->flag & HA_PART_KEY_SEG)) &&
        (stored_field_cmp_to_item(param->thd, field, value) <= 0))
      tree->min_flag=NEAR_MIN;
    tree->max_flag= NO_MAX_RANGE;
    break;
  case GE_FUNC:
    /* Don't use open ranges for partial key_segments */
    if ((!(key_part->flag & HA_PART_KEY_SEG)) &&
        (stored_field_cmp_to_item(param->thd, field, value) < 0))
      tree->min_flag= NEAR_MIN;
    tree->max_flag=NO_MAX_RANGE;
    break;
  case EQ_FUNC:
  case EQUAL_FUNC:
    break;
  default:
    DBUG_ASSERT(0);
    break;
  }

end:
  DBUG_RETURN(tree);
}


/******************************************************************************
** Tree manipulation functions
** If tree is 0 it means that the condition can't be tested. It refers
** to a non existent table or to a field in current table with isn't a key.
** The different tree flags:
** IMPOSSIBLE:	 Condition is never TRUE
** ALWAYS:	 Condition is always TRUE
** MAYBE:	 Condition may exists when tables are read
** MAYBE_KEY:	 Condition refers to a key that may be used in join loop
** KEY_RANGE:	 Condition uses a key
******************************************************************************/

/*
  Add a new key test to a key when scanning through all keys
  This will never be called for same key parts.
*/

static SEL_ARG *
sel_add(SEL_ARG *key1,SEL_ARG *key2)
{
  SEL_ARG *root,**key_link;

  if (!key1)
    return key2;
  if (!key2)
    return key1;

  key_link= &root;
  while (key1 && key2)
  {
    if (key1->part < key2->part)
    {
      *key_link= key1;
      key_link= &key1->next_key_part;
      key1=key1->next_key_part;
    }
    else
    {
      *key_link= key2;
      key_link= &key2->next_key_part;
      key2=key2->next_key_part;
    }
  }
  *key_link=key1 ? key1 : key2;
  return root;
}


/* 
  Build a range tree for the conjunction of the range parts of two trees

  SYNOPSIS
    and_range_trees()
      param           Context info for the operation
      tree1           SEL_TREE for the first conjunct          
      tree2           SEL_TREE for the second conjunct
      result          SEL_TREE for the result

  DESCRIPTION
    This function takes range parts of two trees tree1 and tree2 and builds
    a range tree for the conjunction of the formulas that these two range parts
    represent.
    More exactly: 
    if the range part of tree1 represents the normalized formula 
      R1_1 AND ... AND R1_k,
    and the range part of tree2 represents the normalized formula
      R2_1 AND ... AND R2_k,
    then the range part of the result represents the formula:
     RT = R_1 AND ... AND R_k, where R_i=(R1_i AND R2_i) for each i from [1..k]

    The function assumes that tree1 is never equal to tree2. At the same
    time the tree result can be the same as tree1 (but never as tree2).
    If result==tree1 then rt replaces the range part of tree1 leaving
    imerges as they are.
    if result!=tree1 than it is assumed that the SEL_ARG trees in tree1 and
    tree2 should be preserved. Otherwise they can be destroyed.

  RETURN 
    1    if the type the result tree is  SEL_TREE::IMPOSSIBLE
    0    otherwise    
*/

static
int and_range_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree1, SEL_TREE *tree2,
                    SEL_TREE *result)
{
  DBUG_ENTER("and_ranges");
  key_map  result_keys;
  result_keys.clear_all();
  key_map anded_keys= tree1->keys_map;
  anded_keys.merge(tree2->keys_map);
  int key_no;
  key_map::Iterator it(anded_keys);
  while ((key_no= it++) != key_map::Iterator::BITMAP_END)
  {
    uint flag=0;
    SEL_ARG *key1= tree1->keys[key_no];
    SEL_ARG *key2= tree2->keys[key_no];
    if (key1 && !key1->simple_key())
      flag|= CLONE_KEY1_MAYBE;
    if (key2 && !key2->simple_key())
      flag|=CLONE_KEY2_MAYBE;
    if (result != tree1)
    { 
      if (key1)
        key1->incr_refs();
      if (key2)
        key2->incr_refs();
    }
    SEL_ARG *key;
    if ((result->keys[key_no]= key =key_and(param, key1, key2, flag)))
    {
      if (key && key->type == SEL_ARG::IMPOSSIBLE)
      {
	result->type= SEL_TREE::IMPOSSIBLE;
        DBUG_RETURN(1);
      }
      result_keys.set_bit(key_no);
#ifdef EXTRA_DEBUG
      if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
        key->test_use_count(key);
#endif
    }
  }
  result->keys_map= result_keys;
  DBUG_RETURN(0);
}
  

/*
  Build a SEL_TREE for a conjunction out of such trees for the conjuncts

  SYNOPSIS
    tree_and()
      param           Context info for the operation
      tree1           SEL_TREE for the first conjunct          
      tree2           SEL_TREE for the second conjunct

  DESCRIPTION
    This function builds a tree for the formula (A AND B) out of the trees
    tree1 and tree2 that has been built for the formulas A and B respectively.

    In a general case
      tree1 represents the formula RT1 AND MT1,
        where RT1 = R1_1 AND ... AND R1_k1, MT1=M1_1 AND ... AND M1_l1;
      tree2 represents the formula RT2 AND MT2 
        where RT2 = R2_1 AND ... AND R2_k2, MT2=M2_1 AND ... AND M2_l2.

    The result tree will represent the formula of the the following structure:
      RT AND RT1MT2 AND RT2MT1, such that
        rt is a tree obtained by range intersection of trees tree1 and tree2,
        RT1MT2 = RT1M2_1 AND ... AND RT1M2_l2,
        RT2MT1 = RT2M1_1 AND ... AND RT2M1_l1,
        where rt1m2_i (i=1,...,l2) is the result of the pushdown operation
        of range tree rt1 into imerge m2_i, while rt2m1_j (j=1,...,l1) is the
        result of the pushdown operation of range tree rt2 into imerge m1_j.

    RT1MT2/RT2MT is empty if MT2/MT1 is empty.
 
    The range intersection of two range trees is produced by the function
    and_range_trees. The pushdown of a range tree to a imerge is performed
    by the function imerge_list_and_tree. This function may produce imerges
    containing only one range tree. Such trees are intersected with rt and 
    the result of intersection is returned as the range part of the result
    tree, while the corresponding imerges are removed altogether from its
    imerge part. 
    
  NOTE
    The pushdown operation of range trees into imerges is needed to be able
    to construct valid imerges for the condition like this:
      key1_p1=c1 AND (key1_p2 BETWEEN c21 AND c22 OR key2 < c2)

  NOTE
    Currently we do not support intersection between indexes and index merges.
    When this will be supported the list of imerges for the result tree
    should include also imerges from M1 and M2. That's why an extra parameter
    is added to the function imerge_list_and_tree. If we call the function
    with the last parameter equal to FALSE then MT1 and MT2 will be preserved
    in the imerge list of the result tree. This can lead to the exponential
    growth of the imerge list though. 
    Currently the last parameter of imerge_list_and_tree calls is always
    TRUE.

  RETURN
    The result tree, if a success
    0 - otherwise.        
*/

static 
SEL_TREE *tree_and(RANGE_OPT_PARAM *param, SEL_TREE *tree1, SEL_TREE *tree2)
{
  DBUG_ENTER("tree_and");
  if (!tree1)
    DBUG_RETURN(tree2);
  if (!tree2)
    DBUG_RETURN(tree1);
  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree1);
  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree2);
  if (tree1->type == SEL_TREE::MAYBE)
  {
    if (tree2->type == SEL_TREE::KEY)
      tree2->type=SEL_TREE::KEY_SMALLER;
    DBUG_RETURN(tree2);
  }
  if (tree2->type == SEL_TREE::MAYBE)
  {
    tree1->type=SEL_TREE::KEY_SMALLER;
    DBUG_RETURN(tree1);
  }

  if (!tree1->merges.is_empty())
    imerge_list_and_tree(param, &tree1->merges, tree2, TRUE);
  if (!tree2->merges.is_empty())
    imerge_list_and_tree(param, &tree2->merges, tree1, TRUE);
  if (and_range_trees(param, tree1, tree2, tree1))
    DBUG_RETURN(tree1);
  imerge_list_and_list(&tree1->merges, &tree2->merges);
  eliminate_single_tree_imerges(param, tree1);
  DBUG_RETURN(tree1);
}


/*
  Eliminate single tree imerges in a SEL_TREE objects

  SYNOPSIS
    eliminate_single_tree_imerges()
      param      Context info for the function
      tree       SEL_TREE where single tree imerges are to be eliminated 

  DESCRIPTION
    For each imerge in 'tree' that contains only one disjunct tree, i.e.
    for any imerge of the form m=rt, the function performs and operation
    the range part of tree, replaces rt the with the result of anding and
    removes imerge m from the the merge part of 'tree'.

  RETURN VALUE
    none          
*/

static
void eliminate_single_tree_imerges(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
  SEL_IMERGE *imerge;
  List<SEL_IMERGE> merges= tree->merges;
  List_iterator<SEL_IMERGE> it(merges);
  tree->merges.empty();
  while ((imerge= it++))
  {
    if (imerge->trees+1 == imerge->trees_next)
    {
      tree= tree_and(param, tree, *imerge->trees);
      it.remove();
    }
  }
  tree->merges= merges;
} 


/*
  For two trees check that there are indexes with ranges in both of them  
 
  SYNOPSIS
    sel_trees_have_common_keys()
      tree1           SEL_TREE for the first tree
      tree2           SEL_TREE for the second tree
      common_keys OUT bitmap of all indexes with ranges in both trees

  DESCRIPTION
    For two trees tree1 and tree1 the function checks if there are indexes
    in their range parts such that SEL_ARG trees are defined for them in the
    range parts of both trees. The function returns the bitmap of such 
    indexes in the parameter common_keys.

  RETURN 
    TRUE    if there are such indexes (common_keys is nor empty)
    FALSE   otherwise
*/

static
bool sel_trees_have_common_keys(SEL_TREE *tree1, SEL_TREE *tree2, 
                                key_map *common_keys)
{
  *common_keys= tree1->keys_map;
  common_keys->intersect(tree2->keys_map);
  return !common_keys->is_clear_all();
}


/*
  Check whether range parts of two trees can be ored for some indexes

  SYNOPSIS
    sel_trees_can_be_ored()
      param              Context info for the function
      tree1              SEL_TREE for the first tree
      tree2              SEL_TREE for the second tree
      common_keys IN/OUT IN: bitmap of all indexes with SEL_ARG in both trees
                        OUT: bitmap of all indexes that can be ored

  DESCRIPTION
    For two trees tree1 and tree2 and the bitmap common_keys containing
    bits for indexes that have SEL_ARG trees in range parts of both trees
    the function checks if there are indexes for which SEL_ARG trees can
    be ored. Two SEL_ARG trees for the same index can be ored if the most
    major components of the index used in these trees coincide. If the 
    SEL_ARG trees for an index cannot be ored the function clears the bit
    for this index in the bitmap common_keys.

    The function does not verify that indexes marked in common_keys really
    have SEL_ARG trees in both tree1 and tree2. It assumes that this is true.

  NOTE
    The function sel_trees_can_be_ored is usually used in pair with the
    function sel_trees_have_common_keys.

  RETURN
    TRUE    if there are indexes for which SEL_ARG trees can be ored 
    FALSE   otherwise
*/

static
bool sel_trees_can_be_ored(RANGE_OPT_PARAM* param,
                           SEL_TREE *tree1, SEL_TREE *tree2, 
                           key_map *common_keys)
{
  DBUG_ENTER("sel_trees_can_be_ored");
  if (!sel_trees_have_common_keys(tree1, tree2, common_keys))
    DBUG_RETURN(FALSE);
  int key_no;
  key_map::Iterator it(*common_keys);
  while ((key_no= it++) != key_map::Iterator::BITMAP_END)
  {
    DBUG_ASSERT(tree1->keys[key_no] && tree2->keys[key_no]);
    /* Trees have a common key, check if they refer to the same key part */
    if (tree1->keys[key_no]->part != tree2->keys[key_no]->part)
      common_keys->clear_bit(key_no);
  }
  DBUG_RETURN(!common_keys->is_clear_all());
}

/*
  Check whether range parts of two trees must be ored for some indexes

  SYNOPSIS
    sel_trees_must_be_ored()
      param              Context info for the function
      tree1              SEL_TREE for the first tree
      tree2              SEL_TREE for the second tree
      ordable_keys       bitmap of SEL_ARG trees that can be ored

  DESCRIPTION
    For two trees tree1 and tree2 the function checks whether they must be
    ored. The function assumes that the bitmap ordable_keys contains bits for
    those corresponding pairs of SEL_ARG trees from tree1 and tree2 that can
    be ored.
    We believe that tree1 and tree2 must be ored if any pair of SEL_ARG trees
    r1 and r2, such that r1 is from tree1 and r2 is from tree2 and both
    of them are marked in ordable_keys, can be merged.
    
  NOTE
    The function sel_trees_must_be_ored as a rule is used in pair with the
    function sel_trees_can_be_ored.

  RETURN
    TRUE    if there are indexes for which SEL_ARG trees must be ored 
    FALSE   otherwise
*/

static
bool sel_trees_must_be_ored(RANGE_OPT_PARAM* param,
                            SEL_TREE *tree1, SEL_TREE *tree2,
                            key_map oredable_keys)
{
  key_map tmp;
  DBUG_ENTER("sel_trees_must_be_ored");

  tmp= tree1->keys_map;
  tmp.merge(tree2->keys_map);
  tmp.subtract(oredable_keys);
  if (!tmp.is_clear_all())
    DBUG_RETURN(FALSE);

  int idx1, idx2;
  key_map::Iterator it1(oredable_keys);
  while ((idx1= it1++) != key_map::Iterator::BITMAP_END)
  {
    KEY_PART *key1_init= param->key[idx1]+tree1->keys[idx1]->part;
    KEY_PART *key1_end= param->key[idx1]+tree1->keys[idx1]->max_part_no;
    key_map::Iterator it2(oredable_keys);
    while ((idx2= it2++) != key_map::Iterator::BITMAP_END)
    {
      if (idx2 <= idx1)
        continue;
      
      KEY_PART *key2_init= param->key[idx2]+tree2->keys[idx2]->part;
      KEY_PART *key2_end= param->key[idx2]+tree2->keys[idx2]->max_part_no;
      KEY_PART *part1, *part2;
      for (part1= key1_init, part2= key2_init;
           part1 < key1_end && part2 < key2_end;
           part1++, part2++)
      { 
        if (!part1->field->eq(part2->field))
          DBUG_RETURN(FALSE);
      }
    }
  }
      
  DBUG_RETURN(TRUE);
}  


/*
  Remove the trees that are not suitable for record retrieval

  SYNOPSIS
    remove_nonrange_trees()
      param  Context info for the function
      tree   Tree to be processed, tree->type is KEY or KEY_SMALLER
 
  DESCRIPTION
    This function walks through tree->keys[] and removes the SEL_ARG* trees
    that are not "maybe" trees (*) and cannot be used to construct quick range
    selects.
    (*) - have type MAYBE or MAYBE_KEY. Perhaps we should remove trees of
          these types here as well.

    A SEL_ARG* tree cannot be used to construct quick select if it has
    tree->part != 0. (e.g. it could represent "keypart2 < const").
    
    Normally we allow construction of SEL_TREE objects that have SEL_ARG
    trees that do not allow quick range select construction.
    For example:
    for " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
    tree1= SEL_TREE { SEL_ARG{keypart1=1} }
    tree2= SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
                                               from this
    call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
                                   tree.

    Another example:
    tree3= SEL_TREE { SEL_ARG{key1part1 = 1} }
    tree4= SEL_TREE { SEL_ARG{key2part2 = 2} }  -- can't make quick range select
                                               from this
    call tree_or(tree3, tree4) -- creates a SEL_MERGE ot of which no index
    merge can be constructed, but it is potentially useful, as anding it with
    tree5= SEL_TREE { SEL_ARG{key2part1 = 3} } creates an index merge that
    represents the formula
      key1part1=1 AND key2part1=3 OR key2part1=3 AND key2part2=2 
    for which an index merge can be built. 

    Any final SEL_TREE may contain SEL_ARG trees for which no quick select
    can be built. Such SEL_ARG trees should be removed from the range part
    before different range scans are evaluated. Such SEL_ARG trees also should
    be removed from all range trees of each index merge before different
    possible index merge plans are evaluated. If after this removal one
    of the range trees in the index merge becomes empty the whole index merge
    must be discarded.
       
  RETURN
    0  Ok, some suitable trees left
    1  No tree->keys[] left.
*/

static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
  bool res= FALSE;
  for (uint i=0; i < param->keys; i++)
  {
    if (tree->keys[i])
    {
      if (tree->keys[i]->part)
      {
        tree->keys[i]= NULL;
        tree->keys_map.clear_bit(i);
      }
      else
        res= TRUE;
    }
  }
  return !res;
}


/*
  Build a SEL_TREE for a disjunction out of such trees for the disjuncts

  SYNOPSIS
    tree_or()
      param           Context info for the operation
      tree1           SEL_TREE for the first disjunct          
      tree2           SEL_TREE for the second disjunct

  DESCRIPTION
    This function builds a tree for the formula (A OR B) out of the trees
    tree1 and tree2 that has been built for the formulas A and B respectively.

    In a general case
      tree1 represents the formula RT1 AND MT1,
        where RT1=R1_1 AND ... AND R1_k1, MT1=M1_1 AND ... AND M1_l1;
      tree2 represents the formula RT2 AND MT2 
        where RT2=R2_1 AND ... AND R2_k2, MT2=M2_1 and ... and M2_l2.

    The function constructs the result tree according the formula
      (RT1 OR RT2) AND (MT1 OR RT1) AND (MT2 OR RT2) AND (MT1 OR MT2)
    that is equivalent to the formula (RT1 AND MT1) OR (RT2 AND MT2).

    To limit the number of produced imerges the function considers
    a weaker formula than the original one:
      (RT1 AND M1_1) OR (RT2 AND M2_1) 
    that is equivalent to:
      (RT1 OR RT2)                  (1)
        AND 
      (M1_1 OR M2_1)                (2)
        AND
      (M1_1 OR RT2)                 (3)
        AND
      (M2_1 OR RT1)                 (4)

    For the first conjunct (1) the function builds a tree with a range part
    and, possibly, one imerge. For the other conjuncts (2-4)the function
    produces sets of imerges. All constructed imerges are included into the
    result tree.
    
    For the formula (1) the function produces the tree representing a formula  
    of the structure RT [AND M], such that:
     - the range tree rt contains the result of oring SEL_ARG trees from rt1
       and rt2
     - the imerge m consists of two range trees rt1 and rt2.
    The imerge m is added if it's not true that rt1 and rt2 must be ored
    If rt1 and rt2 can't be ored rt is empty and only m is produced for (1).

    To produce imerges for the formula (2) the function calls the function
    imerge_list_or_list passing it the merge parts of tree1 and tree2 as
    parameters.

    To produce imerges for the formula (3) the function calls the function
    imerge_list_or_tree passing it the imerge m1_1 and the range tree rt2 as
    parameters. Similarly, to produce imerges for the formula (4) the function
    calls the function imerge_list_or_tree passing it the imerge m2_1 and the
    range tree rt1.

    If rt1 is empty then the trees for (1) and (4) are empty.
    If rt2 is empty then the trees for (1) and (3) are empty.
    If mt1 is empty then the trees for (2) and (3) are empty.
    If mt2 is empty then the trees for (2) and (4) are empty.

  RETURN
    The result tree for the operation if a success
    0 - otherwise
*/

static SEL_TREE *
tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
{
  DBUG_ENTER("tree_or");
  if (!tree1 || !tree2)
    DBUG_RETURN(0);
  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree2);
  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree1);
  if (tree1->type == SEL_TREE::MAYBE)
    DBUG_RETURN(tree1);				// Can't use this
  if (tree2->type == SEL_TREE::MAYBE)
    DBUG_RETURN(tree2);

  SEL_TREE *result= NULL;
  key_map result_keys;
  key_map ored_keys;
  SEL_TREE *rtree[2]= {NULL,NULL};
  SEL_IMERGE *imerge[2]= {NULL, NULL};
  bool no_ranges1= tree1->without_ranges();
  bool no_ranges2= tree2->without_ranges();
  bool no_merges1= tree1->without_imerges();
  bool no_merges2= tree2->without_imerges();
  if (!no_ranges1 && !no_merges2)
  {
    rtree[0]= new SEL_TREE(tree1, TRUE, param);
    imerge[1]= new SEL_IMERGE(tree2->merges.head(), 0, param);
  }
  if (!no_ranges2 && !no_merges1)
  {
    rtree[1]= new SEL_TREE(tree2, TRUE, param);
    imerge[0]= new SEL_IMERGE(tree1->merges.head(), 0, param);
  }
  bool no_imerge_from_ranges= FALSE;

  /* Build the range part of the tree for the formula (1) */ 
  if (sel_trees_can_be_ored(param, tree1, tree2, &ored_keys))
  {
    bool must_be_ored= sel_trees_must_be_ored(param, tree1, tree2, ored_keys);
    no_imerge_from_ranges= must_be_ored;

    if (no_imerge_from_ranges && no_merges1 && no_merges2)
    {
      /*
        Reuse tree1 as the result in simple cases. This reduces memory usage
        for e.g. "key IN (c1, ..., cN)" which produces a lot of ranges.
      */
      result= tree1;
      result->keys_map.clear_all();
    }
    else
    {
      if (!(result= new (param->mem_root) SEL_TREE(param->mem_root,
                                                   param->keys)))
      {
        DBUG_RETURN(result);
      }
    }

    key_map::Iterator it(ored_keys);
    int key_no;
    while ((key_no= it++) != key_map::Iterator::BITMAP_END)
    {
      SEL_ARG *key1= tree1->keys[key_no];
      SEL_ARG *key2= tree2->keys[key_no];
      if (!must_be_ored)
      {
        key1->incr_refs();
        key2->incr_refs();
      }
      if ((result->keys[key_no]= key_or(param, key1, key2)))
        result->keys_map.set_bit(key_no);
    }
    result->type= tree1->type;
  }
  else
  {
    if (!result && !(result= new (param->mem_root) SEL_TREE(param->mem_root,
                                                            param->keys)))
      DBUG_RETURN(result);
  }

  if (no_imerge_from_ranges && no_merges1 && no_merges2)
  {
    if (result->keys_map.is_clear_all())
      result->type= SEL_TREE::ALWAYS;
    DBUG_RETURN(result);
  }

  SEL_IMERGE *imerge_from_ranges;
  if (!(imerge_from_ranges= new SEL_IMERGE()))
    result= NULL;
  else if (!no_ranges1 && !no_ranges2 && !no_imerge_from_ranges)
  {
    /* Build the imerge part of the tree for the formula (1) */
    SEL_TREE *rt1= tree1;
    SEL_TREE *rt2= tree2;
    if (no_merges1)
      rt1= new SEL_TREE(tree1, TRUE, param);
    if (no_merges2)
      rt2= new SEL_TREE(tree2, TRUE, param);
    if (!rt1 || !rt2 ||
        result->merges.push_back(imerge_from_ranges) ||
        imerge_from_ranges->or_sel_tree(param, rt1) ||
        imerge_from_ranges->or_sel_tree(param, rt2))
      result= NULL;
  }
  if (!result)
    DBUG_RETURN(result);

  result->type= tree1->type;

  if (!no_merges1 && !no_merges2 && 
      !imerge_list_or_list(param, &tree1->merges, &tree2->merges))
  {
    /* Build the imerges for the formula (2) */
    imerge_list_and_list(&result->merges, &tree1->merges);
  }

  /* Build the imerges for the formulas (3) and (4) */
  for (uint i=0; i < 2; i++)
  {
    List<SEL_IMERGE> merges;
    SEL_TREE *rt= rtree[i];
    SEL_IMERGE *im= imerge[1-i];
    
    if (rt && im && !merges.push_back(im) && 
        !imerge_list_or_tree(param, &merges, rt))
      imerge_list_and_list(&result->merges, &merges);
  }
 
  DBUG_RETURN(result);
}


/* And key trees where key1->part < key2 -> part */

static SEL_ARG *
and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, 
             uint clone_flag)
{
  SEL_ARG *next;
  ulong use_count=key1->use_count;

  if (key1->elements != 1)
  {
    key2->use_count+=key1->elements-1; //psergey: why we don't count that key1 has n-k-p?
    key2->increment_use_count((int) key1->elements-1);
  }
  if (key1->type == SEL_ARG::MAYBE_KEY)
  {
    key1->right= key1->left= &null_element;
    key1->next= key1->prev= 0;
  }
  for (next=key1->first(); next ; next=next->next)
  {
    if (next->next_key_part)
    {
      SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
      if (tmp && tmp->type == SEL_ARG::IMPOSSIBLE)
      {
	key1=key1->tree_delete(next);
	continue;
      }
      next->next_key_part=tmp;
      if (use_count)
	next->increment_use_count(use_count);
      if (param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
        break;
    }
    else
      next->next_key_part=key2;
  }
  if (!key1)
    return &null_element;			// Impossible ranges
  key1->use_count++;
  key1->max_part_no= MY_MAX(key2->max_part_no, key2->part+1);
  return key1;
}


/*
  Produce a SEL_ARG graph that represents "key1 AND key2"

  SYNOPSIS
    key_and()
      param   Range analysis context (needed to track if we have allocated
              too many SEL_ARGs)
      key1    First argument, root of its RB-tree
      key2    Second argument, root of its RB-tree

  RETURN
    RB-tree root of the resulting SEL_ARG graph.
    NULL if the result of AND operation is an empty interval {0}.
*/

static SEL_ARG *
key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
{
  if (!key1)
    return key2;
  if (!key2)
    return key1;
  if (key1->part != key2->part)
  {
    if (key1->part > key2->part)
    {
      swap_variables(SEL_ARG *, key1, key2);
      clone_flag=swap_clone_flag(clone_flag);
    }
    // key1->part < key2->part
    key1->use_count--;
    if (key1->use_count > 0)
      if (!(key1= key1->clone_tree(param)))
	return 0;				// OOM
    return and_all_keys(param, key1, key2, clone_flag);
  }

  if (((clone_flag & CLONE_KEY2_MAYBE) &&
       !(clone_flag & CLONE_KEY1_MAYBE) &&
       key2->type != SEL_ARG::MAYBE_KEY) ||
      key1->type == SEL_ARG::MAYBE_KEY)
  {						// Put simple key in key2
    swap_variables(SEL_ARG *, key1, key2);
    clone_flag=swap_clone_flag(clone_flag);
  }

  /* If one of the key is MAYBE_KEY then the found region may be smaller */
  if (key2->type == SEL_ARG::MAYBE_KEY)
  {
    if (key1->use_count > 1)
    {
      key1->use_count--;
      if (!(key1=key1->clone_tree(param)))
	return 0;				// OOM
      key1->use_count++;
    }
    if (key1->type == SEL_ARG::MAYBE_KEY)
    {						// Both are maybe key
      key1->next_key_part=key_and(param, key1->next_key_part, 
                                  key2->next_key_part, clone_flag);
      if (key1->next_key_part &&
	  key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
	return key1;
    }
    else
    {
      key1->maybe_smaller();
      if (key2->next_key_part)
      {
	key1->use_count--;			// Incremented in and_all_keys
	return and_all_keys(param, key1, key2, clone_flag);
      }
      key2->use_count--;			// Key2 doesn't have a tree
    }
    return key1;
  }

  if ((key1->min_flag | key2->min_flag) & GEOM_FLAG)
  {
    /* TODO: why not leave one of the trees? */
    key1->free_tree();
    key2->free_tree();
    return 0;					// Can't optimize this
  }

  key1->use_count--;
  key2->use_count--;
  SEL_ARG *e1=key1->first(), *e2=key2->first(), *new_tree=0;
  uint max_part_no= MY_MAX(key1->max_part_no, key2->max_part_no);

  while (e1 && e2)
  {
    int cmp=e1->cmp_min_to_min(e2);
    if (cmp < 0)
    {
      if (get_range(&e1,&e2,key1))
	continue;
    }
    else if (get_range(&e2,&e1,key2))
      continue;
    SEL_ARG *next=key_and(param, e1->next_key_part, e2->next_key_part,
                          clone_flag);
    e1->incr_refs();
    e2->incr_refs();
    if (!next || next->type != SEL_ARG::IMPOSSIBLE)
    {
      SEL_ARG *new_arg= e1->clone_and(param->thd, e2);
      if (!new_arg)
	return &null_element;			// End of memory
      new_arg->next_key_part=next;
      if (!new_tree)
      {
	new_tree=new_arg;
      }
      else
	new_tree=new_tree->insert(new_arg);
    }
    if (e1->cmp_max_to_max(e2) < 0)
      e1=e1->next;				// e1 can't overlapp next e2
    else
      e2=e2->next;
  }
  key1->free_tree();
  key2->free_tree();
  if (!new_tree)
    return &null_element;			// Impossible range
  new_tree->max_part_no= max_part_no;
  return new_tree;
}


static bool
get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1)
{
  (*e1)=root1->find_range(*e2);			// first e1->min < e2->min
  if ((*e1)->cmp_max_to_min(*e2) < 0)
  {
    if (!((*e1)=(*e1)->next))
      return 1;
    if ((*e1)->cmp_min_to_max(*e2) > 0)
    {
      (*e2)=(*e2)->next;
      return 1;
    }
  }
  return 0;
}


/**
   Combine two range expression under a common OR. On a logical level, the
   transformation is key_or( expr1, expr2 ) => expr1 OR expr2.

   Both expressions are assumed to be in the SEL_ARG format. In a logic sense,
   theformat is reminiscent of DNF, since an expression such as the following

   ( 1 < kp1 < 10 AND p1 ) OR ( 10 <= kp2 < 20 AND p2 )

   where there is a key consisting of keyparts ( kp1, kp2, ..., kpn ) and p1
   and p2 are valid SEL_ARG expressions over keyparts kp2 ... kpn, is a valid
   SEL_ARG condition. The disjuncts appear ordered by the minimum endpoint of
   the first range and ranges must not overlap. It follows that they are also
   ordered by maximum endpoints. Thus

   ( 1 < kp1 <= 2 AND ( kp2 = 2 OR kp2 = 3 ) ) OR kp1 = 3

   Is a a valid SER_ARG expression for a key of at least 2 keyparts.
   
   For simplicity, we will assume that expr2 is a single range predicate,
   i.e. on the form ( a < x < b AND ... ). It is easy to generalize to a
   disjunction of several predicates by subsequently call key_or for each
   disjunct.

   The algorithm iterates over each disjunct of expr1, and for each disjunct
   where the first keypart's range overlaps with the first keypart's range in
   expr2:
   
   If the predicates are equal for the rest of the keyparts, or if there are
   no more, the range in expr2 has its endpoints copied in, and the SEL_ARG
   node in expr2 is deallocated. If more ranges became connected in expr1, the
   surplus is also dealocated. If they differ, two ranges are created.
   
   - The range leading up to the overlap. Empty if endpoints are equal.

   - The overlapping sub-range. May be the entire range if they are equal.

   Finally, there may be one more range if expr2's first keypart's range has a
   greater maximum endpoint than the last range in expr1.

   For the overlapping sub-range, we recursively call key_or. Thus in order to
   compute key_or of

     (1) ( 1 < kp1 < 10 AND 1 < kp2 < 10 ) 

     (2) ( 2 < kp1 < 20 AND 4 < kp2 < 20 )

   We create the ranges 1 < kp <= 2, 2 < kp1 < 10, 10 <= kp1 < 20. For the
   first one, we simply hook on the condition for the second keypart from (1)
   : 1 < kp2 < 10. For the second range 2 < kp1 < 10, key_or( 1 < kp2 < 10, 4
   < kp2 < 20 ) is called, yielding 1 < kp2 < 20. For the last range, we reuse
   the range 4 < kp2 < 20 from (2) for the second keypart. The result is thus
   
   ( 1  <  kp1 <= 2 AND 1 < kp2 < 10 ) OR
   ( 2  <  kp1 < 10 AND 1 < kp2 < 20 ) OR
   ( 10 <= kp1 < 20 AND 4 < kp2 < 20 )
*/
static SEL_ARG *
key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
{
  if (!key1)
  {
    if (key2)
    {
      key2->use_count--;
      key2->free_tree();
    }
    return 0;
  }
  if (!key2)
  {
    key1->use_count--;
    key1->free_tree();
    return 0;
  }
  key1->use_count--;
  key2->use_count--;

  if (key1->part != key2->part || 
      (key1->min_flag | key2->min_flag) & GEOM_FLAG)
  {
    key1->free_tree();
    key2->free_tree();
    return 0;                                   // Can't optimize this
  }

  // If one of the key is MAYBE_KEY then the found region may be bigger
  if (key1->type == SEL_ARG::MAYBE_KEY)
  {
    key2->free_tree();
    key1->use_count++;
    return key1;
  }
  if (key2->type == SEL_ARG::MAYBE_KEY)
  {
    key1->free_tree();
    key2->use_count++;
    return key2;
  }

  if (key1->use_count > 0)
  {
    if (key2->use_count == 0 || key1->elements > key2->elements)
    {
      swap_variables(SEL_ARG *,key1,key2);
    }
    if (key1->use_count > 0 && !(key1=key1->clone_tree(param)))
      return 0;                                 // OOM
  }

  // Add tree at key2 to tree at key1
  bool key2_shared=key2->use_count != 0;
  key1->maybe_flag|=key2->maybe_flag;

  /*
    Notation for illustrations used in the rest of this function: 

      Range: [--------]
             ^        ^
             start    stop

      Two overlapping ranges:
        [-----]               [----]            [--]
            [---]     or    [---]       or   [-------]

      Ambiguity: *** 
        The range starts or stops somewhere in the "***" range.
        Example: a starts before b and may end before/the same plase/after b
        a: [----***]
        b:   [---]

      Adjacent ranges:
        Ranges that meet but do not overlap. Example: a = "x < 3", b = "x >= 3"
        a: ----]
        b:      [----
   */

  uint max_part_no= MY_MAX(key1->max_part_no, key2->max_part_no);

  for (key2=key2->first(); key2; )
  {
    /*
      key1 consists of one or more ranges. tmp is the range currently
      being handled.

      initialize tmp to the latest range in key1 that starts the same
      place or before the range in key2 starts

      key2:           [------]
      key1: [---] [-----] [----]
                  ^
                  tmp
    */
    SEL_ARG *tmp=key1->find_range(key2);

    /*
      Used to describe how two key values are positioned compared to
      each other. Consider key_value_a.<cmp_func>(key_value_b):

        -2: key_value_a is smaller than key_value_b, and they are adjacent
        -1: key_value_a is smaller than key_value_b (not adjacent)
         0: the key values are equal
         1: key_value_a is bigger than key_value_b (not adjacent)
        -2: key_value_a is bigger than key_value_b, and they are adjacent

      Example: "cmp= tmp->cmp_max_to_min(key2)"

      key2:         [--------            (10 <= x ...)
      tmp:    -----]                      (... x <  10) => cmp==-2
      tmp:    ----]                       (... x <=  9) => cmp==-1
      tmp:    ------]                     (... x  = 10) => cmp== 0
      tmp:    --------]                   (... x <= 12) => cmp== 1
      (cmp == 2 does not make sense for cmp_max_to_min())
     */
    int cmp= 0;

    if (!tmp)
    {
      /*
        The range in key2 starts before the first range in key1. Use
        the first range in key1 as tmp.

        key2:     [--------]
        key1:            [****--] [----]   [-------]
                         ^
                         tmp
      */
      tmp=key1->first();
      cmp= -1;
    }
    else if ((cmp= tmp->cmp_max_to_min(key2)) < 0)
    {
      /*
        This is the case:
        key2:          [-------]
        tmp:   [----**]
       */
      SEL_ARG *next=tmp->next;
      if (cmp == -2 && eq_tree(tmp->next_key_part,key2->next_key_part))
      {
        /*
          Adjacent (cmp==-2) and equal next_key_parts => ranges can be merged

          This is the case:
          key2:          [-------]
          tmp:     [----]

          Result:
          key2:    [-------------]     => inserted into key1 below
          tmp:                         => deleted
        */
        SEL_ARG *key2_next=key2->next;
        if (key2_shared)
        {
          if (!(key2=new SEL_ARG(*key2)))
            return 0;           // out of memory
          key2->increment_use_count(key1->use_count+1);
          key2->next=key2_next;                 // New copy of key2
        }

        key2->copy_min(tmp);
        if (!(key1=key1->tree_delete(tmp)))
        {                                       // Only one key in tree
          key1=key2;
          key1->make_root();
          key2=key2_next;
          break;
        }
      }
      if (!(tmp=next)) // Move to next range in key1. Now tmp.min > key2.min
        break;         // No more ranges in key1. Copy rest of key2
    }

    if (cmp < 0)
    {
      /*
        This is the case:
        key2:  [--***]
        tmp:       [----]
      */
      int tmp_cmp;
      if ((tmp_cmp=tmp->cmp_min_to_max(key2)) > 0)
      {
        /*
          This is the case:
          key2:  [------**]
          tmp:             [----]
        */
        if (tmp_cmp == 2 && eq_tree(tmp->next_key_part,key2->next_key_part))
        {
          /*
            Adjacent ranges with equal next_key_part. Merge like this:

            This is the case:
            key2:    [------]
            tmp:             [-----]

            Result:
            key2:    [------]
            tmp:     [-------------]

            Then move on to next key2 range.
          */
          tmp->copy_min_to_min(key2);
          key1->merge_flags(key2);
          if (tmp->min_flag & NO_MIN_RANGE &&
              tmp->max_flag & NO_MAX_RANGE)
          {
            if (key1->maybe_flag)
              return new SEL_ARG(SEL_ARG::MAYBE_KEY);
            return 0;
          }
          key2->increment_use_count(-1);        // Free not used tree
          key2=key2->next;
          continue;
        }
        else
        {
          /*
            key2 not adjacent to tmp or has different next_key_part.
            Insert into key1 and move to next range in key2
            
            This is the case:
            key2:  [------**]
            tmp:             [----]

            Result:
            key1_  [------**][----]
                   ^         ^
                   insert    tmp
          */
          SEL_ARG *next=key2->next;
          if (key2_shared)
          {
            SEL_ARG *cpy= new SEL_ARG(*key2);   // Must make copy
            if (!cpy)
              return 0;                         // OOM
            key1=key1->insert(cpy);
            key2->increment_use_count(key1->use_count+1);
          }
          else
            key1=key1->insert(key2);            // Will destroy key2_root
          key2=next;
          continue;
        }
      }
    }

    /*
      The ranges in tmp and key2 are overlapping:

      key2:          [----------] 
      tmp:        [*****-----*****]

      Corollary: tmp.min <= key2.max
    */
    if (eq_tree(tmp->next_key_part,key2->next_key_part))
    {
      // Merge overlapping ranges with equal next_key_part
      if (tmp->is_same(key2))
      {
        /*
          Found exact match of key2 inside key1.
          Use the relevant range in key1.
        */
        tmp->merge_flags(key2);                 // Copy maybe flags
        key2->increment_use_count(-1);          // Free not used tree
      }
      else
      {
        SEL_ARG *last= tmp;
        SEL_ARG *first= tmp;

        /*
          Find the last range in key1 that overlaps key2 and
          where all ranges first...last have the same next_key_part as
          key2.

          key2:  [****----------------------*******]
          key1:     [--]  [----] [---]  [-----] [xxxx]
                    ^                   ^       ^
                    first               last    different next_key_part

          Since key2 covers them, the ranges between first and last
          are merged into one range by deleting first...last-1 from
          the key1 tree. In the figure, this applies to first and the
          two consecutive ranges. The range of last is then extended:
            * last.min: Set to MY_MIN(key2.min, first.min)
            * last.max: If there is a last->next that overlaps key2 (i.e.,
                        last->next has a different next_key_part):
                                        Set adjacent to last->next.min
                        Otherwise:      Set to MY_MAX(key2.max, last.max)

          Result:
          key2:  [****----------------------*******]
                    [--]  [----] [---]                   => deleted from key1
          key1:  [**------------------------***][xxxx]
                 ^                              ^
                 tmp=last                       different next_key_part
        */
        while (last->next && last->next->cmp_min_to_max(key2) <= 0 &&
               eq_tree(last->next->next_key_part,key2->next_key_part))
        {
          /*
            last->next is covered by key2 and has same next_key_part.
            last can be deleted
          */
          SEL_ARG *save=last;
          last=last->next;
          key1=key1->tree_delete(save);
        }
        // Redirect tmp to last which will cover the entire range
        tmp= last;

        /*
          We need the minimum endpoint of first so we can compare it
          with the minimum endpoint of the enclosing key2 range.
        */
        last->copy_min(first);
        bool full_range= last->copy_min(key2);
        if (!full_range)
        {
          if (last->next && key2->cmp_max_to_min(last->next) >= 0)
          {
            /*
              This is the case:
              key2:    [-------------]
              key1:  [***------]  [xxxx]
                     ^            ^
                     last         different next_key_part

              Extend range of last up to last->next:
              key2:    [-------------]
              key1:  [***--------][xxxx]
            */
            last->copy_min_to_max(last->next);
          }
          else
            /*
              This is the case:
              key2:    [--------*****]
              key1:  [***---------]    [xxxx]
                     ^                 ^
                     last              different next_key_part

              Extend range of last up to MY_MAX(last.max, key2.max):
              key2:    [--------*****]
              key1:  [***----------**] [xxxx]
             */
            full_range= last->copy_max(key2);
        }
        if (full_range)
        {                                       // Full range
          key1->free_tree();
          for (; key2 ; key2=key2->next)
            key2->increment_use_count(-1);      // Free not used tree
          if (key1->maybe_flag)
            return new SEL_ARG(SEL_ARG::MAYBE_KEY);
          return 0;
        }
      }
    }

    if (cmp >= 0 && tmp->cmp_min_to_min(key2) < 0)
    {
      /*
        This is the case ("cmp>=0" means that tmp.max >= key2.min):
        key2:              [----]
        tmp:     [------------*****]
      */

      if (!tmp->next_key_part)
      {
        if (key2->use_count)
	{
	  SEL_ARG *key2_cpy= new SEL_ARG(*key2);
          if (key2_cpy)
            return 0;
          key2= key2_cpy;
	}
        /*
          tmp->next_key_part is empty: cut the range that is covered
          by tmp from key2. 
          Reason: (key2->next_key_part OR tmp->next_key_part) will be
          empty and therefore equal to tmp->next_key_part. Thus, this
          part of the key2 range is completely covered by tmp.
        */
        if (tmp->cmp_max_to_max(key2) >= 0)
        {
          /*
            tmp covers the entire range in key2. 
            key2:              [----]
            tmp:     [-----------------]

            Move on to next range in key2
          */
          key2->increment_use_count(-1); // Free not used tree
          key2=key2->next;
          continue;
        }
        else
        {
          /*
            This is the case:
            key2:           [-------]
            tmp:     [---------]

            Result:
            key2:               [---]
            tmp:     [---------]
          */
          key2->copy_max_to_min(tmp);
          continue;
        }
      }

      /*
        The ranges are overlapping but have not been merged because
        next_key_part of tmp and key2 differ. 
        key2:              [----]
        tmp:     [------------*****]

        Split tmp in two where key2 starts:
        key2:              [----]
        key1:    [--------][--*****]
                 ^         ^
                 insert    tmp
      */
      SEL_ARG *new_arg=tmp->clone_first(key2);
      if (!new_arg)
        return 0;                               // OOM
      if ((new_arg->next_key_part= tmp->next_key_part))
        new_arg->increment_use_count(key1->use_count+1);
      tmp->copy_min_to_min(key2);
      key1=key1->insert(new_arg);
    } // tmp.min >= key2.min due to this if()

    /*
      Now key2.min <= tmp.min <= key2.max:
      key2:   [---------]
      tmp:    [****---*****]
     */
    SEL_ARG key2_cpy(*key2); // Get copy we can modify
    for (;;)
    {
      if (tmp->cmp_min_to_min(&key2_cpy) > 0)
      {
        /*
          This is the case:
          key2_cpy:    [------------]
          key1:                 [-*****]
                                ^
                                tmp
                             
          Result:
          key2_cpy:             [---]
          key1:        [-------][-*****]
                       ^        ^
                       insert   tmp
         */
        SEL_ARG *new_arg=key2_cpy.clone_first(tmp);
        if (!new_arg)
          return 0; // OOM
        if ((new_arg->next_key_part=key2_cpy.next_key_part))
          new_arg->increment_use_count(key1->use_count+1);
        key1=key1->insert(new_arg);
        key2_cpy.copy_min_to_min(tmp);
      } 
      // Now key2_cpy.min == tmp.min

      if ((cmp= tmp->cmp_max_to_max(&key2_cpy)) <= 0)
      {
        /*
          tmp.max <= key2_cpy.max:
          key2_cpy:   a)  [-------]    or b)     [----]
          tmp:            [----]                 [----]

          Steps:
           1) Update next_key_part of tmp: OR it with key2_cpy->next_key_part.
           2) If case a: Insert range [tmp.max, key2_cpy.max] into key1 using
                         next_key_part of key2_cpy

           Result:
           key1:      a)  [----][-]    or b)     [----]
         */
        tmp->maybe_flag|= key2_cpy.maybe_flag;
        key2_cpy.increment_use_count(key1->use_count+1);
        tmp->next_key_part= key_or(param, tmp->next_key_part,
                                   key2_cpy.next_key_part);

        if (!cmp)
          break;                     // case b: done with this key2 range

        // Make key2_cpy the range [tmp.max, key2_cpy.max]
        key2_cpy.copy_max_to_min(tmp);
        if (!(tmp=tmp->next))
        {
          /*
            No more ranges in key1. Insert key2_cpy and go to "end"
            label to insert remaining ranges in key2 if any.
          */
          SEL_ARG *tmp2= new SEL_ARG(key2_cpy);
          if (!tmp2)
            return 0; // OOM
          key1=key1->insert(tmp2);
          key2=key2->next;
          goto end;
        }
        if (tmp->cmp_min_to_max(&key2_cpy) > 0)
        {
          /*
            The next range in key1 does not overlap with key2_cpy.
            Insert this range into key1 and move on to the next range
            in key2.
          */
          SEL_ARG *tmp2= new SEL_ARG(key2_cpy);
          if (!tmp2)
            return 0;                           // OOM
          key1=key1->insert(tmp2);
          break;
        }
        /*
          key2_cpy overlaps with the next range in key1 and the case
          is now "key2.min <= tmp.min <= key2.max". Go back to for(;;)
          to handle this situation.
        */
        continue;
      }
      else
      {
        /*
          This is the case:
          key2_cpy:   [-------]
          tmp:        [------------]

          Result:
          key1:       [-------][---]
                      ^        ^
                      new_arg  tmp
          Steps:
           0) If tmp->next_key_part is empty: do nothing. Reason:
              (key2_cpy->next_key_part OR tmp->next_key_part) will be
              empty and therefore equal to tmp->next_key_part. Thus,
              the range in key2_cpy is completely covered by tmp
           1) Make new_arg with range [tmp.min, key2_cpy.max].
              new_arg->next_key_part is OR between next_key_part
              of tmp and key2_cpy
           2) Make tmp the range [key2.max, tmp.max]
           3) Insert new_arg into key1
        */
        if (!tmp->next_key_part) // Step 0
        {
          key2_cpy.increment_use_count(-1);     // Free not used tree
          break;
        }
        SEL_ARG *new_arg=tmp->clone_last(&key2_cpy);
        if (!new_arg)
          return 0; // OOM
        tmp->copy_max_to_min(&key2_cpy);
        tmp->increment_use_count(key1->use_count+1);
        /* Increment key count as it may be used for next loop */
        key2_cpy.increment_use_count(1);
        new_arg->next_key_part= key_or(param, tmp->next_key_part,
                                       key2_cpy.next_key_part);
        key1=key1->insert(new_arg);
        break;
      }
    }
    // Move on to next range in key2
    key2=key2->next;                            
  }

end:
  /*
    Add key2 ranges that are non-overlapping with and higher than the
    highest range in key1.
  */
  while (key2)
  {
    SEL_ARG *next=key2->next;
    if (key2_shared)
    {
      SEL_ARG *tmp=new SEL_ARG(*key2);          // Must make copy
      if (!tmp)
        return 0;
      key2->increment_use_count(key1->use_count+1);
      key1=key1->insert(tmp);
    }
    else
      key1=key1->insert(key2);                  // Will destroy key2_root
    key2=next;
  }
  key1->use_count++;

  key1->max_part_no= max_part_no;
  return key1;
}


/* Compare if two trees are equal */

static bool eq_tree(SEL_ARG* a,SEL_ARG *b)
{
  if (a == b)
    return 1;
  if (!a || !b || !a->is_same(b))
    return 0;
  if (a->left != &null_element && b->left != &null_element)
  {
    if (!eq_tree(a->left,b->left))
      return 0;
  }
  else if (a->left != &null_element || b->left != &null_element)
    return 0;
  if (a->right != &null_element && b->right != &null_element)
  {
    if (!eq_tree(a->right,b->right))
      return 0;
  }
  else if (a->right != &null_element || b->right != &null_element)
    return 0;
  if (a->next_key_part != b->next_key_part)
  {						// Sub range
    if (!a->next_key_part != !b->next_key_part ||
	!eq_tree(a->next_key_part, b->next_key_part))
      return 0;
  }
  return 1;
}


SEL_ARG *
SEL_ARG::insert(SEL_ARG *key)
{
  SEL_ARG *element,**UNINIT_VAR(par),*UNINIT_VAR(last_element);

  for (element= this; element != &null_element ; )
  {
    last_element=element;
    if (key->cmp_min_to_min(element) > 0)
    {
      par= &element->right; element= element->right;
    }
    else
    {
      par = &element->left; element= element->left;
    }
  }
  *par=key;
  key->parent=last_element;
	/* Link in list */
  if (par == &last_element->left)
  {
    key->next=last_element;
    if ((key->prev=last_element->prev))
      key->prev->next=key;
    last_element->prev=key;
  }
  else
  {
    if ((key->next=last_element->next))
      key->next->prev=key;
    key->prev=last_element;
    last_element->next=key;
  }
  key->left=key->right= &null_element;
  SEL_ARG *root=rb_insert(key);			// rebalance tree
  root->use_count=this->use_count;		// copy root info
  root->elements= this->elements+1;
  root->maybe_flag=this->maybe_flag;
  return root;
}


/*
** Find best key with min <= given key
** Because the call context this should never return 0 to get_range
*/

SEL_ARG *
SEL_ARG::find_range(SEL_ARG *key)
{
  SEL_ARG *element=this,*found=0;

  for (;;)
  {
    if (element == &null_element)
      return found;
    int cmp=element->cmp_min_to_min(key);
    if (cmp == 0)
      return element;
    if (cmp < 0)
    {
      found=element;
      element=element->right;
    }
    else
      element=element->left;
  }
}


/*
  Remove a element from the tree

  SYNOPSIS
    tree_delete()
    key		Key that is to be deleted from tree (this)

  NOTE
    This also frees all sub trees that is used by the element

  RETURN
    root of new tree (with key deleted)
*/

SEL_ARG *
SEL_ARG::tree_delete(SEL_ARG *key)
{
  enum leaf_color remove_color;
  SEL_ARG *root,*nod,**par,*fix_par;
  DBUG_ENTER("tree_delete");

  root=this;
  this->parent= 0;

  /* Unlink from list */
  if (key->prev)
    key->prev->next=key->next;
  if (key->next)
    key->next->prev=key->prev;
  key->increment_use_count(-1);
  if (!key->parent)
    par= &root;
  else
    par=key->parent_ptr();

  if (key->left == &null_element)
  {
    *par=nod=key->right;
    fix_par=key->parent;
    if (nod != &null_element)
      nod->parent=fix_par;
    remove_color= key->color;
  }
  else if (key->right == &null_element)
  {
    *par= nod=key->left;
    nod->parent=fix_par=key->parent;
    remove_color= key->color;
  }
  else
  {
    SEL_ARG *tmp=key->next;			// next bigger key (exist!)
    nod= *tmp->parent_ptr()= tmp->right;	// unlink tmp from tree
    fix_par=tmp->parent;
    if (nod != &null_element)
      nod->parent=fix_par;
    remove_color= tmp->color;

    tmp->parent=key->parent;			// Move node in place of key
    (tmp->left=key->left)->parent=tmp;
    if ((tmp->right=key->right) != &null_element)
      tmp->right->parent=tmp;
    tmp->color=key->color;
    *par=tmp;
    if (fix_par == key)				// key->right == key->next
      fix_par=tmp;				// new parent of nod
  }

  if (root == &null_element)
    DBUG_RETURN(0);				// Maybe root later
  if (remove_color == BLACK)
    root=rb_delete_fixup(root,nod,fix_par);
  test_rb_tree(root,root->parent);

  root->use_count=this->use_count;		// Fix root counters
  root->elements=this->elements-1;
  root->maybe_flag=this->maybe_flag;
  DBUG_RETURN(root);
}


	/* Functions to fix up the tree after insert and delete */

static void left_rotate(SEL_ARG **root,SEL_ARG *leaf)
{
  SEL_ARG *y=leaf->right;
  leaf->right=y->left;
  if (y->left != &null_element)
    y->left->parent=leaf;
  if (!(y->parent=leaf->parent))
    *root=y;
  else
    *leaf->parent_ptr()=y;
  y->left=leaf;
  leaf->parent=y;
}

static void right_rotate(SEL_ARG **root,SEL_ARG *leaf)
{
  SEL_ARG *y=leaf->left;
  leaf->left=y->right;
  if (y->right != &null_element)
    y->right->parent=leaf;
  if (!(y->parent=leaf->parent))
    *root=y;
  else
    *leaf->parent_ptr()=y;
  y->right=leaf;
  leaf->parent=y;
}


SEL_ARG *
SEL_ARG::rb_insert(SEL_ARG *leaf)
{
  SEL_ARG *y,*par,*par2,*root;
  root= this; root->parent= 0;

  leaf->color=RED;
  while (leaf != root && (par= leaf->parent)->color == RED)
  {					// This can't be root or 1 level under
    if (par == (par2= leaf->parent->parent)->left)
    {
      y= par2->right;
      if (y->color == RED)
      {
	par->color=BLACK;
	y->color=BLACK;
	leaf=par2;
	leaf->color=RED;		/* And the loop continues */
      }
      else
      {
	if (leaf == par->right)
	{
	  left_rotate(&root,leaf->parent);
	  par=leaf;			/* leaf is now parent to old leaf */
	}
	par->color=BLACK;
	par2->color=RED;
	right_rotate(&root,par2);
	break;
      }
    }
    else
    {
      y= par2->left;
      if (y->color == RED)
      {
	par->color=BLACK;
	y->color=BLACK;
	leaf=par2;
	leaf->color=RED;		/* And the loop continues */
      }
      else
      {
	if (leaf == par->left)
	{
	  right_rotate(&root,par);
	  par=leaf;
	}
	par->color=BLACK;
	par2->color=RED;
	left_rotate(&root,par2);
	break;
      }
    }
  }
  root->color=BLACK;
  test_rb_tree(root,root->parent);
  return root;
}


SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key,SEL_ARG *par)
{
  SEL_ARG *x,*w;
  root->parent=0;

  x= key;
  while (x != root && x->color == SEL_ARG::BLACK)
  {
    if (x == par->left)
    {
      w=par->right;
      if (w->color == SEL_ARG::RED)
      {
	w->color=SEL_ARG::BLACK;
	par->color=SEL_ARG::RED;
	left_rotate(&root,par);
	w=par->right;
      }
      if (w->left->color == SEL_ARG::BLACK && w->right->color == SEL_ARG::BLACK)
      {
	w->color=SEL_ARG::RED;
	x=par;
      }
      else
      {
	if (w->right->color == SEL_ARG::BLACK)
	{
	  w->left->color=SEL_ARG::BLACK;
	  w->color=SEL_ARG::RED;
	  right_rotate(&root,w);
	  w=par->right;
	}
	w->color=par->color;
	par->color=SEL_ARG::BLACK;
	w->right->color=SEL_ARG::BLACK;
	left_rotate(&root,par);
	x=root;
	break;
      }
    }
    else
    {
      w=par->left;
      if (w->color == SEL_ARG::RED)
      {
	w->color=SEL_ARG::BLACK;
	par->color=SEL_ARG::RED;
	right_rotate(&root,par);
	w=par->left;
      }
      if (w->right->color == SEL_ARG::BLACK && w->left->color == SEL_ARG::BLACK)
      {
	w->color=SEL_ARG::RED;
	x=par;
      }
      else
      {
	if (w->left->color == SEL_ARG::BLACK)
	{
	  w->right->color=SEL_ARG::BLACK;
	  w->color=SEL_ARG::RED;
	  left_rotate(&root,w);
	  w=par->left;
	}
	w->color=par->color;
	par->color=SEL_ARG::BLACK;
	w->left->color=SEL_ARG::BLACK;
	right_rotate(&root,par);
	x=root;
	break;
      }
    }
    par=x->parent;
  }
  x->color=SEL_ARG::BLACK;
  return root;
}


	/* Test that the properties for a red-black tree hold */

#ifdef EXTRA_DEBUG
int test_rb_tree(SEL_ARG *element,SEL_ARG *parent)
{
  int count_l,count_r;

  if (element == &null_element)
    return 0;					// Found end of tree
  if (element->parent != parent)
  {
    sql_print_error("Wrong tree: Parent doesn't point at parent");
    return -1;
  }
  if (element->color == SEL_ARG::RED &&
      (element->left->color == SEL_ARG::RED ||
       element->right->color == SEL_ARG::RED))
  {
    sql_print_error("Wrong tree: Found two red in a row");
    return -1;
  }
  if (element->left == element->right && element->left != &null_element)
  {						// Dummy test
    sql_print_error("Wrong tree: Found right == left");
    return -1;
  }
  count_l=test_rb_tree(element->left,element);
  count_r=test_rb_tree(element->right,element);
  if (count_l >= 0 && count_r >= 0)
  {
    if (count_l == count_r)
      return count_l+(element->color == SEL_ARG::BLACK);
    sql_print_error("Wrong tree: Incorrect black-count: %d - %d",
	    count_l,count_r);
  }
  return -1;					// Error, no more warnings
}


/**
  Count how many times SEL_ARG graph "root" refers to its part "key" via
  transitive closure.
  
  @param root  An RB-Root node in a SEL_ARG graph.
  @param key   Another RB-Root node in that SEL_ARG graph.

  The passed "root" node may refer to "key" node via root->next_key_part,
  root->next->n

  This function counts how many times the node "key" is referred (via
  SEL_ARG::next_key_part) by 
  - intervals of RB-tree pointed by "root", 
  - intervals of RB-trees that are pointed by SEL_ARG::next_key_part from 
  intervals of RB-tree pointed by "root",
  - and so on.
    
  Here is an example (horizontal links represent next_key_part pointers, 
  vertical links - next/prev prev pointers):  
    
         +----+               $
         |root|-----------------+
         +----+               $ |
           |                  $ |
           |                  $ |
         +----+       +---+   $ |     +---+    Here the return value
         |    |- ... -|   |---$-+--+->|key|    will be 4.
         +----+       +---+   $ |  |  +---+
           |                  $ |  |
          ...                 $ |  |
           |                  $ |  |
         +----+   +---+       $ |  |
         |    |---|   |---------+  |
         +----+   +---+       $    |
           |        |         $    |
          ...     +---+       $    |
                  |   |------------+
                  +---+       $
  @return 
  Number of links to "key" from nodes reachable from "root".
*/

static ulong count_key_part_usage(SEL_ARG *root, SEL_ARG *key)
{
  ulong count= 0;
  for (root=root->first(); root ; root=root->next)
  {
    if (root->next_key_part)
    {
      if (root->next_key_part == key)
	count++;
      if (root->next_key_part->part < key->part)
	count+=count_key_part_usage(root->next_key_part,key);
    }
  }
  return count;
}


/*
  Check if SEL_ARG::use_count value is correct

  SYNOPSIS
    SEL_ARG::test_use_count()
      root  The root node of the SEL_ARG graph (an RB-tree root node that
            has the least value of sel_arg->part in the entire graph, and
            thus is the "origin" of the graph)

  DESCRIPTION
    Check if SEL_ARG::use_count value is correct. See the definition of
    use_count for what is "correct".
*/

void SEL_ARG::test_use_count(SEL_ARG *root)
{
  uint e_count=0;

  if (this->type != SEL_ARG::KEY_RANGE)
    return;
  for (SEL_ARG *pos=first(); pos ; pos=pos->next)
  {
    e_count++;
    if (pos->next_key_part)
    {
      ulong count=count_key_part_usage(root,pos->next_key_part);
      if (count > pos->next_key_part->use_count)
      {
        sql_print_information("Use_count: Wrong count for key at 0x%lx, %lu "
                              "should be %lu", (long unsigned int)pos,
                              pos->next_key_part->use_count, count);
	return;
      }
      pos->next_key_part->test_use_count(root);
    }
  }
  if (e_count != elements)
    sql_print_warning("Wrong use count: %u (should be %u) for tree at 0x%lx",
                      e_count, elements, (long unsigned int) this);
}
#endif

/*
  Calculate cost and E(#rows) for a given index and intervals tree 

  SYNOPSIS
    check_quick_select()
      param             Parameter from test_quick_select
      idx               Number of index to use in PARAM::key SEL_TREE::key
      index_only        TRUE  - assume only index tuples will be accessed
                        FALSE - assume full table rows will be read
      tree              Transformed selection condition, tree->key[idx] holds
                        the intervals for the given index.
      update_tbl_stats  TRUE <=> update table->quick_* with information
                        about range scan we've evaluated.
      mrr_flags   INOUT MRR access flags
      cost        OUT   Scan cost

  NOTES
    param->is_ror_scan is set to reflect if the key scan is a ROR (see
    is_key_scan_ror function for more info)
    param->table->quick_*, param->range_count (and maybe others) are
    updated with data of given key scan, see quick_range_seq_next for details.

  RETURN
    Estimate # of records to be retrieved.
    HA_POS_ERROR if estimate calculation failed due to table handler problems.
*/

static
ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
                           SEL_ARG *tree, bool update_tbl_stats, 
                           uint *mrr_flags, uint *bufsize, Cost_estimate *cost)
{
  SEL_ARG_RANGE_SEQ seq;
  RANGE_SEQ_IF seq_if = {NULL, sel_arg_range_seq_init, sel_arg_range_seq_next, 0, 0};
  handler *file= param->table->file;
  ha_rows rows= HA_POS_ERROR;
  uint keynr= param->real_keynr[idx];
  DBUG_ENTER("check_quick_select");
  
  /* Handle cases when we don't have a valid non-empty list of range */
  if (!tree)
    DBUG_RETURN(HA_POS_ERROR);
  if (tree->type == SEL_ARG::IMPOSSIBLE)
    DBUG_RETURN(0L);
  if (tree->type != SEL_ARG::KEY_RANGE || tree->part != 0)
    DBUG_RETURN(HA_POS_ERROR);

  seq.keyno= idx;
  seq.real_keyno= keynr;
  seq.param= param;
  seq.start= tree;

  param->range_count=0;
  param->max_key_part=0;

  param->is_ror_scan= TRUE;
  if (file->index_flags(keynr, 0, TRUE) & HA_KEY_SCAN_NOT_ROR)
    param->is_ror_scan= FALSE;
  
  *mrr_flags= param->force_default_mrr? HA_MRR_USE_DEFAULT_IMPL: 0;
  /*
    Pass HA_MRR_SORTED to see if MRR implementation can handle sorting.
  */
  *mrr_flags|= HA_MRR_NO_ASSOCIATION | HA_MRR_SORTED;

  bool pk_is_clustered= file->primary_key_is_clustered();
  if (index_only && 
      (file->index_flags(keynr, param->max_key_part, 1) & HA_KEYREAD_ONLY) &&
      !(file->index_flags(keynr, param->max_key_part, 1) & HA_CLUSTERED_INDEX))
     *mrr_flags |= HA_MRR_INDEX_ONLY;
  
  if (param->thd->lex->sql_command != SQLCOM_SELECT)
    *mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;

  *bufsize= param->thd->variables.mrr_buff_size;
  /*
    Skip materialized derived table/view result table from MRR check as
    they aren't contain any data yet.
  */
  if (param->table->pos_in_table_list->is_non_derived())
    rows= file->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
                                            bufsize, mrr_flags, cost);
  if (rows != HA_POS_ERROR)
  {
    param->quick_rows[keynr]= rows;
    param->possible_keys.set_bit(keynr);
    if (update_tbl_stats)
    {
      param->table->quick_keys.set_bit(keynr);
      param->table->quick_key_parts[keynr]= param->max_key_part+1;
      param->table->quick_n_ranges[keynr]= param->range_count;
      param->table->quick_condition_rows=
        MY_MIN(param->table->quick_condition_rows, rows);
      param->table->quick_rows[keynr]= rows;
      param->table->quick_costs[keynr]= cost->total_cost();
    }
  }
  /* Figure out if the key scan is ROR (returns rows in ROWID order) or not */
  enum ha_key_alg key_alg= param->table->key_info[seq.real_keyno].algorithm;
  if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
  {
    /* 
      All scans are non-ROR scans for those index types.
      TODO: Don't have this logic here, make table engines return 
      appropriate flags instead.
    */
    param->is_ror_scan= FALSE;
  }
  else if (param->table->s->primary_key == keynr && pk_is_clustered)
  {
    /* Clustered PK scan is always a ROR scan (TODO: same as above) */
    param->is_ror_scan= TRUE;
  }
  else if (param->range_count > 1)
  {
    /* 
      Scaning multiple key values in the index: the records are ROR
      for each value, but not between values. E.g, "SELECT ... x IN
      (1,3)" returns ROR order for all records with x=1, then ROR
      order for records with x=3
    */
    param->is_ror_scan= FALSE;
  }

  DBUG_PRINT("exit", ("Records: %lu", (ulong) rows));
  DBUG_RETURN(rows); //psergey-merge:todo: maintain first_null_comp.
}


/*
  Check if key scan on given index with equality conditions on first n key
  parts is a ROR scan.

  SYNOPSIS
    is_key_scan_ror()
      param  Parameter from test_quick_select
      keynr  Number of key in the table. The key must not be a clustered
             primary key.
      nparts Number of first key parts for which equality conditions
             are present.

  NOTES
    ROR (Rowid Ordered Retrieval) key scan is a key scan that produces
    ordered sequence of rowids (ha_xxx::cmp_ref is the comparison function)

    This function is needed to handle a practically-important special case:
    an index scan is a ROR scan if it is done using a condition in form

        "key1_1=c_1 AND ... AND key1_n=c_n"

    where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])

    and the table has a clustered Primary Key defined as 
      PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k) 
    
    i.e. the first key parts of it are identical to uncovered parts ot the 
    key being scanned. This function assumes that the index flags do not
    include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).

    Check (1) is made in quick_range_seq_next()

  RETURN
    TRUE   The scan is ROR-scan
    FALSE  Otherwise
*/

static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts)
{
  KEY *table_key= param->table->key_info + keynr;
  KEY_PART_INFO *key_part= table_key->key_part + nparts;
  KEY_PART_INFO *key_part_end= (table_key->key_part +
                                table_key->user_defined_key_parts);
  uint pk_number;
  
  for (KEY_PART_INFO *kp= table_key->key_part; kp < key_part; kp++)
  {
    uint16 fieldnr= param->table->key_info[keynr].
                    key_part[kp - table_key->key_part].fieldnr - 1;
    if (param->table->field[fieldnr]->key_length() != kp->length)
      return FALSE;
  }
  
  /*
    If there are equalities for all key parts, it is a ROR scan. If there are
    equalities all keyparts and even some of key parts from "Extended Key"
    index suffix, it is a ROR-scan, too.
  */
  if (key_part >= key_part_end)
    return TRUE;

  key_part= table_key->key_part + nparts;
  pk_number= param->table->s->primary_key;
  if (!param->table->file->primary_key_is_clustered() || pk_number == MAX_KEY)
    return FALSE;

  KEY_PART_INFO *pk_part= param->table->key_info[pk_number].key_part;
  KEY_PART_INFO *pk_part_end= pk_part +
                              param->table->key_info[pk_number].user_defined_key_parts;
  for (;(key_part!=key_part_end) && (pk_part != pk_part_end);
       ++key_part, ++pk_part)
  {
    if ((key_part->field != pk_part->field) ||
        (key_part->length != pk_part->length))
      return FALSE;
  }
  return (key_part == key_part_end);
}


/*
  Create a QUICK_RANGE_SELECT from given key and SEL_ARG tree for that key.

  SYNOPSIS
    get_quick_select()
      param
      idx            Index of used key in param->key.
      key_tree       SEL_ARG tree for the used key
      mrr_flags      MRR parameter for quick select
      mrr_buf_size   MRR parameter for quick select
      parent_alloc   If not NULL, use it to allocate memory for
                     quick select data. Otherwise use quick->alloc.
  NOTES
    The caller must call QUICK_SELECT::init for returned quick select.

    CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
    deallocated when the returned quick select is deleted.

  RETURN
    NULL on error
    otherwise created quick select
*/

QUICK_RANGE_SELECT *
get_quick_select(PARAM *param,uint idx,SEL_ARG *key_tree, uint mrr_flags,
                 uint mrr_buf_size, MEM_ROOT *parent_alloc)
{
  QUICK_RANGE_SELECT *quick;
  bool create_err= FALSE;
  DBUG_ENTER("get_quick_select");

  if (param->table->key_info[param->real_keynr[idx]].flags & HA_SPATIAL)
    quick=new QUICK_RANGE_SELECT_GEOM(param->thd, param->table,
                                      param->real_keynr[idx],
                                      MY_TEST(parent_alloc),
                                      parent_alloc, &create_err);
  else
    quick=new QUICK_RANGE_SELECT(param->thd, param->table,
                                 param->real_keynr[idx],
                                 MY_TEST(parent_alloc), NULL, &create_err);

  if (quick)
  {
    if (create_err ||
	get_quick_keys(param,quick,param->key[idx],key_tree,param->min_key,0,
		       param->max_key,0))
    {
      delete quick;
      quick=0;
    }
    else
    {
      KEY *keyinfo= param->table->key_info+param->real_keynr[idx];
      quick->mrr_flags= mrr_flags;
      quick->mrr_buf_size= mrr_buf_size;
      quick->key_parts=(KEY_PART*)
        memdup_root(parent_alloc? parent_alloc : &quick->alloc,
                    (char*) param->key[idx],
                    sizeof(KEY_PART)*
                    param->table->actual_n_key_parts(keyinfo));
    }
  }
  DBUG_RETURN(quick);
}


/*
** Fix this to get all possible sub_ranges
*/
bool
get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
	       SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
	       uchar *max_key, uint max_key_flag)
{
  QUICK_RANGE *range;
  uint flag;
  int min_part= key_tree->part-1, // # of keypart values in min_key buffer
      max_part= key_tree->part-1; // # of keypart values in max_key buffer

  if (key_tree->left != &null_element)
  {
    if (get_quick_keys(param,quick,key,key_tree->left,
		       min_key,min_key_flag, max_key, max_key_flag))
      return 1;
  }
  uchar *tmp_min_key=min_key,*tmp_max_key=max_key;
  min_part+= key_tree->store_min(key[key_tree->part].store_length,
                                 &tmp_min_key,min_key_flag);
  max_part+= key_tree->store_max(key[key_tree->part].store_length,
                                 &tmp_max_key,max_key_flag);

  if (key_tree->next_key_part &&
      key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
      key_tree->next_key_part->part == key_tree->part+1)
  {						  // const key as prefix
    if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
         memcmp(min_key, max_key, (uint)(tmp_max_key - max_key))==0 &&
	 key_tree->min_flag==0 && key_tree->max_flag==0)
    {
      if (get_quick_keys(param,quick,key,key_tree->next_key_part,
			 tmp_min_key, min_key_flag | key_tree->min_flag,
			 tmp_max_key, max_key_flag | key_tree->max_flag))
	return 1;
      goto end;					// Ugly, but efficient
    }
    {
      uint tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
      if (!tmp_min_flag)
        min_part+= key_tree->next_key_part->store_min_key(key,
                                                          &tmp_min_key,
                                                          &tmp_min_flag,
                                                          MAX_KEY);
      if (!tmp_max_flag)
        max_part+= key_tree->next_key_part->store_max_key(key,
                                                          &tmp_max_key,
                                                          &tmp_max_flag,
                                                          MAX_KEY);
      flag=tmp_min_flag | tmp_max_flag;
    }
  }
  else
  {
    flag = (key_tree->min_flag & GEOM_FLAG) ?
      key_tree->min_flag : key_tree->min_flag | key_tree->max_flag;
  }

  /*
    Ensure that some part of min_key and max_key are used.  If not,
    regard this as no lower/upper range
  */
  if ((flag & GEOM_FLAG) == 0)
  {
    if (tmp_min_key != param->min_key)
      flag&= ~NO_MIN_RANGE;
    else
      flag|= NO_MIN_RANGE;
    if (tmp_max_key != param->max_key)
      flag&= ~NO_MAX_RANGE;
    else
      flag|= NO_MAX_RANGE;
  }
  if (flag == 0)
  {
    uint length= (uint) (tmp_min_key - param->min_key);
    if (length == (uint) (tmp_max_key - param->max_key) &&
	!memcmp(param->min_key,param->max_key,length))
    {
      KEY *table_key=quick->head->key_info+quick->index;
      flag=EQ_RANGE;
      if ((table_key->flags & HA_NOSAME) &&
          min_part == key_tree->part &&
          key_tree->part == table_key->user_defined_key_parts-1)
      {
        DBUG_ASSERT(min_part == max_part);
        if ((table_key->flags & HA_NULL_PART_KEY) &&
            null_part_in_key(key,
                             param->min_key,
                             (uint) (tmp_min_key - param->min_key)))
          flag|= NULL_RANGE;
        else
          flag|= UNIQUE_RANGE;
      }
    }
  }

  /* Get range for retrieving rows in QUICK_SELECT::get_next */
  if (!(range= new (param->thd->mem_root) QUICK_RANGE(
                               param->thd,
                               param->min_key,
			       (uint) (tmp_min_key - param->min_key),
                               min_part >=0 ? make_keypart_map(min_part) : 0,
			       param->max_key,
			       (uint) (tmp_max_key - param->max_key),
                               max_part >=0 ? make_keypart_map(max_part) : 0,
			       flag)))
    return 1;			// out of memory

  set_if_bigger(quick->max_used_key_length, range->min_length);
  set_if_bigger(quick->max_used_key_length, range->max_length);
  set_if_bigger(quick->used_key_parts, (uint) key_tree->part+1);
  if (insert_dynamic(&quick->ranges, (uchar*) &range))
    return 1;

 end:
  if (key_tree->right != &null_element)
    return get_quick_keys(param,quick,key,key_tree->right,
			  min_key,min_key_flag,
			  max_key,max_key_flag);
  return 0;
}

/*
  Return 1 if there is only one range and this uses the whole unique key
*/

bool QUICK_RANGE_SELECT::unique_key_range()
{
  if (ranges.elements == 1)
  {
    QUICK_RANGE *tmp= *((QUICK_RANGE**)ranges.buffer);
    if ((tmp->flag & (EQ_RANGE | NULL_RANGE)) == EQ_RANGE)
    {
      KEY *key=head->key_info+index;
      return (key->flags & HA_NOSAME) && key->key_length == tmp->min_length;
    }
  }
  return 0;
}



/*
  Return TRUE if any part of the key is NULL

  SYNOPSIS
    null_part_in_key()    
      key_part  Array of key parts (index description)
      key       Key values tuple
      length    Length of key values tuple in bytes.

  RETURN
    TRUE   The tuple has at least one "keypartX is NULL"
    FALSE  Otherwise
*/

static bool null_part_in_key(KEY_PART *key_part, const uchar *key, uint length)
{
  for (const uchar *end=key+length ;
       key < end;
       key+= key_part++->store_length)
  {
    if (key_part->null_bit && *key)
      return 1;
  }
  return 0;
}


bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
{
  return is_key_used(head, index, fields);
}

bool QUICK_INDEX_SORT_SELECT::is_keys_used(const MY_BITMAP *fields)
{
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    if (is_key_used(head, quick->index, fields))
      return 1;
  }
  return 0;
}

bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
{
  QUICK_SELECT_WITH_RECORD *qr;
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> it(quick_selects);
  while ((qr= it++))
  {
    if (is_key_used(head, qr->quick->index, fields))
      return 1;
  }
  return 0;
}

bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
{
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
    if (quick->is_keys_used(fields))
      return 1;
  }
  return 0;
}


FT_SELECT *get_ft_select(THD *thd, TABLE *table, uint key)
{
  bool create_err= FALSE;
  FT_SELECT *fts= new FT_SELECT(thd, table, key, &create_err);
  if (create_err)
  {
    delete fts;
    return NULL;
  }
  else
    return fts;
}

/*
  Create quick select from ref/ref_or_null scan.

  SYNOPSIS
    get_quick_select_for_ref()
      thd      Thread handle
      table    Table to access
      ref      ref[_or_null] scan parameters
      records  Estimate of number of records (needed only to construct
               quick select)
  NOTES
    This allocates things in a new memory root, as this may be called many
    times during a query.

  RETURN
    Quick select that retrieves the same rows as passed ref scan
    NULL on error.
*/

QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
                                             TABLE_REF *ref, ha_rows records)
{
  MEM_ROOT *old_root, *alloc;
  QUICK_RANGE_SELECT *quick;
  KEY *key_info = &table->key_info[ref->key];
  KEY_PART *key_part;
  QUICK_RANGE *range;
  uint part;
  bool create_err= FALSE;
  Cost_estimate cost;
  uint max_used_key_len;

  old_root= thd->mem_root;
  /* The following call may change thd->mem_root */
  quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0, 0, &create_err);
  /* save mem_root set by QUICK_RANGE_SELECT constructor */
  alloc= thd->mem_root;
  /*
    return back default mem_root (thd->mem_root) changed by
    QUICK_RANGE_SELECT constructor
  */
  thd->mem_root= old_root;

  if (!quick || create_err)
    return 0;			/* no ranges found */
  if (quick->init())
    goto err;
  quick->records= records;

  if ((cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error) ||
      !(range= new(alloc) QUICK_RANGE()))
    goto err;                                   // out of memory

  range->min_key= range->max_key= ref->key_buff;
  range->min_length= range->max_length= ref->key_length;
  range->min_keypart_map= range->max_keypart_map=
    make_prev_keypart_map(ref->key_parts);
  range->flag= EQ_RANGE;

  if (!(quick->key_parts=key_part=(KEY_PART *)
	alloc_root(&quick->alloc,sizeof(KEY_PART)*ref->key_parts)))
    goto err;
  
  max_used_key_len=0;
  for (part=0 ; part < ref->key_parts ;part++,key_part++)
  {
    key_part->part=part;
    key_part->field=        key_info->key_part[part].field;
    key_part->length=       key_info->key_part[part].length;
    key_part->store_length= key_info->key_part[part].store_length;
    key_part->null_bit=     key_info->key_part[part].null_bit;
    key_part->flag=         (uint8) key_info->key_part[part].key_part_flag;

    max_used_key_len +=key_info->key_part[part].store_length;
  }

  quick->max_used_key_length= max_used_key_len;

  if (insert_dynamic(&quick->ranges,(uchar*)&range))
    goto err;

  /*
     Add a NULL range if REF_OR_NULL optimization is used.
     For example:
       if we have "WHERE A=2 OR A IS NULL" we created the (A=2) range above
       and have ref->null_ref_key set. Will create a new NULL range here.
  */
  if (ref->null_ref_key)
  {
    QUICK_RANGE *null_range;

    *ref->null_ref_key= 1;		// Set null byte then create a range
    if (!(null_range= new (alloc)
          QUICK_RANGE(thd, ref->key_buff, ref->key_length,
                      make_prev_keypart_map(ref->key_parts),
                      ref->key_buff, ref->key_length,
                      make_prev_keypart_map(ref->key_parts), EQ_RANGE)))
      goto err;
    *ref->null_ref_key= 0;		// Clear null byte
    if (insert_dynamic(&quick->ranges,(uchar*)&null_range))
      goto err;
  }

  /* Call multi_range_read_info() to get the MRR flags and buffer size */
  quick->mrr_flags= HA_MRR_NO_ASSOCIATION | 
                    (table->file->keyread_enabled() ? HA_MRR_INDEX_ONLY : 0);
  if (thd->lex->sql_command != SQLCOM_SELECT)
    quick->mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;

  quick->mrr_buf_size= thd->variables.mrr_buff_size;
  if (table->file->multi_range_read_info(quick->index, 1, (uint)records,
                                         ~0, 
                                         &quick->mrr_buf_size,
                                         &quick->mrr_flags, &cost))
    goto err;

  return quick;
err:
  delete quick;
  return 0;
}


/*
  Perform key scans for all used indexes (except CPK), get rowids and merge 
  them into an ordered non-recurrent sequence of rowids.
  
  The merge/duplicate removal is performed using Unique class. We put all
  rowids into Unique, get the sorted sequence and destroy the Unique.
  
  If table has a clustered primary key that covers all rows (TRUE for bdb
  and innodb currently) and one of the index_merge scans is a scan on PK,
  then rows that will be retrieved by PK scan are not put into Unique and 
  primary key scan is not performed here, it is performed later separately.

  RETURN
    0     OK
    other error
*/

int read_keys_and_merge_scans(THD *thd,
                              TABLE *head,
                              List<QUICK_RANGE_SELECT> quick_selects,
                              QUICK_RANGE_SELECT *pk_quick_select,
                              READ_RECORD *read_record,
                              bool intersection,
                              key_map *filtered_scans,
                              Unique **unique_ptr)
{
  List_iterator_fast<QUICK_RANGE_SELECT> cur_quick_it(quick_selects);
  QUICK_RANGE_SELECT* cur_quick;
  int result;
  Unique *unique= *unique_ptr;
  handler *file= head->file;
  bool with_cpk_filter= pk_quick_select != NULL;
  DBUG_ENTER("read_keys_and_merge");

  /* We're going to just read rowids. */
  head->file->ha_start_keyread(head->s->primary_key);
  head->prepare_for_position();

  cur_quick_it.rewind();
  cur_quick= cur_quick_it++;
  bool first_quick= TRUE;
  DBUG_ASSERT(cur_quick != 0);
  
  /*
    We reuse the same instance of handler so we need to call both init and 
    reset here.
  */
  if (cur_quick->init() || cur_quick->reset())
    goto err;

  if (unique == NULL)
  {
    DBUG_EXECUTE_IF("index_merge_may_not_create_a_Unique", DBUG_ABORT(); );
    DBUG_EXECUTE_IF("only_one_Unique_may_be_created", 
                    DBUG_SET("+d,index_merge_may_not_create_a_Unique"); );

    unique= new Unique(refpos_order_cmp, (void *)file,
                       file->ref_length,
                       thd->variables.sortbuff_size,
		       intersection ? quick_selects.elements : 0);                     
    if (!unique)
      goto err;
    *unique_ptr= unique;
  }
  else
  {
    unique->reset();
  }

  DBUG_ASSERT(file->ref_length == unique->get_size());
  DBUG_ASSERT(thd->variables.sortbuff_size == unique->get_max_in_memory_size());

  for (;;)
  {
    while ((result= cur_quick->get_next()) == HA_ERR_END_OF_FILE)
    {
      if (intersection)
        with_cpk_filter= filtered_scans->is_set(cur_quick->index);
      if (first_quick)
      {
        first_quick= FALSE;
        if (intersection && unique->is_in_memory())
          unique->close_for_expansion();
      }
      cur_quick->range_end();
      cur_quick= cur_quick_it++;
      if (!cur_quick)
        break;

      if (cur_quick->file->inited != handler::NONE) 
        cur_quick->file->ha_index_end();
      if (cur_quick->init() || cur_quick->reset())
        goto err;
    }

    if (result)
    {
      if (result != HA_ERR_END_OF_FILE)
      {
        cur_quick->range_end();
        goto err;
      }
      break;
    }

    if (thd->killed)
      goto err;

    if (with_cpk_filter &&
        pk_quick_select->row_in_ranges() != intersection )
      continue;

    cur_quick->file->position(cur_quick->record);
    if (unique->unique_add((char*)cur_quick->file->ref))
      goto err;
  }

  /*
    Ok all rowids are in the Unique now. The next call will initialize
    the unique structure so it can be used to iterate through the rowids
    sequence.
  */
  result= unique->get(head);
  /*
    index merge currently doesn't support "using index" at all
  */
  head->file->ha_end_keyread();
  if (init_read_record(read_record, thd, head, (SQL_SELECT*) 0,
                       &unique->sort, 1 , 1, TRUE))
    result= 1;
 DBUG_RETURN(result);

err:
  head->file->ha_end_keyread();
  DBUG_RETURN(1);
}


int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()

{
  int result;
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::read_keys_and_merge");
  result= read_keys_and_merge_scans(thd, head, quick_selects, pk_quick_select,
                                    &read_record, FALSE, NULL, &unique);
  doing_pk_scan= FALSE;
  DBUG_RETURN(result);
}

/*
  Get next row for index_merge.
  NOTES
    The rows are read from
      1. rowids stored in Unique.
      2. QUICK_RANGE_SELECT with clustered primary key (if any).
    The sets of rows retrieved in 1) and 2) are guaranteed to be disjoint.
*/

int QUICK_INDEX_MERGE_SELECT::get_next()
{
  int result;
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::get_next");

  if (doing_pk_scan)
    DBUG_RETURN(pk_quick_select->get_next());

  if ((result= read_record.read_record(&read_record)) == -1)
  {
    result= HA_ERR_END_OF_FILE;
    end_read_record(&read_record);
    // Free things used by sort early. Shouldn't be strictly necessary
    unique->sort.reset();
    /* All rows from Unique have been retrieved, do a clustered PK scan */
    if (pk_quick_select)
    {
      doing_pk_scan= TRUE;
      if ((result= pk_quick_select->init()) ||
          (result= pk_quick_select->reset()))
        DBUG_RETURN(result);
      DBUG_RETURN(pk_quick_select->get_next());
    }
  }

  DBUG_RETURN(result);
}

int QUICK_INDEX_INTERSECT_SELECT::read_keys_and_merge()

{
  int result;
  DBUG_ENTER("QUICK_INDEX_INTERSECT_SELECT::read_keys_and_merge");
  result= read_keys_and_merge_scans(thd, head, quick_selects, pk_quick_select,
                                    &read_record, TRUE, &filtered_scans,
                                    &unique);
  DBUG_RETURN(result);
}

int QUICK_INDEX_INTERSECT_SELECT::get_next()
{
  int result;
  DBUG_ENTER("QUICK_INDEX_INTERSECT_SELECT::get_next");

  if ((result= read_record.read_record(&read_record)) == -1)
  {
    result= HA_ERR_END_OF_FILE;
    end_read_record(&read_record);
    unique->sort.reset();                       // Free things early
  }

  DBUG_RETURN(result);
}


/*
  Retrieve next record.
  SYNOPSIS
     QUICK_ROR_INTERSECT_SELECT::get_next()

  NOTES
    Invariant on enter/exit: all intersected selects have retrieved all index
    records with rowid <= some_rowid_val and no intersected select has
    retrieved any index records with rowid > some_rowid_val.
    We start fresh and loop until we have retrieved the same rowid in each of
    the key scans or we got an error.

    If a Clustered PK scan is present, it is used only to check if row
    satisfies its condition (and never used for row retrieval).

    Locking: to ensure that exclusive locks are only set on records that
    are included in the final result we must release the lock
    on all rows we read but do not include in the final result. This
    must be done on each index that reads the record and the lock
    must be released using the same handler (the same quick object) as
    used when reading the record.

  RETURN
   0     - Ok
   other - Error code if any error occurred.
*/

int QUICK_ROR_INTERSECT_SELECT::get_next()
{
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> quick_it(quick_selects);
  QUICK_SELECT_WITH_RECORD *qr;
  QUICK_RANGE_SELECT* quick;

  /* quick that reads the given rowid first. This is needed in order
  to be able to unlock the row using the same handler object that locked
  it */
  QUICK_RANGE_SELECT* quick_with_last_rowid;

  int error, cmp;
  uint last_rowid_count=0;
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::get_next");

  do
  {
    /* Get a rowid for first quick and save it as a 'candidate' */
    qr= quick_it++;
    quick= qr->quick;
    error= quick->get_next();
    if (cpk_quick)
    {
      while (!error && !cpk_quick->row_in_ranges())
      {
        quick->file->unlock_row(); /* row not in range; unlock */
        error= quick->get_next();
      }
    }
    if (error)
      DBUG_RETURN(error);

    /* Save the read key tuple */
    key_copy(qr->key_tuple, record, head->key_info + quick->index,
             quick->max_used_key_length);

    quick->file->position(quick->record);
    memcpy(last_rowid, quick->file->ref, head->file->ref_length);
    last_rowid_count= 1;
    quick_with_last_rowid= quick;

    while (last_rowid_count < quick_selects.elements)
    {
      if (!(qr= quick_it++))
      {
        quick_it.rewind();
        qr= quick_it++;
      }
      quick= qr->quick;

      do
      {
        DBUG_EXECUTE_IF("innodb_quick_report_deadlock",
                        DBUG_SET("+d,innodb_report_deadlock"););
        if ((error= quick->get_next()))
        {
          /* On certain errors like deadlock, trx might be rolled back.*/
          if (!thd->transaction_rollback_request)
            quick_with_last_rowid->file->unlock_row();
          DBUG_RETURN(error);
        }
        quick->file->position(quick->record);
        cmp= head->file->cmp_ref(quick->file->ref, last_rowid);
        if (cmp < 0)
        {
          /* This row is being skipped.  Release lock on it. */
          quick->file->unlock_row();
        }
      } while (cmp < 0);

      key_copy(qr->key_tuple, record, head->key_info + quick->index,
               quick->max_used_key_length);

      /* Ok, current select 'caught up' and returned ref >= cur_ref */
      if (cmp > 0)
      {
        /* Found a row with ref > cur_ref. Make it a new 'candidate' */
        if (cpk_quick)
        {
          while (!cpk_quick->row_in_ranges())
          {
            quick->file->unlock_row(); /* row not in range; unlock */
            if ((error= quick->get_next()))
            {
              /* On certain errors like deadlock, trx might be rolled back.*/
              if (!thd->transaction_rollback_request)
                quick_with_last_rowid->file->unlock_row();
              DBUG_RETURN(error);
            }
          }
          quick->file->position(quick->record);
        }
        memcpy(last_rowid, quick->file->ref, head->file->ref_length);
        quick_with_last_rowid->file->unlock_row();
        last_rowid_count= 1;
        quick_with_last_rowid= quick;

        //save the fields here
        key_copy(qr->key_tuple, record, head->key_info + quick->index,
                 quick->max_used_key_length);
      }
      else
      {
        /* current 'candidate' row confirmed by this select */
        last_rowid_count++;
      }
    }

    /* We get here if we got the same row ref in all scans. */
    if (need_to_fetch_row)
      error= head->file->ha_rnd_pos(head->record[0], last_rowid);
  } while (error == HA_ERR_RECORD_DELETED);

  if (!need_to_fetch_row)
  {
    /* Restore the columns we've read/saved with other quick selects */
    quick_it.rewind();
    while ((qr= quick_it++))
    {
      if (qr->quick != quick)
      {
        key_restore(record, qr->key_tuple, head->key_info + qr->quick->index,
                    qr->quick->max_used_key_length);
      }
    }
  }

  DBUG_RETURN(error);
}


/*
  Retrieve next record.
  SYNOPSIS
    QUICK_ROR_UNION_SELECT::get_next()

  NOTES
    Enter/exit invariant:
    For each quick select in the queue a {key,rowid} tuple has been
    retrieved but the corresponding row hasn't been passed to output.

  RETURN
   0     - Ok
   other - Error code if any error occurred.
*/

int QUICK_ROR_UNION_SELECT::get_next()
{
  int error, dup_row;
  QUICK_SELECT_I *quick;
  uchar *tmp;
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::get_next");

  do
  {
    do
    {
      if (!queue.elements)
        DBUG_RETURN(HA_ERR_END_OF_FILE);
      /* Ok, we have a queue with >= 1 scans */

      quick= (QUICK_SELECT_I*)queue_top(&queue);
      memcpy(cur_rowid, quick->last_rowid, rowid_length);

      /* put into queue rowid from the same stream as top element */
      if ((error= quick->get_next()))
      {
        if (error != HA_ERR_END_OF_FILE)
          DBUG_RETURN(error);
        queue_remove_top(&queue);
      }
      else
      {
        quick->save_last_pos();
        queue_replace_top(&queue);
      }

      if (!have_prev_rowid)
      {
        /* No rows have been returned yet */
        dup_row= FALSE;
        have_prev_rowid= TRUE;
      }
      else
        dup_row= !head->file->cmp_ref(cur_rowid, prev_rowid);
    } while (dup_row);

    tmp= cur_rowid;
    cur_rowid= prev_rowid;
    prev_rowid= tmp;

    error= head->file->ha_rnd_pos(quick->record, prev_rowid);
  } while (error == HA_ERR_RECORD_DELETED);
  DBUG_RETURN(error);
}


int QUICK_RANGE_SELECT::reset()
{
  uint  buf_size;
  uchar *mrange_buff;
  int   error;
  HANDLER_BUFFER empty_buf;
  MY_BITMAP * const save_read_set= head->read_set;
  MY_BITMAP * const save_write_set= head->write_set;
  MY_BITMAP * const save_vcol_set= head->vcol_set;
  DBUG_ENTER("QUICK_RANGE_SELECT::reset");
  last_range= NULL;
  cur_range= (QUICK_RANGE**) ranges.buffer;
  RANGE_SEQ_IF seq_funcs= {NULL, quick_range_seq_init, quick_range_seq_next, 0, 0};
  
  if (file->inited == handler::RND)
  {
    /* Handler could be left in this state by MRR */
    if ((error= file->ha_rnd_end()))
      DBUG_RETURN(error);
  }

  if (in_ror_merged_scan)
    head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap,
                                       &column_bitmap);

  if (file->inited == handler::NONE)
  {
    DBUG_EXECUTE_IF("bug14365043_2",
                    DBUG_SET("+d,ha_index_init_fail"););
    if ((error= file->ha_index_init(index,1)))
    {
        file->print_error(error, MYF(0));
        goto err;
    }
  }

  /* Allocate buffer if we need one but haven't allocated it yet */
  if (mrr_buf_size && !mrr_buf_desc)
  {
    buf_size= mrr_buf_size;
    while (buf_size && !my_multi_malloc(MYF(MY_WME),
                                        &mrr_buf_desc, sizeof(*mrr_buf_desc),
                                        &mrange_buff, buf_size,
                                        NullS))
    {
      /* Try to shrink the buffers until both are 0. */
      buf_size/= 2;
    }
    if (!mrr_buf_desc)
      DBUG_RETURN(HA_ERR_OUT_OF_MEM);

    /* Initialize the handler buffer. */
    mrr_buf_desc->buffer= mrange_buff;
    mrr_buf_desc->buffer_end= mrange_buff + buf_size;
    mrr_buf_desc->end_of_used_area= mrange_buff;
  }

  if (!mrr_buf_desc)
    empty_buf.buffer= empty_buf.buffer_end= empty_buf.end_of_used_area= NULL;
 
  error= file->multi_range_read_init(&seq_funcs, (void*)this, ranges.elements,
                                     mrr_flags, mrr_buf_desc? mrr_buf_desc: 
                                                              &empty_buf);
err:
  /* Restore bitmaps set on entry */
  if (in_ror_merged_scan)
    head->column_bitmaps_set_no_signal(save_read_set, save_write_set,
                                       save_vcol_set);
  DBUG_RETURN(error);
}


/*
  Get next possible record using quick-struct.

  SYNOPSIS
    QUICK_RANGE_SELECT::get_next()

  NOTES
    Record is read into table->record[0]

  RETURN
    0			Found row
    HA_ERR_END_OF_FILE	No (more) rows in range
    #			Error code
*/

int QUICK_RANGE_SELECT::get_next()
{
  range_id_t dummy;
  int result;
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next");

  if (!in_ror_merged_scan)
    DBUG_RETURN(file->multi_range_read_next(&dummy));

  MY_BITMAP * const save_read_set= head->read_set;
  MY_BITMAP * const save_write_set= head->write_set;
  MY_BITMAP * const save_vcol_set= head->vcol_set;
  /*
    We don't need to signal the bitmap change as the bitmap is always the
    same for this head->file
  */
  head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap,
                                     &column_bitmap);
  result= file->multi_range_read_next(&dummy);
  head->column_bitmaps_set_no_signal(save_read_set, save_write_set,
                                     save_vcol_set);
  DBUG_RETURN(result);
}


/*
  Get the next record with a different prefix.

  @param prefix_length   length of cur_prefix
  @param group_key_parts The number of key parts in the group prefix
  @param cur_prefix      prefix of a key to be searched for

  Each subsequent call to the method retrieves the first record that has a
  prefix with length prefix_length and which is different from cur_prefix,
  such that the record with the new prefix is within the ranges described by
  this->ranges. The record found is stored into the buffer pointed by
  this->record. The method is useful for GROUP-BY queries with range
  conditions to discover the prefix of the next group that satisfies the range
  conditions.

  @todo

    This method is a modified copy of QUICK_RANGE_SELECT::get_next(), so both
    methods should be unified into a more general one to reduce code
    duplication.

  @retval 0                  on success
  @retval HA_ERR_END_OF_FILE if returned all keys
  @retval other              if some error occurred
*/

int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length,
                                        uint group_key_parts,
                                        uchar *cur_prefix)
{
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next_prefix");
  const key_part_map keypart_map= make_prev_keypart_map(group_key_parts);

  for (;;)
  {
    int result;
    if (last_range)
    {
      /* Read the next record in the same range with prefix after cur_prefix. */
      DBUG_ASSERT(cur_prefix != NULL);
      result= file->ha_index_read_map(record, cur_prefix, keypart_map,
                                      HA_READ_AFTER_KEY);
      if (result || last_range->max_keypart_map == 0)
        DBUG_RETURN(result);

      key_range previous_endpoint;
      last_range->make_max_endpoint(&previous_endpoint, prefix_length, keypart_map);
      if (file->compare_key(&previous_endpoint) <= 0)
        DBUG_RETURN(0);
    }

    uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
    if (count == 0)
    {
      /* Ranges have already been used up before. None is left for read. */
      last_range= 0;
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    last_range= *(cur_range++);

    key_range start_key, end_key;
    last_range->make_min_endpoint(&start_key, prefix_length, keypart_map);
    last_range->make_max_endpoint(&end_key, prefix_length, keypart_map);

    result= file->read_range_first(last_range->min_keypart_map ? &start_key : 0,
				   last_range->max_keypart_map ? &end_key : 0,
                                   MY_TEST(last_range->flag & EQ_RANGE),
				   TRUE);
    if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
      last_range= 0;			// Stop searching

    if (result != HA_ERR_END_OF_FILE)
      DBUG_RETURN(result);
    last_range= 0;			// No matching rows; go to next range
  }
}


/* Get next for geometrical indexes */

int QUICK_RANGE_SELECT_GEOM::get_next()
{
  DBUG_ENTER("QUICK_RANGE_SELECT_GEOM::get_next");

  for (;;)
  {
    int result;
    if (last_range)
    {
      // Already read through key
      result= file->ha_index_next_same(record, last_range->min_key,
                                       last_range->min_length);
      if (result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
    }

    uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
    if (count == 0)
    {
      /* Ranges have already been used up before. None is left for read. */
      last_range= 0;
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    last_range= *(cur_range++);

    result= file->ha_index_read_map(record, last_range->min_key,
                                    last_range->min_keypart_map,
                                    (ha_rkey_function)(last_range->flag ^
                                                       GEOM_FLAG));
    if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
      DBUG_RETURN(result);
    last_range= 0;				// Not found, to next range
  }
}


/*
  Check if current row will be retrieved by this QUICK_RANGE_SELECT

  NOTES
    It is assumed that currently a scan is being done on another index
    which reads all necessary parts of the index that is scanned by this
    quick select.
    The implementation does a binary search on sorted array of disjoint
    ranges, without taking size of range into account.

    This function is used to filter out clustered PK scan rows in
    index_merge quick select.

  RETURN
    TRUE  if current row will be retrieved by this quick select
    FALSE if not
*/

bool QUICK_RANGE_SELECT::row_in_ranges()
{
  QUICK_RANGE *res;
  uint min= 0;
  uint max= ranges.elements - 1;
  uint mid= (max + min)/2;

  while (min != max)
  {
    if (cmp_next(*(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid)))
    {
      /* current row value > mid->max */
      min= mid + 1;
    }
    else
      max= mid;
    mid= (min + max) / 2;
  }
  res= *(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid);
  return (!cmp_next(res) && !cmp_prev(res));
}

/*
  This is a hack: we inherit from QUICK_RANGE_SELECT so that we can use the
  get_next() interface, but we have to hold a pointer to the original
  QUICK_RANGE_SELECT because its data are used all over the place. What
  should be done is to factor out the data that is needed into a base
  class (QUICK_SELECT), and then have two subclasses (_ASC and _DESC)
  which handle the ranges and implement the get_next() function.  But
  for now, this seems to work right at least.
 */

QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
                                     uint used_key_parts_arg)
 :QUICK_RANGE_SELECT(*q), rev_it(rev_ranges),
  used_key_parts (used_key_parts_arg)
{
  QUICK_RANGE *r;
  /*
    Use default MRR implementation for reverse scans. No table engine
    currently can do an MRR scan with output in reverse index order.
  */
  mrr_buf_desc= NULL;
  mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
  mrr_buf_size= 0;

  QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
  QUICK_RANGE **end_range= pr + ranges.elements;
  for (; pr!=end_range; pr++)
    rev_ranges.push_front(*pr);

  /* Remove EQ_RANGE flag for keys that are not using the full key */
  for (r = rev_it++; r; r = rev_it++)
  {
    if ((r->flag & EQ_RANGE) &&
	head->key_info[index].key_length != r->max_length)
      r->flag&= ~EQ_RANGE;
  }
  rev_it.rewind();
  q->dont_free=1;				// Don't free shared mem
}


int QUICK_SELECT_DESC::get_next()
{
  DBUG_ENTER("QUICK_SELECT_DESC::get_next");

  /* The max key is handled as follows:
   *   - if there is NO_MAX_RANGE, start at the end and move backwards
   *   - if it is an EQ_RANGE, which means that max key covers the entire
   *     key, go directly to the key and read through it (sorting backwards is
   *     same as sorting forwards)
   *   - if it is NEAR_MAX, go to the key or next, step back once, and
   *     move backwards
   *   - otherwise (not NEAR_MAX == include the key), go after the key,
   *     step back once, and move backwards
   */

  for (;;)
  {
    int result;
    if (last_range)
    {						// Already read through key
      result = ((last_range->flag & EQ_RANGE && 
                 used_key_parts <= head->key_info[index].user_defined_key_parts) ? 
                file->ha_index_next_same(record, last_range->min_key,
                                      last_range->min_length) :
                file->ha_index_prev(record));
      if (!result)
      {
	if (cmp_prev(*rev_it.ref()) == 0)
	  DBUG_RETURN(0);
      }
      else if (result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
    }

    if (!(last_range= rev_it++))
      DBUG_RETURN(HA_ERR_END_OF_FILE);		// All ranges used

    key_range       start_key;
    start_key.key=    (const uchar*) last_range->min_key;
    start_key.length= last_range->min_length;
    start_key.flag=   ((last_range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
                       (last_range->flag & EQ_RANGE) ?
                       HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
    start_key.keypart_map= last_range->min_keypart_map;
    key_range       end_key;
    end_key.key=      (const uchar*) last_range->max_key;
    end_key.length=   last_range->max_length;
    end_key.flag=     (last_range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
                       HA_READ_AFTER_KEY);
    end_key.keypart_map= last_range->max_keypart_map;
    result= file->prepare_range_scan((last_range->flag & NO_MIN_RANGE) ? NULL : &start_key,
                                     (last_range->flag & NO_MAX_RANGE) ? NULL : &end_key);
    if (result)
    {
      DBUG_RETURN(result);
    }

    if (last_range->flag & NO_MAX_RANGE)        // Read last record
    {
      int local_error;
      if ((local_error= file->ha_index_last(record)))
	DBUG_RETURN(local_error);		// Empty table
      if (cmp_prev(last_range) == 0)
	DBUG_RETURN(0);
      last_range= 0;                            // No match; go to next range
      continue;
    }

    if (last_range->flag & EQ_RANGE &&
        used_key_parts <= head->key_info[index].user_defined_key_parts)

    {
      result= file->ha_index_read_map(record, last_range->max_key,
                                      last_range->max_keypart_map,
                                      HA_READ_KEY_EXACT);
    }
    else
    {
      DBUG_ASSERT(last_range->flag & NEAR_MAX ||
                  (last_range->flag & EQ_RANGE && 
                   used_key_parts > head->key_info[index].user_defined_key_parts) ||
                  range_reads_after_key(last_range));
      result= file->ha_index_read_map(record, last_range->max_key,
                                      last_range->max_keypart_map,
                                      ((last_range->flag & NEAR_MAX) ?
                                       HA_READ_BEFORE_KEY :
                                       HA_READ_PREFIX_LAST_OR_PREV));
    }
    if (result)
    {
      if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
      last_range= 0;                            // Not found, to next range
      continue;
    }
    if (cmp_prev(last_range) == 0)
    {
      if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
	last_range= 0;				// Stop searching
      DBUG_RETURN(0);				// Found key is in range
    }
    last_range= 0;                              // To next range
  }
}


/**
  Create a compatible quick select with the result ordered in an opposite way

  @param used_key_parts_arg  Number of used key parts

  @retval NULL in case of errors (OOM etc)
  @retval pointer to a newly created QUICK_SELECT_DESC if success
*/

QUICK_SELECT_I *QUICK_RANGE_SELECT::make_reverse(uint used_key_parts_arg)
{
  QUICK_SELECT_DESC *new_quick= new QUICK_SELECT_DESC(this, used_key_parts_arg);
  if (new_quick == NULL)
  {
    delete new_quick;
    return NULL;
  }
  return new_quick;
}


/*
  Compare if found key is over max-value
  Returns 0 if key <= range->max_key
  TODO: Figure out why can't this function be as simple as cmp_prev(). 
*/

int QUICK_RANGE_SELECT::cmp_next(QUICK_RANGE *range_arg)
{
  if (range_arg->flag & NO_MAX_RANGE)
    return 0;                                   /* key can't be to large */

  KEY_PART *key_part=key_parts;
  uint store_length;

  for (uchar *key=range_arg->max_key, *end=key+range_arg->max_length;
       key < end;
       key+= store_length, key_part++)
  {
    int cmp;
    store_length= key_part->store_length;
    if (key_part->null_bit)
    {
      if (*key)
      {
        if (!key_part->field->is_null())
          return 1;
        continue;
      }
      else if (key_part->field->is_null())
        return 0;
      key++;					// Skip null byte
      store_length--;
    }
    if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
      return 0;
    if (cmp > 0)
      return 1;
  }
  return (range_arg->flag & NEAR_MAX) ? 1 : 0;          // Exact match
}


/*
  Returns 0 if found key is inside range (found key >= range->min_key).
*/

int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg)
{
  int cmp;
  if (range_arg->flag & NO_MIN_RANGE)
    return 0;					/* key can't be to small */

  cmp= key_cmp(key_part_info, range_arg->min_key,
               range_arg->min_length);
  if (cmp > 0 || (cmp == 0 && !(range_arg->flag & NEAR_MIN)))
    return 0;
  return 1;                                     // outside of range
}


/*
 * TRUE if this range will require using HA_READ_AFTER_KEY
   See comment in get_next() about this
 */

bool QUICK_SELECT_DESC::range_reads_after_key(QUICK_RANGE *range_arg)
{
  return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
	  !(range_arg->flag & EQ_RANGE) ||
	  head->key_info[index].key_length != range_arg->max_length) ? 1 : 0;
}


void QUICK_SELECT_I::add_key_name(String *str, bool *first)
{
  KEY *key_info= head->key_info + index;

  if (*first)
    *first= FALSE;
  else
    str->append(',');
  str->append(key_info->name);
}
 

Explain_quick_select* QUICK_RANGE_SELECT::get_explain(MEM_ROOT *local_alloc)
{
  Explain_quick_select *res;
  if ((res= new (local_alloc) Explain_quick_select(QS_TYPE_RANGE)))
    res->range.set(local_alloc, &head->key_info[index], max_used_key_length);
  return res;
}


Explain_quick_select*
QUICK_GROUP_MIN_MAX_SELECT::get_explain(MEM_ROOT *local_alloc)
{
  Explain_quick_select *res;
  if ((res= new (local_alloc) Explain_quick_select(QS_TYPE_GROUP_MIN_MAX)))
    res->range.set(local_alloc, &head->key_info[index], max_used_key_length);
  return res;
}


Explain_quick_select*
QUICK_INDEX_SORT_SELECT::get_explain(MEM_ROOT *local_alloc)
{
  Explain_quick_select *res;
  if (!(res= new (local_alloc) Explain_quick_select(get_type())))
    return NULL;

  QUICK_RANGE_SELECT *quick;
  Explain_quick_select *child_explain;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    if ((child_explain= quick->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }

  if (pk_quick_select)
  {
    if ((child_explain= pk_quick_select->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }
  return res;
}


/*
  Same as QUICK_INDEX_SORT_SELECT::get_explain(), but primary key is printed
  first
*/

Explain_quick_select*
QUICK_INDEX_INTERSECT_SELECT::get_explain(MEM_ROOT *local_alloc)
{
  Explain_quick_select *res;
  Explain_quick_select *child_explain;

  if (!(res= new (local_alloc) Explain_quick_select(get_type())))
    return NULL;

  if (pk_quick_select)
  {
    if ((child_explain= pk_quick_select->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }

  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    if ((child_explain= quick->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }
  return res;
}


Explain_quick_select*
QUICK_ROR_INTERSECT_SELECT::get_explain(MEM_ROOT *local_alloc)
{
  Explain_quick_select *res;
  Explain_quick_select *child_explain;

  if (!(res= new (local_alloc) Explain_quick_select(get_type())))
    return NULL;

  QUICK_SELECT_WITH_RECORD *qr;
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> it(quick_selects);
  while ((qr= it++))
  {
    if ((child_explain= qr->quick->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }

  if (cpk_quick)
  {
    if ((child_explain= cpk_quick->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }
  return res;
}


Explain_quick_select*
QUICK_ROR_UNION_SELECT::get_explain(MEM_ROOT *local_alloc)
{
  Explain_quick_select *res;
  Explain_quick_select *child_explain;

  if (!(res= new (local_alloc) Explain_quick_select(get_type())))
    return NULL;

  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
    if ((child_explain= quick->get_explain(local_alloc)))
      res->children.push_back(child_explain);
    else
      return NULL;
  }

  return res;
}


void QUICK_SELECT_I::add_key_and_length(String *key_names,
                                        String *used_lengths,
                                        bool *first)
{
  char buf[64];
  uint length;
  KEY *key_info= head->key_info + index;

  if (*first)
    *first= FALSE;
  else
  {
    key_names->append(',');
    used_lengths->append(',');
  }
  key_names->append(key_info->name);
  length= longlong10_to_str(max_used_key_length, buf, 10) - buf;
  used_lengths->append(buf, length);
}


void QUICK_RANGE_SELECT::add_keys_and_lengths(String *key_names,
                                              String *used_lengths)
{
  bool first= TRUE;

  add_key_and_length(key_names, used_lengths, &first);
}

void QUICK_INDEX_MERGE_SELECT::add_keys_and_lengths(String *key_names,
                                                    String *used_lengths)
{
  QUICK_RANGE_SELECT *quick;
  bool first= TRUE;

  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);

  while ((quick= it++))
  {
    quick->add_key_and_length(key_names, used_lengths, &first);
  }

  if (pk_quick_select)
    pk_quick_select->add_key_and_length(key_names, used_lengths, &first);
}


void QUICK_INDEX_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
                                                        String *used_lengths)
{
  QUICK_RANGE_SELECT *quick;
  bool first= TRUE;

  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);

  if (pk_quick_select)
    pk_quick_select->add_key_and_length(key_names, used_lengths, &first);

  while ((quick= it++))
  {
    quick->add_key_and_length(key_names, used_lengths, &first);
  }
}

void QUICK_ROR_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
                                                      String *used_lengths)
{
  QUICK_SELECT_WITH_RECORD *qr;
  bool first= TRUE;

  List_iterator_fast<QUICK_SELECT_WITH_RECORD> it(quick_selects);

  while ((qr= it++))
  {
    qr->quick->add_key_and_length(key_names, used_lengths, &first);
  }
  if (cpk_quick)
    cpk_quick->add_key_and_length(key_names, used_lengths, &first);
}

void QUICK_ROR_UNION_SELECT::add_keys_and_lengths(String *key_names,
                                                  String *used_lengths)
{
  QUICK_SELECT_I *quick;
  bool first= TRUE;

  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);

  while ((quick= it++))
  {
    if (first)
      first= FALSE;
    else
    {
      used_lengths->append(',');
      key_names->append(',');
    }
    quick->add_keys_and_lengths(key_names, used_lengths);
  }
}


void QUICK_RANGE_SELECT::add_used_key_part_to_set()
{
  uint key_len;
  KEY_PART *part= key_parts;
  for (key_len=0; key_len < max_used_key_length;
       key_len += (part++)->store_length)
  {
    /*
      We have to use field_index instead of part->field
      as for partial fields, part->field points to
      a temporary field that is only part of the original
      field.  field_index always points to the original field
    */
    Field *field= head->field[part->field->field_index];
    field->register_field_in_read_map();
  }
}


void QUICK_GROUP_MIN_MAX_SELECT::add_used_key_part_to_set()
{
  uint key_len;
  KEY_PART_INFO *part= index_info->key_part;
  for (key_len=0; key_len < max_used_key_length;
       key_len += (part++)->store_length)
  {
    /*
      We have to use field_index instead of part->field
      as for partial fields, part->field points to
      a temporary field that is only part of the original
      field.  field_index always points to the original field
    */
    Field *field= head->field[part->field->field_index];
    field->register_field_in_read_map();
  }
}


void QUICK_ROR_INTERSECT_SELECT::add_used_key_part_to_set()
{
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> it(quick_selects);
  QUICK_SELECT_WITH_RECORD *quick;
  while ((quick= it++))
  {
    quick->quick->add_used_key_part_to_set();
  }
}


void QUICK_INDEX_SORT_SELECT::add_used_key_part_to_set()
{
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    quick->add_used_key_part_to_set();
  }
  if (pk_quick_select)
    pk_quick_select->add_used_key_part_to_set();
}


void QUICK_ROR_UNION_SELECT::add_used_key_part_to_set()
{
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);

  while ((quick= it++))
  {
    quick->add_used_key_part_to_set();
  }
}


/*******************************************************************************
* Implementation of QUICK_GROUP_MIN_MAX_SELECT
*******************************************************************************/

static inline uint get_field_keypart(KEY *index, Field *field);
static bool get_sel_arg_for_keypart(Field *field, SEL_ARG *index_range_tree,
                                    SEL_ARG **cur_range);
static bool get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
                       KEY_PART_INFO *first_non_group_part,
                       KEY_PART_INFO *min_max_arg_part,
                       KEY_PART_INFO *last_part, THD *thd,
                       uchar *key_infix, uint *key_infix_len,
                       KEY_PART_INFO **first_non_infix_part);
static bool
check_group_min_max_predicates(Item *cond, Item_field *min_max_arg_item,
                               Field::imagetype image_type,
                               bool *has_min_max_fld, bool *has_other_fld);

static void
cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
                   uint group_key_parts, SEL_TREE *range_tree,
                   SEL_ARG *index_tree, ha_rows quick_prefix_records,
                   bool have_min, bool have_max,
                   double *read_cost, ha_rows *records);


/**
  Test if this access method is applicable to a GROUP query with MIN/MAX
  functions, and if so, construct a new TRP object.

  DESCRIPTION
    Test whether a query can be computed via a QUICK_GROUP_MIN_MAX_SELECT.
    Queries computable via a QUICK_GROUP_MIN_MAX_SELECT must satisfy the
    following conditions:
    A) Table T has at least one compound index I of the form:
       I = <A_1, ...,A_k, [B_1,..., B_m], C, [D_1,...,D_n]>
    B) Query conditions:
    B0. Q is over a single table T.
    B1. The attributes referenced by Q are a subset of the attributes of I.
    B2. All attributes QA in Q can be divided into 3 overlapping groups:
        - SA = {S_1, ..., S_l, [C]} - from the SELECT clause, where C is
          referenced by any number of MIN and/or MAX functions if present.
        - WA = {W_1, ..., W_p} - from the WHERE clause
        - GA = <G_1, ..., G_k> - from the GROUP BY clause (if any)
             = SA              - if Q is a DISTINCT query (based on the
                                 equivalence of DISTINCT and GROUP queries.
        - NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
          GROUP BY and not referenced by MIN/MAX functions.
        with the following properties specified below.
    B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not 
        applicable.

    SA1. There is at most one attribute in SA referenced by any number of
         MIN and/or MAX functions which, which if present, is denoted as C.
    SA2. The position of the C attribute in the index is after the last A_k.
    SA3. The attribute C can be referenced in the WHERE clause only in
         predicates of the forms:
         - (C {< | <= | > | >= | =} const)
         - (const {< | <= | > | >= | =} C)
         - (C between const_i and const_j)
         - C IS NULL
         - C IS NOT NULL
         - C != const
    SA4. If Q has a GROUP BY clause, there are no other aggregate functions
         except MIN and MAX. For queries with DISTINCT, aggregate functions
         are allowed.
    SA5. The select list in DISTINCT queries should not contain expressions.
    SA6. Clustered index can not be used by GROUP_MIN_MAX quick select
         for AGG_FUNC(DISTINCT ...) optimization because cursor position is
         never stored after a unique key lookup in the clustered index and
         furhter index_next/prev calls can not be used. So loose index scan
         optimization can not be used in this case.
    SA7. If Q has both AGG_FUNC(DISTINCT ...) and MIN/MAX() functions then this
         access method is not used.
         For above queries MIN/MAX() aggregation has to be done at
         nested_loops_join (end_send_group). But with current design MIN/MAX()
         is always set as part of loose index scan. Because of this mismatch
         MIN() and MAX() values will be set incorrectly. For such queries to
         work we need a new interface for loose index scan. This new interface
         should only fetch records with min and max values and let
         end_send_group to do aggregation. Until then do not use
         loose_index_scan.
    GA1. If Q has a GROUP BY clause, then GA is a prefix of I. That is, if
         G_i = A_j => i = j.
    GA2. If Q has a DISTINCT clause, then there is a permutation of SA that
         forms a prefix of I. This permutation is used as the GROUP clause
         when the DISTINCT query is converted to a GROUP query.
    GA3. The attributes in GA may participate in arbitrary predicates, divided
         into two groups:
         - RNG(G_1,...,G_q ; where q <= k) is a range condition over the
           attributes of a prefix of GA
         - PA(G_i1,...G_iq) is an arbitrary predicate over an arbitrary subset
           of GA. Since P is applied to only GROUP attributes it filters some
           groups, and thus can be applied after the grouping.
    GA4. There are no expressions among G_i, just direct column references.
    NGA1.If in the index I there is a gap between the last GROUP attribute G_k,
         and the MIN/MAX attribute C, then NGA must consist of exactly the
         index attributes that constitute the gap. As a result there is a
         permutation of NGA, BA=<B_1,...,B_m>, that coincides with the gap
         in the index.
    NGA2.If BA <> {}, then the WHERE clause must contain a conjunction EQ of
         equality conditions for all NG_i of the form (NG_i = const) or
         (const = NG_i), such that each NG_i is referenced in exactly one
         conjunct. Informally, the predicates provide constants to fill the
         gap in the index.
    NGA3.If BA <> {}, there can only be one range. TODO: This is a code
         limitation and is not strictly needed. See BUG#15947433
    WA1. There are no other attributes in the WHERE clause except the ones
         referenced in predicates RNG, PA, PC, EQ defined above. Therefore
         WA is subset of (GA union NGA union C) for GA,NGA,C that pass the
         above tests. By transitivity then it also follows that each WA_i
         participates in the index I (if this was already tested for GA, NGA
         and C).
    WA2. If there is a predicate on C, then it must be in conjunction
         to all predicates on all earlier keyparts in I.

    C) Overall query form:
       SELECT EXPR([A_1,...,A_k], [B_1,...,B_m], [MIN(C)], [MAX(C)])
         FROM T
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
         [AND EQ(B_1,...,B_m)]
         [AND PC(C)]
         [AND PA(A_i1,...,A_iq)]
       GROUP BY A_1,...,A_k
       [HAVING PH(A_1, ..., B_1,..., C)]
    where EXPR(...) is an arbitrary expression over some or all SELECT fields,
    or:
       SELECT DISTINCT A_i1,...,A_ik
         FROM T
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
         [AND PA(A_i1,...,A_iq)];

  NOTES
    If the current query satisfies the conditions above, and if
    (mem_root! = NULL), then the function constructs and returns a new TRP
    object, that is later used to construct a new QUICK_GROUP_MIN_MAX_SELECT.
    If (mem_root == NULL), then the function only tests whether the current
    query satisfies the conditions above, and, if so, sets
    is_applicable = TRUE.

    Queries with DISTINCT for which index access can be used are transformed
    into equivalent group-by queries of the form:

    SELECT A_1,...,A_k FROM T
     WHERE [RNG(A_1,...,A_p ; where p <= k)]
      [AND PA(A_i1,...,A_iq)]
    GROUP BY A_1,...,A_k;

    The group-by list is a permutation of the select attributes, according
    to their order in the index.

  TODO
  - What happens if the query groups by the MIN/MAX field, and there is no
    other field as in: "select MY_MIN(a) from t1 group by a" ?
  - We assume that the general correctness of the GROUP-BY query was checked
    before this point. Is this correct, or do we have to check it completely?
  - Lift the limitation in condition (B3), that is, make this access method 
    applicable to ROLLUP queries.

 @param  param     Parameter from test_quick_select
 @param  sel_tree  Range tree generated by get_mm_tree
 @param  read_time Best read time so far (=table/index scan time)
 @return table read plan
   @retval NULL  Loose index scan not applicable or mem_root == NULL
   @retval !NULL Loose index scan table read plan
*/

static TRP_GROUP_MIN_MAX *
get_best_group_min_max(PARAM *param, SEL_TREE *tree, double read_time)
{
  THD *thd= param->thd;
  JOIN *join= thd->lex->current_select->join;
  TABLE *table= param->table;
  bool have_min= FALSE;              /* TRUE if there is a MIN function. */
  bool have_max= FALSE;              /* TRUE if there is a MAX function. */
  Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
  KEY_PART_INFO *min_max_arg_part= NULL; /* The corresponding keypart. */
  uint group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
  KEY *index_info= NULL;    /* The index chosen for data access. */
  uint index= 0;            /* The id of the chosen index. */
  uint group_key_parts= 0;  // Number of index key parts in the group prefix.
  uint used_key_parts= 0;   /* Number of index key parts used for access. */
  uchar key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
  uint key_infix_len= 0;          /* Length of key_infix. */
  TRP_GROUP_MIN_MAX *read_plan= NULL; /* The eventually constructed TRP. */
  uint key_part_nr;
  uint elements_in_group;
  ORDER *tmp_group;
  Item *item;
  Item_field *item_field;
  bool is_agg_distinct;
  List<Item_field> agg_distinct_flds;

  DBUG_ENTER("get_best_group_min_max");

  /* Perform few 'cheap' tests whether this access method is applicable. */
  if (!join)
    DBUG_RETURN(NULL);        /* This is not a select statement. */
  if ((join->table_count != 1) ||  /* The query must reference one table. */
      (join->select_lex->olap == ROLLUP_TYPE)) /* Check (B3) for ROLLUP */
    DBUG_RETURN(NULL);
  if (table->s->keys == 0)        /* There are no indexes to use. */
    DBUG_RETURN(NULL);
  if (join->conds && join->conds->used_tables() & OUTER_REF_TABLE_BIT)
    DBUG_RETURN(NULL); /* Cannot execute with correlated conditions. */

  /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
  List_iterator<Item> select_items_it(join->fields_list);
  is_agg_distinct = is_indexed_agg_distinct(join, &agg_distinct_flds);

  if ((!join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
      (!join->select_distinct) &&
      !is_agg_distinct)
    DBUG_RETURN(NULL);
  /* Analyze the query in more detail. */

  if (join->sum_funcs[0])
  {
    Item_sum *min_max_item;
    Item_sum **func_ptr= join->sum_funcs;
    while ((min_max_item= *(func_ptr++)))
    {
      if (min_max_item->sum_func() == Item_sum::MIN_FUNC)
        have_min= TRUE;
      else if (min_max_item->sum_func() == Item_sum::MAX_FUNC)
        have_max= TRUE;
      else if (is_agg_distinct &&
               (min_max_item->sum_func() == Item_sum::COUNT_DISTINCT_FUNC ||
                min_max_item->sum_func() == Item_sum::SUM_DISTINCT_FUNC ||
                min_max_item->sum_func() == Item_sum::AVG_DISTINCT_FUNC))
        continue;
      else
        DBUG_RETURN(NULL);

      /* The argument of MIN/MAX. */
      Item *expr= min_max_item->get_arg(0)->real_item();
      if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
      {
        if (! min_max_arg_item)
          min_max_arg_item= (Item_field*) expr;
        else if (! min_max_arg_item->eq(expr, 1))
          DBUG_RETURN(NULL);
      }
      else
        DBUG_RETURN(NULL);
    }
  }

  /* Check (SA7). */
  if (is_agg_distinct && (have_max || have_min))
  {
    DBUG_RETURN(NULL);
  }

  /* Check (SA5). */
  if (join->select_distinct)
  {
    while ((item= select_items_it++))
    {
      if (item->real_item()->type() != Item::FIELD_ITEM)
        DBUG_RETURN(NULL);
    }
  }

  /* Check (GA4) - that there are no expressions among the group attributes. */
  elements_in_group= 0;
  for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
  {
    if ((*tmp_group->item)->real_item()->type() != Item::FIELD_ITEM)
      DBUG_RETURN(NULL);
    elements_in_group++;
  }

  /*
    Check that table has at least one compound index such that the conditions
    (GA1,GA2) are all TRUE. If there is more than one such index, select the
    first one. Here we set the variables: group_prefix_len and index_info.
  */
  /* Cost-related variables for the best index so far. */
  double best_read_cost= DBL_MAX;
  ha_rows best_records= 0;
  SEL_ARG *best_index_tree= NULL;
  ha_rows best_quick_prefix_records= 0;
  uint best_param_idx= 0;

  const uint pk= param->table->s->primary_key;
  uint max_key_part;  
  SEL_ARG *cur_index_tree= NULL;
  ha_rows cur_quick_prefix_records= 0;

  // We go through allowed indexes
  for (uint cur_param_idx= 0; cur_param_idx < param->keys ; ++cur_param_idx)
  {
    const uint cur_index= param->real_keynr[cur_param_idx];
    KEY *const cur_index_info= &table->key_info[cur_index];
    KEY_PART_INFO *cur_part;
    KEY_PART_INFO *end_part; /* Last part for loops. */
    /* Last index part. */
    KEY_PART_INFO *last_part;
    KEY_PART_INFO *first_non_group_part;
    KEY_PART_INFO *first_non_infix_part;
    uint key_parts;
    uint key_infix_parts;
    uint cur_group_key_parts= 0;
    uint cur_group_prefix_len= 0;
    double cur_read_cost;
    ha_rows cur_records;
    key_map used_key_parts_map;
    uint cur_key_infix_len= 0;
    uchar cur_key_infix[MAX_KEY_LENGTH];
    uint cur_used_key_parts;
    
    /*
      Check (B1) - if current index is covering.
      (was also: "Exclude UNIQUE indexes ..." but this was removed because 
      there are cases Loose Scan over a multi-part index is useful).
    */
    if (!table->covering_keys.is_set(cur_index) ||
        !table->keys_in_use_for_group_by.is_set(cur_index))
      continue;
    
    /*
      This function is called on the precondition that the index is covering.
      Therefore if the GROUP BY list contains more elements than the index,
      these are duplicates. The GROUP BY list cannot be a prefix of the index.
    */
    if (elements_in_group > table->actual_n_key_parts(cur_index_info))
      continue;
    
    /*
      Unless extended keys can be used for cur_index:
      If the current storage manager is such that it appends the primary key to
      each index, then the above condition is insufficient to check if the
      index is covering. In such cases it may happen that some fields are
      covered by the PK index, but not by the current index. Since we can't
      use the concatenation of both indexes for index lookup, such an index
      does not qualify as covering in our case. If this is the case, below
      we check that all query fields are indeed covered by 'cur_index'.
    */
    if (cur_index_info->user_defined_key_parts == table->actual_n_key_parts(cur_index_info)
        && pk < MAX_KEY && cur_index != pk &&
        (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
    {
      /* For each table field */
      for (uint i= 0; i < table->s->fields; i++)
      {
        Field *cur_field= table->field[i];
        /*
          If the field is used in the current query ensure that it's
          part of 'cur_index'
        */
        if (bitmap_is_set(table->read_set, cur_field->field_index) &&
            !cur_field->part_of_key_not_clustered.is_set(cur_index))
          goto next_index;                  // Field was not part of key
      }
    }

    max_key_part= 0;
    used_key_parts_map.clear_all();

    /*
      Check (GA1) for GROUP BY queries.
    */
    if (join->group_list)
    {
      cur_part= cur_index_info->key_part;
      end_part= cur_part + table->actual_n_key_parts(cur_index_info);
      /* Iterate in parallel over the GROUP list and the index parts. */
      for (tmp_group= join->group_list; tmp_group && (cur_part != end_part);
           tmp_group= tmp_group->next, cur_part++)
      {
        /*
          TODO:
          tmp_group::item is an array of Item, is it OK to consider only the
          first Item? If so, then why? What is the array for?
        */
        /* Above we already checked that all group items are fields. */
        DBUG_ASSERT((*tmp_group->item)->real_item()->type() == Item::FIELD_ITEM);
        Item_field *group_field= (Item_field *) (*tmp_group->item)->real_item();
        if (group_field->field->eq(cur_part->field))
        {
          cur_group_prefix_len+= cur_part->store_length;
          ++cur_group_key_parts;
          max_key_part= cur_part - cur_index_info->key_part + 1;
          used_key_parts_map.set_bit(max_key_part);
        }
        else
          goto next_index;
      }
    }
    /*
      Check (GA2) if this is a DISTINCT query.
      If GA2, then Store a new ORDER object in group_fields_array at the
      position of the key part of item_field->field. Thus we get the ORDER
      objects for each field ordered as the corresponding key parts.
      Later group_fields_array of ORDER objects is used to convert the query
      to a GROUP query.
    */
    if ((!join->group && join->select_distinct) ||
        is_agg_distinct)
    {
      if (!is_agg_distinct)
      {
        select_items_it.rewind();
      }

      List_iterator<Item_field> agg_distinct_flds_it (agg_distinct_flds);
      while (NULL != (item = (is_agg_distinct ?
             (Item *) agg_distinct_flds_it++ : select_items_it++)))
      {
        /* (SA5) already checked above. */
        item_field= (Item_field*) item->real_item(); 
        DBUG_ASSERT(item->real_item()->type() == Item::FIELD_ITEM);

        /* not doing loose index scan for derived tables */
        if (!item_field->field)
          goto next_index;

        /* Find the order of the key part in the index. */
        key_part_nr= get_field_keypart(cur_index_info, item_field->field);
        /*
          Check if this attribute was already present in the select list.
          If it was present, then its corresponding key part was alredy used.
        */
        if (used_key_parts_map.is_set(key_part_nr))
          continue;
        if (key_part_nr < 1 ||
            (!is_agg_distinct && key_part_nr > join->fields_list.elements))
          goto next_index;
        cur_part= cur_index_info->key_part + key_part_nr - 1;
        cur_group_prefix_len+= cur_part->store_length;
        used_key_parts_map.set_bit(key_part_nr);
        ++cur_group_key_parts;
        max_key_part= MY_MAX(max_key_part,key_part_nr);
      }
      /*
        Check that used key parts forms a prefix of the index.
        To check this we compare bits in all_parts and cur_parts.
        all_parts have all bits set from 0 to (max_key_part-1).
        cur_parts have bits set for only used keyparts.
      */
      ulonglong all_parts, cur_parts;
      all_parts= (1ULL << max_key_part) - 1;
      cur_parts= used_key_parts_map.to_ulonglong() >> 1;
      if (all_parts != cur_parts)
        goto next_index;
    }

    /* Check (SA2). */
    if (min_max_arg_item)
    {
      key_part_nr= get_field_keypart(cur_index_info, min_max_arg_item->field);
      if (key_part_nr <= cur_group_key_parts)
        goto next_index;
      min_max_arg_part= cur_index_info->key_part + key_part_nr - 1;
    }

    /*
      Aplly a heuristic: there is no point to use loose index scan when we're
      using the whole unique index.
    */
    if (cur_index_info->flags & HA_NOSAME && 
        cur_group_key_parts == cur_index_info->user_defined_key_parts)
    {
      goto next_index;
    }

    /*
      Check (NGA1, NGA2) and extract a sequence of constants to be used as part
      of all search keys.
    */

    /*
      If there is MIN/MAX, each keypart between the last group part and the
      MIN/MAX part must participate in one equality with constants, and all
      keyparts after the MIN/MAX part must not be referenced in the query.

      If there is no MIN/MAX, the keyparts after the last group part can be
      referenced only in equalities with constants, and the referenced keyparts
      must form a sequence without any gaps that starts immediately after the
      last group keypart.
    */
    key_parts= table->actual_n_key_parts(cur_index_info);
    last_part= cur_index_info->key_part + key_parts;
    first_non_group_part= (cur_group_key_parts < key_parts) ?
                          cur_index_info->key_part + cur_group_key_parts :
                          NULL;
    first_non_infix_part= min_max_arg_part ?
                          (min_max_arg_part < last_part) ?
                             min_max_arg_part :
                             NULL :
                           NULL;
    if (first_non_group_part &&
        (!min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
    {
      if (tree)
      {
        SEL_ARG *index_range_tree= tree->keys[cur_param_idx];
        if (!get_constant_key_infix(cur_index_info, index_range_tree,
                                    first_non_group_part, min_max_arg_part,
                                    last_part, thd, cur_key_infix, 
                                    &cur_key_infix_len,
                                    &first_non_infix_part))
          goto next_index;
      }
      else if (min_max_arg_part &&
               (min_max_arg_part - first_non_group_part > 0))
      {
        /*
          There is a gap but no range tree, thus no predicates at all for the
          non-group keyparts.
        */
        goto next_index;
      }
      else if (first_non_group_part && join->conds)
      {
        /*
          If there is no MIN/MAX function in the query, but some index
          key part is referenced in the WHERE clause, then this index
          cannot be used because the WHERE condition over the keypart's
          field cannot be 'pushed' to the index (because there is no
          range 'tree'), and the WHERE clause must be evaluated before
          GROUP BY/DISTINCT.
        */
        /*
          Store the first and last keyparts that need to be analyzed
          into one array that can be passed as parameter.
        */
        KEY_PART_INFO *key_part_range[2];
        key_part_range[0]= first_non_group_part;
        key_part_range[1]= last_part;

        /* Check if cur_part is referenced in the WHERE clause. */
        if (join->conds->walk(&Item::find_item_in_field_list_processor, 0,
                              key_part_range))
          goto next_index;
      }
    }

    /*
      Test (WA1) partially - that no other keypart after the last infix part is
      referenced in the query.
    */
    if (first_non_infix_part)
    {
      cur_part= first_non_infix_part +
                (min_max_arg_part && (min_max_arg_part < last_part));
      for (; cur_part != last_part; cur_part++)
      {
        if (bitmap_is_set(table->read_set, cur_part->field->field_index))
          goto next_index;
      }
    }

    /**
      Test WA2:If there are conditions on a column C participating in
      MIN/MAX, those conditions must be conjunctions to all earlier
      keyparts. Otherwise, Loose Index Scan cannot be used.
    */
    if (tree && min_max_arg_item)
    {
      SEL_ARG *index_range_tree= tree->keys[cur_param_idx];
      SEL_ARG *cur_range= NULL;
      if (get_sel_arg_for_keypart(min_max_arg_part->field,
                                  index_range_tree, &cur_range) ||
          (cur_range && cur_range->type != SEL_ARG::KEY_RANGE))
      {
        goto next_index;
      }
    }

    /* If we got to this point, cur_index_info passes the test. */
    key_infix_parts= cur_key_infix_len ? (uint) 
                     (first_non_infix_part - first_non_group_part) : 0;
    cur_used_key_parts= cur_group_key_parts + key_infix_parts;

    /* Compute the cost of using this index. */
    if (tree)
    {
      cur_index_tree= tree->keys[cur_param_idx];
      /* Check if this range tree can be used for prefix retrieval. */
      Cost_estimate dummy_cost;
      uint mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
      uint mrr_bufsize=0;
      cur_quick_prefix_records= check_quick_select(param, cur_param_idx,
                                                   FALSE /*don't care*/,
                                                   cur_index_tree, TRUE,
                                                   &mrr_flags, &mrr_bufsize,
                                                   &dummy_cost);
    }
    cost_group_min_max(table, cur_index_info, cur_used_key_parts,
                       cur_group_key_parts, tree, cur_index_tree,
                       cur_quick_prefix_records, have_min, have_max,
                       &cur_read_cost, &cur_records);
    /*
      If cur_read_cost is lower than best_read_cost use cur_index.
      Do not compare doubles directly because they may have different
      representations (64 vs. 80 bits).
    */
    if (cur_read_cost < best_read_cost - (DBL_EPSILON * cur_read_cost))
    {
      index_info= cur_index_info;
      index= cur_index;
      best_read_cost= cur_read_cost;
      best_records= cur_records;
      best_index_tree= cur_index_tree;
      best_quick_prefix_records= cur_quick_prefix_records;
      best_param_idx= cur_param_idx;
      group_key_parts= cur_group_key_parts;
      group_prefix_len= cur_group_prefix_len;
      key_infix_len= cur_key_infix_len;
      if (key_infix_len)
        memcpy (key_infix, cur_key_infix, sizeof (key_infix));
      used_key_parts= cur_used_key_parts;
    }

  next_index:;
  }
  if (!index_info) /* No usable index found. */
    DBUG_RETURN(NULL);

  /* Check (SA3) for the where clause. */
  bool has_min_max_fld= false, has_other_fld= false;
  if (join->conds && min_max_arg_item &&
      !check_group_min_max_predicates(join->conds, min_max_arg_item,
                                      (index_info->flags & HA_SPATIAL) ?
                                      Field::itMBR : Field::itRAW,
                                      &has_min_max_fld, &has_other_fld))
    DBUG_RETURN(NULL);

  /*
    Check (SA6) if clustered key is used
  */
  if (is_agg_distinct && index == table->s->primary_key &&
      table->file->primary_key_is_clustered())
    DBUG_RETURN(NULL);

  /* The query passes all tests, so construct a new TRP object. */
  read_plan= new (param->mem_root)
                 TRP_GROUP_MIN_MAX(have_min, have_max, is_agg_distinct,
                                   min_max_arg_part,
                                   group_prefix_len, used_key_parts,
                                   group_key_parts, index_info, index,
                                   key_infix_len,
                                   (key_infix_len > 0) ? key_infix : NULL,
                                   tree, best_index_tree, best_param_idx,
                                   best_quick_prefix_records);
  if (read_plan)
  {
    if (tree && read_plan->quick_prefix_records == 0)
      DBUG_RETURN(NULL);

    read_plan->read_cost= best_read_cost;
    read_plan->records=   best_records;
    if (read_time < best_read_cost && is_agg_distinct)
    {
      read_plan->read_cost= 0;
      read_plan->use_index_scan();
    }

    DBUG_PRINT("info",
               ("Returning group min/max plan: cost: %g, records: %lu",
                read_plan->read_cost, (ulong) read_plan->records));
  }

  DBUG_RETURN(read_plan);
}


/*
  Check that the MIN/MAX attribute participates only in range predicates
  with constants.

  SYNOPSIS
    check_group_min_max_predicates()
    cond            [in]  the expression tree being analyzed
    min_max_arg     [in]  the field referenced by the MIN/MAX function(s)
    image_type      [in]
    has_min_max_arg [out] true if the subtree being analyzed references
                          min_max_arg
    has_other_arg   [out] true if the subtree being analyzed references a
                          column other min_max_arg

  DESCRIPTION
    The function walks recursively over the cond tree representing a WHERE
    clause, and checks condition (SA3) - if a field is referenced by a MIN/MAX
    aggregate function, it is referenced only by one of the following
    predicates $FUNC$:
    {=, !=, <, <=, >, >=, between, is [not] null, multiple equal}.
    In addition the function checks that the WHERE condition is equivalent to
    "cond1 AND cond2" where :
    cond1 - does not use min_max_column at all.
    cond2 - is an AND/OR tree with leaves in form
    "$FUNC$(min_max_column[, const])".

  RETURN
    TRUE  if cond passes the test
    FALSE o/w
*/

static bool
check_group_min_max_predicates(Item *cond, Item_field *min_max_arg_item,
                               Field::imagetype image_type,
                               bool *has_min_max_arg, bool *has_other_arg)
{
  DBUG_ENTER("check_group_min_max_predicates");
  DBUG_ASSERT(cond && min_max_arg_item);

  cond= cond->real_item();
  Item::Type cond_type= cond->real_type();
  if (cond_type == Item::COND_ITEM) /* 'AND' or 'OR' */
  {
    DBUG_PRINT("info", ("Analyzing: %s", ((Item_func*) cond)->func_name()));
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
    Item *and_or_arg;
    Item_func::Functype func_type= ((Item_cond*) cond)->functype();
    bool has_min_max= false, has_other= false;
    while ((and_or_arg= li++))
    {
      /*
        The WHERE clause doesn't pass the condition if:
        (1) any subtree doesn't pass the condition or
        (2) the subtree passes the test, but it is an OR and it references both
            the min/max argument and other columns.
      */
      if (!check_group_min_max_predicates(and_or_arg, min_max_arg_item,     //1
                                          image_type,
                                          &has_min_max, &has_other) ||
          (func_type == Item_func::COND_OR_FUNC && has_min_max && has_other))//2
        DBUG_RETURN(FALSE);
    }
    *has_min_max_arg= has_min_max || *has_min_max_arg;
    *has_other_arg= has_other || *has_other_arg;
    DBUG_RETURN(TRUE);
  }

  /*
    Disallow loose index scan if the MIN/MAX argument field is referenced by
    a subquery in the WHERE clause.
  */

  if (unlikely(cond_type == Item::SUBSELECT_ITEM))
  {
    Item_subselect *subs_cond= (Item_subselect*) cond;
    if (subs_cond->is_correlated)
    {
      DBUG_ASSERT(subs_cond->upper_refs.elements > 0);
      List_iterator_fast<Item_subselect::Ref_to_outside>
        li(subs_cond->upper_refs);
      Item_subselect::Ref_to_outside *dep;
      while ((dep= li++))
      {
        if (dep->item->eq(min_max_arg_item, FALSE))
          DBUG_RETURN(FALSE);
      }
    }
    DBUG_RETURN(TRUE);
  }
  /*
    Subquery with IS [NOT] NULL
    TODO: Look into the cache_item and optimize it like we do for
    subselect's above
   */
  if (unlikely(cond_type == Item::CACHE_ITEM))
    DBUG_RETURN(cond->const_item());
  
  /*
    Condition of the form 'field' is equivalent to 'field <> 0' and thus
    satisfies the SA3 condition.
  */
  if (cond_type == Item::FIELD_ITEM)
  {
    DBUG_PRINT("info", ("Analyzing: %s", cond->full_name()));
    if (min_max_arg_item->eq((Item_field*)cond, 1))
      *has_min_max_arg= true;
    else
      *has_other_arg= true;
    DBUG_RETURN(TRUE);
  }

  /* We presume that at this point there are no other Items than functions. */
  DBUG_ASSERT(cond_type == Item::FUNC_ITEM);
  if (unlikely(cond_type != Item::FUNC_ITEM))   /* Safety */
    DBUG_RETURN(FALSE);
  
  /* Test if cond references only group-by or non-group fields. */
  Item_func *pred= (Item_func*) cond;
  Item_func::Functype pred_type= pred->functype();
  DBUG_PRINT("info", ("Analyzing: %s", pred->func_name()));
  if (pred_type == Item_func::MULT_EQUAL_FUNC)
  {
    /*
      Check that each field in a multiple equality is either a constant or
      it is a reference to the min/max argument, or it doesn't contain the
      min/max argument at all.
    */
    Item_equal_fields_iterator eq_it(*((Item_equal*)pred));
    Item *eq_item;
    bool has_min_max= false, has_other= false;
    while ((eq_item= eq_it++))
    {
      if (min_max_arg_item->eq(eq_item->real_item(), 1))
        has_min_max= true;
      else
        has_other= true;
    }
    *has_min_max_arg= has_min_max || *has_min_max_arg;
    *has_other_arg= has_other || *has_other_arg;
    DBUG_RETURN(!(has_min_max && has_other));
  }

  Item **arguments= pred->arguments();
  Item *cur_arg;
  bool has_min_max= false, has_other= false;
  for (uint arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
  {
    cur_arg= arguments[arg_idx]->real_item();
    DBUG_PRINT("info", ("cur_arg: %s", cur_arg->full_name()));
    if (cur_arg->type() == Item::FIELD_ITEM)
    {
      if (min_max_arg_item->eq(cur_arg, 1)) 
      {
        has_min_max= true;
        /*
          If pred references the MIN/MAX argument, check whether pred is a range
          condition that compares the MIN/MAX argument with a constant.
        */
        if (pred_type != Item_func::EQUAL_FUNC     &&
            pred_type != Item_func::LT_FUNC        &&
            pred_type != Item_func::LE_FUNC        &&
            pred_type != Item_func::GT_FUNC        &&
            pred_type != Item_func::GE_FUNC        &&
            pred_type != Item_func::BETWEEN        &&
            pred_type != Item_func::ISNULL_FUNC    &&
            pred_type != Item_func::ISNOTNULL_FUNC &&
            pred_type != Item_func::EQ_FUNC        &&
            pred_type != Item_func::NE_FUNC)
          DBUG_RETURN(FALSE);

        /* Check that pred compares min_max_arg_item with a constant. */
        Item *args[3];
        bzero(args, 3 * sizeof(Item*));
        bool inv;
        /* Test if this is a comparison of a field and a constant. */
        if (!simple_pred(pred, args, &inv))
          DBUG_RETURN(FALSE);

        if (args[0] && args[1]) // this is a binary function or BETWEEN
        {
          DBUG_ASSERT(pred->is_bool_type());
          Item_bool_func *bool_func= (Item_bool_func*) pred;
          Field *field= min_max_arg_item->field;
          if (!args[2]) // this is a binary function
          {
            if (!field->can_optimize_group_min_max(bool_func, args[1]))
              DBUG_RETURN(FALSE);
          }
          else // this is BETWEEN
          {
            if (!field->can_optimize_group_min_max(bool_func, args[1]) ||
                !field->can_optimize_group_min_max(bool_func, args[2]))
              DBUG_RETURN(FALSE);
          }
        }
      }
      else
        has_other= true;
    }
    else if (cur_arg->type() == Item::FUNC_ITEM)
    {
      if (!check_group_min_max_predicates(cur_arg, min_max_arg_item, image_type,
                                          &has_min_max, &has_other))
        DBUG_RETURN(FALSE);
    }
    else if (cur_arg->const_item() && !cur_arg->is_expensive())
    {
      /*
        For predicates of the form "const OP expr" we also have to check 'expr'
        to make a decision.
      */
      continue;
    }
    else
      DBUG_RETURN(FALSE);
    if(has_min_max && has_other)
      DBUG_RETURN(FALSE);
  }
  *has_min_max_arg= has_min_max || *has_min_max_arg;
  *has_other_arg= has_other || *has_other_arg;

  DBUG_RETURN(TRUE);
}


/*
  Get the SEL_ARG tree 'tree' for the keypart covering 'field', if
  any. 'tree' must be a unique conjunction to ALL predicates in earlier
  keyparts of 'keypart_tree'.

  E.g., if 'keypart_tree' is for a composite index (kp1,kp2) and kp2
  covers 'field', all these conditions satisfies the requirement:

   1. "(kp1=2 OR kp1=3) AND kp2=10"    => returns "kp2=10"
   2. "(kp1=2 AND kp2=10) OR (kp1=3 AND kp2=10)"  => returns "kp2=10"
   3. "(kp1=2 AND (kp2=10 OR kp2=11)) OR (kp1=3 AND (kp2=10 OR kp2=11))"
                                       => returns "kp2=10  OR kp2=11"

   whereas these do not
   1. "(kp1=2 AND kp2=10) OR kp1=3"
   2. "(kp1=2 AND kp2=10) OR (kp1=3 AND kp2=11)"
   3. "(kp1=2 AND kp2=10) OR (kp1=3 AND (kp2=10 OR kp2=11))"

   This function effectively tests requirement WA2. In combination with
   a test that the returned tree has no more than one range it is also
   a test of NGA3.

  @param[in]   field          The field we want the SEL_ARG tree for
  @param[in]   keypart_tree   Root node of the SEL_ARG* tree for the index
  @param[out]  cur_range      The SEL_ARG tree, if any, for the keypart
                              covering field 'keypart_field'
  @retval true   'keypart_tree' contained a predicate for 'field' that
                  is not conjunction to all predicates on earlier keyparts
  @retval false  otherwise
*/

static bool
get_sel_arg_for_keypart(Field *field,
                        SEL_ARG *keypart_tree,
                        SEL_ARG **cur_range)
{
  if (keypart_tree == NULL)
    return false;
  if (keypart_tree->field->eq(field))
  {
    *cur_range= keypart_tree;
    return false;
  }

  SEL_ARG *tree_first_range= NULL;
  SEL_ARG *first_kp=  keypart_tree->first();

  for (SEL_ARG *cur_kp= first_kp; cur_kp; cur_kp= cur_kp->next)
  {
    SEL_ARG *curr_tree= NULL;
    if (cur_kp->next_key_part)
    {
      if (get_sel_arg_for_keypart(field,
                                  cur_kp->next_key_part,
                                  &curr_tree))
        return true;
    }
    /*
      Check if the SEL_ARG tree for 'field' is identical for all ranges in
      'keypart_tree
     */
    if (cur_kp == first_kp)
      tree_first_range= curr_tree;
    else if (!all_same(tree_first_range, curr_tree))
      return true;
  }
  *cur_range= tree_first_range;
  return false;
}

/*
  Extract a sequence of constants from a conjunction of equality predicates.

  SYNOPSIS
    get_constant_key_infix()
    index_info             [in]  Descriptor of the chosen index.
    index_range_tree       [in]  Range tree for the chosen index
    first_non_group_part   [in]  First index part after group attribute parts
    min_max_arg_part       [in]  The keypart of the MIN/MAX argument if any
    last_part              [in]  Last keypart of the index
    thd                    [in]  Current thread
    key_infix              [out] Infix of constants to be used for index lookup
    key_infix_len          [out] Lenghth of the infix
    first_non_infix_part   [out] The first keypart after the infix (if any)

  DESCRIPTION
    Test conditions (NGA1, NGA2, NGA3) from get_best_group_min_max(). Namely,
    for each keypart field NG_i not in GROUP-BY, check that there is exactly one
    constant equality predicate among conds with the form (NG_i = const_ci) or
    (const_ci = NG_i).. In addition, there can only be one range when there is
    such a gap.
    Thus all the NGF_i attributes must fill the 'gap' between the last group-by
    attribute and the MIN/MAX attribute in the index (if present).  Also ensure
    that there is only a single range on NGF_i (NGA3). If these
    conditions hold, copy each constant from its corresponding predicate into
    key_infix, in the order its NG_i attribute appears in the index, and update
    key_infix_len with the total length of the key parts in key_infix.

  RETURN
    TRUE  if the index passes the test
    FALSE o/w
*/
static bool
get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
                       KEY_PART_INFO *first_non_group_part,
                       KEY_PART_INFO *min_max_arg_part,
                       KEY_PART_INFO *last_part, THD *thd,
                       uchar *key_infix, uint *key_infix_len,
                       KEY_PART_INFO **first_non_infix_part)
{
  SEL_ARG       *cur_range;
  KEY_PART_INFO *cur_part;
  /* End part for the first loop below. */
  KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;

  *key_infix_len= 0;
  uchar *key_ptr= key_infix;
  for (cur_part= first_non_group_part; cur_part != end_part; cur_part++)
  {
    cur_range= NULL;
    /*
      Check NGA3:
      1. get_sel_arg_for_keypart gets the range tree for the 'field' and also
         checks for a unique conjunction of this tree with all the predicates
         on the earlier keyparts in the index.
      2. Check for multiple ranges on the found keypart tree.

      We assume that index_range_tree points to the leftmost keypart in
      the index.
    */
    if (get_sel_arg_for_keypart(cur_part->field, index_range_tree,
                                &cur_range))
      return false;

    if (cur_range && cur_range->elements > 1)
      return false;

    if (!cur_range || cur_range->type != SEL_ARG::KEY_RANGE)
    {
      if (min_max_arg_part)
        return false; /* The current keypart has no range predicates at all. */
      else
      {
        *first_non_infix_part= cur_part;
        return true;
      }
    }

    if ((cur_range->min_flag & NO_MIN_RANGE) ||
        (cur_range->max_flag & NO_MAX_RANGE) ||
        (cur_range->min_flag & NEAR_MIN) || (cur_range->max_flag & NEAR_MAX))
      return false;

    uint field_length= cur_part->store_length;
    if (cur_range->maybe_null &&
         cur_range->min_value[0] && cur_range->max_value[0])
    {
      /*
        cur_range specifies 'IS NULL'. In this case the argument points
        to a "null value" (is_null_string) that may not always be long
        enough for a direct memcpy to a field.
      */
      DBUG_ASSERT (field_length > 0);
      *key_ptr= 1;
      bzero(key_ptr+1,field_length-1);
      key_ptr+= field_length;
      *key_infix_len+= field_length;
    }
    else if (memcmp(cur_range->min_value, cur_range->max_value, field_length) == 0)
    { /* cur_range specifies an equality condition. */
      memcpy(key_ptr, cur_range->min_value, field_length);
      key_ptr+= field_length;
      *key_infix_len+= field_length;
    }
    else
      return false;
  }

  if (!min_max_arg_part && (cur_part == last_part))
    *first_non_infix_part= last_part;

  return TRUE;
}


/*
  Find the key part referenced by a field.

  SYNOPSIS
    get_field_keypart()
    index  descriptor of an index
    field  field that possibly references some key part in index

  NOTES
    The return value can be used to get a KEY_PART_INFO pointer by
    part= index->key_part + get_field_keypart(...) - 1;

  RETURN
    Positive number which is the consecutive number of the key part, or
    0 if field does not reference any index field.
*/

static inline uint
get_field_keypart(KEY *index, Field *field)
{
  KEY_PART_INFO *part, *end;

  for (part= index->key_part,
         end= part + field->table->actual_n_key_parts(index);
       part < end; part++)
  {
    if (field->eq(part->field))
      return part - index->key_part + 1;
  }
  return 0;
}


/*
  Compute the cost of a quick_group_min_max_select for a particular index.

  SYNOPSIS
    cost_group_min_max()
    table                [in] The table being accessed
    index_info           [in] The index used to access the table
    used_key_parts       [in] Number of key parts used to access the index
    group_key_parts      [in] Number of index key parts in the group prefix
    range_tree           [in] Tree of ranges for all indexes
    index_tree           [in] The range tree for the current index
    quick_prefix_records [in] Number of records retrieved by the internally
			      used quick range select if any
    have_min             [in] True if there is a MIN function
    have_max             [in] True if there is a MAX function
    read_cost           [out] The cost to retrieve rows via this quick select
    records             [out] The number of rows retrieved

  DESCRIPTION
    This method computes the access cost of a TRP_GROUP_MIN_MAX instance and
    the number of rows returned.

  NOTES
    The cost computation distinguishes several cases:
    1) No equality predicates over non-group attributes (thus no key_infix).
       If groups are bigger than blocks on the average, then we assume that it
       is very unlikely that block ends are aligned with group ends, thus even
       if we look for both MIN and MAX keys, all pairs of neighbor MIN/MAX
       keys, except for the first MIN and the last MAX keys, will be in the
       same block.  If groups are smaller than blocks, then we are going to
       read all blocks.
    2) There are equality predicates over non-group attributes.
       In this case the group prefix is extended by additional constants, and
       as a result the min/max values are inside sub-groups of the original
       groups. The number of blocks that will be read depends on whether the
       ends of these sub-groups will be contained in the same or in different
       blocks. We compute the probability for the two ends of a subgroup to be
       in two different blocks as the ratio of:
       - the number of positions of the left-end of a subgroup inside a group,
         such that the right end of the subgroup is past the end of the buffer
         containing the left-end, and
       - the total number of possible positions for the left-end of the
         subgroup, which is the number of keys in the containing group.
       We assume it is very unlikely that two ends of subsequent subgroups are
       in the same block.
    3) The are range predicates over the group attributes.
       Then some groups may be filtered by the range predicates. We use the
       selectivity of the range predicates to decide how many groups will be
       filtered.

  TODO
     - Take into account the optional range predicates over the MIN/MAX
       argument.
     - Check if we have a PK index and we use all cols - then each key is a
       group, and it will be better to use an index scan.

  RETURN
    None
*/

void cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
                        uint group_key_parts, SEL_TREE *range_tree,
                        SEL_ARG *index_tree, ha_rows quick_prefix_records,
                        bool have_min, bool have_max,
                        double *read_cost, ha_rows *records)
{
  ha_rows table_records;
  ha_rows num_groups;
  ha_rows num_blocks;
  uint    keys_per_block;
  ha_rows keys_per_group;
  ha_rows keys_per_subgroup; /* Average number of keys in sub-groups */
                          /* formed by a key infix. */
  double p_overlap; /* Probability that a sub-group overlaps two blocks. */
  double quick_prefix_selectivity;
  double io_cost;
  DBUG_ENTER("cost_group_min_max");

  table_records= table->stat_records();
  keys_per_block= (uint) (table->file->stats.block_size / 2 /
                          (index_info->key_length + table->file->ref_length)
                          + 1);
  num_blocks= (ha_rows)(table_records / keys_per_block) + 1;

  /* Compute the number of keys in a group. */
  if (!group_key_parts)
  {
    /* Summary over the whole table */
    keys_per_group= table_records;
  }
  else
  {
    keys_per_group= (ha_rows) index_info->actual_rec_per_key(group_key_parts -
                                                             1);
  }

  if (keys_per_group == 0) /* If there is no statistics try to guess */
    /* each group contains 10% of all records */
    keys_per_group= (table_records / 10) + 1;
  num_groups= (table_records / keys_per_group) + 1;

  /* Apply the selectivity of the quick select for group prefixes. */
  if (range_tree && (quick_prefix_records != HA_POS_ERROR))
  {
    quick_prefix_selectivity= (double) quick_prefix_records /
                              (double) table_records;
    num_groups= (ha_rows) rint(num_groups * quick_prefix_selectivity);
    set_if_bigger(num_groups, 1);
  }

  if (used_key_parts > group_key_parts)
  { /*
      Compute the probability that two ends of a subgroup are inside
      different blocks.
    */
    keys_per_subgroup= (ha_rows) index_info->actual_rec_per_key(used_key_parts - 1);
    if (keys_per_subgroup >= keys_per_block) /* If a subgroup is bigger than */
      p_overlap= 1.0;       /* a block, it will overlap at least two blocks. */
    else
    {
      double blocks_per_group= (double) num_blocks / (double) num_groups;
      p_overlap= (blocks_per_group * (keys_per_subgroup - 1)) / keys_per_group;
      p_overlap= MY_MIN(p_overlap, 1.0);
    }
    io_cost= (double) MY_MIN(num_groups * (1 + p_overlap), num_blocks);
  }
  else
    io_cost= (keys_per_group > keys_per_block) ?
             (have_min && have_max) ? (double) (num_groups + 1) :
                                      (double) num_groups :
             (double) num_blocks;

  /*
    CPU cost must be comparable to that of an index scan as computed
    in SQL_SELECT::test_quick_select(). When the groups are small,
    e.g. for a unique index, using index scan will be cheaper since it
    reads the next record without having to re-position to it on every
    group. To make the CPU cost reflect this, we estimate the CPU cost
    as the sum of:
    1. Cost for evaluating the condition (similarly as for index scan).
    2. Cost for navigating the index structure (assuming a b-tree).
       Note: We only add the cost for one comparision per block. For a
             b-tree the number of comparisons will be larger.
       TODO: This cost should be provided by the storage engine.
  */
  const double tree_traversal_cost= 
    ceil(log(static_cast<double>(table_records))/
         log(static_cast<double>(keys_per_block))) * 
    1/double(2*TIME_FOR_COMPARE); 

  const double cpu_cost= num_groups *
                         (tree_traversal_cost + 1/double(TIME_FOR_COMPARE));

  *read_cost= io_cost + cpu_cost;
  *records= num_groups;

  DBUG_PRINT("info",
             ("table rows: %lu  keys/block: %u  keys/group: %lu  result rows: %lu  blocks: %lu",
              (ulong)table_records, keys_per_block, (ulong) keys_per_group, 
              (ulong) *records, (ulong) num_blocks));
  DBUG_VOID_RETURN;
}


/*
  Construct a new quick select object for queries with group by with min/max.

  SYNOPSIS
    TRP_GROUP_MIN_MAX::make_quick()
    param              Parameter from test_quick_select
    retrieve_full_rows ignored
    parent_alloc       Memory pool to use, if any.

  NOTES
    Make_quick ignores the retrieve_full_rows parameter because
    QUICK_GROUP_MIN_MAX_SELECT always performs 'index only' scans.
    The other parameter are ignored as well because all necessary
    data to create the QUICK object is computed at this TRP creation
    time.

  RETURN
    New QUICK_GROUP_MIN_MAX_SELECT object if successfully created,
    NULL otherwise.
*/

QUICK_SELECT_I *
TRP_GROUP_MIN_MAX::make_quick(PARAM *param, bool retrieve_full_rows,
                              MEM_ROOT *parent_alloc)
{
  QUICK_GROUP_MIN_MAX_SELECT *quick;
  DBUG_ENTER("TRP_GROUP_MIN_MAX::make_quick");

  quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
                                        param->thd->lex->current_select->join,
                                        have_min, have_max, 
                                        have_agg_distinct, min_max_arg_part,
                                        group_prefix_len, group_key_parts,
                                        used_key_parts, index_info, index,
                                        read_cost, records, key_infix_len,
                                        key_infix, parent_alloc, is_index_scan);
  if (!quick)
    DBUG_RETURN(NULL);

  if (quick->init())
  {
    delete quick;
    DBUG_RETURN(NULL);
  }

  if (range_tree)
  {
    DBUG_ASSERT(quick_prefix_records > 0);
    if (quick_prefix_records == HA_POS_ERROR)
      quick->quick_prefix_select= NULL; /* Can't construct a quick select. */
    else
      /* Make a QUICK_RANGE_SELECT to be used for group prefix retrieval. */
      quick->quick_prefix_select= get_quick_select(param, param_idx,
                                                   index_tree,
                                                   HA_MRR_USE_DEFAULT_IMPL, 0,
                                                   &quick->alloc);

    /*
      Extract the SEL_ARG subtree that contains only ranges for the MIN/MAX
      attribute, and create an array of QUICK_RANGES to be used by the
      new quick select.
    */
    if (min_max_arg_part)
    {
      SEL_ARG *min_max_range= index_tree;
      while (min_max_range) /* Find the tree for the MIN/MAX key part. */
      {
        if (min_max_range->field->eq(min_max_arg_part->field))
          break;
        min_max_range= min_max_range->next_key_part;
      }
      /* Scroll to the leftmost interval for the MIN/MAX argument. */
      while (min_max_range && min_max_range->prev)
        min_max_range= min_max_range->prev;
      /* Create an array of QUICK_RANGEs for the MIN/MAX argument. */
      while (min_max_range)
      {
        if (quick->add_range(min_max_range))
        {
          delete quick;
          quick= NULL;
          DBUG_RETURN(NULL);
        }
        min_max_range= min_max_range->next;
      }
    }
  }
  else
    quick->quick_prefix_select= NULL;

  quick->update_key_stat();
  quick->adjust_prefix_ranges();

  DBUG_RETURN(quick);
}


/*
  Construct new quick select for group queries with min/max.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::QUICK_GROUP_MIN_MAX_SELECT()
    table             The table being accessed
    join              Descriptor of the current query
    have_min          TRUE if the query selects a MIN function
    have_max          TRUE if the query selects a MAX function
    min_max_arg_part  The only argument field of all MIN/MAX functions
    group_prefix_len  Length of all key parts in the group prefix
    prefix_key_parts  All key parts in the group prefix
    index_info        The index chosen for data access
    use_index         The id of index_info
    read_cost         Cost of this access method
    records           Number of records returned
    key_infix_len     Length of the key infix appended to the group prefix
    key_infix         Infix of constants from equality predicates
    parent_alloc      Memory pool for this and quick_prefix_select data
    is_index_scan     get the next different key not by jumping on it via
                      index read, but by scanning until the end of the 
                      rows with equal key value.

  RETURN
    None
*/

QUICK_GROUP_MIN_MAX_SELECT::
QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg,
                           bool have_max_arg, bool have_agg_distinct_arg,
                           KEY_PART_INFO *min_max_arg_part_arg,
                           uint group_prefix_len_arg, uint group_key_parts_arg,
                           uint used_key_parts_arg, KEY *index_info_arg,
                           uint use_index, double read_cost_arg,
                           ha_rows records_arg, uint key_infix_len_arg,
                           uchar *key_infix_arg, MEM_ROOT *parent_alloc,
                           bool is_index_scan_arg)
  :file(table->file), join(join_arg), index_info(index_info_arg),
   group_prefix_len(group_prefix_len_arg),
   group_key_parts(group_key_parts_arg), have_min(have_min_arg),
   have_max(have_max_arg), have_agg_distinct(have_agg_distinct_arg),
   seen_first_key(FALSE), min_max_arg_part(min_max_arg_part_arg),
   key_infix(key_infix_arg), key_infix_len(key_infix_len_arg),
   min_functions_it(NULL), max_functions_it(NULL),
   is_index_scan(is_index_scan_arg)
{
  head=       table;
  index=      use_index;
  record=     head->record[0];
  tmp_record= head->record[1];
  read_time= read_cost_arg;
  records= records_arg;
  used_key_parts= used_key_parts_arg;
  real_key_parts= used_key_parts_arg;
  real_prefix_len= group_prefix_len + key_infix_len;
  group_prefix= NULL;
  min_max_arg_len= min_max_arg_part ? min_max_arg_part->store_length : 0;

  /*
    We can't have parent_alloc set as the init function can't handle this case
    yet.
  */
  DBUG_ASSERT(!parent_alloc);
  if (!parent_alloc)
  {
    init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0,
                   MYF(MY_THREAD_SPECIFIC));
    join->thd->mem_root= &alloc;
  }
  else
    bzero(&alloc, sizeof(MEM_ROOT));            // ensure that it's not used
}


/*
  Do post-constructor initialization.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::init()
  
  DESCRIPTION
    The method performs initialization that cannot be done in the constructor
    such as memory allocations that may fail. It allocates memory for the
    group prefix and inifix buffers, and for the lists of MIN/MAX item to be
    updated during execution.

  RETURN
    0      OK
    other  Error code
*/

int QUICK_GROUP_MIN_MAX_SELECT::init()
{
  if (group_prefix) /* Already initialized. */
    return 0;
  
  /*
    We allocate one byte more to serve the case when the last field in
    the buffer is compared using uint3korr (e.g. a Field_newdate field)
  */
  if (!(last_prefix= (uchar*) alloc_root(&alloc, group_prefix_len+1)))
      return 1;
  /*
    We may use group_prefix to store keys with all select fields, so allocate
    enough space for it.
    We allocate one byte more to serve the case when the last field in
    the buffer is compared using uint3korr (e.g. a Field_newdate field)
  */
  if (!(group_prefix= (uchar*) alloc_root(&alloc,
                                          real_prefix_len+min_max_arg_len+1)))
    return 1;

  if (key_infix_len > 0)
  {
    /*
      The memory location pointed to by key_infix will be deleted soon, so
      allocate a new buffer and copy the key_infix into it.
    */
    uchar *tmp_key_infix= (uchar*) alloc_root(&alloc, key_infix_len);
    if (!tmp_key_infix)
      return 1;
    memcpy(tmp_key_infix, this->key_infix, key_infix_len);
    this->key_infix= tmp_key_infix;
  }

  if (min_max_arg_part)
  {
    if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16,
                              MYF(MY_THREAD_SPECIFIC)))
      return 1;

    if (have_min)
    {
      if (!(min_functions= new List<Item_sum>))
        return 1;
    }
    else
      min_functions= NULL;
    if (have_max)
    {
      if (!(max_functions= new List<Item_sum>))
        return 1;
    }
    else
      max_functions= NULL;

    Item_sum *min_max_item;
    Item_sum **func_ptr= join->sum_funcs;
    while ((min_max_item= *(func_ptr++)))
    {
      if (have_min && (min_max_item->sum_func() == Item_sum::MIN_FUNC))
        min_functions->push_back(min_max_item);
      else if (have_max && (min_max_item->sum_func() == Item_sum::MAX_FUNC))
        max_functions->push_back(min_max_item);
    }

    if (have_min)
    {
      if (!(min_functions_it= new List_iterator<Item_sum>(*min_functions)))
        return 1;
    }

    if (have_max)
    {
      if (!(max_functions_it= new List_iterator<Item_sum>(*max_functions)))
        return 1;
    }
  }
  else
    min_max_ranges.elements= 0;

  return 0;
}


QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT()
{
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT");
  if (file->inited != handler::NONE) 
  {
    DBUG_ASSERT(file == head->file);
    head->file->ha_end_keyread();
    /*
      There may be a code path when the same table was first accessed by index,
      then the index is closed, and the table is scanned (order by + loose scan).
    */
    file->ha_index_or_rnd_end();
  }
  if (min_max_arg_part)
    delete_dynamic(&min_max_ranges);
  free_root(&alloc,MYF(0));
  delete min_functions_it;
  delete max_functions_it;
  delete quick_prefix_select;
  DBUG_VOID_RETURN; 
}


/*
  Eventually create and add a new quick range object.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::add_range()
    sel_range  Range object from which a 

  NOTES
    Construct a new QUICK_RANGE object from a SEL_ARG object, and
    add it to the array min_max_ranges. If sel_arg is an infinite
    range, e.g. (x < 5 or x > 4), then skip it and do not construct
    a quick range.

  RETURN
    FALSE on success
    TRUE  otherwise
*/

bool QUICK_GROUP_MIN_MAX_SELECT::add_range(SEL_ARG *sel_range)
{
  QUICK_RANGE *range;
  uint range_flag= sel_range->min_flag | sel_range->max_flag;

  /* Skip (-inf,+inf) ranges, e.g. (x < 5 or x > 4). */
  if ((range_flag & NO_MIN_RANGE) && (range_flag & NO_MAX_RANGE))
    return FALSE;

  if (!(sel_range->min_flag & NO_MIN_RANGE) &&
      !(sel_range->max_flag & NO_MAX_RANGE))
  {
    if (sel_range->maybe_null &&
        sel_range->min_value[0] && sel_range->max_value[0])
      range_flag|= NULL_RANGE; /* IS NULL condition */
    else if (memcmp(sel_range->min_value, sel_range->max_value,
                    min_max_arg_len) == 0)
      range_flag|= EQ_RANGE;  /* equality condition */
  }
  range= new QUICK_RANGE(join->thd, sel_range->min_value, min_max_arg_len,
                         make_keypart_map(sel_range->part),
                         sel_range->max_value, min_max_arg_len,
                         make_keypart_map(sel_range->part),
                         range_flag);
  if (!range)
    return TRUE;
  if (insert_dynamic(&min_max_ranges, (uchar*)&range))
    return TRUE;
  return FALSE;
}


/*
  Opens the ranges if there are more conditions in quick_prefix_select than
  the ones used for jumping through the prefixes.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges()

  NOTES
    quick_prefix_select is made over the conditions on the whole key.
    It defines a number of ranges of length x. 
    However when jumping through the prefixes we use only the the first 
    few most significant keyparts in the range key. However if there
    are more keyparts to follow the ones we are using we must make the 
    condition on the key inclusive (because x < "ab" means 
    x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b').
    To achive the above we must turn off the NEAR_MIN/NEAR_MAX
*/
void QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges ()
{
  if (quick_prefix_select &&
      group_prefix_len < quick_prefix_select->max_used_key_length)
  {
    DYNAMIC_ARRAY *arr;
    uint inx;

    for (inx= 0, arr= &quick_prefix_select->ranges; inx < arr->elements; inx++)
    {
      QUICK_RANGE *range;

      get_dynamic(arr, (uchar*)&range, inx);
      range->flag &= ~(NEAR_MIN | NEAR_MAX);
    }
  }
}


/*
  Determine the total number and length of the keys that will be used for
  index lookup.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()

  DESCRIPTION
    The total length of the keys used for index lookup depends on whether
    there are any predicates referencing the min/max argument, and/or if
    the min/max argument field can be NULL.
    This function does an optimistic analysis whether the search key might
    be extended by a constant for the min/max keypart. It is 'optimistic'
    because during actual execution it may happen that a particular range
    is skipped, and then a shorter key will be used. However this is data
    dependent and can't be easily estimated here.

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
{
  max_used_key_length= real_prefix_len;
  if (min_max_ranges.elements > 0)
  {
    QUICK_RANGE *cur_range;
    if (have_min)
    { /* Check if the right-most range has a lower boundary. */
      get_dynamic(&min_max_ranges, (uchar*)&cur_range,
                  min_max_ranges.elements - 1);
      if (!(cur_range->flag & NO_MIN_RANGE))
      {
        max_used_key_length+= min_max_arg_len;
        used_key_parts++;
        return;
      }
    }
    if (have_max)
    { /* Check if the left-most range has an upper boundary. */
      get_dynamic(&min_max_ranges, (uchar*)&cur_range, 0);
      if (!(cur_range->flag & NO_MAX_RANGE))
      {
        max_used_key_length+= min_max_arg_len;
        used_key_parts++;
        return;
      }
    }
  }
  else if (have_min && min_max_arg_part &&
           min_max_arg_part->field->real_maybe_null())
  {
    /*
      If a MIN/MAX argument value is NULL, we can quickly determine
      that we're in the beginning of the next group, because NULLs
      are always < any other value. This allows us to quickly
      determine the end of the current group and jump to the next
      group (see next_min()) and thus effectively increases the
      usable key length.
    */
    max_used_key_length+= min_max_arg_len;
    used_key_parts++;
  }
}


/*
  Initialize a quick group min/max select for key retrieval.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::reset()

  DESCRIPTION
    Initialize the index chosen for access and find and store the prefix
    of the last group. The method is expensive since it performs disk access.

  RETURN
    0      OK
    other  Error code
*/

int QUICK_GROUP_MIN_MAX_SELECT::reset(void)
{
  int result;
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::reset");

  seen_first_key= FALSE;
  head->file->ha_start_keyread(index); /* We need only the key attributes */

  if ((result= file->ha_index_init(index,1)))
  {
    head->file->print_error(result, MYF(0));
    DBUG_RETURN(result);
  }
  if (quick_prefix_select && quick_prefix_select->reset())
    DBUG_RETURN(1);
  result= file->ha_index_last(record);
  if (result == HA_ERR_END_OF_FILE)
    DBUG_RETURN(0);
  /* Save the prefix of the last group. */
  key_copy(last_prefix, record, index_info, group_prefix_len);

  DBUG_RETURN(0);
}



/* 
  Get the next key containing the MIN and/or MAX key for the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::get_next()

  DESCRIPTION
    The method finds the next subsequent group of records that satisfies the
    query conditions and finds the keys that contain the MIN/MAX values for
    the key part referenced by the MIN/MAX function(s). Once a group and its
    MIN/MAX values are found, store these values in the Item_sum objects for
    the MIN/MAX functions. The rest of the values in the result row are stored
    in the Item_field::result_field of each select field. If the query does
    not contain MIN and/or MAX functions, then the function only finds the
    group prefix, which is a query answer itself.

  NOTES
    If both MIN and MAX are computed, then we use the fact that if there is
    no MIN key, there can't be a MAX key as well, so we can skip looking
    for a MAX key in this case.

  RETURN
    0                  on success
    HA_ERR_END_OF_FILE if returned all keys
    other              if some error occurred
*/

int QUICK_GROUP_MIN_MAX_SELECT::get_next()
{
  int min_res= 0;
  int max_res= 0;
#ifdef HPUX11
  /*
    volatile is required by a bug in the HP compiler due to which the
    last test of result fails.
  */
  volatile int result;
#else
  int result;
#endif
  int is_last_prefix= 0;

  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::get_next");

  /*
    Loop until a group is found that satisfies all query conditions or the last
    group is reached.
  */
  do
  {
    result= next_prefix();
    /*
      Check if this is the last group prefix. Notice that at this point
      this->record contains the current prefix in record format.
    */
    if (!result)
    {
      is_last_prefix= key_cmp(index_info->key_part, last_prefix,
                              group_prefix_len);
      DBUG_ASSERT(is_last_prefix <= 0);
    }
    else 
    {
      if (result == HA_ERR_KEY_NOT_FOUND)
        continue;
      break;
    }

    if (have_min)
    {
      min_res= next_min();
      if (min_res == 0)
        update_min_result();
    }
    /* If there is no MIN in the group, there is no MAX either. */
    if ((have_max && !have_min) ||
        (have_max && have_min && (min_res == 0)))
    {
      max_res= next_max();
      if (max_res == 0)
        update_max_result();
      /* If a MIN was found, a MAX must have been found as well. */
      DBUG_ASSERT((have_max && !have_min) ||
                  (have_max && have_min && (max_res == 0)));
    }
    /*
      If this is just a GROUP BY or DISTINCT without MIN or MAX and there
      are equality predicates for the key parts after the group, find the
      first sub-group with the extended prefix.
    */
    if (!have_min && !have_max && key_infix_len > 0)
      result= file->ha_index_read_map(record, group_prefix,
                                      make_prev_keypart_map(real_key_parts),
                                      HA_READ_KEY_EXACT);

    result= have_min ? min_res : have_max ? max_res : result;
  } while ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
           is_last_prefix != 0);

  if (result == HA_ERR_KEY_NOT_FOUND)
    result= HA_ERR_END_OF_FILE;

  DBUG_RETURN(result);
}


/*
  Retrieve the minimal key in the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_min()

  DESCRIPTION
    Find the minimal key within this group such that the key satisfies the query
    conditions and NULL semantics. The found key is loaded into this->record.

  IMPLEMENTATION
    Depending on the values of min_max_ranges.elements, key_infix_len, and
    whether there is a  NULL in the MIN field, this function may directly
    return without any data access. In this case we use the key loaded into
    this->record by the call to this->next_prefix() just before this call.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if no MIN key was found that fulfills all conditions.
    HA_ERR_END_OF_FILE   - "" -
    other                if some error occurred
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_min()
{
  int result= 0;
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_min");

  /* Find the MIN key using the eventually extended group prefix. */
  if (min_max_ranges.elements > 0)
  {
    if ((result= next_min_in_range()))
      DBUG_RETURN(result);
  }
  else
  {
    /* Apply the constant equality conditions to the non-group select fields */
    if (key_infix_len > 0)
    {
      if ((result=
           file->ha_index_read_map(record, group_prefix,
                                   make_prev_keypart_map(real_key_parts),
                                   HA_READ_KEY_EXACT)))
        DBUG_RETURN(result);
    }

    /*
      If the min/max argument field is NULL, skip subsequent rows in the same
      group with NULL in it. Notice that:
      - if the first row in a group doesn't have a NULL in the field, no row
      in the same group has (because NULL < any other value),
      - min_max_arg_part->field->ptr points to some place in 'record'.
    */
    if (min_max_arg_part && min_max_arg_part->field->is_null())
    {
      uchar *tmp_key_buff= (uchar*)my_alloca(max_used_key_length);
      /* Find the first subsequent record without NULL in the MIN/MAX field. */
      key_copy(tmp_key_buff, record, index_info, max_used_key_length);
      result= file->ha_index_read_map(record, tmp_key_buff,
                                      make_keypart_map(real_key_parts),
                                      HA_READ_AFTER_KEY);
      /*
        Check if the new record belongs to the current group by comparing its
        prefix with the group's prefix. If it is from the next group, then the
        whole group has NULLs in the MIN/MAX field, so use the first record in
        the group as a result.
        TODO:
        It is possible to reuse this new record as the result candidate for the
        next call to next_min(), and to save one lookup in the next call. For
        this add a new member 'this->next_group_prefix'.
      */
      if (!result)
      {
        if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
          key_restore(record, tmp_key_buff, index_info, 0);
      }
      else if (result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE)
        result= 0; /* There is a result in any case. */
      my_afree(tmp_key_buff);
    }
  }

  /*
    If the MIN attribute is non-nullable, this->record already contains the
    MIN key in the group, so just return.
  */
  DBUG_RETURN(result);
}


/* 
  Retrieve the maximal key in the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_max()

  DESCRIPTION
    Lookup the maximal key of the group, and store it into this->record.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if no MAX key was found that fulfills all conditions.
    HA_ERR_END_OF_FILE	 - "" -
    other                if some error occurred
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_max()
{
  int result;

  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_max");

  /* Get the last key in the (possibly extended) group. */
  if (min_max_ranges.elements > 0)
    result= next_max_in_range();
  else
    result= file->ha_index_read_map(record, group_prefix,
                                    make_prev_keypart_map(real_key_parts),
                                    HA_READ_PREFIX_LAST);
  DBUG_RETURN(result);
}


/** 
  Find the next different key value by skiping all the rows with the same key 
  value.

  Implements a specialized loose index access method for queries 
  containing aggregate functions with distinct of the form:
    SELECT [SUM|COUNT|AVG](DISTINCT a,...) FROM t
  This method comes to replace the index scan + Unique class 
  (distinct selection) for loose index scan that visits all the rows of a 
  covering index instead of jumping in the beginning of each group.
  TODO: Placeholder function. To be replaced by a handler API call

  @param is_index_scan     hint to use index scan instead of random index read 
                           to find the next different value.
  @param file              table handler
  @param key_part          group key to compare
  @param record            row data
  @param group_prefix      current key prefix data
  @param group_prefix_len  length of the current key prefix data
  @param group_key_parts   number of the current key prefix columns
  @return status
    @retval  0  success
    @retval !0  failure
*/

static int index_next_different (bool is_index_scan, handler *file, 
                                KEY_PART_INFO *key_part, uchar * record, 
                                const uchar * group_prefix,
                                uint group_prefix_len, 
                                uint group_key_parts)
{
  if (is_index_scan)
  {
    int result= 0;

    while (!key_cmp (key_part, group_prefix, group_prefix_len))
    {
      result= file->ha_index_next(record);
      if (result)
        return(result);
    }
    return result;
  }
  else
    return file->ha_index_read_map(record, group_prefix,
                                make_prev_keypart_map(group_key_parts),
                                HA_READ_AFTER_KEY);
}


/*
  Determine the prefix of the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_prefix()

  DESCRIPTION
    Determine the prefix of the next group that satisfies the query conditions.
    If there is a range condition referencing the group attributes, use a
    QUICK_RANGE_SELECT object to retrieve the *first* key that satisfies the
    condition. If there is a key infix of constants, append this infix
    immediately after the group attributes. The possibly extended prefix is
    stored in this->group_prefix. The first key of the found group is stored in
    this->record, on which relies this->next_min().

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if there is no key with the formed prefix
    HA_ERR_END_OF_FILE   if there are no more keys
    other                if some error occurred
*/
int QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
{
  int result;
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_prefix");

  if (quick_prefix_select)
  {
    uchar *cur_prefix= seen_first_key ? group_prefix : NULL;
    if ((result= quick_prefix_select->get_next_prefix(group_prefix_len,
                                                      group_key_parts, 
                                                      cur_prefix)))
      DBUG_RETURN(result);
    seen_first_key= TRUE;
  }
  else
  {
    if (!seen_first_key)
    {
      result= file->ha_index_first(record);
      if (result)
        DBUG_RETURN(result);
      seen_first_key= TRUE;
    }
    else
    {
      /* Load the first key in this group into record. */
      result= index_next_different (is_index_scan, file, index_info->key_part,
                            record, group_prefix, group_prefix_len, 
                            group_key_parts);
      if (result)
        DBUG_RETURN(result);
    }
  }

  /* Save the prefix of this group for subsequent calls. */
  key_copy(group_prefix, record, index_info, group_prefix_len);
  /* Append key_infix to group_prefix. */
  if (key_infix_len > 0)
    memcpy(group_prefix + group_prefix_len,
           key_infix, key_infix_len);

  DBUG_RETURN(0);
}


/**
  Allocate a temporary buffer, populate the buffer using the group prefix key
  and the min/max field key, and compare the result to the current key pointed
  by index_info.
  
  @param key    - the min or max field key
  @param length - length of "key"
*/
int
QUICK_GROUP_MIN_MAX_SELECT::cmp_min_max_key(const uchar *key, uint16 length)
{
  /*
    Allocate a buffer.
    Note, we allocate one extra byte, because some of Field_xxx::cmp(),
    e.g. Field_newdate::cmp(), use uint3korr() which actually read four bytes
    and then bit-and the read value with 0xFFFFFF.
    See "MDEV-7920 main.group_min_max fails ... with valgrind" for details.
  */
  uchar *buffer= (uchar*) my_alloca(real_prefix_len + min_max_arg_len + 1);
  /* Concatenate the group prefix key and the min/max field key */
  memcpy(buffer, group_prefix, real_prefix_len);
  memcpy(buffer + real_prefix_len, key, length);
  /* Compare the key pointed by key_info to the created key */
  int cmp_res= key_cmp(index_info->key_part, buffer,
                       real_prefix_len + min_max_arg_len);
  my_afree(buffer);
  return cmp_res;
}


/*
  Find the minimal key in a group that satisfies some range conditions for the
  min/max argument field.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()

  DESCRIPTION
    Given the sequence of ranges min_max_ranges, find the minimal key that is
    in the left-most possible range. If there is no such key, then the current
    group does not have a MIN key that satisfies the WHERE clause. If a key is
    found, its value is stored in this->record.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
                         the ranges
    HA_ERR_END_OF_FILE   - "" -
    other                if some error
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
{
  ha_rkey_function find_flag;
  key_part_map keypart_map;
  QUICK_RANGE *cur_range;
  bool found_null= FALSE;
  int result= HA_ERR_KEY_NOT_FOUND;

  DBUG_ASSERT(min_max_ranges.elements > 0);

  for (uint range_idx= 0; range_idx < min_max_ranges.elements; range_idx++)
  { /* Search from the left-most range to the right. */
    get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx);

    /*
      If the current value for the min/max argument is bigger than the right
      boundary of cur_range, there is no need to check this range.
    */
    if (range_idx != 0 && !(cur_range->flag & NO_MAX_RANGE) &&
        (key_cmp(min_max_arg_part, (const uchar*) cur_range->max_key,
                 min_max_arg_len) == 1))
      continue;

    if (cur_range->flag & NO_MIN_RANGE)
    {
      keypart_map= make_prev_keypart_map(real_key_parts);
      find_flag= HA_READ_KEY_EXACT;
    }
    else
    {
      /* Extend the search key with the lower boundary for this range. */
      memcpy(group_prefix + real_prefix_len, cur_range->min_key,
             cur_range->min_length);
      keypart_map= make_keypart_map(real_key_parts);
      find_flag= (cur_range->flag & (EQ_RANGE | NULL_RANGE)) ?
                 HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MIN) ?
                 HA_READ_AFTER_KEY : HA_READ_KEY_OR_NEXT;
    }

    result= file->ha_index_read_map(record, group_prefix, keypart_map,
                                    find_flag);
    if (result)
    {
      if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
          (cur_range->flag & (EQ_RANGE | NULL_RANGE)))
        continue; /* Check the next range. */

      /*
        In all other cases (HA_ERR_*, HA_READ_KEY_EXACT with NO_MIN_RANGE,
        HA_READ_AFTER_KEY, HA_READ_KEY_OR_NEXT) if the lookup failed for this
        range, it can't succeed for any other subsequent range.
      */
      break;
    }

    /* A key was found. */
    if (cur_range->flag & EQ_RANGE)
      break; /* No need to perform the checks below for equal keys. */

    if (cur_range->flag & NULL_RANGE)
    {
      /*
        Remember this key, and continue looking for a non-NULL key that
        satisfies some other condition.
      */
      memcpy(tmp_record, record, head->s->rec_buff_length);
      found_null= TRUE;
      continue;
    }

    /* Check if record belongs to the current group. */
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
    {
      result= HA_ERR_KEY_NOT_FOUND;
      continue;
    }

    /* If there is an upper limit, check if the found key is in the range. */
    if ( !(cur_range->flag & NO_MAX_RANGE) )
    {
      int cmp_res= cmp_min_max_key(cur_range->max_key, cur_range->max_length);
      /*
        The key is outside of the range if: 
        the interval is open and the key is equal to the maximum boundry
        or
        the key is greater than the maximum
      */
      if (((cur_range->flag & NEAR_MAX) && cmp_res == 0) ||
          cmp_res > 0)
      {
        result= HA_ERR_KEY_NOT_FOUND;
        continue;
      }
    }
    /* If we got to this point, the current key qualifies as MIN. */
    DBUG_ASSERT(result == 0);
    break;
  }
  /*
    If there was a key with NULL in the MIN/MAX field, and there was no other
    key without NULL from the same group that satisfies some other condition,
    then use the key with the NULL.
  */
  if (found_null && result)
  {
    memcpy(record, tmp_record, head->s->rec_buff_length);
    result= 0;
  }
  return result;
}


/*
  Find the maximal key in a group that satisfies some range conditions for the
  min/max argument field.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()

  DESCRIPTION
    Given the sequence of ranges min_max_ranges, find the maximal key that is
    in the right-most possible range. If there is no such key, then the current
    group does not have a MAX key that satisfies the WHERE clause. If a key is
    found, its value is stored in this->record.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
                         the ranges
    HA_ERR_END_OF_FILE   - "" -
    other                if some error
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
{
  ha_rkey_function find_flag;
  key_part_map keypart_map;
  QUICK_RANGE *cur_range;
  int result;

  DBUG_ASSERT(min_max_ranges.elements > 0);

  for (uint range_idx= min_max_ranges.elements; range_idx > 0; range_idx--)
  { /* Search from the right-most range to the left. */
    get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx - 1);

    /*
      If the current value for the min/max argument is smaller than the left
      boundary of cur_range, there is no need to check this range.
    */
    if (range_idx != min_max_ranges.elements &&
        !(cur_range->flag & NO_MIN_RANGE) &&
        (key_cmp(min_max_arg_part, (const uchar*) cur_range->min_key,
                 min_max_arg_len) == -1))
      continue;

    if (cur_range->flag & NO_MAX_RANGE)
    {
      keypart_map= make_prev_keypart_map(real_key_parts);
      find_flag= HA_READ_PREFIX_LAST;
    }
    else
    {
      /* Extend the search key with the upper boundary for this range. */
      memcpy(group_prefix + real_prefix_len, cur_range->max_key,
             cur_range->max_length);
      keypart_map= make_keypart_map(real_key_parts);
      find_flag= (cur_range->flag & EQ_RANGE) ?
                 HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MAX) ?
                 HA_READ_BEFORE_KEY : HA_READ_PREFIX_LAST_OR_PREV;
    }

    result= file->ha_index_read_map(record, group_prefix, keypart_map,
                                    find_flag);

    if (result)
    {
      if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
          (cur_range->flag & EQ_RANGE))
        continue; /* Check the next range. */

      /*
        In no key was found with this upper bound, there certainly are no keys
        in the ranges to the left.
      */
      return result;
    }
    /* A key was found. */
    if (cur_range->flag & EQ_RANGE)
      return 0; /* No need to perform the checks below for equal keys. */

    /* Check if record belongs to the current group. */
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
      continue;                                 // Row not found

    /* If there is a lower limit, check if the found key is in the range. */
    if ( !(cur_range->flag & NO_MIN_RANGE) )
    {
      int cmp_res= cmp_min_max_key(cur_range->min_key, cur_range->min_length);
      /*
        The key is outside of the range if: 
        the interval is open and the key is equal to the minimum boundry
        or
        the key is less than the minimum
      */
      if (((cur_range->flag & NEAR_MIN) && cmp_res == 0) ||
          cmp_res < 0)
        continue;
    }
    /* If we got to this point, the current key qualifies as MAX. */
    return result;
  }
  return HA_ERR_KEY_NOT_FOUND;
}


/*
  Update all MIN function results with the newly found value.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::update_min_result()

  DESCRIPTION
    The method iterates through all MIN functions and updates the result value
    of each function by calling Item_sum::reset(), which in turn picks the new
    result value from this->head->record[0], previously updated by
    next_min(). The updated value is stored in a member variable of each of the
    Item_sum objects, depending on the value type.

  IMPLEMENTATION
    The update must be done separately for MIN and MAX, immediately after
    next_min() was called and before next_max() is called, because both MIN and
    MAX take their result value from the same buffer this->head->record[0]
    (i.e.  this->record).

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
{
  Item_sum *min_func;

  min_functions_it->rewind();
  while ((min_func= (*min_functions_it)++))
    min_func->reset_and_add();
}


/*
  Update all MAX function results with the newly found value.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::update_max_result()

  DESCRIPTION
    The method iterates through all MAX functions and updates the result value
    of each function by calling Item_sum::reset(), which in turn picks the new
    result value from this->head->record[0], previously updated by
    next_max(). The updated value is stored in a member variable of each of the
    Item_sum objects, depending on the value type.

  IMPLEMENTATION
    The update must be done separately for MIN and MAX, immediately after
    next_max() was called, because both MIN and MAX take their result value
    from the same buffer this->head->record[0] (i.e.  this->record).

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
{
  Item_sum *max_func;

  max_functions_it->rewind();
  while ((max_func= (*max_functions_it)++))
    max_func->reset_and_add();
}


/*
  Append comma-separated list of keys this quick select uses to key_names;
  append comma-separated list of corresponding used lengths to used_lengths.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths()
    key_names    [out] Names of used indexes
    used_lengths [out] Corresponding lengths of the index names

  DESCRIPTION
    This method is used by select_describe to extract the names of the
    indexes used by a quick select.

*/

void QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths(String *key_names,
                                                      String *used_lengths)
{
  bool first= TRUE;

  add_key_and_length(key_names, used_lengths, &first);
}


#ifndef DBUG_OFF

static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
                           const char *msg)
{
  char buff[1024];
  DBUG_ENTER("print_sel_tree");

  String tmp(buff,sizeof(buff),&my_charset_bin);
  tmp.length(0);
  for (uint idx= 0; idx < param->keys; idx++)
  {
    if (tree_map->is_set(idx))
    {
      uint keynr= param->real_keynr[idx];
      if (tmp.length())
        tmp.append(',');
      tmp.append(param->table->key_info[keynr].name);
    }
  }
  if (!tmp.length())
    tmp.append(STRING_WITH_LEN("(empty)"));

  DBUG_PRINT("info", ("SEL_TREE: 0x%lx (%s)  scans: %s", (long) tree, msg,
                      tmp.c_ptr_safe()));

  DBUG_VOID_RETURN;
}


static void print_ror_scans_arr(TABLE *table, const char *msg,
                                struct st_ror_scan_info **start,
                                struct st_ror_scan_info **end)
{
  DBUG_ENTER("print_ror_scans_arr");

  char buff[1024];
  String tmp(buff,sizeof(buff),&my_charset_bin);
  tmp.length(0);
  for (;start != end; start++)
  {
    if (tmp.length())
      tmp.append(',');
    tmp.append(table->key_info[(*start)->keynr].name);
  }
  if (!tmp.length())
    tmp.append(STRING_WITH_LEN("(empty)"));
  DBUG_PRINT("info", ("ROR key scans (%s): %s", msg, tmp.c_ptr()));
  DBUG_VOID_RETURN;
}


/*****************************************************************************
** Print a quick range for debugging
** TODO:
** This should be changed to use a String to store each row instead
** of locking the DEBUG stream !
*****************************************************************************/

static void
print_key(KEY_PART *key_part, const uchar *key, uint used_length)
{
  char buff[1024];
  const uchar *key_end= key+used_length;
  uint store_length;
  TABLE *table= key_part->field->table;
  my_bitmap_map *old_sets[2];

  dbug_tmp_use_all_columns(table, old_sets, table->read_set, table->write_set);

  for (; key < key_end; key+=store_length, key_part++)
  {
    String tmp(buff,sizeof(buff),&my_charset_bin);
    Field *field=      key_part->field;
    store_length= key_part->store_length;

    if (field->real_maybe_null())
    {
      if (*key)
      {
	fwrite("NULL",sizeof(char),4,DBUG_FILE);
	continue;
      }
      key++;					// Skip null byte
      store_length--;
    }
    field->set_key_image(key, key_part->length);
    if (field->type() == MYSQL_TYPE_BIT)
      (void) field->val_int_as_str(&tmp, 1);
    else
      field->val_str(&tmp);
    fwrite(tmp.ptr(),sizeof(char),tmp.length(),DBUG_FILE);
    if (key+store_length < key_end)
      fputc('/',DBUG_FILE);
  }
  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
}


static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg)
{
  char buf[MAX_KEY/8+1];
  TABLE *table;
  my_bitmap_map *old_sets[2];
  DBUG_ENTER("print_quick");
  if (!quick)
    DBUG_VOID_RETURN;
  DBUG_LOCK_FILE;

  table= quick->head;
  dbug_tmp_use_all_columns(table, old_sets, table->read_set, table->write_set);
  quick->dbug_dump(0, TRUE);
  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);

  fprintf(DBUG_FILE,"other_keys: 0x%s:\n", needed_reg->print(buf));

  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}


void QUICK_RANGE_SELECT::dbug_dump(int indent, bool verbose)
{
  /* purecov: begin inspected */
  fprintf(DBUG_FILE, "%*squick range select, key %s, length: %d\n",
	  indent, "", head->key_info[index].name, max_used_key_length);

  if (verbose)
  {
    QUICK_RANGE *range;
    QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
    QUICK_RANGE **end_range= pr + ranges.elements;
    for (; pr != end_range; ++pr)
    {
      fprintf(DBUG_FILE, "%*s", indent + 2, "");
      range= *pr;
      if (!(range->flag & NO_MIN_RANGE))
      {
        print_key(key_parts, range->min_key, range->min_length);
        if (range->flag & NEAR_MIN)
	  fputs(" < ",DBUG_FILE);
        else
	  fputs(" <= ",DBUG_FILE);
      }
      fputs("X",DBUG_FILE);

      if (!(range->flag & NO_MAX_RANGE))
      {
        if (range->flag & NEAR_MAX)
	  fputs(" < ",DBUG_FILE);
        else
	  fputs(" <= ",DBUG_FILE);
        print_key(key_parts, range->max_key, range->max_length);
      }
      fputs("\n",DBUG_FILE);
    }
  }
  /* purecov: end */    
}

void QUICK_INDEX_SORT_SELECT::dbug_dump(int indent, bool verbose)
{
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  QUICK_RANGE_SELECT *quick;
  fprintf(DBUG_FILE, "%*squick index_merge select\n", indent, "");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((quick= it++))
    quick->dbug_dump(indent+2, verbose);
  if (pk_quick_select)
  {
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
    pk_quick_select->dbug_dump(indent+2, verbose);
  }
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
}

void QUICK_ROR_INTERSECT_SELECT::dbug_dump(int indent, bool verbose)
{
  List_iterator_fast<QUICK_SELECT_WITH_RECORD> it(quick_selects);
  QUICK_SELECT_WITH_RECORD *qr;
  fprintf(DBUG_FILE, "%*squick ROR-intersect select, %scovering\n",
          indent, "", need_to_fetch_row? "":"non-");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((qr= it++))
    qr->quick->dbug_dump(indent+2, verbose);
  if (cpk_quick)
  {
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
    cpk_quick->dbug_dump(indent+2, verbose);
  }
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
}

void QUICK_ROR_UNION_SELECT::dbug_dump(int indent, bool verbose)
{
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  QUICK_SELECT_I *quick;
  fprintf(DBUG_FILE, "%*squick ROR-union select\n", indent, "");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((quick= it++))
    quick->dbug_dump(indent+2, verbose);
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
}


/*
  Print quick select information to DBUG_FILE.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::dbug_dump()
    indent  Indentation offset
    verbose If TRUE show more detailed output.

  DESCRIPTION
    Print the contents of this quick select to DBUG_FILE. The method also
    calls dbug_dump() for the used quick select if any.

  IMPLEMENTATION
    Caller is responsible for locking DBUG_FILE before this call and unlocking
    it afterwards.

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::dbug_dump(int indent, bool verbose)
{
  fprintf(DBUG_FILE,
          "%*squick_group_min_max_select: index %s (%d), length: %d\n",
	  indent, "", index_info->name, index, max_used_key_length);
  if (key_infix_len > 0)
  {
    fprintf(DBUG_FILE, "%*susing key_infix with length %d:\n",
            indent, "", key_infix_len);
  }
  if (quick_prefix_select)
  {
    fprintf(DBUG_FILE, "%*susing quick_range_select:\n", indent, "");
    quick_prefix_select->dbug_dump(indent + 2, verbose);
  }
  if (min_max_ranges.elements > 0)
  {
    fprintf(DBUG_FILE, "%*susing %d quick_ranges for MIN/MAX:\n",
            indent, "", min_max_ranges.elements);
  }
}

#endif /* !DBUG_OFF */